Implement C++11 inheriting constructors.
[official-gcc.git] / gcc / cp / class.c
bloba478de8056376268f2c44beee008ce7aad3b6af5
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,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,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,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,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,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,gc) **);
185 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
186 VEC(constructor_elt,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,gc) **, tree *);
196 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
197 static tree dfs_fixup_binfo_vtbls (tree, void *);
198 static int record_subobject_offset (tree, tree, splay_tree);
199 static int check_subobject_offset (tree, tree, splay_tree);
200 static int walk_subobject_offsets (tree, subobject_offset_fn,
201 tree, splay_tree, tree, int);
202 static void record_subobject_offsets (tree, tree, splay_tree, bool);
203 static int layout_conflict_p (tree, tree, splay_tree, int);
204 static int splay_tree_compare_integer_csts (splay_tree_key k1,
205 splay_tree_key k2);
206 static void warn_about_ambiguous_bases (tree);
207 static bool type_requires_array_cookie (tree);
208 static bool contains_empty_class_p (tree);
209 static bool base_derived_from (tree, tree);
210 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
211 static tree end_of_base (tree);
212 static tree get_vcall_index (tree, tree);
214 /* Variables shared between class.c and call.c. */
216 int n_vtables = 0;
217 int n_vtable_entries = 0;
218 int n_vtable_searches = 0;
219 int n_vtable_elems = 0;
220 int n_convert_harshness = 0;
221 int n_compute_conversion_costs = 0;
222 int n_inner_fields_searched = 0;
224 /* Convert to or from a base subobject. EXPR is an expression of type
225 `A' or `A*', an expression of type `B' or `B*' is returned. To
226 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
227 the B base instance within A. To convert base A to derived B, CODE
228 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
229 In this latter case, A must not be a morally virtual base of B.
230 NONNULL is true if EXPR is known to be non-NULL (this is only
231 needed when EXPR is of pointer type). CV qualifiers are preserved
232 from EXPR. */
234 tree
235 build_base_path (enum tree_code code,
236 tree expr,
237 tree binfo,
238 int nonnull,
239 tsubst_flags_t complain)
241 tree v_binfo = NULL_TREE;
242 tree d_binfo = NULL_TREE;
243 tree probe;
244 tree offset;
245 tree target_type;
246 tree null_test = NULL;
247 tree ptr_target_type;
248 int fixed_type_p;
249 int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
250 bool has_empty = false;
251 bool virtual_access;
253 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
254 return error_mark_node;
256 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
258 d_binfo = probe;
259 if (is_empty_class (BINFO_TYPE (probe)))
260 has_empty = true;
261 if (!v_binfo && BINFO_VIRTUAL_P (probe))
262 v_binfo = probe;
265 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
266 if (want_pointer)
267 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
269 if (code == PLUS_EXPR
270 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
272 /* This can happen when adjust_result_of_qualified_name_lookup can't
273 find a unique base binfo in a call to a member function. We
274 couldn't give the diagnostic then since we might have been calling
275 a static member function, so we do it now. */
276 if (complain & tf_error)
278 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
279 ba_unique, NULL, complain);
280 gcc_assert (base == error_mark_node);
282 return error_mark_node;
285 gcc_assert ((code == MINUS_EXPR
286 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
287 || code == PLUS_EXPR);
289 if (binfo == d_binfo)
290 /* Nothing to do. */
291 return expr;
293 if (code == MINUS_EXPR && v_binfo)
295 if (complain & tf_error)
296 error ("cannot convert from base %qT to derived type %qT via "
297 "virtual base %qT", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
298 BINFO_TYPE (v_binfo));
299 return error_mark_node;
302 if (!want_pointer)
303 /* This must happen before the call to save_expr. */
304 expr = cp_build_addr_expr (expr, complain);
305 else
306 expr = mark_rvalue_use (expr);
308 offset = BINFO_OFFSET (binfo);
309 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
310 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
311 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
312 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
313 expression returned matches the input. */
314 target_type = cp_build_qualified_type
315 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
316 ptr_target_type = build_pointer_type (target_type);
318 /* Do we need to look in the vtable for the real offset? */
319 virtual_access = (v_binfo && fixed_type_p <= 0);
321 /* Don't bother with the calculations inside sizeof; they'll ICE if the
322 source type is incomplete and the pointer value doesn't matter. In a
323 template (even in fold_non_dependent_expr), we don't have vtables set
324 up properly yet, and the value doesn't matter there either; we're just
325 interested in the result of overload resolution. */
326 if (cp_unevaluated_operand != 0
327 || in_template_function ())
329 expr = build_nop (ptr_target_type, expr);
330 if (!want_pointer)
331 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
332 return expr;
335 /* If we're in an NSDMI, we don't have the full constructor context yet
336 that we need for converting to a virtual base, so just build a stub
337 CONVERT_EXPR and expand it later in bot_replace. */
338 if (virtual_access && fixed_type_p < 0
339 && current_scope () != current_function_decl)
341 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
342 CONVERT_EXPR_VBASE_PATH (expr) = true;
343 if (!want_pointer)
344 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
345 return expr;
348 /* Do we need to check for a null pointer? */
349 if (want_pointer && !nonnull)
351 /* If we know the conversion will not actually change the value
352 of EXPR, then we can avoid testing the expression for NULL.
353 We have to avoid generating a COMPONENT_REF for a base class
354 field, because other parts of the compiler know that such
355 expressions are always non-NULL. */
356 if (!virtual_access && integer_zerop (offset))
357 return build_nop (ptr_target_type, expr);
358 null_test = error_mark_node;
361 /* Protect against multiple evaluation if necessary. */
362 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
363 expr = save_expr (expr);
365 /* Now that we've saved expr, build the real null test. */
366 if (null_test)
368 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
369 null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
370 expr, zero);
373 /* If this is a simple base reference, express it as a COMPONENT_REF. */
374 if (code == PLUS_EXPR && !virtual_access
375 /* We don't build base fields for empty bases, and they aren't very
376 interesting to the optimizers anyway. */
377 && !has_empty)
379 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
380 expr = build_simple_base_path (expr, binfo);
381 if (want_pointer)
382 expr = build_address (expr);
383 target_type = TREE_TYPE (expr);
384 goto out;
387 if (virtual_access)
389 /* Going via virtual base V_BINFO. We need the static offset
390 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
391 V_BINFO. That offset is an entry in D_BINFO's vtable. */
392 tree v_offset;
394 if (fixed_type_p < 0 && in_base_initializer)
396 /* In a base member initializer, we cannot rely on the
397 vtable being set up. We have to indirect via the
398 vtt_parm. */
399 tree t;
401 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
402 t = build_pointer_type (t);
403 v_offset = convert (t, current_vtt_parm);
404 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
406 else
407 v_offset = build_vfield_ref (cp_build_indirect_ref (expr, RO_NULL,
408 complain),
409 TREE_TYPE (TREE_TYPE (expr)));
411 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
412 v_offset = build1 (NOP_EXPR,
413 build_pointer_type (ptrdiff_type_node),
414 v_offset);
415 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
416 TREE_CONSTANT (v_offset) = 1;
418 offset = convert_to_integer (ptrdiff_type_node,
419 size_diffop_loc (input_location, offset,
420 BINFO_OFFSET (v_binfo)));
422 if (!integer_zerop (offset))
423 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
425 if (fixed_type_p < 0)
426 /* Negative fixed_type_p means this is a constructor or destructor;
427 virtual base layout is fixed in in-charge [cd]tors, but not in
428 base [cd]tors. */
429 offset = build3 (COND_EXPR, ptrdiff_type_node,
430 build2 (EQ_EXPR, boolean_type_node,
431 current_in_charge_parm, integer_zero_node),
432 v_offset,
433 convert_to_integer (ptrdiff_type_node,
434 BINFO_OFFSET (binfo)));
435 else
436 offset = v_offset;
439 if (want_pointer)
440 target_type = ptr_target_type;
442 expr = build1 (NOP_EXPR, ptr_target_type, expr);
444 if (!integer_zerop (offset))
446 offset = fold_convert (sizetype, offset);
447 if (code == MINUS_EXPR)
448 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
449 expr = fold_build_pointer_plus (expr, offset);
451 else
452 null_test = NULL;
454 if (!want_pointer)
455 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
457 out:
458 if (null_test)
459 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
460 build_zero_cst (target_type));
462 return expr;
465 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
466 Perform a derived-to-base conversion by recursively building up a
467 sequence of COMPONENT_REFs to the appropriate base fields. */
469 static tree
470 build_simple_base_path (tree expr, tree binfo)
472 tree type = BINFO_TYPE (binfo);
473 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
474 tree field;
476 if (d_binfo == NULL_TREE)
478 tree temp;
480 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
482 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
483 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
484 an lvalue in the front end; only _DECLs and _REFs are lvalues
485 in the back end. */
486 temp = unary_complex_lvalue (ADDR_EXPR, expr);
487 if (temp)
488 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
490 return expr;
493 /* Recurse. */
494 expr = build_simple_base_path (expr, d_binfo);
496 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
497 field; field = DECL_CHAIN (field))
498 /* Is this the base field created by build_base_field? */
499 if (TREE_CODE (field) == FIELD_DECL
500 && DECL_FIELD_IS_BASE (field)
501 && TREE_TYPE (field) == type
502 /* If we're looking for a field in the most-derived class,
503 also check the field offset; we can have two base fields
504 of the same type if one is an indirect virtual base and one
505 is a direct non-virtual base. */
506 && (BINFO_INHERITANCE_CHAIN (d_binfo)
507 || tree_int_cst_equal (byte_position (field),
508 BINFO_OFFSET (binfo))))
510 /* We don't use build_class_member_access_expr here, as that
511 has unnecessary checks, and more importantly results in
512 recursive calls to dfs_walk_once. */
513 int type_quals = cp_type_quals (TREE_TYPE (expr));
515 expr = build3 (COMPONENT_REF,
516 cp_build_qualified_type (type, type_quals),
517 expr, field, NULL_TREE);
518 expr = fold_if_not_in_template (expr);
520 /* Mark the expression const or volatile, as appropriate.
521 Even though we've dealt with the type above, we still have
522 to mark the expression itself. */
523 if (type_quals & TYPE_QUAL_CONST)
524 TREE_READONLY (expr) = 1;
525 if (type_quals & TYPE_QUAL_VOLATILE)
526 TREE_THIS_VOLATILE (expr) = 1;
528 return expr;
531 /* Didn't find the base field?!? */
532 gcc_unreachable ();
535 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
536 type is a class type or a pointer to a class type. In the former
537 case, TYPE is also a class type; in the latter it is another
538 pointer type. If CHECK_ACCESS is true, an error message is emitted
539 if TYPE is inaccessible. If OBJECT has pointer type, the value is
540 assumed to be non-NULL. */
542 tree
543 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
544 tsubst_flags_t complain)
546 tree binfo;
547 tree object_type;
549 if (TYPE_PTR_P (TREE_TYPE (object)))
551 object_type = TREE_TYPE (TREE_TYPE (object));
552 type = TREE_TYPE (type);
554 else
555 object_type = TREE_TYPE (object);
557 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
558 NULL, complain);
559 if (!binfo || binfo == error_mark_node)
560 return error_mark_node;
562 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
565 /* EXPR is an expression with unqualified class type. BASE is a base
566 binfo of that class type. Returns EXPR, converted to the BASE
567 type. This function assumes that EXPR is the most derived class;
568 therefore virtual bases can be found at their static offsets. */
570 tree
571 convert_to_base_statically (tree expr, tree base)
573 tree expr_type;
575 expr_type = TREE_TYPE (expr);
576 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
578 /* If this is a non-empty base, use a COMPONENT_REF. */
579 if (!is_empty_class (BINFO_TYPE (base)))
580 return build_simple_base_path (expr, base);
582 /* We use fold_build2 and fold_convert below to simplify the trees
583 provided to the optimizers. It is not safe to call these functions
584 when processing a template because they do not handle C++-specific
585 trees. */
586 gcc_assert (!processing_template_decl);
587 expr = cp_build_addr_expr (expr, tf_warning_or_error);
588 if (!integer_zerop (BINFO_OFFSET (base)))
589 expr = fold_build_pointer_plus_loc (input_location,
590 expr, BINFO_OFFSET (base));
591 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
592 expr = build_fold_indirect_ref_loc (input_location, expr);
595 return expr;
599 tree
600 build_vfield_ref (tree datum, tree type)
602 tree vfield, vcontext;
604 if (datum == error_mark_node)
605 return error_mark_node;
607 /* First, convert to the requested type. */
608 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
609 datum = convert_to_base (datum, type, /*check_access=*/false,
610 /*nonnull=*/true, tf_warning_or_error);
612 /* Second, the requested type may not be the owner of its own vptr.
613 If not, convert to the base class that owns it. We cannot use
614 convert_to_base here, because VCONTEXT may appear more than once
615 in the inheritance hierarchy of TYPE, and thus direct conversion
616 between the types may be ambiguous. Following the path back up
617 one step at a time via primary bases avoids the problem. */
618 vfield = TYPE_VFIELD (type);
619 vcontext = DECL_CONTEXT (vfield);
620 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
622 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
623 type = TREE_TYPE (datum);
626 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
629 /* Given an object INSTANCE, return an expression which yields the
630 vtable element corresponding to INDEX. There are many special
631 cases for INSTANCE which we take care of here, mainly to avoid
632 creating extra tree nodes when we don't have to. */
634 static tree
635 build_vtbl_ref_1 (tree instance, tree idx)
637 tree aref;
638 tree vtbl = NULL_TREE;
640 /* Try to figure out what a reference refers to, and
641 access its virtual function table directly. */
643 int cdtorp = 0;
644 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
646 tree basetype = non_reference (TREE_TYPE (instance));
648 if (fixed_type && !cdtorp)
650 tree binfo = lookup_base (fixed_type, basetype,
651 ba_unique, NULL, tf_none);
652 if (binfo && binfo != error_mark_node)
653 vtbl = unshare_expr (BINFO_VTABLE (binfo));
656 if (!vtbl)
657 vtbl = build_vfield_ref (instance, basetype);
659 aref = build_array_ref (input_location, vtbl, idx);
660 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
662 return aref;
665 tree
666 build_vtbl_ref (tree instance, tree idx)
668 tree aref = build_vtbl_ref_1 (instance, idx);
670 return aref;
673 /* Given a stable object pointer INSTANCE_PTR, return an expression which
674 yields a function pointer corresponding to vtable element INDEX. */
676 tree
677 build_vfn_ref (tree instance_ptr, tree idx)
679 tree aref;
681 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
682 tf_warning_or_error),
683 idx);
685 /* When using function descriptors, the address of the
686 vtable entry is treated as a function pointer. */
687 if (TARGET_VTABLE_USES_DESCRIPTORS)
688 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
689 cp_build_addr_expr (aref, tf_warning_or_error));
691 /* Remember this as a method reference, for later devirtualization. */
692 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
694 return aref;
697 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
698 for the given TYPE. */
700 static tree
701 get_vtable_name (tree type)
703 return mangle_vtbl_for_type (type);
706 /* DECL is an entity associated with TYPE, like a virtual table or an
707 implicitly generated constructor. Determine whether or not DECL
708 should have external or internal linkage at the object file
709 level. This routine does not deal with COMDAT linkage and other
710 similar complexities; it simply sets TREE_PUBLIC if it possible for
711 entities in other translation units to contain copies of DECL, in
712 the abstract. */
714 void
715 set_linkage_according_to_type (tree /*type*/, tree decl)
717 TREE_PUBLIC (decl) = 1;
718 determine_visibility (decl);
721 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
722 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
723 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
725 static tree
726 build_vtable (tree class_type, tree name, tree vtable_type)
728 tree decl;
730 decl = build_lang_decl (VAR_DECL, name, vtable_type);
731 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
732 now to avoid confusion in mangle_decl. */
733 SET_DECL_ASSEMBLER_NAME (decl, name);
734 DECL_CONTEXT (decl) = class_type;
735 DECL_ARTIFICIAL (decl) = 1;
736 TREE_STATIC (decl) = 1;
737 TREE_READONLY (decl) = 1;
738 DECL_VIRTUAL_P (decl) = 1;
739 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
740 DECL_VTABLE_OR_VTT_P (decl) = 1;
741 /* At one time the vtable info was grabbed 2 words at a time. This
742 fails on sparc unless you have 8-byte alignment. (tiemann) */
743 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
744 DECL_ALIGN (decl));
745 set_linkage_according_to_type (class_type, decl);
746 /* The vtable has not been defined -- yet. */
747 DECL_EXTERNAL (decl) = 1;
748 DECL_NOT_REALLY_EXTERN (decl) = 1;
750 /* Mark the VAR_DECL node representing the vtable itself as a
751 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
752 is rather important that such things be ignored because any
753 effort to actually generate DWARF for them will run into
754 trouble when/if we encounter code like:
756 #pragma interface
757 struct S { virtual void member (); };
759 because the artificial declaration of the vtable itself (as
760 manufactured by the g++ front end) will say that the vtable is
761 a static member of `S' but only *after* the debug output for
762 the definition of `S' has already been output. This causes
763 grief because the DWARF entry for the definition of the vtable
764 will try to refer back to an earlier *declaration* of the
765 vtable as a static member of `S' and there won't be one. We
766 might be able to arrange to have the "vtable static member"
767 attached to the member list for `S' before the debug info for
768 `S' get written (which would solve the problem) but that would
769 require more intrusive changes to the g++ front end. */
770 DECL_IGNORED_P (decl) = 1;
772 return decl;
775 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
776 or even complete. If this does not exist, create it. If COMPLETE is
777 nonzero, then complete the definition of it -- that will render it
778 impossible to actually build the vtable, but is useful to get at those
779 which are known to exist in the runtime. */
781 tree
782 get_vtable_decl (tree type, int complete)
784 tree decl;
786 if (CLASSTYPE_VTABLES (type))
787 return CLASSTYPE_VTABLES (type);
789 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
790 CLASSTYPE_VTABLES (type) = decl;
792 if (complete)
794 DECL_EXTERNAL (decl) = 1;
795 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
798 return decl;
801 /* Build the primary virtual function table for TYPE. If BINFO is
802 non-NULL, build the vtable starting with the initial approximation
803 that it is the same as the one which is the head of the association
804 list. Returns a nonzero value if a new vtable is actually
805 created. */
807 static int
808 build_primary_vtable (tree binfo, tree type)
810 tree decl;
811 tree virtuals;
813 decl = get_vtable_decl (type, /*complete=*/0);
815 if (binfo)
817 if (BINFO_NEW_VTABLE_MARKED (binfo))
818 /* We have already created a vtable for this base, so there's
819 no need to do it again. */
820 return 0;
822 virtuals = copy_list (BINFO_VIRTUALS (binfo));
823 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
824 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
825 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
827 else
829 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
830 virtuals = NULL_TREE;
833 if (GATHER_STATISTICS)
835 n_vtables += 1;
836 n_vtable_elems += list_length (virtuals);
839 /* Initialize the association list for this type, based
840 on our first approximation. */
841 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
842 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
843 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
844 return 1;
847 /* Give BINFO a new virtual function table which is initialized
848 with a skeleton-copy of its original initialization. The only
849 entry that changes is the `delta' entry, so we can really
850 share a lot of structure.
852 FOR_TYPE is the most derived type which caused this table to
853 be needed.
855 Returns nonzero if we haven't met BINFO before.
857 The order in which vtables are built (by calling this function) for
858 an object must remain the same, otherwise a binary incompatibility
859 can result. */
861 static int
862 build_secondary_vtable (tree binfo)
864 if (BINFO_NEW_VTABLE_MARKED (binfo))
865 /* We already created a vtable for this base. There's no need to
866 do it again. */
867 return 0;
869 /* Remember that we've created a vtable for this BINFO, so that we
870 don't try to do so again. */
871 SET_BINFO_NEW_VTABLE_MARKED (binfo);
873 /* Make fresh virtual list, so we can smash it later. */
874 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
876 /* Secondary vtables are laid out as part of the same structure as
877 the primary vtable. */
878 BINFO_VTABLE (binfo) = NULL_TREE;
879 return 1;
882 /* Create a new vtable for BINFO which is the hierarchy dominated by
883 T. Return nonzero if we actually created a new vtable. */
885 static int
886 make_new_vtable (tree t, tree binfo)
888 if (binfo == TYPE_BINFO (t))
889 /* In this case, it is *type*'s vtable we are modifying. We start
890 with the approximation that its vtable is that of the
891 immediate base class. */
892 return build_primary_vtable (binfo, t);
893 else
894 /* This is our very own copy of `basetype' to play with. Later,
895 we will fill in all the virtual functions that override the
896 virtual functions in these base classes which are not defined
897 by the current type. */
898 return build_secondary_vtable (binfo);
901 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
902 (which is in the hierarchy dominated by T) list FNDECL as its
903 BV_FN. DELTA is the required constant adjustment from the `this'
904 pointer where the vtable entry appears to the `this' required when
905 the function is actually called. */
907 static void
908 modify_vtable_entry (tree t,
909 tree binfo,
910 tree fndecl,
911 tree delta,
912 tree *virtuals)
914 tree v;
916 v = *virtuals;
918 if (fndecl != BV_FN (v)
919 || !tree_int_cst_equal (delta, BV_DELTA (v)))
921 /* We need a new vtable for BINFO. */
922 if (make_new_vtable (t, binfo))
924 /* If we really did make a new vtable, we also made a copy
925 of the BINFO_VIRTUALS list. Now, we have to find the
926 corresponding entry in that list. */
927 *virtuals = BINFO_VIRTUALS (binfo);
928 while (BV_FN (*virtuals) != BV_FN (v))
929 *virtuals = TREE_CHAIN (*virtuals);
930 v = *virtuals;
933 BV_DELTA (v) = delta;
934 BV_VCALL_INDEX (v) = NULL_TREE;
935 BV_FN (v) = fndecl;
940 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
941 the USING_DECL naming METHOD. Returns true if the method could be
942 added to the method vec. */
944 bool
945 add_method (tree type, tree method, tree using_decl)
947 unsigned slot;
948 tree overload;
949 bool template_conv_p = false;
950 bool conv_p;
951 VEC(tree,gc) *method_vec;
952 bool complete_p;
953 bool insert_p = false;
954 tree current_fns;
955 tree fns;
957 if (method == error_mark_node)
958 return false;
960 complete_p = COMPLETE_TYPE_P (type);
961 conv_p = DECL_CONV_FN_P (method);
962 if (conv_p)
963 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
964 && DECL_TEMPLATE_CONV_FN_P (method));
966 method_vec = CLASSTYPE_METHOD_VEC (type);
967 if (!method_vec)
969 /* Make a new method vector. We start with 8 entries. We must
970 allocate at least two (for constructors and destructors), and
971 we're going to end up with an assignment operator at some
972 point as well. */
973 method_vec = VEC_alloc (tree, gc, 8);
974 /* Create slots for constructors and destructors. */
975 VEC_quick_push (tree, method_vec, NULL_TREE);
976 VEC_quick_push (tree, method_vec, NULL_TREE);
977 CLASSTYPE_METHOD_VEC (type) = method_vec;
980 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
981 grok_special_member_properties (method);
983 /* Constructors and destructors go in special slots. */
984 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
985 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
986 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
988 slot = CLASSTYPE_DESTRUCTOR_SLOT;
990 if (TYPE_FOR_JAVA (type))
992 if (!DECL_ARTIFICIAL (method))
993 error ("Java class %qT cannot have a destructor", type);
994 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
995 error ("Java class %qT cannot have an implicit non-trivial "
996 "destructor",
997 type);
1000 else
1002 tree m;
1004 insert_p = true;
1005 /* See if we already have an entry with this name. */
1006 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1007 VEC_iterate (tree, method_vec, slot, m);
1008 ++slot)
1010 m = OVL_CURRENT (m);
1011 if (template_conv_p)
1013 if (TREE_CODE (m) == TEMPLATE_DECL
1014 && DECL_TEMPLATE_CONV_FN_P (m))
1015 insert_p = false;
1016 break;
1018 if (conv_p && !DECL_CONV_FN_P (m))
1019 break;
1020 if (DECL_NAME (m) == DECL_NAME (method))
1022 insert_p = false;
1023 break;
1025 if (complete_p
1026 && !DECL_CONV_FN_P (m)
1027 && DECL_NAME (m) > DECL_NAME (method))
1028 break;
1031 current_fns = insert_p ? NULL_TREE : VEC_index (tree, method_vec, slot);
1033 /* Check to see if we've already got this method. */
1034 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1036 tree fn = OVL_CURRENT (fns);
1037 tree fn_type;
1038 tree method_type;
1039 tree parms1;
1040 tree parms2;
1042 if (TREE_CODE (fn) != TREE_CODE (method))
1043 continue;
1045 /* [over.load] Member function declarations with the
1046 same name and the same parameter types cannot be
1047 overloaded if any of them is a static member
1048 function declaration.
1050 [namespace.udecl] When a using-declaration brings names
1051 from a base class into a derived class scope, member
1052 functions in the derived class override and/or hide member
1053 functions with the same name and parameter types in a base
1054 class (rather than conflicting). */
1055 fn_type = TREE_TYPE (fn);
1056 method_type = TREE_TYPE (method);
1057 parms1 = TYPE_ARG_TYPES (fn_type);
1058 parms2 = TYPE_ARG_TYPES (method_type);
1060 /* Compare the quals on the 'this' parm. Don't compare
1061 the whole types, as used functions are treated as
1062 coming from the using class in overload resolution. */
1063 if (! DECL_STATIC_FUNCTION_P (fn)
1064 && ! DECL_STATIC_FUNCTION_P (method)
1065 && TREE_TYPE (TREE_VALUE (parms1)) != error_mark_node
1066 && TREE_TYPE (TREE_VALUE (parms2)) != error_mark_node
1067 && (cp_type_quals (TREE_TYPE (TREE_VALUE (parms1)))
1068 != cp_type_quals (TREE_TYPE (TREE_VALUE (parms2)))))
1069 continue;
1071 /* For templates, the return type and template parameters
1072 must be identical. */
1073 if (TREE_CODE (fn) == TEMPLATE_DECL
1074 && (!same_type_p (TREE_TYPE (fn_type),
1075 TREE_TYPE (method_type))
1076 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1077 DECL_TEMPLATE_PARMS (method))))
1078 continue;
1080 if (! DECL_STATIC_FUNCTION_P (fn))
1081 parms1 = TREE_CHAIN (parms1);
1082 if (! DECL_STATIC_FUNCTION_P (method))
1083 parms2 = TREE_CHAIN (parms2);
1085 if (compparms (parms1, parms2)
1086 && (!DECL_CONV_FN_P (fn)
1087 || same_type_p (TREE_TYPE (fn_type),
1088 TREE_TYPE (method_type))))
1090 if (DECL_INHERITED_CTOR_BASE (method))
1092 if (DECL_INHERITED_CTOR_BASE (fn))
1094 error_at (DECL_SOURCE_LOCATION (method),
1095 "%q#D inherited from %qT", method,
1096 DECL_INHERITED_CTOR_BASE (method));
1097 error_at (DECL_SOURCE_LOCATION (fn),
1098 "conflicts with version inherited from %qT",
1099 DECL_INHERITED_CTOR_BASE (fn));
1101 /* Otherwise defer to the other function. */
1102 return false;
1104 if (using_decl)
1106 if (DECL_CONTEXT (fn) == type)
1107 /* Defer to the local function. */
1108 return false;
1110 else
1112 error ("%q+#D cannot be overloaded", method);
1113 error ("with %q+#D", fn);
1116 /* We don't call duplicate_decls here to merge the
1117 declarations because that will confuse things if the
1118 methods have inline definitions. In particular, we
1119 will crash while processing the definitions. */
1120 return false;
1124 /* A class should never have more than one destructor. */
1125 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1126 return false;
1128 /* Add the new binding. */
1129 if (using_decl)
1131 overload = ovl_cons (method, current_fns);
1132 OVL_USED (overload) = true;
1134 else
1135 overload = build_overload (method, current_fns);
1137 if (conv_p)
1138 TYPE_HAS_CONVERSION (type) = 1;
1139 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1140 push_class_level_binding (DECL_NAME (method), overload);
1142 if (insert_p)
1144 bool reallocated;
1146 /* We only expect to add few methods in the COMPLETE_P case, so
1147 just make room for one more method in that case. */
1148 if (complete_p)
1149 reallocated = VEC_reserve_exact (tree, gc, method_vec, 1);
1150 else
1151 reallocated = VEC_reserve (tree, gc, method_vec, 1);
1152 if (reallocated)
1153 CLASSTYPE_METHOD_VEC (type) = method_vec;
1154 if (slot == VEC_length (tree, method_vec))
1155 VEC_quick_push (tree, method_vec, overload);
1156 else
1157 VEC_quick_insert (tree, method_vec, slot, overload);
1159 else
1160 /* Replace the current slot. */
1161 VEC_replace (tree, method_vec, slot, overload);
1162 return true;
1165 /* Subroutines of finish_struct. */
1167 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1168 legit, otherwise return 0. */
1170 static int
1171 alter_access (tree t, tree fdecl, tree access)
1173 tree elem;
1175 if (!DECL_LANG_SPECIFIC (fdecl))
1176 retrofit_lang_decl (fdecl);
1178 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1180 elem = purpose_member (t, DECL_ACCESS (fdecl));
1181 if (elem)
1183 if (TREE_VALUE (elem) != access)
1185 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1186 error ("conflicting access specifications for method"
1187 " %q+D, ignored", TREE_TYPE (fdecl));
1188 else
1189 error ("conflicting access specifications for field %qE, ignored",
1190 DECL_NAME (fdecl));
1192 else
1194 /* They're changing the access to the same thing they changed
1195 it to before. That's OK. */
1199 else
1201 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1202 tf_warning_or_error);
1203 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1204 return 1;
1206 return 0;
1209 /* Process the USING_DECL, which is a member of T. */
1211 static void
1212 handle_using_decl (tree using_decl, tree t)
1214 tree decl = USING_DECL_DECLS (using_decl);
1215 tree name = DECL_NAME (using_decl);
1216 tree access
1217 = TREE_PRIVATE (using_decl) ? access_private_node
1218 : TREE_PROTECTED (using_decl) ? access_protected_node
1219 : access_public_node;
1220 tree flist = NULL_TREE;
1221 tree old_value;
1223 gcc_assert (!processing_template_decl && decl);
1225 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1226 tf_warning_or_error);
1227 if (old_value)
1229 if (is_overloaded_fn (old_value))
1230 old_value = OVL_CURRENT (old_value);
1232 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1233 /* OK */;
1234 else
1235 old_value = NULL_TREE;
1238 cp_emit_debug_info_for_using (decl, USING_DECL_SCOPE (using_decl));
1240 if (is_overloaded_fn (decl))
1241 flist = decl;
1243 if (! old_value)
1245 else if (is_overloaded_fn (old_value))
1247 if (flist)
1248 /* It's OK to use functions from a base when there are functions with
1249 the same name already present in the current class. */;
1250 else
1252 error ("%q+D invalid in %q#T", using_decl, t);
1253 error (" because of local method %q+#D with same name",
1254 OVL_CURRENT (old_value));
1255 return;
1258 else if (!DECL_ARTIFICIAL (old_value))
1260 error ("%q+D invalid in %q#T", using_decl, t);
1261 error (" because of local member %q+#D with same name", old_value);
1262 return;
1265 /* Make type T see field decl FDECL with access ACCESS. */
1266 if (flist)
1267 for (; flist; flist = OVL_NEXT (flist))
1269 add_method (t, OVL_CURRENT (flist), using_decl);
1270 alter_access (t, OVL_CURRENT (flist), access);
1272 else
1273 alter_access (t, decl, access);
1276 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1277 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1278 properties of the bases. */
1280 static void
1281 check_bases (tree t,
1282 int* cant_have_const_ctor_p,
1283 int* no_const_asn_ref_p)
1285 int i;
1286 bool seen_non_virtual_nearly_empty_base_p = 0;
1287 int seen_tm_mask = 0;
1288 tree base_binfo;
1289 tree binfo;
1290 tree field = NULL_TREE;
1292 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1293 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1294 if (TREE_CODE (field) == FIELD_DECL)
1295 break;
1297 for (binfo = TYPE_BINFO (t), i = 0;
1298 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1300 tree basetype = TREE_TYPE (base_binfo);
1302 gcc_assert (COMPLETE_TYPE_P (basetype));
1304 if (CLASSTYPE_FINAL (basetype))
1305 error ("cannot derive from %<final%> base %qT in derived type %qT",
1306 basetype, t);
1308 /* If any base class is non-literal, so is the derived class. */
1309 if (!CLASSTYPE_LITERAL_P (basetype))
1310 CLASSTYPE_LITERAL_P (t) = false;
1312 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1313 here because the case of virtual functions but non-virtual
1314 dtor is handled in finish_struct_1. */
1315 if (!TYPE_POLYMORPHIC_P (basetype))
1316 warning (OPT_Weffc__,
1317 "base class %q#T has a non-virtual destructor", basetype);
1319 /* If the base class doesn't have copy constructors or
1320 assignment operators that take const references, then the
1321 derived class cannot have such a member automatically
1322 generated. */
1323 if (TYPE_HAS_COPY_CTOR (basetype)
1324 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1325 *cant_have_const_ctor_p = 1;
1326 if (TYPE_HAS_COPY_ASSIGN (basetype)
1327 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1328 *no_const_asn_ref_p = 1;
1330 if (BINFO_VIRTUAL_P (base_binfo))
1331 /* A virtual base does not effect nearly emptiness. */
1333 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1335 if (seen_non_virtual_nearly_empty_base_p)
1336 /* And if there is more than one nearly empty base, then the
1337 derived class is not nearly empty either. */
1338 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1339 else
1340 /* Remember we've seen one. */
1341 seen_non_virtual_nearly_empty_base_p = 1;
1343 else if (!is_empty_class (basetype))
1344 /* If the base class is not empty or nearly empty, then this
1345 class cannot be nearly empty. */
1346 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1348 /* A lot of properties from the bases also apply to the derived
1349 class. */
1350 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1351 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1352 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1353 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1354 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1355 || !TYPE_HAS_COPY_ASSIGN (basetype));
1356 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1357 || !TYPE_HAS_COPY_CTOR (basetype));
1358 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1359 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1360 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1361 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1362 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1363 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1364 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1365 || TYPE_HAS_COMPLEX_DFLT (basetype));
1367 /* A standard-layout class is a class that:
1369 * has no non-standard-layout base classes, */
1370 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1371 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1373 tree basefield;
1374 /* ...has no base classes of the same type as the first non-static
1375 data member... */
1376 if (field && DECL_CONTEXT (field) == t
1377 && (same_type_ignoring_top_level_qualifiers_p
1378 (TREE_TYPE (field), basetype)))
1379 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1380 else
1381 /* ...either has no non-static data members in the most-derived
1382 class and at most one base class with non-static data
1383 members, or has no base classes with non-static data
1384 members */
1385 for (basefield = TYPE_FIELDS (basetype); basefield;
1386 basefield = DECL_CHAIN (basefield))
1387 if (TREE_CODE (basefield) == FIELD_DECL)
1389 if (field)
1390 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1391 else
1392 field = basefield;
1393 break;
1397 /* Don't bother collecting tm attributes if transactional memory
1398 support is not enabled. */
1399 if (flag_tm)
1401 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1402 if (tm_attr)
1403 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1407 /* If one of the base classes had TM attributes, and the current class
1408 doesn't define its own, then the current class inherits one. */
1409 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1411 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1412 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1416 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1417 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1418 that have had a nearly-empty virtual primary base stolen by some
1419 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1420 T. */
1422 static void
1423 determine_primary_bases (tree t)
1425 unsigned i;
1426 tree primary = NULL_TREE;
1427 tree type_binfo = TYPE_BINFO (t);
1428 tree base_binfo;
1430 /* Determine the primary bases of our bases. */
1431 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1432 base_binfo = TREE_CHAIN (base_binfo))
1434 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1436 /* See if we're the non-virtual primary of our inheritance
1437 chain. */
1438 if (!BINFO_VIRTUAL_P (base_binfo))
1440 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1441 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1443 if (parent_primary
1444 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1445 BINFO_TYPE (parent_primary)))
1446 /* We are the primary binfo. */
1447 BINFO_PRIMARY_P (base_binfo) = 1;
1449 /* Determine if we have a virtual primary base, and mark it so.
1451 if (primary && BINFO_VIRTUAL_P (primary))
1453 tree this_primary = copied_binfo (primary, base_binfo);
1455 if (BINFO_PRIMARY_P (this_primary))
1456 /* Someone already claimed this base. */
1457 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1458 else
1460 tree delta;
1462 BINFO_PRIMARY_P (this_primary) = 1;
1463 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1465 /* A virtual binfo might have been copied from within
1466 another hierarchy. As we're about to use it as a
1467 primary base, make sure the offsets match. */
1468 delta = size_diffop_loc (input_location,
1469 convert (ssizetype,
1470 BINFO_OFFSET (base_binfo)),
1471 convert (ssizetype,
1472 BINFO_OFFSET (this_primary)));
1474 propagate_binfo_offsets (this_primary, delta);
1479 /* First look for a dynamic direct non-virtual base. */
1480 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1482 tree basetype = BINFO_TYPE (base_binfo);
1484 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1486 primary = base_binfo;
1487 goto found;
1491 /* A "nearly-empty" virtual base class can be the primary base
1492 class, if no non-virtual polymorphic base can be found. Look for
1493 a nearly-empty virtual dynamic base that is not already a primary
1494 base of something in the hierarchy. If there is no such base,
1495 just pick the first nearly-empty virtual base. */
1497 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1498 base_binfo = TREE_CHAIN (base_binfo))
1499 if (BINFO_VIRTUAL_P (base_binfo)
1500 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1502 if (!BINFO_PRIMARY_P (base_binfo))
1504 /* Found one that is not primary. */
1505 primary = base_binfo;
1506 goto found;
1508 else if (!primary)
1509 /* Remember the first candidate. */
1510 primary = base_binfo;
1513 found:
1514 /* If we've got a primary base, use it. */
1515 if (primary)
1517 tree basetype = BINFO_TYPE (primary);
1519 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1520 if (BINFO_PRIMARY_P (primary))
1521 /* We are stealing a primary base. */
1522 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1523 BINFO_PRIMARY_P (primary) = 1;
1524 if (BINFO_VIRTUAL_P (primary))
1526 tree delta;
1528 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1529 /* A virtual binfo might have been copied from within
1530 another hierarchy. As we're about to use it as a primary
1531 base, make sure the offsets match. */
1532 delta = size_diffop_loc (input_location, ssize_int (0),
1533 convert (ssizetype, BINFO_OFFSET (primary)));
1535 propagate_binfo_offsets (primary, delta);
1538 primary = TYPE_BINFO (basetype);
1540 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1541 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1542 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1546 /* Update the variant types of T. */
1548 void
1549 fixup_type_variants (tree t)
1551 tree variants;
1553 if (!t)
1554 return;
1556 for (variants = TYPE_NEXT_VARIANT (t);
1557 variants;
1558 variants = TYPE_NEXT_VARIANT (variants))
1560 /* These fields are in the _TYPE part of the node, not in
1561 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1562 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1563 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1564 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1565 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1567 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1569 TYPE_BINFO (variants) = TYPE_BINFO (t);
1571 /* Copy whatever these are holding today. */
1572 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1573 TYPE_METHODS (variants) = TYPE_METHODS (t);
1574 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1578 /* Early variant fixups: we apply attributes at the beginning of the class
1579 definition, and we need to fix up any variants that have already been
1580 made via elaborated-type-specifier so that check_qualified_type works. */
1582 void
1583 fixup_attribute_variants (tree t)
1585 tree variants;
1587 if (!t)
1588 return;
1590 for (variants = TYPE_NEXT_VARIANT (t);
1591 variants;
1592 variants = TYPE_NEXT_VARIANT (variants))
1594 /* These are the two fields that check_qualified_type looks at and
1595 are affected by attributes. */
1596 TYPE_ATTRIBUTES (variants) = TYPE_ATTRIBUTES (t);
1597 TYPE_ALIGN (variants) = TYPE_ALIGN (t);
1601 /* Set memoizing fields and bits of T (and its variants) for later
1602 use. */
1604 static void
1605 finish_struct_bits (tree t)
1607 /* Fix up variants (if any). */
1608 fixup_type_variants (t);
1610 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1611 /* For a class w/o baseclasses, 'finish_struct' has set
1612 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1613 Similarly for a class whose base classes do not have vtables.
1614 When neither of these is true, we might have removed abstract
1615 virtuals (by providing a definition), added some (by declaring
1616 new ones), or redeclared ones from a base class. We need to
1617 recalculate what's really an abstract virtual at this point (by
1618 looking in the vtables). */
1619 get_pure_virtuals (t);
1621 /* If this type has a copy constructor or a destructor, force its
1622 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
1623 nonzero. This will cause it to be passed by invisible reference
1624 and prevent it from being returned in a register. */
1625 if (type_has_nontrivial_copy_init (t)
1626 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1628 tree variants;
1629 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1630 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1632 SET_TYPE_MODE (variants, BLKmode);
1633 TREE_ADDRESSABLE (variants) = 1;
1638 /* Issue warnings about T having private constructors, but no friends,
1639 and so forth.
1641 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1642 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1643 non-private static member functions. */
1645 static void
1646 maybe_warn_about_overly_private_class (tree t)
1648 int has_member_fn = 0;
1649 int has_nonprivate_method = 0;
1650 tree fn;
1652 if (!warn_ctor_dtor_privacy
1653 /* If the class has friends, those entities might create and
1654 access instances, so we should not warn. */
1655 || (CLASSTYPE_FRIEND_CLASSES (t)
1656 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1657 /* We will have warned when the template was declared; there's
1658 no need to warn on every instantiation. */
1659 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1660 /* There's no reason to even consider warning about this
1661 class. */
1662 return;
1664 /* We only issue one warning, if more than one applies, because
1665 otherwise, on code like:
1667 class A {
1668 // Oops - forgot `public:'
1669 A();
1670 A(const A&);
1671 ~A();
1674 we warn several times about essentially the same problem. */
1676 /* Check to see if all (non-constructor, non-destructor) member
1677 functions are private. (Since there are no friends or
1678 non-private statics, we can't ever call any of the private member
1679 functions.) */
1680 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
1681 /* We're not interested in compiler-generated methods; they don't
1682 provide any way to call private members. */
1683 if (!DECL_ARTIFICIAL (fn))
1685 if (!TREE_PRIVATE (fn))
1687 if (DECL_STATIC_FUNCTION_P (fn))
1688 /* A non-private static member function is just like a
1689 friend; it can create and invoke private member
1690 functions, and be accessed without a class
1691 instance. */
1692 return;
1694 has_nonprivate_method = 1;
1695 /* Keep searching for a static member function. */
1697 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1698 has_member_fn = 1;
1701 if (!has_nonprivate_method && has_member_fn)
1703 /* There are no non-private methods, and there's at least one
1704 private member function that isn't a constructor or
1705 destructor. (If all the private members are
1706 constructors/destructors we want to use the code below that
1707 issues error messages specifically referring to
1708 constructors/destructors.) */
1709 unsigned i;
1710 tree binfo = TYPE_BINFO (t);
1712 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
1713 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
1715 has_nonprivate_method = 1;
1716 break;
1718 if (!has_nonprivate_method)
1720 warning (OPT_Wctor_dtor_privacy,
1721 "all member functions in class %qT are private", t);
1722 return;
1726 /* Even if some of the member functions are non-private, the class
1727 won't be useful for much if all the constructors or destructors
1728 are private: such an object can never be created or destroyed. */
1729 fn = CLASSTYPE_DESTRUCTORS (t);
1730 if (fn && TREE_PRIVATE (fn))
1732 warning (OPT_Wctor_dtor_privacy,
1733 "%q#T only defines a private destructor and has no friends",
1735 return;
1738 /* Warn about classes that have private constructors and no friends. */
1739 if (TYPE_HAS_USER_CONSTRUCTOR (t)
1740 /* Implicitly generated constructors are always public. */
1741 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
1742 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
1744 int nonprivate_ctor = 0;
1746 /* If a non-template class does not define a copy
1747 constructor, one is defined for it, enabling it to avoid
1748 this warning. For a template class, this does not
1749 happen, and so we would normally get a warning on:
1751 template <class T> class C { private: C(); };
1753 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
1754 complete non-template or fully instantiated classes have this
1755 flag set. */
1756 if (!TYPE_HAS_COPY_CTOR (t))
1757 nonprivate_ctor = 1;
1758 else
1759 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
1761 tree ctor = OVL_CURRENT (fn);
1762 /* Ideally, we wouldn't count copy constructors (or, in
1763 fact, any constructor that takes an argument of the
1764 class type as a parameter) because such things cannot
1765 be used to construct an instance of the class unless
1766 you already have one. But, for now at least, we're
1767 more generous. */
1768 if (! TREE_PRIVATE (ctor))
1770 nonprivate_ctor = 1;
1771 break;
1775 if (nonprivate_ctor == 0)
1777 warning (OPT_Wctor_dtor_privacy,
1778 "%q#T only defines private constructors and has no friends",
1780 return;
1785 static struct {
1786 gt_pointer_operator new_value;
1787 void *cookie;
1788 } resort_data;
1790 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1792 static int
1793 method_name_cmp (const void* m1_p, const void* m2_p)
1795 const tree *const m1 = (const tree *) m1_p;
1796 const tree *const m2 = (const tree *) m2_p;
1798 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1799 return 0;
1800 if (*m1 == NULL_TREE)
1801 return -1;
1802 if (*m2 == NULL_TREE)
1803 return 1;
1804 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1805 return -1;
1806 return 1;
1809 /* This routine compares two fields like method_name_cmp but using the
1810 pointer operator in resort_field_decl_data. */
1812 static int
1813 resort_method_name_cmp (const void* m1_p, const void* m2_p)
1815 const tree *const m1 = (const tree *) m1_p;
1816 const tree *const m2 = (const tree *) m2_p;
1817 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1818 return 0;
1819 if (*m1 == NULL_TREE)
1820 return -1;
1821 if (*m2 == NULL_TREE)
1822 return 1;
1824 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
1825 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
1826 resort_data.new_value (&d1, resort_data.cookie);
1827 resort_data.new_value (&d2, resort_data.cookie);
1828 if (d1 < d2)
1829 return -1;
1831 return 1;
1834 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
1836 void
1837 resort_type_method_vec (void* obj,
1838 void* /*orig_obj*/,
1839 gt_pointer_operator new_value,
1840 void* cookie)
1842 VEC(tree,gc) *method_vec = (VEC(tree,gc) *) obj;
1843 int len = VEC_length (tree, method_vec);
1844 size_t slot;
1845 tree fn;
1847 /* The type conversion ops have to live at the front of the vec, so we
1848 can't sort them. */
1849 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1850 VEC_iterate (tree, method_vec, slot, fn);
1851 ++slot)
1852 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1853 break;
1855 if (len - slot > 1)
1857 resort_data.new_value = new_value;
1858 resort_data.cookie = cookie;
1859 qsort (VEC_address (tree, method_vec) + slot, len - slot, sizeof (tree),
1860 resort_method_name_cmp);
1864 /* Warn about duplicate methods in fn_fields.
1866 Sort methods that are not special (i.e., constructors, destructors,
1867 and type conversion operators) so that we can find them faster in
1868 search. */
1870 static void
1871 finish_struct_methods (tree t)
1873 tree fn_fields;
1874 VEC(tree,gc) *method_vec;
1875 int slot, len;
1877 method_vec = CLASSTYPE_METHOD_VEC (t);
1878 if (!method_vec)
1879 return;
1881 len = VEC_length (tree, method_vec);
1883 /* Clear DECL_IN_AGGR_P for all functions. */
1884 for (fn_fields = TYPE_METHODS (t); fn_fields;
1885 fn_fields = DECL_CHAIN (fn_fields))
1886 DECL_IN_AGGR_P (fn_fields) = 0;
1888 /* Issue warnings about private constructors and such. If there are
1889 no methods, then some public defaults are generated. */
1890 maybe_warn_about_overly_private_class (t);
1892 /* The type conversion ops have to live at the front of the vec, so we
1893 can't sort them. */
1894 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1895 VEC_iterate (tree, method_vec, slot, fn_fields);
1896 ++slot)
1897 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
1898 break;
1899 if (len - slot > 1)
1900 qsort (VEC_address (tree, method_vec) + slot,
1901 len-slot, sizeof (tree), method_name_cmp);
1904 /* Make BINFO's vtable have N entries, including RTTI entries,
1905 vbase and vcall offsets, etc. Set its type and call the back end
1906 to lay it out. */
1908 static void
1909 layout_vtable_decl (tree binfo, int n)
1911 tree atype;
1912 tree vtable;
1914 atype = build_array_of_n_type (vtable_entry_type, n);
1915 layout_type (atype);
1917 /* We may have to grow the vtable. */
1918 vtable = get_vtbl_decl_for_binfo (binfo);
1919 if (!same_type_p (TREE_TYPE (vtable), atype))
1921 TREE_TYPE (vtable) = atype;
1922 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
1923 layout_decl (vtable, 0);
1927 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
1928 have the same signature. */
1931 same_signature_p (const_tree fndecl, const_tree base_fndecl)
1933 /* One destructor overrides another if they are the same kind of
1934 destructor. */
1935 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
1936 && special_function_p (base_fndecl) == special_function_p (fndecl))
1937 return 1;
1938 /* But a non-destructor never overrides a destructor, nor vice
1939 versa, nor do different kinds of destructors override
1940 one-another. For example, a complete object destructor does not
1941 override a deleting destructor. */
1942 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
1943 return 0;
1945 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
1946 || (DECL_CONV_FN_P (fndecl)
1947 && DECL_CONV_FN_P (base_fndecl)
1948 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
1949 DECL_CONV_FN_TYPE (base_fndecl))))
1951 tree types, base_types;
1952 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1953 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
1954 if ((cp_type_quals (TREE_TYPE (TREE_VALUE (base_types)))
1955 == cp_type_quals (TREE_TYPE (TREE_VALUE (types))))
1956 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
1957 return 1;
1959 return 0;
1962 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
1963 subobject. */
1965 static bool
1966 base_derived_from (tree derived, tree base)
1968 tree probe;
1970 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1972 if (probe == derived)
1973 return true;
1974 else if (BINFO_VIRTUAL_P (probe))
1975 /* If we meet a virtual base, we can't follow the inheritance
1976 any more. See if the complete type of DERIVED contains
1977 such a virtual base. */
1978 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
1979 != NULL_TREE);
1981 return false;
1984 typedef struct find_final_overrider_data_s {
1985 /* The function for which we are trying to find a final overrider. */
1986 tree fn;
1987 /* The base class in which the function was declared. */
1988 tree declaring_base;
1989 /* The candidate overriders. */
1990 tree candidates;
1991 /* Path to most derived. */
1992 VEC(tree,heap) *path;
1993 } find_final_overrider_data;
1995 /* Add the overrider along the current path to FFOD->CANDIDATES.
1996 Returns true if an overrider was found; false otherwise. */
1998 static bool
1999 dfs_find_final_overrider_1 (tree binfo,
2000 find_final_overrider_data *ffod,
2001 unsigned depth)
2003 tree method;
2005 /* If BINFO is not the most derived type, try a more derived class.
2006 A definition there will overrider a definition here. */
2007 if (depth)
2009 depth--;
2010 if (dfs_find_final_overrider_1
2011 (VEC_index (tree, ffod->path, depth), ffod, depth))
2012 return true;
2015 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2016 if (method)
2018 tree *candidate = &ffod->candidates;
2020 /* Remove any candidates overridden by this new function. */
2021 while (*candidate)
2023 /* If *CANDIDATE overrides METHOD, then METHOD
2024 cannot override anything else on the list. */
2025 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2026 return true;
2027 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2028 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2029 *candidate = TREE_CHAIN (*candidate);
2030 else
2031 candidate = &TREE_CHAIN (*candidate);
2034 /* Add the new function. */
2035 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2036 return true;
2039 return false;
2042 /* Called from find_final_overrider via dfs_walk. */
2044 static tree
2045 dfs_find_final_overrider_pre (tree binfo, void *data)
2047 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2049 if (binfo == ffod->declaring_base)
2050 dfs_find_final_overrider_1 (binfo, ffod, VEC_length (tree, ffod->path));
2051 VEC_safe_push (tree, heap, ffod->path, binfo);
2053 return NULL_TREE;
2056 static tree
2057 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2059 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2060 VEC_pop (tree, ffod->path);
2062 return NULL_TREE;
2065 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2066 FN and whose TREE_VALUE is the binfo for the base where the
2067 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2068 DERIVED) is the base object in which FN is declared. */
2070 static tree
2071 find_final_overrider (tree derived, tree binfo, tree fn)
2073 find_final_overrider_data ffod;
2075 /* Getting this right is a little tricky. This is valid:
2077 struct S { virtual void f (); };
2078 struct T { virtual void f (); };
2079 struct U : public S, public T { };
2081 even though calling `f' in `U' is ambiguous. But,
2083 struct R { virtual void f(); };
2084 struct S : virtual public R { virtual void f (); };
2085 struct T : virtual public R { virtual void f (); };
2086 struct U : public S, public T { };
2088 is not -- there's no way to decide whether to put `S::f' or
2089 `T::f' in the vtable for `R'.
2091 The solution is to look at all paths to BINFO. If we find
2092 different overriders along any two, then there is a problem. */
2093 if (DECL_THUNK_P (fn))
2094 fn = THUNK_TARGET (fn);
2096 /* Determine the depth of the hierarchy. */
2097 ffod.fn = fn;
2098 ffod.declaring_base = binfo;
2099 ffod.candidates = NULL_TREE;
2100 ffod.path = VEC_alloc (tree, heap, 30);
2102 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2103 dfs_find_final_overrider_post, &ffod);
2105 VEC_free (tree, heap, ffod.path);
2107 /* If there was no winner, issue an error message. */
2108 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2109 return error_mark_node;
2111 return ffod.candidates;
2114 /* Return the index of the vcall offset for FN when TYPE is used as a
2115 virtual base. */
2117 static tree
2118 get_vcall_index (tree fn, tree type)
2120 VEC(tree_pair_s,gc) *indices = CLASSTYPE_VCALL_INDICES (type);
2121 tree_pair_p p;
2122 unsigned ix;
2124 FOR_EACH_VEC_ELT (tree_pair_s, indices, ix, p)
2125 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2126 || same_signature_p (fn, p->purpose))
2127 return p->value;
2129 /* There should always be an appropriate index. */
2130 gcc_unreachable ();
2133 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2134 dominated by T. FN is the old function; VIRTUALS points to the
2135 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2136 of that entry in the list. */
2138 static void
2139 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2140 unsigned ix)
2142 tree b;
2143 tree overrider;
2144 tree delta;
2145 tree virtual_base;
2146 tree first_defn;
2147 tree overrider_fn, overrider_target;
2148 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2149 tree over_return, base_return;
2150 bool lost = false;
2152 /* Find the nearest primary base (possibly binfo itself) which defines
2153 this function; this is the class the caller will convert to when
2154 calling FN through BINFO. */
2155 for (b = binfo; ; b = get_primary_binfo (b))
2157 gcc_assert (b);
2158 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2159 break;
2161 /* The nearest definition is from a lost primary. */
2162 if (BINFO_LOST_PRIMARY_P (b))
2163 lost = true;
2165 first_defn = b;
2167 /* Find the final overrider. */
2168 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2169 if (overrider == error_mark_node)
2171 error ("no unique final overrider for %qD in %qT", target_fn, t);
2172 return;
2174 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2176 /* Check for adjusting covariant return types. */
2177 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2178 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2180 if (POINTER_TYPE_P (over_return)
2181 && TREE_CODE (over_return) == TREE_CODE (base_return)
2182 && CLASS_TYPE_P (TREE_TYPE (over_return))
2183 && CLASS_TYPE_P (TREE_TYPE (base_return))
2184 /* If the overrider is invalid, don't even try. */
2185 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2187 /* If FN is a covariant thunk, we must figure out the adjustment
2188 to the final base FN was converting to. As OVERRIDER_TARGET might
2189 also be converting to the return type of FN, we have to
2190 combine the two conversions here. */
2191 tree fixed_offset, virtual_offset;
2193 over_return = TREE_TYPE (over_return);
2194 base_return = TREE_TYPE (base_return);
2196 if (DECL_THUNK_P (fn))
2198 gcc_assert (DECL_RESULT_THUNK_P (fn));
2199 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2200 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2202 else
2203 fixed_offset = virtual_offset = NULL_TREE;
2205 if (virtual_offset)
2206 /* Find the equivalent binfo within the return type of the
2207 overriding function. We will want the vbase offset from
2208 there. */
2209 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2210 over_return);
2211 else if (!same_type_ignoring_top_level_qualifiers_p
2212 (over_return, base_return))
2214 /* There was no existing virtual thunk (which takes
2215 precedence). So find the binfo of the base function's
2216 return type within the overriding function's return type.
2217 We cannot call lookup base here, because we're inside a
2218 dfs_walk, and will therefore clobber the BINFO_MARKED
2219 flags. Fortunately we know the covariancy is valid (it
2220 has already been checked), so we can just iterate along
2221 the binfos, which have been chained in inheritance graph
2222 order. Of course it is lame that we have to repeat the
2223 search here anyway -- we should really be caching pieces
2224 of the vtable and avoiding this repeated work. */
2225 tree thunk_binfo, base_binfo;
2227 /* Find the base binfo within the overriding function's
2228 return type. We will always find a thunk_binfo, except
2229 when the covariancy is invalid (which we will have
2230 already diagnosed). */
2231 for (base_binfo = TYPE_BINFO (base_return),
2232 thunk_binfo = TYPE_BINFO (over_return);
2233 thunk_binfo;
2234 thunk_binfo = TREE_CHAIN (thunk_binfo))
2235 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2236 BINFO_TYPE (base_binfo)))
2237 break;
2239 /* See if virtual inheritance is involved. */
2240 for (virtual_offset = thunk_binfo;
2241 virtual_offset;
2242 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2243 if (BINFO_VIRTUAL_P (virtual_offset))
2244 break;
2246 if (virtual_offset
2247 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2249 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2251 if (virtual_offset)
2253 /* We convert via virtual base. Adjust the fixed
2254 offset to be from there. */
2255 offset =
2256 size_diffop (offset,
2257 convert (ssizetype,
2258 BINFO_OFFSET (virtual_offset)));
2260 if (fixed_offset)
2261 /* There was an existing fixed offset, this must be
2262 from the base just converted to, and the base the
2263 FN was thunking to. */
2264 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2265 else
2266 fixed_offset = offset;
2270 if (fixed_offset || virtual_offset)
2271 /* Replace the overriding function with a covariant thunk. We
2272 will emit the overriding function in its own slot as
2273 well. */
2274 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2275 fixed_offset, virtual_offset);
2277 else
2278 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2279 !DECL_THUNK_P (fn));
2281 /* If we need a covariant thunk, then we may need to adjust first_defn.
2282 The ABI specifies that the thunks emitted with a function are
2283 determined by which bases the function overrides, so we need to be
2284 sure that we're using a thunk for some overridden base; even if we
2285 know that the necessary this adjustment is zero, there may not be an
2286 appropriate zero-this-adjusment thunk for us to use since thunks for
2287 overriding virtual bases always use the vcall offset.
2289 Furthermore, just choosing any base that overrides this function isn't
2290 quite right, as this slot won't be used for calls through a type that
2291 puts a covariant thunk here. Calling the function through such a type
2292 will use a different slot, and that slot is the one that determines
2293 the thunk emitted for that base.
2295 So, keep looking until we find the base that we're really overriding
2296 in this slot: the nearest primary base that doesn't use a covariant
2297 thunk in this slot. */
2298 if (overrider_target != overrider_fn)
2300 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2301 /* We already know that the overrider needs a covariant thunk. */
2302 b = get_primary_binfo (b);
2303 for (; ; b = get_primary_binfo (b))
2305 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2306 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2307 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2308 break;
2309 if (BINFO_LOST_PRIMARY_P (b))
2310 lost = true;
2312 first_defn = b;
2315 /* Assume that we will produce a thunk that convert all the way to
2316 the final overrider, and not to an intermediate virtual base. */
2317 virtual_base = NULL_TREE;
2319 /* See if we can convert to an intermediate virtual base first, and then
2320 use the vcall offset located there to finish the conversion. */
2321 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2323 /* If we find the final overrider, then we can stop
2324 walking. */
2325 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2326 BINFO_TYPE (TREE_VALUE (overrider))))
2327 break;
2329 /* If we find a virtual base, and we haven't yet found the
2330 overrider, then there is a virtual base between the
2331 declaring base (first_defn) and the final overrider. */
2332 if (BINFO_VIRTUAL_P (b))
2334 virtual_base = b;
2335 break;
2339 /* Compute the constant adjustment to the `this' pointer. The
2340 `this' pointer, when this function is called, will point at BINFO
2341 (or one of its primary bases, which are at the same offset). */
2342 if (virtual_base)
2343 /* The `this' pointer needs to be adjusted from the declaration to
2344 the nearest virtual base. */
2345 delta = size_diffop_loc (input_location,
2346 convert (ssizetype, BINFO_OFFSET (virtual_base)),
2347 convert (ssizetype, BINFO_OFFSET (first_defn)));
2348 else if (lost)
2349 /* If the nearest definition is in a lost primary, we don't need an
2350 entry in our vtable. Except possibly in a constructor vtable,
2351 if we happen to get our primary back. In that case, the offset
2352 will be zero, as it will be a primary base. */
2353 delta = size_zero_node;
2354 else
2355 /* The `this' pointer needs to be adjusted from pointing to
2356 BINFO to pointing at the base where the final overrider
2357 appears. */
2358 delta = size_diffop_loc (input_location,
2359 convert (ssizetype,
2360 BINFO_OFFSET (TREE_VALUE (overrider))),
2361 convert (ssizetype, BINFO_OFFSET (binfo)));
2363 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2365 if (virtual_base)
2366 BV_VCALL_INDEX (*virtuals)
2367 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2368 else
2369 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2371 BV_LOST_PRIMARY (*virtuals) = lost;
2374 /* Called from modify_all_vtables via dfs_walk. */
2376 static tree
2377 dfs_modify_vtables (tree binfo, void* data)
2379 tree t = (tree) data;
2380 tree virtuals;
2381 tree old_virtuals;
2382 unsigned ix;
2384 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2385 /* A base without a vtable needs no modification, and its bases
2386 are uninteresting. */
2387 return dfs_skip_bases;
2389 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2390 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2391 /* Don't do the primary vtable, if it's new. */
2392 return NULL_TREE;
2394 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2395 /* There's no need to modify the vtable for a non-virtual primary
2396 base; we're not going to use that vtable anyhow. We do still
2397 need to do this for virtual primary bases, as they could become
2398 non-primary in a construction vtable. */
2399 return NULL_TREE;
2401 make_new_vtable (t, binfo);
2403 /* Now, go through each of the virtual functions in the virtual
2404 function table for BINFO. Find the final overrider, and update
2405 the BINFO_VIRTUALS list appropriately. */
2406 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2407 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2408 virtuals;
2409 ix++, virtuals = TREE_CHAIN (virtuals),
2410 old_virtuals = TREE_CHAIN (old_virtuals))
2411 update_vtable_entry_for_fn (t,
2412 binfo,
2413 BV_FN (old_virtuals),
2414 &virtuals, ix);
2416 return NULL_TREE;
2419 /* Update all of the primary and secondary vtables for T. Create new
2420 vtables as required, and initialize their RTTI information. Each
2421 of the functions in VIRTUALS is declared in T and may override a
2422 virtual function from a base class; find and modify the appropriate
2423 entries to point to the overriding functions. Returns a list, in
2424 declaration order, of the virtual functions that are declared in T,
2425 but do not appear in the primary base class vtable, and which
2426 should therefore be appended to the end of the vtable for T. */
2428 static tree
2429 modify_all_vtables (tree t, tree virtuals)
2431 tree binfo = TYPE_BINFO (t);
2432 tree *fnsp;
2434 /* Update all of the vtables. */
2435 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2437 /* Add virtual functions not already in our primary vtable. These
2438 will be both those introduced by this class, and those overridden
2439 from secondary bases. It does not include virtuals merely
2440 inherited from secondary bases. */
2441 for (fnsp = &virtuals; *fnsp; )
2443 tree fn = TREE_VALUE (*fnsp);
2445 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2446 || DECL_VINDEX (fn) == error_mark_node)
2448 /* We don't need to adjust the `this' pointer when
2449 calling this function. */
2450 BV_DELTA (*fnsp) = integer_zero_node;
2451 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2453 /* This is a function not already in our vtable. Keep it. */
2454 fnsp = &TREE_CHAIN (*fnsp);
2456 else
2457 /* We've already got an entry for this function. Skip it. */
2458 *fnsp = TREE_CHAIN (*fnsp);
2461 return virtuals;
2464 /* Get the base virtual function declarations in T that have the
2465 indicated NAME. */
2467 static tree
2468 get_basefndecls (tree name, tree t)
2470 tree methods;
2471 tree base_fndecls = NULL_TREE;
2472 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2473 int i;
2475 /* Find virtual functions in T with the indicated NAME. */
2476 i = lookup_fnfields_1 (t, name);
2477 if (i != -1)
2478 for (methods = VEC_index (tree, CLASSTYPE_METHOD_VEC (t), i);
2479 methods;
2480 methods = OVL_NEXT (methods))
2482 tree method = OVL_CURRENT (methods);
2484 if (TREE_CODE (method) == FUNCTION_DECL
2485 && DECL_VINDEX (method))
2486 base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
2489 if (base_fndecls)
2490 return base_fndecls;
2492 for (i = 0; i < n_baseclasses; i++)
2494 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2495 base_fndecls = chainon (get_basefndecls (name, basetype),
2496 base_fndecls);
2499 return base_fndecls;
2502 /* If this declaration supersedes the declaration of
2503 a method declared virtual in the base class, then
2504 mark this field as being virtual as well. */
2506 void
2507 check_for_override (tree decl, tree ctype)
2509 bool overrides_found = false;
2510 if (TREE_CODE (decl) == TEMPLATE_DECL)
2511 /* In [temp.mem] we have:
2513 A specialization of a member function template does not
2514 override a virtual function from a base class. */
2515 return;
2516 if ((DECL_DESTRUCTOR_P (decl)
2517 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2518 || DECL_CONV_FN_P (decl))
2519 && look_for_overrides (ctype, decl)
2520 && !DECL_STATIC_FUNCTION_P (decl))
2521 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2522 the error_mark_node so that we know it is an overriding
2523 function. */
2525 DECL_VINDEX (decl) = decl;
2526 overrides_found = true;
2529 if (DECL_VIRTUAL_P (decl))
2531 if (!DECL_VINDEX (decl))
2532 DECL_VINDEX (decl) = error_mark_node;
2533 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2534 if (DECL_DESTRUCTOR_P (decl))
2535 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2537 else if (DECL_FINAL_P (decl))
2538 error ("%q+#D marked final, but is not virtual", decl);
2539 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2540 error ("%q+#D marked override, but does not override", decl);
2543 /* Warn about hidden virtual functions that are not overridden in t.
2544 We know that constructors and destructors don't apply. */
2546 static void
2547 warn_hidden (tree t)
2549 VEC(tree,gc) *method_vec = CLASSTYPE_METHOD_VEC (t);
2550 tree fns;
2551 size_t i;
2553 /* We go through each separately named virtual function. */
2554 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2555 VEC_iterate (tree, method_vec, i, fns);
2556 ++i)
2558 tree fn;
2559 tree name;
2560 tree fndecl;
2561 tree base_fndecls;
2562 tree base_binfo;
2563 tree binfo;
2564 int j;
2566 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2567 have the same name. Figure out what name that is. */
2568 name = DECL_NAME (OVL_CURRENT (fns));
2569 /* There are no possibly hidden functions yet. */
2570 base_fndecls = NULL_TREE;
2571 /* Iterate through all of the base classes looking for possibly
2572 hidden functions. */
2573 for (binfo = TYPE_BINFO (t), j = 0;
2574 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2576 tree basetype = BINFO_TYPE (base_binfo);
2577 base_fndecls = chainon (get_basefndecls (name, basetype),
2578 base_fndecls);
2581 /* If there are no functions to hide, continue. */
2582 if (!base_fndecls)
2583 continue;
2585 /* Remove any overridden functions. */
2586 for (fn = fns; fn; fn = OVL_NEXT (fn))
2588 fndecl = OVL_CURRENT (fn);
2589 if (DECL_VINDEX (fndecl))
2591 tree *prev = &base_fndecls;
2593 while (*prev)
2594 /* If the method from the base class has the same
2595 signature as the method from the derived class, it
2596 has been overridden. */
2597 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2598 *prev = TREE_CHAIN (*prev);
2599 else
2600 prev = &TREE_CHAIN (*prev);
2604 /* Now give a warning for all base functions without overriders,
2605 as they are hidden. */
2606 while (base_fndecls)
2608 /* Here we know it is a hider, and no overrider exists. */
2609 warning (OPT_Woverloaded_virtual, "%q+D was hidden", TREE_VALUE (base_fndecls));
2610 warning (OPT_Woverloaded_virtual, " by %q+D", fns);
2611 base_fndecls = TREE_CHAIN (base_fndecls);
2616 /* Check for things that are invalid. There are probably plenty of other
2617 things we should check for also. */
2619 static void
2620 finish_struct_anon (tree t)
2622 tree field;
2624 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2626 if (TREE_STATIC (field))
2627 continue;
2628 if (TREE_CODE (field) != FIELD_DECL)
2629 continue;
2631 if (DECL_NAME (field) == NULL_TREE
2632 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2634 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2635 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2636 for (; elt; elt = DECL_CHAIN (elt))
2638 /* We're generally only interested in entities the user
2639 declared, but we also find nested classes by noticing
2640 the TYPE_DECL that we create implicitly. You're
2641 allowed to put one anonymous union inside another,
2642 though, so we explicitly tolerate that. We use
2643 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2644 we also allow unnamed types used for defining fields. */
2645 if (DECL_ARTIFICIAL (elt)
2646 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2647 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2648 continue;
2650 if (TREE_CODE (elt) != FIELD_DECL)
2652 if (is_union)
2653 permerror (input_location, "%q+#D invalid; an anonymous union can "
2654 "only have non-static data members", elt);
2655 else
2656 permerror (input_location, "%q+#D invalid; an anonymous struct can "
2657 "only have non-static data members", elt);
2658 continue;
2661 if (TREE_PRIVATE (elt))
2663 if (is_union)
2664 permerror (input_location, "private member %q+#D in anonymous union", elt);
2665 else
2666 permerror (input_location, "private member %q+#D in anonymous struct", elt);
2668 else if (TREE_PROTECTED (elt))
2670 if (is_union)
2671 permerror (input_location, "protected member %q+#D in anonymous union", elt);
2672 else
2673 permerror (input_location, "protected member %q+#D in anonymous struct", elt);
2676 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2677 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2683 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2684 will be used later during class template instantiation.
2685 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2686 a non-static member data (FIELD_DECL), a member function
2687 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2688 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2689 When FRIEND_P is nonzero, T is either a friend class
2690 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2691 (FUNCTION_DECL, TEMPLATE_DECL). */
2693 void
2694 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2696 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2697 if (CLASSTYPE_TEMPLATE_INFO (type))
2698 CLASSTYPE_DECL_LIST (type)
2699 = tree_cons (friend_p ? NULL_TREE : type,
2700 t, CLASSTYPE_DECL_LIST (type));
2703 /* This function is called from declare_virt_assop_and_dtor via
2704 dfs_walk_all.
2706 DATA is a type that direcly or indirectly inherits the base
2707 represented by BINFO. If BINFO contains a virtual assignment [copy
2708 assignment or move assigment] operator or a virtual constructor,
2709 declare that function in DATA if it hasn't been already declared. */
2711 static tree
2712 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
2714 tree bv, fn, t = (tree)data;
2715 tree opname = ansi_assopname (NOP_EXPR);
2717 gcc_assert (t && CLASS_TYPE_P (t));
2718 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
2720 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2721 /* A base without a vtable needs no modification, and its bases
2722 are uninteresting. */
2723 return dfs_skip_bases;
2725 if (BINFO_PRIMARY_P (binfo))
2726 /* If this is a primary base, then we have already looked at the
2727 virtual functions of its vtable. */
2728 return NULL_TREE;
2730 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
2732 fn = BV_FN (bv);
2734 if (DECL_NAME (fn) == opname)
2736 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
2737 lazily_declare_fn (sfk_copy_assignment, t);
2738 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
2739 lazily_declare_fn (sfk_move_assignment, t);
2741 else if (DECL_DESTRUCTOR_P (fn)
2742 && CLASSTYPE_LAZY_DESTRUCTOR (t))
2743 lazily_declare_fn (sfk_destructor, t);
2746 return NULL_TREE;
2749 /* If the class type T has a direct or indirect base that contains a
2750 virtual assignment operator or a virtual destructor, declare that
2751 function in T if it hasn't been already declared. */
2753 static void
2754 declare_virt_assop_and_dtor (tree t)
2756 if (!(TYPE_POLYMORPHIC_P (t)
2757 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
2758 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
2759 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
2760 return;
2762 dfs_walk_all (TYPE_BINFO (t),
2763 dfs_declare_virt_assop_and_dtor,
2764 NULL, t);
2767 /* Declare the inheriting constructor for class T inherited from base
2768 constructor CTOR with the parameter array PARMS of size NPARMS. */
2770 static void
2771 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
2773 /* We don't declare an inheriting ctor that would be a default,
2774 copy or move ctor. */
2775 if (nparms == 0
2776 || (nparms == 1
2777 && TREE_CODE (parms[0]) == REFERENCE_TYPE
2778 && TYPE_MAIN_VARIANT (TREE_TYPE (parms[0])) == t))
2779 return;
2780 int i;
2781 tree parmlist = void_list_node;
2782 for (i = nparms - 1; i >= 0; i--)
2783 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
2784 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
2785 t, false, ctor, parmlist);
2786 if (add_method (t, fn, NULL_TREE))
2788 DECL_CHAIN (fn) = TYPE_METHODS (t);
2789 TYPE_METHODS (t) = fn;
2793 /* Declare all the inheriting constructors for class T inherited from base
2794 constructor CTOR. */
2796 static void
2797 one_inherited_ctor (tree ctor, tree t)
2799 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
2801 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
2802 int i = 0;
2803 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
2805 if (TREE_PURPOSE (parms))
2806 one_inheriting_sig (t, ctor, new_parms, i);
2807 new_parms[i++] = TREE_VALUE (parms);
2809 one_inheriting_sig (t, ctor, new_parms, i);
2812 /* Create default constructors, assignment operators, and so forth for
2813 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
2814 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
2815 the class cannot have a default constructor, copy constructor
2816 taking a const reference argument, or an assignment operator taking
2817 a const reference, respectively. */
2819 static void
2820 add_implicitly_declared_members (tree t, tree* access_decls,
2821 int cant_have_const_cctor,
2822 int cant_have_const_assignment)
2824 bool move_ok = false;
2826 if (cxx_dialect >= cxx0x && !CLASSTYPE_DESTRUCTORS (t)
2827 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
2828 && !type_has_move_constructor (t) && !type_has_move_assign (t))
2829 move_ok = true;
2831 /* Destructor. */
2832 if (!CLASSTYPE_DESTRUCTORS (t))
2834 /* In general, we create destructors lazily. */
2835 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
2837 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2838 && TYPE_FOR_JAVA (t))
2839 /* But if this is a Java class, any non-trivial destructor is
2840 invalid, even if compiler-generated. Therefore, if the
2841 destructor is non-trivial we create it now. */
2842 lazily_declare_fn (sfk_destructor, t);
2845 /* [class.ctor]
2847 If there is no user-declared constructor for a class, a default
2848 constructor is implicitly declared. */
2849 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
2851 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
2852 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
2853 if (cxx_dialect >= cxx0x)
2854 TYPE_HAS_CONSTEXPR_CTOR (t)
2855 /* This might force the declaration. */
2856 = type_has_constexpr_default_constructor (t);
2859 /* [class.ctor]
2861 If a class definition does not explicitly declare a copy
2862 constructor, one is declared implicitly. */
2863 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
2865 TYPE_HAS_COPY_CTOR (t) = 1;
2866 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
2867 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
2868 if (move_ok)
2869 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
2872 /* If there is no assignment operator, one will be created if and
2873 when it is needed. For now, just record whether or not the type
2874 of the parameter to the assignment operator will be a const or
2875 non-const reference. */
2876 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
2878 TYPE_HAS_COPY_ASSIGN (t) = 1;
2879 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
2880 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
2881 if (move_ok)
2882 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
2885 /* We can't be lazy about declaring functions that might override
2886 a virtual function from a base class. */
2887 declare_virt_assop_and_dtor (t);
2889 while (*access_decls)
2891 tree using_decl = TREE_VALUE (*access_decls);
2892 tree decl = USING_DECL_DECLS (using_decl);
2893 if (DECL_SELF_REFERENCE_P (decl))
2895 /* declare, then remove the decl */
2896 tree ctor_list = CLASSTYPE_CONSTRUCTORS (TREE_TYPE (decl));
2897 location_t loc = input_location;
2898 input_location = DECL_SOURCE_LOCATION (using_decl);
2899 if (ctor_list)
2900 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
2901 one_inherited_ctor (OVL_CURRENT (ctor_list), t);
2902 *access_decls = TREE_CHAIN (*access_decls);
2903 input_location = loc;
2905 else
2906 access_decls = &TREE_CHAIN (*access_decls);
2910 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
2911 count the number of fields in TYPE, including anonymous union
2912 members. */
2914 static int
2915 count_fields (tree fields)
2917 tree x;
2918 int n_fields = 0;
2919 for (x = fields; x; x = DECL_CHAIN (x))
2921 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2922 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
2923 else
2924 n_fields += 1;
2926 return n_fields;
2929 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
2930 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
2931 elts, starting at offset IDX. */
2933 static int
2934 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
2936 tree x;
2937 for (x = fields; x; x = DECL_CHAIN (x))
2939 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2940 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
2941 else
2942 field_vec->elts[idx++] = x;
2944 return idx;
2947 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
2948 starting at offset IDX. */
2950 static int
2951 add_enum_fields_to_record_type (tree enumtype,
2952 struct sorted_fields_type *field_vec,
2953 int idx)
2955 tree values;
2956 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
2957 field_vec->elts[idx++] = TREE_VALUE (values);
2958 return idx;
2961 /* FIELD is a bit-field. We are finishing the processing for its
2962 enclosing type. Issue any appropriate messages and set appropriate
2963 flags. Returns false if an error has been diagnosed. */
2965 static bool
2966 check_bitfield_decl (tree field)
2968 tree type = TREE_TYPE (field);
2969 tree w;
2971 /* Extract the declared width of the bitfield, which has been
2972 temporarily stashed in DECL_INITIAL. */
2973 w = DECL_INITIAL (field);
2974 gcc_assert (w != NULL_TREE);
2975 /* Remove the bit-field width indicator so that the rest of the
2976 compiler does not treat that value as an initializer. */
2977 DECL_INITIAL (field) = NULL_TREE;
2979 /* Detect invalid bit-field type. */
2980 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
2982 error ("bit-field %q+#D with non-integral type", field);
2983 w = error_mark_node;
2985 else
2987 location_t loc = input_location;
2988 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
2989 STRIP_NOPS (w);
2991 /* detect invalid field size. */
2992 input_location = DECL_SOURCE_LOCATION (field);
2993 w = cxx_constant_value (w);
2994 input_location = loc;
2996 if (TREE_CODE (w) != INTEGER_CST)
2998 error ("bit-field %q+D width not an integer constant", field);
2999 w = error_mark_node;
3001 else if (tree_int_cst_sgn (w) < 0)
3003 error ("negative width in bit-field %q+D", field);
3004 w = error_mark_node;
3006 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3008 error ("zero width for bit-field %q+D", field);
3009 w = error_mark_node;
3011 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3012 && TREE_CODE (type) != ENUMERAL_TYPE
3013 && TREE_CODE (type) != BOOLEAN_TYPE)
3014 warning (0, "width of %q+D exceeds its type", field);
3015 else if (TREE_CODE (type) == ENUMERAL_TYPE
3016 && (0 > (compare_tree_int
3017 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3018 warning (0, "%q+D is too small to hold all values of %q#T", field, type);
3021 if (w != error_mark_node)
3023 DECL_SIZE (field) = convert (bitsizetype, w);
3024 DECL_BIT_FIELD (field) = 1;
3025 return true;
3027 else
3029 /* Non-bit-fields are aligned for their type. */
3030 DECL_BIT_FIELD (field) = 0;
3031 CLEAR_DECL_C_BIT_FIELD (field);
3032 return false;
3036 /* FIELD is a non bit-field. We are finishing the processing for its
3037 enclosing type T. Issue any appropriate messages and set appropriate
3038 flags. */
3040 static void
3041 check_field_decl (tree field,
3042 tree t,
3043 int* cant_have_const_ctor,
3044 int* no_const_asn_ref,
3045 int* any_default_members)
3047 tree type = strip_array_types (TREE_TYPE (field));
3049 /* In C++98 an anonymous union cannot contain any fields which would change
3050 the settings of CANT_HAVE_CONST_CTOR and friends. */
3051 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx0x)
3053 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3054 structs. So, we recurse through their fields here. */
3055 else if (ANON_AGGR_TYPE_P (type))
3057 tree fields;
3059 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
3060 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3061 check_field_decl (fields, t, cant_have_const_ctor,
3062 no_const_asn_ref, any_default_members);
3064 /* Check members with class type for constructors, destructors,
3065 etc. */
3066 else if (CLASS_TYPE_P (type))
3068 /* Never let anything with uninheritable virtuals
3069 make it through without complaint. */
3070 abstract_virtuals_error (field, type);
3072 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx0x)
3074 static bool warned;
3075 int oldcount = errorcount;
3076 if (TYPE_NEEDS_CONSTRUCTING (type))
3077 error ("member %q+#D with constructor not allowed in union",
3078 field);
3079 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3080 error ("member %q+#D with destructor not allowed in union", field);
3081 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3082 error ("member %q+#D with copy assignment operator not allowed in union",
3083 field);
3084 if (!warned && errorcount > oldcount)
3086 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3087 "only available with -std=c++11 or -std=gnu++11");
3088 warned = true;
3091 else
3093 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3094 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3095 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3096 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3097 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3098 || !TYPE_HAS_COPY_ASSIGN (type));
3099 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3100 || !TYPE_HAS_COPY_CTOR (type));
3101 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3102 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3103 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3104 || TYPE_HAS_COMPLEX_DFLT (type));
3107 if (TYPE_HAS_COPY_CTOR (type)
3108 && !TYPE_HAS_CONST_COPY_CTOR (type))
3109 *cant_have_const_ctor = 1;
3111 if (TYPE_HAS_COPY_ASSIGN (type)
3112 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3113 *no_const_asn_ref = 1;
3115 if (DECL_INITIAL (field) != NULL_TREE)
3117 /* `build_class_init_list' does not recognize
3118 non-FIELD_DECLs. */
3119 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
3120 error ("multiple fields in union %qT initialized", t);
3121 *any_default_members = 1;
3125 /* Check the data members (both static and non-static), class-scoped
3126 typedefs, etc., appearing in the declaration of T. Issue
3127 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3128 declaration order) of access declarations; each TREE_VALUE in this
3129 list is a USING_DECL.
3131 In addition, set the following flags:
3133 EMPTY_P
3134 The class is empty, i.e., contains no non-static data members.
3136 CANT_HAVE_CONST_CTOR_P
3137 This class cannot have an implicitly generated copy constructor
3138 taking a const reference.
3140 CANT_HAVE_CONST_ASN_REF
3141 This class cannot have an implicitly generated assignment
3142 operator taking a const reference.
3144 All of these flags should be initialized before calling this
3145 function.
3147 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3148 fields can be added by adding to this chain. */
3150 static void
3151 check_field_decls (tree t, tree *access_decls,
3152 int *cant_have_const_ctor_p,
3153 int *no_const_asn_ref_p)
3155 tree *field;
3156 tree *next;
3157 bool has_pointers;
3158 int any_default_members;
3159 int cant_pack = 0;
3160 int field_access = -1;
3162 /* Assume there are no access declarations. */
3163 *access_decls = NULL_TREE;
3164 /* Assume this class has no pointer members. */
3165 has_pointers = false;
3166 /* Assume none of the members of this class have default
3167 initializations. */
3168 any_default_members = 0;
3170 for (field = &TYPE_FIELDS (t); *field; field = next)
3172 tree x = *field;
3173 tree type = TREE_TYPE (x);
3174 int this_field_access;
3176 next = &DECL_CHAIN (x);
3178 if (TREE_CODE (x) == USING_DECL)
3180 /* Save the access declarations for our caller. */
3181 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3182 continue;
3185 if (TREE_CODE (x) == TYPE_DECL
3186 || TREE_CODE (x) == TEMPLATE_DECL)
3187 continue;
3189 /* If we've gotten this far, it's a data member, possibly static,
3190 or an enumerator. */
3191 if (TREE_CODE (x) != CONST_DECL)
3192 DECL_CONTEXT (x) = t;
3194 /* When this goes into scope, it will be a non-local reference. */
3195 DECL_NONLOCAL (x) = 1;
3197 if (TREE_CODE (t) == UNION_TYPE)
3199 /* [class.union]
3201 If a union contains a static data member, or a member of
3202 reference type, the program is ill-formed. */
3203 if (TREE_CODE (x) == VAR_DECL)
3205 error ("%q+D may not be static because it is a member of a union", x);
3206 continue;
3208 if (TREE_CODE (type) == REFERENCE_TYPE)
3210 error ("%q+D may not have reference type %qT because"
3211 " it is a member of a union",
3212 x, type);
3213 continue;
3217 /* Perform error checking that did not get done in
3218 grokdeclarator. */
3219 if (TREE_CODE (type) == FUNCTION_TYPE)
3221 error ("field %q+D invalidly declared function type", x);
3222 type = build_pointer_type (type);
3223 TREE_TYPE (x) = type;
3225 else if (TREE_CODE (type) == METHOD_TYPE)
3227 error ("field %q+D invalidly declared method type", x);
3228 type = build_pointer_type (type);
3229 TREE_TYPE (x) = type;
3232 if (type == error_mark_node)
3233 continue;
3235 if (TREE_CODE (x) == CONST_DECL || TREE_CODE (x) == VAR_DECL)
3236 continue;
3238 /* Now it can only be a FIELD_DECL. */
3240 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3241 CLASSTYPE_NON_AGGREGATE (t) = 1;
3243 /* If at least one non-static data member is non-literal, the whole
3244 class becomes non-literal. Note: if the type is incomplete we
3245 will complain later on. */
3246 if (COMPLETE_TYPE_P (type) && !literal_type_p (type))
3247 CLASSTYPE_LITERAL_P (t) = false;
3249 /* A standard-layout class is a class that:
3251 has the same access control (Clause 11) for all non-static data members,
3252 ... */
3253 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3254 if (field_access == -1)
3255 field_access = this_field_access;
3256 else if (this_field_access != field_access)
3257 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3259 /* If this is of reference type, check if it needs an init. */
3260 if (TREE_CODE (type) == REFERENCE_TYPE)
3262 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3263 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3264 if (DECL_INITIAL (x) == NULL_TREE)
3265 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3267 /* ARM $12.6.2: [A member initializer list] (or, for an
3268 aggregate, initialization by a brace-enclosed list) is the
3269 only way to initialize nonstatic const and reference
3270 members. */
3271 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3272 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3275 type = strip_array_types (type);
3277 if (TYPE_PACKED (t))
3279 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3281 warning
3283 "ignoring packed attribute because of unpacked non-POD field %q+#D",
3285 cant_pack = 1;
3287 else if (DECL_C_BIT_FIELD (x)
3288 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3289 DECL_PACKED (x) = 1;
3292 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3293 /* We don't treat zero-width bitfields as making a class
3294 non-empty. */
3296 else
3298 /* The class is non-empty. */
3299 CLASSTYPE_EMPTY_P (t) = 0;
3300 /* The class is not even nearly empty. */
3301 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3302 /* If one of the data members contains an empty class,
3303 so does T. */
3304 if (CLASS_TYPE_P (type)
3305 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3306 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3309 /* This is used by -Weffc++ (see below). Warn only for pointers
3310 to members which might hold dynamic memory. So do not warn
3311 for pointers to functions or pointers to members. */
3312 if (TYPE_PTR_P (type)
3313 && !TYPE_PTRFN_P (type))
3314 has_pointers = true;
3316 if (CLASS_TYPE_P (type))
3318 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3319 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3320 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3321 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3324 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3325 CLASSTYPE_HAS_MUTABLE (t) = 1;
3327 if (! layout_pod_type_p (type))
3328 /* DR 148 now allows pointers to members (which are POD themselves),
3329 to be allowed in POD structs. */
3330 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3332 if (!std_layout_type_p (type))
3333 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3335 if (! zero_init_p (type))
3336 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3338 /* We set DECL_C_BIT_FIELD in grokbitfield.
3339 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3340 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3341 check_field_decl (x, t,
3342 cant_have_const_ctor_p,
3343 no_const_asn_ref_p,
3344 &any_default_members);
3346 /* Now that we've removed bit-field widths from DECL_INITIAL,
3347 anything left in DECL_INITIAL is an NSDMI that makes the class
3348 non-aggregate. */
3349 if (DECL_INITIAL (x))
3350 CLASSTYPE_NON_AGGREGATE (t) = true;
3352 /* If any field is const, the structure type is pseudo-const. */
3353 if (CP_TYPE_CONST_P (type))
3355 C_TYPE_FIELDS_READONLY (t) = 1;
3356 if (DECL_INITIAL (x) == NULL_TREE)
3357 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3359 /* ARM $12.6.2: [A member initializer list] (or, for an
3360 aggregate, initialization by a brace-enclosed list) is the
3361 only way to initialize nonstatic const and reference
3362 members. */
3363 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3364 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3366 /* A field that is pseudo-const makes the structure likewise. */
3367 else if (CLASS_TYPE_P (type))
3369 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3370 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3371 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3372 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3375 /* Core issue 80: A nonstatic data member is required to have a
3376 different name from the class iff the class has a
3377 user-declared constructor. */
3378 if (constructor_name_p (DECL_NAME (x), t)
3379 && TYPE_HAS_USER_CONSTRUCTOR (t))
3380 permerror (input_location, "field %q+#D with same name as class", x);
3383 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3384 it should also define a copy constructor and an assignment operator to
3385 implement the correct copy semantic (deep vs shallow, etc.). As it is
3386 not feasible to check whether the constructors do allocate dynamic memory
3387 and store it within members, we approximate the warning like this:
3389 -- Warn only if there are members which are pointers
3390 -- Warn only if there is a non-trivial constructor (otherwise,
3391 there cannot be memory allocated).
3392 -- Warn only if there is a non-trivial destructor. We assume that the
3393 user at least implemented the cleanup correctly, and a destructor
3394 is needed to free dynamic memory.
3396 This seems enough for practical purposes. */
3397 if (warn_ecpp
3398 && has_pointers
3399 && TYPE_HAS_USER_CONSTRUCTOR (t)
3400 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3401 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3403 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3405 if (! TYPE_HAS_COPY_CTOR (t))
3407 warning (OPT_Weffc__,
3408 " but does not override %<%T(const %T&)%>", t, t);
3409 if (!TYPE_HAS_COPY_ASSIGN (t))
3410 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3412 else if (! TYPE_HAS_COPY_ASSIGN (t))
3413 warning (OPT_Weffc__,
3414 " but does not override %<operator=(const %T&)%>", t);
3417 /* Non-static data member initializers make the default constructor
3418 non-trivial. */
3419 if (any_default_members)
3421 TYPE_NEEDS_CONSTRUCTING (t) = true;
3422 TYPE_HAS_COMPLEX_DFLT (t) = true;
3425 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3426 if (cant_pack)
3427 TYPE_PACKED (t) = 0;
3429 /* Check anonymous struct/anonymous union fields. */
3430 finish_struct_anon (t);
3432 /* We've built up the list of access declarations in reverse order.
3433 Fix that now. */
3434 *access_decls = nreverse (*access_decls);
3437 /* If TYPE is an empty class type, records its OFFSET in the table of
3438 OFFSETS. */
3440 static int
3441 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3443 splay_tree_node n;
3445 if (!is_empty_class (type))
3446 return 0;
3448 /* Record the location of this empty object in OFFSETS. */
3449 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3450 if (!n)
3451 n = splay_tree_insert (offsets,
3452 (splay_tree_key) offset,
3453 (splay_tree_value) NULL_TREE);
3454 n->value = ((splay_tree_value)
3455 tree_cons (NULL_TREE,
3456 type,
3457 (tree) n->value));
3459 return 0;
3462 /* Returns nonzero if TYPE is an empty class type and there is
3463 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3465 static int
3466 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3468 splay_tree_node n;
3469 tree t;
3471 if (!is_empty_class (type))
3472 return 0;
3474 /* Record the location of this empty object in OFFSETS. */
3475 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3476 if (!n)
3477 return 0;
3479 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3480 if (same_type_p (TREE_VALUE (t), type))
3481 return 1;
3483 return 0;
3486 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3487 F for every subobject, passing it the type, offset, and table of
3488 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3489 be traversed.
3491 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3492 than MAX_OFFSET will not be walked.
3494 If F returns a nonzero value, the traversal ceases, and that value
3495 is returned. Otherwise, returns zero. */
3497 static int
3498 walk_subobject_offsets (tree type,
3499 subobject_offset_fn f,
3500 tree offset,
3501 splay_tree offsets,
3502 tree max_offset,
3503 int vbases_p)
3505 int r = 0;
3506 tree type_binfo = NULL_TREE;
3508 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3509 stop. */
3510 if (max_offset && INT_CST_LT (max_offset, offset))
3511 return 0;
3513 if (type == error_mark_node)
3514 return 0;
3516 if (!TYPE_P (type))
3518 if (abi_version_at_least (2))
3519 type_binfo = type;
3520 type = BINFO_TYPE (type);
3523 if (CLASS_TYPE_P (type))
3525 tree field;
3526 tree binfo;
3527 int i;
3529 /* Avoid recursing into objects that are not interesting. */
3530 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3531 return 0;
3533 /* Record the location of TYPE. */
3534 r = (*f) (type, offset, offsets);
3535 if (r)
3536 return r;
3538 /* Iterate through the direct base classes of TYPE. */
3539 if (!type_binfo)
3540 type_binfo = TYPE_BINFO (type);
3541 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3543 tree binfo_offset;
3545 if (abi_version_at_least (2)
3546 && BINFO_VIRTUAL_P (binfo))
3547 continue;
3549 if (!vbases_p
3550 && BINFO_VIRTUAL_P (binfo)
3551 && !BINFO_PRIMARY_P (binfo))
3552 continue;
3554 if (!abi_version_at_least (2))
3555 binfo_offset = size_binop (PLUS_EXPR,
3556 offset,
3557 BINFO_OFFSET (binfo));
3558 else
3560 tree orig_binfo;
3561 /* We cannot rely on BINFO_OFFSET being set for the base
3562 class yet, but the offsets for direct non-virtual
3563 bases can be calculated by going back to the TYPE. */
3564 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3565 binfo_offset = size_binop (PLUS_EXPR,
3566 offset,
3567 BINFO_OFFSET (orig_binfo));
3570 r = walk_subobject_offsets (binfo,
3572 binfo_offset,
3573 offsets,
3574 max_offset,
3575 (abi_version_at_least (2)
3576 ? /*vbases_p=*/0 : vbases_p));
3577 if (r)
3578 return r;
3581 if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type))
3583 unsigned ix;
3584 VEC(tree,gc) *vbases;
3586 /* Iterate through the virtual base classes of TYPE. In G++
3587 3.2, we included virtual bases in the direct base class
3588 loop above, which results in incorrect results; the
3589 correct offsets for virtual bases are only known when
3590 working with the most derived type. */
3591 if (vbases_p)
3592 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3593 VEC_iterate (tree, vbases, ix, binfo); ix++)
3595 r = walk_subobject_offsets (binfo,
3597 size_binop (PLUS_EXPR,
3598 offset,
3599 BINFO_OFFSET (binfo)),
3600 offsets,
3601 max_offset,
3602 /*vbases_p=*/0);
3603 if (r)
3604 return r;
3606 else
3608 /* We still have to walk the primary base, if it is
3609 virtual. (If it is non-virtual, then it was walked
3610 above.) */
3611 tree vbase = get_primary_binfo (type_binfo);
3613 if (vbase && BINFO_VIRTUAL_P (vbase)
3614 && BINFO_PRIMARY_P (vbase)
3615 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3617 r = (walk_subobject_offsets
3618 (vbase, f, offset,
3619 offsets, max_offset, /*vbases_p=*/0));
3620 if (r)
3621 return r;
3626 /* Iterate through the fields of TYPE. */
3627 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3628 if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
3630 tree field_offset;
3632 if (abi_version_at_least (2))
3633 field_offset = byte_position (field);
3634 else
3635 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3636 field_offset = DECL_FIELD_OFFSET (field);
3638 r = walk_subobject_offsets (TREE_TYPE (field),
3640 size_binop (PLUS_EXPR,
3641 offset,
3642 field_offset),
3643 offsets,
3644 max_offset,
3645 /*vbases_p=*/1);
3646 if (r)
3647 return r;
3650 else if (TREE_CODE (type) == ARRAY_TYPE)
3652 tree element_type = strip_array_types (type);
3653 tree domain = TYPE_DOMAIN (type);
3654 tree index;
3656 /* Avoid recursing into objects that are not interesting. */
3657 if (!CLASS_TYPE_P (element_type)
3658 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3659 return 0;
3661 /* Step through each of the elements in the array. */
3662 for (index = size_zero_node;
3663 /* G++ 3.2 had an off-by-one error here. */
3664 (abi_version_at_least (2)
3665 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3666 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3667 index = size_binop (PLUS_EXPR, index, size_one_node))
3669 r = walk_subobject_offsets (TREE_TYPE (type),
3671 offset,
3672 offsets,
3673 max_offset,
3674 /*vbases_p=*/1);
3675 if (r)
3676 return r;
3677 offset = size_binop (PLUS_EXPR, offset,
3678 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3679 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3680 there's no point in iterating through the remaining
3681 elements of the array. */
3682 if (max_offset && INT_CST_LT (max_offset, offset))
3683 break;
3687 return 0;
3690 /* Record all of the empty subobjects of TYPE (either a type or a
3691 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3692 is being placed at OFFSET; otherwise, it is a base class that is
3693 being placed at OFFSET. */
3695 static void
3696 record_subobject_offsets (tree type,
3697 tree offset,
3698 splay_tree offsets,
3699 bool is_data_member)
3701 tree max_offset;
3702 /* If recording subobjects for a non-static data member or a
3703 non-empty base class , we do not need to record offsets beyond
3704 the size of the biggest empty class. Additional data members
3705 will go at the end of the class. Additional base classes will go
3706 either at offset zero (if empty, in which case they cannot
3707 overlap with offsets past the size of the biggest empty class) or
3708 at the end of the class.
3710 However, if we are placing an empty base class, then we must record
3711 all offsets, as either the empty class is at offset zero (where
3712 other empty classes might later be placed) or at the end of the
3713 class (where other objects might then be placed, so other empty
3714 subobjects might later overlap). */
3715 if (is_data_member
3716 || !is_empty_class (BINFO_TYPE (type)))
3717 max_offset = sizeof_biggest_empty_class;
3718 else
3719 max_offset = NULL_TREE;
3720 walk_subobject_offsets (type, record_subobject_offset, offset,
3721 offsets, max_offset, is_data_member);
3724 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3725 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3726 virtual bases of TYPE are examined. */
3728 static int
3729 layout_conflict_p (tree type,
3730 tree offset,
3731 splay_tree offsets,
3732 int vbases_p)
3734 splay_tree_node max_node;
3736 /* Get the node in OFFSETS that indicates the maximum offset where
3737 an empty subobject is located. */
3738 max_node = splay_tree_max (offsets);
3739 /* If there aren't any empty subobjects, then there's no point in
3740 performing this check. */
3741 if (!max_node)
3742 return 0;
3744 return walk_subobject_offsets (type, check_subobject_offset, offset,
3745 offsets, (tree) (max_node->key),
3746 vbases_p);
3749 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3750 non-static data member of the type indicated by RLI. BINFO is the
3751 binfo corresponding to the base subobject, OFFSETS maps offsets to
3752 types already located at those offsets. This function determines
3753 the position of the DECL. */
3755 static void
3756 layout_nonempty_base_or_field (record_layout_info rli,
3757 tree decl,
3758 tree binfo,
3759 splay_tree offsets)
3761 tree offset = NULL_TREE;
3762 bool field_p;
3763 tree type;
3765 if (binfo)
3767 /* For the purposes of determining layout conflicts, we want to
3768 use the class type of BINFO; TREE_TYPE (DECL) will be the
3769 CLASSTYPE_AS_BASE version, which does not contain entries for
3770 zero-sized bases. */
3771 type = TREE_TYPE (binfo);
3772 field_p = false;
3774 else
3776 type = TREE_TYPE (decl);
3777 field_p = true;
3780 /* Try to place the field. It may take more than one try if we have
3781 a hard time placing the field without putting two objects of the
3782 same type at the same address. */
3783 while (1)
3785 struct record_layout_info_s old_rli = *rli;
3787 /* Place this field. */
3788 place_field (rli, decl);
3789 offset = byte_position (decl);
3791 /* We have to check to see whether or not there is already
3792 something of the same type at the offset we're about to use.
3793 For example, consider:
3795 struct S {};
3796 struct T : public S { int i; };
3797 struct U : public S, public T {};
3799 Here, we put S at offset zero in U. Then, we can't put T at
3800 offset zero -- its S component would be at the same address
3801 as the S we already allocated. So, we have to skip ahead.
3802 Since all data members, including those whose type is an
3803 empty class, have nonzero size, any overlap can happen only
3804 with a direct or indirect base-class -- it can't happen with
3805 a data member. */
3806 /* In a union, overlap is permitted; all members are placed at
3807 offset zero. */
3808 if (TREE_CODE (rli->t) == UNION_TYPE)
3809 break;
3810 /* G++ 3.2 did not check for overlaps when placing a non-empty
3811 virtual base. */
3812 if (!abi_version_at_least (2) && binfo && BINFO_VIRTUAL_P (binfo))
3813 break;
3814 if (layout_conflict_p (field_p ? type : binfo, offset,
3815 offsets, field_p))
3817 /* Strip off the size allocated to this field. That puts us
3818 at the first place we could have put the field with
3819 proper alignment. */
3820 *rli = old_rli;
3822 /* Bump up by the alignment required for the type. */
3823 rli->bitpos
3824 = size_binop (PLUS_EXPR, rli->bitpos,
3825 bitsize_int (binfo
3826 ? CLASSTYPE_ALIGN (type)
3827 : TYPE_ALIGN (type)));
3828 normalize_rli (rli);
3830 else
3831 /* There was no conflict. We're done laying out this field. */
3832 break;
3835 /* Now that we know where it will be placed, update its
3836 BINFO_OFFSET. */
3837 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3838 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
3839 this point because their BINFO_OFFSET is copied from another
3840 hierarchy. Therefore, we may not need to add the entire
3841 OFFSET. */
3842 propagate_binfo_offsets (binfo,
3843 size_diffop_loc (input_location,
3844 convert (ssizetype, offset),
3845 convert (ssizetype,
3846 BINFO_OFFSET (binfo))));
3849 /* Returns true if TYPE is empty and OFFSET is nonzero. */
3851 static int
3852 empty_base_at_nonzero_offset_p (tree type,
3853 tree offset,
3854 splay_tree /*offsets*/)
3856 return is_empty_class (type) && !integer_zerop (offset);
3859 /* Layout the empty base BINFO. EOC indicates the byte currently just
3860 past the end of the class, and should be correctly aligned for a
3861 class of the type indicated by BINFO; OFFSETS gives the offsets of
3862 the empty bases allocated so far. T is the most derived
3863 type. Return nonzero iff we added it at the end. */
3865 static bool
3866 layout_empty_base (record_layout_info rli, tree binfo,
3867 tree eoc, splay_tree offsets)
3869 tree alignment;
3870 tree basetype = BINFO_TYPE (binfo);
3871 bool atend = false;
3873 /* This routine should only be used for empty classes. */
3874 gcc_assert (is_empty_class (basetype));
3875 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3877 if (!integer_zerop (BINFO_OFFSET (binfo)))
3879 if (abi_version_at_least (2))
3880 propagate_binfo_offsets
3881 (binfo, size_diffop_loc (input_location,
3882 size_zero_node, BINFO_OFFSET (binfo)));
3883 else
3884 warning (OPT_Wabi,
3885 "offset of empty base %qT may not be ABI-compliant and may"
3886 "change in a future version of GCC",
3887 BINFO_TYPE (binfo));
3890 /* This is an empty base class. We first try to put it at offset
3891 zero. */
3892 if (layout_conflict_p (binfo,
3893 BINFO_OFFSET (binfo),
3894 offsets,
3895 /*vbases_p=*/0))
3897 /* That didn't work. Now, we move forward from the next
3898 available spot in the class. */
3899 atend = true;
3900 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
3901 while (1)
3903 if (!layout_conflict_p (binfo,
3904 BINFO_OFFSET (binfo),
3905 offsets,
3906 /*vbases_p=*/0))
3907 /* We finally found a spot where there's no overlap. */
3908 break;
3910 /* There's overlap here, too. Bump along to the next spot. */
3911 propagate_binfo_offsets (binfo, alignment);
3915 if (CLASSTYPE_USER_ALIGN (basetype))
3917 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
3918 if (warn_packed)
3919 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
3920 TYPE_USER_ALIGN (rli->t) = 1;
3923 return atend;
3926 /* Layout the base given by BINFO in the class indicated by RLI.
3927 *BASE_ALIGN is a running maximum of the alignments of
3928 any base class. OFFSETS gives the location of empty base
3929 subobjects. T is the most derived type. Return nonzero if the new
3930 object cannot be nearly-empty. A new FIELD_DECL is inserted at
3931 *NEXT_FIELD, unless BINFO is for an empty base class.
3933 Returns the location at which the next field should be inserted. */
3935 static tree *
3936 build_base_field (record_layout_info rli, tree binfo,
3937 splay_tree offsets, tree *next_field)
3939 tree t = rli->t;
3940 tree basetype = BINFO_TYPE (binfo);
3942 if (!COMPLETE_TYPE_P (basetype))
3943 /* This error is now reported in xref_tag, thus giving better
3944 location information. */
3945 return next_field;
3947 /* Place the base class. */
3948 if (!is_empty_class (basetype))
3950 tree decl;
3952 /* The containing class is non-empty because it has a non-empty
3953 base class. */
3954 CLASSTYPE_EMPTY_P (t) = 0;
3956 /* Create the FIELD_DECL. */
3957 decl = build_decl (input_location,
3958 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
3959 DECL_ARTIFICIAL (decl) = 1;
3960 DECL_IGNORED_P (decl) = 1;
3961 DECL_FIELD_CONTEXT (decl) = t;
3962 if (CLASSTYPE_AS_BASE (basetype))
3964 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3965 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3966 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3967 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3968 DECL_MODE (decl) = TYPE_MODE (basetype);
3969 DECL_FIELD_IS_BASE (decl) = 1;
3971 /* Try to place the field. It may take more than one try if we
3972 have a hard time placing the field without putting two
3973 objects of the same type at the same address. */
3974 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
3975 /* Add the new FIELD_DECL to the list of fields for T. */
3976 DECL_CHAIN (decl) = *next_field;
3977 *next_field = decl;
3978 next_field = &DECL_CHAIN (decl);
3981 else
3983 tree eoc;
3984 bool atend;
3986 /* On some platforms (ARM), even empty classes will not be
3987 byte-aligned. */
3988 eoc = round_up_loc (input_location,
3989 rli_size_unit_so_far (rli),
3990 CLASSTYPE_ALIGN_UNIT (basetype));
3991 atend = layout_empty_base (rli, binfo, eoc, offsets);
3992 /* A nearly-empty class "has no proper base class that is empty,
3993 not morally virtual, and at an offset other than zero." */
3994 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
3996 if (atend)
3997 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3998 /* The check above (used in G++ 3.2) is insufficient because
3999 an empty class placed at offset zero might itself have an
4000 empty base at a nonzero offset. */
4001 else if (walk_subobject_offsets (basetype,
4002 empty_base_at_nonzero_offset_p,
4003 size_zero_node,
4004 /*offsets=*/NULL,
4005 /*max_offset=*/NULL_TREE,
4006 /*vbases_p=*/true))
4008 if (abi_version_at_least (2))
4009 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4010 else
4011 warning (OPT_Wabi,
4012 "class %qT will be considered nearly empty in a "
4013 "future version of GCC", t);
4017 /* We do not create a FIELD_DECL for empty base classes because
4018 it might overlap some other field. We want to be able to
4019 create CONSTRUCTORs for the class by iterating over the
4020 FIELD_DECLs, and the back end does not handle overlapping
4021 FIELD_DECLs. */
4023 /* An empty virtual base causes a class to be non-empty
4024 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4025 here because that was already done when the virtual table
4026 pointer was created. */
4029 /* Record the offsets of BINFO and its base subobjects. */
4030 record_subobject_offsets (binfo,
4031 BINFO_OFFSET (binfo),
4032 offsets,
4033 /*is_data_member=*/false);
4035 return next_field;
4038 /* Layout all of the non-virtual base classes. Record empty
4039 subobjects in OFFSETS. T is the most derived type. Return nonzero
4040 if the type cannot be nearly empty. The fields created
4041 corresponding to the base classes will be inserted at
4042 *NEXT_FIELD. */
4044 static void
4045 build_base_fields (record_layout_info rli,
4046 splay_tree offsets, tree *next_field)
4048 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4049 subobjects. */
4050 tree t = rli->t;
4051 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4052 int i;
4054 /* The primary base class is always allocated first. */
4055 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4056 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4057 offsets, next_field);
4059 /* Now allocate the rest of the bases. */
4060 for (i = 0; i < n_baseclasses; ++i)
4062 tree base_binfo;
4064 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4066 /* The primary base was already allocated above, so we don't
4067 need to allocate it again here. */
4068 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4069 continue;
4071 /* Virtual bases are added at the end (a primary virtual base
4072 will have already been added). */
4073 if (BINFO_VIRTUAL_P (base_binfo))
4074 continue;
4076 next_field = build_base_field (rli, base_binfo,
4077 offsets, next_field);
4081 /* Go through the TYPE_METHODS of T issuing any appropriate
4082 diagnostics, figuring out which methods override which other
4083 methods, and so forth. */
4085 static void
4086 check_methods (tree t)
4088 tree x;
4090 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4092 check_for_override (x, t);
4093 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4094 error ("initializer specified for non-virtual method %q+D", x);
4095 /* The name of the field is the original field name
4096 Save this in auxiliary field for later overloading. */
4097 if (DECL_VINDEX (x))
4099 TYPE_POLYMORPHIC_P (t) = 1;
4100 if (DECL_PURE_VIRTUAL_P (x))
4101 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
4103 /* All user-provided destructors are non-trivial.
4104 Constructors and assignment ops are handled in
4105 grok_special_member_properties. */
4106 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4107 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4111 /* FN is a constructor or destructor. Clone the declaration to create
4112 a specialized in-charge or not-in-charge version, as indicated by
4113 NAME. */
4115 static tree
4116 build_clone (tree fn, tree name)
4118 tree parms;
4119 tree clone;
4121 /* Copy the function. */
4122 clone = copy_decl (fn);
4123 /* Reset the function name. */
4124 DECL_NAME (clone) = name;
4125 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4126 /* Remember where this function came from. */
4127 DECL_ABSTRACT_ORIGIN (clone) = fn;
4128 /* Make it easy to find the CLONE given the FN. */
4129 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4130 DECL_CHAIN (fn) = clone;
4132 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4133 if (TREE_CODE (clone) == TEMPLATE_DECL)
4135 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4136 DECL_TEMPLATE_RESULT (clone) = result;
4137 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4138 DECL_TI_TEMPLATE (result) = clone;
4139 TREE_TYPE (clone) = TREE_TYPE (result);
4140 return clone;
4143 DECL_CLONED_FUNCTION (clone) = fn;
4144 /* There's no pending inline data for this function. */
4145 DECL_PENDING_INLINE_INFO (clone) = NULL;
4146 DECL_PENDING_INLINE_P (clone) = 0;
4148 /* The base-class destructor is not virtual. */
4149 if (name == base_dtor_identifier)
4151 DECL_VIRTUAL_P (clone) = 0;
4152 if (TREE_CODE (clone) != TEMPLATE_DECL)
4153 DECL_VINDEX (clone) = NULL_TREE;
4156 /* If there was an in-charge parameter, drop it from the function
4157 type. */
4158 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4160 tree basetype;
4161 tree parmtypes;
4162 tree exceptions;
4164 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4165 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4166 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4167 /* Skip the `this' parameter. */
4168 parmtypes = TREE_CHAIN (parmtypes);
4169 /* Skip the in-charge parameter. */
4170 parmtypes = TREE_CHAIN (parmtypes);
4171 /* And the VTT parm, in a complete [cd]tor. */
4172 if (DECL_HAS_VTT_PARM_P (fn)
4173 && ! DECL_NEEDS_VTT_PARM_P (clone))
4174 parmtypes = TREE_CHAIN (parmtypes);
4175 /* If this is subobject constructor or destructor, add the vtt
4176 parameter. */
4177 TREE_TYPE (clone)
4178 = build_method_type_directly (basetype,
4179 TREE_TYPE (TREE_TYPE (clone)),
4180 parmtypes);
4181 if (exceptions)
4182 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4183 exceptions);
4184 TREE_TYPE (clone)
4185 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4186 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4189 /* Copy the function parameters. */
4190 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4191 /* Remove the in-charge parameter. */
4192 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4194 DECL_CHAIN (DECL_ARGUMENTS (clone))
4195 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4196 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4198 /* And the VTT parm, in a complete [cd]tor. */
4199 if (DECL_HAS_VTT_PARM_P (fn))
4201 if (DECL_NEEDS_VTT_PARM_P (clone))
4202 DECL_HAS_VTT_PARM_P (clone) = 1;
4203 else
4205 DECL_CHAIN (DECL_ARGUMENTS (clone))
4206 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4207 DECL_HAS_VTT_PARM_P (clone) = 0;
4211 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4213 DECL_CONTEXT (parms) = clone;
4214 cxx_dup_lang_specific_decl (parms);
4217 /* Create the RTL for this function. */
4218 SET_DECL_RTL (clone, NULL);
4219 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4221 if (pch_file)
4222 note_decl_for_pch (clone);
4224 return clone;
4227 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4228 not invoke this function directly.
4230 For a non-thunk function, returns the address of the slot for storing
4231 the function it is a clone of. Otherwise returns NULL_TREE.
4233 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4234 cloned_function is unset. This is to support the separate
4235 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4236 on a template makes sense, but not the former. */
4238 tree *
4239 decl_cloned_function_p (const_tree decl, bool just_testing)
4241 tree *ptr;
4242 if (just_testing)
4243 decl = STRIP_TEMPLATE (decl);
4245 if (TREE_CODE (decl) != FUNCTION_DECL
4246 || !DECL_LANG_SPECIFIC (decl)
4247 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4249 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4250 if (!just_testing)
4251 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4252 else
4253 #endif
4254 return NULL;
4257 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4258 if (just_testing && *ptr == NULL_TREE)
4259 return NULL;
4260 else
4261 return ptr;
4264 /* Produce declarations for all appropriate clones of FN. If
4265 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4266 CLASTYPE_METHOD_VEC as well. */
4268 void
4269 clone_function_decl (tree fn, int update_method_vec_p)
4271 tree clone;
4273 /* Avoid inappropriate cloning. */
4274 if (DECL_CHAIN (fn)
4275 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4276 return;
4278 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4280 /* For each constructor, we need two variants: an in-charge version
4281 and a not-in-charge version. */
4282 clone = build_clone (fn, complete_ctor_identifier);
4283 if (update_method_vec_p)
4284 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4285 clone = build_clone (fn, base_ctor_identifier);
4286 if (update_method_vec_p)
4287 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4289 else
4291 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4293 /* For each destructor, we need three variants: an in-charge
4294 version, a not-in-charge version, and an in-charge deleting
4295 version. We clone the deleting version first because that
4296 means it will go second on the TYPE_METHODS list -- and that
4297 corresponds to the correct layout order in the virtual
4298 function table.
4300 For a non-virtual destructor, we do not build a deleting
4301 destructor. */
4302 if (DECL_VIRTUAL_P (fn))
4304 clone = build_clone (fn, deleting_dtor_identifier);
4305 if (update_method_vec_p)
4306 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4308 clone = build_clone (fn, complete_dtor_identifier);
4309 if (update_method_vec_p)
4310 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4311 clone = build_clone (fn, base_dtor_identifier);
4312 if (update_method_vec_p)
4313 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4316 /* Note that this is an abstract function that is never emitted. */
4317 DECL_ABSTRACT (fn) = 1;
4320 /* DECL is an in charge constructor, which is being defined. This will
4321 have had an in class declaration, from whence clones were
4322 declared. An out-of-class definition can specify additional default
4323 arguments. As it is the clones that are involved in overload
4324 resolution, we must propagate the information from the DECL to its
4325 clones. */
4327 void
4328 adjust_clone_args (tree decl)
4330 tree clone;
4332 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4333 clone = DECL_CHAIN (clone))
4335 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4336 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4337 tree decl_parms, clone_parms;
4339 clone_parms = orig_clone_parms;
4341 /* Skip the 'this' parameter. */
4342 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4343 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4345 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4346 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4347 if (DECL_HAS_VTT_PARM_P (decl))
4348 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4350 clone_parms = orig_clone_parms;
4351 if (DECL_HAS_VTT_PARM_P (clone))
4352 clone_parms = TREE_CHAIN (clone_parms);
4354 for (decl_parms = orig_decl_parms; decl_parms;
4355 decl_parms = TREE_CHAIN (decl_parms),
4356 clone_parms = TREE_CHAIN (clone_parms))
4358 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4359 TREE_TYPE (clone_parms)));
4361 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4363 /* A default parameter has been added. Adjust the
4364 clone's parameters. */
4365 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4366 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4367 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4368 tree type;
4370 clone_parms = orig_decl_parms;
4372 if (DECL_HAS_VTT_PARM_P (clone))
4374 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4375 TREE_VALUE (orig_clone_parms),
4376 clone_parms);
4377 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4379 type = build_method_type_directly (basetype,
4380 TREE_TYPE (TREE_TYPE (clone)),
4381 clone_parms);
4382 if (exceptions)
4383 type = build_exception_variant (type, exceptions);
4384 if (attrs)
4385 type = cp_build_type_attribute_variant (type, attrs);
4386 TREE_TYPE (clone) = type;
4388 clone_parms = NULL_TREE;
4389 break;
4392 gcc_assert (!clone_parms);
4396 /* For each of the constructors and destructors in T, create an
4397 in-charge and not-in-charge variant. */
4399 static void
4400 clone_constructors_and_destructors (tree t)
4402 tree fns;
4404 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4405 out now. */
4406 if (!CLASSTYPE_METHOD_VEC (t))
4407 return;
4409 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4410 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4411 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4412 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4415 /* Deduce noexcept for a destructor DTOR. */
4417 void
4418 deduce_noexcept_on_destructor (tree dtor)
4420 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4422 tree ctx = DECL_CONTEXT (dtor);
4423 tree implicit_fn = implicitly_declare_fn (sfk_destructor, ctx,
4424 /*const_p=*/false,
4425 NULL, NULL);
4426 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
4427 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4431 /* For each destructor in T, deduce noexcept:
4433 12.4/3: A declaration of a destructor that does not have an
4434 exception-specification is implicitly considered to have the
4435 same exception-specification as an implicit declaration (15.4). */
4437 static void
4438 deduce_noexcept_on_destructors (tree t)
4440 tree fns;
4442 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4443 out now. */
4444 if (!CLASSTYPE_METHOD_VEC (t))
4445 return;
4447 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4448 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4451 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4452 of TYPE for virtual functions which FNDECL overrides. Return a
4453 mask of the tm attributes found therein. */
4455 static int
4456 look_for_tm_attr_overrides (tree type, tree fndecl)
4458 tree binfo = TYPE_BINFO (type);
4459 tree base_binfo;
4460 int ix, found = 0;
4462 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4464 tree o, basetype = BINFO_TYPE (base_binfo);
4466 if (!TYPE_POLYMORPHIC_P (basetype))
4467 continue;
4469 o = look_for_overrides_here (basetype, fndecl);
4470 if (o)
4471 found |= tm_attr_to_mask (find_tm_attribute
4472 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4473 else
4474 found |= look_for_tm_attr_overrides (basetype, fndecl);
4477 return found;
4480 /* Subroutine of set_method_tm_attributes. Handle the checks and
4481 inheritance for one virtual method FNDECL. */
4483 static void
4484 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4486 tree tm_attr;
4487 int found, have;
4489 found = look_for_tm_attr_overrides (type, fndecl);
4491 /* If FNDECL doesn't actually override anything (i.e. T is the
4492 class that first declares FNDECL virtual), then we're done. */
4493 if (found == 0)
4494 return;
4496 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4497 have = tm_attr_to_mask (tm_attr);
4499 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4500 tm_pure must match exactly, otherwise no weakening of
4501 tm_safe > tm_callable > nothing. */
4502 /* ??? The tm_pure attribute didn't make the transition to the
4503 multivendor language spec. */
4504 if (have == TM_ATTR_PURE)
4506 if (found != TM_ATTR_PURE)
4508 found &= -found;
4509 goto err_override;
4512 /* If the overridden function is tm_pure, then FNDECL must be. */
4513 else if (found == TM_ATTR_PURE && tm_attr)
4514 goto err_override;
4515 /* Look for base class combinations that cannot be satisfied. */
4516 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4518 found &= ~TM_ATTR_PURE;
4519 found &= -found;
4520 error_at (DECL_SOURCE_LOCATION (fndecl),
4521 "method overrides both %<transaction_pure%> and %qE methods",
4522 tm_mask_to_attr (found));
4524 /* If FNDECL did not declare an attribute, then inherit the most
4525 restrictive one. */
4526 else if (tm_attr == NULL)
4528 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
4530 /* Otherwise validate that we're not weaker than a function
4531 that is being overridden. */
4532 else
4534 found &= -found;
4535 if (found <= TM_ATTR_CALLABLE && have > found)
4536 goto err_override;
4538 return;
4540 err_override:
4541 error_at (DECL_SOURCE_LOCATION (fndecl),
4542 "method declared %qE overriding %qE method",
4543 tm_attr, tm_mask_to_attr (found));
4546 /* For each of the methods in T, propagate a class-level tm attribute. */
4548 static void
4549 set_method_tm_attributes (tree t)
4551 tree class_tm_attr, fndecl;
4553 /* Don't bother collecting tm attributes if transactional memory
4554 support is not enabled. */
4555 if (!flag_tm)
4556 return;
4558 /* Process virtual methods first, as they inherit directly from the
4559 base virtual function and also require validation of new attributes. */
4560 if (TYPE_CONTAINS_VPTR_P (t))
4562 tree vchain;
4563 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4564 vchain = TREE_CHAIN (vchain))
4566 fndecl = BV_FN (vchain);
4567 if (DECL_THUNK_P (fndecl))
4568 fndecl = THUNK_TARGET (fndecl);
4569 set_one_vmethod_tm_attributes (t, fndecl);
4573 /* If the class doesn't have an attribute, nothing more to do. */
4574 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4575 if (class_tm_attr == NULL)
4576 return;
4578 /* Any method that does not yet have a tm attribute inherits
4579 the one from the class. */
4580 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
4582 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4583 apply_tm_attr (fndecl, class_tm_attr);
4587 /* Returns true iff class T has a user-defined constructor other than
4588 the default constructor. */
4590 bool
4591 type_has_user_nondefault_constructor (tree t)
4593 tree fns;
4595 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4596 return false;
4598 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4600 tree fn = OVL_CURRENT (fns);
4601 if (!DECL_ARTIFICIAL (fn)
4602 && (TREE_CODE (fn) == TEMPLATE_DECL
4603 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4604 != NULL_TREE)))
4605 return true;
4608 return false;
4611 /* Returns the defaulted constructor if T has one. Otherwise, returns
4612 NULL_TREE. */
4614 tree
4615 in_class_defaulted_default_constructor (tree t)
4617 tree fns, args;
4619 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4620 return NULL_TREE;
4622 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4624 tree fn = OVL_CURRENT (fns);
4626 if (DECL_DEFAULTED_IN_CLASS_P (fn))
4628 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
4629 while (args && TREE_PURPOSE (args))
4630 args = TREE_CHAIN (args);
4631 if (!args || args == void_list_node)
4632 return fn;
4636 return NULL_TREE;
4639 /* Returns true iff FN is a user-provided function, i.e. user-declared
4640 and not defaulted at its first declaration; or explicit, private,
4641 protected, or non-const. */
4643 bool
4644 user_provided_p (tree fn)
4646 if (TREE_CODE (fn) == TEMPLATE_DECL)
4647 return true;
4648 else
4649 return (!DECL_ARTIFICIAL (fn)
4650 && !DECL_DEFAULTED_IN_CLASS_P (fn));
4653 /* Returns true iff class T has a user-provided constructor. */
4655 bool
4656 type_has_user_provided_constructor (tree t)
4658 tree fns;
4660 if (!CLASS_TYPE_P (t))
4661 return false;
4663 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4664 return false;
4666 /* This can happen in error cases; avoid crashing. */
4667 if (!CLASSTYPE_METHOD_VEC (t))
4668 return false;
4670 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4671 if (user_provided_p (OVL_CURRENT (fns)))
4672 return true;
4674 return false;
4677 /* Returns true iff class T has a user-provided default constructor. */
4679 bool
4680 type_has_user_provided_default_constructor (tree t)
4682 tree fns;
4684 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4685 return false;
4687 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4689 tree fn = OVL_CURRENT (fns);
4690 if (TREE_CODE (fn) == FUNCTION_DECL
4691 && user_provided_p (fn)
4692 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
4693 return true;
4696 return false;
4699 /* If default-initialization leaves part of TYPE uninitialized, returns
4700 a DECL for the field or TYPE itself (DR 253). */
4702 tree
4703 default_init_uninitialized_part (tree type)
4705 tree t, r, binfo;
4706 int i;
4708 type = strip_array_types (type);
4709 if (!CLASS_TYPE_P (type))
4710 return type;
4711 if (type_has_user_provided_default_constructor (type))
4712 return NULL_TREE;
4713 for (binfo = TYPE_BINFO (type), i = 0;
4714 BINFO_BASE_ITERATE (binfo, i, t); ++i)
4716 r = default_init_uninitialized_part (BINFO_TYPE (t));
4717 if (r)
4718 return r;
4720 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
4721 if (TREE_CODE (t) == FIELD_DECL
4722 && !DECL_ARTIFICIAL (t)
4723 && !DECL_INITIAL (t))
4725 r = default_init_uninitialized_part (TREE_TYPE (t));
4726 if (r)
4727 return DECL_P (r) ? r : t;
4730 return NULL_TREE;
4733 /* Returns true iff for class T, a trivial synthesized default constructor
4734 would be constexpr. */
4736 bool
4737 trivial_default_constructor_is_constexpr (tree t)
4739 /* A defaulted trivial default constructor is constexpr
4740 if there is nothing to initialize. */
4741 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
4742 return is_really_empty_class (t);
4745 /* Returns true iff class T has a constexpr default constructor. */
4747 bool
4748 type_has_constexpr_default_constructor (tree t)
4750 tree fns;
4752 if (!CLASS_TYPE_P (t))
4754 /* The caller should have stripped an enclosing array. */
4755 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
4756 return false;
4758 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
4760 if (!TYPE_HAS_COMPLEX_DFLT (t))
4761 return trivial_default_constructor_is_constexpr (t);
4762 /* Non-trivial, we need to check subobject constructors. */
4763 lazily_declare_fn (sfk_constructor, t);
4765 fns = locate_ctor (t);
4766 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
4769 /* Returns true iff class TYPE has a virtual destructor. */
4771 bool
4772 type_has_virtual_destructor (tree type)
4774 tree dtor;
4776 if (!CLASS_TYPE_P (type))
4777 return false;
4779 gcc_assert (COMPLETE_TYPE_P (type));
4780 dtor = CLASSTYPE_DESTRUCTORS (type);
4781 return (dtor && DECL_VIRTUAL_P (dtor));
4784 /* Returns true iff class T has a move constructor. */
4786 bool
4787 type_has_move_constructor (tree t)
4789 tree fns;
4791 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4793 gcc_assert (COMPLETE_TYPE_P (t));
4794 lazily_declare_fn (sfk_move_constructor, t);
4797 if (!CLASSTYPE_METHOD_VEC (t))
4798 return false;
4800 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4801 if (move_fn_p (OVL_CURRENT (fns)))
4802 return true;
4804 return false;
4807 /* Returns true iff class T has a move assignment operator. */
4809 bool
4810 type_has_move_assign (tree t)
4812 tree fns;
4814 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
4816 gcc_assert (COMPLETE_TYPE_P (t));
4817 lazily_declare_fn (sfk_move_assignment, t);
4820 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
4821 fns; fns = OVL_NEXT (fns))
4822 if (move_fn_p (OVL_CURRENT (fns)))
4823 return true;
4825 return false;
4828 /* Returns true iff class T has a move constructor that was explicitly
4829 declared in the class body. Note that this is different from
4830 "user-provided", which doesn't include functions that are defaulted in
4831 the class. */
4833 bool
4834 type_has_user_declared_move_constructor (tree t)
4836 tree fns;
4838 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4839 return false;
4841 if (!CLASSTYPE_METHOD_VEC (t))
4842 return false;
4844 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4846 tree fn = OVL_CURRENT (fns);
4847 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
4848 return true;
4851 return false;
4854 /* Returns true iff class T has a move assignment operator that was
4855 explicitly declared in the class body. */
4857 bool
4858 type_has_user_declared_move_assign (tree t)
4860 tree fns;
4862 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
4863 return false;
4865 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
4866 fns; fns = OVL_NEXT (fns))
4868 tree fn = OVL_CURRENT (fns);
4869 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
4870 return true;
4873 return false;
4876 /* Nonzero if we need to build up a constructor call when initializing an
4877 object of this class, either because it has a user-provided constructor
4878 or because it doesn't have a default constructor (so we need to give an
4879 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
4880 what you care about is whether or not an object can be produced by a
4881 constructor (e.g. so we don't set TREE_READONLY on const variables of
4882 such type); use this function when what you care about is whether or not
4883 to try to call a constructor to create an object. The latter case is
4884 the former plus some cases of constructors that cannot be called. */
4886 bool
4887 type_build_ctor_call (tree t)
4889 tree inner;
4890 if (TYPE_NEEDS_CONSTRUCTING (t))
4891 return true;
4892 inner = strip_array_types (t);
4893 return (CLASS_TYPE_P (inner) && !TYPE_HAS_DEFAULT_CONSTRUCTOR (inner)
4894 && !ANON_AGGR_TYPE_P (inner));
4897 /* Remove all zero-width bit-fields from T. */
4899 static void
4900 remove_zero_width_bit_fields (tree t)
4902 tree *fieldsp;
4904 fieldsp = &TYPE_FIELDS (t);
4905 while (*fieldsp)
4907 if (TREE_CODE (*fieldsp) == FIELD_DECL
4908 && DECL_C_BIT_FIELD (*fieldsp)
4909 /* We should not be confused by the fact that grokbitfield
4910 temporarily sets the width of the bit field into
4911 DECL_INITIAL (*fieldsp).
4912 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
4913 to that width. */
4914 && integer_zerop (DECL_SIZE (*fieldsp)))
4915 *fieldsp = DECL_CHAIN (*fieldsp);
4916 else
4917 fieldsp = &DECL_CHAIN (*fieldsp);
4921 /* Returns TRUE iff we need a cookie when dynamically allocating an
4922 array whose elements have the indicated class TYPE. */
4924 static bool
4925 type_requires_array_cookie (tree type)
4927 tree fns;
4928 bool has_two_argument_delete_p = false;
4930 gcc_assert (CLASS_TYPE_P (type));
4932 /* If there's a non-trivial destructor, we need a cookie. In order
4933 to iterate through the array calling the destructor for each
4934 element, we'll have to know how many elements there are. */
4935 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4936 return true;
4938 /* If the usual deallocation function is a two-argument whose second
4939 argument is of type `size_t', then we have to pass the size of
4940 the array to the deallocation function, so we will need to store
4941 a cookie. */
4942 fns = lookup_fnfields (TYPE_BINFO (type),
4943 ansi_opname (VEC_DELETE_EXPR),
4944 /*protect=*/0);
4945 /* If there are no `operator []' members, or the lookup is
4946 ambiguous, then we don't need a cookie. */
4947 if (!fns || fns == error_mark_node)
4948 return false;
4949 /* Loop through all of the functions. */
4950 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
4952 tree fn;
4953 tree second_parm;
4955 /* Select the current function. */
4956 fn = OVL_CURRENT (fns);
4957 /* See if this function is a one-argument delete function. If
4958 it is, then it will be the usual deallocation function. */
4959 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4960 if (second_parm == void_list_node)
4961 return false;
4962 /* Do not consider this function if its second argument is an
4963 ellipsis. */
4964 if (!second_parm)
4965 continue;
4966 /* Otherwise, if we have a two-argument function and the second
4967 argument is `size_t', it will be the usual deallocation
4968 function -- unless there is one-argument function, too. */
4969 if (TREE_CHAIN (second_parm) == void_list_node
4970 && same_type_p (TREE_VALUE (second_parm), size_type_node))
4971 has_two_argument_delete_p = true;
4974 return has_two_argument_delete_p;
4977 /* Finish computing the `literal type' property of class type T.
4979 At this point, we have already processed base classes and
4980 non-static data members. We need to check whether the copy
4981 constructor is trivial, the destructor is trivial, and there
4982 is a trivial default constructor or at least one constexpr
4983 constructor other than the copy constructor. */
4985 static void
4986 finalize_literal_type_property (tree t)
4988 tree fn;
4990 if (cxx_dialect < cxx0x
4991 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
4992 CLASSTYPE_LITERAL_P (t) = false;
4993 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
4994 && CLASSTYPE_NON_AGGREGATE (t)
4995 && !TYPE_HAS_CONSTEXPR_CTOR (t))
4996 CLASSTYPE_LITERAL_P (t) = false;
4998 if (!CLASSTYPE_LITERAL_P (t))
4999 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5000 if (DECL_DECLARED_CONSTEXPR_P (fn)
5001 && TREE_CODE (fn) != TEMPLATE_DECL
5002 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5003 && !DECL_CONSTRUCTOR_P (fn))
5005 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5006 if (!DECL_GENERATED_P (fn))
5008 error ("enclosing class of constexpr non-static member "
5009 "function %q+#D is not a literal type", fn);
5010 explain_non_literal_class (t);
5015 /* T is a non-literal type used in a context which requires a constant
5016 expression. Explain why it isn't literal. */
5018 void
5019 explain_non_literal_class (tree t)
5021 static struct pointer_set_t *diagnosed;
5023 if (!CLASS_TYPE_P (t))
5024 return;
5025 t = TYPE_MAIN_VARIANT (t);
5027 if (diagnosed == NULL)
5028 diagnosed = pointer_set_create ();
5029 if (pointer_set_insert (diagnosed, t) != 0)
5030 /* Already explained. */
5031 return;
5033 inform (0, "%q+T is not literal because:", t);
5034 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5035 inform (0, " %q+T has a non-trivial destructor", t);
5036 else if (CLASSTYPE_NON_AGGREGATE (t)
5037 && !TYPE_HAS_TRIVIAL_DFLT (t)
5038 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5040 inform (0, " %q+T is not an aggregate, does not have a trivial "
5041 "default constructor, and has no constexpr constructor that "
5042 "is not a copy or move constructor", t);
5043 if (TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5044 && !type_has_user_provided_default_constructor (t))
5046 /* Note that we can't simply call locate_ctor because when the
5047 constructor is deleted it just returns NULL_TREE. */
5048 tree fns;
5049 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5051 tree fn = OVL_CURRENT (fns);
5052 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5054 parms = skip_artificial_parms_for (fn, parms);
5056 if (sufficient_parms_p (parms))
5058 if (DECL_DELETED_FN (fn))
5059 maybe_explain_implicit_delete (fn);
5060 else
5061 explain_invalid_constexpr_fn (fn);
5062 break;
5067 else
5069 tree binfo, base_binfo, field; int i;
5070 for (binfo = TYPE_BINFO (t), i = 0;
5071 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5073 tree basetype = TREE_TYPE (base_binfo);
5074 if (!CLASSTYPE_LITERAL_P (basetype))
5076 inform (0, " base class %qT of %q+T is non-literal",
5077 basetype, t);
5078 explain_non_literal_class (basetype);
5079 return;
5082 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5084 tree ftype;
5085 if (TREE_CODE (field) != FIELD_DECL)
5086 continue;
5087 ftype = TREE_TYPE (field);
5088 if (!literal_type_p (ftype))
5090 inform (0, " non-static data member %q+D has "
5091 "non-literal type", field);
5092 if (CLASS_TYPE_P (ftype))
5093 explain_non_literal_class (ftype);
5099 /* Check the validity of the bases and members declared in T. Add any
5100 implicitly-generated functions (like copy-constructors and
5101 assignment operators). Compute various flag bits (like
5102 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5103 level: i.e., independently of the ABI in use. */
5105 static void
5106 check_bases_and_members (tree t)
5108 /* Nonzero if the implicitly generated copy constructor should take
5109 a non-const reference argument. */
5110 int cant_have_const_ctor;
5111 /* Nonzero if the implicitly generated assignment operator
5112 should take a non-const reference argument. */
5113 int no_const_asn_ref;
5114 tree access_decls;
5115 bool saved_complex_asn_ref;
5116 bool saved_nontrivial_dtor;
5117 tree fn;
5119 /* By default, we use const reference arguments and generate default
5120 constructors. */
5121 cant_have_const_ctor = 0;
5122 no_const_asn_ref = 0;
5124 /* Deduce noexcept on destructors. */
5125 if (cxx_dialect >= cxx0x)
5126 deduce_noexcept_on_destructors (t);
5128 /* Check all the base-classes. */
5129 check_bases (t, &cant_have_const_ctor,
5130 &no_const_asn_ref);
5132 /* Check all the method declarations. */
5133 check_methods (t);
5135 /* Save the initial values of these flags which only indicate whether
5136 or not the class has user-provided functions. As we analyze the
5137 bases and members we can set these flags for other reasons. */
5138 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5139 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5141 /* Check all the data member declarations. We cannot call
5142 check_field_decls until we have called check_bases check_methods,
5143 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5144 being set appropriately. */
5145 check_field_decls (t, &access_decls,
5146 &cant_have_const_ctor,
5147 &no_const_asn_ref);
5149 /* A nearly-empty class has to be vptr-containing; a nearly empty
5150 class contains just a vptr. */
5151 if (!TYPE_CONTAINS_VPTR_P (t))
5152 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5154 /* Do some bookkeeping that will guide the generation of implicitly
5155 declared member functions. */
5156 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5157 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5158 /* We need to call a constructor for this class if it has a
5159 user-provided constructor, or if the default constructor is going
5160 to initialize the vptr. (This is not an if-and-only-if;
5161 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5162 themselves need constructing.) */
5163 TYPE_NEEDS_CONSTRUCTING (t)
5164 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5165 /* [dcl.init.aggr]
5167 An aggregate is an array or a class with no user-provided
5168 constructors ... and no virtual functions.
5170 Again, other conditions for being an aggregate are checked
5171 elsewhere. */
5172 CLASSTYPE_NON_AGGREGATE (t)
5173 |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
5174 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5175 retain the old definition internally for ABI reasons. */
5176 CLASSTYPE_NON_LAYOUT_POD_P (t)
5177 |= (CLASSTYPE_NON_AGGREGATE (t)
5178 || saved_nontrivial_dtor || saved_complex_asn_ref);
5179 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5180 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5181 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5182 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5184 /* If the class has no user-declared constructor, but does have
5185 non-static const or reference data members that can never be
5186 initialized, issue a warning. */
5187 if (warn_uninitialized
5188 /* Classes with user-declared constructors are presumed to
5189 initialize these members. */
5190 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5191 /* Aggregates can be initialized with brace-enclosed
5192 initializers. */
5193 && CLASSTYPE_NON_AGGREGATE (t))
5195 tree field;
5197 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5199 tree type;
5201 if (TREE_CODE (field) != FIELD_DECL
5202 || DECL_INITIAL (field) != NULL_TREE)
5203 continue;
5205 type = TREE_TYPE (field);
5206 if (TREE_CODE (type) == REFERENCE_TYPE)
5207 warning (OPT_Wuninitialized, "non-static reference %q+#D "
5208 "in class without a constructor", field);
5209 else if (CP_TYPE_CONST_P (type)
5210 && (!CLASS_TYPE_P (type)
5211 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5212 warning (OPT_Wuninitialized, "non-static const member %q+#D "
5213 "in class without a constructor", field);
5217 /* Synthesize any needed methods. */
5218 add_implicitly_declared_members (t, &access_decls,
5219 cant_have_const_ctor,
5220 no_const_asn_ref);
5222 /* Check defaulted declarations here so we have cant_have_const_ctor
5223 and don't need to worry about clones. */
5224 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5225 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5227 int copy = copy_fn_p (fn);
5228 if (copy > 0)
5230 bool imp_const_p
5231 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5232 : !no_const_asn_ref);
5233 bool fn_const_p = (copy == 2);
5235 if (fn_const_p && !imp_const_p)
5236 /* If the function is defaulted outside the class, we just
5237 give the synthesis error. */
5238 error ("%q+D declared to take const reference, but implicit "
5239 "declaration would take non-const", fn);
5241 defaulted_late_check (fn);
5244 if (LAMBDA_TYPE_P (t))
5246 /* "The closure type associated with a lambda-expression has a deleted
5247 default constructor and a deleted copy assignment operator." */
5248 TYPE_NEEDS_CONSTRUCTING (t) = 1;
5249 TYPE_HAS_COMPLEX_DFLT (t) = 1;
5250 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
5251 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 0;
5253 /* "This class type is not an aggregate." */
5254 CLASSTYPE_NON_AGGREGATE (t) = 1;
5257 /* Compute the 'literal type' property before we
5258 do anything with non-static member functions. */
5259 finalize_literal_type_property (t);
5261 /* Create the in-charge and not-in-charge variants of constructors
5262 and destructors. */
5263 clone_constructors_and_destructors (t);
5265 /* Process the using-declarations. */
5266 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5267 handle_using_decl (TREE_VALUE (access_decls), t);
5269 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5270 finish_struct_methods (t);
5272 /* Figure out whether or not we will need a cookie when dynamically
5273 allocating an array of this type. */
5274 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5275 = type_requires_array_cookie (t);
5278 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5279 accordingly. If a new vfield was created (because T doesn't have a
5280 primary base class), then the newly created field is returned. It
5281 is not added to the TYPE_FIELDS list; it is the caller's
5282 responsibility to do that. Accumulate declared virtual functions
5283 on VIRTUALS_P. */
5285 static tree
5286 create_vtable_ptr (tree t, tree* virtuals_p)
5288 tree fn;
5290 /* Collect the virtual functions declared in T. */
5291 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5292 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5293 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5295 tree new_virtual = make_node (TREE_LIST);
5297 BV_FN (new_virtual) = fn;
5298 BV_DELTA (new_virtual) = integer_zero_node;
5299 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5301 TREE_CHAIN (new_virtual) = *virtuals_p;
5302 *virtuals_p = new_virtual;
5305 /* If we couldn't find an appropriate base class, create a new field
5306 here. Even if there weren't any new virtual functions, we might need a
5307 new virtual function table if we're supposed to include vptrs in
5308 all classes that need them. */
5309 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5311 /* We build this decl with vtbl_ptr_type_node, which is a
5312 `vtable_entry_type*'. It might seem more precise to use
5313 `vtable_entry_type (*)[N]' where N is the number of virtual
5314 functions. However, that would require the vtable pointer in
5315 base classes to have a different type than the vtable pointer
5316 in derived classes. We could make that happen, but that
5317 still wouldn't solve all the problems. In particular, the
5318 type-based alias analysis code would decide that assignments
5319 to the base class vtable pointer can't alias assignments to
5320 the derived class vtable pointer, since they have different
5321 types. Thus, in a derived class destructor, where the base
5322 class constructor was inlined, we could generate bad code for
5323 setting up the vtable pointer.
5325 Therefore, we use one type for all vtable pointers. We still
5326 use a type-correct type; it's just doesn't indicate the array
5327 bounds. That's better than using `void*' or some such; it's
5328 cleaner, and it let's the alias analysis code know that these
5329 stores cannot alias stores to void*! */
5330 tree field;
5332 field = build_decl (input_location,
5333 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5334 DECL_VIRTUAL_P (field) = 1;
5335 DECL_ARTIFICIAL (field) = 1;
5336 DECL_FIELD_CONTEXT (field) = t;
5337 DECL_FCONTEXT (field) = t;
5338 if (TYPE_PACKED (t))
5339 DECL_PACKED (field) = 1;
5341 TYPE_VFIELD (t) = field;
5343 /* This class is non-empty. */
5344 CLASSTYPE_EMPTY_P (t) = 0;
5346 return field;
5349 return NULL_TREE;
5352 /* Add OFFSET to all base types of BINFO which is a base in the
5353 hierarchy dominated by T.
5355 OFFSET, which is a type offset, is number of bytes. */
5357 static void
5358 propagate_binfo_offsets (tree binfo, tree offset)
5360 int i;
5361 tree primary_binfo;
5362 tree base_binfo;
5364 /* Update BINFO's offset. */
5365 BINFO_OFFSET (binfo)
5366 = convert (sizetype,
5367 size_binop (PLUS_EXPR,
5368 convert (ssizetype, BINFO_OFFSET (binfo)),
5369 offset));
5371 /* Find the primary base class. */
5372 primary_binfo = get_primary_binfo (binfo);
5374 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5375 propagate_binfo_offsets (primary_binfo, offset);
5377 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5378 downwards. */
5379 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5381 /* Don't do the primary base twice. */
5382 if (base_binfo == primary_binfo)
5383 continue;
5385 if (BINFO_VIRTUAL_P (base_binfo))
5386 continue;
5388 propagate_binfo_offsets (base_binfo, offset);
5392 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5393 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5394 empty subobjects of T. */
5396 static void
5397 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5399 tree vbase;
5400 tree t = rli->t;
5401 bool first_vbase = true;
5402 tree *next_field;
5404 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5405 return;
5407 if (!abi_version_at_least(2))
5409 /* In G++ 3.2, we incorrectly rounded the size before laying out
5410 the virtual bases. */
5411 finish_record_layout (rli, /*free_p=*/false);
5412 #ifdef STRUCTURE_SIZE_BOUNDARY
5413 /* Packed structures don't need to have minimum size. */
5414 if (! TYPE_PACKED (t))
5415 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
5416 #endif
5417 rli->offset = TYPE_SIZE_UNIT (t);
5418 rli->bitpos = bitsize_zero_node;
5419 rli->record_align = TYPE_ALIGN (t);
5422 /* Find the last field. The artificial fields created for virtual
5423 bases will go after the last extant field to date. */
5424 next_field = &TYPE_FIELDS (t);
5425 while (*next_field)
5426 next_field = &DECL_CHAIN (*next_field);
5428 /* Go through the virtual bases, allocating space for each virtual
5429 base that is not already a primary base class. These are
5430 allocated in inheritance graph order. */
5431 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5433 if (!BINFO_VIRTUAL_P (vbase))
5434 continue;
5436 if (!BINFO_PRIMARY_P (vbase))
5438 tree basetype = TREE_TYPE (vbase);
5440 /* This virtual base is not a primary base of any class in the
5441 hierarchy, so we have to add space for it. */
5442 next_field = build_base_field (rli, vbase,
5443 offsets, next_field);
5445 /* If the first virtual base might have been placed at a
5446 lower address, had we started from CLASSTYPE_SIZE, rather
5447 than TYPE_SIZE, issue a warning. There can be both false
5448 positives and false negatives from this warning in rare
5449 cases; to deal with all the possibilities would probably
5450 require performing both layout algorithms and comparing
5451 the results which is not particularly tractable. */
5452 if (warn_abi
5453 && first_vbase
5454 && (tree_int_cst_lt
5455 (size_binop (CEIL_DIV_EXPR,
5456 round_up_loc (input_location,
5457 CLASSTYPE_SIZE (t),
5458 CLASSTYPE_ALIGN (basetype)),
5459 bitsize_unit_node),
5460 BINFO_OFFSET (vbase))))
5461 warning (OPT_Wabi,
5462 "offset of virtual base %qT is not ABI-compliant and "
5463 "may change in a future version of GCC",
5464 basetype);
5466 first_vbase = false;
5471 /* Returns the offset of the byte just past the end of the base class
5472 BINFO. */
5474 static tree
5475 end_of_base (tree binfo)
5477 tree size;
5479 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5480 size = TYPE_SIZE_UNIT (char_type_node);
5481 else if (is_empty_class (BINFO_TYPE (binfo)))
5482 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5483 allocate some space for it. It cannot have virtual bases, so
5484 TYPE_SIZE_UNIT is fine. */
5485 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5486 else
5487 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5489 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5492 /* Returns the offset of the byte just past the end of the base class
5493 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5494 only non-virtual bases are included. */
5496 static tree
5497 end_of_class (tree t, int include_virtuals_p)
5499 tree result = size_zero_node;
5500 VEC(tree,gc) *vbases;
5501 tree binfo;
5502 tree base_binfo;
5503 tree offset;
5504 int i;
5506 for (binfo = TYPE_BINFO (t), i = 0;
5507 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5509 if (!include_virtuals_p
5510 && BINFO_VIRTUAL_P (base_binfo)
5511 && (!BINFO_PRIMARY_P (base_binfo)
5512 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5513 continue;
5515 offset = end_of_base (base_binfo);
5516 if (INT_CST_LT_UNSIGNED (result, offset))
5517 result = offset;
5520 /* G++ 3.2 did not check indirect virtual bases. */
5521 if (abi_version_at_least (2) && include_virtuals_p)
5522 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5523 VEC_iterate (tree, vbases, i, base_binfo); i++)
5525 offset = end_of_base (base_binfo);
5526 if (INT_CST_LT_UNSIGNED (result, offset))
5527 result = offset;
5530 return result;
5533 /* Warn about bases of T that are inaccessible because they are
5534 ambiguous. For example:
5536 struct S {};
5537 struct T : public S {};
5538 struct U : public S, public T {};
5540 Here, `(S*) new U' is not allowed because there are two `S'
5541 subobjects of U. */
5543 static void
5544 warn_about_ambiguous_bases (tree t)
5546 int i;
5547 VEC(tree,gc) *vbases;
5548 tree basetype;
5549 tree binfo;
5550 tree base_binfo;
5552 /* If there are no repeated bases, nothing can be ambiguous. */
5553 if (!CLASSTYPE_REPEATED_BASE_P (t))
5554 return;
5556 /* Check direct bases. */
5557 for (binfo = TYPE_BINFO (t), i = 0;
5558 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5560 basetype = BINFO_TYPE (base_binfo);
5562 if (!uniquely_derived_from_p (basetype, t))
5563 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5564 basetype, t);
5567 /* Check for ambiguous virtual bases. */
5568 if (extra_warnings)
5569 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5570 VEC_iterate (tree, vbases, i, binfo); i++)
5572 basetype = BINFO_TYPE (binfo);
5574 if (!uniquely_derived_from_p (basetype, t))
5575 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
5576 "to ambiguity", basetype, t);
5580 /* Compare two INTEGER_CSTs K1 and K2. */
5582 static int
5583 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5585 return tree_int_cst_compare ((tree) k1, (tree) k2);
5588 /* Increase the size indicated in RLI to account for empty classes
5589 that are "off the end" of the class. */
5591 static void
5592 include_empty_classes (record_layout_info rli)
5594 tree eoc;
5595 tree rli_size;
5597 /* It might be the case that we grew the class to allocate a
5598 zero-sized base class. That won't be reflected in RLI, yet,
5599 because we are willing to overlay multiple bases at the same
5600 offset. However, now we need to make sure that RLI is big enough
5601 to reflect the entire class. */
5602 eoc = end_of_class (rli->t,
5603 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5604 rli_size = rli_size_unit_so_far (rli);
5605 if (TREE_CODE (rli_size) == INTEGER_CST
5606 && INT_CST_LT_UNSIGNED (rli_size, eoc))
5608 if (!abi_version_at_least (2))
5609 /* In version 1 of the ABI, the size of a class that ends with
5610 a bitfield was not rounded up to a whole multiple of a
5611 byte. Because rli_size_unit_so_far returns only the number
5612 of fully allocated bytes, any extra bits were not included
5613 in the size. */
5614 rli->bitpos = round_down (rli->bitpos, BITS_PER_UNIT);
5615 else
5616 /* The size should have been rounded to a whole byte. */
5617 gcc_assert (tree_int_cst_equal
5618 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5619 rli->bitpos
5620 = size_binop (PLUS_EXPR,
5621 rli->bitpos,
5622 size_binop (MULT_EXPR,
5623 convert (bitsizetype,
5624 size_binop (MINUS_EXPR,
5625 eoc, rli_size)),
5626 bitsize_int (BITS_PER_UNIT)));
5627 normalize_rli (rli);
5631 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5632 BINFO_OFFSETs for all of the base-classes. Position the vtable
5633 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5635 static void
5636 layout_class_type (tree t, tree *virtuals_p)
5638 tree non_static_data_members;
5639 tree field;
5640 tree vptr;
5641 record_layout_info rli;
5642 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5643 types that appear at that offset. */
5644 splay_tree empty_base_offsets;
5645 /* True if the last field layed out was a bit-field. */
5646 bool last_field_was_bitfield = false;
5647 /* The location at which the next field should be inserted. */
5648 tree *next_field;
5649 /* T, as a base class. */
5650 tree base_t;
5652 /* Keep track of the first non-static data member. */
5653 non_static_data_members = TYPE_FIELDS (t);
5655 /* Start laying out the record. */
5656 rli = start_record_layout (t);
5658 /* Mark all the primary bases in the hierarchy. */
5659 determine_primary_bases (t);
5661 /* Create a pointer to our virtual function table. */
5662 vptr = create_vtable_ptr (t, virtuals_p);
5664 /* The vptr is always the first thing in the class. */
5665 if (vptr)
5667 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
5668 TYPE_FIELDS (t) = vptr;
5669 next_field = &DECL_CHAIN (vptr);
5670 place_field (rli, vptr);
5672 else
5673 next_field = &TYPE_FIELDS (t);
5675 /* Build FIELD_DECLs for all of the non-virtual base-types. */
5676 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
5677 NULL, NULL);
5678 build_base_fields (rli, empty_base_offsets, next_field);
5680 /* Layout the non-static data members. */
5681 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
5683 tree type;
5684 tree padding;
5686 /* We still pass things that aren't non-static data members to
5687 the back end, in case it wants to do something with them. */
5688 if (TREE_CODE (field) != FIELD_DECL)
5690 place_field (rli, field);
5691 /* If the static data member has incomplete type, keep track
5692 of it so that it can be completed later. (The handling
5693 of pending statics in finish_record_layout is
5694 insufficient; consider:
5696 struct S1;
5697 struct S2 { static S1 s1; };
5699 At this point, finish_record_layout will be called, but
5700 S1 is still incomplete.) */
5701 if (TREE_CODE (field) == VAR_DECL)
5703 maybe_register_incomplete_var (field);
5704 /* The visibility of static data members is determined
5705 at their point of declaration, not their point of
5706 definition. */
5707 determine_visibility (field);
5709 continue;
5712 type = TREE_TYPE (field);
5713 if (type == error_mark_node)
5714 continue;
5716 padding = NULL_TREE;
5718 /* If this field is a bit-field whose width is greater than its
5719 type, then there are some special rules for allocating
5720 it. */
5721 if (DECL_C_BIT_FIELD (field)
5722 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
5724 unsigned int itk;
5725 tree integer_type;
5726 bool was_unnamed_p = false;
5727 /* We must allocate the bits as if suitably aligned for the
5728 longest integer type that fits in this many bits. type
5729 of the field. Then, we are supposed to use the left over
5730 bits as additional padding. */
5731 for (itk = itk_char; itk != itk_none; ++itk)
5732 if (integer_types[itk] != NULL_TREE
5733 && (INT_CST_LT (size_int (MAX_FIXED_MODE_SIZE),
5734 TYPE_SIZE (integer_types[itk]))
5735 || INT_CST_LT (DECL_SIZE (field),
5736 TYPE_SIZE (integer_types[itk]))))
5737 break;
5739 /* ITK now indicates a type that is too large for the
5740 field. We have to back up by one to find the largest
5741 type that fits. */
5744 --itk;
5745 integer_type = integer_types[itk];
5746 } while (itk > 0 && integer_type == NULL_TREE);
5748 /* Figure out how much additional padding is required. GCC
5749 3.2 always created a padding field, even if it had zero
5750 width. */
5751 if (!abi_version_at_least (2)
5752 || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field)))
5754 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
5755 /* In a union, the padding field must have the full width
5756 of the bit-field; all fields start at offset zero. */
5757 padding = DECL_SIZE (field);
5758 else
5760 if (TREE_CODE (t) == UNION_TYPE)
5761 warning (OPT_Wabi, "size assigned to %qT may not be "
5762 "ABI-compliant and may change in a future "
5763 "version of GCC",
5765 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
5766 TYPE_SIZE (integer_type));
5769 #ifdef PCC_BITFIELD_TYPE_MATTERS
5770 /* An unnamed bitfield does not normally affect the
5771 alignment of the containing class on a target where
5772 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
5773 make any exceptions for unnamed bitfields when the
5774 bitfields are longer than their types. Therefore, we
5775 temporarily give the field a name. */
5776 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
5778 was_unnamed_p = true;
5779 DECL_NAME (field) = make_anon_name ();
5781 #endif
5782 DECL_SIZE (field) = TYPE_SIZE (integer_type);
5783 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
5784 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
5785 layout_nonempty_base_or_field (rli, field, NULL_TREE,
5786 empty_base_offsets);
5787 if (was_unnamed_p)
5788 DECL_NAME (field) = NULL_TREE;
5789 /* Now that layout has been performed, set the size of the
5790 field to the size of its declared type; the rest of the
5791 field is effectively invisible. */
5792 DECL_SIZE (field) = TYPE_SIZE (type);
5793 /* We must also reset the DECL_MODE of the field. */
5794 if (abi_version_at_least (2))
5795 DECL_MODE (field) = TYPE_MODE (type);
5796 else if (warn_abi
5797 && DECL_MODE (field) != TYPE_MODE (type))
5798 /* Versions of G++ before G++ 3.4 did not reset the
5799 DECL_MODE. */
5800 warning (OPT_Wabi,
5801 "the offset of %qD may not be ABI-compliant and may "
5802 "change in a future version of GCC", field);
5804 else
5805 layout_nonempty_base_or_field (rli, field, NULL_TREE,
5806 empty_base_offsets);
5808 /* Remember the location of any empty classes in FIELD. */
5809 if (abi_version_at_least (2))
5810 record_subobject_offsets (TREE_TYPE (field),
5811 byte_position(field),
5812 empty_base_offsets,
5813 /*is_data_member=*/true);
5815 /* If a bit-field does not immediately follow another bit-field,
5816 and yet it starts in the middle of a byte, we have failed to
5817 comply with the ABI. */
5818 if (warn_abi
5819 && DECL_C_BIT_FIELD (field)
5820 /* The TREE_NO_WARNING flag gets set by Objective-C when
5821 laying out an Objective-C class. The ObjC ABI differs
5822 from the C++ ABI, and so we do not want a warning
5823 here. */
5824 && !TREE_NO_WARNING (field)
5825 && !last_field_was_bitfield
5826 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
5827 DECL_FIELD_BIT_OFFSET (field),
5828 bitsize_unit_node)))
5829 warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may "
5830 "change in a future version of GCC", field);
5832 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
5833 offset of the field. */
5834 if (warn_abi
5835 && !abi_version_at_least (2)
5836 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
5837 byte_position (field))
5838 && contains_empty_class_p (TREE_TYPE (field)))
5839 warning (OPT_Wabi, "%q+D contains empty classes which may cause base "
5840 "classes to be placed at different locations in a "
5841 "future version of GCC", field);
5843 /* The middle end uses the type of expressions to determine the
5844 possible range of expression values. In order to optimize
5845 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
5846 must be made aware of the width of "i", via its type.
5848 Because C++ does not have integer types of arbitrary width,
5849 we must (for the purposes of the front end) convert from the
5850 type assigned here to the declared type of the bitfield
5851 whenever a bitfield expression is used as an rvalue.
5852 Similarly, when assigning a value to a bitfield, the value
5853 must be converted to the type given the bitfield here. */
5854 if (DECL_C_BIT_FIELD (field))
5856 unsigned HOST_WIDE_INT width;
5857 tree ftype = TREE_TYPE (field);
5858 width = tree_low_cst (DECL_SIZE (field), /*unsignedp=*/1);
5859 if (width != TYPE_PRECISION (ftype))
5861 TREE_TYPE (field)
5862 = c_build_bitfield_integer_type (width,
5863 TYPE_UNSIGNED (ftype));
5864 TREE_TYPE (field)
5865 = cp_build_qualified_type (TREE_TYPE (field),
5866 cp_type_quals (ftype));
5870 /* If we needed additional padding after this field, add it
5871 now. */
5872 if (padding)
5874 tree padding_field;
5876 padding_field = build_decl (input_location,
5877 FIELD_DECL,
5878 NULL_TREE,
5879 char_type_node);
5880 DECL_BIT_FIELD (padding_field) = 1;
5881 DECL_SIZE (padding_field) = padding;
5882 DECL_CONTEXT (padding_field) = t;
5883 DECL_ARTIFICIAL (padding_field) = 1;
5884 DECL_IGNORED_P (padding_field) = 1;
5885 layout_nonempty_base_or_field (rli, padding_field,
5886 NULL_TREE,
5887 empty_base_offsets);
5890 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
5893 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
5895 /* Make sure that we are on a byte boundary so that the size of
5896 the class without virtual bases will always be a round number
5897 of bytes. */
5898 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
5899 normalize_rli (rli);
5902 /* G++ 3.2 does not allow virtual bases to be overlaid with tail
5903 padding. */
5904 if (!abi_version_at_least (2))
5905 include_empty_classes(rli);
5907 /* Delete all zero-width bit-fields from the list of fields. Now
5908 that the type is laid out they are no longer important. */
5909 remove_zero_width_bit_fields (t);
5911 /* Create the version of T used for virtual bases. We do not use
5912 make_class_type for this version; this is an artificial type. For
5913 a POD type, we just reuse T. */
5914 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
5916 base_t = make_node (TREE_CODE (t));
5918 /* Set the size and alignment for the new type. In G++ 3.2, all
5919 empty classes were considered to have size zero when used as
5920 base classes. */
5921 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
5923 TYPE_SIZE (base_t) = bitsize_zero_node;
5924 TYPE_SIZE_UNIT (base_t) = size_zero_node;
5925 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
5926 warning (OPT_Wabi,
5927 "layout of classes derived from empty class %qT "
5928 "may change in a future version of GCC",
5931 else
5933 tree eoc;
5935 /* If the ABI version is not at least two, and the last
5936 field was a bit-field, RLI may not be on a byte
5937 boundary. In particular, rli_size_unit_so_far might
5938 indicate the last complete byte, while rli_size_so_far
5939 indicates the total number of bits used. Therefore,
5940 rli_size_so_far, rather than rli_size_unit_so_far, is
5941 used to compute TYPE_SIZE_UNIT. */
5942 eoc = end_of_class (t, /*include_virtuals_p=*/0);
5943 TYPE_SIZE_UNIT (base_t)
5944 = size_binop (MAX_EXPR,
5945 convert (sizetype,
5946 size_binop (CEIL_DIV_EXPR,
5947 rli_size_so_far (rli),
5948 bitsize_int (BITS_PER_UNIT))),
5949 eoc);
5950 TYPE_SIZE (base_t)
5951 = size_binop (MAX_EXPR,
5952 rli_size_so_far (rli),
5953 size_binop (MULT_EXPR,
5954 convert (bitsizetype, eoc),
5955 bitsize_int (BITS_PER_UNIT)));
5957 TYPE_ALIGN (base_t) = rli->record_align;
5958 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
5960 /* Copy the fields from T. */
5961 next_field = &TYPE_FIELDS (base_t);
5962 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5963 if (TREE_CODE (field) == FIELD_DECL)
5965 *next_field = build_decl (input_location,
5966 FIELD_DECL,
5967 DECL_NAME (field),
5968 TREE_TYPE (field));
5969 DECL_CONTEXT (*next_field) = base_t;
5970 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
5971 DECL_FIELD_BIT_OFFSET (*next_field)
5972 = DECL_FIELD_BIT_OFFSET (field);
5973 DECL_SIZE (*next_field) = DECL_SIZE (field);
5974 DECL_MODE (*next_field) = DECL_MODE (field);
5975 next_field = &DECL_CHAIN (*next_field);
5978 /* Record the base version of the type. */
5979 CLASSTYPE_AS_BASE (t) = base_t;
5980 TYPE_CONTEXT (base_t) = t;
5982 else
5983 CLASSTYPE_AS_BASE (t) = t;
5985 /* Every empty class contains an empty class. */
5986 if (CLASSTYPE_EMPTY_P (t))
5987 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
5989 /* Set the TYPE_DECL for this type to contain the right
5990 value for DECL_OFFSET, so that we can use it as part
5991 of a COMPONENT_REF for multiple inheritance. */
5992 layout_decl (TYPE_MAIN_DECL (t), 0);
5994 /* Now fix up any virtual base class types that we left lying
5995 around. We must get these done before we try to lay out the
5996 virtual function table. As a side-effect, this will remove the
5997 base subobject fields. */
5998 layout_virtual_bases (rli, empty_base_offsets);
6000 /* Make sure that empty classes are reflected in RLI at this
6001 point. */
6002 include_empty_classes(rli);
6004 /* Make sure not to create any structures with zero size. */
6005 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6006 place_field (rli,
6007 build_decl (input_location,
6008 FIELD_DECL, NULL_TREE, char_type_node));
6010 /* If this is a non-POD, declaring it packed makes a difference to how it
6011 can be used as a field; don't let finalize_record_size undo it. */
6012 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6013 rli->packed_maybe_necessary = true;
6015 /* Let the back end lay out the type. */
6016 finish_record_layout (rli, /*free_p=*/true);
6018 /* Warn about bases that can't be talked about due to ambiguity. */
6019 warn_about_ambiguous_bases (t);
6021 /* Now that we're done with layout, give the base fields the real types. */
6022 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6023 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6024 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6026 /* Clean up. */
6027 splay_tree_delete (empty_base_offsets);
6029 if (CLASSTYPE_EMPTY_P (t)
6030 && tree_int_cst_lt (sizeof_biggest_empty_class,
6031 TYPE_SIZE_UNIT (t)))
6032 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6035 /* Determine the "key method" for the class type indicated by TYPE,
6036 and set CLASSTYPE_KEY_METHOD accordingly. */
6038 void
6039 determine_key_method (tree type)
6041 tree method;
6043 if (TYPE_FOR_JAVA (type)
6044 || processing_template_decl
6045 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6046 || CLASSTYPE_INTERFACE_KNOWN (type))
6047 return;
6049 /* The key method is the first non-pure virtual function that is not
6050 inline at the point of class definition. On some targets the
6051 key function may not be inline; those targets should not call
6052 this function until the end of the translation unit. */
6053 for (method = TYPE_METHODS (type); method != NULL_TREE;
6054 method = DECL_CHAIN (method))
6055 if (DECL_VINDEX (method) != NULL_TREE
6056 && ! DECL_DECLARED_INLINE_P (method)
6057 && ! DECL_PURE_VIRTUAL_P (method))
6059 CLASSTYPE_KEY_METHOD (type) = method;
6060 break;
6063 return;
6067 /* Allocate and return an instance of struct sorted_fields_type with
6068 N fields. */
6070 static struct sorted_fields_type *
6071 sorted_fields_type_new (int n)
6073 struct sorted_fields_type *sft;
6074 sft = ggc_alloc_sorted_fields_type (sizeof (struct sorted_fields_type)
6075 + n * sizeof (tree));
6076 sft->len = n;
6078 return sft;
6082 /* Perform processing required when the definition of T (a class type)
6083 is complete. */
6085 void
6086 finish_struct_1 (tree t)
6088 tree x;
6089 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6090 tree virtuals = NULL_TREE;
6092 if (COMPLETE_TYPE_P (t))
6094 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6095 error ("redefinition of %q#T", t);
6096 popclass ();
6097 return;
6100 /* If this type was previously laid out as a forward reference,
6101 make sure we lay it out again. */
6102 TYPE_SIZE (t) = NULL_TREE;
6103 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6105 /* Make assumptions about the class; we'll reset the flags if
6106 necessary. */
6107 CLASSTYPE_EMPTY_P (t) = 1;
6108 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6109 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6110 CLASSTYPE_LITERAL_P (t) = true;
6112 /* Do end-of-class semantic processing: checking the validity of the
6113 bases and members and add implicitly generated methods. */
6114 check_bases_and_members (t);
6116 /* Find the key method. */
6117 if (TYPE_CONTAINS_VPTR_P (t))
6119 /* The Itanium C++ ABI permits the key method to be chosen when
6120 the class is defined -- even though the key method so
6121 selected may later turn out to be an inline function. On
6122 some systems (such as ARM Symbian OS) the key method cannot
6123 be determined until the end of the translation unit. On such
6124 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6125 will cause the class to be added to KEYED_CLASSES. Then, in
6126 finish_file we will determine the key method. */
6127 if (targetm.cxx.key_method_may_be_inline ())
6128 determine_key_method (t);
6130 /* If a polymorphic class has no key method, we may emit the vtable
6131 in every translation unit where the class definition appears. */
6132 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6133 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6136 /* Layout the class itself. */
6137 layout_class_type (t, &virtuals);
6138 if (CLASSTYPE_AS_BASE (t) != t)
6139 /* We use the base type for trivial assignments, and hence it
6140 needs a mode. */
6141 compute_record_mode (CLASSTYPE_AS_BASE (t));
6143 virtuals = modify_all_vtables (t, nreverse (virtuals));
6145 /* If necessary, create the primary vtable for this class. */
6146 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6148 /* We must enter these virtuals into the table. */
6149 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6150 build_primary_vtable (NULL_TREE, t);
6151 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6152 /* Here we know enough to change the type of our virtual
6153 function table, but we will wait until later this function. */
6154 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6157 if (TYPE_CONTAINS_VPTR_P (t))
6159 int vindex;
6160 tree fn;
6162 if (BINFO_VTABLE (TYPE_BINFO (t)))
6163 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6164 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6165 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6167 /* Add entries for virtual functions introduced by this class. */
6168 BINFO_VIRTUALS (TYPE_BINFO (t))
6169 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6171 /* Set DECL_VINDEX for all functions declared in this class. */
6172 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6174 fn = TREE_CHAIN (fn),
6175 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6176 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6178 tree fndecl = BV_FN (fn);
6180 if (DECL_THUNK_P (fndecl))
6181 /* A thunk. We should never be calling this entry directly
6182 from this vtable -- we'd use the entry for the non
6183 thunk base function. */
6184 DECL_VINDEX (fndecl) = NULL_TREE;
6185 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6186 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6190 finish_struct_bits (t);
6191 set_method_tm_attributes (t);
6193 /* Complete the rtl for any static member objects of the type we're
6194 working on. */
6195 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6196 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
6197 && TREE_TYPE (x) != error_mark_node
6198 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6199 DECL_MODE (x) = TYPE_MODE (t);
6201 /* Done with FIELDS...now decide whether to sort these for
6202 faster lookups later.
6204 We use a small number because most searches fail (succeeding
6205 ultimately as the search bores through the inheritance
6206 hierarchy), and we want this failure to occur quickly. */
6208 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
6210 /* Complain if one of the field types requires lower visibility. */
6211 constrain_class_visibility (t);
6213 /* Make the rtl for any new vtables we have created, and unmark
6214 the base types we marked. */
6215 finish_vtbls (t);
6217 /* Build the VTT for T. */
6218 build_vtt (t);
6220 /* This warning does not make sense for Java classes, since they
6221 cannot have destructors. */
6222 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor && TYPE_POLYMORPHIC_P (t))
6224 tree dtor;
6226 dtor = CLASSTYPE_DESTRUCTORS (t);
6227 if (/* An implicitly declared destructor is always public. And,
6228 if it were virtual, we would have created it by now. */
6229 !dtor
6230 || (!DECL_VINDEX (dtor)
6231 && (/* public non-virtual */
6232 (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
6233 || (/* non-public non-virtual with friends */
6234 (TREE_PRIVATE (dtor) || TREE_PROTECTED (dtor))
6235 && (CLASSTYPE_FRIEND_CLASSES (t)
6236 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))))))
6237 warning (OPT_Wnon_virtual_dtor,
6238 "%q#T has virtual functions and accessible"
6239 " non-virtual destructor", t);
6242 complete_vars (t);
6244 if (warn_overloaded_virtual)
6245 warn_hidden (t);
6247 /* Class layout, assignment of virtual table slots, etc., is now
6248 complete. Give the back end a chance to tweak the visibility of
6249 the class or perform any other required target modifications. */
6250 targetm.cxx.adjust_class_at_definition (t);
6252 maybe_suppress_debug_info (t);
6254 dump_class_hierarchy (t);
6256 /* Finish debugging output for this type. */
6257 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6259 if (TYPE_TRANSPARENT_AGGR (t))
6261 tree field = first_field (t);
6262 if (field == NULL_TREE || error_operand_p (field))
6264 error ("type transparent class %qT does not have any fields", t);
6265 TYPE_TRANSPARENT_AGGR (t) = 0;
6267 else if (DECL_ARTIFICIAL (field))
6269 if (DECL_FIELD_IS_BASE (field))
6270 error ("type transparent class %qT has base classes", t);
6271 else
6273 gcc_checking_assert (DECL_VIRTUAL_P (field));
6274 error ("type transparent class %qT has virtual functions", t);
6276 TYPE_TRANSPARENT_AGGR (t) = 0;
6281 /* Insert FIELDS into T for the sorted case if the FIELDS count is
6282 equal to THRESHOLD or greater than THRESHOLD. */
6284 static void
6285 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
6287 int n_fields = count_fields (fields);
6288 if (n_fields >= threshold)
6290 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6291 add_fields_to_record_type (fields, field_vec, 0);
6292 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6293 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6297 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
6299 void
6300 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
6302 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
6303 if (sorted_fields)
6305 int i;
6306 int n_fields
6307 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
6308 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6310 for (i = 0; i < sorted_fields->len; ++i)
6311 field_vec->elts[i] = sorted_fields->elts[i];
6313 add_enum_fields_to_record_type (enumtype, field_vec,
6314 sorted_fields->len);
6315 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6316 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6320 /* When T was built up, the member declarations were added in reverse
6321 order. Rearrange them to declaration order. */
6323 void
6324 unreverse_member_declarations (tree t)
6326 tree next;
6327 tree prev;
6328 tree x;
6330 /* The following lists are all in reverse order. Put them in
6331 declaration order now. */
6332 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
6333 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6335 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
6336 reverse order, so we can't just use nreverse. */
6337 prev = NULL_TREE;
6338 for (x = TYPE_FIELDS (t);
6339 x && TREE_CODE (x) != TYPE_DECL;
6340 x = next)
6342 next = DECL_CHAIN (x);
6343 DECL_CHAIN (x) = prev;
6344 prev = x;
6346 if (prev)
6348 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6349 if (prev)
6350 TYPE_FIELDS (t) = prev;
6354 tree
6355 finish_struct (tree t, tree attributes)
6357 location_t saved_loc = input_location;
6359 /* Now that we've got all the field declarations, reverse everything
6360 as necessary. */
6361 unreverse_member_declarations (t);
6363 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6365 /* Nadger the current location so that diagnostics point to the start of
6366 the struct, not the end. */
6367 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
6369 if (processing_template_decl)
6371 tree x;
6373 finish_struct_methods (t);
6374 TYPE_SIZE (t) = bitsize_zero_node;
6375 TYPE_SIZE_UNIT (t) = size_zero_node;
6377 /* We need to emit an error message if this type was used as a parameter
6378 and it is an abstract type, even if it is a template. We construct
6379 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
6380 account and we call complete_vars with this type, which will check
6381 the PARM_DECLS. Note that while the type is being defined,
6382 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
6383 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
6384 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
6385 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
6386 if (DECL_PURE_VIRTUAL_P (x))
6387 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
6388 complete_vars (t);
6389 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
6390 an enclosing scope is a template class, so that this function be
6391 found by lookup_fnfields_1 when the using declaration is not
6392 instantiated yet. */
6393 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6394 if (TREE_CODE (x) == USING_DECL)
6396 tree fn = strip_using_decl (x);
6397 if (is_overloaded_fn (fn))
6398 for (; fn; fn = OVL_NEXT (fn))
6399 add_method (t, OVL_CURRENT (fn), x);
6402 /* Remember current #pragma pack value. */
6403 TYPE_PRECISION (t) = maximum_field_alignment;
6405 /* Fix up any variants we've already built. */
6406 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6408 TYPE_SIZE (x) = TYPE_SIZE (t);
6409 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
6410 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6411 TYPE_METHODS (x) = TYPE_METHODS (t);
6414 else
6415 finish_struct_1 (t);
6417 input_location = saved_loc;
6419 TYPE_BEING_DEFINED (t) = 0;
6421 if (current_class_type)
6422 popclass ();
6423 else
6424 error ("trying to finish struct, but kicked out due to previous parse errors");
6426 if (processing_template_decl && at_function_scope_p ()
6427 /* Lambdas are defined by the LAMBDA_EXPR. */
6428 && !LAMBDA_TYPE_P (t))
6429 add_stmt (build_min (TAG_DEFN, t));
6431 return t;
6434 /* Return the dynamic type of INSTANCE, if known.
6435 Used to determine whether the virtual function table is needed
6436 or not.
6438 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6439 of our knowledge of its type. *NONNULL should be initialized
6440 before this function is called. */
6442 static tree
6443 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
6445 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
6447 switch (TREE_CODE (instance))
6449 case INDIRECT_REF:
6450 if (POINTER_TYPE_P (TREE_TYPE (instance)))
6451 return NULL_TREE;
6452 else
6453 return RECUR (TREE_OPERAND (instance, 0));
6455 case CALL_EXPR:
6456 /* This is a call to a constructor, hence it's never zero. */
6457 if (TREE_HAS_CONSTRUCTOR (instance))
6459 if (nonnull)
6460 *nonnull = 1;
6461 return TREE_TYPE (instance);
6463 return NULL_TREE;
6465 case SAVE_EXPR:
6466 /* This is a call to a constructor, hence it's never zero. */
6467 if (TREE_HAS_CONSTRUCTOR (instance))
6469 if (nonnull)
6470 *nonnull = 1;
6471 return TREE_TYPE (instance);
6473 return RECUR (TREE_OPERAND (instance, 0));
6475 case POINTER_PLUS_EXPR:
6476 case PLUS_EXPR:
6477 case MINUS_EXPR:
6478 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
6479 return RECUR (TREE_OPERAND (instance, 0));
6480 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
6481 /* Propagate nonnull. */
6482 return RECUR (TREE_OPERAND (instance, 0));
6484 return NULL_TREE;
6486 CASE_CONVERT:
6487 return RECUR (TREE_OPERAND (instance, 0));
6489 case ADDR_EXPR:
6490 instance = TREE_OPERAND (instance, 0);
6491 if (nonnull)
6493 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
6494 with a real object -- given &p->f, p can still be null. */
6495 tree t = get_base_address (instance);
6496 /* ??? Probably should check DECL_WEAK here. */
6497 if (t && DECL_P (t))
6498 *nonnull = 1;
6500 return RECUR (instance);
6502 case COMPONENT_REF:
6503 /* If this component is really a base class reference, then the field
6504 itself isn't definitive. */
6505 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
6506 return RECUR (TREE_OPERAND (instance, 0));
6507 return RECUR (TREE_OPERAND (instance, 1));
6509 case VAR_DECL:
6510 case FIELD_DECL:
6511 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
6512 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
6514 if (nonnull)
6515 *nonnull = 1;
6516 return TREE_TYPE (TREE_TYPE (instance));
6518 /* fall through... */
6519 case TARGET_EXPR:
6520 case PARM_DECL:
6521 case RESULT_DECL:
6522 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
6524 if (nonnull)
6525 *nonnull = 1;
6526 return TREE_TYPE (instance);
6528 else if (instance == current_class_ptr)
6530 if (nonnull)
6531 *nonnull = 1;
6533 /* if we're in a ctor or dtor, we know our type. If
6534 current_class_ptr is set but we aren't in a function, we're in
6535 an NSDMI (and therefore a constructor). */
6536 if (current_scope () != current_function_decl
6537 || (DECL_LANG_SPECIFIC (current_function_decl)
6538 && (DECL_CONSTRUCTOR_P (current_function_decl)
6539 || DECL_DESTRUCTOR_P (current_function_decl))))
6541 if (cdtorp)
6542 *cdtorp = 1;
6543 return TREE_TYPE (TREE_TYPE (instance));
6546 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
6548 /* We only need one hash table because it is always left empty. */
6549 static hash_table <pointer_hash <tree_node> > ht;
6550 if (!ht.is_created ())
6551 ht.create (37);
6553 /* Reference variables should be references to objects. */
6554 if (nonnull)
6555 *nonnull = 1;
6557 /* Enter the INSTANCE in a table to prevent recursion; a
6558 variable's initializer may refer to the variable
6559 itself. */
6560 if (TREE_CODE (instance) == VAR_DECL
6561 && DECL_INITIAL (instance)
6562 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
6563 && !ht.find (instance))
6565 tree type;
6566 tree_node **slot;
6568 slot = ht.find_slot (instance, INSERT);
6569 *slot = instance;
6570 type = RECUR (DECL_INITIAL (instance));
6571 ht.remove_elt (instance);
6573 return type;
6576 return NULL_TREE;
6578 default:
6579 return NULL_TREE;
6581 #undef RECUR
6584 /* Return nonzero if the dynamic type of INSTANCE is known, and
6585 equivalent to the static type. We also handle the case where
6586 INSTANCE is really a pointer. Return negative if this is a
6587 ctor/dtor. There the dynamic type is known, but this might not be
6588 the most derived base of the original object, and hence virtual
6589 bases may not be layed out according to this type.
6591 Used to determine whether the virtual function table is needed
6592 or not.
6594 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6595 of our knowledge of its type. *NONNULL should be initialized
6596 before this function is called. */
6599 resolves_to_fixed_type_p (tree instance, int* nonnull)
6601 tree t = TREE_TYPE (instance);
6602 int cdtorp = 0;
6603 tree fixed;
6605 /* processing_template_decl can be false in a template if we're in
6606 fold_non_dependent_expr, but we still want to suppress this check. */
6607 if (in_template_function ())
6609 /* In a template we only care about the type of the result. */
6610 if (nonnull)
6611 *nonnull = true;
6612 return true;
6615 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
6616 if (fixed == NULL_TREE)
6617 return 0;
6618 if (POINTER_TYPE_P (t))
6619 t = TREE_TYPE (t);
6620 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
6621 return 0;
6622 return cdtorp ? -1 : 1;
6626 void
6627 init_class_processing (void)
6629 current_class_depth = 0;
6630 current_class_stack_size = 10;
6631 current_class_stack
6632 = XNEWVEC (struct class_stack_node, current_class_stack_size);
6633 local_classes = VEC_alloc (tree, gc, 8);
6634 sizeof_biggest_empty_class = size_zero_node;
6636 ridpointers[(int) RID_PUBLIC] = access_public_node;
6637 ridpointers[(int) RID_PRIVATE] = access_private_node;
6638 ridpointers[(int) RID_PROTECTED] = access_protected_node;
6641 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
6643 static void
6644 restore_class_cache (void)
6646 tree type;
6648 /* We are re-entering the same class we just left, so we don't
6649 have to search the whole inheritance matrix to find all the
6650 decls to bind again. Instead, we install the cached
6651 class_shadowed list and walk through it binding names. */
6652 push_binding_level (previous_class_level);
6653 class_binding_level = previous_class_level;
6654 /* Restore IDENTIFIER_TYPE_VALUE. */
6655 for (type = class_binding_level->type_shadowed;
6656 type;
6657 type = TREE_CHAIN (type))
6658 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
6661 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
6662 appropriate for TYPE.
6664 So that we may avoid calls to lookup_name, we cache the _TYPE
6665 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
6667 For multiple inheritance, we perform a two-pass depth-first search
6668 of the type lattice. */
6670 void
6671 pushclass (tree type)
6673 class_stack_node_t csn;
6675 type = TYPE_MAIN_VARIANT (type);
6677 /* Make sure there is enough room for the new entry on the stack. */
6678 if (current_class_depth + 1 >= current_class_stack_size)
6680 current_class_stack_size *= 2;
6681 current_class_stack
6682 = XRESIZEVEC (struct class_stack_node, current_class_stack,
6683 current_class_stack_size);
6686 /* Insert a new entry on the class stack. */
6687 csn = current_class_stack + current_class_depth;
6688 csn->name = current_class_name;
6689 csn->type = current_class_type;
6690 csn->access = current_access_specifier;
6691 csn->names_used = 0;
6692 csn->hidden = 0;
6693 current_class_depth++;
6695 /* Now set up the new type. */
6696 current_class_name = TYPE_NAME (type);
6697 if (TREE_CODE (current_class_name) == TYPE_DECL)
6698 current_class_name = DECL_NAME (current_class_name);
6699 current_class_type = type;
6701 /* By default, things in classes are private, while things in
6702 structures or unions are public. */
6703 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
6704 ? access_private_node
6705 : access_public_node);
6707 if (previous_class_level
6708 && type != previous_class_level->this_entity
6709 && current_class_depth == 1)
6711 /* Forcibly remove any old class remnants. */
6712 invalidate_class_lookup_cache ();
6715 if (!previous_class_level
6716 || type != previous_class_level->this_entity
6717 || current_class_depth > 1)
6718 pushlevel_class ();
6719 else
6720 restore_class_cache ();
6723 /* When we exit a toplevel class scope, we save its binding level so
6724 that we can restore it quickly. Here, we've entered some other
6725 class, so we must invalidate our cache. */
6727 void
6728 invalidate_class_lookup_cache (void)
6730 previous_class_level = NULL;
6733 /* Get out of the current class scope. If we were in a class scope
6734 previously, that is the one popped to. */
6736 void
6737 popclass (void)
6739 poplevel_class ();
6741 current_class_depth--;
6742 current_class_name = current_class_stack[current_class_depth].name;
6743 current_class_type = current_class_stack[current_class_depth].type;
6744 current_access_specifier = current_class_stack[current_class_depth].access;
6745 if (current_class_stack[current_class_depth].names_used)
6746 splay_tree_delete (current_class_stack[current_class_depth].names_used);
6749 /* Mark the top of the class stack as hidden. */
6751 void
6752 push_class_stack (void)
6754 if (current_class_depth)
6755 ++current_class_stack[current_class_depth - 1].hidden;
6758 /* Mark the top of the class stack as un-hidden. */
6760 void
6761 pop_class_stack (void)
6763 if (current_class_depth)
6764 --current_class_stack[current_class_depth - 1].hidden;
6767 /* Returns 1 if the class type currently being defined is either T or
6768 a nested type of T. */
6770 bool
6771 currently_open_class (tree t)
6773 int i;
6775 if (!CLASS_TYPE_P (t))
6776 return false;
6778 t = TYPE_MAIN_VARIANT (t);
6780 /* We start looking from 1 because entry 0 is from global scope,
6781 and has no type. */
6782 for (i = current_class_depth; i > 0; --i)
6784 tree c;
6785 if (i == current_class_depth)
6786 c = current_class_type;
6787 else
6789 if (current_class_stack[i].hidden)
6790 break;
6791 c = current_class_stack[i].type;
6793 if (!c)
6794 continue;
6795 if (same_type_p (c, t))
6796 return true;
6798 return false;
6801 /* If either current_class_type or one of its enclosing classes are derived
6802 from T, return the appropriate type. Used to determine how we found
6803 something via unqualified lookup. */
6805 tree
6806 currently_open_derived_class (tree t)
6808 int i;
6810 /* The bases of a dependent type are unknown. */
6811 if (dependent_type_p (t))
6812 return NULL_TREE;
6814 if (!current_class_type)
6815 return NULL_TREE;
6817 if (DERIVED_FROM_P (t, current_class_type))
6818 return current_class_type;
6820 for (i = current_class_depth - 1; i > 0; --i)
6822 if (current_class_stack[i].hidden)
6823 break;
6824 if (DERIVED_FROM_P (t, current_class_stack[i].type))
6825 return current_class_stack[i].type;
6828 return NULL_TREE;
6831 /* Returns the innermost class type which is not a lambda closure type. */
6833 tree
6834 current_nonlambda_class_type (void)
6836 int i;
6838 /* We start looking from 1 because entry 0 is from global scope,
6839 and has no type. */
6840 for (i = current_class_depth; i > 0; --i)
6842 tree c;
6843 if (i == current_class_depth)
6844 c = current_class_type;
6845 else
6847 if (current_class_stack[i].hidden)
6848 break;
6849 c = current_class_stack[i].type;
6851 if (!c)
6852 continue;
6853 if (!LAMBDA_TYPE_P (c))
6854 return c;
6856 return NULL_TREE;
6859 /* When entering a class scope, all enclosing class scopes' names with
6860 static meaning (static variables, static functions, types and
6861 enumerators) have to be visible. This recursive function calls
6862 pushclass for all enclosing class contexts until global or a local
6863 scope is reached. TYPE is the enclosed class. */
6865 void
6866 push_nested_class (tree type)
6868 /* A namespace might be passed in error cases, like A::B:C. */
6869 if (type == NULL_TREE
6870 || !CLASS_TYPE_P (type))
6871 return;
6873 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
6875 pushclass (type);
6878 /* Undoes a push_nested_class call. */
6880 void
6881 pop_nested_class (void)
6883 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
6885 popclass ();
6886 if (context && CLASS_TYPE_P (context))
6887 pop_nested_class ();
6890 /* Returns the number of extern "LANG" blocks we are nested within. */
6893 current_lang_depth (void)
6895 return VEC_length (tree, current_lang_base);
6898 /* Set global variables CURRENT_LANG_NAME to appropriate value
6899 so that behavior of name-mangling machinery is correct. */
6901 void
6902 push_lang_context (tree name)
6904 VEC_safe_push (tree, gc, current_lang_base, current_lang_name);
6906 if (name == lang_name_cplusplus)
6908 current_lang_name = name;
6910 else if (name == lang_name_java)
6912 current_lang_name = name;
6913 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
6914 (See record_builtin_java_type in decl.c.) However, that causes
6915 incorrect debug entries if these types are actually used.
6916 So we re-enable debug output after extern "Java". */
6917 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
6918 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
6919 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
6920 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
6921 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
6922 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
6923 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
6924 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
6926 else if (name == lang_name_c)
6928 current_lang_name = name;
6930 else
6931 error ("language string %<\"%E\"%> not recognized", name);
6934 /* Get out of the current language scope. */
6936 void
6937 pop_lang_context (void)
6939 current_lang_name = VEC_pop (tree, current_lang_base);
6942 /* Type instantiation routines. */
6944 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
6945 matches the TARGET_TYPE. If there is no satisfactory match, return
6946 error_mark_node, and issue an error & warning messages under
6947 control of FLAGS. Permit pointers to member function if FLAGS
6948 permits. If TEMPLATE_ONLY, the name of the overloaded function was
6949 a template-id, and EXPLICIT_TARGS are the explicitly provided
6950 template arguments.
6952 If OVERLOAD is for one or more member functions, then ACCESS_PATH
6953 is the base path used to reference those member functions. If
6954 the address is resolved to a member function, access checks will be
6955 performed and errors issued if appropriate. */
6957 static tree
6958 resolve_address_of_overloaded_function (tree target_type,
6959 tree overload,
6960 tsubst_flags_t flags,
6961 bool template_only,
6962 tree explicit_targs,
6963 tree access_path)
6965 /* Here's what the standard says:
6967 [over.over]
6969 If the name is a function template, template argument deduction
6970 is done, and if the argument deduction succeeds, the deduced
6971 arguments are used to generate a single template function, which
6972 is added to the set of overloaded functions considered.
6974 Non-member functions and static member functions match targets of
6975 type "pointer-to-function" or "reference-to-function." Nonstatic
6976 member functions match targets of type "pointer-to-member
6977 function;" the function type of the pointer to member is used to
6978 select the member function from the set of overloaded member
6979 functions. If a nonstatic member function is selected, the
6980 reference to the overloaded function name is required to have the
6981 form of a pointer to member as described in 5.3.1.
6983 If more than one function is selected, any template functions in
6984 the set are eliminated if the set also contains a non-template
6985 function, and any given template function is eliminated if the
6986 set contains a second template function that is more specialized
6987 than the first according to the partial ordering rules 14.5.5.2.
6988 After such eliminations, if any, there shall remain exactly one
6989 selected function. */
6991 int is_ptrmem = 0;
6992 /* We store the matches in a TREE_LIST rooted here. The functions
6993 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
6994 interoperability with most_specialized_instantiation. */
6995 tree matches = NULL_TREE;
6996 tree fn;
6997 tree target_fn_type;
6999 /* By the time we get here, we should be seeing only real
7000 pointer-to-member types, not the internal POINTER_TYPE to
7001 METHOD_TYPE representation. */
7002 gcc_assert (TREE_CODE (target_type) != POINTER_TYPE
7003 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7005 gcc_assert (is_overloaded_fn (overload));
7007 /* Check that the TARGET_TYPE is reasonable. */
7008 if (TYPE_PTRFN_P (target_type))
7009 /* This is OK. */;
7010 else if (TYPE_PTRMEMFUNC_P (target_type))
7011 /* This is OK, too. */
7012 is_ptrmem = 1;
7013 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7014 /* This is OK, too. This comes from a conversion to reference
7015 type. */
7016 target_type = build_reference_type (target_type);
7017 else
7019 if (flags & tf_error)
7020 error ("cannot resolve overloaded function %qD based on"
7021 " conversion to type %qT",
7022 DECL_NAME (OVL_FUNCTION (overload)), target_type);
7023 return error_mark_node;
7026 /* Non-member functions and static member functions match targets of type
7027 "pointer-to-function" or "reference-to-function." Nonstatic member
7028 functions match targets of type "pointer-to-member-function;" the
7029 function type of the pointer to member is used to select the member
7030 function from the set of overloaded member functions.
7032 So figure out the FUNCTION_TYPE that we want to match against. */
7033 target_fn_type = static_fn_type (target_type);
7035 /* If we can find a non-template function that matches, we can just
7036 use it. There's no point in generating template instantiations
7037 if we're just going to throw them out anyhow. But, of course, we
7038 can only do this when we don't *need* a template function. */
7039 if (!template_only)
7041 tree fns;
7043 for (fns = overload; fns; fns = OVL_NEXT (fns))
7045 tree fn = OVL_CURRENT (fns);
7047 if (TREE_CODE (fn) == TEMPLATE_DECL)
7048 /* We're not looking for templates just yet. */
7049 continue;
7051 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7052 != is_ptrmem)
7053 /* We're looking for a non-static member, and this isn't
7054 one, or vice versa. */
7055 continue;
7057 /* Ignore functions which haven't been explicitly
7058 declared. */
7059 if (DECL_ANTICIPATED (fn))
7060 continue;
7062 /* See if there's a match. */
7063 if (same_type_p (target_fn_type, static_fn_type (fn)))
7064 matches = tree_cons (fn, NULL_TREE, matches);
7068 /* Now, if we've already got a match (or matches), there's no need
7069 to proceed to the template functions. But, if we don't have a
7070 match we need to look at them, too. */
7071 if (!matches)
7073 tree target_arg_types;
7074 tree target_ret_type;
7075 tree fns;
7076 tree *args;
7077 unsigned int nargs, ia;
7078 tree arg;
7080 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7081 target_ret_type = TREE_TYPE (target_fn_type);
7083 nargs = list_length (target_arg_types);
7084 args = XALLOCAVEC (tree, nargs);
7085 for (arg = target_arg_types, ia = 0;
7086 arg != NULL_TREE && arg != void_list_node;
7087 arg = TREE_CHAIN (arg), ++ia)
7088 args[ia] = TREE_VALUE (arg);
7089 nargs = ia;
7091 for (fns = overload; fns; fns = OVL_NEXT (fns))
7093 tree fn = OVL_CURRENT (fns);
7094 tree instantiation;
7095 tree targs;
7097 if (TREE_CODE (fn) != TEMPLATE_DECL)
7098 /* We're only looking for templates. */
7099 continue;
7101 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7102 != is_ptrmem)
7103 /* We're not looking for a non-static member, and this is
7104 one, or vice versa. */
7105 continue;
7107 /* Try to do argument deduction. */
7108 targs = make_tree_vec (DECL_NTPARMS (fn));
7109 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7110 nargs, target_ret_type,
7111 DEDUCE_EXACT, LOOKUP_NORMAL,
7112 false);
7113 if (instantiation == error_mark_node)
7114 /* Instantiation failed. */
7115 continue;
7117 /* See if there's a match. */
7118 if (same_type_p (target_fn_type, static_fn_type (instantiation)))
7119 matches = tree_cons (instantiation, fn, matches);
7122 /* Now, remove all but the most specialized of the matches. */
7123 if (matches)
7125 tree match = most_specialized_instantiation (matches);
7127 if (match != error_mark_node)
7128 matches = tree_cons (TREE_PURPOSE (match),
7129 NULL_TREE,
7130 NULL_TREE);
7134 /* Now we should have exactly one function in MATCHES. */
7135 if (matches == NULL_TREE)
7137 /* There were *no* matches. */
7138 if (flags & tf_error)
7140 error ("no matches converting function %qD to type %q#T",
7141 DECL_NAME (OVL_CURRENT (overload)),
7142 target_type);
7144 print_candidates (overload);
7146 return error_mark_node;
7148 else if (TREE_CHAIN (matches))
7150 /* There were too many matches. First check if they're all
7151 the same function. */
7152 tree match;
7154 fn = TREE_PURPOSE (matches);
7155 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7156 if (!decls_match (fn, TREE_PURPOSE (match)))
7157 break;
7159 if (match)
7161 if (flags & tf_error)
7163 error ("converting overloaded function %qD to type %q#T is ambiguous",
7164 DECL_NAME (OVL_FUNCTION (overload)),
7165 target_type);
7167 /* Since print_candidates expects the functions in the
7168 TREE_VALUE slot, we flip them here. */
7169 for (match = matches; match; match = TREE_CHAIN (match))
7170 TREE_VALUE (match) = TREE_PURPOSE (match);
7172 print_candidates (matches);
7175 return error_mark_node;
7179 /* Good, exactly one match. Now, convert it to the correct type. */
7180 fn = TREE_PURPOSE (matches);
7182 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7183 && !(flags & tf_ptrmem_ok) && !flag_ms_extensions)
7185 static int explained;
7187 if (!(flags & tf_error))
7188 return error_mark_node;
7190 permerror (input_location, "assuming pointer to member %qD", fn);
7191 if (!explained)
7193 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7194 explained = 1;
7198 /* If we're doing overload resolution purely for the purpose of
7199 determining conversion sequences, we should not consider the
7200 function used. If this conversion sequence is selected, the
7201 function will be marked as used at this point. */
7202 if (!(flags & tf_conv))
7204 /* Make =delete work with SFINAE. */
7205 if (DECL_DELETED_FN (fn) && !(flags & tf_error))
7206 return error_mark_node;
7208 mark_used (fn);
7211 /* We could not check access to member functions when this
7212 expression was originally created since we did not know at that
7213 time to which function the expression referred. */
7214 if (DECL_FUNCTION_MEMBER_P (fn))
7216 gcc_assert (access_path);
7217 perform_or_defer_access_check (access_path, fn, fn, flags);
7220 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7221 return cp_build_addr_expr (fn, flags);
7222 else
7224 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7225 will mark the function as addressed, but here we must do it
7226 explicitly. */
7227 cxx_mark_addressable (fn);
7229 return fn;
7233 /* This function will instantiate the type of the expression given in
7234 RHS to match the type of LHSTYPE. If errors exist, then return
7235 error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then
7236 we complain on errors. If we are not complaining, never modify rhs,
7237 as overload resolution wants to try many possible instantiations, in
7238 the hope that at least one will work.
7240 For non-recursive calls, LHSTYPE should be a function, pointer to
7241 function, or a pointer to member function. */
7243 tree
7244 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
7246 tsubst_flags_t flags_in = flags;
7247 tree access_path = NULL_TREE;
7249 flags &= ~tf_ptrmem_ok;
7251 if (lhstype == unknown_type_node)
7253 if (flags & tf_error)
7254 error ("not enough type information");
7255 return error_mark_node;
7258 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7260 if (same_type_p (lhstype, TREE_TYPE (rhs)))
7261 return rhs;
7262 if (flag_ms_extensions
7263 && TYPE_PTRMEMFUNC_P (lhstype)
7264 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7265 /* Microsoft allows `A::f' to be resolved to a
7266 pointer-to-member. */
7268 else
7270 if (flags & tf_error)
7271 error ("cannot convert %qE from type %qT to type %qT",
7272 rhs, TREE_TYPE (rhs), lhstype);
7273 return error_mark_node;
7277 if (BASELINK_P (rhs))
7279 access_path = BASELINK_ACCESS_BINFO (rhs);
7280 rhs = BASELINK_FUNCTIONS (rhs);
7283 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7284 deduce any type information. */
7285 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
7287 if (flags & tf_error)
7288 error ("not enough type information");
7289 return error_mark_node;
7292 /* There only a few kinds of expressions that may have a type
7293 dependent on overload resolution. */
7294 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
7295 || TREE_CODE (rhs) == COMPONENT_REF
7296 || really_overloaded_fn (rhs)
7297 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
7299 /* This should really only be used when attempting to distinguish
7300 what sort of a pointer to function we have. For now, any
7301 arithmetic operation which is not supported on pointers
7302 is rejected as an error. */
7304 switch (TREE_CODE (rhs))
7306 case COMPONENT_REF:
7308 tree member = TREE_OPERAND (rhs, 1);
7310 member = instantiate_type (lhstype, member, flags);
7311 if (member != error_mark_node
7312 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
7313 /* Do not lose object's side effects. */
7314 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
7315 TREE_OPERAND (rhs, 0), member);
7316 return member;
7319 case OFFSET_REF:
7320 rhs = TREE_OPERAND (rhs, 1);
7321 if (BASELINK_P (rhs))
7322 return instantiate_type (lhstype, rhs, flags_in);
7324 /* This can happen if we are forming a pointer-to-member for a
7325 member template. */
7326 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
7328 /* Fall through. */
7330 case TEMPLATE_ID_EXPR:
7332 tree fns = TREE_OPERAND (rhs, 0);
7333 tree args = TREE_OPERAND (rhs, 1);
7335 return
7336 resolve_address_of_overloaded_function (lhstype, fns, flags_in,
7337 /*template_only=*/true,
7338 args, access_path);
7341 case OVERLOAD:
7342 case FUNCTION_DECL:
7343 return
7344 resolve_address_of_overloaded_function (lhstype, rhs, flags_in,
7345 /*template_only=*/false,
7346 /*explicit_targs=*/NULL_TREE,
7347 access_path);
7349 case ADDR_EXPR:
7351 if (PTRMEM_OK_P (rhs))
7352 flags |= tf_ptrmem_ok;
7354 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
7357 case ERROR_MARK:
7358 return error_mark_node;
7360 default:
7361 gcc_unreachable ();
7363 return error_mark_node;
7366 /* Return the name of the virtual function pointer field
7367 (as an IDENTIFIER_NODE) for the given TYPE. Note that
7368 this may have to look back through base types to find the
7369 ultimate field name. (For single inheritance, these could
7370 all be the same name. Who knows for multiple inheritance). */
7372 static tree
7373 get_vfield_name (tree type)
7375 tree binfo, base_binfo;
7376 char *buf;
7378 for (binfo = TYPE_BINFO (type);
7379 BINFO_N_BASE_BINFOS (binfo);
7380 binfo = base_binfo)
7382 base_binfo = BINFO_BASE_BINFO (binfo, 0);
7384 if (BINFO_VIRTUAL_P (base_binfo)
7385 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
7386 break;
7389 type = BINFO_TYPE (binfo);
7390 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
7391 + TYPE_NAME_LENGTH (type) + 2);
7392 sprintf (buf, VFIELD_NAME_FORMAT,
7393 IDENTIFIER_POINTER (constructor_name (type)));
7394 return get_identifier (buf);
7397 void
7398 print_class_statistics (void)
7400 if (! GATHER_STATISTICS)
7401 return;
7403 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
7404 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
7405 if (n_vtables)
7407 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
7408 n_vtables, n_vtable_searches);
7409 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
7410 n_vtable_entries, n_vtable_elems);
7414 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
7415 according to [class]:
7416 The class-name is also inserted
7417 into the scope of the class itself. For purposes of access checking,
7418 the inserted class name is treated as if it were a public member name. */
7420 void
7421 build_self_reference (void)
7423 tree name = constructor_name (current_class_type);
7424 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
7425 tree saved_cas;
7427 DECL_NONLOCAL (value) = 1;
7428 DECL_CONTEXT (value) = current_class_type;
7429 DECL_ARTIFICIAL (value) = 1;
7430 SET_DECL_SELF_REFERENCE_P (value);
7431 set_underlying_type (value);
7433 if (processing_template_decl)
7434 value = push_template_decl (value);
7436 saved_cas = current_access_specifier;
7437 current_access_specifier = access_public_node;
7438 finish_member_declaration (value);
7439 current_access_specifier = saved_cas;
7442 /* Returns 1 if TYPE contains only padding bytes. */
7445 is_empty_class (tree type)
7447 if (type == error_mark_node)
7448 return 0;
7450 if (! CLASS_TYPE_P (type))
7451 return 0;
7453 /* In G++ 3.2, whether or not a class was empty was determined by
7454 looking at its size. */
7455 if (abi_version_at_least (2))
7456 return CLASSTYPE_EMPTY_P (type);
7457 else
7458 return integer_zerop (CLASSTYPE_SIZE (type));
7461 /* Returns true if TYPE contains an empty class. */
7463 static bool
7464 contains_empty_class_p (tree type)
7466 if (is_empty_class (type))
7467 return true;
7468 if (CLASS_TYPE_P (type))
7470 tree field;
7471 tree binfo;
7472 tree base_binfo;
7473 int i;
7475 for (binfo = TYPE_BINFO (type), i = 0;
7476 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7477 if (contains_empty_class_p (BINFO_TYPE (base_binfo)))
7478 return true;
7479 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
7480 if (TREE_CODE (field) == FIELD_DECL
7481 && !DECL_ARTIFICIAL (field)
7482 && is_empty_class (TREE_TYPE (field)))
7483 return true;
7485 else if (TREE_CODE (type) == ARRAY_TYPE)
7486 return contains_empty_class_p (TREE_TYPE (type));
7487 return false;
7490 /* Returns true if TYPE contains no actual data, just various
7491 possible combinations of empty classes and possibly a vptr. */
7493 bool
7494 is_really_empty_class (tree type)
7496 if (CLASS_TYPE_P (type))
7498 tree field;
7499 tree binfo;
7500 tree base_binfo;
7501 int i;
7503 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
7504 out, but we'd like to be able to check this before then. */
7505 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
7506 return true;
7508 for (binfo = TYPE_BINFO (type), i = 0;
7509 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7510 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
7511 return false;
7512 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7513 if (TREE_CODE (field) == FIELD_DECL
7514 && !DECL_ARTIFICIAL (field)
7515 && !is_really_empty_class (TREE_TYPE (field)))
7516 return false;
7517 return true;
7519 else if (TREE_CODE (type) == ARRAY_TYPE)
7520 return is_really_empty_class (TREE_TYPE (type));
7521 return false;
7524 /* Note that NAME was looked up while the current class was being
7525 defined and that the result of that lookup was DECL. */
7527 void
7528 maybe_note_name_used_in_class (tree name, tree decl)
7530 splay_tree names_used;
7532 /* If we're not defining a class, there's nothing to do. */
7533 if (!(innermost_scope_kind() == sk_class
7534 && TYPE_BEING_DEFINED (current_class_type)
7535 && !LAMBDA_TYPE_P (current_class_type)))
7536 return;
7538 /* If there's already a binding for this NAME, then we don't have
7539 anything to worry about. */
7540 if (lookup_member (current_class_type, name,
7541 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
7542 return;
7544 if (!current_class_stack[current_class_depth - 1].names_used)
7545 current_class_stack[current_class_depth - 1].names_used
7546 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
7547 names_used = current_class_stack[current_class_depth - 1].names_used;
7549 splay_tree_insert (names_used,
7550 (splay_tree_key) name,
7551 (splay_tree_value) decl);
7554 /* Note that NAME was declared (as DECL) in the current class. Check
7555 to see that the declaration is valid. */
7557 void
7558 note_name_declared_in_class (tree name, tree decl)
7560 splay_tree names_used;
7561 splay_tree_node n;
7563 /* Look to see if we ever used this name. */
7564 names_used
7565 = current_class_stack[current_class_depth - 1].names_used;
7566 if (!names_used)
7567 return;
7568 /* The C language allows members to be declared with a type of the same
7569 name, and the C++ standard says this diagnostic is not required. So
7570 allow it in extern "C" blocks unless predantic is specified.
7571 Allow it in all cases if -ms-extensions is specified. */
7572 if ((!pedantic && current_lang_name == lang_name_c)
7573 || flag_ms_extensions)
7574 return;
7575 n = splay_tree_lookup (names_used, (splay_tree_key) name);
7576 if (n)
7578 /* [basic.scope.class]
7580 A name N used in a class S shall refer to the same declaration
7581 in its context and when re-evaluated in the completed scope of
7582 S. */
7583 permerror (input_location, "declaration of %q#D", decl);
7584 permerror (input_location, "changes meaning of %qD from %q+#D",
7585 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
7589 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
7590 Secondary vtables are merged with primary vtables; this function
7591 will return the VAR_DECL for the primary vtable. */
7593 tree
7594 get_vtbl_decl_for_binfo (tree binfo)
7596 tree decl;
7598 decl = BINFO_VTABLE (binfo);
7599 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
7601 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
7602 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
7604 if (decl)
7605 gcc_assert (TREE_CODE (decl) == VAR_DECL);
7606 return decl;
7610 /* Returns the binfo for the primary base of BINFO. If the resulting
7611 BINFO is a virtual base, and it is inherited elsewhere in the
7612 hierarchy, then the returned binfo might not be the primary base of
7613 BINFO in the complete object. Check BINFO_PRIMARY_P or
7614 BINFO_LOST_PRIMARY_P to be sure. */
7616 static tree
7617 get_primary_binfo (tree binfo)
7619 tree primary_base;
7621 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
7622 if (!primary_base)
7623 return NULL_TREE;
7625 return copied_binfo (primary_base, binfo);
7628 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
7630 static int
7631 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
7633 if (!indented_p)
7634 fprintf (stream, "%*s", indent, "");
7635 return 1;
7638 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
7639 INDENT should be zero when called from the top level; it is
7640 incremented recursively. IGO indicates the next expected BINFO in
7641 inheritance graph ordering. */
7643 static tree
7644 dump_class_hierarchy_r (FILE *stream,
7645 int flags,
7646 tree binfo,
7647 tree igo,
7648 int indent)
7650 int indented = 0;
7651 tree base_binfo;
7652 int i;
7654 indented = maybe_indent_hierarchy (stream, indent, 0);
7655 fprintf (stream, "%s (0x%lx) ",
7656 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
7657 (unsigned long) binfo);
7658 if (binfo != igo)
7660 fprintf (stream, "alternative-path\n");
7661 return igo;
7663 igo = TREE_CHAIN (binfo);
7665 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
7666 tree_low_cst (BINFO_OFFSET (binfo), 0));
7667 if (is_empty_class (BINFO_TYPE (binfo)))
7668 fprintf (stream, " empty");
7669 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
7670 fprintf (stream, " nearly-empty");
7671 if (BINFO_VIRTUAL_P (binfo))
7672 fprintf (stream, " virtual");
7673 fprintf (stream, "\n");
7675 indented = 0;
7676 if (BINFO_PRIMARY_P (binfo))
7678 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7679 fprintf (stream, " primary-for %s (0x%lx)",
7680 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
7681 TFF_PLAIN_IDENTIFIER),
7682 (unsigned long)BINFO_INHERITANCE_CHAIN (binfo));
7684 if (BINFO_LOST_PRIMARY_P (binfo))
7686 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7687 fprintf (stream, " lost-primary");
7689 if (indented)
7690 fprintf (stream, "\n");
7692 if (!(flags & TDF_SLIM))
7694 int indented = 0;
7696 if (BINFO_SUBVTT_INDEX (binfo))
7698 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7699 fprintf (stream, " subvttidx=%s",
7700 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
7701 TFF_PLAIN_IDENTIFIER));
7703 if (BINFO_VPTR_INDEX (binfo))
7705 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7706 fprintf (stream, " vptridx=%s",
7707 expr_as_string (BINFO_VPTR_INDEX (binfo),
7708 TFF_PLAIN_IDENTIFIER));
7710 if (BINFO_VPTR_FIELD (binfo))
7712 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7713 fprintf (stream, " vbaseoffset=%s",
7714 expr_as_string (BINFO_VPTR_FIELD (binfo),
7715 TFF_PLAIN_IDENTIFIER));
7717 if (BINFO_VTABLE (binfo))
7719 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7720 fprintf (stream, " vptr=%s",
7721 expr_as_string (BINFO_VTABLE (binfo),
7722 TFF_PLAIN_IDENTIFIER));
7725 if (indented)
7726 fprintf (stream, "\n");
7729 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7730 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
7732 return igo;
7735 /* Dump the BINFO hierarchy for T. */
7737 static void
7738 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
7740 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7741 fprintf (stream, " size=%lu align=%lu\n",
7742 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
7743 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
7744 fprintf (stream, " base size=%lu base align=%lu\n",
7745 (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0)
7746 / BITS_PER_UNIT),
7747 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
7748 / BITS_PER_UNIT));
7749 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
7750 fprintf (stream, "\n");
7753 /* Debug interface to hierarchy dumping. */
7755 void
7756 debug_class (tree t)
7758 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
7761 static void
7762 dump_class_hierarchy (tree t)
7764 int flags;
7765 FILE *stream = dump_begin (TDI_class, &flags);
7767 if (stream)
7769 dump_class_hierarchy_1 (stream, flags, t);
7770 dump_end (TDI_class, stream);
7774 static void
7775 dump_array (FILE * stream, tree decl)
7777 tree value;
7778 unsigned HOST_WIDE_INT ix;
7779 HOST_WIDE_INT elt;
7780 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
7782 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
7783 / BITS_PER_UNIT);
7784 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
7785 fprintf (stream, " %s entries",
7786 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
7787 TFF_PLAIN_IDENTIFIER));
7788 fprintf (stream, "\n");
7790 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
7791 ix, value)
7792 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
7793 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
7796 static void
7797 dump_vtable (tree t, tree binfo, tree vtable)
7799 int flags;
7800 FILE *stream = dump_begin (TDI_class, &flags);
7802 if (!stream)
7803 return;
7805 if (!(flags & TDF_SLIM))
7807 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
7809 fprintf (stream, "%s for %s",
7810 ctor_vtbl_p ? "Construction vtable" : "Vtable",
7811 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
7812 if (ctor_vtbl_p)
7814 if (!BINFO_VIRTUAL_P (binfo))
7815 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
7816 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7818 fprintf (stream, "\n");
7819 dump_array (stream, vtable);
7820 fprintf (stream, "\n");
7823 dump_end (TDI_class, stream);
7826 static void
7827 dump_vtt (tree t, tree vtt)
7829 int flags;
7830 FILE *stream = dump_begin (TDI_class, &flags);
7832 if (!stream)
7833 return;
7835 if (!(flags & TDF_SLIM))
7837 fprintf (stream, "VTT for %s\n",
7838 type_as_string (t, TFF_PLAIN_IDENTIFIER));
7839 dump_array (stream, vtt);
7840 fprintf (stream, "\n");
7843 dump_end (TDI_class, stream);
7846 /* Dump a function or thunk and its thunkees. */
7848 static void
7849 dump_thunk (FILE *stream, int indent, tree thunk)
7851 static const char spaces[] = " ";
7852 tree name = DECL_NAME (thunk);
7853 tree thunks;
7855 fprintf (stream, "%.*s%p %s %s", indent, spaces,
7856 (void *)thunk,
7857 !DECL_THUNK_P (thunk) ? "function"
7858 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
7859 name ? IDENTIFIER_POINTER (name) : "<unset>");
7860 if (DECL_THUNK_P (thunk))
7862 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
7863 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
7865 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
7866 if (!virtual_adjust)
7867 /*NOP*/;
7868 else if (DECL_THIS_THUNK_P (thunk))
7869 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
7870 tree_low_cst (virtual_adjust, 0));
7871 else
7872 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
7873 tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust), 0),
7874 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
7875 if (THUNK_ALIAS (thunk))
7876 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
7878 fprintf (stream, "\n");
7879 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
7880 dump_thunk (stream, indent + 2, thunks);
7883 /* Dump the thunks for FN. */
7885 void
7886 debug_thunks (tree fn)
7888 dump_thunk (stderr, 0, fn);
7891 /* Virtual function table initialization. */
7893 /* Create all the necessary vtables for T and its base classes. */
7895 static void
7896 finish_vtbls (tree t)
7898 tree vbase;
7899 VEC(constructor_elt,gc) *v = NULL;
7900 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
7902 /* We lay out the primary and secondary vtables in one contiguous
7903 vtable. The primary vtable is first, followed by the non-virtual
7904 secondary vtables in inheritance graph order. */
7905 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
7906 vtable, t, &v);
7908 /* Then come the virtual bases, also in inheritance graph order. */
7909 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
7911 if (!BINFO_VIRTUAL_P (vbase))
7912 continue;
7913 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
7916 if (BINFO_VTABLE (TYPE_BINFO (t)))
7917 initialize_vtable (TYPE_BINFO (t), v);
7920 /* Initialize the vtable for BINFO with the INITS. */
7922 static void
7923 initialize_vtable (tree binfo, VEC(constructor_elt,gc) *inits)
7925 tree decl;
7927 layout_vtable_decl (binfo, VEC_length (constructor_elt, inits));
7928 decl = get_vtbl_decl_for_binfo (binfo);
7929 initialize_artificial_var (decl, inits);
7930 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
7933 /* Build the VTT (virtual table table) for T.
7934 A class requires a VTT if it has virtual bases.
7936 This holds
7937 1 - primary virtual pointer for complete object T
7938 2 - secondary VTTs for each direct non-virtual base of T which requires a
7940 3 - secondary virtual pointers for each direct or indirect base of T which
7941 has virtual bases or is reachable via a virtual path from T.
7942 4 - secondary VTTs for each direct or indirect virtual base of T.
7944 Secondary VTTs look like complete object VTTs without part 4. */
7946 static void
7947 build_vtt (tree t)
7949 tree type;
7950 tree vtt;
7951 tree index;
7952 VEC(constructor_elt,gc) *inits;
7954 /* Build up the initializers for the VTT. */
7955 inits = NULL;
7956 index = size_zero_node;
7957 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
7959 /* If we didn't need a VTT, we're done. */
7960 if (!inits)
7961 return;
7963 /* Figure out the type of the VTT. */
7964 type = build_array_of_n_type (const_ptr_type_node,
7965 VEC_length (constructor_elt, inits));
7967 /* Now, build the VTT object itself. */
7968 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
7969 initialize_artificial_var (vtt, inits);
7970 /* Add the VTT to the vtables list. */
7971 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
7972 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
7974 dump_vtt (t, vtt);
7977 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
7978 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
7979 and CHAIN the vtable pointer for this binfo after construction is
7980 complete. VALUE can also be another BINFO, in which case we recurse. */
7982 static tree
7983 binfo_ctor_vtable (tree binfo)
7985 tree vt;
7987 while (1)
7989 vt = BINFO_VTABLE (binfo);
7990 if (TREE_CODE (vt) == TREE_LIST)
7991 vt = TREE_VALUE (vt);
7992 if (TREE_CODE (vt) == TREE_BINFO)
7993 binfo = vt;
7994 else
7995 break;
7998 return vt;
8001 /* Data for secondary VTT initialization. */
8002 typedef struct secondary_vptr_vtt_init_data_s
8004 /* Is this the primary VTT? */
8005 bool top_level_p;
8007 /* Current index into the VTT. */
8008 tree index;
8010 /* Vector of initializers built up. */
8011 VEC(constructor_elt,gc) *inits;
8013 /* The type being constructed by this secondary VTT. */
8014 tree type_being_constructed;
8015 } secondary_vptr_vtt_init_data;
8017 /* Recursively build the VTT-initializer for BINFO (which is in the
8018 hierarchy dominated by T). INITS points to the end of the initializer
8019 list to date. INDEX is the VTT index where the next element will be
8020 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8021 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8022 for virtual bases of T. When it is not so, we build the constructor
8023 vtables for the BINFO-in-T variant. */
8025 static void
8026 build_vtt_inits (tree binfo, tree t, VEC(constructor_elt,gc) **inits, tree *index)
8028 int i;
8029 tree b;
8030 tree init;
8031 secondary_vptr_vtt_init_data data;
8032 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8034 /* We only need VTTs for subobjects with virtual bases. */
8035 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8036 return;
8038 /* We need to use a construction vtable if this is not the primary
8039 VTT. */
8040 if (!top_level_p)
8042 build_ctor_vtbl_group (binfo, t);
8044 /* Record the offset in the VTT where this sub-VTT can be found. */
8045 BINFO_SUBVTT_INDEX (binfo) = *index;
8048 /* Add the address of the primary vtable for the complete object. */
8049 init = binfo_ctor_vtable (binfo);
8050 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8051 if (top_level_p)
8053 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8054 BINFO_VPTR_INDEX (binfo) = *index;
8056 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8058 /* Recursively add the secondary VTTs for non-virtual bases. */
8059 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8060 if (!BINFO_VIRTUAL_P (b))
8061 build_vtt_inits (b, t, inits, index);
8063 /* Add secondary virtual pointers for all subobjects of BINFO with
8064 either virtual bases or reachable along a virtual path, except
8065 subobjects that are non-virtual primary bases. */
8066 data.top_level_p = top_level_p;
8067 data.index = *index;
8068 data.inits = *inits;
8069 data.type_being_constructed = BINFO_TYPE (binfo);
8071 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8073 *index = data.index;
8075 /* data.inits might have grown as we added secondary virtual pointers.
8076 Make sure our caller knows about the new vector. */
8077 *inits = data.inits;
8079 if (top_level_p)
8080 /* Add the secondary VTTs for virtual bases in inheritance graph
8081 order. */
8082 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8084 if (!BINFO_VIRTUAL_P (b))
8085 continue;
8087 build_vtt_inits (b, t, inits, index);
8089 else
8090 /* Remove the ctor vtables we created. */
8091 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8094 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8095 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8097 static tree
8098 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8100 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8102 /* We don't care about bases that don't have vtables. */
8103 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8104 return dfs_skip_bases;
8106 /* We're only interested in proper subobjects of the type being
8107 constructed. */
8108 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8109 return NULL_TREE;
8111 /* We're only interested in bases with virtual bases or reachable
8112 via a virtual path from the type being constructed. */
8113 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8114 || binfo_via_virtual (binfo, data->type_being_constructed)))
8115 return dfs_skip_bases;
8117 /* We're not interested in non-virtual primary bases. */
8118 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8119 return NULL_TREE;
8121 /* Record the index where this secondary vptr can be found. */
8122 if (data->top_level_p)
8124 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8125 BINFO_VPTR_INDEX (binfo) = data->index;
8127 if (BINFO_VIRTUAL_P (binfo))
8129 /* It's a primary virtual base, and this is not a
8130 construction vtable. Find the base this is primary of in
8131 the inheritance graph, and use that base's vtable
8132 now. */
8133 while (BINFO_PRIMARY_P (binfo))
8134 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8138 /* Add the initializer for the secondary vptr itself. */
8139 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8141 /* Advance the vtt index. */
8142 data->index = size_binop (PLUS_EXPR, data->index,
8143 TYPE_SIZE_UNIT (ptr_type_node));
8145 return NULL_TREE;
8148 /* Called from build_vtt_inits via dfs_walk. After building
8149 constructor vtables and generating the sub-vtt from them, we need
8150 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8151 binfo of the base whose sub vtt was generated. */
8153 static tree
8154 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8156 tree vtable = BINFO_VTABLE (binfo);
8158 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8159 /* If this class has no vtable, none of its bases do. */
8160 return dfs_skip_bases;
8162 if (!vtable)
8163 /* This might be a primary base, so have no vtable in this
8164 hierarchy. */
8165 return NULL_TREE;
8167 /* If we scribbled the construction vtable vptr into BINFO, clear it
8168 out now. */
8169 if (TREE_CODE (vtable) == TREE_LIST
8170 && (TREE_PURPOSE (vtable) == (tree) data))
8171 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8173 return NULL_TREE;
8176 /* Build the construction vtable group for BINFO which is in the
8177 hierarchy dominated by T. */
8179 static void
8180 build_ctor_vtbl_group (tree binfo, tree t)
8182 tree type;
8183 tree vtbl;
8184 tree id;
8185 tree vbase;
8186 VEC(constructor_elt,gc) *v;
8188 /* See if we've already created this construction vtable group. */
8189 id = mangle_ctor_vtbl_for_type (t, binfo);
8190 if (IDENTIFIER_GLOBAL_VALUE (id))
8191 return;
8193 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8194 /* Build a version of VTBL (with the wrong type) for use in
8195 constructing the addresses of secondary vtables in the
8196 construction vtable group. */
8197 vtbl = build_vtable (t, id, ptr_type_node);
8198 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8200 v = NULL;
8201 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8202 binfo, vtbl, t, &v);
8204 /* Add the vtables for each of our virtual bases using the vbase in T
8205 binfo. */
8206 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8207 vbase;
8208 vbase = TREE_CHAIN (vbase))
8210 tree b;
8212 if (!BINFO_VIRTUAL_P (vbase))
8213 continue;
8214 b = copied_binfo (vbase, binfo);
8216 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8219 /* Figure out the type of the construction vtable. */
8220 type = build_array_of_n_type (vtable_entry_type,
8221 VEC_length (constructor_elt, v));
8222 layout_type (type);
8223 TREE_TYPE (vtbl) = type;
8224 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8225 layout_decl (vtbl, 0);
8227 /* Initialize the construction vtable. */
8228 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8229 initialize_artificial_var (vtbl, v);
8230 dump_vtable (t, binfo, vtbl);
8233 /* Add the vtbl initializers for BINFO (and its bases other than
8234 non-virtual primaries) to the list of INITS. BINFO is in the
8235 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8236 the constructor the vtbl inits should be accumulated for. (If this
8237 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8238 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8239 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8240 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8241 but are not necessarily the same in terms of layout. */
8243 static void
8244 accumulate_vtbl_inits (tree binfo,
8245 tree orig_binfo,
8246 tree rtti_binfo,
8247 tree vtbl,
8248 tree t,
8249 VEC(constructor_elt,gc) **inits)
8251 int i;
8252 tree base_binfo;
8253 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8255 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8257 /* If it doesn't have a vptr, we don't do anything. */
8258 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8259 return;
8261 /* If we're building a construction vtable, we're not interested in
8262 subobjects that don't require construction vtables. */
8263 if (ctor_vtbl_p
8264 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8265 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8266 return;
8268 /* Build the initializers for the BINFO-in-T vtable. */
8269 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8271 /* Walk the BINFO and its bases. We walk in preorder so that as we
8272 initialize each vtable we can figure out at what offset the
8273 secondary vtable lies from the primary vtable. We can't use
8274 dfs_walk here because we need to iterate through bases of BINFO
8275 and RTTI_BINFO simultaneously. */
8276 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8278 /* Skip virtual bases. */
8279 if (BINFO_VIRTUAL_P (base_binfo))
8280 continue;
8281 accumulate_vtbl_inits (base_binfo,
8282 BINFO_BASE_BINFO (orig_binfo, i),
8283 rtti_binfo, vtbl, t,
8284 inits);
8288 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8289 BINFO vtable to L. */
8291 static void
8292 dfs_accumulate_vtbl_inits (tree binfo,
8293 tree orig_binfo,
8294 tree rtti_binfo,
8295 tree orig_vtbl,
8296 tree t,
8297 VEC(constructor_elt,gc) **l)
8299 tree vtbl = NULL_TREE;
8300 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8301 int n_inits;
8303 if (ctor_vtbl_p
8304 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
8306 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
8307 primary virtual base. If it is not the same primary in
8308 the hierarchy of T, we'll need to generate a ctor vtable
8309 for it, to place at its location in T. If it is the same
8310 primary, we still need a VTT entry for the vtable, but it
8311 should point to the ctor vtable for the base it is a
8312 primary for within the sub-hierarchy of RTTI_BINFO.
8314 There are three possible cases:
8316 1) We are in the same place.
8317 2) We are a primary base within a lost primary virtual base of
8318 RTTI_BINFO.
8319 3) We are primary to something not a base of RTTI_BINFO. */
8321 tree b;
8322 tree last = NULL_TREE;
8324 /* First, look through the bases we are primary to for RTTI_BINFO
8325 or a virtual base. */
8326 b = binfo;
8327 while (BINFO_PRIMARY_P (b))
8329 b = BINFO_INHERITANCE_CHAIN (b);
8330 last = b;
8331 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8332 goto found;
8334 /* If we run out of primary links, keep looking down our
8335 inheritance chain; we might be an indirect primary. */
8336 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
8337 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8338 break;
8339 found:
8341 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
8342 base B and it is a base of RTTI_BINFO, this is case 2. In
8343 either case, we share our vtable with LAST, i.e. the
8344 derived-most base within B of which we are a primary. */
8345 if (b == rtti_binfo
8346 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
8347 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
8348 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
8349 binfo_ctor_vtable after everything's been set up. */
8350 vtbl = last;
8352 /* Otherwise, this is case 3 and we get our own. */
8354 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
8355 return;
8357 n_inits = VEC_length (constructor_elt, *l);
8359 if (!vtbl)
8361 tree index;
8362 int non_fn_entries;
8364 /* Add the initializer for this vtable. */
8365 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
8366 &non_fn_entries, l);
8368 /* Figure out the position to which the VPTR should point. */
8369 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
8370 index = size_binop (MULT_EXPR,
8371 TYPE_SIZE_UNIT (vtable_entry_type),
8372 size_int (non_fn_entries + n_inits));
8373 vtbl = fold_build_pointer_plus (vtbl, index);
8376 if (ctor_vtbl_p)
8377 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
8378 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
8379 straighten this out. */
8380 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
8381 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
8382 /* Throw away any unneeded intializers. */
8383 VEC_truncate (constructor_elt, *l, n_inits);
8384 else
8385 /* For an ordinary vtable, set BINFO_VTABLE. */
8386 BINFO_VTABLE (binfo) = vtbl;
8389 static GTY(()) tree abort_fndecl_addr;
8391 /* Construct the initializer for BINFO's virtual function table. BINFO
8392 is part of the hierarchy dominated by T. If we're building a
8393 construction vtable, the ORIG_BINFO is the binfo we should use to
8394 find the actual function pointers to put in the vtable - but they
8395 can be overridden on the path to most-derived in the graph that
8396 ORIG_BINFO belongs. Otherwise,
8397 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
8398 BINFO that should be indicated by the RTTI information in the
8399 vtable; it will be a base class of T, rather than T itself, if we
8400 are building a construction vtable.
8402 The value returned is a TREE_LIST suitable for wrapping in a
8403 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
8404 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
8405 number of non-function entries in the vtable.
8407 It might seem that this function should never be called with a
8408 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
8409 base is always subsumed by a derived class vtable. However, when
8410 we are building construction vtables, we do build vtables for
8411 primary bases; we need these while the primary base is being
8412 constructed. */
8414 static void
8415 build_vtbl_initializer (tree binfo,
8416 tree orig_binfo,
8417 tree t,
8418 tree rtti_binfo,
8419 int* non_fn_entries_p,
8420 VEC(constructor_elt,gc) **inits)
8422 tree v;
8423 vtbl_init_data vid;
8424 unsigned ix, jx;
8425 tree vbinfo;
8426 VEC(tree,gc) *vbases;
8427 constructor_elt *e;
8429 /* Initialize VID. */
8430 memset (&vid, 0, sizeof (vid));
8431 vid.binfo = binfo;
8432 vid.derived = t;
8433 vid.rtti_binfo = rtti_binfo;
8434 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8435 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8436 vid.generate_vcall_entries = true;
8437 /* The first vbase or vcall offset is at index -3 in the vtable. */
8438 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
8440 /* Add entries to the vtable for RTTI. */
8441 build_rtti_vtbl_entries (binfo, &vid);
8443 /* Create an array for keeping track of the functions we've
8444 processed. When we see multiple functions with the same
8445 signature, we share the vcall offsets. */
8446 vid.fns = VEC_alloc (tree, gc, 32);
8447 /* Add the vcall and vbase offset entries. */
8448 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
8450 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
8451 build_vbase_offset_vtbl_entries. */
8452 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
8453 VEC_iterate (tree, vbases, ix, vbinfo); ix++)
8454 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
8456 /* If the target requires padding between data entries, add that now. */
8457 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
8459 int n_entries = VEC_length (constructor_elt, vid.inits);
8461 VEC_safe_grow (constructor_elt, gc, vid.inits,
8462 TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
8464 /* Move data entries into their new positions and add padding
8465 after the new positions. Iterate backwards so we don't
8466 overwrite entries that we would need to process later. */
8467 for (ix = n_entries - 1;
8468 VEC_iterate (constructor_elt, vid.inits, ix, e);
8469 ix--)
8471 int j;
8472 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
8473 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
8475 VEC_replace (constructor_elt, vid.inits, new_position, *e);
8477 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
8479 constructor_elt *f = &VEC_index (constructor_elt, vid.inits,
8480 new_position - j);
8481 f->index = NULL_TREE;
8482 f->value = build1 (NOP_EXPR, vtable_entry_type,
8483 null_pointer_node);
8488 if (non_fn_entries_p)
8489 *non_fn_entries_p = VEC_length (constructor_elt, vid.inits);
8491 /* The initializers for virtual functions were built up in reverse
8492 order. Straighten them out and add them to the running list in one
8493 step. */
8494 jx = VEC_length (constructor_elt, *inits);
8495 VEC_safe_grow (constructor_elt, gc, *inits,
8496 (jx + VEC_length (constructor_elt, vid.inits)));
8498 for (ix = VEC_length (constructor_elt, vid.inits) - 1;
8499 VEC_iterate (constructor_elt, vid.inits, ix, e);
8500 ix--, jx++)
8501 VEC_replace (constructor_elt, *inits, jx, *e);
8503 /* Go through all the ordinary virtual functions, building up
8504 initializers. */
8505 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
8507 tree delta;
8508 tree vcall_index;
8509 tree fn, fn_original;
8510 tree init = NULL_TREE;
8512 fn = BV_FN (v);
8513 fn_original = fn;
8514 if (DECL_THUNK_P (fn))
8516 if (!DECL_NAME (fn))
8517 finish_thunk (fn);
8518 if (THUNK_ALIAS (fn))
8520 fn = THUNK_ALIAS (fn);
8521 BV_FN (v) = fn;
8523 fn_original = THUNK_TARGET (fn);
8526 /* If the only definition of this function signature along our
8527 primary base chain is from a lost primary, this vtable slot will
8528 never be used, so just zero it out. This is important to avoid
8529 requiring extra thunks which cannot be generated with the function.
8531 We first check this in update_vtable_entry_for_fn, so we handle
8532 restored primary bases properly; we also need to do it here so we
8533 zero out unused slots in ctor vtables, rather than filling them
8534 with erroneous values (though harmless, apart from relocation
8535 costs). */
8536 if (BV_LOST_PRIMARY (v))
8537 init = size_zero_node;
8539 if (! init)
8541 /* Pull the offset for `this', and the function to call, out of
8542 the list. */
8543 delta = BV_DELTA (v);
8544 vcall_index = BV_VCALL_INDEX (v);
8546 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
8547 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8549 /* You can't call an abstract virtual function; it's abstract.
8550 So, we replace these functions with __pure_virtual. */
8551 if (DECL_PURE_VIRTUAL_P (fn_original))
8553 fn = abort_fndecl;
8554 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8556 if (abort_fndecl_addr == NULL)
8557 abort_fndecl_addr
8558 = fold_convert (vfunc_ptr_type_node,
8559 build_fold_addr_expr (fn));
8560 init = abort_fndecl_addr;
8563 /* Likewise for deleted virtuals. */
8564 else if (DECL_DELETED_FN (fn_original))
8566 fn = get_identifier ("__cxa_deleted_virtual");
8567 if (!get_global_value_if_present (fn, &fn))
8568 fn = push_library_fn (fn, (build_function_type_list
8569 (void_type_node, NULL_TREE)),
8570 NULL_TREE);
8571 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8572 init = fold_convert (vfunc_ptr_type_node,
8573 build_fold_addr_expr (fn));
8575 else
8577 if (!integer_zerop (delta) || vcall_index)
8579 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
8580 if (!DECL_NAME (fn))
8581 finish_thunk (fn);
8583 /* Take the address of the function, considering it to be of an
8584 appropriate generic type. */
8585 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8586 init = fold_convert (vfunc_ptr_type_node,
8587 build_fold_addr_expr (fn));
8591 /* And add it to the chain of initializers. */
8592 if (TARGET_VTABLE_USES_DESCRIPTORS)
8594 int i;
8595 if (init == size_zero_node)
8596 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8597 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8598 else
8599 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8601 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
8602 fn, build_int_cst (NULL_TREE, i));
8603 TREE_CONSTANT (fdesc) = 1;
8605 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
8608 else
8609 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8613 /* Adds to vid->inits the initializers for the vbase and vcall
8614 offsets in BINFO, which is in the hierarchy dominated by T. */
8616 static void
8617 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
8619 tree b;
8621 /* If this is a derived class, we must first create entries
8622 corresponding to the primary base class. */
8623 b = get_primary_binfo (binfo);
8624 if (b)
8625 build_vcall_and_vbase_vtbl_entries (b, vid);
8627 /* Add the vbase entries for this base. */
8628 build_vbase_offset_vtbl_entries (binfo, vid);
8629 /* Add the vcall entries for this base. */
8630 build_vcall_offset_vtbl_entries (binfo, vid);
8633 /* Returns the initializers for the vbase offset entries in the vtable
8634 for BINFO (which is part of the class hierarchy dominated by T), in
8635 reverse order. VBASE_OFFSET_INDEX gives the vtable index
8636 where the next vbase offset will go. */
8638 static void
8639 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
8641 tree vbase;
8642 tree t;
8643 tree non_primary_binfo;
8645 /* If there are no virtual baseclasses, then there is nothing to
8646 do. */
8647 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8648 return;
8650 t = vid->derived;
8652 /* We might be a primary base class. Go up the inheritance hierarchy
8653 until we find the most derived class of which we are a primary base:
8654 it is the offset of that which we need to use. */
8655 non_primary_binfo = binfo;
8656 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8658 tree b;
8660 /* If we have reached a virtual base, then it must be a primary
8661 base (possibly multi-level) of vid->binfo, or we wouldn't
8662 have called build_vcall_and_vbase_vtbl_entries for it. But it
8663 might be a lost primary, so just skip down to vid->binfo. */
8664 if (BINFO_VIRTUAL_P (non_primary_binfo))
8666 non_primary_binfo = vid->binfo;
8667 break;
8670 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8671 if (get_primary_binfo (b) != non_primary_binfo)
8672 break;
8673 non_primary_binfo = b;
8676 /* Go through the virtual bases, adding the offsets. */
8677 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8678 vbase;
8679 vbase = TREE_CHAIN (vbase))
8681 tree b;
8682 tree delta;
8684 if (!BINFO_VIRTUAL_P (vbase))
8685 continue;
8687 /* Find the instance of this virtual base in the complete
8688 object. */
8689 b = copied_binfo (vbase, binfo);
8691 /* If we've already got an offset for this virtual base, we
8692 don't need another one. */
8693 if (BINFO_VTABLE_PATH_MARKED (b))
8694 continue;
8695 BINFO_VTABLE_PATH_MARKED (b) = 1;
8697 /* Figure out where we can find this vbase offset. */
8698 delta = size_binop (MULT_EXPR,
8699 vid->index,
8700 convert (ssizetype,
8701 TYPE_SIZE_UNIT (vtable_entry_type)));
8702 if (vid->primary_vtbl_p)
8703 BINFO_VPTR_FIELD (b) = delta;
8705 if (binfo != TYPE_BINFO (t))
8706 /* The vbase offset had better be the same. */
8707 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
8709 /* The next vbase will come at a more negative offset. */
8710 vid->index = size_binop (MINUS_EXPR, vid->index,
8711 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
8713 /* The initializer is the delta from BINFO to this virtual base.
8714 The vbase offsets go in reverse inheritance-graph order, and
8715 we are walking in inheritance graph order so these end up in
8716 the right order. */
8717 delta = size_diffop_loc (input_location,
8718 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
8720 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
8721 fold_build1_loc (input_location, NOP_EXPR,
8722 vtable_entry_type, delta));
8726 /* Adds the initializers for the vcall offset entries in the vtable
8727 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
8728 to VID->INITS. */
8730 static void
8731 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
8733 /* We only need these entries if this base is a virtual base. We
8734 compute the indices -- but do not add to the vtable -- when
8735 building the main vtable for a class. */
8736 if (binfo == TYPE_BINFO (vid->derived)
8737 || (BINFO_VIRTUAL_P (binfo)
8738 /* If BINFO is RTTI_BINFO, then (since BINFO does not
8739 correspond to VID->DERIVED), we are building a primary
8740 construction virtual table. Since this is a primary
8741 virtual table, we do not need the vcall offsets for
8742 BINFO. */
8743 && binfo != vid->rtti_binfo))
8745 /* We need a vcall offset for each of the virtual functions in this
8746 vtable. For example:
8748 class A { virtual void f (); };
8749 class B1 : virtual public A { virtual void f (); };
8750 class B2 : virtual public A { virtual void f (); };
8751 class C: public B1, public B2 { virtual void f (); };
8753 A C object has a primary base of B1, which has a primary base of A. A
8754 C also has a secondary base of B2, which no longer has a primary base
8755 of A. So the B2-in-C construction vtable needs a secondary vtable for
8756 A, which will adjust the A* to a B2* to call f. We have no way of
8757 knowing what (or even whether) this offset will be when we define B2,
8758 so we store this "vcall offset" in the A sub-vtable and look it up in
8759 a "virtual thunk" for B2::f.
8761 We need entries for all the functions in our primary vtable and
8762 in our non-virtual bases' secondary vtables. */
8763 vid->vbase = binfo;
8764 /* If we are just computing the vcall indices -- but do not need
8765 the actual entries -- not that. */
8766 if (!BINFO_VIRTUAL_P (binfo))
8767 vid->generate_vcall_entries = false;
8768 /* Now, walk through the non-virtual bases, adding vcall offsets. */
8769 add_vcall_offset_vtbl_entries_r (binfo, vid);
8773 /* Build vcall offsets, starting with those for BINFO. */
8775 static void
8776 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
8778 int i;
8779 tree primary_binfo;
8780 tree base_binfo;
8782 /* Don't walk into virtual bases -- except, of course, for the
8783 virtual base for which we are building vcall offsets. Any
8784 primary virtual base will have already had its offsets generated
8785 through the recursion in build_vcall_and_vbase_vtbl_entries. */
8786 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
8787 return;
8789 /* If BINFO has a primary base, process it first. */
8790 primary_binfo = get_primary_binfo (binfo);
8791 if (primary_binfo)
8792 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
8794 /* Add BINFO itself to the list. */
8795 add_vcall_offset_vtbl_entries_1 (binfo, vid);
8797 /* Scan the non-primary bases of BINFO. */
8798 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8799 if (base_binfo != primary_binfo)
8800 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
8803 /* Called from build_vcall_offset_vtbl_entries_r. */
8805 static void
8806 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
8808 /* Make entries for the rest of the virtuals. */
8809 if (abi_version_at_least (2))
8811 tree orig_fn;
8813 /* The ABI requires that the methods be processed in declaration
8814 order. G++ 3.2 used the order in the vtable. */
8815 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
8816 orig_fn;
8817 orig_fn = DECL_CHAIN (orig_fn))
8818 if (DECL_VINDEX (orig_fn))
8819 add_vcall_offset (orig_fn, binfo, vid);
8821 else
8823 tree derived_virtuals;
8824 tree base_virtuals;
8825 tree orig_virtuals;
8826 /* If BINFO is a primary base, the most derived class which has
8827 BINFO as a primary base; otherwise, just BINFO. */
8828 tree non_primary_binfo;
8830 /* We might be a primary base class. Go up the inheritance hierarchy
8831 until we find the most derived class of which we are a primary base:
8832 it is the BINFO_VIRTUALS there that we need to consider. */
8833 non_primary_binfo = binfo;
8834 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8836 tree b;
8838 /* If we have reached a virtual base, then it must be vid->vbase,
8839 because we ignore other virtual bases in
8840 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
8841 base (possibly multi-level) of vid->binfo, or we wouldn't
8842 have called build_vcall_and_vbase_vtbl_entries for it. But it
8843 might be a lost primary, so just skip down to vid->binfo. */
8844 if (BINFO_VIRTUAL_P (non_primary_binfo))
8846 gcc_assert (non_primary_binfo == vid->vbase);
8847 non_primary_binfo = vid->binfo;
8848 break;
8851 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8852 if (get_primary_binfo (b) != non_primary_binfo)
8853 break;
8854 non_primary_binfo = b;
8857 if (vid->ctor_vtbl_p)
8858 /* For a ctor vtable we need the equivalent binfo within the hierarchy
8859 where rtti_binfo is the most derived type. */
8860 non_primary_binfo
8861 = original_binfo (non_primary_binfo, vid->rtti_binfo);
8863 for (base_virtuals = BINFO_VIRTUALS (binfo),
8864 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
8865 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
8866 base_virtuals;
8867 base_virtuals = TREE_CHAIN (base_virtuals),
8868 derived_virtuals = TREE_CHAIN (derived_virtuals),
8869 orig_virtuals = TREE_CHAIN (orig_virtuals))
8871 tree orig_fn;
8873 /* Find the declaration that originally caused this function to
8874 be present in BINFO_TYPE (binfo). */
8875 orig_fn = BV_FN (orig_virtuals);
8877 /* When processing BINFO, we only want to generate vcall slots for
8878 function slots introduced in BINFO. So don't try to generate
8879 one if the function isn't even defined in BINFO. */
8880 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), DECL_CONTEXT (orig_fn)))
8881 continue;
8883 add_vcall_offset (orig_fn, binfo, vid);
8888 /* Add a vcall offset entry for ORIG_FN to the vtable. */
8890 static void
8891 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
8893 size_t i;
8894 tree vcall_offset;
8895 tree derived_entry;
8897 /* If there is already an entry for a function with the same
8898 signature as FN, then we do not need a second vcall offset.
8899 Check the list of functions already present in the derived
8900 class vtable. */
8901 FOR_EACH_VEC_ELT (tree, vid->fns, i, derived_entry)
8903 if (same_signature_p (derived_entry, orig_fn)
8904 /* We only use one vcall offset for virtual destructors,
8905 even though there are two virtual table entries. */
8906 || (DECL_DESTRUCTOR_P (derived_entry)
8907 && DECL_DESTRUCTOR_P (orig_fn)))
8908 return;
8911 /* If we are building these vcall offsets as part of building
8912 the vtable for the most derived class, remember the vcall
8913 offset. */
8914 if (vid->binfo == TYPE_BINFO (vid->derived))
8916 tree_pair_s elt = {orig_fn, vid->index};
8917 VEC_safe_push (tree_pair_s, gc, CLASSTYPE_VCALL_INDICES (vid->derived),
8918 elt);
8921 /* The next vcall offset will be found at a more negative
8922 offset. */
8923 vid->index = size_binop (MINUS_EXPR, vid->index,
8924 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
8926 /* Keep track of this function. */
8927 VEC_safe_push (tree, gc, vid->fns, orig_fn);
8929 if (vid->generate_vcall_entries)
8931 tree base;
8932 tree fn;
8934 /* Find the overriding function. */
8935 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
8936 if (fn == error_mark_node)
8937 vcall_offset = build_zero_cst (vtable_entry_type);
8938 else
8940 base = TREE_VALUE (fn);
8942 /* The vbase we're working on is a primary base of
8943 vid->binfo. But it might be a lost primary, so its
8944 BINFO_OFFSET might be wrong, so we just use the
8945 BINFO_OFFSET from vid->binfo. */
8946 vcall_offset = size_diffop_loc (input_location,
8947 BINFO_OFFSET (base),
8948 BINFO_OFFSET (vid->binfo));
8949 vcall_offset = fold_build1_loc (input_location,
8950 NOP_EXPR, vtable_entry_type,
8951 vcall_offset);
8953 /* Add the initializer to the vtable. */
8954 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
8958 /* Return vtbl initializers for the RTTI entries corresponding to the
8959 BINFO's vtable. The RTTI entries should indicate the object given
8960 by VID->rtti_binfo. */
8962 static void
8963 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
8965 tree b;
8966 tree t;
8967 tree offset;
8968 tree decl;
8969 tree init;
8971 t = BINFO_TYPE (vid->rtti_binfo);
8973 /* To find the complete object, we will first convert to our most
8974 primary base, and then add the offset in the vtbl to that value. */
8975 b = binfo;
8976 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8977 && !BINFO_LOST_PRIMARY_P (b))
8979 tree primary_base;
8981 primary_base = get_primary_binfo (b);
8982 gcc_assert (BINFO_PRIMARY_P (primary_base)
8983 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8984 b = primary_base;
8986 offset = size_diffop_loc (input_location,
8987 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8989 /* The second entry is the address of the typeinfo object. */
8990 if (flag_rtti)
8991 decl = build_address (get_tinfo_decl (t));
8992 else
8993 decl = integer_zero_node;
8995 /* Convert the declaration to a type that can be stored in the
8996 vtable. */
8997 init = build_nop (vfunc_ptr_type_node, decl);
8998 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9000 /* Add the offset-to-top entry. It comes earlier in the vtable than
9001 the typeinfo entry. Convert the offset to look like a
9002 function pointer, so that we can put it in the vtable. */
9003 init = build_nop (vfunc_ptr_type_node, offset);
9004 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9007 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9008 accessibility. */
9010 bool
9011 uniquely_derived_from_p (tree parent, tree type)
9013 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9014 return base && base != error_mark_node;
9017 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9019 bool
9020 publicly_uniquely_derived_p (tree parent, tree type)
9022 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9023 NULL, tf_none);
9024 return base && base != error_mark_node;
9027 #include "gt-cp-class.h"