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