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