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