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