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