2013-06-20 Andrew Sutton <andrew.n.sutton@gmail.com>
[official-gcc.git] / gcc / cp / class.c
blob31cd1c701080d6d85a27291f0fba1dfd55462c21
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* High-level class interface. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "flags.h"
31 #include "toplev.h"
32 #include "target.h"
33 #include "convert.h"
34 #include "cgraph.h"
35 #include "dumpfile.h"
36 #include "splay-tree.h"
37 #include "pointer-set.h"
38 #include "hash-table.h"
40 /* The number of nested classes being processed. If we are not in the
41 scope of any class, this is zero. */
43 int current_class_depth;
45 /* In order to deal with nested classes, we keep a stack of classes.
46 The topmost entry is the innermost class, and is the entry at index
47 CURRENT_CLASS_DEPTH */
49 typedef struct class_stack_node {
50 /* The name of the class. */
51 tree name;
53 /* The _TYPE node for the class. */
54 tree type;
56 /* The access specifier pending for new declarations in the scope of
57 this class. */
58 tree access;
60 /* If were defining TYPE, the names used in this class. */
61 splay_tree names_used;
63 /* Nonzero if this class is no longer open, because of a call to
64 push_to_top_level. */
65 size_t hidden;
66 }* class_stack_node_t;
68 typedef struct vtbl_init_data_s
70 /* The base for which we're building initializers. */
71 tree binfo;
72 /* The type of the most-derived type. */
73 tree derived;
74 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
75 unless ctor_vtbl_p is true. */
76 tree rtti_binfo;
77 /* The negative-index vtable initializers built up so far. These
78 are in order from least negative index to most negative index. */
79 vec<constructor_elt, va_gc> *inits;
80 /* The binfo for the virtual base for which we're building
81 vcall offset initializers. */
82 tree vbase;
83 /* The functions in vbase for which we have already provided vcall
84 offsets. */
85 vec<tree, va_gc> *fns;
86 /* The vtable index of the next vcall or vbase offset. */
87 tree index;
88 /* Nonzero if we are building the initializer for the primary
89 vtable. */
90 int primary_vtbl_p;
91 /* Nonzero if we are building the initializer for a construction
92 vtable. */
93 int ctor_vtbl_p;
94 /* True when adding vcall offset entries to the vtable. False when
95 merely computing the indices. */
96 bool generate_vcall_entries;
97 } vtbl_init_data;
99 /* The type of a function passed to walk_subobject_offsets. */
100 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
102 /* The stack itself. This is a dynamically resized array. The
103 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
104 static int current_class_stack_size;
105 static class_stack_node_t current_class_stack;
107 /* The size of the largest empty class seen in this translation unit. */
108 static GTY (()) tree sizeof_biggest_empty_class;
110 /* An array of all local classes present in this translation unit, in
111 declaration order. */
112 vec<tree, va_gc> *local_classes;
114 static tree get_vfield_name (tree);
115 static void finish_struct_anon (tree);
116 static tree get_vtable_name (tree);
117 static tree get_basefndecls (tree, tree);
118 static int build_primary_vtable (tree, tree);
119 static int build_secondary_vtable (tree);
120 static void finish_vtbls (tree);
121 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
122 static void finish_struct_bits (tree);
123 static int alter_access (tree, tree, tree);
124 static void handle_using_decl (tree, tree);
125 static tree dfs_modify_vtables (tree, void *);
126 static tree modify_all_vtables (tree, tree);
127 static void determine_primary_bases (tree);
128 static void finish_struct_methods (tree);
129 static void maybe_warn_about_overly_private_class (tree);
130 static int method_name_cmp (const void *, const void *);
131 static int resort_method_name_cmp (const void *, const void *);
132 static void add_implicitly_declared_members (tree, tree*, int, int);
133 static tree fixed_type_or_null (tree, int *, int *);
134 static tree build_simple_base_path (tree expr, tree binfo);
135 static tree build_vtbl_ref_1 (tree, tree);
136 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
137 vec<constructor_elt, va_gc> **);
138 static int count_fields (tree);
139 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
140 static void insert_into_classtype_sorted_fields (tree, tree, int);
141 static bool check_bitfield_decl (tree);
142 static void check_field_decl (tree, tree, int *, int *, int *);
143 static void check_field_decls (tree, tree *, int *, int *);
144 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
145 static void build_base_fields (record_layout_info, splay_tree, tree *);
146 static void check_methods (tree);
147 static void remove_zero_width_bit_fields (tree);
148 static void check_bases (tree, int *, int *);
149 static void check_bases_and_members (tree);
150 static tree create_vtable_ptr (tree, tree *);
151 static void include_empty_classes (record_layout_info);
152 static void layout_class_type (tree, tree *);
153 static void propagate_binfo_offsets (tree, tree);
154 static void layout_virtual_bases (record_layout_info, splay_tree);
155 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
156 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
157 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
158 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
159 static void add_vcall_offset (tree, tree, vtbl_init_data *);
160 static void layout_vtable_decl (tree, int);
161 static tree dfs_find_final_overrider_pre (tree, void *);
162 static tree dfs_find_final_overrider_post (tree, void *);
163 static tree find_final_overrider (tree, tree, tree);
164 static int make_new_vtable (tree, tree);
165 static tree get_primary_binfo (tree);
166 static int maybe_indent_hierarchy (FILE *, int, int);
167 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
168 static void dump_class_hierarchy (tree);
169 static void dump_class_hierarchy_1 (FILE *, int, tree);
170 static void dump_array (FILE *, tree);
171 static void dump_vtable (tree, tree, tree);
172 static void dump_vtt (tree, tree);
173 static void dump_thunk (FILE *, int, tree);
174 static tree build_vtable (tree, tree, tree);
175 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
176 static void layout_nonempty_base_or_field (record_layout_info,
177 tree, tree, splay_tree);
178 static tree end_of_class (tree, int);
179 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
180 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
181 vec<constructor_elt, va_gc> **);
182 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
183 vec<constructor_elt, va_gc> **);
184 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
185 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
186 static void clone_constructors_and_destructors (tree);
187 static tree build_clone (tree, tree);
188 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
189 static void build_ctor_vtbl_group (tree, tree);
190 static void build_vtt (tree);
191 static tree binfo_ctor_vtable (tree);
192 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
193 tree *);
194 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
195 static tree dfs_fixup_binfo_vtbls (tree, void *);
196 static int record_subobject_offset (tree, tree, splay_tree);
197 static int check_subobject_offset (tree, tree, splay_tree);
198 static int walk_subobject_offsets (tree, subobject_offset_fn,
199 tree, splay_tree, tree, int);
200 static void record_subobject_offsets (tree, tree, splay_tree, bool);
201 static int layout_conflict_p (tree, tree, splay_tree, int);
202 static int splay_tree_compare_integer_csts (splay_tree_key k1,
203 splay_tree_key k2);
204 static void warn_about_ambiguous_bases (tree);
205 static bool type_requires_array_cookie (tree);
206 static bool contains_empty_class_p (tree);
207 static bool base_derived_from (tree, tree);
208 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
209 static tree end_of_base (tree);
210 static tree get_vcall_index (tree, tree);
212 /* Variables shared between class.c and call.c. */
214 int n_vtables = 0;
215 int n_vtable_entries = 0;
216 int n_vtable_searches = 0;
217 int n_vtable_elems = 0;
218 int n_convert_harshness = 0;
219 int n_compute_conversion_costs = 0;
220 int n_inner_fields_searched = 0;
222 /* Convert to or from a base subobject. EXPR is an expression of type
223 `A' or `A*', an expression of type `B' or `B*' is returned. To
224 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
225 the B base instance within A. To convert base A to derived B, CODE
226 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
227 In this latter case, A must not be a morally virtual base of B.
228 NONNULL is true if EXPR is known to be non-NULL (this is only
229 needed when EXPR is of pointer type). CV qualifiers are preserved
230 from EXPR. */
232 tree
233 build_base_path (enum tree_code code,
234 tree expr,
235 tree binfo,
236 int nonnull,
237 tsubst_flags_t complain)
239 tree v_binfo = NULL_TREE;
240 tree d_binfo = NULL_TREE;
241 tree probe;
242 tree offset;
243 tree target_type;
244 tree null_test = NULL;
245 tree ptr_target_type;
246 int fixed_type_p;
247 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
248 bool has_empty = false;
249 bool virtual_access;
251 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
252 return error_mark_node;
254 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
256 d_binfo = probe;
257 if (is_empty_class (BINFO_TYPE (probe)))
258 has_empty = true;
259 if (!v_binfo && BINFO_VIRTUAL_P (probe))
260 v_binfo = probe;
263 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
264 if (want_pointer)
265 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
267 if (code == PLUS_EXPR
268 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
270 /* This can happen when adjust_result_of_qualified_name_lookup can't
271 find a unique base binfo in a call to a member function. We
272 couldn't give the diagnostic then since we might have been calling
273 a static member function, so we do it now. */
274 if (complain & tf_error)
276 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
277 ba_unique, NULL, complain);
278 gcc_assert (base == error_mark_node);
280 return error_mark_node;
283 gcc_assert ((code == MINUS_EXPR
284 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
285 || code == PLUS_EXPR);
287 if (binfo == d_binfo)
288 /* Nothing to do. */
289 return expr;
291 if (code == MINUS_EXPR && v_binfo)
293 if (complain & tf_error)
294 error ("cannot convert from base %qT to derived type %qT via "
295 "virtual base %qT", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
296 BINFO_TYPE (v_binfo));
297 return error_mark_node;
300 if (!want_pointer)
301 /* This must happen before the call to save_expr. */
302 expr = cp_build_addr_expr (expr, complain);
303 else
304 expr = mark_rvalue_use (expr);
306 offset = BINFO_OFFSET (binfo);
307 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
308 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
309 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
310 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
311 expression returned matches the input. */
312 target_type = cp_build_qualified_type
313 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
314 ptr_target_type = build_pointer_type (target_type);
316 /* Do we need to look in the vtable for the real offset? */
317 virtual_access = (v_binfo && fixed_type_p <= 0);
319 /* Don't bother with the calculations inside sizeof; they'll ICE if the
320 source type is incomplete and the pointer value doesn't matter. In a
321 template (even in fold_non_dependent_expr), we don't have vtables set
322 up properly yet, and the value doesn't matter there either; we're just
323 interested in the result of overload resolution. */
324 if (cp_unevaluated_operand != 0
325 || in_template_function ())
327 expr = build_nop (ptr_target_type, expr);
328 if (!want_pointer)
329 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
330 return expr;
333 /* If we're in an NSDMI, we don't have the full constructor context yet
334 that we need for converting to a virtual base, so just build a stub
335 CONVERT_EXPR and expand it later in bot_replace. */
336 if (virtual_access && fixed_type_p < 0
337 && current_scope () != current_function_decl)
339 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
340 CONVERT_EXPR_VBASE_PATH (expr) = true;
341 if (!want_pointer)
342 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
343 return expr;
346 /* Do we need to check for a null pointer? */
347 if (want_pointer && !nonnull)
349 /* If we know the conversion will not actually change the value
350 of EXPR, then we can avoid testing the expression for NULL.
351 We have to avoid generating a COMPONENT_REF for a base class
352 field, because other parts of the compiler know that such
353 expressions are always non-NULL. */
354 if (!virtual_access && integer_zerop (offset))
355 return build_nop (ptr_target_type, expr);
356 null_test = error_mark_node;
359 /* Protect against multiple evaluation if necessary. */
360 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
361 expr = save_expr (expr);
363 /* Now that we've saved expr, build the real null test. */
364 if (null_test)
366 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
367 null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
368 expr, zero);
371 /* If this is a simple base reference, express it as a COMPONENT_REF. */
372 if (code == PLUS_EXPR && !virtual_access
373 /* We don't build base fields for empty bases, and they aren't very
374 interesting to the optimizers anyway. */
375 && !has_empty)
377 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
378 expr = build_simple_base_path (expr, binfo);
379 if (want_pointer)
380 expr = build_address (expr);
381 target_type = TREE_TYPE (expr);
382 goto out;
385 if (virtual_access)
387 /* Going via virtual base V_BINFO. We need the static offset
388 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
389 V_BINFO. That offset is an entry in D_BINFO's vtable. */
390 tree v_offset;
392 if (fixed_type_p < 0 && in_base_initializer)
394 /* In a base member initializer, we cannot rely on the
395 vtable being set up. We have to indirect via the
396 vtt_parm. */
397 tree t;
399 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
400 t = build_pointer_type (t);
401 v_offset = convert (t, current_vtt_parm);
402 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
404 else
405 v_offset = build_vfield_ref (cp_build_indirect_ref (expr, RO_NULL,
406 complain),
407 TREE_TYPE (TREE_TYPE (expr)));
409 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
410 v_offset = build1 (NOP_EXPR,
411 build_pointer_type (ptrdiff_type_node),
412 v_offset);
413 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
414 TREE_CONSTANT (v_offset) = 1;
416 offset = convert_to_integer (ptrdiff_type_node,
417 size_diffop_loc (input_location, offset,
418 BINFO_OFFSET (v_binfo)));
420 if (!integer_zerop (offset))
421 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
423 if (fixed_type_p < 0)
424 /* Negative fixed_type_p means this is a constructor or destructor;
425 virtual base layout is fixed in in-charge [cd]tors, but not in
426 base [cd]tors. */
427 offset = build3 (COND_EXPR, ptrdiff_type_node,
428 build2 (EQ_EXPR, boolean_type_node,
429 current_in_charge_parm, integer_zero_node),
430 v_offset,
431 convert_to_integer (ptrdiff_type_node,
432 BINFO_OFFSET (binfo)));
433 else
434 offset = v_offset;
437 if (want_pointer)
438 target_type = ptr_target_type;
440 expr = build1 (NOP_EXPR, ptr_target_type, expr);
442 if (!integer_zerop (offset))
444 offset = fold_convert (sizetype, offset);
445 if (code == MINUS_EXPR)
446 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
447 expr = fold_build_pointer_plus (expr, offset);
449 else
450 null_test = NULL;
452 if (!want_pointer)
453 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
455 out:
456 if (null_test)
457 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
458 build_zero_cst (target_type));
460 return expr;
463 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
464 Perform a derived-to-base conversion by recursively building up a
465 sequence of COMPONENT_REFs to the appropriate base fields. */
467 static tree
468 build_simple_base_path (tree expr, tree binfo)
470 tree type = BINFO_TYPE (binfo);
471 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
472 tree field;
474 if (d_binfo == NULL_TREE)
476 tree temp;
478 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
480 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
481 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
482 an lvalue in the front end; only _DECLs and _REFs are lvalues
483 in the back end. */
484 temp = unary_complex_lvalue (ADDR_EXPR, expr);
485 if (temp)
486 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
488 return expr;
491 /* Recurse. */
492 expr = build_simple_base_path (expr, d_binfo);
494 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
495 field; field = DECL_CHAIN (field))
496 /* Is this the base field created by build_base_field? */
497 if (TREE_CODE (field) == FIELD_DECL
498 && DECL_FIELD_IS_BASE (field)
499 && TREE_TYPE (field) == type
500 /* If we're looking for a field in the most-derived class,
501 also check the field offset; we can have two base fields
502 of the same type if one is an indirect virtual base and one
503 is a direct non-virtual base. */
504 && (BINFO_INHERITANCE_CHAIN (d_binfo)
505 || tree_int_cst_equal (byte_position (field),
506 BINFO_OFFSET (binfo))))
508 /* We don't use build_class_member_access_expr here, as that
509 has unnecessary checks, and more importantly results in
510 recursive calls to dfs_walk_once. */
511 int type_quals = cp_type_quals (TREE_TYPE (expr));
513 expr = build3 (COMPONENT_REF,
514 cp_build_qualified_type (type, type_quals),
515 expr, field, NULL_TREE);
516 expr = fold_if_not_in_template (expr);
518 /* Mark the expression const or volatile, as appropriate.
519 Even though we've dealt with the type above, we still have
520 to mark the expression itself. */
521 if (type_quals & TYPE_QUAL_CONST)
522 TREE_READONLY (expr) = 1;
523 if (type_quals & TYPE_QUAL_VOLATILE)
524 TREE_THIS_VOLATILE (expr) = 1;
526 return expr;
529 /* Didn't find the base field?!? */
530 gcc_unreachable ();
533 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
534 type is a class type or a pointer to a class type. In the former
535 case, TYPE is also a class type; in the latter it is another
536 pointer type. If CHECK_ACCESS is true, an error message is emitted
537 if TYPE is inaccessible. If OBJECT has pointer type, the value is
538 assumed to be non-NULL. */
540 tree
541 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
542 tsubst_flags_t complain)
544 tree binfo;
545 tree object_type;
547 if (TYPE_PTR_P (TREE_TYPE (object)))
549 object_type = TREE_TYPE (TREE_TYPE (object));
550 type = TREE_TYPE (type);
552 else
553 object_type = TREE_TYPE (object);
555 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
556 NULL, complain);
557 if (!binfo || binfo == error_mark_node)
558 return error_mark_node;
560 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
563 /* EXPR is an expression with unqualified class type. BASE is a base
564 binfo of that class type. Returns EXPR, converted to the BASE
565 type. This function assumes that EXPR is the most derived class;
566 therefore virtual bases can be found at their static offsets. */
568 tree
569 convert_to_base_statically (tree expr, tree base)
571 tree expr_type;
573 expr_type = TREE_TYPE (expr);
574 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
576 /* If this is a non-empty base, use a COMPONENT_REF. */
577 if (!is_empty_class (BINFO_TYPE (base)))
578 return build_simple_base_path (expr, base);
580 /* We use fold_build2 and fold_convert below to simplify the trees
581 provided to the optimizers. It is not safe to call these functions
582 when processing a template because they do not handle C++-specific
583 trees. */
584 gcc_assert (!processing_template_decl);
585 expr = cp_build_addr_expr (expr, tf_warning_or_error);
586 if (!integer_zerop (BINFO_OFFSET (base)))
587 expr = fold_build_pointer_plus_loc (input_location,
588 expr, BINFO_OFFSET (base));
589 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
590 expr = build_fold_indirect_ref_loc (input_location, expr);
593 return expr;
597 tree
598 build_vfield_ref (tree datum, tree type)
600 tree vfield, vcontext;
602 if (datum == error_mark_node)
603 return error_mark_node;
605 /* First, convert to the requested type. */
606 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
607 datum = convert_to_base (datum, type, /*check_access=*/false,
608 /*nonnull=*/true, tf_warning_or_error);
610 /* Second, the requested type may not be the owner of its own vptr.
611 If not, convert to the base class that owns it. We cannot use
612 convert_to_base here, because VCONTEXT may appear more than once
613 in the inheritance hierarchy of TYPE, and thus direct conversion
614 between the types may be ambiguous. Following the path back up
615 one step at a time via primary bases avoids the problem. */
616 vfield = TYPE_VFIELD (type);
617 vcontext = DECL_CONTEXT (vfield);
618 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
620 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
621 type = TREE_TYPE (datum);
624 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
627 /* Given an object INSTANCE, return an expression which yields the
628 vtable element corresponding to INDEX. There are many special
629 cases for INSTANCE which we take care of here, mainly to avoid
630 creating extra tree nodes when we don't have to. */
632 static tree
633 build_vtbl_ref_1 (tree instance, tree idx)
635 tree aref;
636 tree vtbl = NULL_TREE;
638 /* Try to figure out what a reference refers to, and
639 access its virtual function table directly. */
641 int cdtorp = 0;
642 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
644 tree basetype = non_reference (TREE_TYPE (instance));
646 if (fixed_type && !cdtorp)
648 tree binfo = lookup_base (fixed_type, basetype,
649 ba_unique, NULL, tf_none);
650 if (binfo && binfo != error_mark_node)
651 vtbl = unshare_expr (BINFO_VTABLE (binfo));
654 if (!vtbl)
655 vtbl = build_vfield_ref (instance, basetype);
657 aref = build_array_ref (input_location, vtbl, idx);
658 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
660 return aref;
663 tree
664 build_vtbl_ref (tree instance, tree idx)
666 tree aref = build_vtbl_ref_1 (instance, idx);
668 return aref;
671 /* Given a stable object pointer INSTANCE_PTR, return an expression which
672 yields a function pointer corresponding to vtable element INDEX. */
674 tree
675 build_vfn_ref (tree instance_ptr, tree idx)
677 tree aref;
679 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
680 tf_warning_or_error),
681 idx);
683 /* When using function descriptors, the address of the
684 vtable entry is treated as a function pointer. */
685 if (TARGET_VTABLE_USES_DESCRIPTORS)
686 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
687 cp_build_addr_expr (aref, tf_warning_or_error));
689 /* Remember this as a method reference, for later devirtualization. */
690 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
692 return aref;
695 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
696 for the given TYPE. */
698 static tree
699 get_vtable_name (tree type)
701 return mangle_vtbl_for_type (type);
704 /* DECL is an entity associated with TYPE, like a virtual table or an
705 implicitly generated constructor. Determine whether or not DECL
706 should have external or internal linkage at the object file
707 level. This routine does not deal with COMDAT linkage and other
708 similar complexities; it simply sets TREE_PUBLIC if it possible for
709 entities in other translation units to contain copies of DECL, in
710 the abstract. */
712 void
713 set_linkage_according_to_type (tree /*type*/, tree decl)
715 TREE_PUBLIC (decl) = 1;
716 determine_visibility (decl);
719 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
720 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
721 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
723 static tree
724 build_vtable (tree class_type, tree name, tree vtable_type)
726 tree decl;
728 decl = build_lang_decl (VAR_DECL, name, vtable_type);
729 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
730 now to avoid confusion in mangle_decl. */
731 SET_DECL_ASSEMBLER_NAME (decl, name);
732 DECL_CONTEXT (decl) = class_type;
733 DECL_ARTIFICIAL (decl) = 1;
734 TREE_STATIC (decl) = 1;
735 TREE_READONLY (decl) = 1;
736 DECL_VIRTUAL_P (decl) = 1;
737 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
738 DECL_VTABLE_OR_VTT_P (decl) = 1;
739 /* At one time the vtable info was grabbed 2 words at a time. This
740 fails on sparc unless you have 8-byte alignment. (tiemann) */
741 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
742 DECL_ALIGN (decl));
743 set_linkage_according_to_type (class_type, decl);
744 /* The vtable has not been defined -- yet. */
745 DECL_EXTERNAL (decl) = 1;
746 DECL_NOT_REALLY_EXTERN (decl) = 1;
748 /* Mark the VAR_DECL node representing the vtable itself as a
749 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
750 is rather important that such things be ignored because any
751 effort to actually generate DWARF for them will run into
752 trouble when/if we encounter code like:
754 #pragma interface
755 struct S { virtual void member (); };
757 because the artificial declaration of the vtable itself (as
758 manufactured by the g++ front end) will say that the vtable is
759 a static member of `S' but only *after* the debug output for
760 the definition of `S' has already been output. This causes
761 grief because the DWARF entry for the definition of the vtable
762 will try to refer back to an earlier *declaration* of the
763 vtable as a static member of `S' and there won't be one. We
764 might be able to arrange to have the "vtable static member"
765 attached to the member list for `S' before the debug info for
766 `S' get written (which would solve the problem) but that would
767 require more intrusive changes to the g++ front end. */
768 DECL_IGNORED_P (decl) = 1;
770 return decl;
773 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
774 or even complete. If this does not exist, create it. If COMPLETE is
775 nonzero, then complete the definition of it -- that will render it
776 impossible to actually build the vtable, but is useful to get at those
777 which are known to exist in the runtime. */
779 tree
780 get_vtable_decl (tree type, int complete)
782 tree decl;
784 if (CLASSTYPE_VTABLES (type))
785 return CLASSTYPE_VTABLES (type);
787 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
788 CLASSTYPE_VTABLES (type) = decl;
790 if (complete)
792 DECL_EXTERNAL (decl) = 1;
793 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
796 return decl;
799 /* Build the primary virtual function table for TYPE. If BINFO is
800 non-NULL, build the vtable starting with the initial approximation
801 that it is the same as the one which is the head of the association
802 list. Returns a nonzero value if a new vtable is actually
803 created. */
805 static int
806 build_primary_vtable (tree binfo, tree type)
808 tree decl;
809 tree virtuals;
811 decl = get_vtable_decl (type, /*complete=*/0);
813 if (binfo)
815 if (BINFO_NEW_VTABLE_MARKED (binfo))
816 /* We have already created a vtable for this base, so there's
817 no need to do it again. */
818 return 0;
820 virtuals = copy_list (BINFO_VIRTUALS (binfo));
821 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
822 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
823 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
825 else
827 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
828 virtuals = NULL_TREE;
831 if (GATHER_STATISTICS)
833 n_vtables += 1;
834 n_vtable_elems += list_length (virtuals);
837 /* Initialize the association list for this type, based
838 on our first approximation. */
839 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
840 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
841 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
842 return 1;
845 /* Give BINFO a new virtual function table which is initialized
846 with a skeleton-copy of its original initialization. The only
847 entry that changes is the `delta' entry, so we can really
848 share a lot of structure.
850 FOR_TYPE is the most derived type which caused this table to
851 be needed.
853 Returns nonzero if we haven't met BINFO before.
855 The order in which vtables are built (by calling this function) for
856 an object must remain the same, otherwise a binary incompatibility
857 can result. */
859 static int
860 build_secondary_vtable (tree binfo)
862 if (BINFO_NEW_VTABLE_MARKED (binfo))
863 /* We already created a vtable for this base. There's no need to
864 do it again. */
865 return 0;
867 /* Remember that we've created a vtable for this BINFO, so that we
868 don't try to do so again. */
869 SET_BINFO_NEW_VTABLE_MARKED (binfo);
871 /* Make fresh virtual list, so we can smash it later. */
872 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
874 /* Secondary vtables are laid out as part of the same structure as
875 the primary vtable. */
876 BINFO_VTABLE (binfo) = NULL_TREE;
877 return 1;
880 /* Create a new vtable for BINFO which is the hierarchy dominated by
881 T. Return nonzero if we actually created a new vtable. */
883 static int
884 make_new_vtable (tree t, tree binfo)
886 if (binfo == TYPE_BINFO (t))
887 /* In this case, it is *type*'s vtable we are modifying. We start
888 with the approximation that its vtable is that of the
889 immediate base class. */
890 return build_primary_vtable (binfo, t);
891 else
892 /* This is our very own copy of `basetype' to play with. Later,
893 we will fill in all the virtual functions that override the
894 virtual functions in these base classes which are not defined
895 by the current type. */
896 return build_secondary_vtable (binfo);
899 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
900 (which is in the hierarchy dominated by T) list FNDECL as its
901 BV_FN. DELTA is the required constant adjustment from the `this'
902 pointer where the vtable entry appears to the `this' required when
903 the function is actually called. */
905 static void
906 modify_vtable_entry (tree t,
907 tree binfo,
908 tree fndecl,
909 tree delta,
910 tree *virtuals)
912 tree v;
914 v = *virtuals;
916 if (fndecl != BV_FN (v)
917 || !tree_int_cst_equal (delta, BV_DELTA (v)))
919 /* We need a new vtable for BINFO. */
920 if (make_new_vtable (t, binfo))
922 /* If we really did make a new vtable, we also made a copy
923 of the BINFO_VIRTUALS list. Now, we have to find the
924 corresponding entry in that list. */
925 *virtuals = BINFO_VIRTUALS (binfo);
926 while (BV_FN (*virtuals) != BV_FN (v))
927 *virtuals = TREE_CHAIN (*virtuals);
928 v = *virtuals;
931 BV_DELTA (v) = delta;
932 BV_VCALL_INDEX (v) = NULL_TREE;
933 BV_FN (v) = fndecl;
937 // Returns true if NEW_FN and OLD_FN are non-template member functions
938 // of a class template with with different constraints. The types of the
939 // functions are assumed to be equivalent.
940 static inline bool
941 are_constrained_member_overloads (tree new_fn, tree old_fn)
943 // Non-temploids cannot be constrained.
944 if (!DECL_TEMPLOID_INSTANTIATION (new_fn)
945 && !DECL_TEMPLOID_INSTANTIATION (old_fn))
946 return false;
947 else
948 return !equivalently_constrained (new_fn, old_fn);
952 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
953 the USING_DECL naming METHOD. Returns true if the method could be
954 added to the method vec. */
956 bool
957 add_method (tree type, tree method, tree using_decl)
959 unsigned slot;
960 tree overload;
961 bool template_conv_p = false;
962 bool conv_p;
963 vec<tree, va_gc> *method_vec;
964 bool complete_p;
965 bool insert_p = false;
966 tree current_fns;
967 tree fns;
969 if (method == error_mark_node)
970 return false;
972 complete_p = COMPLETE_TYPE_P (type);
973 conv_p = DECL_CONV_FN_P (method);
974 if (conv_p)
975 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
976 && DECL_TEMPLATE_CONV_FN_P (method));
978 method_vec = CLASSTYPE_METHOD_VEC (type);
979 if (!method_vec)
981 /* Make a new method vector. We start with 8 entries. We must
982 allocate at least two (for constructors and destructors), and
983 we're going to end up with an assignment operator at some
984 point as well. */
985 vec_alloc (method_vec, 8);
986 /* Create slots for constructors and destructors. */
987 method_vec->quick_push (NULL_TREE);
988 method_vec->quick_push (NULL_TREE);
989 CLASSTYPE_METHOD_VEC (type) = method_vec;
992 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
993 grok_special_member_properties (method);
995 /* Constructors and destructors go in special slots. */
996 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
997 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
998 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1000 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1002 if (TYPE_FOR_JAVA (type))
1004 if (!DECL_ARTIFICIAL (method))
1005 error ("Java class %qT cannot have a destructor", type);
1006 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1007 error ("Java class %qT cannot have an implicit non-trivial "
1008 "destructor",
1009 type);
1012 else
1014 tree m;
1016 insert_p = true;
1017 /* See if we already have an entry with this name. */
1018 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1019 vec_safe_iterate (method_vec, slot, &m);
1020 ++slot)
1022 m = OVL_CURRENT (m);
1023 if (template_conv_p)
1025 if (TREE_CODE (m) == TEMPLATE_DECL
1026 && DECL_TEMPLATE_CONV_FN_P (m))
1027 insert_p = false;
1028 break;
1030 if (conv_p && !DECL_CONV_FN_P (m))
1031 break;
1032 if (DECL_NAME (m) == DECL_NAME (method))
1034 insert_p = false;
1035 break;
1037 if (complete_p
1038 && !DECL_CONV_FN_P (m)
1039 && DECL_NAME (m) > DECL_NAME (method))
1040 break;
1043 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1045 /* Check to see if we've already got this method. */
1046 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1048 tree fn = OVL_CURRENT (fns);
1049 tree fn_type;
1050 tree method_type;
1051 tree parms1;
1052 tree parms2;
1054 if (TREE_CODE (fn) != TREE_CODE (method))
1055 continue;
1057 /* [over.load] Member function declarations with the
1058 same name and the same parameter types cannot be
1059 overloaded if any of them is a static member
1060 function declaration.
1062 [over.load] Member function declarations with the same name and
1063 the same parameter-type-list as well as member function template
1064 declarations with the same name, the same parameter-type-list, and
1065 the same template parameter lists cannot be overloaded if any of
1066 them, but not all, have a ref-qualifier.
1068 [namespace.udecl] When a using-declaration brings names
1069 from a base class into a derived class scope, member
1070 functions in the derived class override and/or hide member
1071 functions with the same name and parameter types in a base
1072 class (rather than conflicting). */
1073 fn_type = TREE_TYPE (fn);
1074 method_type = TREE_TYPE (method);
1075 parms1 = TYPE_ARG_TYPES (fn_type);
1076 parms2 = TYPE_ARG_TYPES (method_type);
1078 /* Compare the quals on the 'this' parm. Don't compare
1079 the whole types, as used functions are treated as
1080 coming from the using class in overload resolution. */
1081 if (! DECL_STATIC_FUNCTION_P (fn)
1082 && ! DECL_STATIC_FUNCTION_P (method)
1083 /* Either both or neither need to be ref-qualified for
1084 differing quals to allow overloading. */
1085 && (FUNCTION_REF_QUALIFIED (fn_type)
1086 == FUNCTION_REF_QUALIFIED (method_type))
1087 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1088 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1089 continue;
1091 /* For templates, the return type and template parameters
1092 must be identical. */
1093 if (TREE_CODE (fn) == TEMPLATE_DECL
1094 && (!same_type_p (TREE_TYPE (fn_type),
1095 TREE_TYPE (method_type))
1096 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1097 DECL_TEMPLATE_PARMS (method))))
1098 continue;
1100 if (! DECL_STATIC_FUNCTION_P (fn))
1101 parms1 = TREE_CHAIN (parms1);
1102 if (! DECL_STATIC_FUNCTION_P (method))
1103 parms2 = TREE_CHAIN (parms2);
1105 if (compparms (parms1, parms2)
1106 && (!DECL_CONV_FN_P (fn)
1107 || same_type_p (TREE_TYPE (fn_type),
1108 TREE_TYPE (method_type))))
1110 /* For function versions, their parms and types match
1111 but they are not duplicates. Record function versions
1112 as and when they are found. extern "C" functions are
1113 not treated as versions. */
1114 if (TREE_CODE (fn) == FUNCTION_DECL
1115 && TREE_CODE (method) == FUNCTION_DECL
1116 && !DECL_EXTERN_C_P (fn)
1117 && !DECL_EXTERN_C_P (method)
1118 && targetm.target_option.function_versions (fn, method))
1120 /* Mark functions as versions if necessary. Modify the mangled
1121 decl name if necessary. */
1122 if (!DECL_FUNCTION_VERSIONED (fn))
1124 DECL_FUNCTION_VERSIONED (fn) = 1;
1125 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1126 mangle_decl (fn);
1128 if (!DECL_FUNCTION_VERSIONED (method))
1130 DECL_FUNCTION_VERSIONED (method) = 1;
1131 if (DECL_ASSEMBLER_NAME_SET_P (method))
1132 mangle_decl (method);
1134 record_function_versions (fn, method);
1135 continue;
1137 if (DECL_INHERITED_CTOR_BASE (method))
1139 if (DECL_INHERITED_CTOR_BASE (fn))
1141 error_at (DECL_SOURCE_LOCATION (method),
1142 "%q#D inherited from %qT", method,
1143 DECL_INHERITED_CTOR_BASE (method));
1144 error_at (DECL_SOURCE_LOCATION (fn),
1145 "conflicts with version inherited from %qT",
1146 DECL_INHERITED_CTOR_BASE (fn));
1148 /* Otherwise defer to the other function. */
1149 return false;
1151 if (using_decl)
1153 if (DECL_CONTEXT (fn) == type)
1154 /* Defer to the local function. */
1155 return false;
1157 else if (are_constrained_member_overloads (fn, method))
1158 continue;
1159 else
1161 error ("%q+#D cannot be overloaded", method);
1162 error ("with %q+#D", fn);
1165 /* We don't call duplicate_decls here to merge the
1166 declarations because that will confuse things if the
1167 methods have inline definitions. In particular, we
1168 will crash while processing the definitions. */
1169 return false;
1173 /* A class should never have more than one destructor. */
1174 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1175 return false;
1177 /* Add the new binding. */
1178 if (using_decl)
1180 overload = ovl_cons (method, current_fns);
1181 OVL_USED (overload) = true;
1183 else
1184 overload = build_overload (method, current_fns);
1186 if (conv_p)
1187 TYPE_HAS_CONVERSION (type) = 1;
1188 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1189 push_class_level_binding (DECL_NAME (method), overload);
1191 if (insert_p)
1193 bool reallocated;
1195 /* We only expect to add few methods in the COMPLETE_P case, so
1196 just make room for one more method in that case. */
1197 if (complete_p)
1198 reallocated = vec_safe_reserve_exact (method_vec, 1);
1199 else
1200 reallocated = vec_safe_reserve (method_vec, 1);
1201 if (reallocated)
1202 CLASSTYPE_METHOD_VEC (type) = method_vec;
1203 if (slot == method_vec->length ())
1204 method_vec->quick_push (overload);
1205 else
1206 method_vec->quick_insert (slot, overload);
1208 else
1209 /* Replace the current slot. */
1210 (*method_vec)[slot] = overload;
1211 return true;
1214 /* Subroutines of finish_struct. */
1216 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1217 legit, otherwise return 0. */
1219 static int
1220 alter_access (tree t, tree fdecl, tree access)
1222 tree elem;
1224 if (!DECL_LANG_SPECIFIC (fdecl))
1225 retrofit_lang_decl (fdecl);
1227 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1229 elem = purpose_member (t, DECL_ACCESS (fdecl));
1230 if (elem)
1232 if (TREE_VALUE (elem) != access)
1234 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1235 error ("conflicting access specifications for method"
1236 " %q+D, ignored", TREE_TYPE (fdecl));
1237 else
1238 error ("conflicting access specifications for field %qE, ignored",
1239 DECL_NAME (fdecl));
1241 else
1243 /* They're changing the access to the same thing they changed
1244 it to before. That's OK. */
1248 else
1250 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1251 tf_warning_or_error);
1252 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1253 return 1;
1255 return 0;
1258 /* Process the USING_DECL, which is a member of T. */
1260 static void
1261 handle_using_decl (tree using_decl, tree t)
1263 tree decl = USING_DECL_DECLS (using_decl);
1264 tree name = DECL_NAME (using_decl);
1265 tree access
1266 = TREE_PRIVATE (using_decl) ? access_private_node
1267 : TREE_PROTECTED (using_decl) ? access_protected_node
1268 : access_public_node;
1269 tree flist = NULL_TREE;
1270 tree old_value;
1272 gcc_assert (!processing_template_decl && decl);
1274 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1275 tf_warning_or_error);
1276 if (old_value)
1278 if (is_overloaded_fn (old_value))
1279 old_value = OVL_CURRENT (old_value);
1281 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1282 /* OK */;
1283 else
1284 old_value = NULL_TREE;
1287 cp_emit_debug_info_for_using (decl, USING_DECL_SCOPE (using_decl));
1289 if (is_overloaded_fn (decl))
1290 flist = decl;
1292 if (! old_value)
1294 else if (is_overloaded_fn (old_value))
1296 if (flist)
1297 /* It's OK to use functions from a base when there are functions with
1298 the same name already present in the current class. */;
1299 else
1301 error ("%q+D invalid in %q#T", using_decl, t);
1302 error (" because of local method %q+#D with same name",
1303 OVL_CURRENT (old_value));
1304 return;
1307 else if (!DECL_ARTIFICIAL (old_value))
1309 error ("%q+D invalid in %q#T", using_decl, t);
1310 error (" because of local member %q+#D with same name", old_value);
1311 return;
1314 /* Make type T see field decl FDECL with access ACCESS. */
1315 if (flist)
1316 for (; flist; flist = OVL_NEXT (flist))
1318 add_method (t, OVL_CURRENT (flist), using_decl);
1319 alter_access (t, OVL_CURRENT (flist), access);
1321 else
1322 alter_access (t, decl, access);
1325 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1326 types with abi tags, add the corresponding identifiers to the VEC in
1327 *DATA and set IDENTIFIER_MARKED. */
1329 struct abi_tag_data
1331 tree t;
1332 tree subob;
1335 static tree
1336 find_abi_tags_r (tree *tp, int */*walk_subtrees*/, void *data)
1338 if (!OVERLOAD_TYPE_P (*tp))
1339 return NULL_TREE;
1341 if (tree attributes = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (*tp)))
1343 struct abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1344 for (tree list = TREE_VALUE (attributes); list;
1345 list = TREE_CHAIN (list))
1347 tree tag = TREE_VALUE (list);
1348 tree id = get_identifier (TREE_STRING_POINTER (tag));
1349 if (!IDENTIFIER_MARKED (id))
1351 if (TYPE_P (p->subob))
1353 warning (OPT_Wabi_tag, "%qT does not have the %E abi tag "
1354 "that base %qT has", p->t, tag, p->subob);
1355 inform (location_of (p->subob), "%qT declared here",
1356 p->subob);
1358 else
1360 warning (OPT_Wabi_tag, "%qT does not have the %E abi tag "
1361 "that %qT (used in the type of %qD) has",
1362 p->t, tag, *tp, p->subob);
1363 inform (location_of (p->subob), "%qD declared here",
1364 p->subob);
1365 inform (location_of (*tp), "%qT declared here", *tp);
1370 return NULL_TREE;
1373 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its (transitively
1374 complete) template arguments. */
1376 static void
1377 mark_type_abi_tags (tree t, bool val)
1379 tree attributes = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1380 if (attributes)
1382 for (tree list = TREE_VALUE (attributes); list;
1383 list = TREE_CHAIN (list))
1385 tree tag = TREE_VALUE (list);
1386 tree id = get_identifier (TREE_STRING_POINTER (tag));
1387 IDENTIFIER_MARKED (id) = val;
1391 /* Also mark ABI tags from template arguments. */
1392 if (CLASSTYPE_TEMPLATE_INFO (t))
1394 tree args = CLASSTYPE_TI_ARGS (t);
1395 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1397 tree level = TMPL_ARGS_LEVEL (args, i+1);
1398 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1400 tree arg = TREE_VEC_ELT (level, j);
1401 if (CLASS_TYPE_P (arg))
1402 mark_type_abi_tags (arg, val);
1408 /* Check that class T has all the abi tags that subobject SUBOB has, or
1409 warn if not. */
1411 static void
1412 check_abi_tags (tree t, tree subob)
1414 mark_type_abi_tags (t, true);
1416 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1417 struct abi_tag_data data = { t, subob };
1419 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1421 mark_type_abi_tags (t, false);
1424 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1425 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1426 properties of the bases. */
1428 static void
1429 check_bases (tree t,
1430 int* cant_have_const_ctor_p,
1431 int* no_const_asn_ref_p)
1433 int i;
1434 bool seen_non_virtual_nearly_empty_base_p = 0;
1435 int seen_tm_mask = 0;
1436 tree base_binfo;
1437 tree binfo;
1438 tree field = NULL_TREE;
1440 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1441 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1442 if (TREE_CODE (field) == FIELD_DECL)
1443 break;
1445 for (binfo = TYPE_BINFO (t), i = 0;
1446 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1448 tree basetype = TREE_TYPE (base_binfo);
1450 gcc_assert (COMPLETE_TYPE_P (basetype));
1452 if (CLASSTYPE_FINAL (basetype))
1453 error ("cannot derive from %<final%> base %qT in derived type %qT",
1454 basetype, t);
1456 /* If any base class is non-literal, so is the derived class. */
1457 if (!CLASSTYPE_LITERAL_P (basetype))
1458 CLASSTYPE_LITERAL_P (t) = false;
1460 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1461 here because the case of virtual functions but non-virtual
1462 dtor is handled in finish_struct_1. */
1463 if (!TYPE_POLYMORPHIC_P (basetype))
1464 warning (OPT_Weffc__,
1465 "base class %q#T has a non-virtual destructor", basetype);
1467 /* If the base class doesn't have copy constructors or
1468 assignment operators that take const references, then the
1469 derived class cannot have such a member automatically
1470 generated. */
1471 if (TYPE_HAS_COPY_CTOR (basetype)
1472 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1473 *cant_have_const_ctor_p = 1;
1474 if (TYPE_HAS_COPY_ASSIGN (basetype)
1475 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1476 *no_const_asn_ref_p = 1;
1478 if (BINFO_VIRTUAL_P (base_binfo))
1479 /* A virtual base does not effect nearly emptiness. */
1481 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1483 if (seen_non_virtual_nearly_empty_base_p)
1484 /* And if there is more than one nearly empty base, then the
1485 derived class is not nearly empty either. */
1486 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1487 else
1488 /* Remember we've seen one. */
1489 seen_non_virtual_nearly_empty_base_p = 1;
1491 else if (!is_empty_class (basetype))
1492 /* If the base class is not empty or nearly empty, then this
1493 class cannot be nearly empty. */
1494 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1496 /* A lot of properties from the bases also apply to the derived
1497 class. */
1498 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1499 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1500 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1501 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1502 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1503 || !TYPE_HAS_COPY_ASSIGN (basetype));
1504 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1505 || !TYPE_HAS_COPY_CTOR (basetype));
1506 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1507 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1508 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1509 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1510 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1511 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1512 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1513 || TYPE_HAS_COMPLEX_DFLT (basetype));
1515 /* A standard-layout class is a class that:
1517 * has no non-standard-layout base classes, */
1518 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1519 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1521 tree basefield;
1522 /* ...has no base classes of the same type as the first non-static
1523 data member... */
1524 if (field && DECL_CONTEXT (field) == t
1525 && (same_type_ignoring_top_level_qualifiers_p
1526 (TREE_TYPE (field), basetype)))
1527 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1528 else
1529 /* ...either has no non-static data members in the most-derived
1530 class and at most one base class with non-static data
1531 members, or has no base classes with non-static data
1532 members */
1533 for (basefield = TYPE_FIELDS (basetype); basefield;
1534 basefield = DECL_CHAIN (basefield))
1535 if (TREE_CODE (basefield) == FIELD_DECL)
1537 if (field)
1538 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1539 else
1540 field = basefield;
1541 break;
1545 /* Don't bother collecting tm attributes if transactional memory
1546 support is not enabled. */
1547 if (flag_tm)
1549 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1550 if (tm_attr)
1551 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1554 check_abi_tags (t, basetype);
1557 /* If one of the base classes had TM attributes, and the current class
1558 doesn't define its own, then the current class inherits one. */
1559 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1561 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1562 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1566 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1567 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1568 that have had a nearly-empty virtual primary base stolen by some
1569 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1570 T. */
1572 static void
1573 determine_primary_bases (tree t)
1575 unsigned i;
1576 tree primary = NULL_TREE;
1577 tree type_binfo = TYPE_BINFO (t);
1578 tree base_binfo;
1580 /* Determine the primary bases of our bases. */
1581 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1582 base_binfo = TREE_CHAIN (base_binfo))
1584 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1586 /* See if we're the non-virtual primary of our inheritance
1587 chain. */
1588 if (!BINFO_VIRTUAL_P (base_binfo))
1590 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1591 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1593 if (parent_primary
1594 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1595 BINFO_TYPE (parent_primary)))
1596 /* We are the primary binfo. */
1597 BINFO_PRIMARY_P (base_binfo) = 1;
1599 /* Determine if we have a virtual primary base, and mark it so.
1601 if (primary && BINFO_VIRTUAL_P (primary))
1603 tree this_primary = copied_binfo (primary, base_binfo);
1605 if (BINFO_PRIMARY_P (this_primary))
1606 /* Someone already claimed this base. */
1607 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1608 else
1610 tree delta;
1612 BINFO_PRIMARY_P (this_primary) = 1;
1613 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1615 /* A virtual binfo might have been copied from within
1616 another hierarchy. As we're about to use it as a
1617 primary base, make sure the offsets match. */
1618 delta = size_diffop_loc (input_location,
1619 convert (ssizetype,
1620 BINFO_OFFSET (base_binfo)),
1621 convert (ssizetype,
1622 BINFO_OFFSET (this_primary)));
1624 propagate_binfo_offsets (this_primary, delta);
1629 /* First look for a dynamic direct non-virtual base. */
1630 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1632 tree basetype = BINFO_TYPE (base_binfo);
1634 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1636 primary = base_binfo;
1637 goto found;
1641 /* A "nearly-empty" virtual base class can be the primary base
1642 class, if no non-virtual polymorphic base can be found. Look for
1643 a nearly-empty virtual dynamic base that is not already a primary
1644 base of something in the hierarchy. If there is no such base,
1645 just pick the first nearly-empty virtual base. */
1647 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1648 base_binfo = TREE_CHAIN (base_binfo))
1649 if (BINFO_VIRTUAL_P (base_binfo)
1650 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1652 if (!BINFO_PRIMARY_P (base_binfo))
1654 /* Found one that is not primary. */
1655 primary = base_binfo;
1656 goto found;
1658 else if (!primary)
1659 /* Remember the first candidate. */
1660 primary = base_binfo;
1663 found:
1664 /* If we've got a primary base, use it. */
1665 if (primary)
1667 tree basetype = BINFO_TYPE (primary);
1669 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1670 if (BINFO_PRIMARY_P (primary))
1671 /* We are stealing a primary base. */
1672 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1673 BINFO_PRIMARY_P (primary) = 1;
1674 if (BINFO_VIRTUAL_P (primary))
1676 tree delta;
1678 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1679 /* A virtual binfo might have been copied from within
1680 another hierarchy. As we're about to use it as a primary
1681 base, make sure the offsets match. */
1682 delta = size_diffop_loc (input_location, ssize_int (0),
1683 convert (ssizetype, BINFO_OFFSET (primary)));
1685 propagate_binfo_offsets (primary, delta);
1688 primary = TYPE_BINFO (basetype);
1690 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1691 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1692 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1696 /* Update the variant types of T. */
1698 void
1699 fixup_type_variants (tree t)
1701 tree variants;
1703 if (!t)
1704 return;
1706 for (variants = TYPE_NEXT_VARIANT (t);
1707 variants;
1708 variants = TYPE_NEXT_VARIANT (variants))
1710 /* These fields are in the _TYPE part of the node, not in
1711 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1712 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1713 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1714 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1715 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1717 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1719 TYPE_BINFO (variants) = TYPE_BINFO (t);
1721 /* Copy whatever these are holding today. */
1722 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1723 TYPE_METHODS (variants) = TYPE_METHODS (t);
1724 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1728 /* Early variant fixups: we apply attributes at the beginning of the class
1729 definition, and we need to fix up any variants that have already been
1730 made via elaborated-type-specifier so that check_qualified_type works. */
1732 void
1733 fixup_attribute_variants (tree t)
1735 tree variants;
1737 if (!t)
1738 return;
1740 for (variants = TYPE_NEXT_VARIANT (t);
1741 variants;
1742 variants = TYPE_NEXT_VARIANT (variants))
1744 /* These are the two fields that check_qualified_type looks at and
1745 are affected by attributes. */
1746 TYPE_ATTRIBUTES (variants) = TYPE_ATTRIBUTES (t);
1747 TYPE_ALIGN (variants) = TYPE_ALIGN (t);
1751 /* Set memoizing fields and bits of T (and its variants) for later
1752 use. */
1754 static void
1755 finish_struct_bits (tree t)
1757 /* Fix up variants (if any). */
1758 fixup_type_variants (t);
1760 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1761 /* For a class w/o baseclasses, 'finish_struct' has set
1762 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1763 Similarly for a class whose base classes do not have vtables.
1764 When neither of these is true, we might have removed abstract
1765 virtuals (by providing a definition), added some (by declaring
1766 new ones), or redeclared ones from a base class. We need to
1767 recalculate what's really an abstract virtual at this point (by
1768 looking in the vtables). */
1769 get_pure_virtuals (t);
1771 /* If this type has a copy constructor or a destructor, force its
1772 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
1773 nonzero. This will cause it to be passed by invisible reference
1774 and prevent it from being returned in a register. */
1775 if (type_has_nontrivial_copy_init (t)
1776 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1778 tree variants;
1779 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1780 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1782 SET_TYPE_MODE (variants, BLKmode);
1783 TREE_ADDRESSABLE (variants) = 1;
1788 /* Issue warnings about T having private constructors, but no friends,
1789 and so forth.
1791 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1792 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1793 non-private static member functions. */
1795 static void
1796 maybe_warn_about_overly_private_class (tree t)
1798 int has_member_fn = 0;
1799 int has_nonprivate_method = 0;
1800 tree fn;
1802 if (!warn_ctor_dtor_privacy
1803 /* If the class has friends, those entities might create and
1804 access instances, so we should not warn. */
1805 || (CLASSTYPE_FRIEND_CLASSES (t)
1806 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1807 /* We will have warned when the template was declared; there's
1808 no need to warn on every instantiation. */
1809 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1810 /* There's no reason to even consider warning about this
1811 class. */
1812 return;
1814 /* We only issue one warning, if more than one applies, because
1815 otherwise, on code like:
1817 class A {
1818 // Oops - forgot `public:'
1819 A();
1820 A(const A&);
1821 ~A();
1824 we warn several times about essentially the same problem. */
1826 /* Check to see if all (non-constructor, non-destructor) member
1827 functions are private. (Since there are no friends or
1828 non-private statics, we can't ever call any of the private member
1829 functions.) */
1830 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
1831 /* We're not interested in compiler-generated methods; they don't
1832 provide any way to call private members. */
1833 if (!DECL_ARTIFICIAL (fn))
1835 if (!TREE_PRIVATE (fn))
1837 if (DECL_STATIC_FUNCTION_P (fn))
1838 /* A non-private static member function is just like a
1839 friend; it can create and invoke private member
1840 functions, and be accessed without a class
1841 instance. */
1842 return;
1844 has_nonprivate_method = 1;
1845 /* Keep searching for a static member function. */
1847 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1848 has_member_fn = 1;
1851 if (!has_nonprivate_method && has_member_fn)
1853 /* There are no non-private methods, and there's at least one
1854 private member function that isn't a constructor or
1855 destructor. (If all the private members are
1856 constructors/destructors we want to use the code below that
1857 issues error messages specifically referring to
1858 constructors/destructors.) */
1859 unsigned i;
1860 tree binfo = TYPE_BINFO (t);
1862 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
1863 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
1865 has_nonprivate_method = 1;
1866 break;
1868 if (!has_nonprivate_method)
1870 warning (OPT_Wctor_dtor_privacy,
1871 "all member functions in class %qT are private", t);
1872 return;
1876 /* Even if some of the member functions are non-private, the class
1877 won't be useful for much if all the constructors or destructors
1878 are private: such an object can never be created or destroyed. */
1879 fn = CLASSTYPE_DESTRUCTORS (t);
1880 if (fn && TREE_PRIVATE (fn))
1882 warning (OPT_Wctor_dtor_privacy,
1883 "%q#T only defines a private destructor and has no friends",
1885 return;
1888 /* Warn about classes that have private constructors and no friends. */
1889 if (TYPE_HAS_USER_CONSTRUCTOR (t)
1890 /* Implicitly generated constructors are always public. */
1891 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
1892 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
1894 int nonprivate_ctor = 0;
1896 /* If a non-template class does not define a copy
1897 constructor, one is defined for it, enabling it to avoid
1898 this warning. For a template class, this does not
1899 happen, and so we would normally get a warning on:
1901 template <class T> class C { private: C(); };
1903 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
1904 complete non-template or fully instantiated classes have this
1905 flag set. */
1906 if (!TYPE_HAS_COPY_CTOR (t))
1907 nonprivate_ctor = 1;
1908 else
1909 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
1911 tree ctor = OVL_CURRENT (fn);
1912 /* Ideally, we wouldn't count copy constructors (or, in
1913 fact, any constructor that takes an argument of the
1914 class type as a parameter) because such things cannot
1915 be used to construct an instance of the class unless
1916 you already have one. But, for now at least, we're
1917 more generous. */
1918 if (! TREE_PRIVATE (ctor))
1920 nonprivate_ctor = 1;
1921 break;
1925 if (nonprivate_ctor == 0)
1927 warning (OPT_Wctor_dtor_privacy,
1928 "%q#T only defines private constructors and has no friends",
1930 return;
1935 static struct {
1936 gt_pointer_operator new_value;
1937 void *cookie;
1938 } resort_data;
1940 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1942 static int
1943 method_name_cmp (const void* m1_p, const void* m2_p)
1945 const tree *const m1 = (const tree *) m1_p;
1946 const tree *const m2 = (const tree *) m2_p;
1948 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1949 return 0;
1950 if (*m1 == NULL_TREE)
1951 return -1;
1952 if (*m2 == NULL_TREE)
1953 return 1;
1954 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1955 return -1;
1956 return 1;
1959 /* This routine compares two fields like method_name_cmp but using the
1960 pointer operator in resort_field_decl_data. */
1962 static int
1963 resort_method_name_cmp (const void* m1_p, const void* m2_p)
1965 const tree *const m1 = (const tree *) m1_p;
1966 const tree *const m2 = (const tree *) m2_p;
1967 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1968 return 0;
1969 if (*m1 == NULL_TREE)
1970 return -1;
1971 if (*m2 == NULL_TREE)
1972 return 1;
1974 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
1975 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
1976 resort_data.new_value (&d1, resort_data.cookie);
1977 resort_data.new_value (&d2, resort_data.cookie);
1978 if (d1 < d2)
1979 return -1;
1981 return 1;
1984 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
1986 void
1987 resort_type_method_vec (void* obj,
1988 void* /*orig_obj*/,
1989 gt_pointer_operator new_value,
1990 void* cookie)
1992 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
1993 int len = vec_safe_length (method_vec);
1994 size_t slot;
1995 tree fn;
1997 /* The type conversion ops have to live at the front of the vec, so we
1998 can't sort them. */
1999 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2000 vec_safe_iterate (method_vec, slot, &fn);
2001 ++slot)
2002 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2003 break;
2005 if (len - slot > 1)
2007 resort_data.new_value = new_value;
2008 resort_data.cookie = cookie;
2009 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2010 resort_method_name_cmp);
2014 /* Warn about duplicate methods in fn_fields.
2016 Sort methods that are not special (i.e., constructors, destructors,
2017 and type conversion operators) so that we can find them faster in
2018 search. */
2020 static void
2021 finish_struct_methods (tree t)
2023 tree fn_fields;
2024 vec<tree, va_gc> *method_vec;
2025 int slot, len;
2027 method_vec = CLASSTYPE_METHOD_VEC (t);
2028 if (!method_vec)
2029 return;
2031 len = method_vec->length ();
2033 /* Clear DECL_IN_AGGR_P for all functions. */
2034 for (fn_fields = TYPE_METHODS (t); fn_fields;
2035 fn_fields = DECL_CHAIN (fn_fields))
2036 DECL_IN_AGGR_P (fn_fields) = 0;
2038 /* Issue warnings about private constructors and such. If there are
2039 no methods, then some public defaults are generated. */
2040 maybe_warn_about_overly_private_class (t);
2042 /* The type conversion ops have to live at the front of the vec, so we
2043 can't sort them. */
2044 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2045 method_vec->iterate (slot, &fn_fields);
2046 ++slot)
2047 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
2048 break;
2049 if (len - slot > 1)
2050 qsort (method_vec->address () + slot,
2051 len-slot, sizeof (tree), method_name_cmp);
2054 /* Make BINFO's vtable have N entries, including RTTI entries,
2055 vbase and vcall offsets, etc. Set its type and call the back end
2056 to lay it out. */
2058 static void
2059 layout_vtable_decl (tree binfo, int n)
2061 tree atype;
2062 tree vtable;
2064 atype = build_array_of_n_type (vtable_entry_type, n);
2065 layout_type (atype);
2067 /* We may have to grow the vtable. */
2068 vtable = get_vtbl_decl_for_binfo (binfo);
2069 if (!same_type_p (TREE_TYPE (vtable), atype))
2071 TREE_TYPE (vtable) = atype;
2072 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2073 layout_decl (vtable, 0);
2077 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2078 have the same signature. */
2081 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2083 /* One destructor overrides another if they are the same kind of
2084 destructor. */
2085 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2086 && special_function_p (base_fndecl) == special_function_p (fndecl))
2087 return 1;
2088 /* But a non-destructor never overrides a destructor, nor vice
2089 versa, nor do different kinds of destructors override
2090 one-another. For example, a complete object destructor does not
2091 override a deleting destructor. */
2092 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2093 return 0;
2095 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2096 || (DECL_CONV_FN_P (fndecl)
2097 && DECL_CONV_FN_P (base_fndecl)
2098 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2099 DECL_CONV_FN_TYPE (base_fndecl))))
2101 tree fntype = TREE_TYPE (fndecl);
2102 tree base_fntype = TREE_TYPE (base_fndecl);
2103 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2104 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2105 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2106 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2107 return 1;
2109 return 0;
2112 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2113 subobject. */
2115 static bool
2116 base_derived_from (tree derived, tree base)
2118 tree probe;
2120 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2122 if (probe == derived)
2123 return true;
2124 else if (BINFO_VIRTUAL_P (probe))
2125 /* If we meet a virtual base, we can't follow the inheritance
2126 any more. See if the complete type of DERIVED contains
2127 such a virtual base. */
2128 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2129 != NULL_TREE);
2131 return false;
2134 typedef struct find_final_overrider_data_s {
2135 /* The function for which we are trying to find a final overrider. */
2136 tree fn;
2137 /* The base class in which the function was declared. */
2138 tree declaring_base;
2139 /* The candidate overriders. */
2140 tree candidates;
2141 /* Path to most derived. */
2142 vec<tree> path;
2143 } find_final_overrider_data;
2145 /* Add the overrider along the current path to FFOD->CANDIDATES.
2146 Returns true if an overrider was found; false otherwise. */
2148 static bool
2149 dfs_find_final_overrider_1 (tree binfo,
2150 find_final_overrider_data *ffod,
2151 unsigned depth)
2153 tree method;
2155 /* If BINFO is not the most derived type, try a more derived class.
2156 A definition there will overrider a definition here. */
2157 if (depth)
2159 depth--;
2160 if (dfs_find_final_overrider_1
2161 (ffod->path[depth], ffod, depth))
2162 return true;
2165 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2166 if (method)
2168 tree *candidate = &ffod->candidates;
2170 /* Remove any candidates overridden by this new function. */
2171 while (*candidate)
2173 /* If *CANDIDATE overrides METHOD, then METHOD
2174 cannot override anything else on the list. */
2175 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2176 return true;
2177 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2178 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2179 *candidate = TREE_CHAIN (*candidate);
2180 else
2181 candidate = &TREE_CHAIN (*candidate);
2184 /* Add the new function. */
2185 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2186 return true;
2189 return false;
2192 /* Called from find_final_overrider via dfs_walk. */
2194 static tree
2195 dfs_find_final_overrider_pre (tree binfo, void *data)
2197 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2199 if (binfo == ffod->declaring_base)
2200 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2201 ffod->path.safe_push (binfo);
2203 return NULL_TREE;
2206 static tree
2207 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2209 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2210 ffod->path.pop ();
2212 return NULL_TREE;
2215 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2216 FN and whose TREE_VALUE is the binfo for the base where the
2217 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2218 DERIVED) is the base object in which FN is declared. */
2220 static tree
2221 find_final_overrider (tree derived, tree binfo, tree fn)
2223 find_final_overrider_data ffod;
2225 /* Getting this right is a little tricky. This is valid:
2227 struct S { virtual void f (); };
2228 struct T { virtual void f (); };
2229 struct U : public S, public T { };
2231 even though calling `f' in `U' is ambiguous. But,
2233 struct R { virtual void f(); };
2234 struct S : virtual public R { virtual void f (); };
2235 struct T : virtual public R { virtual void f (); };
2236 struct U : public S, public T { };
2238 is not -- there's no way to decide whether to put `S::f' or
2239 `T::f' in the vtable for `R'.
2241 The solution is to look at all paths to BINFO. If we find
2242 different overriders along any two, then there is a problem. */
2243 if (DECL_THUNK_P (fn))
2244 fn = THUNK_TARGET (fn);
2246 /* Determine the depth of the hierarchy. */
2247 ffod.fn = fn;
2248 ffod.declaring_base = binfo;
2249 ffod.candidates = NULL_TREE;
2250 ffod.path.create (30);
2252 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2253 dfs_find_final_overrider_post, &ffod);
2255 ffod.path.release ();
2257 /* If there was no winner, issue an error message. */
2258 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2259 return error_mark_node;
2261 return ffod.candidates;
2264 /* Return the index of the vcall offset for FN when TYPE is used as a
2265 virtual base. */
2267 static tree
2268 get_vcall_index (tree fn, tree type)
2270 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2271 tree_pair_p p;
2272 unsigned ix;
2274 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2275 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2276 || same_signature_p (fn, p->purpose))
2277 return p->value;
2279 /* There should always be an appropriate index. */
2280 gcc_unreachable ();
2283 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2284 dominated by T. FN is the old function; VIRTUALS points to the
2285 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2286 of that entry in the list. */
2288 static void
2289 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2290 unsigned ix)
2292 tree b;
2293 tree overrider;
2294 tree delta;
2295 tree virtual_base;
2296 tree first_defn;
2297 tree overrider_fn, overrider_target;
2298 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2299 tree over_return, base_return;
2300 bool lost = false;
2302 /* Find the nearest primary base (possibly binfo itself) which defines
2303 this function; this is the class the caller will convert to when
2304 calling FN through BINFO. */
2305 for (b = binfo; ; b = get_primary_binfo (b))
2307 gcc_assert (b);
2308 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2309 break;
2311 /* The nearest definition is from a lost primary. */
2312 if (BINFO_LOST_PRIMARY_P (b))
2313 lost = true;
2315 first_defn = b;
2317 /* Find the final overrider. */
2318 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2319 if (overrider == error_mark_node)
2321 error ("no unique final overrider for %qD in %qT", target_fn, t);
2322 return;
2324 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2326 /* Check for adjusting covariant return types. */
2327 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2328 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2330 if (POINTER_TYPE_P (over_return)
2331 && TREE_CODE (over_return) == TREE_CODE (base_return)
2332 && CLASS_TYPE_P (TREE_TYPE (over_return))
2333 && CLASS_TYPE_P (TREE_TYPE (base_return))
2334 /* If the overrider is invalid, don't even try. */
2335 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2337 /* If FN is a covariant thunk, we must figure out the adjustment
2338 to the final base FN was converting to. As OVERRIDER_TARGET might
2339 also be converting to the return type of FN, we have to
2340 combine the two conversions here. */
2341 tree fixed_offset, virtual_offset;
2343 over_return = TREE_TYPE (over_return);
2344 base_return = TREE_TYPE (base_return);
2346 if (DECL_THUNK_P (fn))
2348 gcc_assert (DECL_RESULT_THUNK_P (fn));
2349 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2350 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2352 else
2353 fixed_offset = virtual_offset = NULL_TREE;
2355 if (virtual_offset)
2356 /* Find the equivalent binfo within the return type of the
2357 overriding function. We will want the vbase offset from
2358 there. */
2359 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2360 over_return);
2361 else if (!same_type_ignoring_top_level_qualifiers_p
2362 (over_return, base_return))
2364 /* There was no existing virtual thunk (which takes
2365 precedence). So find the binfo of the base function's
2366 return type within the overriding function's return type.
2367 We cannot call lookup base here, because we're inside a
2368 dfs_walk, and will therefore clobber the BINFO_MARKED
2369 flags. Fortunately we know the covariancy is valid (it
2370 has already been checked), so we can just iterate along
2371 the binfos, which have been chained in inheritance graph
2372 order. Of course it is lame that we have to repeat the
2373 search here anyway -- we should really be caching pieces
2374 of the vtable and avoiding this repeated work. */
2375 tree thunk_binfo, base_binfo;
2377 /* Find the base binfo within the overriding function's
2378 return type. We will always find a thunk_binfo, except
2379 when the covariancy is invalid (which we will have
2380 already diagnosed). */
2381 for (base_binfo = TYPE_BINFO (base_return),
2382 thunk_binfo = TYPE_BINFO (over_return);
2383 thunk_binfo;
2384 thunk_binfo = TREE_CHAIN (thunk_binfo))
2385 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2386 BINFO_TYPE (base_binfo)))
2387 break;
2389 /* See if virtual inheritance is involved. */
2390 for (virtual_offset = thunk_binfo;
2391 virtual_offset;
2392 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2393 if (BINFO_VIRTUAL_P (virtual_offset))
2394 break;
2396 if (virtual_offset
2397 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2399 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2401 if (virtual_offset)
2403 /* We convert via virtual base. Adjust the fixed
2404 offset to be from there. */
2405 offset =
2406 size_diffop (offset,
2407 convert (ssizetype,
2408 BINFO_OFFSET (virtual_offset)));
2410 if (fixed_offset)
2411 /* There was an existing fixed offset, this must be
2412 from the base just converted to, and the base the
2413 FN was thunking to. */
2414 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2415 else
2416 fixed_offset = offset;
2420 if (fixed_offset || virtual_offset)
2421 /* Replace the overriding function with a covariant thunk. We
2422 will emit the overriding function in its own slot as
2423 well. */
2424 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2425 fixed_offset, virtual_offset);
2427 else
2428 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2429 !DECL_THUNK_P (fn));
2431 /* If we need a covariant thunk, then we may need to adjust first_defn.
2432 The ABI specifies that the thunks emitted with a function are
2433 determined by which bases the function overrides, so we need to be
2434 sure that we're using a thunk for some overridden base; even if we
2435 know that the necessary this adjustment is zero, there may not be an
2436 appropriate zero-this-adjusment thunk for us to use since thunks for
2437 overriding virtual bases always use the vcall offset.
2439 Furthermore, just choosing any base that overrides this function isn't
2440 quite right, as this slot won't be used for calls through a type that
2441 puts a covariant thunk here. Calling the function through such a type
2442 will use a different slot, and that slot is the one that determines
2443 the thunk emitted for that base.
2445 So, keep looking until we find the base that we're really overriding
2446 in this slot: the nearest primary base that doesn't use a covariant
2447 thunk in this slot. */
2448 if (overrider_target != overrider_fn)
2450 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2451 /* We already know that the overrider needs a covariant thunk. */
2452 b = get_primary_binfo (b);
2453 for (; ; b = get_primary_binfo (b))
2455 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2456 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2457 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2458 break;
2459 if (BINFO_LOST_PRIMARY_P (b))
2460 lost = true;
2462 first_defn = b;
2465 /* Assume that we will produce a thunk that convert all the way to
2466 the final overrider, and not to an intermediate virtual base. */
2467 virtual_base = NULL_TREE;
2469 /* See if we can convert to an intermediate virtual base first, and then
2470 use the vcall offset located there to finish the conversion. */
2471 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2473 /* If we find the final overrider, then we can stop
2474 walking. */
2475 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2476 BINFO_TYPE (TREE_VALUE (overrider))))
2477 break;
2479 /* If we find a virtual base, and we haven't yet found the
2480 overrider, then there is a virtual base between the
2481 declaring base (first_defn) and the final overrider. */
2482 if (BINFO_VIRTUAL_P (b))
2484 virtual_base = b;
2485 break;
2489 /* Compute the constant adjustment to the `this' pointer. The
2490 `this' pointer, when this function is called, will point at BINFO
2491 (or one of its primary bases, which are at the same offset). */
2492 if (virtual_base)
2493 /* The `this' pointer needs to be adjusted from the declaration to
2494 the nearest virtual base. */
2495 delta = size_diffop_loc (input_location,
2496 convert (ssizetype, BINFO_OFFSET (virtual_base)),
2497 convert (ssizetype, BINFO_OFFSET (first_defn)));
2498 else if (lost)
2499 /* If the nearest definition is in a lost primary, we don't need an
2500 entry in our vtable. Except possibly in a constructor vtable,
2501 if we happen to get our primary back. In that case, the offset
2502 will be zero, as it will be a primary base. */
2503 delta = size_zero_node;
2504 else
2505 /* The `this' pointer needs to be adjusted from pointing to
2506 BINFO to pointing at the base where the final overrider
2507 appears. */
2508 delta = size_diffop_loc (input_location,
2509 convert (ssizetype,
2510 BINFO_OFFSET (TREE_VALUE (overrider))),
2511 convert (ssizetype, BINFO_OFFSET (binfo)));
2513 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2515 if (virtual_base)
2516 BV_VCALL_INDEX (*virtuals)
2517 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2518 else
2519 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2521 BV_LOST_PRIMARY (*virtuals) = lost;
2524 /* Called from modify_all_vtables via dfs_walk. */
2526 static tree
2527 dfs_modify_vtables (tree binfo, void* data)
2529 tree t = (tree) data;
2530 tree virtuals;
2531 tree old_virtuals;
2532 unsigned ix;
2534 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2535 /* A base without a vtable needs no modification, and its bases
2536 are uninteresting. */
2537 return dfs_skip_bases;
2539 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2540 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2541 /* Don't do the primary vtable, if it's new. */
2542 return NULL_TREE;
2544 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2545 /* There's no need to modify the vtable for a non-virtual primary
2546 base; we're not going to use that vtable anyhow. We do still
2547 need to do this for virtual primary bases, as they could become
2548 non-primary in a construction vtable. */
2549 return NULL_TREE;
2551 make_new_vtable (t, binfo);
2553 /* Now, go through each of the virtual functions in the virtual
2554 function table for BINFO. Find the final overrider, and update
2555 the BINFO_VIRTUALS list appropriately. */
2556 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2557 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2558 virtuals;
2559 ix++, virtuals = TREE_CHAIN (virtuals),
2560 old_virtuals = TREE_CHAIN (old_virtuals))
2561 update_vtable_entry_for_fn (t,
2562 binfo,
2563 BV_FN (old_virtuals),
2564 &virtuals, ix);
2566 return NULL_TREE;
2569 /* Update all of the primary and secondary vtables for T. Create new
2570 vtables as required, and initialize their RTTI information. Each
2571 of the functions in VIRTUALS is declared in T and may override a
2572 virtual function from a base class; find and modify the appropriate
2573 entries to point to the overriding functions. Returns a list, in
2574 declaration order, of the virtual functions that are declared in T,
2575 but do not appear in the primary base class vtable, and which
2576 should therefore be appended to the end of the vtable for T. */
2578 static tree
2579 modify_all_vtables (tree t, tree virtuals)
2581 tree binfo = TYPE_BINFO (t);
2582 tree *fnsp;
2584 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2585 if (TYPE_CONTAINS_VPTR_P (t))
2586 get_vtable_decl (t, false);
2588 /* Update all of the vtables. */
2589 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2591 /* Add virtual functions not already in our primary vtable. These
2592 will be both those introduced by this class, and those overridden
2593 from secondary bases. It does not include virtuals merely
2594 inherited from secondary bases. */
2595 for (fnsp = &virtuals; *fnsp; )
2597 tree fn = TREE_VALUE (*fnsp);
2599 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2600 || DECL_VINDEX (fn) == error_mark_node)
2602 /* We don't need to adjust the `this' pointer when
2603 calling this function. */
2604 BV_DELTA (*fnsp) = integer_zero_node;
2605 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2607 /* This is a function not already in our vtable. Keep it. */
2608 fnsp = &TREE_CHAIN (*fnsp);
2610 else
2611 /* We've already got an entry for this function. Skip it. */
2612 *fnsp = TREE_CHAIN (*fnsp);
2615 return virtuals;
2618 /* Get the base virtual function declarations in T that have the
2619 indicated NAME. */
2621 static tree
2622 get_basefndecls (tree name, tree t)
2624 tree methods;
2625 tree base_fndecls = NULL_TREE;
2626 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2627 int i;
2629 /* Find virtual functions in T with the indicated NAME. */
2630 i = lookup_fnfields_1 (t, name);
2631 if (i != -1)
2632 for (methods = (*CLASSTYPE_METHOD_VEC (t))[i];
2633 methods;
2634 methods = OVL_NEXT (methods))
2636 tree method = OVL_CURRENT (methods);
2638 if (TREE_CODE (method) == FUNCTION_DECL
2639 && DECL_VINDEX (method))
2640 base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
2643 if (base_fndecls)
2644 return base_fndecls;
2646 for (i = 0; i < n_baseclasses; i++)
2648 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2649 base_fndecls = chainon (get_basefndecls (name, basetype),
2650 base_fndecls);
2653 return base_fndecls;
2656 /* If this declaration supersedes the declaration of
2657 a method declared virtual in the base class, then
2658 mark this field as being virtual as well. */
2660 void
2661 check_for_override (tree decl, tree ctype)
2663 bool overrides_found = false;
2664 if (TREE_CODE (decl) == TEMPLATE_DECL)
2665 /* In [temp.mem] we have:
2667 A specialization of a member function template does not
2668 override a virtual function from a base class. */
2669 return;
2670 if ((DECL_DESTRUCTOR_P (decl)
2671 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2672 || DECL_CONV_FN_P (decl))
2673 && look_for_overrides (ctype, decl)
2674 && !DECL_STATIC_FUNCTION_P (decl))
2675 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2676 the error_mark_node so that we know it is an overriding
2677 function. */
2679 DECL_VINDEX (decl) = decl;
2680 overrides_found = true;
2683 if (DECL_VIRTUAL_P (decl))
2685 if (!DECL_VINDEX (decl))
2686 DECL_VINDEX (decl) = error_mark_node;
2687 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2688 if (DECL_DESTRUCTOR_P (decl))
2689 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2691 else if (DECL_FINAL_P (decl))
2692 error ("%q+#D marked final, but is not virtual", decl);
2693 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2694 error ("%q+#D marked override, but does not override", decl);
2697 /* Warn about hidden virtual functions that are not overridden in t.
2698 We know that constructors and destructors don't apply. */
2700 static void
2701 warn_hidden (tree t)
2703 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
2704 tree fns;
2705 size_t i;
2707 /* We go through each separately named virtual function. */
2708 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2709 vec_safe_iterate (method_vec, i, &fns);
2710 ++i)
2712 tree fn;
2713 tree name;
2714 tree fndecl;
2715 tree base_fndecls;
2716 tree base_binfo;
2717 tree binfo;
2718 int j;
2720 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2721 have the same name. Figure out what name that is. */
2722 name = DECL_NAME (OVL_CURRENT (fns));
2723 /* There are no possibly hidden functions yet. */
2724 base_fndecls = NULL_TREE;
2725 /* Iterate through all of the base classes looking for possibly
2726 hidden functions. */
2727 for (binfo = TYPE_BINFO (t), j = 0;
2728 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2730 tree basetype = BINFO_TYPE (base_binfo);
2731 base_fndecls = chainon (get_basefndecls (name, basetype),
2732 base_fndecls);
2735 /* If there are no functions to hide, continue. */
2736 if (!base_fndecls)
2737 continue;
2739 /* Remove any overridden functions. */
2740 for (fn = fns; fn; fn = OVL_NEXT (fn))
2742 fndecl = OVL_CURRENT (fn);
2743 if (DECL_VINDEX (fndecl))
2745 tree *prev = &base_fndecls;
2747 while (*prev)
2748 /* If the method from the base class has the same
2749 signature as the method from the derived class, it
2750 has been overridden. */
2751 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2752 *prev = TREE_CHAIN (*prev);
2753 else
2754 prev = &TREE_CHAIN (*prev);
2758 /* Now give a warning for all base functions without overriders,
2759 as they are hidden. */
2760 while (base_fndecls)
2762 /* Here we know it is a hider, and no overrider exists. */
2763 warning (OPT_Woverloaded_virtual, "%q+D was hidden", TREE_VALUE (base_fndecls));
2764 warning (OPT_Woverloaded_virtual, " by %q+D", fns);
2765 base_fndecls = TREE_CHAIN (base_fndecls);
2770 /* Check for things that are invalid. There are probably plenty of other
2771 things we should check for also. */
2773 static void
2774 finish_struct_anon (tree t)
2776 tree field;
2778 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2780 if (TREE_STATIC (field))
2781 continue;
2782 if (TREE_CODE (field) != FIELD_DECL)
2783 continue;
2785 if (DECL_NAME (field) == NULL_TREE
2786 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2788 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2789 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2790 for (; elt; elt = DECL_CHAIN (elt))
2792 /* We're generally only interested in entities the user
2793 declared, but we also find nested classes by noticing
2794 the TYPE_DECL that we create implicitly. You're
2795 allowed to put one anonymous union inside another,
2796 though, so we explicitly tolerate that. We use
2797 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2798 we also allow unnamed types used for defining fields. */
2799 if (DECL_ARTIFICIAL (elt)
2800 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2801 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2802 continue;
2804 if (TREE_CODE (elt) != FIELD_DECL)
2806 if (is_union)
2807 permerror (input_location, "%q+#D invalid; an anonymous union can "
2808 "only have non-static data members", elt);
2809 else
2810 permerror (input_location, "%q+#D invalid; an anonymous struct can "
2811 "only have non-static data members", elt);
2812 continue;
2815 if (TREE_PRIVATE (elt))
2817 if (is_union)
2818 permerror (input_location, "private member %q+#D in anonymous union", elt);
2819 else
2820 permerror (input_location, "private member %q+#D in anonymous struct", elt);
2822 else if (TREE_PROTECTED (elt))
2824 if (is_union)
2825 permerror (input_location, "protected member %q+#D in anonymous union", elt);
2826 else
2827 permerror (input_location, "protected member %q+#D in anonymous struct", elt);
2830 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2831 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2837 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2838 will be used later during class template instantiation.
2839 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2840 a non-static member data (FIELD_DECL), a member function
2841 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2842 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2843 When FRIEND_P is nonzero, T is either a friend class
2844 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2845 (FUNCTION_DECL, TEMPLATE_DECL). */
2847 void
2848 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2850 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2851 if (CLASSTYPE_TEMPLATE_INFO (type))
2852 CLASSTYPE_DECL_LIST (type)
2853 = tree_cons (friend_p ? NULL_TREE : type,
2854 t, CLASSTYPE_DECL_LIST (type));
2857 /* This function is called from declare_virt_assop_and_dtor via
2858 dfs_walk_all.
2860 DATA is a type that direcly or indirectly inherits the base
2861 represented by BINFO. If BINFO contains a virtual assignment [copy
2862 assignment or move assigment] operator or a virtual constructor,
2863 declare that function in DATA if it hasn't been already declared. */
2865 static tree
2866 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
2868 tree bv, fn, t = (tree)data;
2869 tree opname = ansi_assopname (NOP_EXPR);
2871 gcc_assert (t && CLASS_TYPE_P (t));
2872 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
2874 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2875 /* A base without a vtable needs no modification, and its bases
2876 are uninteresting. */
2877 return dfs_skip_bases;
2879 if (BINFO_PRIMARY_P (binfo))
2880 /* If this is a primary base, then we have already looked at the
2881 virtual functions of its vtable. */
2882 return NULL_TREE;
2884 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
2886 fn = BV_FN (bv);
2888 if (DECL_NAME (fn) == opname)
2890 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
2891 lazily_declare_fn (sfk_copy_assignment, t);
2892 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
2893 lazily_declare_fn (sfk_move_assignment, t);
2895 else if (DECL_DESTRUCTOR_P (fn)
2896 && CLASSTYPE_LAZY_DESTRUCTOR (t))
2897 lazily_declare_fn (sfk_destructor, t);
2900 return NULL_TREE;
2903 /* If the class type T has a direct or indirect base that contains a
2904 virtual assignment operator or a virtual destructor, declare that
2905 function in T if it hasn't been already declared. */
2907 static void
2908 declare_virt_assop_and_dtor (tree t)
2910 if (!(TYPE_POLYMORPHIC_P (t)
2911 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
2912 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
2913 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
2914 return;
2916 dfs_walk_all (TYPE_BINFO (t),
2917 dfs_declare_virt_assop_and_dtor,
2918 NULL, t);
2921 /* Declare the inheriting constructor for class T inherited from base
2922 constructor CTOR with the parameter array PARMS of size NPARMS. */
2924 static void
2925 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
2927 /* We don't declare an inheriting ctor that would be a default,
2928 copy or move ctor for derived or base. */
2929 if (nparms == 0)
2930 return;
2931 if (nparms == 1
2932 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
2934 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
2935 if (parm == t || parm == DECL_CONTEXT (ctor))
2936 return;
2939 tree parmlist = void_list_node;
2940 for (int i = nparms - 1; i >= 0; i--)
2941 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
2942 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
2943 t, false, ctor, parmlist);
2944 if (add_method (t, fn, NULL_TREE))
2946 DECL_CHAIN (fn) = TYPE_METHODS (t);
2947 TYPE_METHODS (t) = fn;
2951 /* Declare all the inheriting constructors for class T inherited from base
2952 constructor CTOR. */
2954 static void
2955 one_inherited_ctor (tree ctor, tree t)
2957 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
2959 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
2960 int i = 0;
2961 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
2963 if (TREE_PURPOSE (parms))
2964 one_inheriting_sig (t, ctor, new_parms, i);
2965 new_parms[i++] = TREE_VALUE (parms);
2967 one_inheriting_sig (t, ctor, new_parms, i);
2968 if (parms == NULL_TREE)
2970 warning (OPT_Winherited_variadic_ctor,
2971 "the ellipsis in %qD is not inherited", ctor);
2972 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
2976 /* Create default constructors, assignment operators, and so forth for
2977 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
2978 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
2979 the class cannot have a default constructor, copy constructor
2980 taking a const reference argument, or an assignment operator taking
2981 a const reference, respectively. */
2983 static void
2984 add_implicitly_declared_members (tree t, tree* access_decls,
2985 int cant_have_const_cctor,
2986 int cant_have_const_assignment)
2988 bool move_ok = false;
2990 if (cxx_dialect >= cxx0x && !CLASSTYPE_DESTRUCTORS (t)
2991 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
2992 && !type_has_move_constructor (t) && !type_has_move_assign (t))
2993 move_ok = true;
2995 /* Destructor. */
2996 if (!CLASSTYPE_DESTRUCTORS (t))
2998 /* In general, we create destructors lazily. */
2999 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3001 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3002 && TYPE_FOR_JAVA (t))
3003 /* But if this is a Java class, any non-trivial destructor is
3004 invalid, even if compiler-generated. Therefore, if the
3005 destructor is non-trivial we create it now. */
3006 lazily_declare_fn (sfk_destructor, t);
3009 /* [class.ctor]
3011 If there is no user-declared constructor for a class, a default
3012 constructor is implicitly declared. */
3013 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3015 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3016 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3017 if (cxx_dialect >= cxx0x)
3018 TYPE_HAS_CONSTEXPR_CTOR (t)
3019 /* This might force the declaration. */
3020 = type_has_constexpr_default_constructor (t);
3023 /* [class.ctor]
3025 If a class definition does not explicitly declare a copy
3026 constructor, one is declared implicitly. */
3027 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
3029 TYPE_HAS_COPY_CTOR (t) = 1;
3030 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3031 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3032 if (move_ok)
3033 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3036 /* If there is no assignment operator, one will be created if and
3037 when it is needed. For now, just record whether or not the type
3038 of the parameter to the assignment operator will be a const or
3039 non-const reference. */
3040 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
3042 TYPE_HAS_COPY_ASSIGN (t) = 1;
3043 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3044 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3045 if (move_ok)
3046 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3049 /* We can't be lazy about declaring functions that might override
3050 a virtual function from a base class. */
3051 declare_virt_assop_and_dtor (t);
3053 while (*access_decls)
3055 tree using_decl = TREE_VALUE (*access_decls);
3056 tree decl = USING_DECL_DECLS (using_decl);
3057 if (DECL_NAME (using_decl) == ctor_identifier)
3059 /* declare, then remove the decl */
3060 tree ctor_list = decl;
3061 location_t loc = input_location;
3062 input_location = DECL_SOURCE_LOCATION (using_decl);
3063 if (ctor_list)
3064 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
3065 one_inherited_ctor (OVL_CURRENT (ctor_list), t);
3066 *access_decls = TREE_CHAIN (*access_decls);
3067 input_location = loc;
3069 else
3070 access_decls = &TREE_CHAIN (*access_decls);
3074 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3075 count the number of fields in TYPE, including anonymous union
3076 members. */
3078 static int
3079 count_fields (tree fields)
3081 tree x;
3082 int n_fields = 0;
3083 for (x = fields; x; x = DECL_CHAIN (x))
3085 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3086 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3087 else
3088 n_fields += 1;
3090 return n_fields;
3093 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3094 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3095 elts, starting at offset IDX. */
3097 static int
3098 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3100 tree x;
3101 for (x = fields; x; x = DECL_CHAIN (x))
3103 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3104 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3105 else
3106 field_vec->elts[idx++] = x;
3108 return idx;
3111 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3112 starting at offset IDX. */
3114 static int
3115 add_enum_fields_to_record_type (tree enumtype,
3116 struct sorted_fields_type *field_vec,
3117 int idx)
3119 tree values;
3120 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3121 field_vec->elts[idx++] = TREE_VALUE (values);
3122 return idx;
3125 /* FIELD is a bit-field. We are finishing the processing for its
3126 enclosing type. Issue any appropriate messages and set appropriate
3127 flags. Returns false if an error has been diagnosed. */
3129 static bool
3130 check_bitfield_decl (tree field)
3132 tree type = TREE_TYPE (field);
3133 tree w;
3135 /* Extract the declared width of the bitfield, which has been
3136 temporarily stashed in DECL_INITIAL. */
3137 w = DECL_INITIAL (field);
3138 gcc_assert (w != NULL_TREE);
3139 /* Remove the bit-field width indicator so that the rest of the
3140 compiler does not treat that value as an initializer. */
3141 DECL_INITIAL (field) = NULL_TREE;
3143 /* Detect invalid bit-field type. */
3144 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3146 error ("bit-field %q+#D with non-integral type", field);
3147 w = error_mark_node;
3149 else
3151 location_t loc = input_location;
3152 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3153 STRIP_NOPS (w);
3155 /* detect invalid field size. */
3156 input_location = DECL_SOURCE_LOCATION (field);
3157 w = cxx_constant_value (w);
3158 input_location = loc;
3160 if (TREE_CODE (w) != INTEGER_CST)
3162 error ("bit-field %q+D width not an integer constant", field);
3163 w = error_mark_node;
3165 else if (tree_int_cst_sgn (w) < 0)
3167 error ("negative width in bit-field %q+D", field);
3168 w = error_mark_node;
3170 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3172 error ("zero width for bit-field %q+D", field);
3173 w = error_mark_node;
3175 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3176 && TREE_CODE (type) != BOOLEAN_TYPE
3177 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3178 || ((TREE_CODE (type) == ENUMERAL_TYPE
3179 || TREE_CODE (type) == BOOLEAN_TYPE)
3180 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3181 warning (0, "width of %q+D exceeds its type", field);
3182 else if (TREE_CODE (type) == ENUMERAL_TYPE
3183 && (0 > (compare_tree_int
3184 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3185 warning (0, "%q+D is too small to hold all values of %q#T", field, type);
3188 if (w != error_mark_node)
3190 DECL_SIZE (field) = convert (bitsizetype, w);
3191 DECL_BIT_FIELD (field) = 1;
3192 return true;
3194 else
3196 /* Non-bit-fields are aligned for their type. */
3197 DECL_BIT_FIELD (field) = 0;
3198 CLEAR_DECL_C_BIT_FIELD (field);
3199 return false;
3203 /* FIELD is a non bit-field. We are finishing the processing for its
3204 enclosing type T. Issue any appropriate messages and set appropriate
3205 flags. */
3207 static void
3208 check_field_decl (tree field,
3209 tree t,
3210 int* cant_have_const_ctor,
3211 int* no_const_asn_ref,
3212 int* any_default_members)
3214 tree type = strip_array_types (TREE_TYPE (field));
3216 /* In C++98 an anonymous union cannot contain any fields which would change
3217 the settings of CANT_HAVE_CONST_CTOR and friends. */
3218 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx0x)
3220 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3221 structs. So, we recurse through their fields here. */
3222 else if (ANON_AGGR_TYPE_P (type))
3224 tree fields;
3226 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
3227 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3228 check_field_decl (fields, t, cant_have_const_ctor,
3229 no_const_asn_ref, any_default_members);
3231 /* Check members with class type for constructors, destructors,
3232 etc. */
3233 else if (CLASS_TYPE_P (type))
3235 /* Never let anything with uninheritable virtuals
3236 make it through without complaint. */
3237 abstract_virtuals_error (field, type);
3239 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx0x)
3241 static bool warned;
3242 int oldcount = errorcount;
3243 if (TYPE_NEEDS_CONSTRUCTING (type))
3244 error ("member %q+#D with constructor not allowed in union",
3245 field);
3246 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3247 error ("member %q+#D with destructor not allowed in union", field);
3248 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3249 error ("member %q+#D with copy assignment operator not allowed in union",
3250 field);
3251 if (!warned && errorcount > oldcount)
3253 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3254 "only available with -std=c++11 or -std=gnu++11");
3255 warned = true;
3258 else
3260 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3261 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3262 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3263 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3264 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3265 || !TYPE_HAS_COPY_ASSIGN (type));
3266 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3267 || !TYPE_HAS_COPY_CTOR (type));
3268 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3269 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3270 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3271 || TYPE_HAS_COMPLEX_DFLT (type));
3274 if (TYPE_HAS_COPY_CTOR (type)
3275 && !TYPE_HAS_CONST_COPY_CTOR (type))
3276 *cant_have_const_ctor = 1;
3278 if (TYPE_HAS_COPY_ASSIGN (type)
3279 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3280 *no_const_asn_ref = 1;
3283 check_abi_tags (t, field);
3285 if (DECL_INITIAL (field) != NULL_TREE)
3287 /* `build_class_init_list' does not recognize
3288 non-FIELD_DECLs. */
3289 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
3290 error ("multiple fields in union %qT initialized", t);
3291 *any_default_members = 1;
3295 /* Check the data members (both static and non-static), class-scoped
3296 typedefs, etc., appearing in the declaration of T. Issue
3297 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3298 declaration order) of access declarations; each TREE_VALUE in this
3299 list is a USING_DECL.
3301 In addition, set the following flags:
3303 EMPTY_P
3304 The class is empty, i.e., contains no non-static data members.
3306 CANT_HAVE_CONST_CTOR_P
3307 This class cannot have an implicitly generated copy constructor
3308 taking a const reference.
3310 CANT_HAVE_CONST_ASN_REF
3311 This class cannot have an implicitly generated assignment
3312 operator taking a const reference.
3314 All of these flags should be initialized before calling this
3315 function.
3317 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3318 fields can be added by adding to this chain. */
3320 static void
3321 check_field_decls (tree t, tree *access_decls,
3322 int *cant_have_const_ctor_p,
3323 int *no_const_asn_ref_p)
3325 tree *field;
3326 tree *next;
3327 bool has_pointers;
3328 int any_default_members;
3329 int cant_pack = 0;
3330 int field_access = -1;
3332 /* Assume there are no access declarations. */
3333 *access_decls = NULL_TREE;
3334 /* Assume this class has no pointer members. */
3335 has_pointers = false;
3336 /* Assume none of the members of this class have default
3337 initializations. */
3338 any_default_members = 0;
3340 for (field = &TYPE_FIELDS (t); *field; field = next)
3342 tree x = *field;
3343 tree type = TREE_TYPE (x);
3344 int this_field_access;
3346 next = &DECL_CHAIN (x);
3348 if (TREE_CODE (x) == USING_DECL)
3350 /* Save the access declarations for our caller. */
3351 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3352 continue;
3355 if (TREE_CODE (x) == TYPE_DECL
3356 || TREE_CODE (x) == TEMPLATE_DECL)
3357 continue;
3359 /* If we've gotten this far, it's a data member, possibly static,
3360 or an enumerator. */
3361 if (TREE_CODE (x) != CONST_DECL)
3362 DECL_CONTEXT (x) = t;
3364 /* When this goes into scope, it will be a non-local reference. */
3365 DECL_NONLOCAL (x) = 1;
3367 if (TREE_CODE (t) == UNION_TYPE)
3369 /* [class.union]
3371 If a union contains a static data member, or a member of
3372 reference type, the program is ill-formed. */
3373 if (VAR_P (x))
3375 error ("%q+D may not be static because it is a member of a union", x);
3376 continue;
3378 if (TREE_CODE (type) == REFERENCE_TYPE)
3380 error ("%q+D may not have reference type %qT because"
3381 " it is a member of a union",
3382 x, type);
3383 continue;
3387 /* Perform error checking that did not get done in
3388 grokdeclarator. */
3389 if (TREE_CODE (type) == FUNCTION_TYPE)
3391 error ("field %q+D invalidly declared function type", x);
3392 type = build_pointer_type (type);
3393 TREE_TYPE (x) = type;
3395 else if (TREE_CODE (type) == METHOD_TYPE)
3397 error ("field %q+D invalidly declared method type", x);
3398 type = build_pointer_type (type);
3399 TREE_TYPE (x) = type;
3402 if (type == error_mark_node)
3403 continue;
3405 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3406 continue;
3408 /* Now it can only be a FIELD_DECL. */
3410 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3411 CLASSTYPE_NON_AGGREGATE (t) = 1;
3413 /* If at least one non-static data member is non-literal, the whole
3414 class becomes non-literal. Note: if the type is incomplete we
3415 will complain later on. */
3416 if (COMPLETE_TYPE_P (type) && !literal_type_p (type))
3417 CLASSTYPE_LITERAL_P (t) = false;
3419 /* A standard-layout class is a class that:
3421 has the same access control (Clause 11) for all non-static data members,
3422 ... */
3423 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3424 if (field_access == -1)
3425 field_access = this_field_access;
3426 else if (this_field_access != field_access)
3427 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3429 /* If this is of reference type, check if it needs an init. */
3430 if (TREE_CODE (type) == REFERENCE_TYPE)
3432 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3433 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3434 if (DECL_INITIAL (x) == NULL_TREE)
3435 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3437 /* ARM $12.6.2: [A member initializer list] (or, for an
3438 aggregate, initialization by a brace-enclosed list) is the
3439 only way to initialize nonstatic const and reference
3440 members. */
3441 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3442 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3445 type = strip_array_types (type);
3447 if (TYPE_PACKED (t))
3449 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3451 warning
3453 "ignoring packed attribute because of unpacked non-POD field %q+#D",
3455 cant_pack = 1;
3457 else if (DECL_C_BIT_FIELD (x)
3458 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3459 DECL_PACKED (x) = 1;
3462 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3463 /* We don't treat zero-width bitfields as making a class
3464 non-empty. */
3466 else
3468 /* The class is non-empty. */
3469 CLASSTYPE_EMPTY_P (t) = 0;
3470 /* The class is not even nearly empty. */
3471 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3472 /* If one of the data members contains an empty class,
3473 so does T. */
3474 if (CLASS_TYPE_P (type)
3475 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3476 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3479 /* This is used by -Weffc++ (see below). Warn only for pointers
3480 to members which might hold dynamic memory. So do not warn
3481 for pointers to functions or pointers to members. */
3482 if (TYPE_PTR_P (type)
3483 && !TYPE_PTRFN_P (type))
3484 has_pointers = true;
3486 if (CLASS_TYPE_P (type))
3488 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3489 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3490 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3491 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3494 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3495 CLASSTYPE_HAS_MUTABLE (t) = 1;
3497 if (! layout_pod_type_p (type))
3498 /* DR 148 now allows pointers to members (which are POD themselves),
3499 to be allowed in POD structs. */
3500 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3502 if (!std_layout_type_p (type))
3503 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3505 if (! zero_init_p (type))
3506 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3508 /* We set DECL_C_BIT_FIELD in grokbitfield.
3509 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3510 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3511 check_field_decl (x, t,
3512 cant_have_const_ctor_p,
3513 no_const_asn_ref_p,
3514 &any_default_members);
3516 /* Now that we've removed bit-field widths from DECL_INITIAL,
3517 anything left in DECL_INITIAL is an NSDMI that makes the class
3518 non-aggregate. */
3519 if (DECL_INITIAL (x))
3520 CLASSTYPE_NON_AGGREGATE (t) = true;
3522 /* If any field is const, the structure type is pseudo-const. */
3523 if (CP_TYPE_CONST_P (type))
3525 C_TYPE_FIELDS_READONLY (t) = 1;
3526 if (DECL_INITIAL (x) == NULL_TREE)
3527 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3529 /* ARM $12.6.2: [A member initializer list] (or, for an
3530 aggregate, initialization by a brace-enclosed list) is the
3531 only way to initialize nonstatic const and reference
3532 members. */
3533 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3534 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3536 /* A field that is pseudo-const makes the structure likewise. */
3537 else if (CLASS_TYPE_P (type))
3539 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3540 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3541 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3542 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3545 /* Core issue 80: A nonstatic data member is required to have a
3546 different name from the class iff the class has a
3547 user-declared constructor. */
3548 if (constructor_name_p (DECL_NAME (x), t)
3549 && TYPE_HAS_USER_CONSTRUCTOR (t))
3550 permerror (input_location, "field %q+#D with same name as class", x);
3553 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3554 it should also define a copy constructor and an assignment operator to
3555 implement the correct copy semantic (deep vs shallow, etc.). As it is
3556 not feasible to check whether the constructors do allocate dynamic memory
3557 and store it within members, we approximate the warning like this:
3559 -- Warn only if there are members which are pointers
3560 -- Warn only if there is a non-trivial constructor (otherwise,
3561 there cannot be memory allocated).
3562 -- Warn only if there is a non-trivial destructor. We assume that the
3563 user at least implemented the cleanup correctly, and a destructor
3564 is needed to free dynamic memory.
3566 This seems enough for practical purposes. */
3567 if (warn_ecpp
3568 && has_pointers
3569 && TYPE_HAS_USER_CONSTRUCTOR (t)
3570 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3571 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3573 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3575 if (! TYPE_HAS_COPY_CTOR (t))
3577 warning (OPT_Weffc__,
3578 " but does not override %<%T(const %T&)%>", t, t);
3579 if (!TYPE_HAS_COPY_ASSIGN (t))
3580 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3582 else if (! TYPE_HAS_COPY_ASSIGN (t))
3583 warning (OPT_Weffc__,
3584 " but does not override %<operator=(const %T&)%>", t);
3587 /* Non-static data member initializers make the default constructor
3588 non-trivial. */
3589 if (any_default_members)
3591 TYPE_NEEDS_CONSTRUCTING (t) = true;
3592 TYPE_HAS_COMPLEX_DFLT (t) = true;
3595 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3596 if (cant_pack)
3597 TYPE_PACKED (t) = 0;
3599 /* Check anonymous struct/anonymous union fields. */
3600 finish_struct_anon (t);
3602 /* We've built up the list of access declarations in reverse order.
3603 Fix that now. */
3604 *access_decls = nreverse (*access_decls);
3607 /* If TYPE is an empty class type, records its OFFSET in the table of
3608 OFFSETS. */
3610 static int
3611 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3613 splay_tree_node n;
3615 if (!is_empty_class (type))
3616 return 0;
3618 /* Record the location of this empty object in OFFSETS. */
3619 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3620 if (!n)
3621 n = splay_tree_insert (offsets,
3622 (splay_tree_key) offset,
3623 (splay_tree_value) NULL_TREE);
3624 n->value = ((splay_tree_value)
3625 tree_cons (NULL_TREE,
3626 type,
3627 (tree) n->value));
3629 return 0;
3632 /* Returns nonzero if TYPE is an empty class type and there is
3633 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3635 static int
3636 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3638 splay_tree_node n;
3639 tree t;
3641 if (!is_empty_class (type))
3642 return 0;
3644 /* Record the location of this empty object in OFFSETS. */
3645 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3646 if (!n)
3647 return 0;
3649 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3650 if (same_type_p (TREE_VALUE (t), type))
3651 return 1;
3653 return 0;
3656 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3657 F for every subobject, passing it the type, offset, and table of
3658 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3659 be traversed.
3661 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3662 than MAX_OFFSET will not be walked.
3664 If F returns a nonzero value, the traversal ceases, and that value
3665 is returned. Otherwise, returns zero. */
3667 static int
3668 walk_subobject_offsets (tree type,
3669 subobject_offset_fn f,
3670 tree offset,
3671 splay_tree offsets,
3672 tree max_offset,
3673 int vbases_p)
3675 int r = 0;
3676 tree type_binfo = NULL_TREE;
3678 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3679 stop. */
3680 if (max_offset && INT_CST_LT (max_offset, offset))
3681 return 0;
3683 if (type == error_mark_node)
3684 return 0;
3686 if (!TYPE_P (type))
3688 if (abi_version_at_least (2))
3689 type_binfo = type;
3690 type = BINFO_TYPE (type);
3693 if (CLASS_TYPE_P (type))
3695 tree field;
3696 tree binfo;
3697 int i;
3699 /* Avoid recursing into objects that are not interesting. */
3700 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3701 return 0;
3703 /* Record the location of TYPE. */
3704 r = (*f) (type, offset, offsets);
3705 if (r)
3706 return r;
3708 /* Iterate through the direct base classes of TYPE. */
3709 if (!type_binfo)
3710 type_binfo = TYPE_BINFO (type);
3711 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3713 tree binfo_offset;
3715 if (abi_version_at_least (2)
3716 && BINFO_VIRTUAL_P (binfo))
3717 continue;
3719 if (!vbases_p
3720 && BINFO_VIRTUAL_P (binfo)
3721 && !BINFO_PRIMARY_P (binfo))
3722 continue;
3724 if (!abi_version_at_least (2))
3725 binfo_offset = size_binop (PLUS_EXPR,
3726 offset,
3727 BINFO_OFFSET (binfo));
3728 else
3730 tree orig_binfo;
3731 /* We cannot rely on BINFO_OFFSET being set for the base
3732 class yet, but the offsets for direct non-virtual
3733 bases can be calculated by going back to the TYPE. */
3734 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3735 binfo_offset = size_binop (PLUS_EXPR,
3736 offset,
3737 BINFO_OFFSET (orig_binfo));
3740 r = walk_subobject_offsets (binfo,
3742 binfo_offset,
3743 offsets,
3744 max_offset,
3745 (abi_version_at_least (2)
3746 ? /*vbases_p=*/0 : vbases_p));
3747 if (r)
3748 return r;
3751 if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type))
3753 unsigned ix;
3754 vec<tree, va_gc> *vbases;
3756 /* Iterate through the virtual base classes of TYPE. In G++
3757 3.2, we included virtual bases in the direct base class
3758 loop above, which results in incorrect results; the
3759 correct offsets for virtual bases are only known when
3760 working with the most derived type. */
3761 if (vbases_p)
3762 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3763 vec_safe_iterate (vbases, ix, &binfo); ix++)
3765 r = walk_subobject_offsets (binfo,
3767 size_binop (PLUS_EXPR,
3768 offset,
3769 BINFO_OFFSET (binfo)),
3770 offsets,
3771 max_offset,
3772 /*vbases_p=*/0);
3773 if (r)
3774 return r;
3776 else
3778 /* We still have to walk the primary base, if it is
3779 virtual. (If it is non-virtual, then it was walked
3780 above.) */
3781 tree vbase = get_primary_binfo (type_binfo);
3783 if (vbase && BINFO_VIRTUAL_P (vbase)
3784 && BINFO_PRIMARY_P (vbase)
3785 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3787 r = (walk_subobject_offsets
3788 (vbase, f, offset,
3789 offsets, max_offset, /*vbases_p=*/0));
3790 if (r)
3791 return r;
3796 /* Iterate through the fields of TYPE. */
3797 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3798 if (TREE_CODE (field) == FIELD_DECL
3799 && TREE_TYPE (field) != error_mark_node
3800 && !DECL_ARTIFICIAL (field))
3802 tree field_offset;
3804 if (abi_version_at_least (2))
3805 field_offset = byte_position (field);
3806 else
3807 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3808 field_offset = DECL_FIELD_OFFSET (field);
3810 r = walk_subobject_offsets (TREE_TYPE (field),
3812 size_binop (PLUS_EXPR,
3813 offset,
3814 field_offset),
3815 offsets,
3816 max_offset,
3817 /*vbases_p=*/1);
3818 if (r)
3819 return r;
3822 else if (TREE_CODE (type) == ARRAY_TYPE)
3824 tree element_type = strip_array_types (type);
3825 tree domain = TYPE_DOMAIN (type);
3826 tree index;
3828 /* Avoid recursing into objects that are not interesting. */
3829 if (!CLASS_TYPE_P (element_type)
3830 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3831 return 0;
3833 /* Step through each of the elements in the array. */
3834 for (index = size_zero_node;
3835 /* G++ 3.2 had an off-by-one error here. */
3836 (abi_version_at_least (2)
3837 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3838 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3839 index = size_binop (PLUS_EXPR, index, size_one_node))
3841 r = walk_subobject_offsets (TREE_TYPE (type),
3843 offset,
3844 offsets,
3845 max_offset,
3846 /*vbases_p=*/1);
3847 if (r)
3848 return r;
3849 offset = size_binop (PLUS_EXPR, offset,
3850 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3851 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3852 there's no point in iterating through the remaining
3853 elements of the array. */
3854 if (max_offset && INT_CST_LT (max_offset, offset))
3855 break;
3859 return 0;
3862 /* Record all of the empty subobjects of TYPE (either a type or a
3863 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3864 is being placed at OFFSET; otherwise, it is a base class that is
3865 being placed at OFFSET. */
3867 static void
3868 record_subobject_offsets (tree type,
3869 tree offset,
3870 splay_tree offsets,
3871 bool is_data_member)
3873 tree max_offset;
3874 /* If recording subobjects for a non-static data member or a
3875 non-empty base class , we do not need to record offsets beyond
3876 the size of the biggest empty class. Additional data members
3877 will go at the end of the class. Additional base classes will go
3878 either at offset zero (if empty, in which case they cannot
3879 overlap with offsets past the size of the biggest empty class) or
3880 at the end of the class.
3882 However, if we are placing an empty base class, then we must record
3883 all offsets, as either the empty class is at offset zero (where
3884 other empty classes might later be placed) or at the end of the
3885 class (where other objects might then be placed, so other empty
3886 subobjects might later overlap). */
3887 if (is_data_member
3888 || !is_empty_class (BINFO_TYPE (type)))
3889 max_offset = sizeof_biggest_empty_class;
3890 else
3891 max_offset = NULL_TREE;
3892 walk_subobject_offsets (type, record_subobject_offset, offset,
3893 offsets, max_offset, is_data_member);
3896 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3897 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3898 virtual bases of TYPE are examined. */
3900 static int
3901 layout_conflict_p (tree type,
3902 tree offset,
3903 splay_tree offsets,
3904 int vbases_p)
3906 splay_tree_node max_node;
3908 /* Get the node in OFFSETS that indicates the maximum offset where
3909 an empty subobject is located. */
3910 max_node = splay_tree_max (offsets);
3911 /* If there aren't any empty subobjects, then there's no point in
3912 performing this check. */
3913 if (!max_node)
3914 return 0;
3916 return walk_subobject_offsets (type, check_subobject_offset, offset,
3917 offsets, (tree) (max_node->key),
3918 vbases_p);
3921 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3922 non-static data member of the type indicated by RLI. BINFO is the
3923 binfo corresponding to the base subobject, OFFSETS maps offsets to
3924 types already located at those offsets. This function determines
3925 the position of the DECL. */
3927 static void
3928 layout_nonempty_base_or_field (record_layout_info rli,
3929 tree decl,
3930 tree binfo,
3931 splay_tree offsets)
3933 tree offset = NULL_TREE;
3934 bool field_p;
3935 tree type;
3937 if (binfo)
3939 /* For the purposes of determining layout conflicts, we want to
3940 use the class type of BINFO; TREE_TYPE (DECL) will be the
3941 CLASSTYPE_AS_BASE version, which does not contain entries for
3942 zero-sized bases. */
3943 type = TREE_TYPE (binfo);
3944 field_p = false;
3946 else
3948 type = TREE_TYPE (decl);
3949 field_p = true;
3952 /* Try to place the field. It may take more than one try if we have
3953 a hard time placing the field without putting two objects of the
3954 same type at the same address. */
3955 while (1)
3957 struct record_layout_info_s old_rli = *rli;
3959 /* Place this field. */
3960 place_field (rli, decl);
3961 offset = byte_position (decl);
3963 /* We have to check to see whether or not there is already
3964 something of the same type at the offset we're about to use.
3965 For example, consider:
3967 struct S {};
3968 struct T : public S { int i; };
3969 struct U : public S, public T {};
3971 Here, we put S at offset zero in U. Then, we can't put T at
3972 offset zero -- its S component would be at the same address
3973 as the S we already allocated. So, we have to skip ahead.
3974 Since all data members, including those whose type is an
3975 empty class, have nonzero size, any overlap can happen only
3976 with a direct or indirect base-class -- it can't happen with
3977 a data member. */
3978 /* In a union, overlap is permitted; all members are placed at
3979 offset zero. */
3980 if (TREE_CODE (rli->t) == UNION_TYPE)
3981 break;
3982 /* G++ 3.2 did not check for overlaps when placing a non-empty
3983 virtual base. */
3984 if (!abi_version_at_least (2) && binfo && BINFO_VIRTUAL_P (binfo))
3985 break;
3986 if (layout_conflict_p (field_p ? type : binfo, offset,
3987 offsets, field_p))
3989 /* Strip off the size allocated to this field. That puts us
3990 at the first place we could have put the field with
3991 proper alignment. */
3992 *rli = old_rli;
3994 /* Bump up by the alignment required for the type. */
3995 rli->bitpos
3996 = size_binop (PLUS_EXPR, rli->bitpos,
3997 bitsize_int (binfo
3998 ? CLASSTYPE_ALIGN (type)
3999 : TYPE_ALIGN (type)));
4000 normalize_rli (rli);
4002 else
4003 /* There was no conflict. We're done laying out this field. */
4004 break;
4007 /* Now that we know where it will be placed, update its
4008 BINFO_OFFSET. */
4009 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4010 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4011 this point because their BINFO_OFFSET is copied from another
4012 hierarchy. Therefore, we may not need to add the entire
4013 OFFSET. */
4014 propagate_binfo_offsets (binfo,
4015 size_diffop_loc (input_location,
4016 convert (ssizetype, offset),
4017 convert (ssizetype,
4018 BINFO_OFFSET (binfo))));
4021 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4023 static int
4024 empty_base_at_nonzero_offset_p (tree type,
4025 tree offset,
4026 splay_tree /*offsets*/)
4028 return is_empty_class (type) && !integer_zerop (offset);
4031 /* Layout the empty base BINFO. EOC indicates the byte currently just
4032 past the end of the class, and should be correctly aligned for a
4033 class of the type indicated by BINFO; OFFSETS gives the offsets of
4034 the empty bases allocated so far. T is the most derived
4035 type. Return nonzero iff we added it at the end. */
4037 static bool
4038 layout_empty_base (record_layout_info rli, tree binfo,
4039 tree eoc, splay_tree offsets)
4041 tree alignment;
4042 tree basetype = BINFO_TYPE (binfo);
4043 bool atend = false;
4045 /* This routine should only be used for empty classes. */
4046 gcc_assert (is_empty_class (basetype));
4047 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4049 if (!integer_zerop (BINFO_OFFSET (binfo)))
4051 if (abi_version_at_least (2))
4052 propagate_binfo_offsets
4053 (binfo, size_diffop_loc (input_location,
4054 size_zero_node, BINFO_OFFSET (binfo)));
4055 else
4056 warning (OPT_Wabi,
4057 "offset of empty base %qT may not be ABI-compliant and may"
4058 "change in a future version of GCC",
4059 BINFO_TYPE (binfo));
4062 /* This is an empty base class. We first try to put it at offset
4063 zero. */
4064 if (layout_conflict_p (binfo,
4065 BINFO_OFFSET (binfo),
4066 offsets,
4067 /*vbases_p=*/0))
4069 /* That didn't work. Now, we move forward from the next
4070 available spot in the class. */
4071 atend = true;
4072 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
4073 while (1)
4075 if (!layout_conflict_p (binfo,
4076 BINFO_OFFSET (binfo),
4077 offsets,
4078 /*vbases_p=*/0))
4079 /* We finally found a spot where there's no overlap. */
4080 break;
4082 /* There's overlap here, too. Bump along to the next spot. */
4083 propagate_binfo_offsets (binfo, alignment);
4087 if (CLASSTYPE_USER_ALIGN (basetype))
4089 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4090 if (warn_packed)
4091 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4092 TYPE_USER_ALIGN (rli->t) = 1;
4095 return atend;
4098 /* Layout the base given by BINFO in the class indicated by RLI.
4099 *BASE_ALIGN is a running maximum of the alignments of
4100 any base class. OFFSETS gives the location of empty base
4101 subobjects. T is the most derived type. Return nonzero if the new
4102 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4103 *NEXT_FIELD, unless BINFO is for an empty base class.
4105 Returns the location at which the next field should be inserted. */
4107 static tree *
4108 build_base_field (record_layout_info rli, tree binfo,
4109 splay_tree offsets, tree *next_field)
4111 tree t = rli->t;
4112 tree basetype = BINFO_TYPE (binfo);
4114 if (!COMPLETE_TYPE_P (basetype))
4115 /* This error is now reported in xref_tag, thus giving better
4116 location information. */
4117 return next_field;
4119 /* Place the base class. */
4120 if (!is_empty_class (basetype))
4122 tree decl;
4124 /* The containing class is non-empty because it has a non-empty
4125 base class. */
4126 CLASSTYPE_EMPTY_P (t) = 0;
4128 /* Create the FIELD_DECL. */
4129 decl = build_decl (input_location,
4130 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4131 DECL_ARTIFICIAL (decl) = 1;
4132 DECL_IGNORED_P (decl) = 1;
4133 DECL_FIELD_CONTEXT (decl) = t;
4134 if (CLASSTYPE_AS_BASE (basetype))
4136 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4137 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4138 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4139 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4140 DECL_MODE (decl) = TYPE_MODE (basetype);
4141 DECL_FIELD_IS_BASE (decl) = 1;
4143 /* Try to place the field. It may take more than one try if we
4144 have a hard time placing the field without putting two
4145 objects of the same type at the same address. */
4146 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4147 /* Add the new FIELD_DECL to the list of fields for T. */
4148 DECL_CHAIN (decl) = *next_field;
4149 *next_field = decl;
4150 next_field = &DECL_CHAIN (decl);
4153 else
4155 tree eoc;
4156 bool atend;
4158 /* On some platforms (ARM), even empty classes will not be
4159 byte-aligned. */
4160 eoc = round_up_loc (input_location,
4161 rli_size_unit_so_far (rli),
4162 CLASSTYPE_ALIGN_UNIT (basetype));
4163 atend = layout_empty_base (rli, binfo, eoc, offsets);
4164 /* A nearly-empty class "has no proper base class that is empty,
4165 not morally virtual, and at an offset other than zero." */
4166 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4168 if (atend)
4169 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4170 /* The check above (used in G++ 3.2) is insufficient because
4171 an empty class placed at offset zero might itself have an
4172 empty base at a nonzero offset. */
4173 else if (walk_subobject_offsets (basetype,
4174 empty_base_at_nonzero_offset_p,
4175 size_zero_node,
4176 /*offsets=*/NULL,
4177 /*max_offset=*/NULL_TREE,
4178 /*vbases_p=*/true))
4180 if (abi_version_at_least (2))
4181 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4182 else
4183 warning (OPT_Wabi,
4184 "class %qT will be considered nearly empty in a "
4185 "future version of GCC", t);
4189 /* We do not create a FIELD_DECL for empty base classes because
4190 it might overlap some other field. We want to be able to
4191 create CONSTRUCTORs for the class by iterating over the
4192 FIELD_DECLs, and the back end does not handle overlapping
4193 FIELD_DECLs. */
4195 /* An empty virtual base causes a class to be non-empty
4196 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4197 here because that was already done when the virtual table
4198 pointer was created. */
4201 /* Record the offsets of BINFO and its base subobjects. */
4202 record_subobject_offsets (binfo,
4203 BINFO_OFFSET (binfo),
4204 offsets,
4205 /*is_data_member=*/false);
4207 return next_field;
4210 /* Layout all of the non-virtual base classes. Record empty
4211 subobjects in OFFSETS. T is the most derived type. Return nonzero
4212 if the type cannot be nearly empty. The fields created
4213 corresponding to the base classes will be inserted at
4214 *NEXT_FIELD. */
4216 static void
4217 build_base_fields (record_layout_info rli,
4218 splay_tree offsets, tree *next_field)
4220 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4221 subobjects. */
4222 tree t = rli->t;
4223 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4224 int i;
4226 /* The primary base class is always allocated first. */
4227 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4228 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4229 offsets, next_field);
4231 /* Now allocate the rest of the bases. */
4232 for (i = 0; i < n_baseclasses; ++i)
4234 tree base_binfo;
4236 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4238 /* The primary base was already allocated above, so we don't
4239 need to allocate it again here. */
4240 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4241 continue;
4243 /* Virtual bases are added at the end (a primary virtual base
4244 will have already been added). */
4245 if (BINFO_VIRTUAL_P (base_binfo))
4246 continue;
4248 next_field = build_base_field (rli, base_binfo,
4249 offsets, next_field);
4253 /* Go through the TYPE_METHODS of T issuing any appropriate
4254 diagnostics, figuring out which methods override which other
4255 methods, and so forth. */
4257 static void
4258 check_methods (tree t)
4260 tree x;
4262 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4264 check_for_override (x, t);
4265 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4266 error ("initializer specified for non-virtual method %q+D", x);
4267 /* The name of the field is the original field name
4268 Save this in auxiliary field for later overloading. */
4269 if (DECL_VINDEX (x))
4271 TYPE_POLYMORPHIC_P (t) = 1;
4272 if (DECL_PURE_VIRTUAL_P (x))
4273 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4275 /* All user-provided destructors are non-trivial.
4276 Constructors and assignment ops are handled in
4277 grok_special_member_properties. */
4278 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4279 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4283 /* FN is a constructor or destructor. Clone the declaration to create
4284 a specialized in-charge or not-in-charge version, as indicated by
4285 NAME. */
4287 static tree
4288 build_clone (tree fn, tree name)
4290 tree parms;
4291 tree clone;
4293 /* Copy the function. */
4294 clone = copy_decl (fn);
4295 /* Reset the function name. */
4296 DECL_NAME (clone) = name;
4297 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4298 /* Remember where this function came from. */
4299 DECL_ABSTRACT_ORIGIN (clone) = fn;
4300 /* Make it easy to find the CLONE given the FN. */
4301 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4302 DECL_CHAIN (fn) = clone;
4304 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4305 if (TREE_CODE (clone) == TEMPLATE_DECL)
4307 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4308 DECL_TEMPLATE_RESULT (clone) = result;
4309 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4310 DECL_TI_TEMPLATE (result) = clone;
4311 TREE_TYPE (clone) = TREE_TYPE (result);
4312 return clone;
4315 DECL_CLONED_FUNCTION (clone) = fn;
4316 /* There's no pending inline data for this function. */
4317 DECL_PENDING_INLINE_INFO (clone) = NULL;
4318 DECL_PENDING_INLINE_P (clone) = 0;
4320 /* The base-class destructor is not virtual. */
4321 if (name == base_dtor_identifier)
4323 DECL_VIRTUAL_P (clone) = 0;
4324 if (TREE_CODE (clone) != TEMPLATE_DECL)
4325 DECL_VINDEX (clone) = NULL_TREE;
4328 /* If there was an in-charge parameter, drop it from the function
4329 type. */
4330 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4332 tree basetype;
4333 tree parmtypes;
4334 tree exceptions;
4336 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4337 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4338 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4339 /* Skip the `this' parameter. */
4340 parmtypes = TREE_CHAIN (parmtypes);
4341 /* Skip the in-charge parameter. */
4342 parmtypes = TREE_CHAIN (parmtypes);
4343 /* And the VTT parm, in a complete [cd]tor. */
4344 if (DECL_HAS_VTT_PARM_P (fn)
4345 && ! DECL_NEEDS_VTT_PARM_P (clone))
4346 parmtypes = TREE_CHAIN (parmtypes);
4347 /* If this is subobject constructor or destructor, add the vtt
4348 parameter. */
4349 TREE_TYPE (clone)
4350 = build_method_type_directly (basetype,
4351 TREE_TYPE (TREE_TYPE (clone)),
4352 parmtypes);
4353 if (exceptions)
4354 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4355 exceptions);
4356 TREE_TYPE (clone)
4357 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4358 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4361 /* Copy the function parameters. */
4362 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4363 /* Remove the in-charge parameter. */
4364 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4366 DECL_CHAIN (DECL_ARGUMENTS (clone))
4367 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4368 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4370 /* And the VTT parm, in a complete [cd]tor. */
4371 if (DECL_HAS_VTT_PARM_P (fn))
4373 if (DECL_NEEDS_VTT_PARM_P (clone))
4374 DECL_HAS_VTT_PARM_P (clone) = 1;
4375 else
4377 DECL_CHAIN (DECL_ARGUMENTS (clone))
4378 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4379 DECL_HAS_VTT_PARM_P (clone) = 0;
4383 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4385 DECL_CONTEXT (parms) = clone;
4386 cxx_dup_lang_specific_decl (parms);
4389 /* Create the RTL for this function. */
4390 SET_DECL_RTL (clone, NULL);
4391 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4393 if (pch_file)
4394 note_decl_for_pch (clone);
4396 return clone;
4399 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4400 not invoke this function directly.
4402 For a non-thunk function, returns the address of the slot for storing
4403 the function it is a clone of. Otherwise returns NULL_TREE.
4405 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4406 cloned_function is unset. This is to support the separate
4407 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4408 on a template makes sense, but not the former. */
4410 tree *
4411 decl_cloned_function_p (const_tree decl, bool just_testing)
4413 tree *ptr;
4414 if (just_testing)
4415 decl = STRIP_TEMPLATE (decl);
4417 if (TREE_CODE (decl) != FUNCTION_DECL
4418 || !DECL_LANG_SPECIFIC (decl)
4419 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4421 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4422 if (!just_testing)
4423 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4424 else
4425 #endif
4426 return NULL;
4429 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4430 if (just_testing && *ptr == NULL_TREE)
4431 return NULL;
4432 else
4433 return ptr;
4436 /* Produce declarations for all appropriate clones of FN. If
4437 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4438 CLASTYPE_METHOD_VEC as well. */
4440 void
4441 clone_function_decl (tree fn, int update_method_vec_p)
4443 tree clone;
4445 /* Avoid inappropriate cloning. */
4446 if (DECL_CHAIN (fn)
4447 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4448 return;
4450 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4452 /* For each constructor, we need two variants: an in-charge version
4453 and a not-in-charge version. */
4454 clone = build_clone (fn, complete_ctor_identifier);
4455 if (update_method_vec_p)
4456 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4457 clone = build_clone (fn, base_ctor_identifier);
4458 if (update_method_vec_p)
4459 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4461 else
4463 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4465 /* For each destructor, we need three variants: an in-charge
4466 version, a not-in-charge version, and an in-charge deleting
4467 version. We clone the deleting version first because that
4468 means it will go second on the TYPE_METHODS list -- and that
4469 corresponds to the correct layout order in the virtual
4470 function table.
4472 For a non-virtual destructor, we do not build a deleting
4473 destructor. */
4474 if (DECL_VIRTUAL_P (fn))
4476 clone = build_clone (fn, deleting_dtor_identifier);
4477 if (update_method_vec_p)
4478 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4480 clone = build_clone (fn, complete_dtor_identifier);
4481 if (update_method_vec_p)
4482 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4483 clone = build_clone (fn, base_dtor_identifier);
4484 if (update_method_vec_p)
4485 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4488 /* Note that this is an abstract function that is never emitted. */
4489 DECL_ABSTRACT (fn) = 1;
4492 /* DECL is an in charge constructor, which is being defined. This will
4493 have had an in class declaration, from whence clones were
4494 declared. An out-of-class definition can specify additional default
4495 arguments. As it is the clones that are involved in overload
4496 resolution, we must propagate the information from the DECL to its
4497 clones. */
4499 void
4500 adjust_clone_args (tree decl)
4502 tree clone;
4504 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4505 clone = DECL_CHAIN (clone))
4507 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4508 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4509 tree decl_parms, clone_parms;
4511 clone_parms = orig_clone_parms;
4513 /* Skip the 'this' parameter. */
4514 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4515 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4517 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4518 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4519 if (DECL_HAS_VTT_PARM_P (decl))
4520 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4522 clone_parms = orig_clone_parms;
4523 if (DECL_HAS_VTT_PARM_P (clone))
4524 clone_parms = TREE_CHAIN (clone_parms);
4526 for (decl_parms = orig_decl_parms; decl_parms;
4527 decl_parms = TREE_CHAIN (decl_parms),
4528 clone_parms = TREE_CHAIN (clone_parms))
4530 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4531 TREE_TYPE (clone_parms)));
4533 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4535 /* A default parameter has been added. Adjust the
4536 clone's parameters. */
4537 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4538 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4539 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4540 tree type;
4542 clone_parms = orig_decl_parms;
4544 if (DECL_HAS_VTT_PARM_P (clone))
4546 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4547 TREE_VALUE (orig_clone_parms),
4548 clone_parms);
4549 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4551 type = build_method_type_directly (basetype,
4552 TREE_TYPE (TREE_TYPE (clone)),
4553 clone_parms);
4554 if (exceptions)
4555 type = build_exception_variant (type, exceptions);
4556 if (attrs)
4557 type = cp_build_type_attribute_variant (type, attrs);
4558 TREE_TYPE (clone) = type;
4560 clone_parms = NULL_TREE;
4561 break;
4564 gcc_assert (!clone_parms);
4568 /* For each of the constructors and destructors in T, create an
4569 in-charge and not-in-charge variant. */
4571 static void
4572 clone_constructors_and_destructors (tree t)
4574 tree fns;
4576 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4577 out now. */
4578 if (!CLASSTYPE_METHOD_VEC (t))
4579 return;
4581 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4582 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4583 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4584 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4587 /* Deduce noexcept for a destructor DTOR. */
4589 void
4590 deduce_noexcept_on_destructor (tree dtor)
4592 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4594 tree ctx = DECL_CONTEXT (dtor);
4595 tree implicit_fn = implicitly_declare_fn (sfk_destructor, ctx,
4596 /*const_p=*/false,
4597 NULL, NULL);
4598 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
4599 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4603 /* For each destructor in T, deduce noexcept:
4605 12.4/3: A declaration of a destructor that does not have an
4606 exception-specification is implicitly considered to have the
4607 same exception-specification as an implicit declaration (15.4). */
4609 static void
4610 deduce_noexcept_on_destructors (tree t)
4612 tree fns;
4614 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4615 out now. */
4616 if (!CLASSTYPE_METHOD_VEC (t))
4617 return;
4619 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4620 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4623 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4624 of TYPE for virtual functions which FNDECL overrides. Return a
4625 mask of the tm attributes found therein. */
4627 static int
4628 look_for_tm_attr_overrides (tree type, tree fndecl)
4630 tree binfo = TYPE_BINFO (type);
4631 tree base_binfo;
4632 int ix, found = 0;
4634 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4636 tree o, basetype = BINFO_TYPE (base_binfo);
4638 if (!TYPE_POLYMORPHIC_P (basetype))
4639 continue;
4641 o = look_for_overrides_here (basetype, fndecl);
4642 if (o)
4643 found |= tm_attr_to_mask (find_tm_attribute
4644 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4645 else
4646 found |= look_for_tm_attr_overrides (basetype, fndecl);
4649 return found;
4652 /* Subroutine of set_method_tm_attributes. Handle the checks and
4653 inheritance for one virtual method FNDECL. */
4655 static void
4656 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4658 tree tm_attr;
4659 int found, have;
4661 found = look_for_tm_attr_overrides (type, fndecl);
4663 /* If FNDECL doesn't actually override anything (i.e. T is the
4664 class that first declares FNDECL virtual), then we're done. */
4665 if (found == 0)
4666 return;
4668 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4669 have = tm_attr_to_mask (tm_attr);
4671 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4672 tm_pure must match exactly, otherwise no weakening of
4673 tm_safe > tm_callable > nothing. */
4674 /* ??? The tm_pure attribute didn't make the transition to the
4675 multivendor language spec. */
4676 if (have == TM_ATTR_PURE)
4678 if (found != TM_ATTR_PURE)
4680 found &= -found;
4681 goto err_override;
4684 /* If the overridden function is tm_pure, then FNDECL must be. */
4685 else if (found == TM_ATTR_PURE && tm_attr)
4686 goto err_override;
4687 /* Look for base class combinations that cannot be satisfied. */
4688 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4690 found &= ~TM_ATTR_PURE;
4691 found &= -found;
4692 error_at (DECL_SOURCE_LOCATION (fndecl),
4693 "method overrides both %<transaction_pure%> and %qE methods",
4694 tm_mask_to_attr (found));
4696 /* If FNDECL did not declare an attribute, then inherit the most
4697 restrictive one. */
4698 else if (tm_attr == NULL)
4700 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
4702 /* Otherwise validate that we're not weaker than a function
4703 that is being overridden. */
4704 else
4706 found &= -found;
4707 if (found <= TM_ATTR_CALLABLE && have > found)
4708 goto err_override;
4710 return;
4712 err_override:
4713 error_at (DECL_SOURCE_LOCATION (fndecl),
4714 "method declared %qE overriding %qE method",
4715 tm_attr, tm_mask_to_attr (found));
4718 /* For each of the methods in T, propagate a class-level tm attribute. */
4720 static void
4721 set_method_tm_attributes (tree t)
4723 tree class_tm_attr, fndecl;
4725 /* Don't bother collecting tm attributes if transactional memory
4726 support is not enabled. */
4727 if (!flag_tm)
4728 return;
4730 /* Process virtual methods first, as they inherit directly from the
4731 base virtual function and also require validation of new attributes. */
4732 if (TYPE_CONTAINS_VPTR_P (t))
4734 tree vchain;
4735 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4736 vchain = TREE_CHAIN (vchain))
4738 fndecl = BV_FN (vchain);
4739 if (DECL_THUNK_P (fndecl))
4740 fndecl = THUNK_TARGET (fndecl);
4741 set_one_vmethod_tm_attributes (t, fndecl);
4745 /* If the class doesn't have an attribute, nothing more to do. */
4746 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4747 if (class_tm_attr == NULL)
4748 return;
4750 /* Any method that does not yet have a tm attribute inherits
4751 the one from the class. */
4752 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
4754 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4755 apply_tm_attr (fndecl, class_tm_attr);
4759 /* Returns true iff class T has a user-defined constructor other than
4760 the default constructor. */
4762 bool
4763 type_has_user_nondefault_constructor (tree t)
4765 tree fns;
4767 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4768 return false;
4770 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4772 tree fn = OVL_CURRENT (fns);
4773 if (!DECL_ARTIFICIAL (fn)
4774 && (TREE_CODE (fn) == TEMPLATE_DECL
4775 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4776 != NULL_TREE)))
4777 return true;
4780 return false;
4783 /* Returns the defaulted constructor if T has one. Otherwise, returns
4784 NULL_TREE. */
4786 tree
4787 in_class_defaulted_default_constructor (tree t)
4789 tree fns, args;
4791 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4792 return NULL_TREE;
4794 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4796 tree fn = OVL_CURRENT (fns);
4798 if (DECL_DEFAULTED_IN_CLASS_P (fn))
4800 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
4801 while (args && TREE_PURPOSE (args))
4802 args = TREE_CHAIN (args);
4803 if (!args || args == void_list_node)
4804 return fn;
4808 return NULL_TREE;
4811 /* Returns true iff FN is a user-provided function, i.e. user-declared
4812 and not defaulted at its first declaration; or explicit, private,
4813 protected, or non-const. */
4815 bool
4816 user_provided_p (tree fn)
4818 if (TREE_CODE (fn) == TEMPLATE_DECL)
4819 return true;
4820 else
4821 return (!DECL_ARTIFICIAL (fn)
4822 && !DECL_DEFAULTED_IN_CLASS_P (fn));
4825 /* Returns true iff class T has a user-provided constructor. */
4827 bool
4828 type_has_user_provided_constructor (tree t)
4830 tree fns;
4832 if (!CLASS_TYPE_P (t))
4833 return false;
4835 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4836 return false;
4838 /* This can happen in error cases; avoid crashing. */
4839 if (!CLASSTYPE_METHOD_VEC (t))
4840 return false;
4842 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4843 if (user_provided_p (OVL_CURRENT (fns)))
4844 return true;
4846 return false;
4849 /* Returns true iff class T has a user-provided default constructor. */
4851 bool
4852 type_has_user_provided_default_constructor (tree t)
4854 tree fns;
4856 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4857 return false;
4859 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4861 tree fn = OVL_CURRENT (fns);
4862 if (TREE_CODE (fn) == FUNCTION_DECL
4863 && user_provided_p (fn)
4864 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
4865 return true;
4868 return false;
4871 /* TYPE is being used as a virtual base, and has a non-trivial move
4872 assignment. Return true if this is due to there being a user-provided
4873 move assignment in TYPE or one of its subobjects; if there isn't, then
4874 multiple move assignment can't cause any harm. */
4876 bool
4877 vbase_has_user_provided_move_assign (tree type)
4879 /* Does the type itself have a user-provided move assignment operator? */
4880 for (tree fns
4881 = lookup_fnfields_slot_nolazy (type, ansi_assopname (NOP_EXPR));
4882 fns; fns = OVL_NEXT (fns))
4884 tree fn = OVL_CURRENT (fns);
4885 if (move_fn_p (fn) && user_provided_p (fn))
4886 return true;
4889 /* Do any of its bases? */
4890 tree binfo = TYPE_BINFO (type);
4891 tree base_binfo;
4892 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
4893 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
4894 return true;
4896 /* Or non-static data members? */
4897 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4899 if (TREE_CODE (field) == FIELD_DECL
4900 && CLASS_TYPE_P (TREE_TYPE (field))
4901 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
4902 return true;
4905 /* Seems not. */
4906 return false;
4909 /* If default-initialization leaves part of TYPE uninitialized, returns
4910 a DECL for the field or TYPE itself (DR 253). */
4912 tree
4913 default_init_uninitialized_part (tree type)
4915 tree t, r, binfo;
4916 int i;
4918 type = strip_array_types (type);
4919 if (!CLASS_TYPE_P (type))
4920 return type;
4921 if (type_has_user_provided_default_constructor (type))
4922 return NULL_TREE;
4923 for (binfo = TYPE_BINFO (type), i = 0;
4924 BINFO_BASE_ITERATE (binfo, i, t); ++i)
4926 r = default_init_uninitialized_part (BINFO_TYPE (t));
4927 if (r)
4928 return r;
4930 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
4931 if (TREE_CODE (t) == FIELD_DECL
4932 && !DECL_ARTIFICIAL (t)
4933 && !DECL_INITIAL (t))
4935 r = default_init_uninitialized_part (TREE_TYPE (t));
4936 if (r)
4937 return DECL_P (r) ? r : t;
4940 return NULL_TREE;
4943 /* Returns true iff for class T, a trivial synthesized default constructor
4944 would be constexpr. */
4946 bool
4947 trivial_default_constructor_is_constexpr (tree t)
4949 /* A defaulted trivial default constructor is constexpr
4950 if there is nothing to initialize. */
4951 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
4952 return is_really_empty_class (t);
4955 /* Returns true iff class T has a constexpr default constructor. */
4957 bool
4958 type_has_constexpr_default_constructor (tree t)
4960 tree fns;
4962 if (!CLASS_TYPE_P (t))
4964 /* The caller should have stripped an enclosing array. */
4965 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
4966 return false;
4968 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
4970 if (!TYPE_HAS_COMPLEX_DFLT (t))
4971 return trivial_default_constructor_is_constexpr (t);
4972 /* Non-trivial, we need to check subobject constructors. */
4973 lazily_declare_fn (sfk_constructor, t);
4975 fns = locate_ctor (t);
4976 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
4979 /* Returns true iff class TYPE has a virtual destructor. */
4981 bool
4982 type_has_virtual_destructor (tree type)
4984 tree dtor;
4986 if (!CLASS_TYPE_P (type))
4987 return false;
4989 gcc_assert (COMPLETE_TYPE_P (type));
4990 dtor = CLASSTYPE_DESTRUCTORS (type);
4991 return (dtor && DECL_VIRTUAL_P (dtor));
4994 /* Returns true iff class T has a move constructor. */
4996 bool
4997 type_has_move_constructor (tree t)
4999 tree fns;
5001 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5003 gcc_assert (COMPLETE_TYPE_P (t));
5004 lazily_declare_fn (sfk_move_constructor, t);
5007 if (!CLASSTYPE_METHOD_VEC (t))
5008 return false;
5010 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5011 if (move_fn_p (OVL_CURRENT (fns)))
5012 return true;
5014 return false;
5017 /* Returns true iff class T has a move assignment operator. */
5019 bool
5020 type_has_move_assign (tree t)
5022 tree fns;
5024 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5026 gcc_assert (COMPLETE_TYPE_P (t));
5027 lazily_declare_fn (sfk_move_assignment, t);
5030 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5031 fns; fns = OVL_NEXT (fns))
5032 if (move_fn_p (OVL_CURRENT (fns)))
5033 return true;
5035 return false;
5038 /* Returns true iff class T has a move constructor that was explicitly
5039 declared in the class body. Note that this is different from
5040 "user-provided", which doesn't include functions that are defaulted in
5041 the class. */
5043 bool
5044 type_has_user_declared_move_constructor (tree t)
5046 tree fns;
5048 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5049 return false;
5051 if (!CLASSTYPE_METHOD_VEC (t))
5052 return false;
5054 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5056 tree fn = OVL_CURRENT (fns);
5057 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5058 return true;
5061 return false;
5064 /* Returns true iff class T has a move assignment operator that was
5065 explicitly declared in the class body. */
5067 bool
5068 type_has_user_declared_move_assign (tree t)
5070 tree fns;
5072 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5073 return false;
5075 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5076 fns; fns = OVL_NEXT (fns))
5078 tree fn = OVL_CURRENT (fns);
5079 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5080 return true;
5083 return false;
5086 /* Nonzero if we need to build up a constructor call when initializing an
5087 object of this class, either because it has a user-provided constructor
5088 or because it doesn't have a default constructor (so we need to give an
5089 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5090 what you care about is whether or not an object can be produced by a
5091 constructor (e.g. so we don't set TREE_READONLY on const variables of
5092 such type); use this function when what you care about is whether or not
5093 to try to call a constructor to create an object. The latter case is
5094 the former plus some cases of constructors that cannot be called. */
5096 bool
5097 type_build_ctor_call (tree t)
5099 tree inner;
5100 if (TYPE_NEEDS_CONSTRUCTING (t))
5101 return true;
5102 inner = strip_array_types (t);
5103 return (CLASS_TYPE_P (inner) && !TYPE_HAS_DEFAULT_CONSTRUCTOR (inner)
5104 && !ANON_AGGR_TYPE_P (inner));
5107 /* Remove all zero-width bit-fields from T. */
5109 static void
5110 remove_zero_width_bit_fields (tree t)
5112 tree *fieldsp;
5114 fieldsp = &TYPE_FIELDS (t);
5115 while (*fieldsp)
5117 if (TREE_CODE (*fieldsp) == FIELD_DECL
5118 && DECL_C_BIT_FIELD (*fieldsp)
5119 /* We should not be confused by the fact that grokbitfield
5120 temporarily sets the width of the bit field into
5121 DECL_INITIAL (*fieldsp).
5122 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5123 to that width. */
5124 && integer_zerop (DECL_SIZE (*fieldsp)))
5125 *fieldsp = DECL_CHAIN (*fieldsp);
5126 else
5127 fieldsp = &DECL_CHAIN (*fieldsp);
5131 /* Returns TRUE iff we need a cookie when dynamically allocating an
5132 array whose elements have the indicated class TYPE. */
5134 static bool
5135 type_requires_array_cookie (tree type)
5137 tree fns;
5138 bool has_two_argument_delete_p = false;
5140 gcc_assert (CLASS_TYPE_P (type));
5142 /* If there's a non-trivial destructor, we need a cookie. In order
5143 to iterate through the array calling the destructor for each
5144 element, we'll have to know how many elements there are. */
5145 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5146 return true;
5148 /* If the usual deallocation function is a two-argument whose second
5149 argument is of type `size_t', then we have to pass the size of
5150 the array to the deallocation function, so we will need to store
5151 a cookie. */
5152 fns = lookup_fnfields (TYPE_BINFO (type),
5153 ansi_opname (VEC_DELETE_EXPR),
5154 /*protect=*/0);
5155 /* If there are no `operator []' members, or the lookup is
5156 ambiguous, then we don't need a cookie. */
5157 if (!fns || fns == error_mark_node)
5158 return false;
5159 /* Loop through all of the functions. */
5160 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
5162 tree fn;
5163 tree second_parm;
5165 /* Select the current function. */
5166 fn = OVL_CURRENT (fns);
5167 /* See if this function is a one-argument delete function. If
5168 it is, then it will be the usual deallocation function. */
5169 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5170 if (second_parm == void_list_node)
5171 return false;
5172 /* Do not consider this function if its second argument is an
5173 ellipsis. */
5174 if (!second_parm)
5175 continue;
5176 /* Otherwise, if we have a two-argument function and the second
5177 argument is `size_t', it will be the usual deallocation
5178 function -- unless there is one-argument function, too. */
5179 if (TREE_CHAIN (second_parm) == void_list_node
5180 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5181 has_two_argument_delete_p = true;
5184 return has_two_argument_delete_p;
5187 /* Finish computing the `literal type' property of class type T.
5189 At this point, we have already processed base classes and
5190 non-static data members. We need to check whether the copy
5191 constructor is trivial, the destructor is trivial, and there
5192 is a trivial default constructor or at least one constexpr
5193 constructor other than the copy constructor. */
5195 static void
5196 finalize_literal_type_property (tree t)
5198 tree fn;
5200 if (cxx_dialect < cxx0x
5201 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5202 CLASSTYPE_LITERAL_P (t) = false;
5203 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5204 && CLASSTYPE_NON_AGGREGATE (t)
5205 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5206 CLASSTYPE_LITERAL_P (t) = false;
5208 if (!CLASSTYPE_LITERAL_P (t))
5209 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5210 if (DECL_DECLARED_CONSTEXPR_P (fn)
5211 && TREE_CODE (fn) != TEMPLATE_DECL
5212 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5213 && !DECL_CONSTRUCTOR_P (fn))
5215 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5216 if (!DECL_GENERATED_P (fn))
5218 error ("enclosing class of constexpr non-static member "
5219 "function %q+#D is not a literal type", fn);
5220 explain_non_literal_class (t);
5225 /* T is a non-literal type used in a context which requires a constant
5226 expression. Explain why it isn't literal. */
5228 void
5229 explain_non_literal_class (tree t)
5231 static struct pointer_set_t *diagnosed;
5233 if (!CLASS_TYPE_P (t))
5234 return;
5235 t = TYPE_MAIN_VARIANT (t);
5237 if (diagnosed == NULL)
5238 diagnosed = pointer_set_create ();
5239 if (pointer_set_insert (diagnosed, t) != 0)
5240 /* Already explained. */
5241 return;
5243 inform (0, "%q+T is not literal because:", t);
5244 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5245 inform (0, " %q+T has a non-trivial destructor", t);
5246 else if (CLASSTYPE_NON_AGGREGATE (t)
5247 && !TYPE_HAS_TRIVIAL_DFLT (t)
5248 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5250 inform (0, " %q+T is not an aggregate, does not have a trivial "
5251 "default constructor, and has no constexpr constructor that "
5252 "is not a copy or move constructor", t);
5253 if (TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5254 && !type_has_user_provided_default_constructor (t))
5256 /* Note that we can't simply call locate_ctor because when the
5257 constructor is deleted it just returns NULL_TREE. */
5258 tree fns;
5259 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5261 tree fn = OVL_CURRENT (fns);
5262 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5264 parms = skip_artificial_parms_for (fn, parms);
5266 if (sufficient_parms_p (parms))
5268 if (DECL_DELETED_FN (fn))
5269 maybe_explain_implicit_delete (fn);
5270 else
5271 explain_invalid_constexpr_fn (fn);
5272 break;
5277 else
5279 tree binfo, base_binfo, field; int i;
5280 for (binfo = TYPE_BINFO (t), i = 0;
5281 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5283 tree basetype = TREE_TYPE (base_binfo);
5284 if (!CLASSTYPE_LITERAL_P (basetype))
5286 inform (0, " base class %qT of %q+T is non-literal",
5287 basetype, t);
5288 explain_non_literal_class (basetype);
5289 return;
5292 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5294 tree ftype;
5295 if (TREE_CODE (field) != FIELD_DECL)
5296 continue;
5297 ftype = TREE_TYPE (field);
5298 if (!literal_type_p (ftype))
5300 inform (0, " non-static data member %q+D has "
5301 "non-literal type", field);
5302 if (CLASS_TYPE_P (ftype))
5303 explain_non_literal_class (ftype);
5309 /* Check the validity of the bases and members declared in T. Add any
5310 implicitly-generated functions (like copy-constructors and
5311 assignment operators). Compute various flag bits (like
5312 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5313 level: i.e., independently of the ABI in use. */
5315 static void
5316 check_bases_and_members (tree t)
5318 /* Nonzero if the implicitly generated copy constructor should take
5319 a non-const reference argument. */
5320 int cant_have_const_ctor;
5321 /* Nonzero if the implicitly generated assignment operator
5322 should take a non-const reference argument. */
5323 int no_const_asn_ref;
5324 tree access_decls;
5325 bool saved_complex_asn_ref;
5326 bool saved_nontrivial_dtor;
5327 tree fn;
5329 /* By default, we use const reference arguments and generate default
5330 constructors. */
5331 cant_have_const_ctor = 0;
5332 no_const_asn_ref = 0;
5334 /* Check all the base-classes. */
5335 check_bases (t, &cant_have_const_ctor,
5336 &no_const_asn_ref);
5338 /* Deduce noexcept on destructors. This needs to happen after we've set
5339 triviality flags appropriately for our bases. */
5340 if (cxx_dialect >= cxx0x)
5341 deduce_noexcept_on_destructors (t);
5343 /* Check all the method declarations. */
5344 check_methods (t);
5346 /* Save the initial values of these flags which only indicate whether
5347 or not the class has user-provided functions. As we analyze the
5348 bases and members we can set these flags for other reasons. */
5349 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5350 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5352 /* Check all the data member declarations. We cannot call
5353 check_field_decls until we have called check_bases check_methods,
5354 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5355 being set appropriately. */
5356 check_field_decls (t, &access_decls,
5357 &cant_have_const_ctor,
5358 &no_const_asn_ref);
5360 /* A nearly-empty class has to be vptr-containing; a nearly empty
5361 class contains just a vptr. */
5362 if (!TYPE_CONTAINS_VPTR_P (t))
5363 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5365 /* Do some bookkeeping that will guide the generation of implicitly
5366 declared member functions. */
5367 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5368 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5369 /* We need to call a constructor for this class if it has a
5370 user-provided constructor, or if the default constructor is going
5371 to initialize the vptr. (This is not an if-and-only-if;
5372 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5373 themselves need constructing.) */
5374 TYPE_NEEDS_CONSTRUCTING (t)
5375 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5376 /* [dcl.init.aggr]
5378 An aggregate is an array or a class with no user-provided
5379 constructors ... and no virtual functions.
5381 Again, other conditions for being an aggregate are checked
5382 elsewhere. */
5383 CLASSTYPE_NON_AGGREGATE (t)
5384 |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
5385 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5386 retain the old definition internally for ABI reasons. */
5387 CLASSTYPE_NON_LAYOUT_POD_P (t)
5388 |= (CLASSTYPE_NON_AGGREGATE (t)
5389 || saved_nontrivial_dtor || saved_complex_asn_ref);
5390 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5391 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5392 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5393 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5395 /* If the class has no user-declared constructor, but does have
5396 non-static const or reference data members that can never be
5397 initialized, issue a warning. */
5398 if (warn_uninitialized
5399 /* Classes with user-declared constructors are presumed to
5400 initialize these members. */
5401 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5402 /* Aggregates can be initialized with brace-enclosed
5403 initializers. */
5404 && CLASSTYPE_NON_AGGREGATE (t))
5406 tree field;
5408 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5410 tree type;
5412 if (TREE_CODE (field) != FIELD_DECL
5413 || DECL_INITIAL (field) != NULL_TREE)
5414 continue;
5416 type = TREE_TYPE (field);
5417 if (TREE_CODE (type) == REFERENCE_TYPE)
5418 warning (OPT_Wuninitialized, "non-static reference %q+#D "
5419 "in class without a constructor", field);
5420 else if (CP_TYPE_CONST_P (type)
5421 && (!CLASS_TYPE_P (type)
5422 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5423 warning (OPT_Wuninitialized, "non-static const member %q+#D "
5424 "in class without a constructor", field);
5428 /* Synthesize any needed methods. */
5429 add_implicitly_declared_members (t, &access_decls,
5430 cant_have_const_ctor,
5431 no_const_asn_ref);
5433 /* Check defaulted declarations here so we have cant_have_const_ctor
5434 and don't need to worry about clones. */
5435 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5436 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5438 int copy = copy_fn_p (fn);
5439 if (copy > 0)
5441 bool imp_const_p
5442 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5443 : !no_const_asn_ref);
5444 bool fn_const_p = (copy == 2);
5446 if (fn_const_p && !imp_const_p)
5447 /* If the function is defaulted outside the class, we just
5448 give the synthesis error. */
5449 error ("%q+D declared to take const reference, but implicit "
5450 "declaration would take non-const", fn);
5452 defaulted_late_check (fn);
5455 if (LAMBDA_TYPE_P (t))
5457 /* "The closure type associated with a lambda-expression has a deleted
5458 default constructor and a deleted copy assignment operator." */
5459 TYPE_NEEDS_CONSTRUCTING (t) = 1;
5460 TYPE_HAS_COMPLEX_DFLT (t) = 1;
5461 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
5462 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 0;
5464 /* "This class type is not an aggregate." */
5465 CLASSTYPE_NON_AGGREGATE (t) = 1;
5468 /* Compute the 'literal type' property before we
5469 do anything with non-static member functions. */
5470 finalize_literal_type_property (t);
5472 /* Create the in-charge and not-in-charge variants of constructors
5473 and destructors. */
5474 clone_constructors_and_destructors (t);
5476 /* Process the using-declarations. */
5477 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5478 handle_using_decl (TREE_VALUE (access_decls), t);
5480 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5481 finish_struct_methods (t);
5483 /* Figure out whether or not we will need a cookie when dynamically
5484 allocating an array of this type. */
5485 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5486 = type_requires_array_cookie (t);
5489 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5490 accordingly. If a new vfield was created (because T doesn't have a
5491 primary base class), then the newly created field is returned. It
5492 is not added to the TYPE_FIELDS list; it is the caller's
5493 responsibility to do that. Accumulate declared virtual functions
5494 on VIRTUALS_P. */
5496 static tree
5497 create_vtable_ptr (tree t, tree* virtuals_p)
5499 tree fn;
5501 /* Collect the virtual functions declared in T. */
5502 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5503 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5504 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5506 tree new_virtual = make_node (TREE_LIST);
5508 BV_FN (new_virtual) = fn;
5509 BV_DELTA (new_virtual) = integer_zero_node;
5510 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5512 TREE_CHAIN (new_virtual) = *virtuals_p;
5513 *virtuals_p = new_virtual;
5516 /* If we couldn't find an appropriate base class, create a new field
5517 here. Even if there weren't any new virtual functions, we might need a
5518 new virtual function table if we're supposed to include vptrs in
5519 all classes that need them. */
5520 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5522 /* We build this decl with vtbl_ptr_type_node, which is a
5523 `vtable_entry_type*'. It might seem more precise to use
5524 `vtable_entry_type (*)[N]' where N is the number of virtual
5525 functions. However, that would require the vtable pointer in
5526 base classes to have a different type than the vtable pointer
5527 in derived classes. We could make that happen, but that
5528 still wouldn't solve all the problems. In particular, the
5529 type-based alias analysis code would decide that assignments
5530 to the base class vtable pointer can't alias assignments to
5531 the derived class vtable pointer, since they have different
5532 types. Thus, in a derived class destructor, where the base
5533 class constructor was inlined, we could generate bad code for
5534 setting up the vtable pointer.
5536 Therefore, we use one type for all vtable pointers. We still
5537 use a type-correct type; it's just doesn't indicate the array
5538 bounds. That's better than using `void*' or some such; it's
5539 cleaner, and it let's the alias analysis code know that these
5540 stores cannot alias stores to void*! */
5541 tree field;
5543 field = build_decl (input_location,
5544 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5545 DECL_VIRTUAL_P (field) = 1;
5546 DECL_ARTIFICIAL (field) = 1;
5547 DECL_FIELD_CONTEXT (field) = t;
5548 DECL_FCONTEXT (field) = t;
5549 if (TYPE_PACKED (t))
5550 DECL_PACKED (field) = 1;
5552 TYPE_VFIELD (t) = field;
5554 /* This class is non-empty. */
5555 CLASSTYPE_EMPTY_P (t) = 0;
5557 return field;
5560 return NULL_TREE;
5563 /* Add OFFSET to all base types of BINFO which is a base in the
5564 hierarchy dominated by T.
5566 OFFSET, which is a type offset, is number of bytes. */
5568 static void
5569 propagate_binfo_offsets (tree binfo, tree offset)
5571 int i;
5572 tree primary_binfo;
5573 tree base_binfo;
5575 /* Update BINFO's offset. */
5576 BINFO_OFFSET (binfo)
5577 = convert (sizetype,
5578 size_binop (PLUS_EXPR,
5579 convert (ssizetype, BINFO_OFFSET (binfo)),
5580 offset));
5582 /* Find the primary base class. */
5583 primary_binfo = get_primary_binfo (binfo);
5585 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5586 propagate_binfo_offsets (primary_binfo, offset);
5588 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5589 downwards. */
5590 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5592 /* Don't do the primary base twice. */
5593 if (base_binfo == primary_binfo)
5594 continue;
5596 if (BINFO_VIRTUAL_P (base_binfo))
5597 continue;
5599 propagate_binfo_offsets (base_binfo, offset);
5603 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5604 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5605 empty subobjects of T. */
5607 static void
5608 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5610 tree vbase;
5611 tree t = rli->t;
5612 bool first_vbase = true;
5613 tree *next_field;
5615 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5616 return;
5618 if (!abi_version_at_least(2))
5620 /* In G++ 3.2, we incorrectly rounded the size before laying out
5621 the virtual bases. */
5622 finish_record_layout (rli, /*free_p=*/false);
5623 #ifdef STRUCTURE_SIZE_BOUNDARY
5624 /* Packed structures don't need to have minimum size. */
5625 if (! TYPE_PACKED (t))
5626 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
5627 #endif
5628 rli->offset = TYPE_SIZE_UNIT (t);
5629 rli->bitpos = bitsize_zero_node;
5630 rli->record_align = TYPE_ALIGN (t);
5633 /* Find the last field. The artificial fields created for virtual
5634 bases will go after the last extant field to date. */
5635 next_field = &TYPE_FIELDS (t);
5636 while (*next_field)
5637 next_field = &DECL_CHAIN (*next_field);
5639 /* Go through the virtual bases, allocating space for each virtual
5640 base that is not already a primary base class. These are
5641 allocated in inheritance graph order. */
5642 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5644 if (!BINFO_VIRTUAL_P (vbase))
5645 continue;
5647 if (!BINFO_PRIMARY_P (vbase))
5649 tree basetype = TREE_TYPE (vbase);
5651 /* This virtual base is not a primary base of any class in the
5652 hierarchy, so we have to add space for it. */
5653 next_field = build_base_field (rli, vbase,
5654 offsets, next_field);
5656 /* If the first virtual base might have been placed at a
5657 lower address, had we started from CLASSTYPE_SIZE, rather
5658 than TYPE_SIZE, issue a warning. There can be both false
5659 positives and false negatives from this warning in rare
5660 cases; to deal with all the possibilities would probably
5661 require performing both layout algorithms and comparing
5662 the results which is not particularly tractable. */
5663 if (warn_abi
5664 && first_vbase
5665 && (tree_int_cst_lt
5666 (size_binop (CEIL_DIV_EXPR,
5667 round_up_loc (input_location,
5668 CLASSTYPE_SIZE (t),
5669 CLASSTYPE_ALIGN (basetype)),
5670 bitsize_unit_node),
5671 BINFO_OFFSET (vbase))))
5672 warning (OPT_Wabi,
5673 "offset of virtual base %qT is not ABI-compliant and "
5674 "may change in a future version of GCC",
5675 basetype);
5677 first_vbase = false;
5682 /* Returns the offset of the byte just past the end of the base class
5683 BINFO. */
5685 static tree
5686 end_of_base (tree binfo)
5688 tree size;
5690 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5691 size = TYPE_SIZE_UNIT (char_type_node);
5692 else if (is_empty_class (BINFO_TYPE (binfo)))
5693 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5694 allocate some space for it. It cannot have virtual bases, so
5695 TYPE_SIZE_UNIT is fine. */
5696 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5697 else
5698 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5700 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5703 /* Returns the offset of the byte just past the end of the base class
5704 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5705 only non-virtual bases are included. */
5707 static tree
5708 end_of_class (tree t, int include_virtuals_p)
5710 tree result = size_zero_node;
5711 vec<tree, va_gc> *vbases;
5712 tree binfo;
5713 tree base_binfo;
5714 tree offset;
5715 int i;
5717 for (binfo = TYPE_BINFO (t), i = 0;
5718 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5720 if (!include_virtuals_p
5721 && BINFO_VIRTUAL_P (base_binfo)
5722 && (!BINFO_PRIMARY_P (base_binfo)
5723 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5724 continue;
5726 offset = end_of_base (base_binfo);
5727 if (INT_CST_LT_UNSIGNED (result, offset))
5728 result = offset;
5731 /* G++ 3.2 did not check indirect virtual bases. */
5732 if (abi_version_at_least (2) && include_virtuals_p)
5733 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5734 vec_safe_iterate (vbases, i, &base_binfo); i++)
5736 offset = end_of_base (base_binfo);
5737 if (INT_CST_LT_UNSIGNED (result, offset))
5738 result = offset;
5741 return result;
5744 /* Warn about bases of T that are inaccessible because they are
5745 ambiguous. For example:
5747 struct S {};
5748 struct T : public S {};
5749 struct U : public S, public T {};
5751 Here, `(S*) new U' is not allowed because there are two `S'
5752 subobjects of U. */
5754 static void
5755 warn_about_ambiguous_bases (tree t)
5757 int i;
5758 vec<tree, va_gc> *vbases;
5759 tree basetype;
5760 tree binfo;
5761 tree base_binfo;
5763 /* If there are no repeated bases, nothing can be ambiguous. */
5764 if (!CLASSTYPE_REPEATED_BASE_P (t))
5765 return;
5767 /* Check direct bases. */
5768 for (binfo = TYPE_BINFO (t), i = 0;
5769 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5771 basetype = BINFO_TYPE (base_binfo);
5773 if (!uniquely_derived_from_p (basetype, t))
5774 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5775 basetype, t);
5778 /* Check for ambiguous virtual bases. */
5779 if (extra_warnings)
5780 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5781 vec_safe_iterate (vbases, i, &binfo); i++)
5783 basetype = BINFO_TYPE (binfo);
5785 if (!uniquely_derived_from_p (basetype, t))
5786 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
5787 "to ambiguity", basetype, t);
5791 /* Compare two INTEGER_CSTs K1 and K2. */
5793 static int
5794 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5796 return tree_int_cst_compare ((tree) k1, (tree) k2);
5799 /* Increase the size indicated in RLI to account for empty classes
5800 that are "off the end" of the class. */
5802 static void
5803 include_empty_classes (record_layout_info rli)
5805 tree eoc;
5806 tree rli_size;
5808 /* It might be the case that we grew the class to allocate a
5809 zero-sized base class. That won't be reflected in RLI, yet,
5810 because we are willing to overlay multiple bases at the same
5811 offset. However, now we need to make sure that RLI is big enough
5812 to reflect the entire class. */
5813 eoc = end_of_class (rli->t,
5814 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5815 rli_size = rli_size_unit_so_far (rli);
5816 if (TREE_CODE (rli_size) == INTEGER_CST
5817 && INT_CST_LT_UNSIGNED (rli_size, eoc))
5819 if (!abi_version_at_least (2))
5820 /* In version 1 of the ABI, the size of a class that ends with
5821 a bitfield was not rounded up to a whole multiple of a
5822 byte. Because rli_size_unit_so_far returns only the number
5823 of fully allocated bytes, any extra bits were not included
5824 in the size. */
5825 rli->bitpos = round_down (rli->bitpos, BITS_PER_UNIT);
5826 else
5827 /* The size should have been rounded to a whole byte. */
5828 gcc_assert (tree_int_cst_equal
5829 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5830 rli->bitpos
5831 = size_binop (PLUS_EXPR,
5832 rli->bitpos,
5833 size_binop (MULT_EXPR,
5834 convert (bitsizetype,
5835 size_binop (MINUS_EXPR,
5836 eoc, rli_size)),
5837 bitsize_int (BITS_PER_UNIT)));
5838 normalize_rli (rli);
5842 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5843 BINFO_OFFSETs for all of the base-classes. Position the vtable
5844 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5846 static void
5847 layout_class_type (tree t, tree *virtuals_p)
5849 tree non_static_data_members;
5850 tree field;
5851 tree vptr;
5852 record_layout_info rli;
5853 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5854 types that appear at that offset. */
5855 splay_tree empty_base_offsets;
5856 /* True if the last field layed out was a bit-field. */
5857 bool last_field_was_bitfield = false;
5858 /* The location at which the next field should be inserted. */
5859 tree *next_field;
5860 /* T, as a base class. */
5861 tree base_t;
5863 /* Keep track of the first non-static data member. */
5864 non_static_data_members = TYPE_FIELDS (t);
5866 /* Start laying out the record. */
5867 rli = start_record_layout (t);
5869 /* Mark all the primary bases in the hierarchy. */
5870 determine_primary_bases (t);
5872 /* Create a pointer to our virtual function table. */
5873 vptr = create_vtable_ptr (t, virtuals_p);
5875 /* The vptr is always the first thing in the class. */
5876 if (vptr)
5878 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
5879 TYPE_FIELDS (t) = vptr;
5880 next_field = &DECL_CHAIN (vptr);
5881 place_field (rli, vptr);
5883 else
5884 next_field = &TYPE_FIELDS (t);
5886 /* Build FIELD_DECLs for all of the non-virtual base-types. */
5887 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
5888 NULL, NULL);
5889 build_base_fields (rli, empty_base_offsets, next_field);
5891 /* Layout the non-static data members. */
5892 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
5894 tree type;
5895 tree padding;
5897 /* We still pass things that aren't non-static data members to
5898 the back end, in case it wants to do something with them. */
5899 if (TREE_CODE (field) != FIELD_DECL)
5901 place_field (rli, field);
5902 /* If the static data member has incomplete type, keep track
5903 of it so that it can be completed later. (The handling
5904 of pending statics in finish_record_layout is
5905 insufficient; consider:
5907 struct S1;
5908 struct S2 { static S1 s1; };
5910 At this point, finish_record_layout will be called, but
5911 S1 is still incomplete.) */
5912 if (VAR_P (field))
5914 maybe_register_incomplete_var (field);
5915 /* The visibility of static data members is determined
5916 at their point of declaration, not their point of
5917 definition. */
5918 determine_visibility (field);
5920 continue;
5923 type = TREE_TYPE (field);
5924 if (type == error_mark_node)
5925 continue;
5927 padding = NULL_TREE;
5929 /* If this field is a bit-field whose width is greater than its
5930 type, then there are some special rules for allocating
5931 it. */
5932 if (DECL_C_BIT_FIELD (field)
5933 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
5935 unsigned int itk;
5936 tree integer_type;
5937 bool was_unnamed_p = false;
5938 /* We must allocate the bits as if suitably aligned for the
5939 longest integer type that fits in this many bits. type
5940 of the field. Then, we are supposed to use the left over
5941 bits as additional padding. */
5942 for (itk = itk_char; itk != itk_none; ++itk)
5943 if (integer_types[itk] != NULL_TREE
5944 && (INT_CST_LT (size_int (MAX_FIXED_MODE_SIZE),
5945 TYPE_SIZE (integer_types[itk]))
5946 || INT_CST_LT (DECL_SIZE (field),
5947 TYPE_SIZE (integer_types[itk]))))
5948 break;
5950 /* ITK now indicates a type that is too large for the
5951 field. We have to back up by one to find the largest
5952 type that fits. */
5955 --itk;
5956 integer_type = integer_types[itk];
5957 } while (itk > 0 && integer_type == NULL_TREE);
5959 /* Figure out how much additional padding is required. GCC
5960 3.2 always created a padding field, even if it had zero
5961 width. */
5962 if (!abi_version_at_least (2)
5963 || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field)))
5965 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
5966 /* In a union, the padding field must have the full width
5967 of the bit-field; all fields start at offset zero. */
5968 padding = DECL_SIZE (field);
5969 else
5971 if (TREE_CODE (t) == UNION_TYPE)
5972 warning (OPT_Wabi, "size assigned to %qT may not be "
5973 "ABI-compliant and may change in a future "
5974 "version of GCC",
5976 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
5977 TYPE_SIZE (integer_type));
5980 #ifdef PCC_BITFIELD_TYPE_MATTERS
5981 /* An unnamed bitfield does not normally affect the
5982 alignment of the containing class on a target where
5983 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
5984 make any exceptions for unnamed bitfields when the
5985 bitfields are longer than their types. Therefore, we
5986 temporarily give the field a name. */
5987 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
5989 was_unnamed_p = true;
5990 DECL_NAME (field) = make_anon_name ();
5992 #endif
5993 DECL_SIZE (field) = TYPE_SIZE (integer_type);
5994 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
5995 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
5996 layout_nonempty_base_or_field (rli, field, NULL_TREE,
5997 empty_base_offsets);
5998 if (was_unnamed_p)
5999 DECL_NAME (field) = NULL_TREE;
6000 /* Now that layout has been performed, set the size of the
6001 field to the size of its declared type; the rest of the
6002 field is effectively invisible. */
6003 DECL_SIZE (field) = TYPE_SIZE (type);
6004 /* We must also reset the DECL_MODE of the field. */
6005 if (abi_version_at_least (2))
6006 DECL_MODE (field) = TYPE_MODE (type);
6007 else if (warn_abi
6008 && DECL_MODE (field) != TYPE_MODE (type))
6009 /* Versions of G++ before G++ 3.4 did not reset the
6010 DECL_MODE. */
6011 warning (OPT_Wabi,
6012 "the offset of %qD may not be ABI-compliant and may "
6013 "change in a future version of GCC", field);
6015 else
6016 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6017 empty_base_offsets);
6019 /* Remember the location of any empty classes in FIELD. */
6020 if (abi_version_at_least (2))
6021 record_subobject_offsets (TREE_TYPE (field),
6022 byte_position(field),
6023 empty_base_offsets,
6024 /*is_data_member=*/true);
6026 /* If a bit-field does not immediately follow another bit-field,
6027 and yet it starts in the middle of a byte, we have failed to
6028 comply with the ABI. */
6029 if (warn_abi
6030 && DECL_C_BIT_FIELD (field)
6031 /* The TREE_NO_WARNING flag gets set by Objective-C when
6032 laying out an Objective-C class. The ObjC ABI differs
6033 from the C++ ABI, and so we do not want a warning
6034 here. */
6035 && !TREE_NO_WARNING (field)
6036 && !last_field_was_bitfield
6037 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6038 DECL_FIELD_BIT_OFFSET (field),
6039 bitsize_unit_node)))
6040 warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may "
6041 "change in a future version of GCC", field);
6043 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
6044 offset of the field. */
6045 if (warn_abi
6046 && !abi_version_at_least (2)
6047 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
6048 byte_position (field))
6049 && contains_empty_class_p (TREE_TYPE (field)))
6050 warning (OPT_Wabi, "%q+D contains empty classes which may cause base "
6051 "classes to be placed at different locations in a "
6052 "future version of GCC", field);
6054 /* The middle end uses the type of expressions to determine the
6055 possible range of expression values. In order to optimize
6056 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6057 must be made aware of the width of "i", via its type.
6059 Because C++ does not have integer types of arbitrary width,
6060 we must (for the purposes of the front end) convert from the
6061 type assigned here to the declared type of the bitfield
6062 whenever a bitfield expression is used as an rvalue.
6063 Similarly, when assigning a value to a bitfield, the value
6064 must be converted to the type given the bitfield here. */
6065 if (DECL_C_BIT_FIELD (field))
6067 unsigned HOST_WIDE_INT width;
6068 tree ftype = TREE_TYPE (field);
6069 width = tree_low_cst (DECL_SIZE (field), /*unsignedp=*/1);
6070 if (width != TYPE_PRECISION (ftype))
6072 TREE_TYPE (field)
6073 = c_build_bitfield_integer_type (width,
6074 TYPE_UNSIGNED (ftype));
6075 TREE_TYPE (field)
6076 = cp_build_qualified_type (TREE_TYPE (field),
6077 cp_type_quals (ftype));
6081 /* If we needed additional padding after this field, add it
6082 now. */
6083 if (padding)
6085 tree padding_field;
6087 padding_field = build_decl (input_location,
6088 FIELD_DECL,
6089 NULL_TREE,
6090 char_type_node);
6091 DECL_BIT_FIELD (padding_field) = 1;
6092 DECL_SIZE (padding_field) = padding;
6093 DECL_CONTEXT (padding_field) = t;
6094 DECL_ARTIFICIAL (padding_field) = 1;
6095 DECL_IGNORED_P (padding_field) = 1;
6096 layout_nonempty_base_or_field (rli, padding_field,
6097 NULL_TREE,
6098 empty_base_offsets);
6101 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6104 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
6106 /* Make sure that we are on a byte boundary so that the size of
6107 the class without virtual bases will always be a round number
6108 of bytes. */
6109 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6110 normalize_rli (rli);
6113 /* G++ 3.2 does not allow virtual bases to be overlaid with tail
6114 padding. */
6115 if (!abi_version_at_least (2))
6116 include_empty_classes(rli);
6118 /* Delete all zero-width bit-fields from the list of fields. Now
6119 that the type is laid out they are no longer important. */
6120 remove_zero_width_bit_fields (t);
6122 /* Create the version of T used for virtual bases. We do not use
6123 make_class_type for this version; this is an artificial type. For
6124 a POD type, we just reuse T. */
6125 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6127 base_t = make_node (TREE_CODE (t));
6129 /* Set the size and alignment for the new type. In G++ 3.2, all
6130 empty classes were considered to have size zero when used as
6131 base classes. */
6132 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
6134 TYPE_SIZE (base_t) = bitsize_zero_node;
6135 TYPE_SIZE_UNIT (base_t) = size_zero_node;
6136 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
6137 warning (OPT_Wabi,
6138 "layout of classes derived from empty class %qT "
6139 "may change in a future version of GCC",
6142 else
6144 tree eoc;
6146 /* If the ABI version is not at least two, and the last
6147 field was a bit-field, RLI may not be on a byte
6148 boundary. In particular, rli_size_unit_so_far might
6149 indicate the last complete byte, while rli_size_so_far
6150 indicates the total number of bits used. Therefore,
6151 rli_size_so_far, rather than rli_size_unit_so_far, is
6152 used to compute TYPE_SIZE_UNIT. */
6153 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6154 TYPE_SIZE_UNIT (base_t)
6155 = size_binop (MAX_EXPR,
6156 convert (sizetype,
6157 size_binop (CEIL_DIV_EXPR,
6158 rli_size_so_far (rli),
6159 bitsize_int (BITS_PER_UNIT))),
6160 eoc);
6161 TYPE_SIZE (base_t)
6162 = size_binop (MAX_EXPR,
6163 rli_size_so_far (rli),
6164 size_binop (MULT_EXPR,
6165 convert (bitsizetype, eoc),
6166 bitsize_int (BITS_PER_UNIT)));
6168 TYPE_ALIGN (base_t) = rli->record_align;
6169 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6171 /* Copy the fields from T. */
6172 next_field = &TYPE_FIELDS (base_t);
6173 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6174 if (TREE_CODE (field) == FIELD_DECL)
6176 *next_field = build_decl (input_location,
6177 FIELD_DECL,
6178 DECL_NAME (field),
6179 TREE_TYPE (field));
6180 DECL_CONTEXT (*next_field) = base_t;
6181 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
6182 DECL_FIELD_BIT_OFFSET (*next_field)
6183 = DECL_FIELD_BIT_OFFSET (field);
6184 DECL_SIZE (*next_field) = DECL_SIZE (field);
6185 DECL_MODE (*next_field) = DECL_MODE (field);
6186 next_field = &DECL_CHAIN (*next_field);
6189 /* Record the base version of the type. */
6190 CLASSTYPE_AS_BASE (t) = base_t;
6191 TYPE_CONTEXT (base_t) = t;
6193 else
6194 CLASSTYPE_AS_BASE (t) = t;
6196 /* Every empty class contains an empty class. */
6197 if (CLASSTYPE_EMPTY_P (t))
6198 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6200 /* Set the TYPE_DECL for this type to contain the right
6201 value for DECL_OFFSET, so that we can use it as part
6202 of a COMPONENT_REF for multiple inheritance. */
6203 layout_decl (TYPE_MAIN_DECL (t), 0);
6205 /* Now fix up any virtual base class types that we left lying
6206 around. We must get these done before we try to lay out the
6207 virtual function table. As a side-effect, this will remove the
6208 base subobject fields. */
6209 layout_virtual_bases (rli, empty_base_offsets);
6211 /* Make sure that empty classes are reflected in RLI at this
6212 point. */
6213 include_empty_classes(rli);
6215 /* Make sure not to create any structures with zero size. */
6216 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6217 place_field (rli,
6218 build_decl (input_location,
6219 FIELD_DECL, NULL_TREE, char_type_node));
6221 /* If this is a non-POD, declaring it packed makes a difference to how it
6222 can be used as a field; don't let finalize_record_size undo it. */
6223 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6224 rli->packed_maybe_necessary = true;
6226 /* Let the back end lay out the type. */
6227 finish_record_layout (rli, /*free_p=*/true);
6229 /* Warn about bases that can't be talked about due to ambiguity. */
6230 warn_about_ambiguous_bases (t);
6232 /* Now that we're done with layout, give the base fields the real types. */
6233 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6234 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6235 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6237 /* Clean up. */
6238 splay_tree_delete (empty_base_offsets);
6240 if (CLASSTYPE_EMPTY_P (t)
6241 && tree_int_cst_lt (sizeof_biggest_empty_class,
6242 TYPE_SIZE_UNIT (t)))
6243 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6246 /* Determine the "key method" for the class type indicated by TYPE,
6247 and set CLASSTYPE_KEY_METHOD accordingly. */
6249 void
6250 determine_key_method (tree type)
6252 tree method;
6254 if (TYPE_FOR_JAVA (type)
6255 || processing_template_decl
6256 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6257 || CLASSTYPE_INTERFACE_KNOWN (type))
6258 return;
6260 /* The key method is the first non-pure virtual function that is not
6261 inline at the point of class definition. On some targets the
6262 key function may not be inline; those targets should not call
6263 this function until the end of the translation unit. */
6264 for (method = TYPE_METHODS (type); method != NULL_TREE;
6265 method = DECL_CHAIN (method))
6266 if (DECL_VINDEX (method) != NULL_TREE
6267 && ! DECL_DECLARED_INLINE_P (method)
6268 && ! DECL_PURE_VIRTUAL_P (method))
6270 CLASSTYPE_KEY_METHOD (type) = method;
6271 break;
6274 return;
6278 /* Allocate and return an instance of struct sorted_fields_type with
6279 N fields. */
6281 static struct sorted_fields_type *
6282 sorted_fields_type_new (int n)
6284 struct sorted_fields_type *sft;
6285 sft = ggc_alloc_sorted_fields_type (sizeof (struct sorted_fields_type)
6286 + n * sizeof (tree));
6287 sft->len = n;
6289 return sft;
6293 /* Perform processing required when the definition of T (a class type)
6294 is complete. */
6296 void
6297 finish_struct_1 (tree t)
6299 tree x;
6300 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6301 tree virtuals = NULL_TREE;
6303 if (COMPLETE_TYPE_P (t))
6305 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6306 error ("redefinition of %q#T", t);
6307 popclass ();
6308 return;
6311 /* If this type was previously laid out as a forward reference,
6312 make sure we lay it out again. */
6313 TYPE_SIZE (t) = NULL_TREE;
6314 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6316 /* Make assumptions about the class; we'll reset the flags if
6317 necessary. */
6318 CLASSTYPE_EMPTY_P (t) = 1;
6319 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6320 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6321 CLASSTYPE_LITERAL_P (t) = true;
6323 /* Do end-of-class semantic processing: checking the validity of the
6324 bases and members and add implicitly generated methods. */
6325 check_bases_and_members (t);
6327 /* Find the key method. */
6328 if (TYPE_CONTAINS_VPTR_P (t))
6330 /* The Itanium C++ ABI permits the key method to be chosen when
6331 the class is defined -- even though the key method so
6332 selected may later turn out to be an inline function. On
6333 some systems (such as ARM Symbian OS) the key method cannot
6334 be determined until the end of the translation unit. On such
6335 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6336 will cause the class to be added to KEYED_CLASSES. Then, in
6337 finish_file we will determine the key method. */
6338 if (targetm.cxx.key_method_may_be_inline ())
6339 determine_key_method (t);
6341 /* If a polymorphic class has no key method, we may emit the vtable
6342 in every translation unit where the class definition appears. */
6343 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6344 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6347 /* Layout the class itself. */
6348 layout_class_type (t, &virtuals);
6349 if (CLASSTYPE_AS_BASE (t) != t)
6350 /* We use the base type for trivial assignments, and hence it
6351 needs a mode. */
6352 compute_record_mode (CLASSTYPE_AS_BASE (t));
6354 virtuals = modify_all_vtables (t, nreverse (virtuals));
6356 /* If necessary, create the primary vtable for this class. */
6357 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6359 /* We must enter these virtuals into the table. */
6360 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6361 build_primary_vtable (NULL_TREE, t);
6362 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6363 /* Here we know enough to change the type of our virtual
6364 function table, but we will wait until later this function. */
6365 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6367 /* If we're warning about ABI tags, check the types of the new
6368 virtual functions. */
6369 if (warn_abi_tag)
6370 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6371 check_abi_tags (t, TREE_VALUE (v));
6374 if (TYPE_CONTAINS_VPTR_P (t))
6376 int vindex;
6377 tree fn;
6379 if (BINFO_VTABLE (TYPE_BINFO (t)))
6380 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6381 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6382 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6384 /* Add entries for virtual functions introduced by this class. */
6385 BINFO_VIRTUALS (TYPE_BINFO (t))
6386 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6388 /* Set DECL_VINDEX for all functions declared in this class. */
6389 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6391 fn = TREE_CHAIN (fn),
6392 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6393 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6395 tree fndecl = BV_FN (fn);
6397 if (DECL_THUNK_P (fndecl))
6398 /* A thunk. We should never be calling this entry directly
6399 from this vtable -- we'd use the entry for the non
6400 thunk base function. */
6401 DECL_VINDEX (fndecl) = NULL_TREE;
6402 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6403 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6407 finish_struct_bits (t);
6408 set_method_tm_attributes (t);
6410 /* Complete the rtl for any static member objects of the type we're
6411 working on. */
6412 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6413 if (VAR_P (x) && TREE_STATIC (x)
6414 && TREE_TYPE (x) != error_mark_node
6415 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6416 DECL_MODE (x) = TYPE_MODE (t);
6418 /* Done with FIELDS...now decide whether to sort these for
6419 faster lookups later.
6421 We use a small number because most searches fail (succeeding
6422 ultimately as the search bores through the inheritance
6423 hierarchy), and we want this failure to occur quickly. */
6425 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
6427 /* Complain if one of the field types requires lower visibility. */
6428 constrain_class_visibility (t);
6430 /* Make the rtl for any new vtables we have created, and unmark
6431 the base types we marked. */
6432 finish_vtbls (t);
6434 /* Build the VTT for T. */
6435 build_vtt (t);
6437 /* This warning does not make sense for Java classes, since they
6438 cannot have destructors. */
6439 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor && TYPE_POLYMORPHIC_P (t))
6441 tree dtor;
6443 dtor = CLASSTYPE_DESTRUCTORS (t);
6444 if (/* An implicitly declared destructor is always public. And,
6445 if it were virtual, we would have created it by now. */
6446 !dtor
6447 || (!DECL_VINDEX (dtor)
6448 && (/* public non-virtual */
6449 (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
6450 || (/* non-public non-virtual with friends */
6451 (TREE_PRIVATE (dtor) || TREE_PROTECTED (dtor))
6452 && (CLASSTYPE_FRIEND_CLASSES (t)
6453 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))))))
6454 warning (OPT_Wnon_virtual_dtor,
6455 "%q#T has virtual functions and accessible"
6456 " non-virtual destructor", t);
6459 complete_vars (t);
6461 if (warn_overloaded_virtual)
6462 warn_hidden (t);
6464 /* Class layout, assignment of virtual table slots, etc., is now
6465 complete. Give the back end a chance to tweak the visibility of
6466 the class or perform any other required target modifications. */
6467 targetm.cxx.adjust_class_at_definition (t);
6469 maybe_suppress_debug_info (t);
6471 dump_class_hierarchy (t);
6473 /* Finish debugging output for this type. */
6474 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6476 if (TYPE_TRANSPARENT_AGGR (t))
6478 tree field = first_field (t);
6479 if (field == NULL_TREE || error_operand_p (field))
6481 error ("type transparent %q#T does not have any fields", t);
6482 TYPE_TRANSPARENT_AGGR (t) = 0;
6484 else if (DECL_ARTIFICIAL (field))
6486 if (DECL_FIELD_IS_BASE (field))
6487 error ("type transparent class %qT has base classes", t);
6488 else
6490 gcc_checking_assert (DECL_VIRTUAL_P (field));
6491 error ("type transparent class %qT has virtual functions", t);
6493 TYPE_TRANSPARENT_AGGR (t) = 0;
6495 else if (TYPE_MODE (t) != DECL_MODE (field))
6497 error ("type transparent %q#T cannot be made transparent because "
6498 "the type of the first field has a different ABI from the "
6499 "class overall", t);
6500 TYPE_TRANSPARENT_AGGR (t) = 0;
6505 /* Insert FIELDS into T for the sorted case if the FIELDS count is
6506 equal to THRESHOLD or greater than THRESHOLD. */
6508 static void
6509 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
6511 int n_fields = count_fields (fields);
6512 if (n_fields >= threshold)
6514 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6515 add_fields_to_record_type (fields, field_vec, 0);
6516 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6517 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6521 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
6523 void
6524 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
6526 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
6527 if (sorted_fields)
6529 int i;
6530 int n_fields
6531 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
6532 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6534 for (i = 0; i < sorted_fields->len; ++i)
6535 field_vec->elts[i] = sorted_fields->elts[i];
6537 add_enum_fields_to_record_type (enumtype, field_vec,
6538 sorted_fields->len);
6539 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6540 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6544 /* When T was built up, the member declarations were added in reverse
6545 order. Rearrange them to declaration order. */
6547 void
6548 unreverse_member_declarations (tree t)
6550 tree next;
6551 tree prev;
6552 tree x;
6554 /* The following lists are all in reverse order. Put them in
6555 declaration order now. */
6556 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
6557 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6559 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
6560 reverse order, so we can't just use nreverse. */
6561 prev = NULL_TREE;
6562 for (x = TYPE_FIELDS (t);
6563 x && TREE_CODE (x) != TYPE_DECL;
6564 x = next)
6566 next = DECL_CHAIN (x);
6567 DECL_CHAIN (x) = prev;
6568 prev = x;
6570 if (prev)
6572 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6573 if (prev)
6574 TYPE_FIELDS (t) = prev;
6578 tree
6579 finish_struct (tree t, tree attributes)
6581 location_t saved_loc = input_location;
6583 /* Now that we've got all the field declarations, reverse everything
6584 as necessary. */
6585 unreverse_member_declarations (t);
6587 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6589 /* Nadger the current location so that diagnostics point to the start of
6590 the struct, not the end. */
6591 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
6593 if (processing_template_decl)
6595 tree x;
6597 finish_struct_methods (t);
6598 TYPE_SIZE (t) = bitsize_zero_node;
6599 TYPE_SIZE_UNIT (t) = size_zero_node;
6601 /* We need to emit an error message if this type was used as a parameter
6602 and it is an abstract type, even if it is a template. We construct
6603 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
6604 account and we call complete_vars with this type, which will check
6605 the PARM_DECLS. Note that while the type is being defined,
6606 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
6607 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
6608 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
6609 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
6610 if (DECL_PURE_VIRTUAL_P (x))
6611 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
6612 complete_vars (t);
6613 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
6614 an enclosing scope is a template class, so that this function be
6615 found by lookup_fnfields_1 when the using declaration is not
6616 instantiated yet. */
6617 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6618 if (TREE_CODE (x) == USING_DECL)
6620 tree fn = strip_using_decl (x);
6621 if (is_overloaded_fn (fn))
6622 for (; fn; fn = OVL_NEXT (fn))
6623 add_method (t, OVL_CURRENT (fn), x);
6626 /* Remember current #pragma pack value. */
6627 TYPE_PRECISION (t) = maximum_field_alignment;
6629 /* Fix up any variants we've already built. */
6630 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6632 TYPE_SIZE (x) = TYPE_SIZE (t);
6633 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
6634 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6635 TYPE_METHODS (x) = TYPE_METHODS (t);
6638 else
6639 finish_struct_1 (t);
6641 input_location = saved_loc;
6643 TYPE_BEING_DEFINED (t) = 0;
6645 if (current_class_type)
6646 popclass ();
6647 else
6648 error ("trying to finish struct, but kicked out due to previous parse errors");
6650 if (processing_template_decl && at_function_scope_p ()
6651 /* Lambdas are defined by the LAMBDA_EXPR. */
6652 && !LAMBDA_TYPE_P (t))
6653 add_stmt (build_min (TAG_DEFN, t));
6655 return t;
6658 /* Hash table to avoid endless recursion when handling references. */
6659 static hash_table <pointer_hash <tree_node> > fixed_type_or_null_ref_ht;
6661 /* Return the dynamic type of INSTANCE, if known.
6662 Used to determine whether the virtual function table is needed
6663 or not.
6665 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6666 of our knowledge of its type. *NONNULL should be initialized
6667 before this function is called. */
6669 static tree
6670 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
6672 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
6674 switch (TREE_CODE (instance))
6676 case INDIRECT_REF:
6677 if (POINTER_TYPE_P (TREE_TYPE (instance)))
6678 return NULL_TREE;
6679 else
6680 return RECUR (TREE_OPERAND (instance, 0));
6682 case CALL_EXPR:
6683 /* This is a call to a constructor, hence it's never zero. */
6684 if (TREE_HAS_CONSTRUCTOR (instance))
6686 if (nonnull)
6687 *nonnull = 1;
6688 return TREE_TYPE (instance);
6690 return NULL_TREE;
6692 case SAVE_EXPR:
6693 /* This is a call to a constructor, hence it's never zero. */
6694 if (TREE_HAS_CONSTRUCTOR (instance))
6696 if (nonnull)
6697 *nonnull = 1;
6698 return TREE_TYPE (instance);
6700 return RECUR (TREE_OPERAND (instance, 0));
6702 case POINTER_PLUS_EXPR:
6703 case PLUS_EXPR:
6704 case MINUS_EXPR:
6705 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
6706 return RECUR (TREE_OPERAND (instance, 0));
6707 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
6708 /* Propagate nonnull. */
6709 return RECUR (TREE_OPERAND (instance, 0));
6711 return NULL_TREE;
6713 CASE_CONVERT:
6714 return RECUR (TREE_OPERAND (instance, 0));
6716 case ADDR_EXPR:
6717 instance = TREE_OPERAND (instance, 0);
6718 if (nonnull)
6720 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
6721 with a real object -- given &p->f, p can still be null. */
6722 tree t = get_base_address (instance);
6723 /* ??? Probably should check DECL_WEAK here. */
6724 if (t && DECL_P (t))
6725 *nonnull = 1;
6727 return RECUR (instance);
6729 case COMPONENT_REF:
6730 /* If this component is really a base class reference, then the field
6731 itself isn't definitive. */
6732 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
6733 return RECUR (TREE_OPERAND (instance, 0));
6734 return RECUR (TREE_OPERAND (instance, 1));
6736 case VAR_DECL:
6737 case FIELD_DECL:
6738 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
6739 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
6741 if (nonnull)
6742 *nonnull = 1;
6743 return TREE_TYPE (TREE_TYPE (instance));
6745 /* fall through... */
6746 case TARGET_EXPR:
6747 case PARM_DECL:
6748 case RESULT_DECL:
6749 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
6751 if (nonnull)
6752 *nonnull = 1;
6753 return TREE_TYPE (instance);
6755 else if (instance == current_class_ptr)
6757 if (nonnull)
6758 *nonnull = 1;
6760 /* if we're in a ctor or dtor, we know our type. If
6761 current_class_ptr is set but we aren't in a function, we're in
6762 an NSDMI (and therefore a constructor). */
6763 if (current_scope () != current_function_decl
6764 || (DECL_LANG_SPECIFIC (current_function_decl)
6765 && (DECL_CONSTRUCTOR_P (current_function_decl)
6766 || DECL_DESTRUCTOR_P (current_function_decl))))
6768 if (cdtorp)
6769 *cdtorp = 1;
6770 return TREE_TYPE (TREE_TYPE (instance));
6773 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
6775 /* We only need one hash table because it is always left empty. */
6776 if (!fixed_type_or_null_ref_ht.is_created ())
6777 fixed_type_or_null_ref_ht.create (37);
6779 /* Reference variables should be references to objects. */
6780 if (nonnull)
6781 *nonnull = 1;
6783 /* Enter the INSTANCE in a table to prevent recursion; a
6784 variable's initializer may refer to the variable
6785 itself. */
6786 if (VAR_P (instance)
6787 && DECL_INITIAL (instance)
6788 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
6789 && !fixed_type_or_null_ref_ht.find (instance))
6791 tree type;
6792 tree_node **slot;
6794 slot = fixed_type_or_null_ref_ht.find_slot (instance, INSERT);
6795 *slot = instance;
6796 type = RECUR (DECL_INITIAL (instance));
6797 fixed_type_or_null_ref_ht.remove_elt (instance);
6799 return type;
6802 return NULL_TREE;
6804 default:
6805 return NULL_TREE;
6807 #undef RECUR
6810 /* Return nonzero if the dynamic type of INSTANCE is known, and
6811 equivalent to the static type. We also handle the case where
6812 INSTANCE is really a pointer. Return negative if this is a
6813 ctor/dtor. There the dynamic type is known, but this might not be
6814 the most derived base of the original object, and hence virtual
6815 bases may not be layed out according to this type.
6817 Used to determine whether the virtual function table is needed
6818 or not.
6820 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6821 of our knowledge of its type. *NONNULL should be initialized
6822 before this function is called. */
6825 resolves_to_fixed_type_p (tree instance, int* nonnull)
6827 tree t = TREE_TYPE (instance);
6828 int cdtorp = 0;
6829 tree fixed;
6831 /* processing_template_decl can be false in a template if we're in
6832 fold_non_dependent_expr, but we still want to suppress this check. */
6833 if (in_template_function ())
6835 /* In a template we only care about the type of the result. */
6836 if (nonnull)
6837 *nonnull = true;
6838 return true;
6841 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
6842 if (fixed == NULL_TREE)
6843 return 0;
6844 if (POINTER_TYPE_P (t))
6845 t = TREE_TYPE (t);
6846 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
6847 return 0;
6848 return cdtorp ? -1 : 1;
6852 void
6853 init_class_processing (void)
6855 current_class_depth = 0;
6856 current_class_stack_size = 10;
6857 current_class_stack
6858 = XNEWVEC (struct class_stack_node, current_class_stack_size);
6859 vec_alloc (local_classes, 8);
6860 sizeof_biggest_empty_class = size_zero_node;
6862 ridpointers[(int) RID_PUBLIC] = access_public_node;
6863 ridpointers[(int) RID_PRIVATE] = access_private_node;
6864 ridpointers[(int) RID_PROTECTED] = access_protected_node;
6867 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
6869 static void
6870 restore_class_cache (void)
6872 tree type;
6874 /* We are re-entering the same class we just left, so we don't
6875 have to search the whole inheritance matrix to find all the
6876 decls to bind again. Instead, we install the cached
6877 class_shadowed list and walk through it binding names. */
6878 push_binding_level (previous_class_level);
6879 class_binding_level = previous_class_level;
6880 /* Restore IDENTIFIER_TYPE_VALUE. */
6881 for (type = class_binding_level->type_shadowed;
6882 type;
6883 type = TREE_CHAIN (type))
6884 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
6887 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
6888 appropriate for TYPE.
6890 So that we may avoid calls to lookup_name, we cache the _TYPE
6891 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
6893 For multiple inheritance, we perform a two-pass depth-first search
6894 of the type lattice. */
6896 void
6897 pushclass (tree type)
6899 class_stack_node_t csn;
6901 type = TYPE_MAIN_VARIANT (type);
6903 /* Make sure there is enough room for the new entry on the stack. */
6904 if (current_class_depth + 1 >= current_class_stack_size)
6906 current_class_stack_size *= 2;
6907 current_class_stack
6908 = XRESIZEVEC (struct class_stack_node, current_class_stack,
6909 current_class_stack_size);
6912 /* Insert a new entry on the class stack. */
6913 csn = current_class_stack + current_class_depth;
6914 csn->name = current_class_name;
6915 csn->type = current_class_type;
6916 csn->access = current_access_specifier;
6917 csn->names_used = 0;
6918 csn->hidden = 0;
6919 current_class_depth++;
6921 /* Now set up the new type. */
6922 current_class_name = TYPE_NAME (type);
6923 if (TREE_CODE (current_class_name) == TYPE_DECL)
6924 current_class_name = DECL_NAME (current_class_name);
6925 current_class_type = type;
6927 /* By default, things in classes are private, while things in
6928 structures or unions are public. */
6929 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
6930 ? access_private_node
6931 : access_public_node);
6933 if (previous_class_level
6934 && type != previous_class_level->this_entity
6935 && current_class_depth == 1)
6937 /* Forcibly remove any old class remnants. */
6938 invalidate_class_lookup_cache ();
6941 if (!previous_class_level
6942 || type != previous_class_level->this_entity
6943 || current_class_depth > 1)
6944 pushlevel_class ();
6945 else
6946 restore_class_cache ();
6949 /* When we exit a toplevel class scope, we save its binding level so
6950 that we can restore it quickly. Here, we've entered some other
6951 class, so we must invalidate our cache. */
6953 void
6954 invalidate_class_lookup_cache (void)
6956 previous_class_level = NULL;
6959 /* Get out of the current class scope. If we were in a class scope
6960 previously, that is the one popped to. */
6962 void
6963 popclass (void)
6965 poplevel_class ();
6967 current_class_depth--;
6968 current_class_name = current_class_stack[current_class_depth].name;
6969 current_class_type = current_class_stack[current_class_depth].type;
6970 current_access_specifier = current_class_stack[current_class_depth].access;
6971 if (current_class_stack[current_class_depth].names_used)
6972 splay_tree_delete (current_class_stack[current_class_depth].names_used);
6975 /* Mark the top of the class stack as hidden. */
6977 void
6978 push_class_stack (void)
6980 if (current_class_depth)
6981 ++current_class_stack[current_class_depth - 1].hidden;
6984 /* Mark the top of the class stack as un-hidden. */
6986 void
6987 pop_class_stack (void)
6989 if (current_class_depth)
6990 --current_class_stack[current_class_depth - 1].hidden;
6993 /* Returns 1 if the class type currently being defined is either T or
6994 a nested type of T. */
6996 bool
6997 currently_open_class (tree t)
6999 int i;
7001 if (!CLASS_TYPE_P (t))
7002 return false;
7004 t = TYPE_MAIN_VARIANT (t);
7006 /* We start looking from 1 because entry 0 is from global scope,
7007 and has no type. */
7008 for (i = current_class_depth; i > 0; --i)
7010 tree c;
7011 if (i == current_class_depth)
7012 c = current_class_type;
7013 else
7015 if (current_class_stack[i].hidden)
7016 break;
7017 c = current_class_stack[i].type;
7019 if (!c)
7020 continue;
7021 if (same_type_p (c, t))
7022 return true;
7024 return false;
7027 /* If either current_class_type or one of its enclosing classes are derived
7028 from T, return the appropriate type. Used to determine how we found
7029 something via unqualified lookup. */
7031 tree
7032 currently_open_derived_class (tree t)
7034 int i;
7036 /* The bases of a dependent type are unknown. */
7037 if (dependent_type_p (t))
7038 return NULL_TREE;
7040 if (!current_class_type)
7041 return NULL_TREE;
7043 if (DERIVED_FROM_P (t, current_class_type))
7044 return current_class_type;
7046 for (i = current_class_depth - 1; i > 0; --i)
7048 if (current_class_stack[i].hidden)
7049 break;
7050 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7051 return current_class_stack[i].type;
7054 return NULL_TREE;
7057 /* Returns the innermost class type which is not a lambda closure type. */
7059 tree
7060 current_nonlambda_class_type (void)
7062 int i;
7064 /* We start looking from 1 because entry 0 is from global scope,
7065 and has no type. */
7066 for (i = current_class_depth; i > 0; --i)
7068 tree c;
7069 if (i == current_class_depth)
7070 c = current_class_type;
7071 else
7073 if (current_class_stack[i].hidden)
7074 break;
7075 c = current_class_stack[i].type;
7077 if (!c)
7078 continue;
7079 if (!LAMBDA_TYPE_P (c))
7080 return c;
7082 return NULL_TREE;
7085 /* When entering a class scope, all enclosing class scopes' names with
7086 static meaning (static variables, static functions, types and
7087 enumerators) have to be visible. This recursive function calls
7088 pushclass for all enclosing class contexts until global or a local
7089 scope is reached. TYPE is the enclosed class. */
7091 void
7092 push_nested_class (tree type)
7094 /* A namespace might be passed in error cases, like A::B:C. */
7095 if (type == NULL_TREE
7096 || !CLASS_TYPE_P (type))
7097 return;
7099 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7101 pushclass (type);
7104 /* Undoes a push_nested_class call. */
7106 void
7107 pop_nested_class (void)
7109 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7111 popclass ();
7112 if (context && CLASS_TYPE_P (context))
7113 pop_nested_class ();
7116 /* Returns the number of extern "LANG" blocks we are nested within. */
7119 current_lang_depth (void)
7121 return vec_safe_length (current_lang_base);
7124 /* Set global variables CURRENT_LANG_NAME to appropriate value
7125 so that behavior of name-mangling machinery is correct. */
7127 void
7128 push_lang_context (tree name)
7130 vec_safe_push (current_lang_base, current_lang_name);
7132 if (name == lang_name_cplusplus)
7134 current_lang_name = name;
7136 else if (name == lang_name_java)
7138 current_lang_name = name;
7139 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
7140 (See record_builtin_java_type in decl.c.) However, that causes
7141 incorrect debug entries if these types are actually used.
7142 So we re-enable debug output after extern "Java". */
7143 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
7144 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
7145 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
7146 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
7147 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
7148 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
7149 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
7150 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
7152 else if (name == lang_name_c)
7154 current_lang_name = name;
7156 else
7157 error ("language string %<\"%E\"%> not recognized", name);
7160 /* Get out of the current language scope. */
7162 void
7163 pop_lang_context (void)
7165 current_lang_name = current_lang_base->pop ();
7168 /* Type instantiation routines. */
7170 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7171 matches the TARGET_TYPE. If there is no satisfactory match, return
7172 error_mark_node, and issue an error & warning messages under
7173 control of FLAGS. Permit pointers to member function if FLAGS
7174 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7175 a template-id, and EXPLICIT_TARGS are the explicitly provided
7176 template arguments.
7178 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7179 is the base path used to reference those member functions. If
7180 the address is resolved to a member function, access checks will be
7181 performed and errors issued if appropriate. */
7183 static tree
7184 resolve_address_of_overloaded_function (tree target_type,
7185 tree overload,
7186 tsubst_flags_t flags,
7187 bool template_only,
7188 tree explicit_targs,
7189 tree access_path)
7191 /* Here's what the standard says:
7193 [over.over]
7195 If the name is a function template, template argument deduction
7196 is done, and if the argument deduction succeeds, the deduced
7197 arguments are used to generate a single template function, which
7198 is added to the set of overloaded functions considered.
7200 Non-member functions and static member functions match targets of
7201 type "pointer-to-function" or "reference-to-function." Nonstatic
7202 member functions match targets of type "pointer-to-member
7203 function;" the function type of the pointer to member is used to
7204 select the member function from the set of overloaded member
7205 functions. If a nonstatic member function is selected, the
7206 reference to the overloaded function name is required to have the
7207 form of a pointer to member as described in 5.3.1.
7209 If more than one function is selected, any template functions in
7210 the set are eliminated if the set also contains a non-template
7211 function, and any given template function is eliminated if the
7212 set contains a second template function that is more specialized
7213 than the first according to the partial ordering rules 14.5.5.2.
7214 After such eliminations, if any, there shall remain exactly one
7215 selected function. */
7217 int is_ptrmem = 0;
7218 /* We store the matches in a TREE_LIST rooted here. The functions
7219 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7220 interoperability with most_specialized_instantiation. */
7221 tree matches = NULL_TREE;
7222 tree fn;
7223 tree target_fn_type;
7225 /* By the time we get here, we should be seeing only real
7226 pointer-to-member types, not the internal POINTER_TYPE to
7227 METHOD_TYPE representation. */
7228 gcc_assert (!TYPE_PTR_P (target_type)
7229 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7231 gcc_assert (is_overloaded_fn (overload));
7233 /* Check that the TARGET_TYPE is reasonable. */
7234 if (TYPE_PTRFN_P (target_type)
7235 || TYPE_REFFN_P (target_type))
7236 /* This is OK. */;
7237 else if (TYPE_PTRMEMFUNC_P (target_type))
7238 /* This is OK, too. */
7239 is_ptrmem = 1;
7240 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7241 /* This is OK, too. This comes from a conversion to reference
7242 type. */
7243 target_type = build_reference_type (target_type);
7244 else
7246 if (flags & tf_error)
7247 error ("cannot resolve overloaded function %qD based on"
7248 " conversion to type %qT",
7249 DECL_NAME (OVL_FUNCTION (overload)), target_type);
7250 return error_mark_node;
7253 /* Non-member functions and static member functions match targets of type
7254 "pointer-to-function" or "reference-to-function." Nonstatic member
7255 functions match targets of type "pointer-to-member-function;" the
7256 function type of the pointer to member is used to select the member
7257 function from the set of overloaded member functions.
7259 So figure out the FUNCTION_TYPE that we want to match against. */
7260 target_fn_type = static_fn_type (target_type);
7262 /* If we can find a non-template function that matches, we can just
7263 use it. There's no point in generating template instantiations
7264 if we're just going to throw them out anyhow. But, of course, we
7265 can only do this when we don't *need* a template function. */
7266 if (!template_only)
7268 tree fns;
7270 for (fns = overload; fns; fns = OVL_NEXT (fns))
7272 tree fn = OVL_CURRENT (fns);
7274 if (TREE_CODE (fn) == TEMPLATE_DECL)
7275 /* We're not looking for templates just yet. */
7276 continue;
7278 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7279 != is_ptrmem)
7280 /* We're looking for a non-static member, and this isn't
7281 one, or vice versa. */
7282 continue;
7284 /* Ignore functions which haven't been explicitly
7285 declared. */
7286 if (DECL_ANTICIPATED (fn))
7287 continue;
7289 /* See if there's a match. */
7290 if (same_type_p (target_fn_type, static_fn_type (fn)))
7291 matches = tree_cons (fn, NULL_TREE, matches);
7295 /* Now, if we've already got a match (or matches), there's no need
7296 to proceed to the template functions. But, if we don't have a
7297 match we need to look at them, too. */
7298 if (!matches)
7300 tree target_arg_types;
7301 tree target_ret_type;
7302 tree fns;
7303 tree *args;
7304 unsigned int nargs, ia;
7305 tree arg;
7307 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7308 target_ret_type = TREE_TYPE (target_fn_type);
7310 nargs = list_length (target_arg_types);
7311 args = XALLOCAVEC (tree, nargs);
7312 for (arg = target_arg_types, ia = 0;
7313 arg != NULL_TREE && arg != void_list_node;
7314 arg = TREE_CHAIN (arg), ++ia)
7315 args[ia] = TREE_VALUE (arg);
7316 nargs = ia;
7318 for (fns = overload; fns; fns = OVL_NEXT (fns))
7320 tree fn = OVL_CURRENT (fns);
7321 tree instantiation;
7322 tree targs;
7324 if (TREE_CODE (fn) != TEMPLATE_DECL)
7325 /* We're only looking for templates. */
7326 continue;
7328 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7329 != is_ptrmem)
7330 /* We're not looking for a non-static member, and this is
7331 one, or vice versa. */
7332 continue;
7334 tree ret = target_ret_type;
7336 /* If the template has a deduced return type, don't expose it to
7337 template argument deduction. */
7338 if (undeduced_auto_decl (fn))
7339 ret = NULL_TREE;
7341 /* Try to do argument deduction. */
7342 targs = make_tree_vec (DECL_NTPARMS (fn));
7343 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7344 nargs, ret,
7345 DEDUCE_EXACT, LOOKUP_NORMAL,
7346 false, false);
7347 if (instantiation == error_mark_node)
7348 /* Instantiation failed. */
7349 continue;
7351 /* And now force instantiation to do return type deduction. */
7352 if (undeduced_auto_decl (instantiation))
7354 ++function_depth;
7355 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7356 --function_depth;
7358 require_deduced_type (instantiation);
7361 /* See if there's a match. */
7362 if (same_type_p (target_fn_type, static_fn_type (instantiation)))
7363 matches = tree_cons (instantiation, fn, matches);
7365 ggc_free (targs);
7368 /* Now, remove all but the most specialized of the matches. */
7369 if (matches)
7371 tree match = most_specialized_instantiation (matches);
7373 if (match != error_mark_node)
7374 matches = tree_cons (TREE_PURPOSE (match),
7375 NULL_TREE,
7376 NULL_TREE);
7380 /* Now we should have exactly one function in MATCHES. */
7381 if (matches == NULL_TREE)
7383 /* There were *no* matches. */
7384 if (flags & tf_error)
7386 error ("no matches converting function %qD to type %q#T",
7387 DECL_NAME (OVL_CURRENT (overload)),
7388 target_type);
7390 print_candidates (overload);
7392 return error_mark_node;
7394 else if (TREE_CHAIN (matches))
7396 /* There were too many matches. First check if they're all
7397 the same function. */
7398 tree match = NULL_TREE;
7400 fn = TREE_PURPOSE (matches);
7402 /* For multi-versioned functions, more than one match is just fine and
7403 decls_match will return false as they are different. */
7404 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7405 if (!decls_match (fn, TREE_PURPOSE (match))
7406 && !targetm.target_option.function_versions
7407 (fn, TREE_PURPOSE (match)))
7408 break;
7410 if (match)
7412 if (flags & tf_error)
7414 error ("converting overloaded function %qD to type %q#T is ambiguous",
7415 DECL_NAME (OVL_FUNCTION (overload)),
7416 target_type);
7418 /* Since print_candidates expects the functions in the
7419 TREE_VALUE slot, we flip them here. */
7420 for (match = matches; match; match = TREE_CHAIN (match))
7421 TREE_VALUE (match) = TREE_PURPOSE (match);
7423 print_candidates (matches);
7426 return error_mark_node;
7430 /* Good, exactly one match. Now, convert it to the correct type. */
7431 fn = TREE_PURPOSE (matches);
7433 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7434 && !(flags & tf_ptrmem_ok) && !flag_ms_extensions)
7436 static int explained;
7438 if (!(flags & tf_error))
7439 return error_mark_node;
7441 permerror (input_location, "assuming pointer to member %qD", fn);
7442 if (!explained)
7444 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7445 explained = 1;
7449 /* If a pointer to a function that is multi-versioned is requested, the
7450 pointer to the dispatcher function is returned instead. This works
7451 well because indirectly calling the function will dispatch the right
7452 function version at run-time. */
7453 if (DECL_FUNCTION_VERSIONED (fn))
7455 fn = get_function_version_dispatcher (fn);
7456 if (fn == NULL)
7457 return error_mark_node;
7458 /* Mark all the versions corresponding to the dispatcher as used. */
7459 if (!(flags & tf_conv))
7460 mark_versions_used (fn);
7463 /* If we're doing overload resolution purely for the purpose of
7464 determining conversion sequences, we should not consider the
7465 function used. If this conversion sequence is selected, the
7466 function will be marked as used at this point. */
7467 if (!(flags & tf_conv))
7469 /* Make =delete work with SFINAE. */
7470 if (DECL_DELETED_FN (fn) && !(flags & tf_error))
7471 return error_mark_node;
7473 mark_used (fn);
7476 /* We could not check access to member functions when this
7477 expression was originally created since we did not know at that
7478 time to which function the expression referred. */
7479 if (DECL_FUNCTION_MEMBER_P (fn))
7481 gcc_assert (access_path);
7482 perform_or_defer_access_check (access_path, fn, fn, flags);
7485 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7486 return cp_build_addr_expr (fn, flags);
7487 else
7489 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7490 will mark the function as addressed, but here we must do it
7491 explicitly. */
7492 cxx_mark_addressable (fn);
7494 return fn;
7498 /* This function will instantiate the type of the expression given in
7499 RHS to match the type of LHSTYPE. If errors exist, then return
7500 error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then
7501 we complain on errors. If we are not complaining, never modify rhs,
7502 as overload resolution wants to try many possible instantiations, in
7503 the hope that at least one will work.
7505 For non-recursive calls, LHSTYPE should be a function, pointer to
7506 function, or a pointer to member function. */
7508 tree
7509 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
7511 tsubst_flags_t flags_in = flags;
7512 tree access_path = NULL_TREE;
7514 flags &= ~tf_ptrmem_ok;
7516 if (lhstype == unknown_type_node)
7518 if (flags & tf_error)
7519 error ("not enough type information");
7520 return error_mark_node;
7523 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7525 tree fntype = non_reference (lhstype);
7526 if (same_type_p (fntype, TREE_TYPE (rhs)))
7527 return rhs;
7528 if (flag_ms_extensions
7529 && TYPE_PTRMEMFUNC_P (fntype)
7530 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7531 /* Microsoft allows `A::f' to be resolved to a
7532 pointer-to-member. */
7534 else
7536 if (flags & tf_error)
7537 error ("cannot convert %qE from type %qT to type %qT",
7538 rhs, TREE_TYPE (rhs), fntype);
7539 return error_mark_node;
7543 if (BASELINK_P (rhs))
7545 access_path = BASELINK_ACCESS_BINFO (rhs);
7546 rhs = BASELINK_FUNCTIONS (rhs);
7549 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7550 deduce any type information. */
7551 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
7553 if (flags & tf_error)
7554 error ("not enough type information");
7555 return error_mark_node;
7558 /* There only a few kinds of expressions that may have a type
7559 dependent on overload resolution. */
7560 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
7561 || TREE_CODE (rhs) == COMPONENT_REF
7562 || really_overloaded_fn (rhs)
7563 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
7565 /* This should really only be used when attempting to distinguish
7566 what sort of a pointer to function we have. For now, any
7567 arithmetic operation which is not supported on pointers
7568 is rejected as an error. */
7570 switch (TREE_CODE (rhs))
7572 case COMPONENT_REF:
7574 tree member = TREE_OPERAND (rhs, 1);
7576 member = instantiate_type (lhstype, member, flags);
7577 if (member != error_mark_node
7578 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
7579 /* Do not lose object's side effects. */
7580 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
7581 TREE_OPERAND (rhs, 0), member);
7582 return member;
7585 case OFFSET_REF:
7586 rhs = TREE_OPERAND (rhs, 1);
7587 if (BASELINK_P (rhs))
7588 return instantiate_type (lhstype, rhs, flags_in);
7590 /* This can happen if we are forming a pointer-to-member for a
7591 member template. */
7592 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
7594 /* Fall through. */
7596 case TEMPLATE_ID_EXPR:
7598 tree fns = TREE_OPERAND (rhs, 0);
7599 tree args = TREE_OPERAND (rhs, 1);
7601 return
7602 resolve_address_of_overloaded_function (lhstype, fns, flags_in,
7603 /*template_only=*/true,
7604 args, access_path);
7607 case OVERLOAD:
7608 case FUNCTION_DECL:
7609 return
7610 resolve_address_of_overloaded_function (lhstype, rhs, flags_in,
7611 /*template_only=*/false,
7612 /*explicit_targs=*/NULL_TREE,
7613 access_path);
7615 case ADDR_EXPR:
7617 if (PTRMEM_OK_P (rhs))
7618 flags |= tf_ptrmem_ok;
7620 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
7623 case ERROR_MARK:
7624 return error_mark_node;
7626 default:
7627 gcc_unreachable ();
7629 return error_mark_node;
7632 /* Return the name of the virtual function pointer field
7633 (as an IDENTIFIER_NODE) for the given TYPE. Note that
7634 this may have to look back through base types to find the
7635 ultimate field name. (For single inheritance, these could
7636 all be the same name. Who knows for multiple inheritance). */
7638 static tree
7639 get_vfield_name (tree type)
7641 tree binfo, base_binfo;
7642 char *buf;
7644 for (binfo = TYPE_BINFO (type);
7645 BINFO_N_BASE_BINFOS (binfo);
7646 binfo = base_binfo)
7648 base_binfo = BINFO_BASE_BINFO (binfo, 0);
7650 if (BINFO_VIRTUAL_P (base_binfo)
7651 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
7652 break;
7655 type = BINFO_TYPE (binfo);
7656 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
7657 + TYPE_NAME_LENGTH (type) + 2);
7658 sprintf (buf, VFIELD_NAME_FORMAT,
7659 IDENTIFIER_POINTER (constructor_name (type)));
7660 return get_identifier (buf);
7663 void
7664 print_class_statistics (void)
7666 if (! GATHER_STATISTICS)
7667 return;
7669 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
7670 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
7671 if (n_vtables)
7673 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
7674 n_vtables, n_vtable_searches);
7675 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
7676 n_vtable_entries, n_vtable_elems);
7680 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
7681 according to [class]:
7682 The class-name is also inserted
7683 into the scope of the class itself. For purposes of access checking,
7684 the inserted class name is treated as if it were a public member name. */
7686 void
7687 build_self_reference (void)
7689 tree name = constructor_name (current_class_type);
7690 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
7691 tree saved_cas;
7693 DECL_NONLOCAL (value) = 1;
7694 DECL_CONTEXT (value) = current_class_type;
7695 DECL_ARTIFICIAL (value) = 1;
7696 SET_DECL_SELF_REFERENCE_P (value);
7697 set_underlying_type (value);
7699 if (processing_template_decl)
7700 value = push_template_decl (value);
7702 saved_cas = current_access_specifier;
7703 current_access_specifier = access_public_node;
7704 finish_member_declaration (value);
7705 current_access_specifier = saved_cas;
7708 /* Returns 1 if TYPE contains only padding bytes. */
7711 is_empty_class (tree type)
7713 if (type == error_mark_node)
7714 return 0;
7716 if (! CLASS_TYPE_P (type))
7717 return 0;
7719 /* In G++ 3.2, whether or not a class was empty was determined by
7720 looking at its size. */
7721 if (abi_version_at_least (2))
7722 return CLASSTYPE_EMPTY_P (type);
7723 else
7724 return integer_zerop (CLASSTYPE_SIZE (type));
7727 /* Returns true if TYPE contains an empty class. */
7729 static bool
7730 contains_empty_class_p (tree type)
7732 if (is_empty_class (type))
7733 return true;
7734 if (CLASS_TYPE_P (type))
7736 tree field;
7737 tree binfo;
7738 tree base_binfo;
7739 int i;
7741 for (binfo = TYPE_BINFO (type), i = 0;
7742 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7743 if (contains_empty_class_p (BINFO_TYPE (base_binfo)))
7744 return true;
7745 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
7746 if (TREE_CODE (field) == FIELD_DECL
7747 && !DECL_ARTIFICIAL (field)
7748 && is_empty_class (TREE_TYPE (field)))
7749 return true;
7751 else if (TREE_CODE (type) == ARRAY_TYPE)
7752 return contains_empty_class_p (TREE_TYPE (type));
7753 return false;
7756 /* Returns true if TYPE contains no actual data, just various
7757 possible combinations of empty classes and possibly a vptr. */
7759 bool
7760 is_really_empty_class (tree type)
7762 if (CLASS_TYPE_P (type))
7764 tree field;
7765 tree binfo;
7766 tree base_binfo;
7767 int i;
7769 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
7770 out, but we'd like to be able to check this before then. */
7771 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
7772 return true;
7774 for (binfo = TYPE_BINFO (type), i = 0;
7775 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7776 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
7777 return false;
7778 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7779 if (TREE_CODE (field) == FIELD_DECL
7780 && !DECL_ARTIFICIAL (field)
7781 && !is_really_empty_class (TREE_TYPE (field)))
7782 return false;
7783 return true;
7785 else if (TREE_CODE (type) == ARRAY_TYPE)
7786 return is_really_empty_class (TREE_TYPE (type));
7787 return false;
7790 /* Note that NAME was looked up while the current class was being
7791 defined and that the result of that lookup was DECL. */
7793 void
7794 maybe_note_name_used_in_class (tree name, tree decl)
7796 splay_tree names_used;
7798 /* If we're not defining a class, there's nothing to do. */
7799 if (!(innermost_scope_kind() == sk_class
7800 && TYPE_BEING_DEFINED (current_class_type)
7801 && !LAMBDA_TYPE_P (current_class_type)))
7802 return;
7804 /* If there's already a binding for this NAME, then we don't have
7805 anything to worry about. */
7806 if (lookup_member (current_class_type, name,
7807 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
7808 return;
7810 if (!current_class_stack[current_class_depth - 1].names_used)
7811 current_class_stack[current_class_depth - 1].names_used
7812 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
7813 names_used = current_class_stack[current_class_depth - 1].names_used;
7815 splay_tree_insert (names_used,
7816 (splay_tree_key) name,
7817 (splay_tree_value) decl);
7820 /* Note that NAME was declared (as DECL) in the current class. Check
7821 to see that the declaration is valid. */
7823 void
7824 note_name_declared_in_class (tree name, tree decl)
7826 splay_tree names_used;
7827 splay_tree_node n;
7829 /* Look to see if we ever used this name. */
7830 names_used
7831 = current_class_stack[current_class_depth - 1].names_used;
7832 if (!names_used)
7833 return;
7834 /* The C language allows members to be declared with a type of the same
7835 name, and the C++ standard says this diagnostic is not required. So
7836 allow it in extern "C" blocks unless predantic is specified.
7837 Allow it in all cases if -ms-extensions is specified. */
7838 if ((!pedantic && current_lang_name == lang_name_c)
7839 || flag_ms_extensions)
7840 return;
7841 n = splay_tree_lookup (names_used, (splay_tree_key) name);
7842 if (n)
7844 /* [basic.scope.class]
7846 A name N used in a class S shall refer to the same declaration
7847 in its context and when re-evaluated in the completed scope of
7848 S. */
7849 permerror (input_location, "declaration of %q#D", decl);
7850 permerror (input_location, "changes meaning of %qD from %q+#D",
7851 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
7855 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
7856 Secondary vtables are merged with primary vtables; this function
7857 will return the VAR_DECL for the primary vtable. */
7859 tree
7860 get_vtbl_decl_for_binfo (tree binfo)
7862 tree decl;
7864 decl = BINFO_VTABLE (binfo);
7865 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
7867 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
7868 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
7870 if (decl)
7871 gcc_assert (VAR_P (decl));
7872 return decl;
7876 /* Returns the binfo for the primary base of BINFO. If the resulting
7877 BINFO is a virtual base, and it is inherited elsewhere in the
7878 hierarchy, then the returned binfo might not be the primary base of
7879 BINFO in the complete object. Check BINFO_PRIMARY_P or
7880 BINFO_LOST_PRIMARY_P to be sure. */
7882 static tree
7883 get_primary_binfo (tree binfo)
7885 tree primary_base;
7887 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
7888 if (!primary_base)
7889 return NULL_TREE;
7891 return copied_binfo (primary_base, binfo);
7894 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
7896 static int
7897 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
7899 if (!indented_p)
7900 fprintf (stream, "%*s", indent, "");
7901 return 1;
7904 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
7905 INDENT should be zero when called from the top level; it is
7906 incremented recursively. IGO indicates the next expected BINFO in
7907 inheritance graph ordering. */
7909 static tree
7910 dump_class_hierarchy_r (FILE *stream,
7911 int flags,
7912 tree binfo,
7913 tree igo,
7914 int indent)
7916 int indented = 0;
7917 tree base_binfo;
7918 int i;
7920 indented = maybe_indent_hierarchy (stream, indent, 0);
7921 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
7922 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
7923 (HOST_WIDE_INT) (uintptr_t) binfo);
7924 if (binfo != igo)
7926 fprintf (stream, "alternative-path\n");
7927 return igo;
7929 igo = TREE_CHAIN (binfo);
7931 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
7932 tree_low_cst (BINFO_OFFSET (binfo), 0));
7933 if (is_empty_class (BINFO_TYPE (binfo)))
7934 fprintf (stream, " empty");
7935 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
7936 fprintf (stream, " nearly-empty");
7937 if (BINFO_VIRTUAL_P (binfo))
7938 fprintf (stream, " virtual");
7939 fprintf (stream, "\n");
7941 indented = 0;
7942 if (BINFO_PRIMARY_P (binfo))
7944 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7945 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
7946 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
7947 TFF_PLAIN_IDENTIFIER),
7948 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
7950 if (BINFO_LOST_PRIMARY_P (binfo))
7952 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7953 fprintf (stream, " lost-primary");
7955 if (indented)
7956 fprintf (stream, "\n");
7958 if (!(flags & TDF_SLIM))
7960 int indented = 0;
7962 if (BINFO_SUBVTT_INDEX (binfo))
7964 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7965 fprintf (stream, " subvttidx=%s",
7966 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
7967 TFF_PLAIN_IDENTIFIER));
7969 if (BINFO_VPTR_INDEX (binfo))
7971 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7972 fprintf (stream, " vptridx=%s",
7973 expr_as_string (BINFO_VPTR_INDEX (binfo),
7974 TFF_PLAIN_IDENTIFIER));
7976 if (BINFO_VPTR_FIELD (binfo))
7978 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7979 fprintf (stream, " vbaseoffset=%s",
7980 expr_as_string (BINFO_VPTR_FIELD (binfo),
7981 TFF_PLAIN_IDENTIFIER));
7983 if (BINFO_VTABLE (binfo))
7985 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7986 fprintf (stream, " vptr=%s",
7987 expr_as_string (BINFO_VTABLE (binfo),
7988 TFF_PLAIN_IDENTIFIER));
7991 if (indented)
7992 fprintf (stream, "\n");
7995 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7996 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
7998 return igo;
8001 /* Dump the BINFO hierarchy for T. */
8003 static void
8004 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
8006 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8007 fprintf (stream, " size=%lu align=%lu\n",
8008 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
8009 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8010 fprintf (stream, " base size=%lu base align=%lu\n",
8011 (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0)
8012 / BITS_PER_UNIT),
8013 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8014 / BITS_PER_UNIT));
8015 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8016 fprintf (stream, "\n");
8019 /* Debug interface to hierarchy dumping. */
8021 void
8022 debug_class (tree t)
8024 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8027 static void
8028 dump_class_hierarchy (tree t)
8030 int flags;
8031 FILE *stream = dump_begin (TDI_class, &flags);
8033 if (stream)
8035 dump_class_hierarchy_1 (stream, flags, t);
8036 dump_end (TDI_class, stream);
8040 static void
8041 dump_array (FILE * stream, tree decl)
8043 tree value;
8044 unsigned HOST_WIDE_INT ix;
8045 HOST_WIDE_INT elt;
8046 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8048 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
8049 / BITS_PER_UNIT);
8050 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8051 fprintf (stream, " %s entries",
8052 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8053 TFF_PLAIN_IDENTIFIER));
8054 fprintf (stream, "\n");
8056 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8057 ix, value)
8058 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8059 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8062 static void
8063 dump_vtable (tree t, tree binfo, tree vtable)
8065 int flags;
8066 FILE *stream = dump_begin (TDI_class, &flags);
8068 if (!stream)
8069 return;
8071 if (!(flags & TDF_SLIM))
8073 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8075 fprintf (stream, "%s for %s",
8076 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8077 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8078 if (ctor_vtbl_p)
8080 if (!BINFO_VIRTUAL_P (binfo))
8081 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8082 (HOST_WIDE_INT) (uintptr_t) binfo);
8083 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8085 fprintf (stream, "\n");
8086 dump_array (stream, vtable);
8087 fprintf (stream, "\n");
8090 dump_end (TDI_class, stream);
8093 static void
8094 dump_vtt (tree t, tree vtt)
8096 int flags;
8097 FILE *stream = dump_begin (TDI_class, &flags);
8099 if (!stream)
8100 return;
8102 if (!(flags & TDF_SLIM))
8104 fprintf (stream, "VTT for %s\n",
8105 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8106 dump_array (stream, vtt);
8107 fprintf (stream, "\n");
8110 dump_end (TDI_class, stream);
8113 /* Dump a function or thunk and its thunkees. */
8115 static void
8116 dump_thunk (FILE *stream, int indent, tree thunk)
8118 static const char spaces[] = " ";
8119 tree name = DECL_NAME (thunk);
8120 tree thunks;
8122 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8123 (void *)thunk,
8124 !DECL_THUNK_P (thunk) ? "function"
8125 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8126 name ? IDENTIFIER_POINTER (name) : "<unset>");
8127 if (DECL_THUNK_P (thunk))
8129 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8130 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8132 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8133 if (!virtual_adjust)
8134 /*NOP*/;
8135 else if (DECL_THIS_THUNK_P (thunk))
8136 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8137 tree_low_cst (virtual_adjust, 0));
8138 else
8139 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8140 tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust), 0),
8141 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8142 if (THUNK_ALIAS (thunk))
8143 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8145 fprintf (stream, "\n");
8146 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8147 dump_thunk (stream, indent + 2, thunks);
8150 /* Dump the thunks for FN. */
8152 void
8153 debug_thunks (tree fn)
8155 dump_thunk (stderr, 0, fn);
8158 /* Virtual function table initialization. */
8160 /* Create all the necessary vtables for T and its base classes. */
8162 static void
8163 finish_vtbls (tree t)
8165 tree vbase;
8166 vec<constructor_elt, va_gc> *v = NULL;
8167 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8169 /* We lay out the primary and secondary vtables in one contiguous
8170 vtable. The primary vtable is first, followed by the non-virtual
8171 secondary vtables in inheritance graph order. */
8172 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8173 vtable, t, &v);
8175 /* Then come the virtual bases, also in inheritance graph order. */
8176 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8178 if (!BINFO_VIRTUAL_P (vbase))
8179 continue;
8180 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8183 if (BINFO_VTABLE (TYPE_BINFO (t)))
8184 initialize_vtable (TYPE_BINFO (t), v);
8187 /* Initialize the vtable for BINFO with the INITS. */
8189 static void
8190 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8192 tree decl;
8194 layout_vtable_decl (binfo, vec_safe_length (inits));
8195 decl = get_vtbl_decl_for_binfo (binfo);
8196 initialize_artificial_var (decl, inits);
8197 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8200 /* Build the VTT (virtual table table) for T.
8201 A class requires a VTT if it has virtual bases.
8203 This holds
8204 1 - primary virtual pointer for complete object T
8205 2 - secondary VTTs for each direct non-virtual base of T which requires a
8207 3 - secondary virtual pointers for each direct or indirect base of T which
8208 has virtual bases or is reachable via a virtual path from T.
8209 4 - secondary VTTs for each direct or indirect virtual base of T.
8211 Secondary VTTs look like complete object VTTs without part 4. */
8213 static void
8214 build_vtt (tree t)
8216 tree type;
8217 tree vtt;
8218 tree index;
8219 vec<constructor_elt, va_gc> *inits;
8221 /* Build up the initializers for the VTT. */
8222 inits = NULL;
8223 index = size_zero_node;
8224 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8226 /* If we didn't need a VTT, we're done. */
8227 if (!inits)
8228 return;
8230 /* Figure out the type of the VTT. */
8231 type = build_array_of_n_type (const_ptr_type_node,
8232 inits->length ());
8234 /* Now, build the VTT object itself. */
8235 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8236 initialize_artificial_var (vtt, inits);
8237 /* Add the VTT to the vtables list. */
8238 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8239 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8241 dump_vtt (t, vtt);
8244 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8245 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8246 and CHAIN the vtable pointer for this binfo after construction is
8247 complete. VALUE can also be another BINFO, in which case we recurse. */
8249 static tree
8250 binfo_ctor_vtable (tree binfo)
8252 tree vt;
8254 while (1)
8256 vt = BINFO_VTABLE (binfo);
8257 if (TREE_CODE (vt) == TREE_LIST)
8258 vt = TREE_VALUE (vt);
8259 if (TREE_CODE (vt) == TREE_BINFO)
8260 binfo = vt;
8261 else
8262 break;
8265 return vt;
8268 /* Data for secondary VTT initialization. */
8269 typedef struct secondary_vptr_vtt_init_data_s
8271 /* Is this the primary VTT? */
8272 bool top_level_p;
8274 /* Current index into the VTT. */
8275 tree index;
8277 /* Vector of initializers built up. */
8278 vec<constructor_elt, va_gc> *inits;
8280 /* The type being constructed by this secondary VTT. */
8281 tree type_being_constructed;
8282 } secondary_vptr_vtt_init_data;
8284 /* Recursively build the VTT-initializer for BINFO (which is in the
8285 hierarchy dominated by T). INITS points to the end of the initializer
8286 list to date. INDEX is the VTT index where the next element will be
8287 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8288 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8289 for virtual bases of T. When it is not so, we build the constructor
8290 vtables for the BINFO-in-T variant. */
8292 static void
8293 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8294 tree *index)
8296 int i;
8297 tree b;
8298 tree init;
8299 secondary_vptr_vtt_init_data data;
8300 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8302 /* We only need VTTs for subobjects with virtual bases. */
8303 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8304 return;
8306 /* We need to use a construction vtable if this is not the primary
8307 VTT. */
8308 if (!top_level_p)
8310 build_ctor_vtbl_group (binfo, t);
8312 /* Record the offset in the VTT where this sub-VTT can be found. */
8313 BINFO_SUBVTT_INDEX (binfo) = *index;
8316 /* Add the address of the primary vtable for the complete object. */
8317 init = binfo_ctor_vtable (binfo);
8318 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8319 if (top_level_p)
8321 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8322 BINFO_VPTR_INDEX (binfo) = *index;
8324 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8326 /* Recursively add the secondary VTTs for non-virtual bases. */
8327 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8328 if (!BINFO_VIRTUAL_P (b))
8329 build_vtt_inits (b, t, inits, index);
8331 /* Add secondary virtual pointers for all subobjects of BINFO with
8332 either virtual bases or reachable along a virtual path, except
8333 subobjects that are non-virtual primary bases. */
8334 data.top_level_p = top_level_p;
8335 data.index = *index;
8336 data.inits = *inits;
8337 data.type_being_constructed = BINFO_TYPE (binfo);
8339 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8341 *index = data.index;
8343 /* data.inits might have grown as we added secondary virtual pointers.
8344 Make sure our caller knows about the new vector. */
8345 *inits = data.inits;
8347 if (top_level_p)
8348 /* Add the secondary VTTs for virtual bases in inheritance graph
8349 order. */
8350 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8352 if (!BINFO_VIRTUAL_P (b))
8353 continue;
8355 build_vtt_inits (b, t, inits, index);
8357 else
8358 /* Remove the ctor vtables we created. */
8359 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8362 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8363 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8365 static tree
8366 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8368 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8370 /* We don't care about bases that don't have vtables. */
8371 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8372 return dfs_skip_bases;
8374 /* We're only interested in proper subobjects of the type being
8375 constructed. */
8376 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8377 return NULL_TREE;
8379 /* We're only interested in bases with virtual bases or reachable
8380 via a virtual path from the type being constructed. */
8381 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8382 || binfo_via_virtual (binfo, data->type_being_constructed)))
8383 return dfs_skip_bases;
8385 /* We're not interested in non-virtual primary bases. */
8386 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8387 return NULL_TREE;
8389 /* Record the index where this secondary vptr can be found. */
8390 if (data->top_level_p)
8392 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8393 BINFO_VPTR_INDEX (binfo) = data->index;
8395 if (BINFO_VIRTUAL_P (binfo))
8397 /* It's a primary virtual base, and this is not a
8398 construction vtable. Find the base this is primary of in
8399 the inheritance graph, and use that base's vtable
8400 now. */
8401 while (BINFO_PRIMARY_P (binfo))
8402 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8406 /* Add the initializer for the secondary vptr itself. */
8407 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8409 /* Advance the vtt index. */
8410 data->index = size_binop (PLUS_EXPR, data->index,
8411 TYPE_SIZE_UNIT (ptr_type_node));
8413 return NULL_TREE;
8416 /* Called from build_vtt_inits via dfs_walk. After building
8417 constructor vtables and generating the sub-vtt from them, we need
8418 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8419 binfo of the base whose sub vtt was generated. */
8421 static tree
8422 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8424 tree vtable = BINFO_VTABLE (binfo);
8426 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8427 /* If this class has no vtable, none of its bases do. */
8428 return dfs_skip_bases;
8430 if (!vtable)
8431 /* This might be a primary base, so have no vtable in this
8432 hierarchy. */
8433 return NULL_TREE;
8435 /* If we scribbled the construction vtable vptr into BINFO, clear it
8436 out now. */
8437 if (TREE_CODE (vtable) == TREE_LIST
8438 && (TREE_PURPOSE (vtable) == (tree) data))
8439 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8441 return NULL_TREE;
8444 /* Build the construction vtable group for BINFO which is in the
8445 hierarchy dominated by T. */
8447 static void
8448 build_ctor_vtbl_group (tree binfo, tree t)
8450 tree type;
8451 tree vtbl;
8452 tree id;
8453 tree vbase;
8454 vec<constructor_elt, va_gc> *v;
8456 /* See if we've already created this construction vtable group. */
8457 id = mangle_ctor_vtbl_for_type (t, binfo);
8458 if (IDENTIFIER_GLOBAL_VALUE (id))
8459 return;
8461 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8462 /* Build a version of VTBL (with the wrong type) for use in
8463 constructing the addresses of secondary vtables in the
8464 construction vtable group. */
8465 vtbl = build_vtable (t, id, ptr_type_node);
8466 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8467 /* Don't export construction vtables from shared libraries. Even on
8468 targets that don't support hidden visibility, this tells
8469 can_refer_decl_in_current_unit_p not to assume that it's safe to
8470 access from a different compilation unit (bz 54314). */
8471 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8472 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8474 v = NULL;
8475 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8476 binfo, vtbl, t, &v);
8478 /* Add the vtables for each of our virtual bases using the vbase in T
8479 binfo. */
8480 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8481 vbase;
8482 vbase = TREE_CHAIN (vbase))
8484 tree b;
8486 if (!BINFO_VIRTUAL_P (vbase))
8487 continue;
8488 b = copied_binfo (vbase, binfo);
8490 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8493 /* Figure out the type of the construction vtable. */
8494 type = build_array_of_n_type (vtable_entry_type, v->length ());
8495 layout_type (type);
8496 TREE_TYPE (vtbl) = type;
8497 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8498 layout_decl (vtbl, 0);
8500 /* Initialize the construction vtable. */
8501 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8502 initialize_artificial_var (vtbl, v);
8503 dump_vtable (t, binfo, vtbl);
8506 /* Add the vtbl initializers for BINFO (and its bases other than
8507 non-virtual primaries) to the list of INITS. BINFO is in the
8508 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8509 the constructor the vtbl inits should be accumulated for. (If this
8510 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8511 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8512 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8513 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8514 but are not necessarily the same in terms of layout. */
8516 static void
8517 accumulate_vtbl_inits (tree binfo,
8518 tree orig_binfo,
8519 tree rtti_binfo,
8520 tree vtbl,
8521 tree t,
8522 vec<constructor_elt, va_gc> **inits)
8524 int i;
8525 tree base_binfo;
8526 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8528 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8530 /* If it doesn't have a vptr, we don't do anything. */
8531 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8532 return;
8534 /* If we're building a construction vtable, we're not interested in
8535 subobjects that don't require construction vtables. */
8536 if (ctor_vtbl_p
8537 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8538 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8539 return;
8541 /* Build the initializers for the BINFO-in-T vtable. */
8542 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8544 /* Walk the BINFO and its bases. We walk in preorder so that as we
8545 initialize each vtable we can figure out at what offset the
8546 secondary vtable lies from the primary vtable. We can't use
8547 dfs_walk here because we need to iterate through bases of BINFO
8548 and RTTI_BINFO simultaneously. */
8549 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8551 /* Skip virtual bases. */
8552 if (BINFO_VIRTUAL_P (base_binfo))
8553 continue;
8554 accumulate_vtbl_inits (base_binfo,
8555 BINFO_BASE_BINFO (orig_binfo, i),
8556 rtti_binfo, vtbl, t,
8557 inits);
8561 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8562 BINFO vtable to L. */
8564 static void
8565 dfs_accumulate_vtbl_inits (tree binfo,
8566 tree orig_binfo,
8567 tree rtti_binfo,
8568 tree orig_vtbl,
8569 tree t,
8570 vec<constructor_elt, va_gc> **l)
8572 tree vtbl = NULL_TREE;
8573 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8574 int n_inits;
8576 if (ctor_vtbl_p
8577 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
8579 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
8580 primary virtual base. If it is not the same primary in
8581 the hierarchy of T, we'll need to generate a ctor vtable
8582 for it, to place at its location in T. If it is the same
8583 primary, we still need a VTT entry for the vtable, but it
8584 should point to the ctor vtable for the base it is a
8585 primary for within the sub-hierarchy of RTTI_BINFO.
8587 There are three possible cases:
8589 1) We are in the same place.
8590 2) We are a primary base within a lost primary virtual base of
8591 RTTI_BINFO.
8592 3) We are primary to something not a base of RTTI_BINFO. */
8594 tree b;
8595 tree last = NULL_TREE;
8597 /* First, look through the bases we are primary to for RTTI_BINFO
8598 or a virtual base. */
8599 b = binfo;
8600 while (BINFO_PRIMARY_P (b))
8602 b = BINFO_INHERITANCE_CHAIN (b);
8603 last = b;
8604 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8605 goto found;
8607 /* If we run out of primary links, keep looking down our
8608 inheritance chain; we might be an indirect primary. */
8609 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
8610 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8611 break;
8612 found:
8614 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
8615 base B and it is a base of RTTI_BINFO, this is case 2. In
8616 either case, we share our vtable with LAST, i.e. the
8617 derived-most base within B of which we are a primary. */
8618 if (b == rtti_binfo
8619 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
8620 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
8621 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
8622 binfo_ctor_vtable after everything's been set up. */
8623 vtbl = last;
8625 /* Otherwise, this is case 3 and we get our own. */
8627 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
8628 return;
8630 n_inits = vec_safe_length (*l);
8632 if (!vtbl)
8634 tree index;
8635 int non_fn_entries;
8637 /* Add the initializer for this vtable. */
8638 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
8639 &non_fn_entries, l);
8641 /* Figure out the position to which the VPTR should point. */
8642 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
8643 index = size_binop (MULT_EXPR,
8644 TYPE_SIZE_UNIT (vtable_entry_type),
8645 size_int (non_fn_entries + n_inits));
8646 vtbl = fold_build_pointer_plus (vtbl, index);
8649 if (ctor_vtbl_p)
8650 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
8651 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
8652 straighten this out. */
8653 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
8654 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
8655 /* Throw away any unneeded intializers. */
8656 (*l)->truncate (n_inits);
8657 else
8658 /* For an ordinary vtable, set BINFO_VTABLE. */
8659 BINFO_VTABLE (binfo) = vtbl;
8662 static GTY(()) tree abort_fndecl_addr;
8664 /* Construct the initializer for BINFO's virtual function table. BINFO
8665 is part of the hierarchy dominated by T. If we're building a
8666 construction vtable, the ORIG_BINFO is the binfo we should use to
8667 find the actual function pointers to put in the vtable - but they
8668 can be overridden on the path to most-derived in the graph that
8669 ORIG_BINFO belongs. Otherwise,
8670 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
8671 BINFO that should be indicated by the RTTI information in the
8672 vtable; it will be a base class of T, rather than T itself, if we
8673 are building a construction vtable.
8675 The value returned is a TREE_LIST suitable for wrapping in a
8676 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
8677 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
8678 number of non-function entries in the vtable.
8680 It might seem that this function should never be called with a
8681 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
8682 base is always subsumed by a derived class vtable. However, when
8683 we are building construction vtables, we do build vtables for
8684 primary bases; we need these while the primary base is being
8685 constructed. */
8687 static void
8688 build_vtbl_initializer (tree binfo,
8689 tree orig_binfo,
8690 tree t,
8691 tree rtti_binfo,
8692 int* non_fn_entries_p,
8693 vec<constructor_elt, va_gc> **inits)
8695 tree v;
8696 vtbl_init_data vid;
8697 unsigned ix, jx;
8698 tree vbinfo;
8699 vec<tree, va_gc> *vbases;
8700 constructor_elt *e;
8702 /* Initialize VID. */
8703 memset (&vid, 0, sizeof (vid));
8704 vid.binfo = binfo;
8705 vid.derived = t;
8706 vid.rtti_binfo = rtti_binfo;
8707 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8708 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8709 vid.generate_vcall_entries = true;
8710 /* The first vbase or vcall offset is at index -3 in the vtable. */
8711 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
8713 /* Add entries to the vtable for RTTI. */
8714 build_rtti_vtbl_entries (binfo, &vid);
8716 /* Create an array for keeping track of the functions we've
8717 processed. When we see multiple functions with the same
8718 signature, we share the vcall offsets. */
8719 vec_alloc (vid.fns, 32);
8720 /* Add the vcall and vbase offset entries. */
8721 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
8723 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
8724 build_vbase_offset_vtbl_entries. */
8725 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
8726 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
8727 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
8729 /* If the target requires padding between data entries, add that now. */
8730 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
8732 int n_entries = vec_safe_length (vid.inits);
8734 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
8736 /* Move data entries into their new positions and add padding
8737 after the new positions. Iterate backwards so we don't
8738 overwrite entries that we would need to process later. */
8739 for (ix = n_entries - 1;
8740 vid.inits->iterate (ix, &e);
8741 ix--)
8743 int j;
8744 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
8745 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
8747 (*vid.inits)[new_position] = *e;
8749 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
8751 constructor_elt *f = &(*vid.inits)[new_position - j];
8752 f->index = NULL_TREE;
8753 f->value = build1 (NOP_EXPR, vtable_entry_type,
8754 null_pointer_node);
8759 if (non_fn_entries_p)
8760 *non_fn_entries_p = vec_safe_length (vid.inits);
8762 /* The initializers for virtual functions were built up in reverse
8763 order. Straighten them out and add them to the running list in one
8764 step. */
8765 jx = vec_safe_length (*inits);
8766 vec_safe_grow (*inits, jx + vid.inits->length ());
8768 for (ix = vid.inits->length () - 1;
8769 vid.inits->iterate (ix, &e);
8770 ix--, jx++)
8771 (**inits)[jx] = *e;
8773 /* Go through all the ordinary virtual functions, building up
8774 initializers. */
8775 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
8777 tree delta;
8778 tree vcall_index;
8779 tree fn, fn_original;
8780 tree init = NULL_TREE;
8782 fn = BV_FN (v);
8783 fn_original = fn;
8784 if (DECL_THUNK_P (fn))
8786 if (!DECL_NAME (fn))
8787 finish_thunk (fn);
8788 if (THUNK_ALIAS (fn))
8790 fn = THUNK_ALIAS (fn);
8791 BV_FN (v) = fn;
8793 fn_original = THUNK_TARGET (fn);
8796 /* If the only definition of this function signature along our
8797 primary base chain is from a lost primary, this vtable slot will
8798 never be used, so just zero it out. This is important to avoid
8799 requiring extra thunks which cannot be generated with the function.
8801 We first check this in update_vtable_entry_for_fn, so we handle
8802 restored primary bases properly; we also need to do it here so we
8803 zero out unused slots in ctor vtables, rather than filling them
8804 with erroneous values (though harmless, apart from relocation
8805 costs). */
8806 if (BV_LOST_PRIMARY (v))
8807 init = size_zero_node;
8809 if (! init)
8811 /* Pull the offset for `this', and the function to call, out of
8812 the list. */
8813 delta = BV_DELTA (v);
8814 vcall_index = BV_VCALL_INDEX (v);
8816 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
8817 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8819 /* You can't call an abstract virtual function; it's abstract.
8820 So, we replace these functions with __pure_virtual. */
8821 if (DECL_PURE_VIRTUAL_P (fn_original))
8823 fn = abort_fndecl;
8824 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8826 if (abort_fndecl_addr == NULL)
8827 abort_fndecl_addr
8828 = fold_convert (vfunc_ptr_type_node,
8829 build_fold_addr_expr (fn));
8830 init = abort_fndecl_addr;
8833 /* Likewise for deleted virtuals. */
8834 else if (DECL_DELETED_FN (fn_original))
8836 fn = get_identifier ("__cxa_deleted_virtual");
8837 if (!get_global_value_if_present (fn, &fn))
8838 fn = push_library_fn (fn, (build_function_type_list
8839 (void_type_node, NULL_TREE)),
8840 NULL_TREE);
8841 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8842 init = fold_convert (vfunc_ptr_type_node,
8843 build_fold_addr_expr (fn));
8845 else
8847 if (!integer_zerop (delta) || vcall_index)
8849 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
8850 if (!DECL_NAME (fn))
8851 finish_thunk (fn);
8853 /* Take the address of the function, considering it to be of an
8854 appropriate generic type. */
8855 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8856 init = fold_convert (vfunc_ptr_type_node,
8857 build_fold_addr_expr (fn));
8861 /* And add it to the chain of initializers. */
8862 if (TARGET_VTABLE_USES_DESCRIPTORS)
8864 int i;
8865 if (init == size_zero_node)
8866 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8867 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8868 else
8869 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8871 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
8872 fn, build_int_cst (NULL_TREE, i));
8873 TREE_CONSTANT (fdesc) = 1;
8875 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
8878 else
8879 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8883 /* Adds to vid->inits the initializers for the vbase and vcall
8884 offsets in BINFO, which is in the hierarchy dominated by T. */
8886 static void
8887 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
8889 tree b;
8891 /* If this is a derived class, we must first create entries
8892 corresponding to the primary base class. */
8893 b = get_primary_binfo (binfo);
8894 if (b)
8895 build_vcall_and_vbase_vtbl_entries (b, vid);
8897 /* Add the vbase entries for this base. */
8898 build_vbase_offset_vtbl_entries (binfo, vid);
8899 /* Add the vcall entries for this base. */
8900 build_vcall_offset_vtbl_entries (binfo, vid);
8903 /* Returns the initializers for the vbase offset entries in the vtable
8904 for BINFO (which is part of the class hierarchy dominated by T), in
8905 reverse order. VBASE_OFFSET_INDEX gives the vtable index
8906 where the next vbase offset will go. */
8908 static void
8909 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
8911 tree vbase;
8912 tree t;
8913 tree non_primary_binfo;
8915 /* If there are no virtual baseclasses, then there is nothing to
8916 do. */
8917 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8918 return;
8920 t = vid->derived;
8922 /* We might be a primary base class. Go up the inheritance hierarchy
8923 until we find the most derived class of which we are a primary base:
8924 it is the offset of that which we need to use. */
8925 non_primary_binfo = binfo;
8926 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8928 tree b;
8930 /* If we have reached a virtual base, then it must be a primary
8931 base (possibly multi-level) of vid->binfo, or we wouldn't
8932 have called build_vcall_and_vbase_vtbl_entries for it. But it
8933 might be a lost primary, so just skip down to vid->binfo. */
8934 if (BINFO_VIRTUAL_P (non_primary_binfo))
8936 non_primary_binfo = vid->binfo;
8937 break;
8940 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8941 if (get_primary_binfo (b) != non_primary_binfo)
8942 break;
8943 non_primary_binfo = b;
8946 /* Go through the virtual bases, adding the offsets. */
8947 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8948 vbase;
8949 vbase = TREE_CHAIN (vbase))
8951 tree b;
8952 tree delta;
8954 if (!BINFO_VIRTUAL_P (vbase))
8955 continue;
8957 /* Find the instance of this virtual base in the complete
8958 object. */
8959 b = copied_binfo (vbase, binfo);
8961 /* If we've already got an offset for this virtual base, we
8962 don't need another one. */
8963 if (BINFO_VTABLE_PATH_MARKED (b))
8964 continue;
8965 BINFO_VTABLE_PATH_MARKED (b) = 1;
8967 /* Figure out where we can find this vbase offset. */
8968 delta = size_binop (MULT_EXPR,
8969 vid->index,
8970 convert (ssizetype,
8971 TYPE_SIZE_UNIT (vtable_entry_type)));
8972 if (vid->primary_vtbl_p)
8973 BINFO_VPTR_FIELD (b) = delta;
8975 if (binfo != TYPE_BINFO (t))
8976 /* The vbase offset had better be the same. */
8977 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
8979 /* The next vbase will come at a more negative offset. */
8980 vid->index = size_binop (MINUS_EXPR, vid->index,
8981 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
8983 /* The initializer is the delta from BINFO to this virtual base.
8984 The vbase offsets go in reverse inheritance-graph order, and
8985 we are walking in inheritance graph order so these end up in
8986 the right order. */
8987 delta = size_diffop_loc (input_location,
8988 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
8990 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
8991 fold_build1_loc (input_location, NOP_EXPR,
8992 vtable_entry_type, delta));
8996 /* Adds the initializers for the vcall offset entries in the vtable
8997 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
8998 to VID->INITS. */
9000 static void
9001 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9003 /* We only need these entries if this base is a virtual base. We
9004 compute the indices -- but do not add to the vtable -- when
9005 building the main vtable for a class. */
9006 if (binfo == TYPE_BINFO (vid->derived)
9007 || (BINFO_VIRTUAL_P (binfo)
9008 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9009 correspond to VID->DERIVED), we are building a primary
9010 construction virtual table. Since this is a primary
9011 virtual table, we do not need the vcall offsets for
9012 BINFO. */
9013 && binfo != vid->rtti_binfo))
9015 /* We need a vcall offset for each of the virtual functions in this
9016 vtable. For example:
9018 class A { virtual void f (); };
9019 class B1 : virtual public A { virtual void f (); };
9020 class B2 : virtual public A { virtual void f (); };
9021 class C: public B1, public B2 { virtual void f (); };
9023 A C object has a primary base of B1, which has a primary base of A. A
9024 C also has a secondary base of B2, which no longer has a primary base
9025 of A. So the B2-in-C construction vtable needs a secondary vtable for
9026 A, which will adjust the A* to a B2* to call f. We have no way of
9027 knowing what (or even whether) this offset will be when we define B2,
9028 so we store this "vcall offset" in the A sub-vtable and look it up in
9029 a "virtual thunk" for B2::f.
9031 We need entries for all the functions in our primary vtable and
9032 in our non-virtual bases' secondary vtables. */
9033 vid->vbase = binfo;
9034 /* If we are just computing the vcall indices -- but do not need
9035 the actual entries -- not that. */
9036 if (!BINFO_VIRTUAL_P (binfo))
9037 vid->generate_vcall_entries = false;
9038 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9039 add_vcall_offset_vtbl_entries_r (binfo, vid);
9043 /* Build vcall offsets, starting with those for BINFO. */
9045 static void
9046 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9048 int i;
9049 tree primary_binfo;
9050 tree base_binfo;
9052 /* Don't walk into virtual bases -- except, of course, for the
9053 virtual base for which we are building vcall offsets. Any
9054 primary virtual base will have already had its offsets generated
9055 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9056 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9057 return;
9059 /* If BINFO has a primary base, process it first. */
9060 primary_binfo = get_primary_binfo (binfo);
9061 if (primary_binfo)
9062 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9064 /* Add BINFO itself to the list. */
9065 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9067 /* Scan the non-primary bases of BINFO. */
9068 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9069 if (base_binfo != primary_binfo)
9070 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9073 /* Called from build_vcall_offset_vtbl_entries_r. */
9075 static void
9076 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9078 /* Make entries for the rest of the virtuals. */
9079 if (abi_version_at_least (2))
9081 tree orig_fn;
9083 /* The ABI requires that the methods be processed in declaration
9084 order. G++ 3.2 used the order in the vtable. */
9085 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
9086 orig_fn;
9087 orig_fn = DECL_CHAIN (orig_fn))
9088 if (DECL_VINDEX (orig_fn))
9089 add_vcall_offset (orig_fn, binfo, vid);
9091 else
9093 tree derived_virtuals;
9094 tree base_virtuals;
9095 tree orig_virtuals;
9096 /* If BINFO is a primary base, the most derived class which has
9097 BINFO as a primary base; otherwise, just BINFO. */
9098 tree non_primary_binfo;
9100 /* We might be a primary base class. Go up the inheritance hierarchy
9101 until we find the most derived class of which we are a primary base:
9102 it is the BINFO_VIRTUALS there that we need to consider. */
9103 non_primary_binfo = binfo;
9104 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9106 tree b;
9108 /* If we have reached a virtual base, then it must be vid->vbase,
9109 because we ignore other virtual bases in
9110 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
9111 base (possibly multi-level) of vid->binfo, or we wouldn't
9112 have called build_vcall_and_vbase_vtbl_entries for it. But it
9113 might be a lost primary, so just skip down to vid->binfo. */
9114 if (BINFO_VIRTUAL_P (non_primary_binfo))
9116 gcc_assert (non_primary_binfo == vid->vbase);
9117 non_primary_binfo = vid->binfo;
9118 break;
9121 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9122 if (get_primary_binfo (b) != non_primary_binfo)
9123 break;
9124 non_primary_binfo = b;
9127 if (vid->ctor_vtbl_p)
9128 /* For a ctor vtable we need the equivalent binfo within the hierarchy
9129 where rtti_binfo is the most derived type. */
9130 non_primary_binfo
9131 = original_binfo (non_primary_binfo, vid->rtti_binfo);
9133 for (base_virtuals = BINFO_VIRTUALS (binfo),
9134 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
9135 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
9136 base_virtuals;
9137 base_virtuals = TREE_CHAIN (base_virtuals),
9138 derived_virtuals = TREE_CHAIN (derived_virtuals),
9139 orig_virtuals = TREE_CHAIN (orig_virtuals))
9141 tree orig_fn;
9143 /* Find the declaration that originally caused this function to
9144 be present in BINFO_TYPE (binfo). */
9145 orig_fn = BV_FN (orig_virtuals);
9147 /* When processing BINFO, we only want to generate vcall slots for
9148 function slots introduced in BINFO. So don't try to generate
9149 one if the function isn't even defined in BINFO. */
9150 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), DECL_CONTEXT (orig_fn)))
9151 continue;
9153 add_vcall_offset (orig_fn, binfo, vid);
9158 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9160 static void
9161 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9163 size_t i;
9164 tree vcall_offset;
9165 tree derived_entry;
9167 /* If there is already an entry for a function with the same
9168 signature as FN, then we do not need a second vcall offset.
9169 Check the list of functions already present in the derived
9170 class vtable. */
9171 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9173 if (same_signature_p (derived_entry, orig_fn)
9174 /* We only use one vcall offset for virtual destructors,
9175 even though there are two virtual table entries. */
9176 || (DECL_DESTRUCTOR_P (derived_entry)
9177 && DECL_DESTRUCTOR_P (orig_fn)))
9178 return;
9181 /* If we are building these vcall offsets as part of building
9182 the vtable for the most derived class, remember the vcall
9183 offset. */
9184 if (vid->binfo == TYPE_BINFO (vid->derived))
9186 tree_pair_s elt = {orig_fn, vid->index};
9187 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9190 /* The next vcall offset will be found at a more negative
9191 offset. */
9192 vid->index = size_binop (MINUS_EXPR, vid->index,
9193 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9195 /* Keep track of this function. */
9196 vec_safe_push (vid->fns, orig_fn);
9198 if (vid->generate_vcall_entries)
9200 tree base;
9201 tree fn;
9203 /* Find the overriding function. */
9204 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9205 if (fn == error_mark_node)
9206 vcall_offset = build_zero_cst (vtable_entry_type);
9207 else
9209 base = TREE_VALUE (fn);
9211 /* The vbase we're working on is a primary base of
9212 vid->binfo. But it might be a lost primary, so its
9213 BINFO_OFFSET might be wrong, so we just use the
9214 BINFO_OFFSET from vid->binfo. */
9215 vcall_offset = size_diffop_loc (input_location,
9216 BINFO_OFFSET (base),
9217 BINFO_OFFSET (vid->binfo));
9218 vcall_offset = fold_build1_loc (input_location,
9219 NOP_EXPR, vtable_entry_type,
9220 vcall_offset);
9222 /* Add the initializer to the vtable. */
9223 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9227 /* Return vtbl initializers for the RTTI entries corresponding to the
9228 BINFO's vtable. The RTTI entries should indicate the object given
9229 by VID->rtti_binfo. */
9231 static void
9232 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9234 tree b;
9235 tree t;
9236 tree offset;
9237 tree decl;
9238 tree init;
9240 t = BINFO_TYPE (vid->rtti_binfo);
9242 /* To find the complete object, we will first convert to our most
9243 primary base, and then add the offset in the vtbl to that value. */
9244 b = binfo;
9245 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9246 && !BINFO_LOST_PRIMARY_P (b))
9248 tree primary_base;
9250 primary_base = get_primary_binfo (b);
9251 gcc_assert (BINFO_PRIMARY_P (primary_base)
9252 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9253 b = primary_base;
9255 offset = size_diffop_loc (input_location,
9256 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9258 /* The second entry is the address of the typeinfo object. */
9259 if (flag_rtti)
9260 decl = build_address (get_tinfo_decl (t));
9261 else
9262 decl = integer_zero_node;
9264 /* Convert the declaration to a type that can be stored in the
9265 vtable. */
9266 init = build_nop (vfunc_ptr_type_node, decl);
9267 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9269 /* Add the offset-to-top entry. It comes earlier in the vtable than
9270 the typeinfo entry. Convert the offset to look like a
9271 function pointer, so that we can put it in the vtable. */
9272 init = build_nop (vfunc_ptr_type_node, offset);
9273 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9276 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9277 accessibility. */
9279 bool
9280 uniquely_derived_from_p (tree parent, tree type)
9282 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9283 return base && base != error_mark_node;
9286 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9288 bool
9289 publicly_uniquely_derived_p (tree parent, tree type)
9291 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9292 NULL, tf_none);
9293 return base && base != error_mark_node;
9296 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9297 class between them, if any. */
9299 tree
9300 common_enclosing_class (tree ctx1, tree ctx2)
9302 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9303 return NULL_TREE;
9304 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9305 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9306 if (ctx1 == ctx2)
9307 return ctx1;
9308 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9309 TYPE_MARKED_P (t) = true;
9310 tree found = NULL_TREE;
9311 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9312 if (TYPE_MARKED_P (t))
9314 found = t;
9315 break;
9317 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9318 TYPE_MARKED_P (t) = false;
9319 return found;
9322 #include "gt-cp-class.h"