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