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