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