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