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