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