]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/class.c
New test case
[thirdparty/gcc.git] / gcc / cp / class.c
CommitLineData
8d08fdba 1/* Functions related to building classes and their related objects.
06ceef4e
RK
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
8d08fdba
MS
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
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
13GNU CC is distributed in the hope that it will be useful,
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
19along with GNU CC; 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"
e7a587ef 28#include "tree.h"
8d08fdba
MS
29#include "cp-tree.h"
30#include "flags.h"
28cbf42c 31#include "rtl.h"
e8abc66f 32#include "output.h"
54f92bfb 33#include "toplev.h"
9cd64686 34#include "ggc.h"
11028a53 35#include "lex.h"
8d08fdba
MS
36
37#include "obstack.h"
38#define obstack_chunk_alloc xmalloc
39#define obstack_chunk_free free
40
8d08fdba 41/* This is how we tell when two virtual member functions are really the
e92cc029 42 same. */
8d08fdba
MS
43#define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
44
158991b7 45extern void set_class_shadows PARAMS ((tree));
8d08fdba 46
61a127b3
MM
47/* The number of nested classes being processed. If we are not in the
48 scope of any class, this is zero. */
49
8d08fdba
MS
50int current_class_depth;
51
61a127b3
MM
52/* In order to deal with nested classes, we keep a stack of classes.
53 The topmost entry is the innermost class, and is the entry at index
54 CURRENT_CLASS_DEPTH */
55
56typedef struct class_stack_node {
57 /* The name of the class. */
58 tree name;
59
60 /* The _TYPE node for the class. */
61 tree type;
62
63 /* The access specifier pending for new declarations in the scope of
64 this class. */
65 tree access;
8f032717
MM
66
67 /* If were defining TYPE, the names used in this class. */
68 splay_tree names_used;
61a127b3
MM
69}* class_stack_node_t;
70
71/* The stack itself. This is an dynamically resized array. The
72 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
73static int current_class_stack_size;
74static class_stack_node_t current_class_stack;
75
158991b7
KG
76static tree get_vfield_name PARAMS ((tree));
77static void finish_struct_anon PARAMS ((tree));
78static tree build_vbase_pointer PARAMS ((tree, tree));
c0bbf652 79static tree build_vtable_entry PARAMS ((tree, tree, tree));
158991b7
KG
80static tree get_vtable_name PARAMS ((tree));
81static tree get_derived_offset PARAMS ((tree, tree));
82static tree get_basefndecls PARAMS ((tree, tree));
83static void set_rtti_entry PARAMS ((tree, tree, tree));
28531dd0
MM
84static int build_primary_vtable PARAMS ((tree, tree));
85static int build_secondary_vtable PARAMS ((tree, tree));
158991b7 86static tree dfs_finish_vtbls PARAMS ((tree, void *));
8d7a5379 87static tree dfs_accumulate_vtbl_inits PARAMS ((tree, void *));
158991b7 88static void finish_vtbls PARAMS ((tree));
5e19c053 89static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
158991b7
KG
90static void add_virtual_function PARAMS ((tree *, tree *, int *, tree, tree));
91static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
92static void delete_duplicate_fields PARAMS ((tree));
93static void finish_struct_bits PARAMS ((tree));
aa52c1ff 94static int alter_access PARAMS ((tree, tree, tree));
158991b7
KG
95static void handle_using_decl PARAMS ((tree, tree));
96static int overrides PARAMS ((tree, tree));
97static int strictly_overrides PARAMS ((tree, tree));
158991b7
KG
98static void mark_overriders PARAMS ((tree, tree));
99static void check_for_override PARAMS ((tree, tree));
158991b7
KG
100static tree dfs_modify_vtables PARAMS ((tree, void *));
101static tree modify_all_vtables PARAMS ((tree, int *, tree));
102static void determine_primary_base PARAMS ((tree, int *));
103static void finish_struct_methods PARAMS ((tree));
104static void maybe_warn_about_overly_private_class PARAMS ((tree));
105static int field_decl_cmp PARAMS ((const tree *, const tree *));
106static int method_name_cmp PARAMS ((const tree *, const tree *));
107static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
108static tree fixed_type_or_null PARAMS ((tree, int *));
109static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
d8e178a0 110 int, tree));
158991b7 111static void build_vtable_entry_ref PARAMS ((tree, tree, tree));
158991b7
KG
112static tree build_vtbl_initializer PARAMS ((tree, tree));
113static int count_fields PARAMS ((tree));
114static int add_fields_to_vec PARAMS ((tree, tree, int));
115static void check_bitfield_decl PARAMS ((tree));
116static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
117static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *,
607cf131 118 int *));
5c24fba6
MM
119static void build_base_field PARAMS ((record_layout_info, tree, int *,
120 unsigned int *, varray_type *));
121static varray_type build_base_fields PARAMS ((record_layout_info, int *));
122static tree build_vbase_pointer_fields PARAMS ((record_layout_info, int *));
07a3462a
JW
123static tree build_vtbl_or_vbase_field PARAMS ((tree, tree, tree, tree, tree,
124 int *));
158991b7
KG
125static void check_methods PARAMS ((tree));
126static void remove_zero_width_bit_fields PARAMS ((tree));
127static void check_bases PARAMS ((tree, int *, int *, int *));
128static void check_bases_and_members PARAMS ((tree, int *));
5c24fba6 129static tree create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
158991b7
KG
130static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
131static void fixup_pending_inline PARAMS ((struct pending_inline *));
132static void fixup_inline_methods PARAMS ((tree));
133static void set_primary_base PARAMS ((tree, int, int *));
134static tree dfs_propagate_binfo_offsets PARAMS ((tree, void *));
135static void propagate_binfo_offsets PARAMS ((tree, tree));
9785e4b1 136static void layout_virtual_bases PARAMS ((tree, varray_type *));
158991b7
KG
137static tree dfs_set_offset_for_shared_vbases PARAMS ((tree, void *));
138static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
139static tree dfs_build_vbase_offset_vtbl_entries PARAMS ((tree, void *));
140static tree build_vbase_offset_vtbl_entries PARAMS ((tree, tree));
141static tree dfs_vcall_offset_queue_p PARAMS ((tree, void *));
142static tree dfs_build_vcall_offset_vtbl_entries PARAMS ((tree, void *));
143static tree build_vcall_offset_vtbl_entries PARAMS ((tree, tree));
144static tree dfs_count_virtuals PARAMS ((tree, void *));
145static void start_vtable PARAMS ((tree, int *));
146static void layout_vtable_decl PARAMS ((tree, int));
147static int num_vfun_entries PARAMS ((tree));
5e19c053
MM
148static tree dfs_find_final_overrider PARAMS ((tree, void *));
149static tree find_final_overrider PARAMS ((tree, tree, tree));
150static tree dfs_find_base PARAMS ((tree, void *));
28531dd0 151static int make_new_vtable PARAMS ((tree, tree));
70a51bda 152extern void dump_class_hierarchy PARAMS ((tree, int));
b9f39201 153static tree build_vtable PARAMS ((tree, tree, tree));
8d7a5379 154static void initialize_vtable PARAMS ((tree, tree));
5c24fba6
MM
155static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
156 tree, tree,
157 varray_type));
9785e4b1
MM
158static tree dfs_record_base_offsets PARAMS ((tree, void *));
159static void record_base_offsets PARAMS ((tree, varray_type *));
160static tree dfs_search_base_offsets PARAMS ((tree, void *));
161static int layout_conflict_p PARAMS ((tree, varray_type));
162static unsigned HOST_WIDE_INT end_of_class PARAMS ((tree, int));
163static void layout_empty_base PARAMS ((tree, tree, varray_type));
3c9d6359 164static void accumulate_vtbl_inits PARAMS ((tree, tree));
8d08fdba 165
51c184be 166/* Variables shared between class.c and call.c. */
8d08fdba 167
5566b478 168#ifdef GATHER_STATISTICS
8d08fdba
MS
169int n_vtables = 0;
170int n_vtable_entries = 0;
171int n_vtable_searches = 0;
172int n_vtable_elems = 0;
173int n_convert_harshness = 0;
174int n_compute_conversion_costs = 0;
175int n_build_method_call = 0;
176int n_inner_fields_searched = 0;
5566b478 177#endif
8d08fdba 178
f8361147
MM
179/* Virtual base class layout. */
180
181/* Returns a list of virtual base class pointers as a chain of
182 FIELD_DECLS. */
e92cc029 183
bd6dd845 184static tree
5c24fba6
MM
185build_vbase_pointer_fields (rli, empty_p)
186 record_layout_info rli;
f8361147 187 int *empty_p;
8d08fdba 188{
f8361147
MM
189 /* Chain to hold all the new FIELD_DECLs which point at virtual
190 base classes. */
5c24fba6 191 tree rec = rli->t;
f8361147
MM
192 tree vbase_decls = NULL_TREE;
193 tree binfos = TYPE_BINFO_BASETYPES (rec);
194 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
195 tree decl;
196 int i;
8d08fdba 197
bbd15aac
MM
198 /* Under the new ABI, there are no vbase pointers in the object.
199 Instead, the offsets are stored in the vtable. */
200 if (vbase_offsets_in_vtable_p ())
201 return NULL_TREE;
f8361147 202
bbd15aac 203 /* Loop over the baseclasses, adding vbase pointers as needed. */
f8361147
MM
204 for (i = 0; i < n_baseclasses; i++)
205 {
206 register tree base_binfo = TREE_VEC_ELT (binfos, i);
207 register tree basetype = BINFO_TYPE (base_binfo);
208
d0f062fb 209 if (!COMPLETE_TYPE_P (basetype))
f8361147
MM
210 /* This error is now reported in xref_tag, thus giving better
211 location information. */
212 continue;
213
214 /* All basetypes are recorded in the association list of the
215 derived type. */
216
217 if (TREE_VIA_VIRTUAL (base_binfo))
218 {
219 int j;
220 const char *name;
221
222 /* The offset for a virtual base class is only used in computing
223 virtual function tables and for initializing virtual base
224 pointers. It is built once `get_vbase_types' is called. */
225
226 /* If this basetype can come from another vbase pointer
227 without an additional indirection, we will share
228 that pointer. If an indirection is involved, we
229 make our own pointer. */
230 for (j = 0; j < n_baseclasses; j++)
231 {
232 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
233 if (! TREE_VIA_VIRTUAL (other_base_binfo)
234 && BINFO_FOR_VBASE (basetype, BINFO_TYPE (other_base_binfo)))
235 goto got_it;
236 }
237 FORMAT_VBASE_NAME (name, basetype);
238 decl = build_vtbl_or_vbase_field (get_identifier (name),
239 get_identifier (VTABLE_BASE),
240 build_pointer_type (basetype),
241 rec,
07a3462a 242 basetype,
f8361147
MM
243 empty_p);
244 BINFO_VPTR_FIELD (base_binfo) = decl;
245 TREE_CHAIN (decl) = vbase_decls;
770ae6cc 246 place_field (rli, decl);
f8361147
MM
247 vbase_decls = decl;
248 *empty_p = 0;
249
250 got_it:
251 /* The space this decl occupies has already been accounted for. */
252 ;
253 }
254 }
255
256 return vbase_decls;
8d08fdba
MS
257}
258
f8361147 259/* Called from build_vbase_offset_vtbl_entries via dfs_walk. */
e92cc029 260
f8361147
MM
261static tree
262dfs_build_vbase_offset_vtbl_entries (binfo, data)
263 tree binfo;
264 void *data;
8d08fdba 265{
f8361147
MM
266 tree list = (tree) data;
267
268 if (TREE_TYPE (list) == binfo)
269 /* The TREE_TYPE of LIST is the base class from which we started
270 walking. If that BINFO is virtual it's not a virtual baseclass
271 of itself. */
272 ;
273 else if (TREE_VIA_VIRTUAL (binfo))
8d08fdba 274 {
f8361147 275 tree init;
bbd15aac
MM
276 tree vbase;
277
278 /* Remember the index to the vbase offset for this virtual
279 base. */
3c9d6359 280 vbase = BINFO_FOR_VBASE (BINFO_TYPE (binfo), TREE_PURPOSE (list));
bbd15aac
MM
281 if (!TREE_VALUE (list))
282 BINFO_VPTR_FIELD (vbase) = build_int_2 (-1, 0);
283 else
284 {
285 BINFO_VPTR_FIELD (vbase) = TREE_PURPOSE (TREE_VALUE (list));
fed3cef0
RK
286 BINFO_VPTR_FIELD (vbase)
287 = fold (build (MINUS_EXPR, integer_type_node,
288 BINFO_VPTR_FIELD (vbase), integer_one_node));
bbd15aac 289 }
8d08fdba 290
bbd15aac
MM
291 /* And record the offset at which this virtual base lies in the
292 vtable. */
f8361147 293 init = BINFO_OFFSET (binfo);
bbd15aac 294 TREE_VALUE (list) = tree_cons (BINFO_VPTR_FIELD (vbase),
fed3cef0 295 init, TREE_VALUE (list));
f8361147
MM
296 }
297
298 SET_BINFO_VTABLE_PATH_MARKED (binfo);
299
300 return NULL_TREE;
301}
302
bbd15aac
MM
303/* Returns the initializers for the vbase offset entries in the vtable
304 for BINFO (which is part of the class hierarchy dominated by T), in
305 reverse order. */
f8361147
MM
306
307static tree
bbd15aac 308build_vbase_offset_vtbl_entries (binfo, t)
f8361147 309 tree binfo;
bbd15aac 310 tree t;
f8361147 311{
f8361147
MM
312 tree inits;
313 tree init;
bbd15aac 314 tree list;
f8361147
MM
315
316 /* Under the old ABI, pointers to virtual bases are stored in each
317 object. */
bbd15aac 318 if (!vbase_offsets_in_vtable_p ())
f8361147
MM
319 return NULL_TREE;
320
321 /* If there are no virtual baseclasses, then there is nothing to
322 do. */
bbd15aac 323 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
f8361147
MM
324 return NULL_TREE;
325
326 inits = NULL_TREE;
327
bbd15aac
MM
328 /* The offsets are allocated in the reverse order of a
329 depth-first left-to-right traversal of the hierarchy. We use
330 BINFO_VTABLE_PATH_MARKED because we are ourselves during a
331 dfs_walk, and so BINFO_MARKED is already in use. */
332 list = build_tree_list (t, NULL_TREE);
333 TREE_TYPE (list) = binfo;
334 dfs_walk (binfo,
335 dfs_build_vbase_offset_vtbl_entries,
336 dfs_vtable_path_unmarked_real_bases_queue_p,
337 list);
338 dfs_walk (binfo,
339 dfs_vtable_path_unmark,
340 dfs_vtable_path_marked_real_bases_queue_p,
341 list);
342 inits = nreverse (TREE_VALUE (list));
f8361147 343
1a588ad7 344 /* We've now got offsets in the right order. However, the offsets
f8361147
MM
345 we've stored are offsets from the beginning of the complete
346 object, and we need offsets from this BINFO. */
347 for (init = inits; init; init = TREE_CHAIN (init))
348 {
bbd15aac
MM
349 /* The dfs_build_vbase_offset_vtbl_entries routine uses the
350 TREE_PURPOSE to scribble in. But, we need to clear it now so
351 that the values are not perceived as labeled initializers. */
352 TREE_PURPOSE (init) = NULL_TREE;
fed3cef0
RK
353 TREE_VALUE (init)
354 = fold (build1 (NOP_EXPR, vtable_entry_type,
355 size_diffop (TREE_VALUE (init),
356 BINFO_OFFSET (binfo))));
f8361147
MM
357 }
358
359 return inits;
360}
361
1a588ad7
MM
362typedef struct vcall_offset_data_s
363{
364 /* The binfo for the most-derived type. */
365 tree derived;
366 /* The binfo for the virtual base for which we're building
367 initializers. */
368 tree vbase;
369 /* The vcall offset initializers built up so far. */
370 tree inits;
371 /* The number of vcall offsets accumulated. */
372 int offsets;
373} vcall_offset_data;
374
375/* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
376
377static tree
378dfs_vcall_offset_queue_p (binfo, data)
379 tree binfo;
380 void *data;
381{
382 vcall_offset_data* vod = (vcall_offset_data *) data;
383
384 return (binfo == vod->vbase) ? binfo : dfs_skip_vbases (binfo, NULL);
385}
386
387/* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
388
389static tree
390dfs_build_vcall_offset_vtbl_entries (binfo, data)
391 tree binfo;
392 void *data;
393{
394 vcall_offset_data* vod;
395 tree virtuals;
396 tree binfo_inits;
397
398 /* Primary bases are not interesting; all of the virtual
399 function table entries have been overridden. */
400 if (BINFO_PRIMARY_MARKED_P (binfo))
401 return NULL_TREE;
402
403 vod = (vcall_offset_data *) data;
404 binfo_inits = NULL_TREE;
405
406 /* We chain the offsets on in reverse order. That's correct --
407 build_vtbl_initializer will straighten them out. */
408 for (virtuals = skip_rtti_stuff (binfo,
409 BINFO_TYPE (binfo),
410 NULL);
411 virtuals;
412 virtuals = TREE_CHAIN (virtuals))
413 {
1a588ad7 414 /* Figure out what function we're looking at. */
fed3cef0
RK
415 tree fn = TREE_VALUE (virtuals);
416 tree base = DECL_CONTEXT (fn);
7d52ae23
MM
417 /* The FN comes from BASE. So, we must caculate the adjustment
418 from the virtual base that derived from BINFO to BASE. */
fed3cef0
RK
419 tree base_binfo = get_binfo (base, vod->derived, /*protect=*/0);
420
421 binfo_inits
422 = tree_cons (NULL_TREE,
423 fold (build1 (NOP_EXPR, vtable_entry_type,
424 size_diffop (BINFO_OFFSET (base_binfo),
425 BINFO_OFFSET (vod->vbase)))),
426 binfo_inits);
1a588ad7
MM
427 }
428
429 /* Now add the initializers we've just created to the list that will
430 be returned to our caller. */
431 vod->inits = chainon (vod->inits, binfo_inits);
432
433 return NULL_TREE;
434}
435
436/* Returns the initializers for the vcall offset entries in the vtable
437 for BINFO (which is part of the class hierarchy dominated by T), in
438 reverse order. */
439
440static tree
441build_vcall_offset_vtbl_entries (binfo, t)
442 tree binfo;
443 tree t;
444{
445 vcall_offset_data vod;
446
447 /* Under the old ABI, the adjustments to the `this' pointer were made
448 elsewhere. */
449 if (!vcall_offsets_in_vtable_p ())
450 return NULL_TREE;
451
452 /* We only need these entries if this base is a virtual base. */
453 if (!TREE_VIA_VIRTUAL (binfo))
454 return NULL_TREE;
455
456 /* We need a vcall offset for each of the virtual functions in this
457 vtable. For example:
458
459 class A { virtual void f (); };
460 class B : virtual public A { };
461 class C: virtual public A, public B {};
462
463 Now imagine:
464
465 B* b = new C;
466 b->f();
467
468 The location of `A' is not at a fixed offset relative to `B'; the
469 offset depends on the complete object derived from `B'. So,
470 `B' vtable contains an entry for `f' that indicates by what
471 amount the `this' pointer for `B' needs to be adjusted to arrive
472 at `A'.
473
474 We need entries for all the functions in our primary vtable and
475 in our non-virtual bases vtables. For each base, the entries
476 appear in the same order as in the base; but the bases themselves
477 appear in reverse depth-first, left-to-right order. */
478 vod.derived = t;
479 vod.vbase = binfo;
480 vod.inits = NULL_TREE;
481 dfs_walk (binfo,
482 dfs_build_vcall_offset_vtbl_entries,
483 dfs_vcall_offset_queue_p,
484 &vod);
485
486 return vod.inits;
487}
488
f8361147 489/* Returns a pointer to the virtual base class of EXP that has the
bbd15aac 490 indicated TYPE. EXP is of class type, not a pointer type. */
f8361147
MM
491
492static tree
493build_vbase_pointer (exp, type)
494 tree exp, type;
495{
bbd15aac
MM
496 if (vbase_offsets_in_vtable_p ())
497 {
498 tree vbase;
499 tree vbase_ptr;
500
501 /* Find the shared copy of TYPE; that's where the vtable offset
502 is recorded. */
503 vbase = BINFO_FOR_VBASE (type, TREE_TYPE (exp));
504 /* Find the virtual function table pointer. */
505 vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
506 /* Compute the location where the offset will lie. */
507 vbase_ptr = build_binary_op (PLUS_EXPR,
508 vbase_ptr,
509 BINFO_VPTR_FIELD (vbase));
510 vbase_ptr = build1 (NOP_EXPR,
511 build_pointer_type (ptrdiff_type_node),
512 vbase_ptr);
513 /* Add the contents of this location to EXP. */
514 return build (PLUS_EXPR,
515 build_pointer_type (type),
516 build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
517 build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
518 }
519 else
520 {
521 char *name;
522 FORMAT_VBASE_NAME (name, type);
523 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
524 }
8d08fdba
MS
525}
526
527/* Build multi-level access to EXPR using hierarchy path PATH.
528 CODE is PLUS_EXPR if we are going with the grain,
529 and MINUS_EXPR if we are not (in which case, we cannot traverse
530 virtual baseclass links).
531
532 TYPE is the type we want this path to have on exit.
533
51ddb82e
JM
534 NONNULL is non-zero if we know (for any reason) that EXPR is
535 not, in fact, zero. */
e92cc029 536
8d08fdba 537tree
51ddb82e 538build_vbase_path (code, type, expr, path, nonnull)
8d08fdba
MS
539 enum tree_code code;
540 tree type, expr, path;
51ddb82e 541 int nonnull;
8d08fdba
MS
542{
543 register int changed = 0;
544 tree last = NULL_TREE, last_virtual = NULL_TREE;
6633d636 545 int fixed_type_p;
8d08fdba
MS
546 tree null_expr = 0, nonnull_expr;
547 tree basetype;
548 tree offset = integer_zero_node;
549
6633d636
MS
550 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
551 return build1 (NOP_EXPR, type, expr);
552
51ddb82e
JM
553 /* We could do better if we had additional logic to convert back to the
554 unconverted type (the static type of the complete object), and then
555 convert back to the type we want. Until that is done, we only optimize
556 if the complete type is the same type as expr has. */
6633d636 557 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
8d08fdba
MS
558
559 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
560 expr = save_expr (expr);
561 nonnull_expr = expr;
562
5e19c053 563 path = reverse_path (path);
8d08fdba
MS
564
565 basetype = BINFO_TYPE (path);
566
567 while (path)
568 {
5e19c053 569 if (TREE_VIA_VIRTUAL (TREE_VALUE (path)))
8d08fdba 570 {
5e19c053 571 last_virtual = BINFO_TYPE (TREE_VALUE (path));
8d08fdba
MS
572 if (code == PLUS_EXPR)
573 {
574 changed = ! fixed_type_p;
575
576 if (changed)
577 {
8d08fdba
MS
578 tree ind;
579
580 /* We already check for ambiguous things in the caller, just
e92cc029 581 find a path. */
8d08fdba
MS
582 if (last)
583 {
584 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
585 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
586 }
587 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
588 nonnull_expr = build_vbase_pointer (ind, last_virtual);
a9aedbc2 589 if (nonnull == 0
84663f74 590 && TREE_CODE (type) == POINTER_TYPE
8d08fdba
MS
591 && null_expr == NULL_TREE)
592 {
f30432d7
MS
593 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
594 expr = build (COND_EXPR, build_pointer_type (last_virtual),
b7484fbe 595 build (EQ_EXPR, boolean_type_node, expr,
8d08fdba
MS
596 integer_zero_node),
597 null_expr, nonnull_expr);
598 }
599 }
600 /* else we'll figure out the offset below. */
601
602 /* Happens in the case of parse errors. */
603 if (nonnull_expr == error_mark_node)
604 return error_mark_node;
605 }
606 else
607 {
8251199e 608 cp_error ("cannot cast up from virtual baseclass `%T'",
8d08fdba
MS
609 last_virtual);
610 return error_mark_node;
611 }
612 }
5e19c053
MM
613 last = TREE_VALUE (path);
614 path = TREE_CHAIN (path);
8d08fdba
MS
615 }
616 /* LAST is now the last basetype assoc on the path. */
617
618 /* A pointer to a virtual base member of a non-null object
619 is non-null. Therefore, we only need to test for zeroness once.
620 Make EXPR the canonical expression to deal with here. */
621 if (null_expr)
622 {
623 TREE_OPERAND (expr, 2) = nonnull_expr;
b9ddcfac
JM
624 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
625 = TREE_TYPE (nonnull_expr);
8d08fdba
MS
626 }
627 else
628 expr = nonnull_expr;
629
630 /* If we go through any virtual base pointers, make sure that
631 casts to BASETYPE from the last virtual base class use
632 the right value for BASETYPE. */
633 if (changed)
634 {
635 tree intype = TREE_TYPE (TREE_TYPE (expr));
fed3cef0 636
f30432d7 637 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
fed3cef0
RK
638 offset
639 = BINFO_OFFSET (get_binfo (last, TYPE_MAIN_VARIANT (intype), 0));
8d08fdba
MS
640 }
641 else
9d4c0187 642 offset = BINFO_OFFSET (last);
8d08fdba 643
665f2503 644 if (! integer_zerop (offset))
8d08fdba 645 {
59be85d7 646 /* Bash types to make the backend happy. */
37c46b43 647 offset = cp_convert (type, offset);
59be85d7 648
51ddb82e 649 /* If expr might be 0, we need to preserve that zeroness. */
f30432d7 650 if (nonnull == 0)
8d08fdba
MS
651 {
652 if (null_expr)
653 TREE_TYPE (null_expr) = type;
654 else
655 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
656 if (TREE_SIDE_EFFECTS (expr))
657 expr = save_expr (expr);
658
659 return build (COND_EXPR, type,
b7484fbe 660 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
8d08fdba
MS
661 null_expr,
662 build (code, type, expr, offset));
663 }
664 else return build (code, type, expr, offset);
665 }
666
667 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
668 be used multiple times in initialization of multiple inheritance. */
669 if (null_expr)
670 {
671 TREE_TYPE (expr) = type;
672 return expr;
673 }
674 else
675 return build1 (NOP_EXPR, type, expr);
676}
677
f8361147 678\f
8d08fdba
MS
679/* Virtual function things. */
680
c0bbf652
MM
681/* Build an entry in the virtual function table. DELTA is the offset
682 for the `this' pointer. VCALL_INDEX is the vtable index containing
7d52ae23
MM
683 the vcall offset; zero if none. ENTRY is the virtual function
684 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
685 but it may not actually be a virtual function table pointer. (For
686 example, it might be the address of the RTTI object, under the new
687 ABI.) */
e92cc029 688
bd6dd845 689static tree
7d52ae23 690build_vtable_entry (delta, vcall_index, entry)
c0bbf652
MM
691 tree delta;
692 tree vcall_index;
7d52ae23 693 tree entry;
8d08fdba 694{
8926095f
MS
695 if (flag_vtable_thunks)
696 {
c0bbf652
MM
697 HOST_WIDE_INT idelta;
698 HOST_WIDE_INT ivindex;
699
665f2503
RK
700 idelta = tree_low_cst (delta, 0);
701 ivindex = tree_low_cst (vcall_index, 0);
c0bbf652 702 if ((idelta || ivindex)
7d52ae23 703 && ! DECL_PURE_VIRTUAL_P (TREE_OPERAND (entry, 0)))
8926095f 704 {
7d52ae23
MM
705 entry = make_thunk (entry, idelta, ivindex);
706 entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
707 TREE_READONLY (entry) = 1;
708 TREE_CONSTANT (entry) = 1;
8926095f
MS
709 }
710#ifdef GATHER_STATISTICS
711 n_vtable_entries += 1;
712#endif
7d52ae23 713 return entry;
8926095f
MS
714 }
715 else
716 {
717 extern int flag_huge_objects;
e1b3e07d
MM
718 tree elems = tree_cons (NULL_TREE, delta,
719 tree_cons (NULL_TREE, integer_zero_node,
7d52ae23 720 build_tree_list (NULL_TREE, entry)));
8926095f
MS
721 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
722
c0bbf652
MM
723 /* We don't use vcall offsets when not using vtable thunks. */
724 my_friendly_assert (integer_zerop (vcall_index), 20000125);
725
329745f7
JM
726 /* DELTA used to be constructed by `size_int' and/or size_binop,
727 which caused overflow problems when it was negative. That should
728 be fixed now. */
8926095f 729
329745f7 730 if (! int_fits_type_p (delta, delta_type_node))
a703fb38
KG
731 {
732 if (flag_huge_objects)
733 sorry ("object size exceeds built-in limit for virtual function table implementation");
734 else
735 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
736 }
737
8926095f
MS
738 TREE_CONSTANT (entry) = 1;
739 TREE_STATIC (entry) = 1;
740 TREE_READONLY (entry) = 1;
8d08fdba
MS
741
742#ifdef GATHER_STATISTICS
8926095f 743 n_vtable_entries += 1;
8d08fdba
MS
744#endif
745
8926095f
MS
746 return entry;
747 }
8d08fdba
MS
748}
749
a1dd0d36
JM
750/* We want to give the assembler the vtable identifier as well as
751 the offset to the function pointer. So we generate
752
59fa060f 753 __asm__ __volatile__ (".vtable_entry %c0, %c1"
a1dd0d36
JM
754 : : "s"(&class_vtable),
755 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
756
757static void
758build_vtable_entry_ref (basetype, vtbl, idx)
759 tree basetype, vtbl, idx;
760{
59fa060f 761 static char asm_stmt[] = ".vtable_entry %c0, %c1";
a1dd0d36
JM
762 tree s, i, i2;
763
764 s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
765 s = build_tree_list (build_string (1, "s"), s);
766
767 i = build_array_ref (vtbl, idx);
768 if (!flag_vtable_thunks)
769 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
770 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
771 i2 = build_array_ref (vtbl, build_int_2(0,0));
772 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
337c90cc 773 i = build_binary_op (MINUS_EXPR, i, i2);
a1dd0d36
JM
774 i = build_tree_list (build_string (1, "i"), i);
775
11028a53
JM
776 finish_asm_stmt (ridpointers[RID_VOLATILE],
777 build_string (sizeof(asm_stmt)-1, asm_stmt),
778 NULL_TREE, chainon (s, i), NULL_TREE);
a1dd0d36
JM
779}
780
8d08fdba 781/* Given an object INSTANCE, return an expression which yields the
6b5fbb55
MS
782 virtual function vtable element corresponding to INDEX. There are
783 many special cases for INSTANCE which we take care of here, mainly
784 to avoid creating extra tree nodes when we don't have to. */
e92cc029 785
8d08fdba 786tree
6b5fbb55
MS
787build_vtbl_ref (instance, idx)
788 tree instance, idx;
8d08fdba 789{
8d08fdba
MS
790 tree vtbl, aref;
791 tree basetype = TREE_TYPE (instance);
792
793 if (TREE_CODE (basetype) == REFERENCE_TYPE)
794 basetype = TREE_TYPE (basetype);
795
4ac14744 796 if (instance == current_class_ref)
849da744 797 vtbl = build_vfield_ref (instance, basetype);
8d08fdba
MS
798 else
799 {
800 if (optimize)
801 {
802 /* Try to figure out what a reference refers to, and
803 access its virtual function table directly. */
804 tree ref = NULL_TREE;
805
806 if (TREE_CODE (instance) == INDIRECT_REF
807 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
808 ref = TREE_OPERAND (instance, 0);
809 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
810 ref = instance;
811
812 if (ref && TREE_CODE (ref) == VAR_DECL
813 && DECL_INITIAL (ref))
814 {
815 tree init = DECL_INITIAL (ref);
816
817 while (TREE_CODE (init) == NOP_EXPR
818 || TREE_CODE (init) == NON_LVALUE_EXPR)
819 init = TREE_OPERAND (init, 0);
820 if (TREE_CODE (init) == ADDR_EXPR)
821 {
822 init = TREE_OPERAND (init, 0);
823 if (IS_AGGR_TYPE (TREE_TYPE (init))
824 && (TREE_CODE (init) == PARM_DECL
825 || TREE_CODE (init) == VAR_DECL))
826 instance = init;
827 }
828 }
829 }
830
831 if (IS_AGGR_TYPE (TREE_TYPE (instance))
8d08fdba
MS
832 && (TREE_CODE (instance) == RESULT_DECL
833 || TREE_CODE (instance) == PARM_DECL
834 || TREE_CODE (instance) == VAR_DECL))
835 vtbl = TYPE_BINFO_VTABLE (basetype);
836 else
849da744 837 vtbl = build_vfield_ref (instance, basetype);
8d08fdba 838 }
a1dd0d36 839
e3417fcd 840 assemble_external (vtbl);
a1dd0d36
JM
841
842 if (flag_vtable_gc)
843 build_vtable_entry_ref (basetype, vtbl, idx);
844
8d08fdba
MS
845 aref = build_array_ref (vtbl, idx);
846
6b5fbb55
MS
847 return aref;
848}
849
850/* Given an object INSTANCE, return an expression which yields the
851 virtual function corresponding to INDEX. There are many special
852 cases for INSTANCE which we take care of here, mainly to avoid
853 creating extra tree nodes when we don't have to. */
e92cc029 854
6b5fbb55
MS
855tree
856build_vfn_ref (ptr_to_instptr, instance, idx)
857 tree *ptr_to_instptr, instance;
858 tree idx;
859{
860 tree aref = build_vtbl_ref (instance, idx);
8d08fdba 861
6b5fbb55
MS
862 /* When using thunks, there is no extra delta, and we get the pfn
863 directly. */
8926095f
MS
864 if (flag_vtable_thunks)
865 return aref;
6b5fbb55
MS
866
867 if (ptr_to_instptr)
8926095f 868 {
6b5fbb55
MS
869 /* Save the intermediate result in a SAVE_EXPR so we don't have to
870 compute each component of the virtual function pointer twice. */
871 if (TREE_CODE (aref) == INDIRECT_REF)
872 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
873
874 *ptr_to_instptr
875 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
876 *ptr_to_instptr,
37c46b43
MS
877 cp_convert (ptrdiff_type_node,
878 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
8926095f 879 }
6b5fbb55
MS
880
881 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
8d08fdba
MS
882}
883
8d08fdba
MS
884/* Return the name of the virtual function table (as an IDENTIFIER_NODE)
885 for the given TYPE. */
e92cc029 886
8d08fdba
MS
887static tree
888get_vtable_name (type)
889 tree type;
890{
891 tree type_id = build_typename_overload (type);
486837a7 892 char *buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
2636fde4 893 + IDENTIFIER_LENGTH (type_id) + 2);
d8e178a0 894 const char *ptr = IDENTIFIER_POINTER (type_id);
8d08fdba
MS
895 int i;
896 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
897#if 0
28531dd0 898 /* We don't take off the numbers; build_secondary_vtable uses the
8d08fdba
MS
899 DECL_ASSEMBLER_NAME for the type, which includes the number
900 in `3foo'. If we were to pull them off here, we'd end up with
901 something like `_vt.foo.3bar', instead of a uniform definition. */
902 while (ptr[i] >= '0' && ptr[i] <= '9')
903 i += 1;
904#endif
486837a7 905 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, ptr+i);
8d08fdba
MS
906 return get_identifier (buf);
907}
908
6b5fbb55 909/* Return the offset to the main vtable for a given base BINFO. */
e92cc029 910
6b5fbb55
MS
911tree
912get_vfield_offset (binfo)
913 tree binfo;
914{
770ae6cc
RK
915 return
916 size_binop (PLUS_EXPR, byte_position (TYPE_VFIELD (BINFO_TYPE (binfo))),
917 BINFO_OFFSET (binfo));
6b5fbb55
MS
918}
919
920/* Get the offset to the start of the original binfo that we derived
921 this binfo from. If we find TYPE first, return the offset only
922 that far. The shortened search is useful because the this pointer
923 on method calling is expected to point to a DECL_CONTEXT (fndecl)
fed3cef0
RK
924 object, and not a baseclass of it. */
925
e92cc029 926
6b5fbb55
MS
927static tree
928get_derived_offset (binfo, type)
929 tree binfo, type;
930{
931 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
932 tree offset2;
933 int i;
fed3cef0 934
6b5fbb55 935 while (BINFO_BASETYPES (binfo)
fed3cef0 936 && (i = CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
6b5fbb55
MS
937 {
938 tree binfos = BINFO_BASETYPES (binfo);
939 if (BINFO_TYPE (binfo) == type)
940 break;
941 binfo = TREE_VEC_ELT (binfos, i);
942 }
fed3cef0 943
6b5fbb55
MS
944 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
945 return size_binop (MINUS_EXPR, offset1, offset2);
946}
947
948/* Update the rtti info for this class. */
e92cc029 949
6b5fbb55
MS
950static void
951set_rtti_entry (virtuals, offset, type)
952 tree virtuals, offset, type;
953{
7267d692 954 tree decl;
6b5fbb55 955
aff08c18
JM
956 if (CLASSTYPE_COM_INTERFACE (type))
957 return;
958
6b5fbb55 959 if (flag_rtti)
7267d692
NS
960 decl = get_tinfo_decl (type);
961 else if (!new_abi_rtti_p ())
83f2ccf4
MM
962 /* If someone tries to get RTTI information for a type compiled
963 without RTTI, they're out of luck. By calling __pure_virtual
964 in this case, we give a small clue as to what went wrong. We
965 could consider having a __no_typeinfo function as well, for a
966 more specific hint. */
7267d692
NS
967 decl = abort_fndecl;
968 else
969 /* For the new-abi, we just point to the type_info object. */
970 decl = NULL_TREE;
6b5fbb55 971
83f2ccf4 972 if (flag_vtable_thunks)
6b5fbb55 973 {
83f2ccf4 974 /* The first slot holds the offset. */
5e19c053
MM
975 BV_DELTA (virtuals) = offset;
976 BV_VCALL_INDEX (virtuals) = integer_zero_node;
6b5fbb55 977
7267d692 978 /* The next node holds the decl. */
83f2ccf4 979 virtuals = TREE_CHAIN (virtuals);
770ae6cc 980 offset = ssize_int (0);
6b5fbb55 981 }
83f2ccf4 982
c0bbf652 983 /* This slot holds the function to call. */
5e19c053
MM
984 BV_DELTA (virtuals) = offset;
985 BV_VCALL_INDEX (virtuals) = integer_zero_node;
986 BV_FN (virtuals) = decl;
6b5fbb55
MS
987}
988
b9f39201
MM
989/* Create a VAR_DECL for a primary or secondary vtable for
990 CLASS_TYPE. Use NAME for the name of the vtable, and VTABLE_TYPE
991 for its type. */
992
993static tree
994build_vtable (class_type, name, vtable_type)
995 tree class_type;
996 tree name;
997 tree vtable_type;
998{
999 tree decl;
1000
1001 decl = build_lang_decl (VAR_DECL, name, vtable_type);
1002 DECL_CONTEXT (decl) = class_type;
1003 DECL_ARTIFICIAL (decl) = 1;
1004 TREE_STATIC (decl) = 1;
1005#ifndef WRITABLE_VTABLES
1006 /* Make them READONLY by default. (mrs) */
1007 TREE_READONLY (decl) = 1;
1008#endif
1009 DECL_VIRTUAL_P (decl) = 1;
1010 import_export_vtable (decl, class_type, 0);
1011
1012 return decl;
1013}
1014
1aa4ccd4
NS
1015/* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
1016 or even complete. If this does not exist, create it. If COMPLETE is
1017 non-zero, then complete the definition of it -- that will render it
1018 impossible to actually build the vtable, but is useful to get at those
1019 which are known to exist in the runtime. */
1020
7d52ae23
MM
1021tree
1022get_vtable_decl (type, complete)
1aa4ccd4
NS
1023 tree type;
1024 int complete;
1025{
1026 tree name = get_vtable_name (type);
1027 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
1028
1029 if (decl)
1030 {
1031 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
1032 && DECL_VIRTUAL_P (decl), 20000118);
1033 return decl;
1034 }
1035
b9f39201 1036 decl = build_vtable (type, name, void_type_node);
1aa4ccd4
NS
1037 decl = pushdecl_top_level (decl);
1038 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
1039
1aa4ccd4
NS
1040 /* At one time the vtable info was grabbed 2 words at a time. This
1041 fails on sparc unless you have 8-byte alignment. (tiemann) */
1042 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
1043 DECL_ALIGN (decl));
1044
1aa4ccd4 1045 if (complete)
217f4eb9
MM
1046 {
1047 DECL_EXTERNAL (decl) = 1;
1048 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
1049 }
1aa4ccd4 1050
1aa4ccd4
NS
1051 return decl;
1052}
1053
28531dd0
MM
1054/* Build the primary virtual function table for TYPE. If BINFO is
1055 non-NULL, build the vtable starting with the initial approximation
1056 that it is the same as the one which is the head of the association
1057 list. Returns a non-zero value if a new vtable is actually
1058 created. */
e92cc029 1059
28531dd0
MM
1060static int
1061build_primary_vtable (binfo, type)
8d08fdba
MS
1062 tree binfo, type;
1063{
8d08fdba
MS
1064 tree virtuals, decl;
1065
1aa4ccd4
NS
1066 decl = get_vtable_decl (type, /*complete=*/0);
1067
8d08fdba
MS
1068 if (binfo)
1069 {
6b5fbb55
MS
1070 tree offset;
1071
3c9d6359 1072 if (BINFO_NEW_VTABLE_MARKED (binfo, type))
0533d788
MM
1073 /* We have already created a vtable for this base, so there's
1074 no need to do it again. */
28531dd0
MM
1075 return 0;
1076
8d08fdba 1077 virtuals = copy_list (BINFO_VIRTUALS (binfo));
1aa4ccd4
NS
1078 TREE_TYPE (decl) = TREE_TYPE (BINFO_VTABLE (binfo));
1079 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (BINFO_VTABLE (binfo)));
06ceef4e
RK
1080 DECL_SIZE_UNIT (decl)
1081 = TYPE_SIZE_UNIT (TREE_TYPE (BINFO_VTABLE (binfo)));
6b5fbb55
MS
1082
1083 /* Now do rtti stuff. */
1084 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
fed3cef0 1085 offset = size_diffop (size_zero_node, offset);
6b5fbb55 1086 set_rtti_entry (virtuals, offset, type);
8d08fdba
MS
1087 }
1088 else
1089 {
1aa4ccd4
NS
1090 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
1091 20000118);
8d08fdba 1092 virtuals = NULL_TREE;
8d08fdba
MS
1093 }
1094
1095#ifdef GATHER_STATISTICS
1096 n_vtables += 1;
1097 n_vtable_elems += list_length (virtuals);
1098#endif
1099
8d08fdba
MS
1100 /* Initialize the association list for this type, based
1101 on our first approximation. */
1102 TYPE_BINFO_VTABLE (type) = decl;
1103 TYPE_BINFO_VIRTUALS (type) = virtuals;
1104
8d08fdba 1105 binfo = TYPE_BINFO (type);
3c9d6359 1106 SET_BINFO_NEW_VTABLE_MARKED (binfo, type);
28531dd0 1107 return 1;
8d08fdba
MS
1108}
1109
8d08fdba
MS
1110/* Give TYPE a new virtual function table which is initialized
1111 with a skeleton-copy of its original initialization. The only
1112 entry that changes is the `delta' entry, so we can really
1113 share a lot of structure.
1114
1115 FOR_TYPE is the derived type which caused this table to
1116 be needed.
1117
2636fde4
JM
1118 BINFO is the type association which provided TYPE for FOR_TYPE.
1119
1120 The order in which vtables are built (by calling this function) for
1121 an object must remain the same, otherwise a binary incompatibility
1122 can result. */
e92cc029 1123
28531dd0
MM
1124static int
1125build_secondary_vtable (binfo, for_type)
7177d104 1126 tree binfo, for_type;
8d08fdba 1127{
2636fde4 1128 tree basetype;
8d08fdba 1129 tree orig_decl = BINFO_VTABLE (binfo);
2636fde4
JM
1130 tree name;
1131 tree new_decl;
5566b478 1132 tree offset;
2636fde4
JM
1133 tree path = binfo;
1134 char *buf, *buf2;
1135 char joiner = '_';
1136 int i;
1137
1138#ifdef JOINER
1139 joiner = JOINER;
1140#endif
1141
8d7a5379
MM
1142 if (TREE_VIA_VIRTUAL (binfo))
1143 my_friendly_assert (binfo == BINFO_FOR_VBASE (BINFO_TYPE (binfo),
1144 current_class_type),
1145 170);
1146
3c9d6359 1147 if (BINFO_NEW_VTABLE_MARKED (binfo, current_class_type))
0533d788
MM
1148 /* We already created a vtable for this base. There's no need to
1149 do it again. */
28531dd0 1150 return 0;
0533d788 1151
8d7a5379
MM
1152 /* Remember that we've created a vtable for this BINFO, so that we
1153 don't try to do so again. */
3c9d6359 1154 SET_BINFO_NEW_VTABLE_MARKED (binfo, current_class_type);
8d7a5379
MM
1155
1156 /* Make fresh virtual list, so we can smash it later. */
1157 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
1158
1159 if (TREE_VIA_VIRTUAL (binfo))
1160 {
1161 tree binfo1 = BINFO_FOR_VBASE (BINFO_TYPE (binfo), for_type);
1162
1163 /* XXX - This should never happen, if it does, the caller should
1164 ensure that the binfo is from for_type's binfos, not from any
1165 base type's. We can remove all this code after a while. */
1166 if (binfo1 != binfo)
1167 warning ("internal inconsistency: binfo offset error for rtti");
1168
1169 offset = BINFO_OFFSET (binfo1);
1170 }
1171 else
1172 offset = BINFO_OFFSET (binfo);
1173
1174 set_rtti_entry (BINFO_VIRTUALS (binfo),
fed3cef0 1175 size_diffop (size_zero_node, offset),
8d7a5379
MM
1176 for_type);
1177
1178 /* In the new ABI, secondary vtables are laid out as part of the
1179 same structure as the primary vtable. */
1180 if (merge_primary_and_secondary_vtables_p ())
1181 {
1182 BINFO_VTABLE (binfo) = NULL_TREE;
1183 return 1;
1184 }
2636fde4 1185
8d7a5379
MM
1186 /* Create the declaration for the secondary vtable. */
1187 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
2636fde4
JM
1188 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
1189 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
1190
1191 /* We know that the vtable that we are going to create doesn't exist
1192 yet in the global namespace, and when we finish, it will be
1193 pushed into the global namespace. In complex MI hierarchies, we
1194 have to loop while the name we are thinking of adding is globally
1195 defined, adding more name components to the vtable name as we
1196 loop, until the name is unique. This is because in complex MI
1197 cases, we might have the same base more than once. This means
1198 that the order in which this function is called for vtables must
1199 remain the same, otherwise binary compatibility can be
1200 compromised. */
1201
1202 while (1)
1203 {
de35891e
JM
1204 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
1205 + 1 + i);
2636fde4
JM
1206 char *new_buf2;
1207
1208 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
1209 buf2);
486837a7
KG
1210 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX) + strlen (buf1) + 1);
1211 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
2636fde4
JM
1212 name = get_identifier (buf);
1213
1214 /* If this name doesn't clash, then we can use it, otherwise
1215 we add more to the name until it is unique. */
1216
1217 if (! IDENTIFIER_GLOBAL_VALUE (name))
1218 break;
1219
1220 /* Set values for next loop through, if the name isn't unique. */
1221
1222 path = BINFO_INHERITANCE_CHAIN (path);
1223
1224 /* We better not run out of stuff to make it unique. */
1225 my_friendly_assert (path != NULL_TREE, 368);
1226
1227 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
1228
de35891e
JM
1229 if (for_type == basetype)
1230 {
1231 /* If we run out of basetypes in the path, we have already
1232 found created a vtable with that name before, we now
1233 resort to tacking on _%d to distinguish them. */
1234 int j = 2;
1235 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
1236 buf1 = (char *) alloca (i);
1237 do {
1238 sprintf (buf1, "%s%c%s%c%d",
1239 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
1240 buf2, joiner, j);
486837a7 1241 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
de35891e 1242 + strlen (buf1) + 1);
486837a7 1243 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
de35891e
JM
1244 name = get_identifier (buf);
1245
1246 /* If this name doesn't clash, then we can use it,
1247 otherwise we add something different to the name until
1248 it is unique. */
1249 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
1250
1251 /* Hey, they really like MI don't they? Increase the 3
1252 above to 6, and the 999 to 999999. :-) */
1253 my_friendly_assert (j <= 999, 369);
1254
1255 break;
1256 }
2636fde4
JM
1257
1258 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
1259 new_buf2 = (char *) alloca (i);
1260 sprintf (new_buf2, "%s%c%s",
1261 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
1262 buf2 = new_buf2;
1263 }
8d08fdba 1264
b9f39201 1265 new_decl = build_vtable (for_type, name, TREE_TYPE (orig_decl));
8d08fdba 1266 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
b9f39201 1267 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
8d08fdba 1268
8d08fdba
MS
1269#ifdef GATHER_STATISTICS
1270 n_vtables += 1;
1271 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
1272#endif
1273
28531dd0 1274 return 1;
8d08fdba
MS
1275}
1276
28531dd0
MM
1277/* Create a new vtable for BINFO which is the hierarchy dominated by
1278 T. */
1279
1280static int
1281make_new_vtable (t, binfo)
1282 tree t;
1283 tree binfo;
1284{
1285 if (binfo == TYPE_BINFO (t))
1286 /* In this case, it is *type*'s vtable we are modifying. We start
1287 with the approximation that it's vtable is that of the
1288 immediate base class. */
1289 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
1290 t);
1291 else
1292 /* This is our very own copy of `basetype' to play with. Later,
1293 we will fill in all the virtual functions that override the
1294 virtual functions in these base classes which are not defined
1295 by the current type. */
1296 return build_secondary_vtable (binfo, t);
1297}
1298
1299/* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
1300 (which is in the hierarchy dominated by T) list FNDECL as its
5e19c053
MM
1301 BV_FN. DELTA is the required adjustment from the `this' pointer
1302 where the vtable entry appears to the `this' required when the
1303 function is actually called. */
8d08fdba
MS
1304
1305static void
5e19c053 1306modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
c0bbf652
MM
1307 tree t;
1308 tree binfo;
1309 tree fndecl;
5e19c053 1310 tree delta;
28531dd0 1311 tree *virtuals;
8d08fdba 1312{
c0bbf652 1313 tree vcall_index;
28531dd0 1314 tree v;
c0bbf652 1315
28531dd0 1316 v = *virtuals;
c0bbf652
MM
1317 vcall_index = integer_zero_node;
1318
5e19c053
MM
1319 if (fndecl != BV_FN (v)
1320 || !tree_int_cst_equal (delta, BV_DELTA (v))
1321 || !tree_int_cst_equal (vcall_index, BV_VCALL_INDEX (v)))
c0bbf652
MM
1322 {
1323 tree base_fndecl;
1324
28531dd0
MM
1325 /* We need a new vtable for BINFO. */
1326 if (make_new_vtable (t, binfo))
1327 {
1328 /* If we really did make a new vtable, we also made a copy
1329 of the BINFO_VIRTUALS list. Now, we have to find the
1330 corresponding entry in that list. */
1331 *virtuals = BINFO_VIRTUALS (binfo);
5e19c053 1332 while (BV_FN (*virtuals) != BV_FN (v))
28531dd0
MM
1333 *virtuals = TREE_CHAIN (*virtuals);
1334 v = *virtuals;
1335 }
8d08fdba 1336
5e19c053
MM
1337 base_fndecl = BV_FN (v);
1338 BV_DELTA (v) = delta;
1339 BV_VCALL_INDEX (v) = vcall_index;
1340 BV_FN (v) = fndecl;
e92cc029 1341
c0bbf652
MM
1342 /* Now assign virtual dispatch information, if unset. We can
1343 dispatch this, through any overridden base function. */
1344 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1345 {
1346 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
1347 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
1348 }
8d08fdba 1349 }
8d08fdba
MS
1350}
1351
bbd15aac
MM
1352/* Call this function whenever its known that a vtable for T is going
1353 to be needed. It's safe to call it more than once. *HAS_VIRTUAL_P
1354 is initialized to the number of slots that are reserved at the
1355 beginning of the vtable for RTTI information. */
1356
1357static void
1358start_vtable (t, has_virtual_p)
1359 tree t;
1360 int *has_virtual_p;
1361{
1362 if (*has_virtual_p == 0 && ! CLASSTYPE_COM_INTERFACE (t))
1363 {
051e6fd7
MM
1364 /* If we are using thunks, use two slots at the front, one
1365 for the offset pointer, one for the tdesc pointer.
1366 For ARM-style vtables, use the same slot for both. */
bbd15aac
MM
1367 if (flag_vtable_thunks)
1368 *has_virtual_p = 2;
1369 else
1370 *has_virtual_p = 1;
1371 }
1372}
1373
7177d104
MS
1374/* Add a virtual function to all the appropriate vtables for the class
1375 T. DECL_VINDEX(X) should be error_mark_node, if we want to
1376 allocate a new slot in our table. If it is error_mark_node, we
1377 know that no other function from another vtable is overridden by X.
1378 HAS_VIRTUAL keeps track of how many virtuals there are in our main
051e6fd7 1379 vtable for the type, and we build upon the NEW_VIRTUALS list
7177d104 1380 and return it. */
e92cc029 1381
aa598818 1382static void
051e6fd7
MM
1383add_virtual_function (new_virtuals_p, overridden_virtuals_p,
1384 has_virtual, fndecl, t)
1385 tree *new_virtuals_p;
1386 tree *overridden_virtuals_p;
8d08fdba 1387 int *has_virtual;
7177d104 1388 tree fndecl;
e92cc029 1389 tree t; /* Structure type. */
8d08fdba 1390{
c0bbf652
MM
1391 tree new_virtual;
1392
051e6fd7
MM
1393 /* If this function doesn't override anything from a base class, we
1394 can just assign it a new DECL_VINDEX now. Otherwise, if it does
1395 override something, we keep it around and assign its DECL_VINDEX
1396 later, in modify_all_vtables. */
1397 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
1398 /* We've already dealt with this function. */
c0bbf652
MM
1399 return;
1400
1401 new_virtual = build_tree_list (integer_zero_node, fndecl);
5e19c053 1402 BV_VCALL_INDEX (new_virtual) = integer_zero_node;
c0bbf652
MM
1403
1404 if (DECL_VINDEX (fndecl) == error_mark_node)
8d08fdba 1405 {
051e6fd7
MM
1406 /* FNDECL is a new virtual function; it doesn't override any
1407 virtual function in a base class. */
1408
6b5fbb55
MS
1409 /* We remember that this was the base sub-object for rtti. */
1410 CLASSTYPE_RTTI (t) = t;
8d08fdba 1411
bbd15aac 1412 start_vtable (t, has_virtual);
f30432d7 1413
a68ad5bd
MM
1414 /* Now assign virtual dispatch information. */
1415 DECL_VINDEX (fndecl) = build_shared_int_cst ((*has_virtual)++);
1416 DECL_VIRTUAL_CONTEXT (fndecl) = t;
8d08fdba 1417
051e6fd7 1418 /* Save the state we've computed on the NEW_VIRTUALS list. */
c0bbf652
MM
1419 TREE_CHAIN (new_virtual) = *new_virtuals_p;
1420 *new_virtuals_p = new_virtual;
1421 }
1422 else
1423 {
1424 /* FNDECL overrides a function from a base class. */
1425 TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
1426 *overridden_virtuals_p = new_virtual;
1427 }
8d08fdba
MS
1428}
1429\f
8d08fdba
MS
1430extern struct obstack *current_obstack;
1431
6b4b3deb 1432/* Add method METHOD to class TYPE.
8d08fdba 1433
6b4b3deb
MM
1434 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1435 the class type where the method should be added. */
e92cc029 1436
8d08fdba
MS
1437void
1438add_method (type, fields, method)
1439 tree type, *fields, method;
1440{
aa52c1ff 1441 int using = (DECL_CONTEXT (method) != type);
61a127b3 1442
8d08fdba 1443 if (fields && *fields)
61a127b3
MM
1444 *fields = build_overload (method, *fields);
1445 else
1446 {
1447 int len;
03017874 1448 int slot;
61a127b3
MM
1449 tree method_vec;
1450
1451 if (!CLASSTYPE_METHOD_VEC (type))
1452 /* Make a new method vector. We start with 8 entries. We must
1453 allocate at least two (for constructors and destructors), and
1454 we're going to end up with an assignment operator at some
1455 point as well.
1456
1457 We could use a TREE_LIST for now, and convert it to a
1458 TREE_VEC in finish_struct, but we would probably waste more
1459 memory making the links in the list than we would by
1460 over-allocating the size of the vector here. Furthermore,
1461 we would complicate all the code that expects this to be a
87e3dbc9
MM
1462 vector. */
1463 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
61a127b3
MM
1464
1465 method_vec = CLASSTYPE_METHOD_VEC (type);
1466 len = TREE_VEC_LENGTH (method_vec);
1467
1468 if (DECL_NAME (method) == constructor_name (type))
03017874
MM
1469 /* A new constructor or destructor. Constructors go in
1470 slot 0; destructors go in slot 1. */
1471 slot = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
8d08fdba
MS
1472 else
1473 {
61a127b3 1474 /* See if we already have an entry with this name. */
03017874
MM
1475 for (slot = 2; slot < len; ++slot)
1476 if (!TREE_VEC_ELT (method_vec, slot)
1477 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1478 slot)))
61a127b3
MM
1479 == DECL_NAME (method)))
1480 break;
1481
03017874 1482 if (slot == len)
8d08fdba 1483 {
61a127b3 1484 /* We need a bigger method vector. */
87e3dbc9 1485 tree new_vec = make_tree_vec (2 * len);
1ddb2906
KG
1486 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1487 (PTR) &TREE_VEC_ELT (new_vec, 0),
61a127b3 1488 len * sizeof (tree));
61a127b3
MM
1489 len = 2 * len;
1490 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
8d08fdba 1491 }
61a127b3 1492
03017874 1493 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
8d08fdba 1494 {
61a127b3
MM
1495 /* Type conversion operators have to come before
1496 ordinary methods; add_conversions depends on this to
1497 speed up looking for conversion operators. So, if
1498 necessary, we slide some of the vector elements up.
1499 In theory, this makes this algorithm O(N^2) but we
1500 don't expect many conversion operators. */
03017874 1501 for (slot = 2; slot < len; ++slot)
8d08fdba 1502 {
03017874
MM
1503 tree fn = TREE_VEC_ELT (method_vec, slot);
1504
61a127b3
MM
1505 if (!fn)
1506 /* There are no more entries in the vector, so we
1507 can insert the new conversion operator here. */
1508 break;
03017874
MM
1509
1510 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1511 /* We can insert the new function right at the
1512 SLOTth position. */
61a127b3 1513 break;
8d08fdba 1514 }
03017874
MM
1515
1516 if (!TREE_VEC_ELT (method_vec, slot))
61a127b3
MM
1517 /* There is nothing in the Ith slot, so we can avoid
1518 moving anything. */
1519 ;
8d08fdba 1520 else
61a127b3
MM
1521 {
1522 /* We know the last slot in the vector is empty
03017874
MM
1523 because we know that at this point there's room
1524 for a new function. */
1525 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1526 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1527 (len - slot - 1) * sizeof (tree));
1528 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
61a127b3 1529 }
8d08fdba 1530 }
61a127b3
MM
1531 }
1532
03017874
MM
1533 if (template_class_depth (type))
1534 /* TYPE is a template class. Don't issue any errors now; wait
1535 until instantiation time to complain. */
1536 ;
1537 else
1538 {
1539 tree fns;
1540
1541 /* Check to see if we've already got this method. */
1542 for (fns = TREE_VEC_ELT (method_vec, slot);
1543 fns;
1544 fns = OVL_NEXT (fns))
1545 {
1546 tree fn = OVL_CURRENT (fns);
1547
1548 if (TREE_CODE (fn) != TREE_CODE (method))
1549 continue;
1550
1551 if (TREE_CODE (method) != TEMPLATE_DECL)
1552 {
1553 /* [over.load] Member function declarations with the
1554 same name and the same parameter types cannot be
1555 overloaded if any of them is a static member
1556 function declaration. */
aa52c1ff
JM
1557 if ((DECL_STATIC_FUNCTION_P (fn)
1558 != DECL_STATIC_FUNCTION_P (method))
1559 || using)
03017874
MM
1560 {
1561 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1562 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1563
1564 if (! DECL_STATIC_FUNCTION_P (fn))
1565 parms1 = TREE_CHAIN (parms1);
aa52c1ff 1566 if (! DECL_STATIC_FUNCTION_P (method))
03017874
MM
1567 parms2 = TREE_CHAIN (parms2);
1568
1569 if (compparms (parms1, parms2))
aa52c1ff
JM
1570 {
1571 if (using)
1572 /* Defer to the local function. */
1573 return;
1574 else
1575 cp_error ("`%#D' and `%#D' cannot be overloaded",
1576 fn, method);
1577 }
03017874
MM
1578 }
1579
1580 /* Since this is an ordinary function in a
1581 non-template class, it's mangled name can be used
1582 as a unique identifier. This technique is only
1583 an optimization; we would get the same results if
1584 we just used decls_match here. */
1585 if (DECL_ASSEMBLER_NAME (fn)
1586 != DECL_ASSEMBLER_NAME (method))
1587 continue;
1588 }
1589 else if (!decls_match (fn, method))
1590 continue;
1591
1592 /* There has already been a declaration of this method
1593 or member template. */
1594 cp_error_at ("`%D' has already been declared in `%T'",
1595 method, type);
1596
1597 /* We don't call duplicate_decls here to merge the
1598 declarations because that will confuse things if the
8f032717 1599 methods have inline definitions. In particular, we
03017874
MM
1600 will crash while processing the definitions. */
1601 return;
1602 }
1603 }
1604
1605 /* Actually insert the new method. */
1606 TREE_VEC_ELT (method_vec, slot)
1607 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
8f032717
MM
1608
1609 /* Add the new binding. */
1610 if (!DECL_CONSTRUCTOR_P (method)
1611 && !DECL_DESTRUCTOR_P (method))
1612 push_class_level_binding (DECL_NAME (method),
1613 TREE_VEC_ELT (method_vec, slot));
8d08fdba 1614 }
8d08fdba
MS
1615}
1616
1617/* Subroutines of finish_struct. */
1618
1619/* Look through the list of fields for this struct, deleting
1620 duplicates as we go. This must be recursive to handle
1621 anonymous unions.
1622
1623 FIELD is the field which may not appear anywhere in FIELDS.
1624 FIELD_PTR, if non-null, is the starting point at which
1625 chained deletions may take place.
1626 The value returned is the first acceptable entry found
1627 in FIELDS.
1628
1629 Note that anonymous fields which are not of UNION_TYPE are
1630 not duplicates, they are just anonymous fields. This happens
1631 when we have unnamed bitfields, for example. */
e92cc029 1632
8d08fdba 1633static tree
00595019
MS
1634delete_duplicate_fields_1 (field, fields)
1635 tree field, fields;
8d08fdba
MS
1636{
1637 tree x;
00595019 1638 tree prev = 0;
8d08fdba
MS
1639 if (DECL_NAME (field) == 0)
1640 {
6bdb8141 1641 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
8d08fdba
MS
1642 return fields;
1643
1644 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
00595019 1645 fields = delete_duplicate_fields_1 (x, fields);
8d08fdba
MS
1646 return fields;
1647 }
1648 else
1649 {
1650 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1651 {
1652 if (DECL_NAME (x) == 0)
1653 {
6bdb8141 1654 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
8d08fdba
MS
1655 continue;
1656 TYPE_FIELDS (TREE_TYPE (x))
00595019 1657 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
8d08fdba
MS
1658 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1659 {
1660 if (prev == 0)
1661 fields = TREE_CHAIN (fields);
1662 else
1663 TREE_CHAIN (prev) = TREE_CHAIN (x);
1664 }
1665 }
58010b57
MM
1666 else if (TREE_CODE (field) == USING_DECL)
1667 /* A using declaration may is allowed to appear more than
1668 once. We'll prune these from the field list later, and
1669 handle_using_decl will complain about invalid multiple
1670 uses. */
1671 ;
1672 else if (DECL_NAME (field) == DECL_NAME (x))
8d08fdba 1673 {
58010b57
MM
1674 if (TREE_CODE (field) == CONST_DECL
1675 && TREE_CODE (x) == CONST_DECL)
1676 cp_error_at ("duplicate enum value `%D'", x);
1677 else if (TREE_CODE (field) == CONST_DECL
1678 || TREE_CODE (x) == CONST_DECL)
1679 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1680 x);
1681 else if (DECL_DECLARES_TYPE_P (field)
1682 && DECL_DECLARES_TYPE_P (x))
8d08fdba 1683 {
58010b57
MM
1684 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1685 continue;
1686 cp_error_at ("duplicate nested type `%D'", x);
8d08fdba 1687 }
58010b57
MM
1688 else if (DECL_DECLARES_TYPE_P (field)
1689 || DECL_DECLARES_TYPE_P (x))
1690 {
1691 /* Hide tag decls. */
1692 if ((TREE_CODE (field) == TYPE_DECL
1693 && DECL_ARTIFICIAL (field))
1694 || (TREE_CODE (x) == TYPE_DECL
1695 && DECL_ARTIFICIAL (x)))
1696 continue;
1697 cp_error_at ("duplicate field `%D' (as type and non-type)",
1698 x);
1699 }
1700 else
1701 cp_error_at ("duplicate member `%D'", x);
1702 if (prev == 0)
1703 fields = TREE_CHAIN (fields);
1704 else
1705 TREE_CHAIN (prev) = TREE_CHAIN (x);
8d08fdba
MS
1706 }
1707 }
1708 }
1709 return fields;
1710}
1711
1712static void
1713delete_duplicate_fields (fields)
1714 tree fields;
1715{
1716 tree x;
1717 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
00595019 1718 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
8d08fdba
MS
1719}
1720
aa52c1ff
JM
1721/* Change the access of FDECL to ACCESS in T. Return 1 if change was
1722 legit, otherwise return 0. */
e92cc029 1723
8d08fdba 1724static int
aa52c1ff 1725alter_access (t, fdecl, access)
8d08fdba
MS
1726 tree t;
1727 tree fdecl;
be99da77 1728 tree access;
8d08fdba
MS
1729{
1730 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
38afd588 1731 if (elem)
8d08fdba 1732 {
38afd588 1733 if (TREE_VALUE (elem) != access)
8d08fdba 1734 {
38afd588 1735 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
8251199e 1736 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
38afd588 1737 else
8251199e 1738 error ("conflicting access specifications for field `%s', ignored",
38afd588 1739 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
8d08fdba
MS
1740 }
1741 else
430bb96b
JL
1742 {
1743 /* They're changing the access to the same thing they changed
1744 it to before. That's OK. */
1745 ;
1746 }
db5ae43f 1747 }
38afd588 1748 else
8d08fdba 1749 {
aa52c1ff 1750 enforce_access (t, fdecl);
be99da77 1751 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
8d08fdba
MS
1752 return 1;
1753 }
1754 return 0;
1755}
1756
58010b57 1757/* Process the USING_DECL, which is a member of T. */
79ad62b2 1758
e9659ab0 1759static void
58010b57 1760handle_using_decl (using_decl, t)
79ad62b2
MM
1761 tree using_decl;
1762 tree t;
79ad62b2
MM
1763{
1764 tree ctype = DECL_INITIAL (using_decl);
1765 tree name = DECL_NAME (using_decl);
1766 tree access
1767 = TREE_PRIVATE (using_decl) ? access_private_node
1768 : TREE_PROTECTED (using_decl) ? access_protected_node
1769 : access_public_node;
1770 tree fdecl, binfo;
1771 tree flist = NULL_TREE;
aa52c1ff 1772 tree old_value;
79ad62b2
MM
1773
1774 binfo = binfo_or_else (ctype, t);
1775 if (! binfo)
1776 return;
1777
1778 if (name == constructor_name (ctype)
1779 || name == constructor_name_full (ctype))
2036a15c
MM
1780 {
1781 cp_error_at ("using-declaration for constructor", using_decl);
1782 return;
1783 }
1784
79ad62b2
MM
1785 fdecl = lookup_member (binfo, name, 0, 0);
1786
1787 if (!fdecl)
1788 {
8251199e 1789 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
79ad62b2
MM
1790 return;
1791 }
1792
aa52c1ff 1793 if (BASELINK_P (fdecl))
79ad62b2
MM
1794 /* Ignore base type this came from. */
1795 fdecl = TREE_VALUE (fdecl);
1796
aa52c1ff
JM
1797 old_value = IDENTIFIER_CLASS_VALUE (name);
1798 if (old_value)
79ad62b2 1799 {
aa52c1ff
JM
1800 if (is_overloaded_fn (old_value))
1801 old_value = OVL_CURRENT (old_value);
1802
1803 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1804 /* OK */;
1805 else
1806 old_value = NULL_TREE;
79ad62b2 1807 }
1c35f5b6 1808
aa52c1ff
JM
1809 if (is_overloaded_fn (fdecl))
1810 flist = fdecl;
1811 else if (! DECL_LANG_SPECIFIC (fdecl))
1812 my_friendly_abort (20000221);
1813
1814 if (! old_value)
1815 ;
1816 else if (is_overloaded_fn (old_value))
79ad62b2 1817 {
aa52c1ff
JM
1818 if (flist)
1819 /* It's OK to use functions from a base when there are functions with
1820 the same name already present in the current class. */;
1821 else
79ad62b2 1822 {
aa52c1ff
JM
1823 cp_error ("`%D' invalid in `%#T'", using_decl, t);
1824 cp_error_at (" because of local method `%#D' with same name",
1825 OVL_CURRENT (old_value));
1826 return;
79ad62b2
MM
1827 }
1828 }
1829 else
aa52c1ff
JM
1830 {
1831 cp_error ("`%D' invalid in `%#T'", using_decl, t);
1832 cp_error_at (" because of local field `%#D' with same name", old_value);
1833 return;
1834 }
1835
1836 /* Make type T see field decl FDECL with access ACCESS.*/
1837 if (flist)
1838 for (; flist; flist = OVL_NEXT (flist))
1839 {
1840 add_method (t, 0, OVL_CURRENT (flist));
1841 alter_access (t, OVL_CURRENT (flist), access);
1842 }
1843 else
1844 alter_access (t, fdecl, access);
79ad62b2 1845}
8d08fdba 1846\f
607cf131
MM
1847/* Run through the base clases of T, updating
1848 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1849 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1850 the bases. */
8d08fdba 1851
607cf131
MM
1852static void
1853check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1854 no_const_asn_ref_p)
8d08fdba 1855 tree t;
607cf131
MM
1856 int *cant_have_default_ctor_p;
1857 int *cant_have_const_ctor_p;
1858 int *no_const_asn_ref_p;
8d08fdba 1859{
607cf131
MM
1860 int n_baseclasses;
1861 int i;
f9c528ea 1862 int seen_nearly_empty_base_p;
607cf131 1863 tree binfos;
8d08fdba 1864
607cf131
MM
1865 binfos = TYPE_BINFO_BASETYPES (t);
1866 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
f9c528ea 1867 seen_nearly_empty_base_p = 0;
607cf131
MM
1868
1869 /* An aggregate cannot have baseclasses. */
1870 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1871
1872 for (i = 0; i < n_baseclasses; ++i)
8d08fdba 1873 {
607cf131
MM
1874 tree base_binfo;
1875 tree basetype;
8d08fdba 1876
607cf131
MM
1877 /* Figure out what base we're looking at. */
1878 base_binfo = TREE_VEC_ELT (binfos, i);
1879 basetype = TREE_TYPE (base_binfo);
9a71c18b 1880
607cf131
MM
1881 /* If the type of basetype is incomplete, then we already
1882 complained about that fact (and we should have fixed it up as
1883 well). */
d0f062fb 1884 if (!COMPLETE_TYPE_P (basetype))
8d08fdba
MS
1885 {
1886 int j;
1887 /* The base type is of incomplete type. It is
1888 probably best to pretend that it does not
1889 exist. */
1890 if (i == n_baseclasses-1)
1891 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1892 TREE_VEC_LENGTH (binfos) -= 1;
1893 n_baseclasses -= 1;
1894 for (j = i; j+1 < n_baseclasses; j++)
1895 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
607cf131 1896 continue;
8d08fdba
MS
1897 }
1898
4c6b7393 1899 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
607cf131
MM
1900 here because the case of virtual functions but non-virtual
1901 dtor is handled in finish_struct_1. */
4c6b7393 1902 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
607cf131
MM
1903 && TYPE_HAS_DESTRUCTOR (basetype))
1904 cp_warning ("base class `%#T' has a non-virtual destructor",
1905 basetype);
8d08fdba 1906
607cf131
MM
1907 /* If the base class doesn't have copy constructors or
1908 assignment operators that take const references, then the
1909 derived class cannot have such a member automatically
1910 generated. */
1911 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1912 *cant_have_const_ctor_p = 1;
1913 if (TYPE_HAS_ASSIGN_REF (basetype)
1914 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1915 *no_const_asn_ref_p = 1;
1916 /* Similarly, if the base class doesn't have a default
1917 constructor, then the derived class won't have an
1918 automatically generated default constructor. */
8d08fdba
MS
1919 if (TYPE_HAS_CONSTRUCTOR (basetype)
1920 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1921 {
607cf131 1922 *cant_have_default_ctor_p = 1;
8d08fdba 1923 if (! TYPE_HAS_CONSTRUCTOR (t))
cb9a3ff8
GDR
1924 cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1925 basetype);
8d08fdba
MS
1926 }
1927
f9c528ea
MM
1928 /* If the base class is not empty or nearly empty, then this
1929 class cannot be nearly empty. */
1930 if (!CLASSTYPE_NEARLY_EMPTY_P (basetype) && !is_empty_class (basetype))
1931 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1932 /* And if there is more than one nearly empty base, then the
1933 derived class is not nearly empty either. */
1934 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype)
1935 && seen_nearly_empty_base_p)
1936 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1937 /* If this is the first nearly empty base class, then remember
1938 that we saw it. */
1939 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1940 seen_nearly_empty_base_p = 1;
1941
607cf131
MM
1942 /* A lot of properties from the bases also apply to the derived
1943 class. */
8d08fdba 1944 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
834c6dff
MM
1945 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1946 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
607cf131
MM
1947 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1948 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
e8abc66f 1949 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
8d08fdba
MS
1950 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1951 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1952 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
4c6b7393 1953 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
8d08fdba 1954
607cf131
MM
1955 /* Derived classes can implicitly become COMified if their bases
1956 are COM. */
aff08c18 1957 if (CLASSTYPE_COM_INTERFACE (basetype))
607cf131
MM
1958 CLASSTYPE_COM_INTERFACE (t) = 1;
1959 else if (i == 0 && CLASSTYPE_COM_INTERFACE (t))
aff08c18 1960 {
5574ac39
MK
1961 cp_error
1962 ("COM interface type `%T' with non-COM leftmost base class `%T'",
1963 t, basetype);
aff08c18
JM
1964 CLASSTYPE_COM_INTERFACE (t) = 0;
1965 }
607cf131
MM
1966 }
1967}
1968
03702748 1969/* Make the Ith baseclass of T its primary base. */
607cf131 1970
03702748
MM
1971static void
1972set_primary_base (t, i, has_virtual_p)
1973 tree t;
1974 int i;
1975 int *has_virtual_p;
1976{
1977 tree basetype;
1978
1979 CLASSTYPE_VFIELD_PARENT (t) = i;
1980 basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
1981 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1982 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1983 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1984 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1985 *has_virtual_p = CLASSTYPE_VSIZE (basetype);
1986}
607cf131 1987
03702748 1988/* Determine the primary class for T. */
607cf131 1989
03702748 1990static void
d2c5305b 1991determine_primary_base (t, has_virtual_p)
607cf131 1992 tree t;
d2c5305b 1993 int *has_virtual_p;
607cf131 1994{
607cf131 1995 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
d2c5305b 1996
8026246f
MM
1997 /* If there are no baseclasses, there is certainly no primary base. */
1998 if (n_baseclasses == 0)
1999 return;
2000
d2c5305b 2001 *has_virtual_p = 0;
607cf131
MM
2002
2003 for (i = 0; i < n_baseclasses; i++)
2004 {
03702748 2005 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
607cf131 2006 tree basetype = BINFO_TYPE (base_binfo);
aff08c18 2007
bbd15aac 2008 if (TYPE_CONTAINS_VPTR_P (basetype))
8d08fdba 2009 {
03702748
MM
2010 /* Even a virtual baseclass can contain our RTTI
2011 information. But, we prefer a non-virtual polymorphic
2012 baseclass. */
2013 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2014 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
6b5fbb55 2015
8026246f
MM
2016 /* A virtual baseclass can't be the primary base under the
2017 old ABI. And under the new ABI we still prefer a
2018 non-virtual base. */
8d08fdba
MS
2019 if (TREE_VIA_VIRTUAL (base_binfo))
2020 continue;
2021
03702748 2022 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
8d08fdba 2023 {
d2c5305b 2024 set_primary_base (t, i, has_virtual_p);
03702748 2025 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
8d08fdba
MS
2026 }
2027 else
2028 {
03702748
MM
2029 tree vfields;
2030
8d08fdba 2031 /* Only add unique vfields, and flatten them out as we go. */
03702748
MM
2032 for (vfields = CLASSTYPE_VFIELDS (basetype);
2033 vfields;
2034 vfields = TREE_CHAIN (vfields))
2035 if (VF_BINFO_VALUE (vfields) == NULL_TREE
2036 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
2037 CLASSTYPE_VFIELDS (t)
2038 = tree_cons (base_binfo,
2039 VF_BASETYPE_VALUE (vfields),
2040 CLASSTYPE_VFIELDS (t));
8d08fdba 2041
d2c5305b
MM
2042 if (*has_virtual_p == 0)
2043 set_primary_base (t, i, has_virtual_p);
8d08fdba
MS
2044 }
2045 }
2046 }
2047
03702748
MM
2048 if (!TYPE_VFIELD (t))
2049 CLASSTYPE_VFIELD_PARENT (t) = -1;
8026246f 2050
dd42e135
MM
2051 /* The new ABI allows for the use of a "nearly-empty" virtual base
2052 class as the primary base class if no non-virtual polymorphic
2053 base can be found. */
2054 if (flag_new_abi && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2055 for (i = 0; i < n_baseclasses; ++i)
2056 {
2057 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
2058 tree basetype = BINFO_TYPE (base_binfo);
2059
2060 if (TREE_VIA_VIRTUAL (base_binfo)
2061 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
2062 {
2063 set_primary_base (t, i, has_virtual_p);
2064 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
2065 break;
2066 }
2067 }
2068
2069 /* Mark the primary base classes at this point. */
8026246f 2070 mark_primary_bases (t);
8d08fdba 2071}
8d08fdba 2072\f
d2c5305b
MM
2073/* Set memoizing fields and bits of T (and its variants) for later
2074 use. */
e92cc029 2075
8d08fdba 2076static void
d2c5305b 2077finish_struct_bits (t)
8d08fdba 2078 tree t;
8d08fdba
MS
2079{
2080 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
8d08fdba
MS
2081
2082 /* Fix up variants (if any). */
2083 tree variants = TYPE_NEXT_VARIANT (t);
2084 while (variants)
2085 {
2086 /* These fields are in the _TYPE part of the node, not in
2087 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2088 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
2089 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
2090 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
834c6dff
MM
2091 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
2092 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
8d08fdba 2093
4c6b7393
MM
2094 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
2095 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
2096 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
8d08fdba
MS
2097 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
2098 /* Copy whatever these are holding today. */
2099 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
2100 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
5566b478 2101 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
e92cc029 2102 TYPE_SIZE (variants) = TYPE_SIZE (t);
509087ae 2103 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
8d08fdba
MS
2104 variants = TYPE_NEXT_VARIANT (variants);
2105 }
2106
d2c5305b 2107 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
fee7654e
MM
2108 /* For a class w/o baseclasses, `finish_struct' has set
2109 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
2110 definition). Similarly for a class whose base classes do not
2111 have vtables. When neither of these is true, we might have
2112 removed abstract virtuals (by providing a definition), added
2113 some (by declaring new ones), or redeclared ones from a base
2114 class. We need to recalculate what's really an abstract virtual
2115 at this point (by looking in the vtables). */
2116 get_pure_virtuals (t);
8d08fdba
MS
2117
2118 if (n_baseclasses)
2119 {
2120 /* Notice whether this class has type conversion functions defined. */
2121 tree binfo = TYPE_BINFO (t);
2122 tree binfos = BINFO_BASETYPES (binfo);
2123 tree basetype;
2124
2125 for (i = n_baseclasses-1; i >= 0; i--)
2126 {
2127 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
2128
0b41abe6 2129 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
8d08fdba
MS
2130 }
2131 }
2132
e8abc66f
MS
2133 /* If this type has a copy constructor, force its mode to be BLKmode, and
2134 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
2135 be passed by invisible reference and prevent it from being returned in
72b7eeff
MS
2136 a register.
2137
2138 Also do this if the class has BLKmode but can still be returned in
2139 registers, since function_cannot_inline_p won't let us inline
2140 functions returning such a type. This affects the HP-PA. */
2141 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
2142 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
2143 && CLASSTYPE_NON_AGGREGATE (t)))
8d08fdba 2144 {
e8abc66f 2145 tree variants;
d2e5ee5c 2146 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
e8abc66f 2147 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
8d08fdba
MS
2148 {
2149 TYPE_MODE (variants) = BLKmode;
2150 TREE_ADDRESSABLE (variants) = 1;
8d08fdba
MS
2151 }
2152 }
2153}
2154
b0e0b31f
MM
2155/* Issue warnings about T having private constructors, but no friends,
2156 and so forth.
aed7b2a6 2157
b0e0b31f
MM
2158 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2159 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2160 non-private static member functions. */
2161
2162static void
2163maybe_warn_about_overly_private_class (t)
2164 tree t;
aed7b2a6 2165{
056a3b12
MM
2166 int has_member_fn = 0;
2167 int has_nonprivate_method = 0;
2168 tree fn;
2169
2170 if (!warn_ctor_dtor_privacy
b0e0b31f
MM
2171 /* If the class has friends, those entities might create and
2172 access instances, so we should not warn. */
056a3b12
MM
2173 || (CLASSTYPE_FRIEND_CLASSES (t)
2174 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
b0e0b31f
MM
2175 /* We will have warned when the template was declared; there's
2176 no need to warn on every instantiation. */
056a3b12
MM
2177 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2178 /* There's no reason to even consider warning about this
2179 class. */
2180 return;
2181
2182 /* We only issue one warning, if more than one applies, because
2183 otherwise, on code like:
2184
2185 class A {
2186 // Oops - forgot `public:'
2187 A();
2188 A(const A&);
2189 ~A();
2190 };
2191
2192 we warn several times about essentially the same problem. */
2193
2194 /* Check to see if all (non-constructor, non-destructor) member
2195 functions are private. (Since there are no friends or
2196 non-private statics, we can't ever call any of the private member
2197 functions.) */
2198 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
2199 /* We're not interested in compiler-generated methods; they don't
2200 provide any way to call private members. */
2201 if (!DECL_ARTIFICIAL (fn))
2202 {
2203 if (!TREE_PRIVATE (fn))
b0e0b31f 2204 {
056a3b12
MM
2205 if (DECL_STATIC_FUNCTION_P (fn))
2206 /* A non-private static member function is just like a
2207 friend; it can create and invoke private member
2208 functions, and be accessed without a class
2209 instance. */
2210 return;
b0e0b31f 2211
056a3b12
MM
2212 has_nonprivate_method = 1;
2213 break;
2214 }
ce0a5952 2215 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
056a3b12
MM
2216 has_member_fn = 1;
2217 }
aed7b2a6 2218
056a3b12
MM
2219 if (!has_nonprivate_method && has_member_fn)
2220 {
ce0a5952
MM
2221 /* There are no non-private methods, and there's at least one
2222 private member function that isn't a constructor or
2223 destructor. (If all the private members are
2224 constructors/destructors we want to use the code below that
2225 issues error messages specifically referring to
2226 constructors/destructors.) */
056a3b12
MM
2227 int i;
2228 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2229 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
2230 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2231 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2232 {
2233 has_nonprivate_method = 1;
2234 break;
2235 }
2236 if (!has_nonprivate_method)
b0e0b31f 2237 {
056a3b12
MM
2238 cp_warning ("all member functions in class `%T' are private", t);
2239 return;
b0e0b31f 2240 }
056a3b12 2241 }
aed7b2a6 2242
056a3b12
MM
2243 /* Even if some of the member functions are non-private, the class
2244 won't be useful for much if all the constructors or destructors
2245 are private: such an object can never be created or destroyed. */
2246 if (TYPE_HAS_DESTRUCTOR (t))
2247 {
2248 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
b0e0b31f 2249
056a3b12
MM
2250 if (TREE_PRIVATE (dtor))
2251 {
2252 cp_warning ("`%#T' only defines a private destructor and has no friends",
2253 t);
2254 return;
b0e0b31f 2255 }
056a3b12 2256 }
b0e0b31f 2257
056a3b12
MM
2258 if (TYPE_HAS_CONSTRUCTOR (t))
2259 {
2260 int nonprivate_ctor = 0;
b0e0b31f 2261
056a3b12
MM
2262 /* If a non-template class does not define a copy
2263 constructor, one is defined for it, enabling it to avoid
2264 this warning. For a template class, this does not
2265 happen, and so we would normally get a warning on:
b0e0b31f 2266
056a3b12 2267 template <class T> class C { private: C(); };
b0e0b31f 2268
056a3b12
MM
2269 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2270 complete non-template or fully instantiated classes have this
2271 flag set. */
2272 if (!TYPE_HAS_INIT_REF (t))
2273 nonprivate_ctor = 1;
2274 else
2275 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2276 fn;
2277 fn = OVL_NEXT (fn))
2278 {
2279 tree ctor = OVL_CURRENT (fn);
2280 /* Ideally, we wouldn't count copy constructors (or, in
2281 fact, any constructor that takes an argument of the
2282 class type as a parameter) because such things cannot
2283 be used to construct an instance of the class unless
2284 you already have one. But, for now at least, we're
2285 more generous. */
2286 if (! TREE_PRIVATE (ctor))
b0e0b31f 2287 {
056a3b12
MM
2288 nonprivate_ctor = 1;
2289 break;
b0e0b31f 2290 }
056a3b12 2291 }
aed7b2a6 2292
056a3b12
MM
2293 if (nonprivate_ctor == 0)
2294 {
2295 cp_warning ("`%#T' only defines private constructors and has no friends",
2296 t);
2297 return;
b0e0b31f
MM
2298 }
2299 }
aed7b2a6
MM
2300}
2301
f90cdf34
MT
2302/* Function to help qsort sort FIELD_DECLs by name order. */
2303
2304static int
2305field_decl_cmp (x, y)
2306 const tree *x, *y;
2307{
2308 if (DECL_NAME (*x) == DECL_NAME (*y))
bff3ce71
JM
2309 /* A nontype is "greater" than a type. */
2310 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
f90cdf34
MT
2311 if (DECL_NAME (*x) == NULL_TREE)
2312 return -1;
2313 if (DECL_NAME (*y) == NULL_TREE)
2314 return 1;
2315 if (DECL_NAME (*x) < DECL_NAME (*y))
2316 return -1;
2317 return 1;
2318}
2319
2320/* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2321
2322static int
2323method_name_cmp (m1, m2)
2324 const tree *m1, *m2;
2325{
2326 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2327 return 0;
2328 if (*m1 == NULL_TREE)
2329 return -1;
2330 if (*m2 == NULL_TREE)
2331 return 1;
2332 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2333 return -1;
2334 return 1;
2335}
b0e0b31f 2336
8d08fdba
MS
2337/* Warn about duplicate methods in fn_fields. Also compact method
2338 lists so that lookup can be made faster.
2339
8d08fdba
MS
2340 Data Structure: List of method lists. The outer list is a
2341 TREE_LIST, whose TREE_PURPOSE field is the field name and the
e1cd6e56
MS
2342 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2343 links the entire list of methods for TYPE_METHODS. Friends are
2344 chained in the same way as member functions (? TREE_CHAIN or
2345 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2346 list. That allows them to be quickly deleted, and requires no
2347 extra storage.
8d08fdba
MS
2348
2349 If there are any constructors/destructors, they are moved to the
2350 front of the list. This makes pushclass more efficient.
2351
f90cdf34
MT
2352 @@ The above comment is obsolete. It mostly describes what add_method
2353 @@ and add_implicitly_declared_members do.
2354
2355 Sort methods that are not special (i.e., constructors, destructors, and
2356 type conversion operators) so that we can find them faster in search. */
8d08fdba 2357
b0e0b31f
MM
2358static void
2359finish_struct_methods (t)
8d08fdba 2360 tree t;
8d08fdba 2361{
b0e0b31f 2362 tree fn_fields;
58010b57 2363 tree method_vec;
fc378698 2364 tree ctor_name = constructor_name (t);
58010b57
MM
2365 int slot, len;
2366
2367 if (!TYPE_METHODS (t))
2368 {
2369 /* Clear these for safety; perhaps some parsing error could set
2370 these incorrectly. */
2371 TYPE_HAS_CONSTRUCTOR (t) = 0;
2372 TYPE_HAS_DESTRUCTOR (t) = 0;
2373 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2374 return;
2375 }
2376
58010b57 2377 method_vec = CLASSTYPE_METHOD_VEC (t);
607cf131 2378 my_friendly_assert (method_vec != NULL_TREE, 19991215);
58010b57 2379 len = TREE_VEC_LENGTH (method_vec);
8d08fdba 2380
fc378698
MS
2381 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2382 and the next few with type conversion operators (if any). */
b0e0b31f
MM
2383 for (fn_fields = TYPE_METHODS (t); fn_fields;
2384 fn_fields = TREE_CHAIN (fn_fields))
8d08fdba 2385 {
8d08fdba 2386 tree fn_name = DECL_NAME (fn_fields);
8d08fdba 2387
8d08fdba
MS
2388 /* Clear out this flag.
2389
2390 @@ Doug may figure out how to break
2391 @@ this with nested classes and friends. */
2392 DECL_IN_AGGR_P (fn_fields) = 0;
2393
2394 /* Note here that a copy ctor is private, so we don't dare generate
2395 a default copy constructor for a class that has a member
2396 of this type without making sure they have access to it. */
fc378698 2397 if (fn_name == ctor_name)
8d08fdba
MS
2398 {
2399 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2400 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2401
2402 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2403 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2404 {
2405 if (TREE_CHAIN (parmtypes) == NULL_TREE
2406 || TREE_CHAIN (parmtypes) == void_list_node
2407 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2408 {
2409 if (TREE_PROTECTED (fn_fields))
2410 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2411 else if (TREE_PRIVATE (fn_fields))
2412 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2413 }
2414 }
61a127b3
MM
2415 }
2416 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
8d08fdba
MS
2417 {
2418 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2419
a292b002 2420 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
8d08fdba
MS
2421 {
2422 if (TREE_PROTECTED (fn_fields))
2423 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2424 else if (TREE_PRIVATE (fn_fields))
2425 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2426 }
2427 }
8d08fdba
MS
2428 }
2429
b0e0b31f
MM
2430 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2431 /* We thought there was a destructor, but there wasn't. Some
2432 parse errors cause this anomalous situation. */
2433 TYPE_HAS_DESTRUCTOR (t) = 0;
2434
2435 /* Issue warnings about private constructors and such. If there are
2436 no methods, then some public defaults are generated. */
f90cdf34
MT
2437 maybe_warn_about_overly_private_class (t);
2438
f90cdf34
MT
2439 /* Now sort the methods. */
2440 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2441 len--;
2442 TREE_VEC_LENGTH (method_vec) = len;
2443
2444 /* The type conversion ops have to live at the front of the vec, so we
2445 can't sort them. */
2446 for (slot = 2; slot < len; ++slot)
2447 {
2448 tree fn = TREE_VEC_ELT (method_vec, slot);
2449
2450 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2451 break;
2452 }
2453 if (len - slot > 1)
2454 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2455 (int (*)(const void *, const void *))method_name_cmp);
8d08fdba
MS
2456}
2457
e92cc029 2458/* Emit error when a duplicate definition of a type is seen. Patch up. */
8d08fdba
MS
2459
2460void
2461duplicate_tag_error (t)
2462 tree t;
2463{
8251199e
JM
2464 cp_error ("redefinition of `%#T'", t);
2465 cp_error_at ("previous definition here", t);
8d08fdba
MS
2466
2467 /* Pretend we haven't defined this type. */
2468
2469 /* All of the component_decl's were TREE_CHAINed together in the parser.
2470 finish_struct_methods walks these chains and assembles all methods with
2471 the same base name into DECL_CHAINs. Now we don't need the parser chains
e92cc029
MS
2472 anymore, so we unravel them. */
2473
2474 /* This used to be in finish_struct, but it turns out that the
2475 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2476 things... */
fc378698 2477 if (CLASSTYPE_METHOD_VEC (t))
8d08fdba 2478 {
fc378698
MS
2479 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2480 int i, len = TREE_VEC_LENGTH (method_vec);
8d08fdba
MS
2481 for (i = 0; i < len; i++)
2482 {
fc378698 2483 tree unchain = TREE_VEC_ELT (method_vec, i);
8d08fdba
MS
2484 while (unchain != NULL_TREE)
2485 {
2c73f9f5
ML
2486 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2487 unchain = OVL_NEXT (unchain);
8d08fdba
MS
2488 }
2489 }
2490 }
2491
2492 if (TYPE_LANG_SPECIFIC (t))
2493 {
8d08fdba 2494 tree binfo = TYPE_BINFO (t);
8d08fdba
MS
2495 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2496 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
13bd123d
NS
2497 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2498 int use_template = CLASSTYPE_USE_TEMPLATE (t);
8d08fdba 2499
1daa5dd8 2500 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
8d08fdba
MS
2501 BINFO_BASETYPES(binfo) = NULL_TREE;
2502
8d08fdba 2503 TYPE_BINFO (t) = binfo;
8d08fdba
MS
2504 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2505 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
8d08fdba 2506 TYPE_REDEFINED (t) = 1;
13bd123d
NS
2507 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2508 CLASSTYPE_USE_TEMPLATE (t) = use_template;
8d08fdba
MS
2509 }
2510 TYPE_SIZE (t) = NULL_TREE;
2511 TYPE_MODE (t) = VOIDmode;
2512 TYPE_FIELDS (t) = NULL_TREE;
2513 TYPE_METHODS (t) = NULL_TREE;
2514 TYPE_VFIELD (t) = NULL_TREE;
2515 TYPE_CONTEXT (t) = NULL_TREE;
6f1b4c42 2516 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
8d08fdba
MS
2517}
2518
8d7a5379
MM
2519/* Make the BINFO's vtablehave N entries, including RTTI entries,
2520 vbase and vcall offsets, etc. Set its type and call the backend
2521 to lay it out. */
1a588ad7
MM
2522
2523static void
2524layout_vtable_decl (binfo, n)
2525 tree binfo;
2526 int n;
2527{
2528 tree itype;
2529 tree atype;
2530
2531 itype = size_int (n);
1a588ad7
MM
2532 atype = build_cplus_array_type (vtable_entry_type,
2533 build_index_type (itype));
2534 layout_type (atype);
2535
2536 /* We may have to grow the vtable. */
2537 if (!same_type_p (TREE_TYPE (BINFO_VTABLE (binfo)), atype))
2538 {
06ceef4e
RK
2539 tree vtable = BINFO_VTABLE (binfo);
2540
2541 TREE_TYPE (vtable) = atype;
2542 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = 0;
2543 layout_decl (vtable, 0);
2544
1a588ad7 2545 /* At one time the vtable info was grabbed 2 words at a time. This
06ceef4e
RK
2546 fails on Sparc unless you have 8-byte alignment. */
2547 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2548 DECL_ALIGN (vtable));
1a588ad7
MM
2549 }
2550}
2551
2552/* Returns the number of virtual function table entries (excluding
2553 RTTI information, vbase and vcall offests, etc.) in the vtable for
2554 BINFO. */
2555
2556static int
2557num_vfun_entries (binfo)
2558 tree binfo;
2559{
2560 return list_length (skip_rtti_stuff (binfo,
2561 BINFO_TYPE (binfo),
2562 NULL));
2563}
2564
2565/* Called from num_extra_vtbl_entries via dfs_walk. */
2566
2567static tree
2568dfs_count_virtuals (binfo, data)
2569 tree binfo;
2570 void *data;
2571{
2572 /* Non-primary bases are not interesting; all of the virtual
2573 function table entries have been overridden. */
2574 if (!BINFO_PRIMARY_MARKED_P (binfo))
2575 ((vcall_offset_data *) data)->offsets += num_vfun_entries (binfo);
2576
2577 return NULL_TREE;
2578}
2579
70ae3201
MM
2580/* Returns the number of extra entries (at negative indices) required
2581 for BINFO's vtable. */
2582
2583tree
2584num_extra_vtbl_entries (binfo)
2585 tree binfo;
2586{
2587 tree type;
2588 int entries;
2589
70ae3201
MM
2590 type = BINFO_TYPE (binfo);
2591 entries = 0;
2592
2593 /* There is an entry for the offset to each virtual base. */
1a588ad7
MM
2594 if (vbase_offsets_in_vtable_p ())
2595 entries += list_length (CLASSTYPE_VBASECLASSES (type));
70ae3201 2596
1a588ad7
MM
2597 /* If this is a virtual base, there are entries for each virtual
2598 function defined in this class or its bases. */
2599 if (vcall_offsets_in_vtable_p () && TREE_VIA_VIRTUAL (binfo))
2600 {
2601 vcall_offset_data vod;
2602
2603 vod.vbase = binfo;
2604 vod.offsets = 0;
2605 dfs_walk (binfo,
2606 dfs_count_virtuals,
2607 dfs_vcall_offset_queue_p,
2608 &vod);
2609 entries += vod.offsets;
2610 }
2611
2612 return entries ? size_int (entries) : size_zero_node;
70ae3201
MM
2613}
2614
2615/* Returns the offset (in bytes) from the beginning of BINFO's vtable
2616 where the vptr should actually point. */
2617
2618tree
2619size_extra_vtbl_entries (binfo)
2620 tree binfo;
2621{
fed3cef0
RK
2622 tree offset = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (vtable_entry_type),
2623 num_extra_vtbl_entries (binfo));
70ae3201
MM
2624 return fold (offset);
2625}
2626
bbd15aac 2627/* Construct the initializer for BINFOs virtual function table. BINFO
8d7a5379
MM
2628 is part of the hierarchy dominated by T. The value returned is a
2629 TREE_LIST suitable for wrapping in a CONSTRUCTOR to use as the
2630 DECL_INITIAL for a vtable. */
83f2ccf4
MM
2631
2632static tree
bbd15aac 2633build_vtbl_initializer (binfo, t)
83f2ccf4 2634 tree binfo;
bbd15aac 2635 tree t;
83f2ccf4
MM
2636{
2637 tree v = BINFO_VIRTUALS (binfo);
2638 tree inits = NULL_TREE;
70ae3201
MM
2639 tree type = BINFO_TYPE (binfo);
2640
1a588ad7
MM
2641 /* Add entries to the vtable that indicate how to adjust the this
2642 pointer when calling a virtual function in this class. */
2643 inits = build_vcall_offset_vtbl_entries (binfo, t);
2644
f8361147 2645 /* Add entries to the vtable for offsets to our virtual bases. */
1a588ad7
MM
2646 inits = chainon (build_vbase_offset_vtbl_entries (binfo, t),
2647 inits);
83f2ccf4
MM
2648
2649 /* Process the RTTI stuff at the head of the list. If we're not
2650 using vtable thunks, then the RTTI entry is just an ordinary
2651 function, and we can process it just like the other virtual
2652 function entries. */
70ae3201 2653 if (!CLASSTYPE_COM_INTERFACE (type) && flag_vtable_thunks)
83f2ccf4
MM
2654 {
2655 tree offset;
2656 tree init;
2657
2658 /* The first entry is an offset. */
2659 offset = TREE_PURPOSE (v);
2660 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST,
2661 19990727);
2662
2663 /* Convert the offset to look like a function pointer, so that
2664 we can put it in the vtable. */
2665 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
2666 TREE_CONSTANT (init) = 1;
83f2ccf4
MM
2667 inits = tree_cons (NULL_TREE, init, inits);
2668
83f2ccf4 2669 v = TREE_CHAIN (v);
7267d692
NS
2670
2671 if (new_abi_rtti_p ())
2672 {
2673 tree decl = TREE_VALUE (v);
2674
2675 if (decl)
2676 decl = build_unary_op (ADDR_EXPR, decl, 0);
2677 else
2678 decl = integer_zero_node;
2679 decl = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
2680 TREE_CONSTANT (decl) = 1;
02fed91d
NS
2681 decl = build_vtable_entry (integer_zero_node, integer_zero_node,
2682 decl);
7267d692
NS
2683 inits = tree_cons (NULL_TREE, decl, inits);
2684
2685 v = TREE_CHAIN (v);
2686 }
2687 /* In the old abi the second entry (the tdesc pointer) is
2688 just an ordinary function, so it can be dealt with like the
2689 virtual functions. */
83f2ccf4
MM
2690 }
2691
2692 /* Go through all the ordinary virtual functions, building up
2693 initializers. */
2694 while (v)
2695 {
2696 tree delta;
c0bbf652 2697 tree vcall_index;
83f2ccf4 2698 tree fn;
7d52ae23 2699 tree pfn;
83f2ccf4
MM
2700 tree init;
2701
2702 /* Pull the offset for `this', and the function to call, out of
2703 the list. */
5e19c053
MM
2704 delta = BV_DELTA (v);
2705 vcall_index = BV_VCALL_INDEX (v);
2706 fn = BV_FN (v);
83f2ccf4
MM
2707 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
2708 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
2709
2710 /* You can't call an abstract virtual function; it's abstract.
2711 So, we replace these functions with __pure_virtual. */
fee7654e 2712 if (DECL_PURE_VIRTUAL_P (fn))
83f2ccf4
MM
2713 fn = abort_fndecl;
2714
7d52ae23
MM
2715 /* Take the address of the function, considering it to be of an
2716 appropriate generic type. */
2717 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
2718 /* The address of a function can't change. */
2719 TREE_CONSTANT (pfn) = 1;
2720 /* Enter it in the vtable. */
2721 init = build_vtable_entry (delta, vcall_index, pfn);
83f2ccf4
MM
2722 /* And add it to the chain of initializers. */
2723 inits = tree_cons (NULL_TREE, init, inits);
2724
2725 /* Keep going. */
2726 v = TREE_CHAIN (v);
2727 }
2728
2729 /* The initializers were built up in reverse order; straighten them
2730 out now. */
8d7a5379
MM
2731 return nreverse (inits);
2732}
2733
2734/* Initialize the vtable for BINFO with the INITS. */
2735
2736static void
2737initialize_vtable (binfo, inits)
2738 tree binfo;
2739 tree inits;
2740{
2741 tree context;
2742 tree decl;
2743
2744 layout_vtable_decl (binfo, list_length (inits));
2745 decl = BINFO_VTABLE (binfo);
2746 context = DECL_CONTEXT (decl);
2747 DECL_CONTEXT (decl) = 0;
2748 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
2749 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
2750 DECL_CONTEXT (decl) = context;
83f2ccf4
MM
2751}
2752
4a314e0c 2753/* Called from finish_vtbls via dfs_walk. */
e92cc029 2754
4a314e0c
MM
2755static tree
2756dfs_finish_vtbls (binfo, data)
6b5fbb55 2757 tree binfo;
bbd15aac 2758 void *data;
8d7a5379 2759{
3c9d6359
MM
2760 tree t = (tree) data;
2761
8d7a5379
MM
2762 if (!BINFO_PRIMARY_MARKED_P (binfo)
2763 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
3c9d6359 2764 && BINFO_NEW_VTABLE_MARKED (binfo, t))
8d7a5379 2765 initialize_vtable (binfo,
3c9d6359 2766 build_vtbl_initializer (binfo, t));
8d7a5379 2767
3c9d6359 2768 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo, t);
8d7a5379
MM
2769 SET_BINFO_MARKED (binfo);
2770
2771 return NULL_TREE;
2772}
2773
2774/* Called from finish_vtbls via dfs_walk when using the new ABI.
2775 Accumulates the vtable initializers for all of the vtables into
2776 TREE_VALUE (DATA). */
2777
2778static tree
2779dfs_accumulate_vtbl_inits (binfo, data)
2780 tree binfo;
2781 void *data;
7177d104 2782{
3c9d6359
MM
2783 tree l;
2784 tree t;
2785
2786 l = (tree) data;
2787 t = TREE_PURPOSE (l);
2788
4a314e0c
MM
2789 if (!BINFO_PRIMARY_MARKED_P (binfo)
2790 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
3c9d6359 2791 && BINFO_NEW_VTABLE_MARKED (binfo, t))
7177d104 2792 {
8d7a5379
MM
2793 /* If this is a secondary vtable, record its location. */
2794 if (binfo != TYPE_BINFO (t))
2795 {
2796 tree vtbl;
2797
2798 vtbl = TYPE_BINFO_VTABLE (t);
2799 vtbl = build1 (ADDR_EXPR,
2800 build_pointer_type (TREE_TYPE (vtbl)),
2801 vtbl);
2802 BINFO_VTABLE (binfo)
2803 = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl,
2804 size_binop (MULT_EXPR,
2805 TYPE_SIZE_UNIT (TREE_TYPE (vtbl)),
2806 size_int (list_length (TREE_VALUE (l)))));
2807 }
2808
3c9d6359 2809 /* Add the initializers for this vtable to the initializers for
8d7a5379
MM
2810 the other vtables we've already got. */
2811 TREE_VALUE (l)
2812 = chainon (TREE_VALUE (l),
2813 build_vtbl_initializer (binfo, t));
7177d104
MS
2814 }
2815
3c9d6359 2816 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo, t);
4a314e0c
MM
2817
2818 return NULL_TREE;
2819}
2820
3c9d6359
MM
2821/* Add the vtbl initializers for BINFO (and its non-primary,
2822 non-virtual bases) to the list of INITS. */
2823
2824static void
2825accumulate_vtbl_inits (binfo, inits)
2826 tree binfo;
2827 tree inits;
2828{
2829 /* Walk the BINFO and its bases. */
2830 dfs_walk_real (binfo,
2831 dfs_accumulate_vtbl_inits,
2832 NULL,
2833 dfs_skip_vbases,
2834 inits);
2835}
2836
4a314e0c
MM
2837/* Create all the necessary vtables for T and its base classes. */
2838
2839static void
2840finish_vtbls (t)
2841 tree t;
2842{
8d7a5379
MM
2843 if (merge_primary_and_secondary_vtables_p ())
2844 {
2845 tree list;
3c9d6359 2846 tree vbase;
8d7a5379
MM
2847
2848 /* Under the new ABI, we lay out the primary and secondary
2849 vtables in one contiguous vtable. The primary vtable is
3c9d6359
MM
2850 first, followed by the non-virtual secondary vtables in
2851 inheritance graph order. */
8d7a5379 2852 list = build_tree_list (t, NULL_TREE);
3c9d6359
MM
2853 accumulate_vtbl_inits (TYPE_BINFO (t), list);
2854 /* Then come the virtual bases, also in inheritance graph
2855 order. */
2856 for (vbase = CLASSTYPE_VBASECLASSES (t);
2857 vbase;
2858 vbase = TREE_CHAIN (vbase))
2859 accumulate_vtbl_inits (vbase, list);
2860
8d7a5379
MM
2861 if (TYPE_BINFO_VTABLE (t))
2862 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
2863 }
2864 else
3c9d6359
MM
2865 {
2866 dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls,
2867 dfs_unmarked_real_bases_queue_p, t);
2868 dfs_walk (TYPE_BINFO (t), dfs_unmark,
2869 dfs_marked_real_bases_queue_p, t);
2870 }
7177d104
MS
2871}
2872
2873/* True if we should override the given BASE_FNDECL with the given
2874 FNDECL. */
e92cc029 2875
7177d104
MS
2876static int
2877overrides (fndecl, base_fndecl)
2878 tree fndecl, base_fndecl;
2879{
e92cc029 2880 /* Destructors have special names. */
beb53fb8
JM
2881 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2882 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
7177d104 2883 return 1;
beb53fb8
JM
2884 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2885 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
7177d104
MS
2886 return 0;
2887 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2888 {
5566b478 2889 tree types, base_types;
7177d104
MS
2890#if 0
2891 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2892 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2893#endif
2894 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2895 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
91063b51
MM
2896 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2897 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2898 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
7177d104
MS
2899 return 1;
2900 }
2901 return 0;
2902}
2903
5e19c053
MM
2904typedef struct find_final_overrider_data_s {
2905 /* The function for which we are trying to find a final overrider. */
2906 tree fn;
2907 /* The base class in which the function was declared. */
2908 tree declaring_base;
2909 /* The most derived class in the hierarchy. */
2910 tree most_derived_type;
2911 /* The final overriding function. */
2912 tree overriding_fn;
2913 /* The BINFO for the class in which the final overriding function
2914 appears. */
2915 tree overriding_base;
2916} find_final_overrider_data;
2917
2918/* Called from find_final_overrider via dfs_walk. */
dd42e135 2919
a292b002 2920static tree
5e19c053
MM
2921dfs_find_final_overrider (binfo, data)
2922 tree binfo;
2923 void *data;
a292b002 2924{
5e19c053 2925 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
a292b002 2926
5e19c053
MM
2927 if (same_type_p (BINFO_TYPE (binfo),
2928 BINFO_TYPE (ffod->declaring_base))
2929 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2930 BINFO_OFFSET (ffod->declaring_base)))
a292b002 2931 {
5e19c053
MM
2932 tree path;
2933 tree method;
2934
2935 /* We've found a path to the declaring base. Walk down the path
2936 looking for an overrider for FN. */
2937 for (path = reverse_path (binfo);
2938 path;
2939 path = TREE_CHAIN (path))
2940 {
2941 for (method = TYPE_METHODS (BINFO_TYPE (TREE_VALUE (path)));
2942 method;
2943 method = TREE_CHAIN (method))
2944 if (DECL_VIRTUAL_P (method) && overrides (method, ffod->fn))
2945 break;
a292b002 2946
5e19c053
MM
2947 if (method)
2948 break;
2949 }
2950
2951 /* If we found an overrider, record the overriding function, and
2952 the base from which it came. */
2953 if (path)
a292b002 2954 {
5e19c053 2955 if (ffod->overriding_fn && ffod->overriding_fn != method)
a292b002 2956 {
5e19c053
MM
2957 /* We've found a different overrider along a different
2958 path. That can be OK if the new one overrides the
2959 old one. Consider:
2960
2961 struct S { virtual void f(); };
2962 struct T : public virtual S { virtual void f(); };
2963 struct U : public virtual S, public virtual T {};
2964
2965 Here `T::f' is the final overrider for `S::f'. */
2966 if (strictly_overrides (method, ffod->overriding_fn))
2967 {
2968 ffod->overriding_fn = method;
2969 ffod->overriding_base = TREE_VALUE (path);
2970 }
2971 else if (!strictly_overrides (ffod->overriding_fn, method))
2972 {
2973 cp_error ("no unique final overrider for `%D' in `%T'",
2974 ffod->most_derived_type,
2975 ffod->fn);
2976 cp_error ("candidates are: `%#D'", ffod->overriding_fn);
2977 cp_error (" `%#D'", method);
2978 return error_mark_node;
2979 }
2980 }
2981 else if (ffod->overriding_base
2982 && (!tree_int_cst_equal
2983 (BINFO_OFFSET (TREE_VALUE (path)),
2984 BINFO_OFFSET (ffod->overriding_base))))
2985 {
2986 /* We've found two instances of the same base that
2987 provide overriders. */
2988 cp_error ("no unique final overrider for `%D' since there two instances of `%T' in `%T'",
2989 ffod->fn,
2990 BINFO_TYPE (ffod->overriding_base),
2991 ffod->most_derived_type);
2992 return error_mark_node;
2993 }
2994 else
2995 {
2996 ffod->overriding_fn = method;
2997 ffod->overriding_base = TREE_VALUE (path);
a292b002 2998 }
a292b002
MS
2999 }
3000 }
dd42e135
MM
3001
3002 return NULL_TREE;
3003}
3004
5e19c053
MM
3005/* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
3006 FN and whose TREE_VALUE is the binfo for the base where the
3007 overriding occurs. BINFO (in the hierarchy dominated by T) is the
3008 base object in which FN is declared. */
e92cc029 3009
a292b002 3010static tree
5e19c053
MM
3011find_final_overrider (t, binfo, fn)
3012 tree t;
3013 tree binfo;
3014 tree fn;
a292b002 3015{
5e19c053 3016 find_final_overrider_data ffod;
a292b002 3017
5e19c053 3018 /* Getting this right is a little tricky. This is legal:
a292b002 3019
5e19c053
MM
3020 struct S { virtual void f (); };
3021 struct T { virtual void f (); };
3022 struct U : public S, public T { };
a292b002 3023
5e19c053 3024 even though calling `f' in `U' is ambiguous. But,
a292b002 3025
5e19c053
MM
3026 struct R { virtual void f(); };
3027 struct S : virtual public R { virtual void f (); };
3028 struct T : virtual public R { virtual void f (); };
3029 struct U : public S, public T { };
dd42e135 3030
5e19c053
MM
3031 is not -- there's no way to decide whether to put `S::f' or
3032 `T::f' in the vtable for `R'.
3033
3034 The solution is to look at all paths to BINFO. If we find
3035 different overriders along any two, then there is a problem. */
3036 ffod.fn = fn;
3037 ffod.declaring_base = binfo;
3038 ffod.most_derived_type = t;
3039 ffod.overriding_fn = NULL_TREE;
3040 ffod.overriding_base = NULL_TREE;
3041
3042 if (dfs_walk (TYPE_BINFO (t),
3043 dfs_find_final_overrider,
3044 NULL,
3045 &ffod))
3046 return error_mark_node;
dd42e135 3047
5e19c053 3048 return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
a292b002
MS
3049}
3050
07b7a812
MM
3051/* Return the BINFO_VIRTUALS list for BINFO, without the RTTI stuff at
3052 the front. If non-NULL, N is set to the number of entries
3053 skipped. */
e92cc029 3054
07b7a812
MM
3055tree
3056skip_rtti_stuff (binfo, t, n)
3057 tree binfo;
3058 tree t;
fed3cef0 3059 HOST_WIDE_INT *n;
f30432d7 3060{
07b7a812 3061 tree virtuals;
f30432d7 3062
aff08c18
JM
3063 if (CLASSTYPE_COM_INTERFACE (t))
3064 return 0;
3065
07b7a812
MM
3066 if (n)
3067 *n = 0;
3068 virtuals = BINFO_VIRTUALS (binfo);
3069 if (virtuals)
f30432d7
MS
3070 {
3071 /* We always reserve a slot for the offset/tdesc entry. */
07b7a812
MM
3072 if (n)
3073 ++*n;
3074 virtuals = TREE_CHAIN (virtuals);
f30432d7 3075 }
07b7a812 3076 if (flag_vtable_thunks && virtuals)
f30432d7
MS
3077 {
3078 /* The second slot is reserved for the tdesc pointer when thunks
3079 are used. */
07b7a812
MM
3080 if (n)
3081 ++*n;
3082 virtuals = TREE_CHAIN (virtuals);
f30432d7 3083 }
07b7a812
MM
3084
3085 return virtuals;
f30432d7
MS
3086}
3087
5e19c053
MM
3088/* Called via dfs_walk. Returns BINFO if BINFO has the same type as
3089 DATA (which is really an _TYPE node). */
83f2ccf4 3090
5e19c053
MM
3091static tree
3092dfs_find_base (binfo, data)
3093 tree binfo;
3094 void *data;
3095{
3096 return (same_type_p (BINFO_TYPE (binfo), (tree) data)
3097 ? binfo : NULL_TREE);
7177d104
MS
3098}
3099
8026246f 3100/* Called from modify_all_vtables via dfs_walk. */
e92cc029 3101
8026246f
MM
3102static tree
3103dfs_modify_vtables (binfo, data)
3104 tree binfo;
3105 void *data;
3106{
3107 if (/* There's no need to modify the vtable for a primary base;
3108 we're not going to use that vtable anyhow. */
3109 !BINFO_PRIMARY_MARKED_P (binfo)
3110 /* Similarly, a base without a vtable needs no modification. */
3111 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
7177d104 3112 {
5e19c053
MM
3113 tree t;
3114 tree virtuals;
3115 tree old_virtuals;
3116
3117 t = (tree) data;
3118
64cfdfb8
MM
3119 /* If we're supporting RTTI then we always need a new vtable to
3120 point to the RTTI information. Under the new ABI we may need
3121 a new vtable to contain vcall and vbase offsets. */
5e19c053
MM
3122 if (flag_rtti || flag_new_abi)
3123 make_new_vtable (t, binfo);
3124
3125 /* Now, go through each of the virtual functions in the virtual
3126 function table for BINFO. Find the final overrider, and
3127 update the BINFO_VIRTUALS list appropriately. */
3128 for (virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), NULL),
3129 old_virtuals = skip_rtti_stuff (TYPE_BINFO (BINFO_TYPE (binfo)),
3130 BINFO_TYPE (binfo),
3131 NULL);
3132 virtuals;
3133 virtuals = TREE_CHAIN (virtuals),
3134 old_virtuals = TREE_CHAIN (old_virtuals))
3135 {
3136 tree b;
3137 tree fn;
3138 tree overrider;
3139 tree vindex;
3140 tree delta;
665f2503
RK
3141 HOST_WIDE_INT vindex_val, i;
3142
5e19c053
MM
3143
3144 /* Find the function which originally caused this vtable
3145 entry to be present. */
3146 fn = BV_FN (old_virtuals);
3147 vindex = DECL_VINDEX (fn);
3148 b = dfs_walk (binfo, dfs_find_base, NULL, DECL_VIRTUAL_CONTEXT (fn));
3149 fn = skip_rtti_stuff (TYPE_BINFO (BINFO_TYPE (b)),
3150 BINFO_TYPE (b),
64cfdfb8 3151 &i);
665f2503
RK
3152 vindex_val = tree_low_cst (vindex, 0);
3153 while (i < vindex_val)
64cfdfb8
MM
3154 {
3155 fn = TREE_CHAIN (fn);
3156 ++i;
3157 }
5e19c053
MM
3158 fn = BV_FN (fn);
3159
3160 /* Handle the case of a virtual function defined in BINFO
3161 itself. */
3162 overrider = find_final_overrider (t, b, fn);
3163 if (overrider == error_mark_node)
3164 continue;
3165
3166 /* The `this' pointer needs to be adjusted from pointing to
3167 BINFO to pointing at the base where the final overrider
3168 appears. */
3169 delta = size_binop (PLUS_EXPR,
3170 get_derived_offset (binfo,
3171 DECL_VIRTUAL_CONTEXT (fn)),
3172 BINFO_OFFSET (binfo));
fed3cef0 3173 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)), delta);
5e19c053
MM
3174
3175 modify_vtable_entry (t,
3176 binfo,
3177 TREE_PURPOSE (overrider),
3178 delta,
3179 &virtuals);
3180 }
7177d104 3181 }
8026246f
MM
3182
3183 SET_BINFO_MARKED (binfo);
3184
3185 return NULL_TREE;
3186}
3187
a68ad5bd
MM
3188/* Update all of the primary and secondary vtables for T. Create new
3189 vtables as required, and initialize their RTTI information. Each
3190 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
3191 function from a base class; find and modify the appropriate entries
3192 to point to the overriding functions. Returns a list, in
3193 declaration order, of the functions that are overridden in this
3194 class, but do not appear in the primary base class vtable, and
3195 which should therefore be appended to the end of the vtable for T. */
3196
3197static tree
3198modify_all_vtables (t, has_virtual_p, overridden_virtuals)
8026246f 3199 tree t;
a68ad5bd
MM
3200 int *has_virtual_p;
3201 tree overridden_virtuals;
8026246f 3202{
a68ad5bd 3203 tree binfo;
8026246f 3204
a68ad5bd
MM
3205 binfo = TYPE_BINFO (t);
3206
5e19c053
MM
3207 /* Update all of the vtables. */
3208 dfs_walk (binfo,
3209 dfs_modify_vtables,
3210 dfs_unmarked_real_bases_queue_p,
3211 t);
3212 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
a68ad5bd
MM
3213
3214 /* If we should include overriding functions for secondary vtables
3215 in our primary vtable, add them now. */
3216 if (all_overridden_vfuns_in_vtables_p ())
3217 {
3218 tree *fnsp = &overridden_virtuals;
3219
3220 while (*fnsp)
3221 {
3222 tree fn = TREE_VALUE (*fnsp);
3223
3224 if (BINFO_VIRTUALS (binfo)
3225 && !value_member (fn, BINFO_VIRTUALS (binfo)))
3226 {
3227 /* We know we need a vtable for this class now. */
3228 start_vtable (t, has_virtual_p);
3229 /* Set the vtable index. */
3230 DECL_VINDEX (fn)
3231 = build_shared_int_cst ((*has_virtual_p)++);
3232 /* We don't need to convert to a base class when calling
3233 this function. */
3234 DECL_VIRTUAL_CONTEXT (fn) = t;
7d52ae23 3235
a68ad5bd
MM
3236 /* We don't need to adjust the `this' pointer when
3237 calling this function. */
5e19c053
MM
3238 BV_DELTA (*fnsp) = integer_zero_node;
3239 BV_VCALL_INDEX (*fnsp) = integer_zero_node;
a68ad5bd
MM
3240
3241 /* This is an overridden function not already in our
3242 vtable. Keep it. */
3243 fnsp = &TREE_CHAIN (*fnsp);
3244 }
3245 else
3246 /* We've already got an entry for this function. Skip
3247 it. */
3248 *fnsp = TREE_CHAIN (*fnsp);
3249 }
3250 }
3251 else
3252 overridden_virtuals = NULL_TREE;
3253
3254 return overridden_virtuals;
7177d104
MS
3255}
3256
39211cd5
MS
3257/* Here, we already know that they match in every respect.
3258 All we have to check is where they had their declarations. */
e92cc029 3259
39211cd5
MS
3260static int
3261strictly_overrides (fndecl1, fndecl2)
3262 tree fndecl1, fndecl2;
3263{
4f1c5b7d
MM
3264 int distance = get_base_distance (DECL_CONTEXT (fndecl2),
3265 DECL_CONTEXT (fndecl1),
39211cd5
MS
3266 0, (tree *)0);
3267 if (distance == -2 || distance > 0)
3268 return 1;
3269 return 0;
3270}
3271
9e9ff709
MS
3272/* Get the base virtual function declarations in T that are either
3273 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
3274 the overrider/hider. */
e92cc029 3275
5ddc28a5 3276static tree
9e9ff709
MS
3277get_basefndecls (fndecl, t)
3278 tree fndecl, t;
3279{
3280 tree methods = TYPE_METHODS (t);
3281 tree base_fndecls = NULL_TREE;
3282 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3283 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3284
3285 while (methods)
3286 {
9e9ff709
MS
3287 if (TREE_CODE (methods) == FUNCTION_DECL
3288 && DECL_VINDEX (methods) != NULL_TREE
3289 && DECL_NAME (fndecl) == DECL_NAME (methods))
58010b57 3290 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
9e9ff709
MS
3291
3292 methods = TREE_CHAIN (methods);
3293 }
3294
3295 if (base_fndecls)
3296 return base_fndecls;
3297
3298 for (i = 0; i < n_baseclasses; i++)
3299 {
3300 tree base_binfo = TREE_VEC_ELT (binfos, i);
3301 tree basetype = BINFO_TYPE (base_binfo);
9e9ff709
MS
3302
3303 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3304 base_fndecls);
3305 }
3306
3307 return base_fndecls;
3308}
3309
3310/* Mark the functions that have been hidden with their overriders.
3311 Since we start out with all functions already marked with a hider,
a4832853
JM
3312 no need to mark functions that are just hidden.
3313
3314 Subroutine of warn_hidden. */
e92cc029 3315
bd6dd845 3316static void
9e9ff709
MS
3317mark_overriders (fndecl, base_fndecls)
3318 tree fndecl, base_fndecls;
3319{
a4832853 3320 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
9e9ff709 3321 {
a4832853 3322 if (overrides (fndecl, TREE_VALUE (base_fndecls)))
9e9ff709 3323 TREE_PURPOSE (base_fndecls) = fndecl;
9e9ff709
MS
3324 }
3325}
3326
2ee887f2
MS
3327/* If this declaration supersedes the declaration of
3328 a method declared virtual in the base class, then
3329 mark this field as being virtual as well. */
3330
bd6dd845 3331static void
cffa8729 3332check_for_override (decl, ctype)
2ee887f2
MS
3333 tree decl, ctype;
3334{
3335 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
3336 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3337 int virtualp = DECL_VIRTUAL_P (decl);
ed70c426 3338 int found_overriden_fn = 0;
2ee887f2
MS
3339
3340 for (i = 0; i < n_baselinks; i++)
3341 {
3342 tree base_binfo = TREE_VEC_ELT (binfos, i);
4c6b7393 3343 if (TYPE_POLYMORPHIC_P (BINFO_TYPE (base_binfo)))
2ee887f2
MS
3344 {
3345 tree tmp = get_matching_virtual
3346 (base_binfo, decl,
3347 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
ed70c426
MM
3348
3349 if (tmp && !found_overriden_fn)
2ee887f2
MS
3350 {
3351 /* If this function overrides some virtual in some base
3352 class, then the function itself is also necessarily
3353 virtual, even if the user didn't explicitly say so. */
3354 DECL_VIRTUAL_P (decl) = 1;
3355
3356 /* The TMP we really want is the one from the deepest
3357 baseclass on this path, taking care not to
3358 duplicate if we have already found it (via another
3359 path to its virtual baseclass. */
3360 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
3361 {
4cc1d462
NS
3362 cp_error_at ("`static %#D' cannot be declared", decl);
3363 cp_error_at (" since `virtual %#D' declared in base class",
2ee887f2
MS
3364 tmp);
3365 break;
3366 }
3367 virtualp = 1;
3368
051e6fd7
MM
3369 /* Set DECL_VINDEX to a value that is neither an
3370 INTEGER_CST nor the error_mark_node so that
3371 add_virtual_function will realize this is an
3372 overridden function. */
3373 DECL_VINDEX (decl)
3374 = tree_cons (tmp, NULL_TREE, DECL_VINDEX (decl));
ed70c426
MM
3375
3376 /* We now know that DECL overrides something,
3377 which is all that is important. But, we must
3378 continue to iterate through all the base-classes
3379 in order to allow get_matching_virtual to check for
3380 various illegal overrides. */
3381 found_overriden_fn = 1;
2ee887f2
MS
3382 }
3383 }
3384 }
3385 if (virtualp)
3386 {
3387 if (DECL_VINDEX (decl) == NULL_TREE)
3388 DECL_VINDEX (decl) = error_mark_node;
3389 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3390 }
3391}
3392
fc378698
MS
3393/* Warn about hidden virtual functions that are not overridden in t.
3394 We know that constructors and destructors don't apply. */
e92cc029 3395
9e9ff709
MS
3396void
3397warn_hidden (t)
3398 tree t;
3399{
3400 tree method_vec = CLASSTYPE_METHOD_VEC (t);
3401 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3402 int i;
3403
3404 /* We go through each separately named virtual function. */
61a127b3 3405 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
9e9ff709 3406 {
2b9dc906 3407 tree fns = TREE_VEC_ELT (method_vec, i);
a544cfd2 3408 tree fndecl = NULL_TREE;
9e9ff709
MS
3409
3410 tree base_fndecls = NULL_TREE;
3411 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3412 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3413
a4832853
JM
3414 /* First see if we have any virtual functions in this batch. */
3415 for (; fns; fns = OVL_NEXT (fns))
3416 {
3417 fndecl = OVL_CURRENT (fns);
3418 if (DECL_VINDEX (fndecl))
3419 break;
3420 }
3421
3422 if (fns == NULL_TREE)
9e9ff709
MS
3423 continue;
3424
3425 /* First we get a list of all possible functions that might be
3426 hidden from each base class. */
3427 for (i = 0; i < n_baseclasses; i++)
3428 {
3429 tree base_binfo = TREE_VEC_ELT (binfos, i);
3430 tree basetype = BINFO_TYPE (base_binfo);
3431
3432 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3433 base_fndecls);
3434 }
3435
2b9dc906 3436 fns = OVL_NEXT (fns);
9e9ff709
MS
3437
3438 /* ...then mark up all the base functions with overriders, preferring
3439 overriders to hiders. */
3440 if (base_fndecls)
a4832853 3441 for (; fns; fns = OVL_NEXT (fns))
9e9ff709 3442 {
a4832853
JM
3443 fndecl = OVL_CURRENT (fns);
3444 if (DECL_VINDEX (fndecl))
3445 mark_overriders (fndecl, base_fndecls);
9e9ff709
MS
3446 }
3447
3448 /* Now give a warning for all base functions without overriders,
3449 as they are hidden. */
a4832853 3450 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
9e9ff709 3451 {
a4832853
JM
3452 if (! overrides (TREE_PURPOSE (base_fndecls),
3453 TREE_VALUE (base_fndecls)))
9e9ff709
MS
3454 {
3455 /* Here we know it is a hider, and no overrider exists. */
8251199e
JM
3456 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
3457 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
9e9ff709 3458 }
9e9ff709
MS
3459 }
3460 }
3461}
3462
3463/* Check for things that are invalid. There are probably plenty of other
3464 things we should check for also. */
e92cc029 3465
9e9ff709
MS
3466static void
3467finish_struct_anon (t)
3468 tree t;
3469{
3470 tree field;
f90cdf34 3471
9e9ff709
MS
3472 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3473 {
3474 if (TREE_STATIC (field))
3475 continue;
3476 if (TREE_CODE (field) != FIELD_DECL)
3477 continue;
3478
3479 if (DECL_NAME (field) == NULL_TREE
6bdb8141 3480 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
9e9ff709 3481 {
f90cdf34
MT
3482 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3483 for (; elt; elt = TREE_CHAIN (elt))
9e9ff709 3484 {
f90cdf34 3485 if (DECL_ARTIFICIAL (elt))
9e9ff709
MS
3486 continue;
3487
f90cdf34 3488 if (DECL_NAME (elt) == constructor_name (t))
cb9a3ff8 3489 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
f90cdf34 3490 elt);
8ebeee52 3491
f90cdf34 3492 if (TREE_CODE (elt) != FIELD_DECL)
8ebeee52
JM
3493 {
3494 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
f90cdf34 3495 elt);
8ebeee52
JM
3496 continue;
3497 }
3498
f90cdf34 3499 if (TREE_PRIVATE (elt))
8251199e 3500 cp_pedwarn_at ("private member `%#D' in anonymous union",
f90cdf34
MT
3501 elt);
3502 else if (TREE_PROTECTED (elt))
8251199e 3503 cp_pedwarn_at ("protected member `%#D' in anonymous union",
f90cdf34 3504 elt);
fc378698 3505
f90cdf34
MT
3506 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3507 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
9e9ff709
MS
3508 }
3509 }
3510 }
3511}
3512
f30432d7
MS
3513extern int interface_only, interface_unknown;
3514
61a127b3
MM
3515/* Create default constructors, assignment operators, and so forth for
3516 the type indicated by T, if they are needed.
3517 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3518 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3519 cannot have a default constructor, copy constructor taking a const
3520 reference argument, or an assignment operator, respectively. If a
3521 virtual destructor is created, its DECL is returned; otherwise the
3522 return value is NULL_TREE. */
3523
3524static tree
3525add_implicitly_declared_members (t, cant_have_default_ctor,
3526 cant_have_const_cctor,
3527 cant_have_assignment)
3528 tree t;
3529 int cant_have_default_ctor;
3530 int cant_have_const_cctor;
3531 int cant_have_assignment;
3532{
3533 tree default_fn;
3534 tree implicit_fns = NULL_TREE;
3535 tree name = TYPE_IDENTIFIER (t);
3536 tree virtual_dtor = NULL_TREE;
3537 tree *f;
3538
3539 /* Destructor. */
834c6dff 3540 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
61a127b3
MM
3541 {
3542 default_fn = cons_up_default_function (t, name, 0);
3543 check_for_override (default_fn, t);
3544
3545 /* If we couldn't make it work, then pretend we didn't need it. */
3546 if (default_fn == void_type_node)
834c6dff 3547 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
61a127b3
MM
3548 else
3549 {
3550 TREE_CHAIN (default_fn) = implicit_fns;
3551 implicit_fns = default_fn;
3552
3553 if (DECL_VINDEX (default_fn))
3554 virtual_dtor = default_fn;
3555 }
3556 }
834c6dff
MM
3557 else
3558 /* Any non-implicit destructor is non-trivial. */
3559 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
61a127b3
MM
3560
3561 /* Default constructor. */
6eabb241 3562 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
61a127b3
MM
3563 {
3564 default_fn = cons_up_default_function (t, name, 2);
3565 TREE_CHAIN (default_fn) = implicit_fns;
3566 implicit_fns = default_fn;
3567 }
3568
3569 /* Copy constructor. */
6eabb241 3570 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
61a127b3
MM
3571 {
3572 /* ARM 12.18: You get either X(X&) or X(const X&), but
3573 not both. --Chip */
3574 default_fn = cons_up_default_function (t, name,
3575 3 + cant_have_const_cctor);
3576 TREE_CHAIN (default_fn) = implicit_fns;
3577 implicit_fns = default_fn;
3578 }
3579
3580 /* Assignment operator. */
6eabb241 3581 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
61a127b3
MM
3582 {
3583 default_fn = cons_up_default_function (t, name,
3584 5 + cant_have_assignment);
3585 TREE_CHAIN (default_fn) = implicit_fns;
3586 implicit_fns = default_fn;
3587 }
3588
3589 /* Now, hook all of the new functions on to TYPE_METHODS,
3590 and add them to the CLASSTYPE_METHOD_VEC. */
3591 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3592 add_method (t, 0, *f);
3593 *f = TYPE_METHODS (t);
3594 TYPE_METHODS (t) = implicit_fns;
3595
3596 return virtual_dtor;
3597}
3598
f90cdf34
MT
3599/* Subroutine of finish_struct_1. Recursively count the number of fields
3600 in TYPE, including anonymous union members. */
3601
3602static int
3603count_fields (fields)
3604 tree fields;
3605{
3606 tree x;
3607 int n_fields = 0;
3608 for (x = fields; x; x = TREE_CHAIN (x))
3609 {
3610 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3611 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3612 else
3613 n_fields += 1;
3614 }
3615 return n_fields;
3616}
3617
3618/* Subroutine of finish_struct_1. Recursively add all the fields in the
3619 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3620
3621static int
3622add_fields_to_vec (fields, field_vec, idx)
3623 tree fields, field_vec;
3624 int idx;
3625{
3626 tree x;
3627 for (x = fields; x; x = TREE_CHAIN (x))
3628 {
3629 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3630 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3631 else
3632 TREE_VEC_ELT (field_vec, idx++) = x;
3633 }
3634 return idx;
3635}
3636
1e30f9b4
MM
3637/* FIELD is a bit-field. We are finishing the processing for its
3638 enclosing type. Issue any appropriate messages and set appropriate
3639 flags. */
3640
3641static void
3642check_bitfield_decl (field)
3643 tree field;
3644{
3645 tree type = TREE_TYPE (field);
cd8ed629 3646 tree w = NULL_TREE;
1e30f9b4 3647
cd8ed629 3648 /* Detect invalid bit-field type. */
1e30f9b4
MM
3649 if (DECL_INITIAL (field)
3650 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3651 {
3652 cp_error_at ("bit-field `%#D' with non-integral type", field);
cd8ed629 3653 w = error_mark_node;
1e30f9b4
MM
3654 }
3655
3656 /* Detect and ignore out of range field width. */
3657 if (DECL_INITIAL (field))
3658 {
cd8ed629 3659 w = DECL_INITIAL (field);
1e30f9b4
MM
3660
3661 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3662 STRIP_NOPS (w);
3663
3664 /* detect invalid field size. */
3665 if (TREE_CODE (w) == CONST_DECL)
3666 w = DECL_INITIAL (w);
3667 else if (TREE_READONLY_DECL_P (w))
3668 w = decl_constant_value (w);
3669
3670 if (TREE_CODE (w) != INTEGER_CST)
3671 {
3672 cp_error_at ("bit-field `%D' width not an integer constant",
3673 field);
cd8ed629 3674 w = error_mark_node;
1e30f9b4 3675 }
05bccae2 3676 else if (tree_int_cst_sgn (w) < 0)
1e30f9b4 3677 {
1e30f9b4 3678 cp_error_at ("negative width in bit-field `%D'", field);
cd8ed629 3679 w = error_mark_node;
1e30f9b4 3680 }
05bccae2 3681 else if (integer_zerop (w) && DECL_NAME (field) != 0)
1e30f9b4 3682 {
1e30f9b4 3683 cp_error_at ("zero width for bit-field `%D'", field);
cd8ed629 3684 w = error_mark_node;
1e30f9b4 3685 }
05bccae2 3686 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
1e30f9b4
MM
3687 && TREE_CODE (type) != ENUMERAL_TYPE
3688 && TREE_CODE (type) != BOOLEAN_TYPE)
3689 cp_warning_at ("width of `%D' exceeds its type", field);
3690 else if (TREE_CODE (type) == ENUMERAL_TYPE
05bccae2
RK
3691 && (0 > compare_tree_int (w,
3692 min_precision (TYPE_MIN_VALUE (type),
3693 TREE_UNSIGNED (type)))
3694 || 0 > compare_tree_int (w,
3695 min_precision
3696 (TYPE_MAX_VALUE (type),
3697 TREE_UNSIGNED (type)))))
1e30f9b4
MM
3698 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3699 field, type);
cd8ed629
MM
3700 }
3701
3702 /* Remove the bit-field width indicator so that the rest of the
3703 compiler does not treat that value as an initializer. */
3704 DECL_INITIAL (field) = NULL_TREE;
1e30f9b4 3705
cd8ed629
MM
3706 if (w != error_mark_node)
3707 {
3708 DECL_SIZE (field) = convert (bitsizetype, w);
3709 DECL_BIT_FIELD (field) = 1;
1e30f9b4 3710
cd8ed629
MM
3711 if (integer_zerop (w))
3712 {
1e30f9b4 3713#ifdef EMPTY_FIELD_BOUNDARY
cd8ed629
MM
3714 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3715 EMPTY_FIELD_BOUNDARY);
1e30f9b4
MM
3716#endif
3717#ifdef PCC_BITFIELD_TYPE_MATTERS
cd8ed629
MM
3718 if (PCC_BITFIELD_TYPE_MATTERS)
3719 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3720 TYPE_ALIGN (type));
1e30f9b4 3721#endif
1e30f9b4
MM
3722 }
3723 }
3724 else
cd8ed629
MM
3725 {
3726 /* Non-bit-fields are aligned for their type. */
3727 DECL_BIT_FIELD (field) = 0;
3728 CLEAR_DECL_C_BIT_FIELD (field);
3729 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3730 }
1e30f9b4
MM
3731}
3732
3733/* FIELD is a non bit-field. We are finishing the processing for its
3734 enclosing type T. Issue any appropriate messages and set appropriate
3735 flags. */
3736
3737static void
3738check_field_decl (field, t, cant_have_const_ctor,
3739 cant_have_default_ctor, no_const_asn_ref,
3740 any_default_members)
3741 tree field;
3742 tree t;
3743 int *cant_have_const_ctor;
3744 int *cant_have_default_ctor;
3745 int *no_const_asn_ref;
3746 int *any_default_members;
3747{
3748 tree type = strip_array_types (TREE_TYPE (field));
3749
3750 /* An anonymous union cannot contain any fields which would change
3751 the settings of CANT_HAVE_CONST_CTOR and friends. */
3752 if (ANON_UNION_TYPE_P (type))
3753 ;
3754 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3755 structs. So, we recurse through their fields here. */
3756 else if (ANON_AGGR_TYPE_P (type))
3757 {
3758 tree fields;
3759
3760 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
17aec3eb 3761 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
1e30f9b4
MM
3762 check_field_decl (fields, t, cant_have_const_ctor,
3763 cant_have_default_ctor, no_const_asn_ref,
3764 any_default_members);
3765 }
3766 /* Check members with class type for constructors, destructors,
3767 etc. */
3768 else if (CLASS_TYPE_P (type))
3769 {
3770 /* Never let anything with uninheritable virtuals
3771 make it through without complaint. */
3772 abstract_virtuals_error (field, type);
3773
3774 if (TREE_CODE (t) == UNION_TYPE)
3775 {
3776 if (TYPE_NEEDS_CONSTRUCTING (type))
3777 cp_error_at ("member `%#D' with constructor not allowed in union",
3778 field);
834c6dff 3779 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1e30f9b4
MM
3780 cp_error_at ("member `%#D' with destructor not allowed in union",
3781 field);
3782 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3783 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3784 field);
3785 }
3786 else
3787 {
3788 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
834c6dff
MM
3789 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3790 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
1e30f9b4
MM
3791 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3792 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3793 }
3794
3795 if (!TYPE_HAS_CONST_INIT_REF (type))
3796 *cant_have_const_ctor = 1;
3797
3798 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3799 *no_const_asn_ref = 1;
3800
3801 if (TYPE_HAS_CONSTRUCTOR (type)
3802 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3803 *cant_have_default_ctor = 1;
3804 }
3805 if (DECL_INITIAL (field) != NULL_TREE)
3806 {
3807 /* `build_class_init_list' does not recognize
3808 non-FIELD_DECLs. */
3809 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3810 cp_error_at ("multiple fields in union `%T' initialized");
3811 *any_default_members = 1;
3812 }
3813
3814 /* Non-bit-fields are aligned for their type, except packed fields
3815 which require only BITS_PER_UNIT alignment. */
3816 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3817 (DECL_PACKED (field)
3818 ? BITS_PER_UNIT
3819 : TYPE_ALIGN (TREE_TYPE (field))));
6bb88f3b 3820}
1e30f9b4 3821
08b962b0
MM
3822/* Check the data members (both static and non-static), class-scoped
3823 typedefs, etc., appearing in the declaration of T. Issue
3824 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3825 declaration order) of access declarations; each TREE_VALUE in this
3826 list is a USING_DECL.
8d08fdba 3827
08b962b0 3828 In addition, set the following flags:
8d08fdba 3829
08b962b0
MM
3830 EMPTY_P
3831 The class is empty, i.e., contains no non-static data members.
8d08fdba 3832
08b962b0
MM
3833 CANT_HAVE_DEFAULT_CTOR_P
3834 This class cannot have an implicitly generated default
3835 constructor.
8d08fdba 3836
08b962b0
MM
3837 CANT_HAVE_CONST_CTOR_P
3838 This class cannot have an implicitly generated copy constructor
3839 taking a const reference.
8d08fdba 3840
08b962b0
MM
3841 CANT_HAVE_CONST_ASN_REF
3842 This class cannot have an implicitly generated assignment
3843 operator taking a const reference.
8d08fdba 3844
08b962b0
MM
3845 All of these flags should be initialized before calling this
3846 function.
8d08fdba 3847
08b962b0
MM
3848 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3849 fields can be added by adding to this chain. */
8d08fdba 3850
607cf131 3851static void
08b962b0
MM
3852check_field_decls (t, access_decls, empty_p,
3853 cant_have_default_ctor_p, cant_have_const_ctor_p,
3854 no_const_asn_ref_p)
3855 tree t;
3856 tree *access_decls;
3857 int *empty_p;
3858 int *cant_have_default_ctor_p;
3859 int *cant_have_const_ctor_p;
3860 int *no_const_asn_ref_p;
3861{
3862 tree *field;
3863 tree *next;
3864 int has_pointers;
3865 int any_default_members;
3866
58010b57
MM
3867 /* First, delete any duplicate fields. */
3868 delete_duplicate_fields (TYPE_FIELDS (t));
3869
08b962b0
MM
3870 /* Assume there are no access declarations. */
3871 *access_decls = NULL_TREE;
3872 /* Assume this class has no pointer members. */
3873 has_pointers = 0;
3874 /* Assume none of the members of this class have default
3875 initializations. */
3876 any_default_members = 0;
3877
3878 for (field = &TYPE_FIELDS (t); *field; field = next)
8d08fdba 3879 {
08b962b0
MM
3880 tree x = *field;
3881 tree type = TREE_TYPE (x);
8d08fdba 3882
f30432d7 3883 GNU_xref_member (current_class_name, x);
8d08fdba 3884
08b962b0 3885 next = &TREE_CHAIN (x);
8d08fdba 3886
c91a56d2 3887 if (TREE_CODE (x) == FIELD_DECL)
691c003d
MS
3888 {
3889 DECL_PACKED (x) |= TYPE_PACKED (t);
e6267549
JM
3890
3891 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
08b962b0
MM
3892 /* We don't treat zero-width bitfields as making a class
3893 non-empty. */
3894 ;
e6267549 3895 else
f9c528ea
MM
3896 {
3897 /* The class is non-empty. */
3898 *empty_p = 0;
3899 /* The class is not even nearly empty. */
3900 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3901 }
691c003d 3902 }
c91a56d2 3903
cffa8729 3904 if (TREE_CODE (x) == USING_DECL)
f30432d7 3905 {
08b962b0
MM
3906 /* Prune the access declaration from the list of fields. */
3907 *field = TREE_CHAIN (x);
3908
3909 /* Save the access declarations for our caller. */
3910 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3911
3912 /* Since we've reset *FIELD there's no reason to skip to the
3913 next field. */
3914 next = field;
f30432d7
MS
3915 continue;
3916 }
8d08fdba 3917
050367a3
MM
3918 if (TREE_CODE (x) == TYPE_DECL
3919 || TREE_CODE (x) == TEMPLATE_DECL)
f30432d7 3920 continue;
8d08fdba 3921
f30432d7 3922 /* If we've gotten this far, it's a data member, possibly static,
e92cc029 3923 or an enumerator. */
8d08fdba 3924
17aec3eb 3925 DECL_CONTEXT (x) = t;
8d08fdba 3926
f30432d7
MS
3927 /* ``A local class cannot have static data members.'' ARM 9.4 */
3928 if (current_function_decl && TREE_STATIC (x))
8251199e 3929 cp_error_at ("field `%D' in local class cannot be static", x);
8d08fdba 3930
f30432d7
MS
3931 /* Perform error checking that did not get done in
3932 grokdeclarator. */
52fb2769 3933 if (TREE_CODE (type) == FUNCTION_TYPE)
f30432d7 3934 {
8251199e 3935 cp_error_at ("field `%D' invalidly declared function type",
f30432d7 3936 x);
52fb2769
NS
3937 type = build_pointer_type (type);
3938 TREE_TYPE (x) = type;
f30432d7 3939 }
52fb2769 3940 else if (TREE_CODE (type) == METHOD_TYPE)
f30432d7 3941 {
8251199e 3942 cp_error_at ("field `%D' invalidly declared method type", x);
52fb2769
NS
3943 type = build_pointer_type (type);
3944 TREE_TYPE (x) = type;
f30432d7 3945 }
52fb2769 3946 else if (TREE_CODE (type) == OFFSET_TYPE)
f30432d7 3947 {
8251199e 3948 cp_error_at ("field `%D' invalidly declared offset type", x);
52fb2769
NS
3949 type = build_pointer_type (type);
3950 TREE_TYPE (x) = type;
f30432d7 3951 }
8d08fdba 3952
52fb2769 3953 if (type == error_mark_node)
f30432d7 3954 continue;
8d08fdba 3955
f30432d7
MS
3956 /* When this goes into scope, it will be a non-local reference. */
3957 DECL_NONLOCAL (x) = 1;
8d08fdba 3958
f30432d7
MS
3959 if (TREE_CODE (x) == CONST_DECL)
3960 continue;
8d08fdba 3961
f30432d7
MS
3962 if (TREE_CODE (x) == VAR_DECL)
3963 {
3964 if (TREE_CODE (t) == UNION_TYPE)
3965 /* Unions cannot have static members. */
8251199e 3966 cp_error_at ("field `%D' declared static in union", x);
8d08fdba 3967
f30432d7
MS
3968 continue;
3969 }
8d08fdba 3970
f30432d7 3971 /* Now it can only be a FIELD_DECL. */
8d08fdba 3972
f30432d7 3973 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
08b962b0 3974 CLASSTYPE_NON_AGGREGATE (t) = 1;
8d08fdba 3975
f30432d7
MS
3976 /* If this is of reference type, check if it needs an init.
3977 Also do a little ANSI jig if necessary. */
52fb2769 3978 if (TREE_CODE (type) == REFERENCE_TYPE)
f30432d7 3979 {
08b962b0 3980 CLASSTYPE_NON_POD_P (t) = 1;
f30432d7 3981 if (DECL_INITIAL (x) == NULL_TREE)
08b962b0 3982 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
8d08fdba 3983
f30432d7
MS
3984 /* ARM $12.6.2: [A member initializer list] (or, for an
3985 aggregate, initialization by a brace-enclosed list) is the
3986 only way to initialize nonstatic const and reference
3987 members. */
08b962b0 3988 *cant_have_default_ctor_p = 1;
e349ee73 3989 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
f30432d7
MS
3990
3991 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3992 {
3993 if (DECL_NAME (x))
8251199e 3994 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
f30432d7 3995 else
8251199e 3996 cp_warning_at ("non-static reference in class without a constructor", x);
8d08fdba 3997 }
f30432d7 3998 }
8d08fdba 3999
1e30f9b4 4000 type = strip_array_types (type);
52fb2769
NS
4001
4002 if (TREE_CODE (type) == POINTER_TYPE)
824b9a4c
MS
4003 has_pointers = 1;
4004
52fb2769 4005 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
08b962b0 4006 CLASSTYPE_HAS_MUTABLE (t) = 1;
a7a7710d 4007
c4d6cee3
JM
4008 if (! pod_type_p (type)
4009 /* For some reason, pointers to members are POD types themselves,
4010 but are not allowed in POD structs. Silly. */
4011 || TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
08b962b0 4012 CLASSTYPE_NON_POD_P (t) = 1;
52fb2769 4013
f30432d7 4014 /* If any field is const, the structure type is pseudo-const. */
52fb2769 4015 if (CP_TYPE_CONST_P (type))
f30432d7
MS
4016 {
4017 C_TYPE_FIELDS_READONLY (t) = 1;
4018 if (DECL_INITIAL (x) == NULL_TREE)
08b962b0 4019 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
f30432d7
MS
4020
4021 /* ARM $12.6.2: [A member initializer list] (or, for an
4022 aggregate, initialization by a brace-enclosed list) is the
4023 only way to initialize nonstatic const and reference
4024 members. */
08b962b0 4025 *cant_have_default_ctor_p = 1;
e349ee73 4026 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
f30432d7 4027
6eabb241 4028 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
f30432d7
MS
4029 {
4030 if (DECL_NAME (x))
8251199e 4031 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
f30432d7 4032 else
8251199e 4033 cp_warning_at ("non-static const member in class without a constructor", x);
f30432d7
MS
4034 }
4035 }
08b962b0
MM
4036 /* A field that is pseudo-const makes the structure likewise. */
4037 else if (IS_AGGR_TYPE (type))
f30432d7 4038 {
08b962b0
MM
4039 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
4040 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
4041 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
f30432d7 4042 }
8d08fdba 4043
162bc98d
JM
4044 /* We set DECL_C_BIT_FIELD in grokbitfield.
4045 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
4046 if (DECL_C_BIT_FIELD (x))
1e30f9b4 4047 check_bitfield_decl (x);
f30432d7 4048 else
1e30f9b4 4049 check_field_decl (x, t,
08b962b0
MM
4050 cant_have_const_ctor_p,
4051 cant_have_default_ctor_p,
4052 no_const_asn_ref_p,
1e30f9b4 4053 &any_default_members);
8d08fdba
MS
4054 }
4055
824b9a4c 4056 /* Effective C++ rule 11. */
7834ab39 4057 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
824b9a4c
MS
4058 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
4059 {
8251199e 4060 cp_warning ("`%#T' has pointer data members", t);
824b9a4c
MS
4061
4062 if (! TYPE_HAS_INIT_REF (t))
4063 {
8251199e 4064 cp_warning (" but does not override `%T(const %T&)'", t, t);
824b9a4c 4065 if (! TYPE_HAS_ASSIGN_REF (t))
8251199e 4066 cp_warning (" or `operator=(const %T&)'", t);
824b9a4c
MS
4067 }
4068 else if (! TYPE_HAS_ASSIGN_REF (t))
8251199e 4069 cp_warning (" but does not override `operator=(const %T&)'", t);
824b9a4c 4070 }
08b962b0 4071
607cf131
MM
4072
4073 /* Check anonymous struct/anonymous union fields. */
4074 finish_struct_anon (t);
4075
08b962b0
MM
4076 /* We've built up the list of access declarations in reverse order.
4077 Fix that now. */
4078 *access_decls = nreverse (*access_decls);
08b962b0
MM
4079}
4080
58010b57
MM
4081/* Return a FIELD_DECL for a pointer-to-virtual-table or
4082 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
4083 field are as indicated. The CLASS_TYPE in which this field occurs
07a3462a
JW
4084 is also indicated. FCONTEXT is the type that is needed for the debug
4085 info output routines. *EMPTY_P is set to a non-zero value by this
58010b57
MM
4086 function to indicate that a class containing this field is
4087 non-empty. */
4088
4089static tree
07a3462a 4090build_vtbl_or_vbase_field (name, assembler_name, type, class_type, fcontext,
58010b57
MM
4091 empty_p)
4092 tree name;
4093 tree assembler_name;
4094 tree type;
4095 tree class_type;
07a3462a 4096 tree fcontext;
58010b57
MM
4097 int *empty_p;
4098{
4099 tree field;
4100
4101 /* This class is non-empty. */
4102 *empty_p = 0;
4103
4104 /* Build the FIELD_DECL. */
4105 field = build_lang_decl (FIELD_DECL, name, type);
4106 DECL_ASSEMBLER_NAME (field) = assembler_name;
4107 DECL_VIRTUAL_P (field) = 1;
4108 DECL_ARTIFICIAL (field) = 1;
4109 DECL_FIELD_CONTEXT (field) = class_type;
07a3462a 4110 DECL_FCONTEXT (field) = fcontext;
58010b57
MM
4111 DECL_ALIGN (field) = TYPE_ALIGN (type);
4112
4113 /* Return it. */
4114 return field;
4115}
4116
5c24fba6
MM
4117/* Record the type of BINFO in the slot in DATA (which is really a
4118 `varray_type *') corresponding to the BINFO_OFFSET. */
607cf131 4119
5c24fba6
MM
4120static tree
4121dfs_record_base_offsets (binfo, data)
4122 tree binfo;
4123 void *data;
4124{
4125 varray_type *v;
770ae6cc 4126 unsigned HOST_WIDE_INT offset = tree_low_cst (BINFO_OFFSET (binfo), 1);
5c24fba6
MM
4127
4128 v = (varray_type *) data;
4129 while (VARRAY_SIZE (*v) <= offset)
4130 VARRAY_GROW (*v, 2 * VARRAY_SIZE (*v));
4131 VARRAY_TREE (*v, offset) = tree_cons (NULL_TREE,
4132 BINFO_TYPE (binfo),
4133 VARRAY_TREE (*v, offset));
4134
4135 return NULL_TREE;
607cf131
MM
4136}
4137
9785e4b1
MM
4138/* Add the offset of BINFO and its bases to BASE_OFFSETS. */
4139
4140static void
4141record_base_offsets (binfo, base_offsets)
4142 tree binfo;
4143 varray_type *base_offsets;
4144{
9785e4b1
MM
4145 dfs_walk (binfo,
4146 dfs_record_base_offsets,
4147 dfs_skip_vbases,
4148 base_offsets);
9785e4b1
MM
4149}
4150
5c24fba6
MM
4151/* Returns non-NULL if there is already an entry in DATA (which is
4152 really a `varray_type') indicating that an object with the same
4153 type of BINFO is already at the BINFO_OFFSET for BINFO. */
d77249e7
MM
4154
4155static tree
5c24fba6
MM
4156dfs_search_base_offsets (binfo, data)
4157 tree binfo;
4158 void *data;
4159{
4160 if (is_empty_class (BINFO_TYPE (binfo)))
4161 {
4162 varray_type v = (varray_type) data;
770ae6cc
RK
4163 /* Find the offset for this BINFO. */
4164 unsigned HOST_WIDE_INT offset = tree_low_cst (BINFO_OFFSET (binfo), 1);
5c24fba6
MM
4165 tree t;
4166
5c24fba6
MM
4167 /* If we haven't yet encountered any objects at offsets that
4168 big, then there's no conflict. */
4169 if (VARRAY_SIZE (v) <= offset)
4170 return NULL_TREE;
4171 /* Otherwise, go through the objects already allocated at this
4172 offset. */
4173 for (t = VARRAY_TREE (v, offset); t; t = TREE_CHAIN (t))
4174 if (same_type_p (TREE_VALUE (t), BINFO_TYPE (binfo)))
4175 return binfo;
4176 }
4177
4178 return NULL_TREE;
4179}
4180
9785e4b1
MM
4181/* Returns non-zero if there's a conflict between BINFO and a base
4182 already mentioned in BASE_OFFSETS if BINFO is placed at its current
4183 BINFO_OFFSET. */
4184
4185static int
4186layout_conflict_p (binfo, base_offsets)
4187 tree binfo;
4188 varray_type base_offsets;
4189{
4190 return dfs_walk (binfo, dfs_search_base_offsets, dfs_skip_vbases,
4191 base_offsets) != NULL_TREE;
4192}
4193
5c24fba6
MM
4194/* DECL is a FIELD_DECL corresponding either to a base subobject of a
4195 non-static data member of the type indicated by RLI. BINFO is the
4196 binfo corresponding to the base subobject, or, if this is a
4197 non-static data-member, a dummy BINFO for the type of the data
01955e96
MM
4198 member. BINFO may be NULL if checks to see if the field overlaps
4199 an existing field with the same type are not required. V maps
4200 offsets to types already located at those offsets. This function
4201 determines the position of the DECL. */
5c24fba6
MM
4202
4203static void
4204layout_nonempty_base_or_field (rli, decl, binfo, v)
4205 record_layout_info rli;
4206 tree decl;
4207 tree binfo;
4208 varray_type v;
4209{
4210 /* Try to place the field. It may take more than one try if we have
4211 a hard time placing the field without putting two objects of the
4212 same type at the same address. */
4213 while (1)
4214 {
4215 tree offset;
770ae6cc 4216 struct record_layout_info old_rli = *rli;
5c24fba6 4217
770ae6cc
RK
4218 /* Place this field. */
4219 place_field (rli, decl);
5c24fba6
MM
4220
4221 /* Now that we know where it wil be placed, update its
4222 BINFO_OFFSET. */
770ae6cc 4223 offset = convert (ssizetype, byte_position (decl));
01955e96
MM
4224 if (binfo)
4225 propagate_binfo_offsets (binfo, offset);
5c24fba6
MM
4226
4227 /* We have to check to see whether or not there is already
4228 something of the same type at the offset we're about to use.
4229 For example:
4230
4231 struct S {};
4232 struct T : public S { int i; };
4233 struct U : public S, public T {};
4234
4235 Here, we put S at offset zero in U. Then, we can't put T at
4236 offset zero -- its S component would be at the same address
4237 as the S we already allocated. So, we have to skip ahead.
4238 Since all data members, including those whose type is an
4239 empty class, have non-zero size, any overlap can happen only
4240 with a direct or indirect base-class -- it can't happen with
4241 a data member. */
9785e4b1 4242 if (binfo && flag_new_abi && layout_conflict_p (binfo, v))
5c24fba6
MM
4243 {
4244 /* Undo the propogate_binfo_offsets call. */
770ae6cc 4245 offset = size_diffop (size_zero_node, offset);
5c24fba6 4246 propagate_binfo_offsets (binfo, offset);
770ae6cc 4247
5c24fba6
MM
4248 /* Strip off the size allocated to this field. That puts us
4249 at the first place we could have put the field with
4250 proper alignment. */
770ae6cc
RK
4251 *rli = old_rli;
4252
4253 /* Bump up by the alignment required for the type, without
5c24fba6 4254 virtual base classes. */
770ae6cc
RK
4255 rli->bitpos
4256 = size_binop (PLUS_EXPR, rli->bitpos,
4257 bitsize_int (CLASSTYPE_ALIGN (BINFO_TYPE (binfo))));
4258 normalize_rli (rli);
5c24fba6
MM
4259 }
4260 else
4261 /* There was no conflict. We're done laying out this field. */
4262 break;
4263 }
4264}
4265
9785e4b1 4266/* Layout the empty base BINFO. EOC indicates the byte currently just
ec386958
MM
4267 past the end of the class, and should be correctly aligned for a
4268 class of the type indicated by BINFO; BINFO_OFFSETS gives the
4269 offsets of the other bases allocated so far. */
9785e4b1
MM
4270
4271static void
4272layout_empty_base (binfo, eoc, binfo_offsets)
4273 tree binfo;
4274 tree eoc;
4275 varray_type binfo_offsets;
4276{
ec386958 4277 tree alignment;
9785e4b1
MM
4278 tree basetype = BINFO_TYPE (binfo);
4279
4280 /* This routine should only be used for empty classes. */
4281 my_friendly_assert (is_empty_class (basetype), 20000321);
ec386958 4282 alignment = ssize_int (CLASSTYPE_ALIGN (basetype));
9785e4b1
MM
4283
4284 /* This is an empty base class. We first try to put it at offset
4285 zero. */
4286 if (layout_conflict_p (binfo, binfo_offsets))
4287 {
4288 /* That didn't work. Now, we move forward from the next
4289 available spot in the class. */
770ae6cc 4290 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
9785e4b1
MM
4291 while (1)
4292 {
4293 if (!layout_conflict_p (binfo, binfo_offsets))
4294 /* We finally found a spot where there's no overlap. */
4295 break;
4296
4297 /* There's overlap here, too. Bump along to the next spot. */
ec386958 4298 propagate_binfo_offsets (binfo, alignment);
9785e4b1
MM
4299 }
4300 }
4301}
4302
5c24fba6
MM
4303/* Build a FIELD_DECL for the base given by BINFO in the class
4304 *indicated by RLI. If the new object is non-empty, clear *EMPTY_P.
4305 *BASE_ALIGN is a running maximum of the alignments of any base
4306 *class. */
4307
4308static void
4309build_base_field (rli, binfo, empty_p, base_align, v)
4310 record_layout_info rli;
d77249e7
MM
4311 tree binfo;
4312 int *empty_p;
d77249e7 4313 unsigned int *base_align;
5c24fba6 4314 varray_type *v;
d77249e7
MM
4315{
4316 tree basetype = BINFO_TYPE (binfo);
4317 tree decl;
4318
d0f062fb 4319 if (!COMPLETE_TYPE_P (basetype))
d77249e7
MM
4320 /* This error is now reported in xref_tag, thus giving better
4321 location information. */
5c24fba6 4322 return;
d77249e7
MM
4323
4324 decl = build_lang_decl (FIELD_DECL, NULL_TREE, basetype);
4325 DECL_ARTIFICIAL (decl) = 1;
5c24fba6 4326 DECL_FIELD_CONTEXT (decl) = rli->t;
d77249e7 4327 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
06ceef4e 4328 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
d77249e7
MM
4329 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4330
d77249e7
MM
4331 if (! flag_new_abi)
4332 {
4333 /* Brain damage for backwards compatibility. For no good
9785e4b1 4334 reason, the old basetype layout made every base have at least
d77249e7 4335 as large as the alignment for the bases up to that point,
9785e4b1 4336 gratuitously wasting space. So we do the same thing here. */
d77249e7
MM
4337 *base_align = MAX (*base_align, DECL_ALIGN (decl));
4338 DECL_SIZE (decl)
665f2503 4339 = size_binop (MAX_EXPR, DECL_SIZE (decl), bitsize_int (*base_align));
06ceef4e 4340 DECL_SIZE_UNIT (decl)
665f2503
RK
4341 = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
4342 size_int (*base_align / BITS_PER_UNIT));
d77249e7
MM
4343 }
4344
5c24fba6
MM
4345 if (!integer_zerop (DECL_SIZE (decl)))
4346 {
4347 /* The containing class is non-empty because it has a non-empty
4348 base class. */
4349 *empty_p = 0;
4350
4351 /* Try to place the field. It may take more than one try if we
4352 have a hard time placing the field without putting two
4353 objects of the same type at the same address. */
4354 layout_nonempty_base_or_field (rli, decl, binfo, *v);
4355 }
4356 else
ec386958
MM
4357 {
4358 unsigned HOST_WIDE_INT eoc;
4359
4360 /* On some platforms (ARM), even empty classes will not be
4361 byte-aligned. */
4362 eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
4363 eoc = CEIL (eoc, DECL_ALIGN (decl)) * DECL_ALIGN (decl);
4364 layout_empty_base (binfo, size_int (eoc), *v);
4365 }
5c24fba6
MM
4366
4367 /* Check for inaccessible base classes. If the same base class
4368 appears more than once in the hierarchy, but isn't virtual, then
4369 it's ambiguous. */
4370 if (get_base_distance (basetype, rli->t, 0, NULL) == -2)
4371 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4372 basetype, rli->t);
4373
4374 /* Record the offsets of BINFO and its base subobjects. */
9785e4b1 4375 record_base_offsets (binfo, v);
d77249e7
MM
4376}
4377
5c24fba6
MM
4378/* Layout all of the non-virtual base classes. Returns a map from
4379 offsets to types present at those offsets. */
607cf131 4380
5c24fba6
MM
4381static varray_type
4382build_base_fields (rli, empty_p)
4383 record_layout_info rli;
607cf131
MM
4384 int *empty_p;
4385{
4386 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4387 subobjects. */
5c24fba6 4388 tree rec = rli->t;
607cf131 4389 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
5c24fba6
MM
4390 int i;
4391 varray_type v;
607cf131
MM
4392 unsigned int base_align = 0;
4393
5c24fba6
MM
4394 /* Create the table mapping offsets to empty base classes. */
4395 VARRAY_TREE_INIT (v, 32, "v");
4396
d77249e7
MM
4397 /* Under the new ABI, the primary base class is always allocated
4398 first. */
4399 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
5c24fba6
MM
4400 build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec),
4401 empty_p, &base_align, &v);
d77249e7
MM
4402
4403 /* Now allocate the rest of the bases. */
607cf131
MM
4404 for (i = 0; i < n_baseclasses; ++i)
4405 {
d77249e7 4406 tree base_binfo;
607cf131 4407
d77249e7
MM
4408 /* Under the new ABI, the primary base was already allocated
4409 above, so we don't need to allocate it again here. */
4410 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (rec))
607cf131
MM
4411 continue;
4412
d77249e7
MM
4413 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
4414
8026246f
MM
4415 /* A primary virtual base class is allocated just like any other
4416 base class, but a non-primary virtual base is allocated
9785e4b1 4417 later, in layout_virtual_bases. */
8026246f 4418 if (TREE_VIA_VIRTUAL (base_binfo)
d77249e7 4419 && !BINFO_PRIMARY_MARKED_P (base_binfo))
607cf131
MM
4420 continue;
4421
5c24fba6 4422 build_base_field (rli, base_binfo, empty_p, &base_align, &v);
607cf131
MM
4423 }
4424
5c24fba6 4425 return v;
607cf131
MM
4426}
4427
58010b57
MM
4428/* Go through the TYPE_METHODS of T issuing any appropriate
4429 diagnostics, figuring out which methods override which other
3ef397c1 4430 methods, and so forth. */
58010b57
MM
4431
4432static void
4433check_methods (t)
4434 tree t;
4435{
4436 tree x;
834c6dff 4437 int seen_one_arg_array_delete_p = 0;
58010b57
MM
4438
4439 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
4440 {
4441 GNU_xref_member (current_class_name, x);
4442
4443 /* If this was an evil function, don't keep it in class. */
4444 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
4445 continue;
4446
58010b57 4447 check_for_override (x, t);
fee7654e 4448 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
58010b57
MM
4449 cp_error_at ("initializer specified for non-virtual method `%D'", x);
4450
4451 /* The name of the field is the original field name
4452 Save this in auxiliary field for later overloading. */
4453 if (DECL_VINDEX (x))
4454 {
3ef397c1 4455 TYPE_POLYMORPHIC_P (t) = 1;
fee7654e
MM
4456 if (DECL_PURE_VIRTUAL_P (x))
4457 CLASSTYPE_PURE_VIRTUALS (t)
4458 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
58010b57 4459 }
834c6dff
MM
4460
4461 if (DECL_ARRAY_DELETE_OPERATOR_P (x))
4462 {
4463 tree second_parm;
4464
4465 /* When dynamically allocating an array of this type, we
4466 need a "cookie" to record how many elements we allocated,
4467 even if the array elements have no non-trivial
4468 destructor, if the usual array deallocation function
4469 takes a second argument of type size_t. The standard (in
4470 [class.free]) requires that the second argument be set
4471 correctly. */
4472 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (x)));
4473 /* This is overly conservative, but we must maintain this
4474 behavior for backwards compatibility. */
4475 if (!flag_new_abi && second_parm != void_list_node)
4476 TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
4477 /* Under the new ABI, we choose only those function that are
4478 explicitly declared as `operator delete[] (void *,
4479 size_t)'. */
4480 else if (flag_new_abi
4481 && !seen_one_arg_array_delete_p
4482 && second_parm
4483 && TREE_CHAIN (second_parm) == void_list_node
4484 && same_type_p (TREE_VALUE (second_parm), sizetype))
4485 TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
4486 /* If there's no second parameter, then this is the usual
4487 deallocation function. */
4488 else if (second_parm == void_list_node)
4489 seen_one_arg_array_delete_p = 1;
4490 }
58010b57 4491 }
58010b57
MM
4492}
4493
4494/* Remove all zero-width bit-fields from T. */
4495
4496static void
4497remove_zero_width_bit_fields (t)
4498 tree t;
4499{
4500 tree *fieldsp;
4501
4502 fieldsp = &TYPE_FIELDS (t);
4503 while (*fieldsp)
4504 {
4505 if (TREE_CODE (*fieldsp) == FIELD_DECL
4506 && DECL_C_BIT_FIELD (*fieldsp)
4507 && DECL_INITIAL (*fieldsp))
4508 *fieldsp = TREE_CHAIN (*fieldsp);
4509 else
4510 fieldsp = &TREE_CHAIN (*fieldsp);
4511 }
4512}
4513
607cf131
MM
4514/* Check the validity of the bases and members declared in T. Add any
4515 implicitly-generated functions (like copy-constructors and
4516 assignment operators). Compute various flag bits (like
4517 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4518 level: i.e., independently of the ABI in use. */
4519
4520static void
4521check_bases_and_members (t, empty_p)
4522 tree t;
4523 int *empty_p;
4524{
4525 /* Nonzero if we are not allowed to generate a default constructor
4526 for this case. */
4527 int cant_have_default_ctor;
4528 /* Nonzero if the implicitly generated copy constructor should take
4529 a non-const reference argument. */
4530 int cant_have_const_ctor;
4531 /* Nonzero if the the implicitly generated assignment operator
4532 should take a non-const reference argument. */
4533 int no_const_asn_ref;
4534 tree access_decls;
4535
4536 /* By default, we use const reference arguments and generate default
4537 constructors. */
4538 cant_have_default_ctor = 0;
4539 cant_have_const_ctor = 0;
4540 no_const_asn_ref = 0;
4541
f9c528ea
MM
4542 /* Assume that the class is nearly empty; we'll clear this flag if
4543 it turns out not to be nearly empty. */
4544 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4545
607cf131
MM
4546 /* Check all the base-classes. */
4547 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4548 &no_const_asn_ref);
4549
4550 /* Check all the data member declarations. */
4551 check_field_decls (t, &access_decls, empty_p,
4552 &cant_have_default_ctor,
4553 &cant_have_const_ctor,
4554 &no_const_asn_ref);
4555
4556 /* Check all the method declarations. */
4557 check_methods (t);
4558
bbd15aac
MM
4559 /* A nearly-empty class has to be vptr-containing; a nearly empty
4560 class contains just a vptr. */
4561 if (!TYPE_CONTAINS_VPTR_P (t))
f9c528ea
MM
4562 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4563
607cf131
MM
4564 /* Do some bookkeeping that will guide the generation of implicitly
4565 declared member functions. */
4566 TYPE_HAS_COMPLEX_INIT_REF (t)
3ef397c1
MM
4567 |= (TYPE_HAS_INIT_REF (t)
4568 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4569 || TYPE_POLYMORPHIC_P (t));
607cf131 4570 TYPE_NEEDS_CONSTRUCTING (t)
3ef397c1
MM
4571 |= (TYPE_HAS_CONSTRUCTOR (t)
4572 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4573 || TYPE_POLYMORPHIC_P (t));
4574 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4575 || TYPE_POLYMORPHIC_P (t));
607cf131
MM
4576 CLASSTYPE_NON_POD_P (t)
4577 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4578 || TYPE_HAS_ASSIGN_REF (t));
4579 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4580 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4581 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
4582
4583 /* Synthesize any needed methods. Note that methods will be synthesized
4584 for anonymous unions; grok_x_components undoes that. */
4585 add_implicitly_declared_members (t, cant_have_default_ctor,
4586 cant_have_const_ctor,
4587 no_const_asn_ref);
4588
aa52c1ff
JM
4589 /* Process the using-declarations. */
4590 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4591 handle_using_decl (TREE_VALUE (access_decls), t);
4592
607cf131
MM
4593 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4594 finish_struct_methods (t);
607cf131
MM
4595}
4596
3ef397c1 4597/* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5c24fba6
MM
4598 accordingly. If a new vfield was created (because T doesn't have a
4599 primary base class), then the newly created field is returned. It
4600 is not added to the TYPE_FIELDS list; it is the callers
4601 responsibility to do that. */
3ef397c1 4602
5c24fba6 4603static tree
d2c5305b 4604create_vtable_ptr (t, empty_p, has_virtual_p,
051e6fd7 4605 new_virtuals_p, overridden_virtuals_p)
3ef397c1
MM
4606 tree t;
4607 int *empty_p;
4608 int *has_virtual_p;
051e6fd7
MM
4609 tree *new_virtuals_p;
4610 tree *overridden_virtuals_p;
3ef397c1
MM
4611{
4612 tree fn;
4613
3ef397c1
MM
4614 /* Loop over the virtual functions, adding them to our various
4615 vtables. */
4616 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4617 if (DECL_VINDEX (fn))
051e6fd7 4618 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
3ef397c1
MM
4619 has_virtual_p, fn, t);
4620
bbd15aac
MM
4621 /* Even if there weren't any new virtual functions, we might need a
4622 new virtual function table if we're supposed to include vptrs in
4623 all classes that need them. */
4624 if (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ())
4625 start_vtable (t, has_virtual_p);
4626
3ef397c1
MM
4627 /* If we couldn't find an appropriate base class, create a new field
4628 here. */
4629 if (*has_virtual_p && !TYPE_VFIELD (t))
4630 {
4631 /* We build this decl with vtbl_ptr_type_node, which is a
4632 `vtable_entry_type*'. It might seem more precise to use
4633 `vtable_entry_type (*)[N]' where N is the number of firtual
4634 functions. However, that would require the vtable pointer in
4635 base classes to have a different type than the vtable pointer
4636 in derived classes. We could make that happen, but that
4637 still wouldn't solve all the problems. In particular, the
4638 type-based alias analysis code would decide that assignments
4639 to the base class vtable pointer can't alias assignments to
4640 the derived class vtable pointer, since they have different
4641 types. Thus, in an derived class destructor, where the base
4642 class constructor was inlined, we could generate bad code for
4643 setting up the vtable pointer.
4644
4645 Therefore, we use one type for all vtable pointers. We still
4646 use a type-correct type; it's just doesn't indicate the array
4647 bounds. That's better than using `void*' or some such; it's
4648 cleaner, and it let's the alias analysis code know that these
4649 stores cannot alias stores to void*! */
4650 TYPE_VFIELD (t)
4651 = build_vtbl_or_vbase_field (get_vfield_name (t),
4652 get_identifier (VFIELD_BASE),
4653 vtbl_ptr_type_node,
4654 t,
07a3462a 4655 t,
3ef397c1
MM
4656 empty_p);
4657
5c24fba6
MM
4658 if (flag_new_abi && CLASSTYPE_N_BASECLASSES (t))
4659 /* If there were any baseclasses, they can't possibly be at
4660 offset zero any more, because that's where the vtable
4661 pointer is. So, converting to a base class is going to
4662 take work. */
4663 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
3ef397c1 4664
5c24fba6 4665 return TYPE_VFIELD (t);
3ef397c1 4666 }
5c24fba6
MM
4667
4668 return NULL_TREE;
3ef397c1
MM
4669}
4670
2ef16140
MM
4671/* Fixup the inline function given by INFO now that the class is
4672 complete. */
08b962b0 4673
2ef16140
MM
4674static void
4675fixup_pending_inline (info)
4676 struct pending_inline *info;
4677{
4678 if (info)
4679 {
4680 tree args;
4681 tree fn = info->fndecl;
08b962b0 4682
2ef16140
MM
4683 args = DECL_ARGUMENTS (fn);
4684 while (args)
4685 {
4686 DECL_CONTEXT (args) = fn;
4687 args = TREE_CHAIN (args);
4688 }
4689 }
4690}
08b962b0 4691
2ef16140
MM
4692/* Fixup the inline methods and friends in TYPE now that TYPE is
4693 complete. */
08b962b0 4694
2ef16140
MM
4695static void
4696fixup_inline_methods (type)
4697 tree type;
08b962b0 4698{
2ef16140 4699 tree method = TYPE_METHODS (type);
08b962b0 4700
2ef16140 4701 if (method && TREE_CODE (method) == TREE_VEC)
08b962b0 4702 {
2ef16140
MM
4703 if (TREE_VEC_ELT (method, 1))
4704 method = TREE_VEC_ELT (method, 1);
4705 else if (TREE_VEC_ELT (method, 0))
4706 method = TREE_VEC_ELT (method, 0);
08b962b0 4707 else
2ef16140 4708 method = TREE_VEC_ELT (method, 2);
08b962b0
MM
4709 }
4710
2ef16140
MM
4711 /* Do inline member functions. */
4712 for (; method; method = TREE_CHAIN (method))
4713 fixup_pending_inline (DECL_PENDING_INLINE_INFO (method));
08b962b0 4714
2ef16140
MM
4715 /* Do friends. */
4716 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4717 method;
4718 method = TREE_CHAIN (method))
4719 fixup_pending_inline (DECL_PENDING_INLINE_INFO (TREE_VALUE (method)));
351c54c8 4720 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
2ef16140 4721}
08b962b0 4722
9d4c0187
MM
4723/* Called from propagate_binfo_offsets via dfs_walk. */
4724
4725static tree
4726dfs_propagate_binfo_offsets (binfo, data)
4727 tree binfo;
4728 void *data;
4729{
4730 tree offset = (tree) data;
4731
770ae6cc
RK
4732 /* Update the BINFO_OFFSET for this base. Allow for the case where it
4733 might be negative. */
4734 BINFO_OFFSET (binfo)
4735 = convert (sizetype, size_binop (PLUS_EXPR,
4736 convert (ssizetype, BINFO_OFFSET (binfo)),
4737 offset));
9d4c0187
MM
4738 SET_BINFO_MARKED (binfo);
4739
4740 return NULL_TREE;
4741}
4742
4743/* Add OFFSET to all base types of BINFO which is a base in the
4744 hierarchy dominated by T.
80fd5f48
MM
4745
4746 OFFSET, which is a type offset, is number of bytes.
4747
4748 Note that we don't have to worry about having two paths to the
4749 same base type, since this type owns its association list. */
4750
4751static void
4752propagate_binfo_offsets (binfo, offset)
4753 tree binfo;
4754 tree offset;
4755{
9d4c0187
MM
4756 dfs_walk (binfo,
4757 dfs_propagate_binfo_offsets,
4758 dfs_skip_nonprimary_vbases_unmarkedp,
4759 offset);
4760 dfs_walk (binfo,
4761 dfs_unmark,
4762 dfs_skip_nonprimary_vbases_markedp,
4763 NULL);
80fd5f48
MM
4764}
4765
9d4c0187 4766/* Called via dfs_walk from layout_virtual bases. */
80fd5f48
MM
4767
4768static tree
9d4c0187 4769dfs_set_offset_for_shared_vbases (binfo, data)
80fd5f48
MM
4770 tree binfo;
4771 void *data;
4772{
9d4c0187 4773 if (TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_MARKED_P (binfo))
80fd5f48 4774 {
9d4c0187
MM
4775 /* Update the shared copy. */
4776 tree shared_binfo;
80fd5f48 4777
9d4c0187
MM
4778 shared_binfo = BINFO_FOR_VBASE (BINFO_TYPE (binfo), (tree) data);
4779 BINFO_OFFSET (shared_binfo) = BINFO_OFFSET (binfo);
80fd5f48
MM
4780 }
4781
9d4c0187
MM
4782 return NULL_TREE;
4783}
4784
4785/* Called via dfs_walk from layout_virtual bases. */
4786
4787static tree
4788dfs_set_offset_for_unshared_vbases (binfo, data)
4789 tree binfo;
4790 void *data;
4791{
4792 /* If this is a virtual base, make sure it has the same offset as
4793 the shared copy. If it's a primary base, then we know it's
4794 correct. */
4795 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4796 {
4797 tree t = (tree) data;
4798 tree vbase;
4799 tree offset;
4800
4801 vbase = BINFO_FOR_VBASE (BINFO_TYPE (binfo), t);
cd8ed629 4802 offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
9d4c0187
MM
4803 propagate_binfo_offsets (binfo, offset);
4804 }
80fd5f48
MM
4805
4806 return NULL_TREE;
4807}
4808
4809/* Set BINFO_OFFSET for all of the virtual bases for T. Update
9785e4b1
MM
4810 TYPE_ALIGN and TYPE_SIZE for T. BASE_OFFSETS is a varray mapping
4811 offsets to the types at those offsets. */
80fd5f48 4812
d2c5305b 4813static void
9785e4b1 4814layout_virtual_bases (t, base_offsets)
80fd5f48 4815 tree t;
9785e4b1 4816 varray_type *base_offsets;
80fd5f48
MM
4817{
4818 tree vbase;
665f2503 4819 unsigned HOST_WIDE_INT dsize;
9785e4b1
MM
4820 unsigned HOST_WIDE_INT eoc;
4821
4822 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4823 return;
4824
4825#ifdef STRUCTURE_SIZE_BOUNDARY
4826 /* Packed structures don't need to have minimum size. */
736fb497
MM
4827 if (! TYPE_PACKED (t))
4828 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
9785e4b1 4829#endif
80fd5f48 4830
80fd5f48 4831 /* DSIZE is the size of the class without the virtual bases. */
665f2503
RK
4832 dsize = tree_low_cst (TYPE_SIZE (t), 1);
4833
80fd5f48
MM
4834 /* Make every class have alignment of at least one. */
4835 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4836
9d4c0187
MM
4837 /* Go through the virtual bases, allocating space for each virtual
4838 base that is not already a primary base class. */
80fd5f48
MM
4839 for (vbase = CLASSTYPE_VBASECLASSES (t);
4840 vbase;
4841 vbase = TREE_CHAIN (vbase))
9d4c0187 4842 if (!BINFO_VBASE_PRIMARY_P (vbase))
80fd5f48
MM
4843 {
4844 /* This virtual base is not a primary base of any class in the
4845 hierarchy, so we have to add space for it. */
4846 tree basetype;
4847 unsigned int desired_align;
4848
4849 basetype = BINFO_TYPE (vbase);
9785e4b1 4850
ec386958
MM
4851 if (flag_new_abi)
4852 desired_align = CLASSTYPE_ALIGN (basetype);
4853 else
4854 /* Under the old ABI, virtual bases were aligned as for the
4855 entire base object (including its virtual bases). That's
4856 wasteful, in general. */
4857 desired_align = TYPE_ALIGN (basetype);
4858 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4859
4860 /* Add padding so that we can put the virtual base class at an
4861 appropriately aligned offset. */
4862 dsize = CEIL (dsize, desired_align) * desired_align;
4863
9785e4b1
MM
4864 /* Under the new ABI, we try to squish empty virtual bases in
4865 just like ordinary empty bases. */
4866 if (flag_new_abi && is_empty_class (basetype))
4867 layout_empty_base (vbase,
4868 size_int (CEIL (dsize, BITS_PER_UNIT)),
4869 *base_offsets);
4870 else
4871 {
9785e4b1
MM
4872 /* And compute the offset of the virtual base. */
4873 propagate_binfo_offsets (vbase,
770ae6cc 4874 ssize_int (CEIL (dsize, BITS_PER_UNIT)));
9785e4b1
MM
4875 /* Every virtual baseclass takes a least a UNIT, so that
4876 we can take it's address and get something different
4877 for each base. */
4878 dsize += MAX (BITS_PER_UNIT,
4879 tree_low_cst (CLASSTYPE_SIZE (basetype), 0));
4880 }
4881
4882 /* Keep track of the offsets assigned to this virtual base. */
4883 record_base_offsets (vbase, base_offsets);
80fd5f48
MM
4884 }
4885
9d4c0187
MM
4886 /* Make sure that all of the CLASSTYPE_VBASECLASSES have their
4887 BINFO_OFFSET set correctly. Those we just allocated certainly
4888 will. The others are primary baseclasses; we walk the hierarchy
4889 to find the primary copies and update the shared copy. */
4890 dfs_walk (TYPE_BINFO (t),
4891 dfs_set_offset_for_shared_vbases,
4892 dfs_unmarked_real_bases_queue_p,
4893 t);
4894
4895 /* Now, go through the TYPE_BINFO hierarchy again, setting the
4896 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4897 bases and their direct and indirect bases. The ambiguity checks
4898 in get_base_distance depend on the BINFO_OFFSETs being set
4899 correctly. */
4900 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
5e19c053
MM
4901 for (vbase = CLASSTYPE_VBASECLASSES (t);
4902 vbase;
4903 vbase = TREE_CHAIN (vbase))
4904 dfs_walk (vbase, dfs_set_offset_for_unshared_vbases, NULL, t);
9d4c0187 4905
9785e4b1
MM
4906 /* If we had empty base classes that protruded beyond the end of the
4907 class, we didn't update DSIZE above; we were hoping to overlay
4908 multiple such bases at the same location. */
4909 eoc = end_of_class (t, /*include_virtuals_p=*/1);
4910 if (eoc * BITS_PER_UNIT > dsize)
4911 dsize = (eoc + 1) * BITS_PER_UNIT;
4912
80fd5f48
MM
4913 /* Now, make sure that the total size of the type is a multiple of
4914 its alignment. */
4915 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
fed3cef0
RK
4916 TYPE_SIZE (t) = bitsize_int (dsize);
4917 TYPE_SIZE_UNIT (t) = convert (sizetype,
770ae6cc
RK
4918 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (t),
4919 bitsize_unit_node));
9785e4b1
MM
4920
4921 /* Check for ambiguous virtual bases. */
4922 if (extra_warnings)
4923 for (vbase = CLASSTYPE_VBASECLASSES (t);
4924 vbase;
4925 vbase = TREE_CHAIN (vbase))
4926 {
4927 tree basetype = BINFO_TYPE (vbase);
4928 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4929 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4930 basetype, t);
4931 }
80fd5f48
MM
4932}
4933
9785e4b1
MM
4934/* Returns the offset of the byte just past the end of the base class
4935 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4936 only non-virtual bases are included. */
80fd5f48 4937
9785e4b1
MM
4938static unsigned HOST_WIDE_INT
4939end_of_class (t, include_virtuals_p)
4940 tree t;
4941 int include_virtuals_p;
80fd5f48 4942{
9785e4b1
MM
4943 unsigned HOST_WIDE_INT result = 0;
4944 int i;
80fd5f48 4945
9785e4b1
MM
4946 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4947 {
4948 tree base_binfo;
4949 tree offset;
4950 unsigned HOST_WIDE_INT end_of_base;
5c24fba6 4951
9785e4b1
MM
4952 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4953
4954 if (!include_virtuals_p
4955 && TREE_VIA_VIRTUAL (base_binfo)
4956 && !BINFO_PRIMARY_MARKED_P (base_binfo))
4957 continue;
80fd5f48 4958
9785e4b1
MM
4959 offset = size_binop (PLUS_EXPR,
4960 BINFO_OFFSET (base_binfo),
4961 CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo)));
4962 end_of_base = tree_low_cst (offset, /*pos=*/1);
4963 if (end_of_base > result)
4964 result = end_of_base;
4965 }
80fd5f48 4966
9785e4b1 4967 return result;
80fd5f48
MM
4968}
4969
2ef16140
MM
4970/* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4971 BINFO_OFFSETs for all of the base-classes. Position the vtable
4972 pointer. */
607cf131 4973
2ef16140 4974static void
d2c5305b 4975layout_class_type (t, empty_p, has_virtual_p,
051e6fd7 4976 new_virtuals_p, overridden_virtuals_p)
2ef16140
MM
4977 tree t;
4978 int *empty_p;
4979 int *has_virtual_p;
051e6fd7
MM
4980 tree *new_virtuals_p;
4981 tree *overridden_virtuals_p;
2ef16140 4982{
5c24fba6
MM
4983 tree non_static_data_members;
4984 tree field;
4985 tree vptr;
4986 record_layout_info rli;
4987 varray_type v;
9785e4b1 4988 unsigned HOST_WIDE_INT eoc;
5c24fba6
MM
4989
4990 /* Keep track of the first non-static data member. */
4991 non_static_data_members = TYPE_FIELDS (t);
4992
770ae6cc
RK
4993 /* Start laying out the record. */
4994 rli = start_record_layout (t);
534170eb 4995
8026246f
MM
4996 /* If possible, we reuse the virtual function table pointer from one
4997 of our base classes. */
4998 determine_primary_base (t, has_virtual_p);
4999
5c24fba6
MM
5000 /* Create a pointer to our virtual function table. */
5001 vptr = create_vtable_ptr (t, empty_p, has_virtual_p,
5002 new_virtuals_p, overridden_virtuals_p);
5003
5004 /* Under the new ABI, the vptr is always the first thing in the
5005 class. */
5006 if (flag_new_abi && vptr)
5007 {
5008 TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
770ae6cc 5009 place_field (rli, vptr);
5c24fba6
MM
5010 }
5011
607cf131 5012 /* Add pointers to all of our virtual base-classes. */
5c24fba6 5013 TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (rli, empty_p),
607cf131
MM
5014 TYPE_FIELDS (t));
5015 /* Build FIELD_DECLs for all of the non-virtual base-types. */
5c24fba6 5016 v = build_base_fields (rli, empty_p);
8d08fdba 5017
c1aa4de7
MM
5018 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
5019 we have to save this before we start modifying
5020 TYPE_NONCOPIED_PARTS. */
2ef16140 5021 fixup_inline_methods (t);
c1aa4de7 5022
5c24fba6 5023 /* Layout the non-static data members. */
770ae6cc 5024 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
5c24fba6
MM
5025 {
5026 tree binfo;
01955e96
MM
5027 tree type;
5028 tree padding;
5c24fba6
MM
5029
5030 /* We still pass things that aren't non-static data members to
5031 the back-end, in case it wants to do something with them. */
5032 if (TREE_CODE (field) != FIELD_DECL)
5033 {
770ae6cc 5034 place_field (rli, field);
5c24fba6
MM
5035 continue;
5036 }
5037
01955e96
MM
5038 type = TREE_TYPE (field);
5039
5040 /* If this field is a bit-field whose width is greater than its
5041 type, then there are some special rules for allocating it
5042 under the new ABI. Under the old ABI, there were no special
5043 rules, but the back-end can't handle bitfields longer than a
5044 `long long', so we use the same mechanism. */
5045 if (DECL_C_BIT_FIELD (field)
5046 && ((flag_new_abi
5047 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
5048 || (!flag_new_abi
770ae6cc
RK
5049 && 0 < compare_tree_int (DECL_SIZE (field),
5050 TYPE_PRECISION
5051 (long_long_unsigned_type_node)))))
01955e96
MM
5052 {
5053 integer_type_kind itk;
5054 tree integer_type;
5055
5056 /* We must allocate the bits as if suitably aligned for the
5057 longest integer type that fits in this many bits. type
5058 of the field. Then, we are supposed to use the left over
5059 bits as additional padding. */
5060 for (itk = itk_char; itk != itk_none; ++itk)
5061 if (INT_CST_LT (DECL_SIZE (field),
5062 TYPE_SIZE (integer_types[itk])))
5063 break;
5064
5065 /* ITK now indicates a type that is too large for the
5066 field. We have to back up by one to find the largest
5067 type that fits. */
5068 integer_type = integer_types[itk - 1];
770ae6cc
RK
5069 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
5070 TYPE_SIZE (integer_type));
01955e96
MM
5071 DECL_SIZE (field) = TYPE_SIZE (integer_type);
5072 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
5073 }
5074 else
5075 padding = NULL_TREE;
5076
5c24fba6 5077 /* Create a dummy BINFO corresponding to this field. */
01955e96 5078 binfo = make_binfo (size_zero_node, type, NULL_TREE, NULL_TREE);
5c24fba6
MM
5079 unshare_base_binfos (binfo);
5080 layout_nonempty_base_or_field (rli, field, binfo, v);
01955e96
MM
5081
5082 /* If we needed additional padding after this field, add it
5083 now. */
5084 if (padding)
5085 {
5086 tree padding_field;
5087
5088 padding_field = build_decl (FIELD_DECL,
5089 NULL_TREE,
5090 char_type_node);
5091 DECL_BIT_FIELD (padding_field) = 1;
5092 DECL_SIZE (padding_field) = padding;
5093 DECL_ALIGN (padding_field) = 1;
5094 layout_nonempty_base_or_field (rli, padding_field, NULL_TREE, v);
5095 }
5c24fba6
MM
5096 }
5097
5c24fba6
MM
5098 /* It might be the case that we grew the class to allocate a
5099 zero-sized base class. That won't be reflected in RLI, yet,
5100 because we are willing to overlay multiple bases at the same
5101 offset. However, now we need to make sure that RLI is big enough
5102 to reflect the entire class. */
9785e4b1 5103 eoc = end_of_class (t, /*include_virtuals_p=*/0);
770ae6cc
RK
5104 if (TREE_CODE (rli_size_unit_so_far (rli)) == INTEGER_CST
5105 && compare_tree_int (rli_size_unit_so_far (rli), eoc) < 0)
5c24fba6 5106 {
9785e4b1
MM
5107 /* We don't handle zero-sized base classes specially under the
5108 old ABI, so if we get here, we had better be operating under
5109 the new ABI rules. */
5110 my_friendly_assert (flag_new_abi, 20000321);
770ae6cc
RK
5111 rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc + 1));
5112 rli->bitpos = bitsize_zero_node;
5c24fba6
MM
5113 }
5114
58010b57 5115 /* We make all structures have at least one element, so that they
5c24fba6
MM
5116 have non-zero size. In the new ABI, the class may be empty even
5117 if it has basetypes. Therefore, we add the fake field after all
5118 the other fields; if there are already FIELD_DECLs on the list,
5119 their offsets will not be disturbed. */
2ef16140 5120 if (*empty_p)
691c003d 5121 {
5c24fba6
MM
5122 tree padding;
5123
534170eb 5124 padding = build_lang_decl (FIELD_DECL, NULL_TREE, char_type_node);
770ae6cc 5125 place_field (rli, padding);
c1aa4de7 5126 TYPE_NONCOPIED_PARTS (t)
534170eb 5127 = tree_cons (NULL_TREE, padding, TYPE_NONCOPIED_PARTS (t));
c1aa4de7 5128 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
691c003d 5129 }
c1aa4de7 5130
5c24fba6
MM
5131 /* Under the old ABI, the vptr comes at the very end of the
5132 class. */
5133 if (!flag_new_abi && vptr)
5134 {
770ae6cc 5135 place_field (rli, vptr);
5c24fba6
MM
5136 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), vptr);
5137 }
5138
3ef397c1
MM
5139 /* Let the back-end lay out the type. Note that at this point we
5140 have only included non-virtual base-classes; we will lay out the
5141 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
5142 this call are not necessarily correct; they are just the size and
5143 alignment when no virtual base clases are used. */
5c24fba6 5144 finish_record_layout (rli);
58010b57 5145
3ef397c1
MM
5146 /* Delete all zero-width bit-fields from the list of fields. Now
5147 that the type is laid out they are no longer important. */
5148 remove_zero_width_bit_fields (t);
5149
9a71c18b 5150 /* Remember the size and alignment of the class before adding
0b41abe6 5151 the virtual bases. */
2ef16140 5152 if (*empty_p && flag_new_abi)
06ceef4e 5153 {
770ae6cc 5154 CLASSTYPE_SIZE (t) = bitsize_zero_node;
fed3cef0 5155 CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
06ceef4e 5156 }
6bc39009
JM
5157 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
5158 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
06ceef4e
RK
5159 {
5160 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
5161 CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
5162 }
732dcb6f 5163 else
06ceef4e
RK
5164 {
5165 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
5166 CLASSTYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (t);
5167 }
5168
0b41abe6
JM
5169 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
5170
8d08fdba
MS
5171 /* Set the TYPE_DECL for this type to contain the right
5172 value for DECL_OFFSET, so that we can use it as part
5173 of a COMPONENT_REF for multiple inheritance. */
d2e5ee5c 5174 layout_decl (TYPE_MAIN_DECL (t), 0);
8d08fdba 5175
7177d104
MS
5176 /* Now fix up any virtual base class types that we left lying
5177 around. We must get these done before we try to lay out the
5c24fba6
MM
5178 virtual function table. As a side-effect, this will remove the
5179 base subobject fields. */
9785e4b1
MM
5180 layout_virtual_bases (t, &v);
5181
5182 /* Clean up. */
5183 VARRAY_FREE (v);
2ef16140
MM
5184}
5185
5186/* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
5187 (or C++ class declaration).
5188
5189 For C++, we must handle the building of derived classes.
5190 Also, C++ allows static class members. The way that this is
5191 handled is to keep the field name where it is (as the DECL_NAME
665f2503 5192 of the field), and place the overloaded decl in the bit position
2ef16140
MM
5193 of the field. layout_record and layout_union will know about this.
5194
5195 More C++ hair: inline functions have text in their
5196 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
5197 meaningful tree structure. After the struct has been laid out, set
5198 things up so that this can happen.
5199
5200 And still more: virtual functions. In the case of single inheritance,
5201 when a new virtual function is seen which redefines a virtual function
5202 from the base class, the new virtual function is placed into
5203 the virtual function table at exactly the same address that
5204 it had in the base class. When this is extended to multiple
5205 inheritance, the same thing happens, except that multiple virtual
5206 function tables must be maintained. The first virtual function
5207 table is treated in exactly the same way as in the case of single
5208 inheritance. Additional virtual function tables have different
5209 DELTAs, which tell how to adjust `this' to point to the right thing.
5210
5211 ATTRIBUTES is the set of decl attributes to be applied, if any. */
5212
5213void
5214finish_struct_1 (t)
5215 tree t;
5216{
5217 tree x;
5218 int has_virtual;
051e6fd7
MM
5219 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
5220 a FUNCTION_DECL. Each of these functions is a virtual function
5221 declared in T that does not override any virtual function from a
5222 base class. */
5223 tree new_virtuals = NULL_TREE;
5224 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5225 except that each declaration here overrides the declaration from
5226 a base class. */
5227 tree overridden_virtuals = NULL_TREE;
2ef16140
MM
5228 int n_fields = 0;
5229 tree vfield;
2ef16140
MM
5230 int empty = 1;
5231
d0f062fb 5232 if (COMPLETE_TYPE_P (t))
2ef16140
MM
5233 {
5234 if (IS_AGGR_TYPE (t))
5235 cp_error ("redefinition of `%#T'", t);
5236 else
5237 my_friendly_abort (172);
5238 popclass ();
5239 return;
5240 }
5241
5242 GNU_xref_decl (current_function_decl, t);
5243
5244 /* If this type was previously laid out as a forward reference,
5245 make sure we lay it out again. */
2ef16140
MM
5246 TYPE_SIZE (t) = NULL_TREE;
5247 CLASSTYPE_GOT_SEMICOLON (t) = 0;
2ef16140
MM
5248 CLASSTYPE_VFIELD_PARENT (t) = -1;
5249 has_virtual = 0;
2ef16140 5250 CLASSTYPE_RTTI (t) = NULL_TREE;
2ef16140
MM
5251
5252 /* Do end-of-class semantic processing: checking the validity of the
03702748 5253 bases and members and add implicitly generated methods. */
2ef16140
MM
5254 check_bases_and_members (t, &empty);
5255
5256 /* Layout the class itself. */
d2c5305b 5257 layout_class_type (t, &empty, &has_virtual,
051e6fd7 5258 &new_virtuals, &overridden_virtuals);
8ebeee52 5259
2986ae00
MS
5260 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
5261 might need to know it for setting up the offsets in the vtable
5262 (or in thunks) below. */
3ef397c1 5263 vfield = TYPE_VFIELD (t);
2986ae00
MS
5264 if (vfield != NULL_TREE
5265 && DECL_FIELD_CONTEXT (vfield) != t)
5266 {
ec386958
MM
5267 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
5268
2986ae00
MS
5269 vfield = copy_node (vfield);
5270 copy_lang_decl (vfield);
5271
2986ae00 5272 DECL_FIELD_CONTEXT (vfield) = t;
770ae6cc
RK
5273 DECL_FIELD_OFFSET (vfield)
5274 = size_binop (PLUS_EXPR,
ec386958 5275 BINFO_OFFSET (binfo),
770ae6cc 5276 DECL_FIELD_OFFSET (vfield));
d3a3fb6a 5277 TYPE_VFIELD (t) = vfield;
2986ae00 5278 }
4c6b7393 5279
a68ad5bd
MM
5280 overridden_virtuals
5281 = modify_all_vtables (t, &has_virtual, nreverse (overridden_virtuals));
db5ae43f 5282
5e19c053 5283 /* If necessary, create the primary vtable for this class. */
051e6fd7 5284 if (new_virtuals
a68ad5bd 5285 || overridden_virtuals
bbd15aac 5286 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ()))
8d08fdba 5287 {
051e6fd7 5288 new_virtuals = nreverse (new_virtuals);
8d08fdba 5289 /* We must enter these virtuals into the table. */
3ef397c1 5290 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
8d08fdba 5291 {
aff08c18
JM
5292 if (! CLASSTYPE_COM_INTERFACE (t))
5293 {
a68ad5bd
MM
5294 /* The second slot is for the tdesc pointer when thunks
5295 are used. */
aff08c18 5296 if (flag_vtable_thunks)
051e6fd7 5297 new_virtuals = tree_cons (NULL_TREE, NULL_TREE, new_virtuals);
f30432d7 5298
aff08c18 5299 /* The first slot is for the rtti offset. */
051e6fd7 5300 new_virtuals = tree_cons (NULL_TREE, NULL_TREE, new_virtuals);
6b5fbb55 5301
051e6fd7 5302 set_rtti_entry (new_virtuals,
aff08c18
JM
5303 convert (ssizetype, integer_zero_node), t);
5304 }
28531dd0 5305 build_primary_vtable (NULL_TREE, t);
8d08fdba 5306 }
3c9d6359 5307 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
0533d788
MM
5308 /* Here we know enough to change the type of our virtual
5309 function table, but we will wait until later this function. */
28531dd0 5310 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
8d08fdba
MS
5311
5312 /* If this type has basetypes with constructors, then those
5313 constructors might clobber the virtual function table. But
5314 they don't if the derived class shares the exact vtable of the base
5315 class. */
5316
5317 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5318 }
bbd15aac
MM
5319 /* If we didn't need a new vtable, see if we should copy one from
5320 the base. */
3ef397c1 5321 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
8d08fdba 5322 {
3ef397c1
MM
5323 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5324
8d08fdba
MS
5325 /* This class contributes nothing new to the virtual function
5326 table. However, it may have declared functions which
5327 went into the virtual function table "inherited" from the
5328 base class. If so, we grab a copy of those updated functions,
5329 and pretend they are ours. */
5330
5331 /* See if we should steal the virtual info from base class. */
5332 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
5333 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
5334 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
5335 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
5336 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5337 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5338 }
5339
bbd15aac 5340 if (TYPE_CONTAINS_VPTR_P (t))
8d08fdba 5341 {
1eb4bea9
MM
5342 if (TYPE_BINFO_VTABLE (t))
5343 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5344 20000116);
5345 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5346 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5347 20000116);
5348
8d08fdba 5349 CLASSTYPE_VSIZE (t) = has_virtual;
1eb4bea9
MM
5350 /* Entries for virtual functions defined in the primary base are
5351 followed by entries for new functions unique to this class. */
5352 TYPE_BINFO_VIRTUALS (t)
051e6fd7 5353 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
a68ad5bd
MM
5354 /* Finally, add entries for functions that override virtuals
5355 from non-primary bases. */
5356 TYPE_BINFO_VIRTUALS (t)
5357 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
8d08fdba
MS
5358 }
5359
3ef397c1
MM
5360 /* If we created a new vtbl pointer for this class, add it to the
5361 list. */
5362 if (TYPE_VFIELD (t) && CLASSTYPE_VFIELD_PARENT (t) == -1)
5363 CLASSTYPE_VFIELDS (t)
5364 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
8d08fdba 5365
d2c5305b 5366 finish_struct_bits (t);
8d08fdba 5367
f30432d7
MS
5368 /* Complete the rtl for any static member objects of the type we're
5369 working on. */
58010b57 5370 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
8d08fdba 5371 {
8d08fdba
MS
5372 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5373 && TREE_TYPE (x) == t)
5374 {
5375 DECL_MODE (x) = TYPE_MODE (t);
5376 make_decl_rtl (x, NULL, 0);
5377 }
5378 }
5379
f90cdf34 5380 /* Done with FIELDS...now decide whether to sort these for
58010b57 5381 faster lookups later.
f90cdf34
MT
5382
5383 The C front-end only does this when n_fields > 15. We use
5384 a smaller number because most searches fail (succeeding
5385 ultimately as the search bores through the inheritance
5386 hierarchy), and we want this failure to occur quickly. */
5387
58010b57
MM
5388 n_fields = count_fields (TYPE_FIELDS (t));
5389 if (n_fields > 7)
f90cdf34
MT
5390 {
5391 tree field_vec = make_tree_vec (n_fields);
58010b57 5392 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
f90cdf34
MT
5393 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5394 (int (*)(const void *, const void *))field_decl_cmp);
5395 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5396 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5397 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5398 }
5399
8d08fdba
MS
5400 if (TYPE_HAS_CONSTRUCTOR (t))
5401 {
5402 tree vfields = CLASSTYPE_VFIELDS (t);
5403
5404 while (vfields)
5405 {
5406 /* Mark the fact that constructor for T
5407 could affect anybody inheriting from T
5408 who wants to initialize vtables for VFIELDS's type. */
5409 if (VF_DERIVED_VALUE (vfields))
5410 TREE_ADDRESSABLE (vfields) = 1;
5411 vfields = TREE_CHAIN (vfields);
5412 }
8d08fdba 5413 }
8d08fdba 5414
8d7a5379
MM
5415 /* Make the rtl for any new vtables we have created, and unmark
5416 the base types we marked. */
5417 finish_vtbls (t);
5418
8d08fdba
MS
5419 if (CLASSTYPE_VSIZE (t) != 0)
5420 {
e92cc029 5421 /* In addition to this one, all the other vfields should be listed. */
8d08fdba
MS
5422 /* Before that can be done, we have to have FIELD_DECLs for them, and
5423 a place to find them. */
c1aa4de7
MM
5424 TYPE_NONCOPIED_PARTS (t)
5425 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
3ef397c1 5426 TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
8d08fdba
MS
5427
5428 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
58010b57 5429 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
8251199e 5430 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
8d08fdba
MS
5431 t);
5432 }
5433
8145f082 5434 hack_incomplete_structures (t);
8d08fdba 5435
9e9ff709
MS
5436 if (warn_overloaded_virtual)
5437 warn_hidden (t);
8d08fdba 5438
ae673f14 5439 maybe_suppress_debug_info (t);
8d08fdba 5440
d2e5ee5c
MS
5441 /* Finish debugging output for this type. */
5442 rest_of_type_compilation (t, toplevel_bindings_p ());
8d08fdba 5443}
f30432d7 5444
61a127b3
MM
5445/* When T was built up, the member declarations were added in reverse
5446 order. Rearrange them to declaration order. */
5447
5448void
5449unreverse_member_declarations (t)
5450 tree t;
5451{
5452 tree next;
5453 tree prev;
5454 tree x;
5455
5456 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5457 reverse order. Put them in declaration order now. */
5458 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5459 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5460
5461 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5462 reverse order, so we can't just use nreverse. */
5463 prev = NULL_TREE;
5464 for (x = TYPE_FIELDS (t);
5465 x && TREE_CODE (x) != TYPE_DECL;
5466 x = next)
5467 {
5468 next = TREE_CHAIN (x);
5469 TREE_CHAIN (x) = prev;
5470 prev = x;
5471 }
5472 if (prev)
5473 {
5474 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5475 if (prev)
5476 TYPE_FIELDS (t) = prev;
5477 }
5478}
5479
f30432d7 5480tree
9f33663b 5481finish_struct (t, attributes)
61a127b3 5482 tree t, attributes;
f30432d7 5483{
61a127b3
MM
5484 /* Now that we've got all the field declarations, reverse everything
5485 as necessary. */
5486 unreverse_member_declarations (t);
f30432d7 5487
6467930b
MS
5488 cplus_decl_attributes (t, attributes, NULL_TREE);
5489
5566b478 5490 if (processing_template_decl)
f30432d7 5491 {
b0e0b31f 5492 finish_struct_methods (t);
5566b478 5493 TYPE_SIZE (t) = integer_zero_node;
6f1b4c42 5494 }
f30432d7 5495 else
9f33663b 5496 finish_struct_1 (t);
5566b478
MS
5497
5498 TYPE_BEING_DEFINED (t) = 0;
8f032717 5499
5566b478 5500 if (current_class_type)
b74a0560 5501 popclass ();
5566b478 5502 else
8251199e 5503 error ("trying to finish struct, but kicked out due to previous parse errors.");
5566b478 5504
ae673f14
JM
5505 if (processing_template_decl)
5506 {
5507 tree scope = current_scope ();
5508 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5509 add_tree (build_min (TAG_DEFN, t));
5510 }
5511
5566b478 5512 return t;
f30432d7 5513}
8d08fdba 5514\f
51ddb82e 5515/* Return the dynamic type of INSTANCE, if known.
8d08fdba
MS
5516 Used to determine whether the virtual function table is needed
5517 or not.
5518
5519 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
97d953bb
MM
5520 of our knowledge of its type. *NONNULL should be initialized
5521 before this function is called. */
e92cc029 5522
d8e178a0 5523static tree
51ddb82e 5524fixed_type_or_null (instance, nonnull)
8d08fdba
MS
5525 tree instance;
5526 int *nonnull;
5527{
5528 switch (TREE_CODE (instance))
5529 {
5530 case INDIRECT_REF:
5531 /* Check that we are not going through a cast of some sort. */
5532 if (TREE_TYPE (instance)
5533 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5534 instance = TREE_OPERAND (instance, 0);
5535 /* fall through... */
5536 case CALL_EXPR:
5537 /* This is a call to a constructor, hence it's never zero. */
5538 if (TREE_HAS_CONSTRUCTOR (instance))
5539 {
5540 if (nonnull)
5541 *nonnull = 1;
51ddb82e 5542 return TREE_TYPE (instance);
8d08fdba 5543 }
51ddb82e 5544 return NULL_TREE;
8d08fdba
MS
5545
5546 case SAVE_EXPR:
5547 /* This is a call to a constructor, hence it's never zero. */
5548 if (TREE_HAS_CONSTRUCTOR (instance))
5549 {
5550 if (nonnull)
5551 *nonnull = 1;
51ddb82e 5552 return TREE_TYPE (instance);
8d08fdba 5553 }
51ddb82e 5554 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
8d08fdba
MS
5555
5556 case RTL_EXPR:
51ddb82e 5557 return NULL_TREE;
8d08fdba
MS
5558
5559 case PLUS_EXPR:
5560 case MINUS_EXPR:
5561 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5562 /* Propagate nonnull. */
51ddb82e 5563 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
8d08fdba 5564 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
51ddb82e
JM
5565 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5566 return NULL_TREE;
8d08fdba
MS
5567
5568 case NOP_EXPR:
5569 case CONVERT_EXPR:
51ddb82e 5570 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
8d08fdba
MS
5571
5572 case ADDR_EXPR:
5573 if (nonnull)
5574 *nonnull = 1;
51ddb82e 5575 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
8d08fdba
MS
5576
5577 case COMPONENT_REF:
51ddb82e 5578 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
8d08fdba 5579
8d08fdba
MS
5580 case VAR_DECL:
5581 case FIELD_DECL:
5582 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5583 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5584 {
5585 if (nonnull)
5586 *nonnull = 1;
51ddb82e 5587 return TREE_TYPE (TREE_TYPE (instance));
8d08fdba 5588 }
e92cc029 5589 /* fall through... */
8d08fdba
MS
5590 case TARGET_EXPR:
5591 case PARM_DECL:
5592 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5593 {
5594 if (nonnull)
5595 *nonnull = 1;
51ddb82e 5596 return TREE_TYPE (instance);
8d08fdba
MS
5597 }
5598 else if (nonnull)
5599 {
4ac14744 5600 if (instance == current_class_ptr
8d08fdba
MS
5601 && flag_this_is_variable <= 0)
5602 {
51ddb82e
JM
5603 /* Normally, 'this' must be non-null. */
5604 if (flag_this_is_variable == 0)
5605 *nonnull = 1;
5606
5607 /* <0 means we're in a constructor and we know our type. */
8d08fdba 5608 if (flag_this_is_variable < 0)
51ddb82e 5609 return TREE_TYPE (TREE_TYPE (instance));
8d08fdba
MS
5610 }
5611 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5612 /* Reference variables should be references to objects. */
5613 *nonnull = 1;
5614 }
51ddb82e 5615 return NULL_TREE;
8d08fdba
MS
5616
5617 default:
51ddb82e 5618 return NULL_TREE;
8d08fdba
MS
5619 }
5620}
51ddb82e
JM
5621
5622/* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5623 to the static type. We also handle the case where INSTANCE is really
5624 a pointer.
5625
5626 Used to determine whether the virtual function table is needed
5627 or not.
5628
5629 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
97d953bb
MM
5630 of our knowledge of its type. *NONNULL should be initialized
5631 before this function is called. */
51ddb82e
JM
5632
5633int
5634resolves_to_fixed_type_p (instance, nonnull)
5635 tree instance;
5636 int *nonnull;
5637{
5638 tree t = TREE_TYPE (instance);
5639 tree fixed = fixed_type_or_null (instance, nonnull);
5640 if (fixed == NULL_TREE)
5641 return 0;
5642 if (POINTER_TYPE_P (t))
5643 t = TREE_TYPE (t);
3bfdc719 5644 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
51ddb82e
JM
5645}
5646
8d08fdba
MS
5647\f
5648void
5649init_class_processing ()
5650{
5651 current_class_depth = 0;
61a127b3
MM
5652 current_class_stack_size = 10;
5653 current_class_stack
5654 = (class_stack_node_t) xmalloc (current_class_stack_size
5655 * sizeof (struct class_stack_node));
8d08fdba 5656
be99da77
MS
5657 access_default_node = build_int_2 (0, 0);
5658 access_public_node = build_int_2 (1, 0);
5659 access_protected_node = build_int_2 (2, 0);
5660 access_private_node = build_int_2 (3, 0);
5661 access_default_virtual_node = build_int_2 (4, 0);
5662 access_public_virtual_node = build_int_2 (5, 0);
d8b55a76
JM
5663 access_protected_virtual_node = build_int_2 (6, 0);
5664 access_private_virtual_node = build_int_2 (7, 0);
8d08fdba
MS
5665}
5666
5667/* Set current scope to NAME. CODE tells us if this is a
5668 STRUCT, UNION, or ENUM environment.
5669
5670 NAME may end up being NULL_TREE if this is an anonymous or
5671 late-bound struct (as in "struct { ... } foo;") */
5672
5673/* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5674 appropriate values, found by looking up the type definition of
5675 NAME (as a CODE).
5676
5677 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5678 which can be seen locally to the class. They are shadowed by
5679 any subsequent local declaration (including parameter names).
5680
5681 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5682 which have static meaning (i.e., static members, static
5683 member functions, enum declarations, etc).
5684
5685 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5686 which can be seen locally to the class (as in 1), but
5687 know that we are doing this for declaration purposes
5688 (i.e. friend foo::bar (int)).
5689
5690 So that we may avoid calls to lookup_name, we cache the _TYPE
5691 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5692
5693 For multiple inheritance, we perform a two-pass depth-first search
5694 of the type lattice. The first pass performs a pre-order search,
5695 marking types after the type has had its fields installed in
5696 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5697 unmarks the marked types. If a field or member function name
5698 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5699 that name becomes `error_mark_node'. */
5700
5701void
5702pushclass (type, modify)
5703 tree type;
5704 int modify;
5705{
7fb4a8f7 5706 type = TYPE_MAIN_VARIANT (type);
8d08fdba 5707
61a127b3
MM
5708 /* Make sure there is enough room for the new entry on the stack. */
5709 if (current_class_depth + 1 >= current_class_stack_size)
8d08fdba 5710 {
61a127b3
MM
5711 current_class_stack_size *= 2;
5712 current_class_stack
5713 = (class_stack_node_t) xrealloc (current_class_stack,
5714 current_class_stack_size
5715 * sizeof (struct class_stack_node));
8d08fdba
MS
5716 }
5717
61a127b3
MM
5718 /* Insert a new entry on the class stack. */
5719 current_class_stack[current_class_depth].name = current_class_name;
5720 current_class_stack[current_class_depth].type = current_class_type;
5721 current_class_stack[current_class_depth].access = current_access_specifier;
8f032717 5722 current_class_stack[current_class_depth].names_used = 0;
61a127b3
MM
5723 current_class_depth++;
5724
5725 /* Now set up the new type. */
8d08fdba
MS
5726 current_class_name = TYPE_NAME (type);
5727 if (TREE_CODE (current_class_name) == TYPE_DECL)
5728 current_class_name = DECL_NAME (current_class_name);
5729 current_class_type = type;
5730
61a127b3
MM
5731 /* By default, things in classes are private, while things in
5732 structures or unions are public. */
5733 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5734 ? access_private_node
5735 : access_public_node);
5736
8d08fdba 5737 if (previous_class_type != NULL_TREE
8f032717 5738 && (type != previous_class_type
d0f062fb 5739 || !COMPLETE_TYPE_P (previous_class_type))
8d08fdba
MS
5740 && current_class_depth == 1)
5741 {
5742 /* Forcibly remove any old class remnants. */
8f032717 5743 invalidate_class_lookup_cache ();
8d08fdba
MS
5744 }
5745
8f032717
MM
5746 /* If we're about to enter a nested class, clear
5747 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5748 if (modify && current_class_depth > 1)
5749 clear_identifier_class_values ();
5750
8d08fdba
MS
5751 pushlevel_class ();
5752
37c46b43 5753#if 0
5566b478
MS
5754 if (CLASSTYPE_TEMPLATE_INFO (type))
5755 overload_template_name (type);
37c46b43 5756#endif
5566b478 5757
8d08fdba
MS
5758 if (modify)
5759 {
5566b478 5760 if (type != previous_class_type || current_class_depth > 1)
8f032717 5761 push_class_decls (type);
8d08fdba
MS
5762 else
5763 {
5764 tree item;
5765
f181d4ae
MM
5766 /* We are re-entering the same class we just left, so we
5767 don't have to search the whole inheritance matrix to find
5768 all the decls to bind again. Instead, we install the
5769 cached class_shadowed list, and walk through it binding
5770 names and setting up IDENTIFIER_TYPE_VALUEs. */
8d08fdba
MS
5771 set_class_shadows (previous_class_values);
5772 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5773 {
5774 tree id = TREE_PURPOSE (item);
d8f8dca1 5775 tree decl = TREE_TYPE (item);
8d08fdba 5776
f181d4ae 5777 push_class_binding (id, decl);
8d08fdba
MS
5778 if (TREE_CODE (decl) == TYPE_DECL)
5779 set_identifier_type_value (id, TREE_TYPE (decl));
5780 }
5781 unuse_fields (type);
5782 }
5783
280f9385 5784 storetags (CLASSTYPE_TAGS (type));
8f032717
MM
5785 }
5786}
5787
5788/* When we exit a toplevel class scope, we save the
5789 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5790 reenter the class. Here, we've entered some other class, so we
5791 must invalidate our cache. */
8d08fdba 5792
8f032717
MM
5793void
5794invalidate_class_lookup_cache ()
5795{
8f032717
MM
5796 tree t;
5797
5798 /* This code can be seen as a cache miss. When we've cached a
5799 class' scope's bindings and we can't use them, we need to reset
5800 them. This is it! */
5801 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5802 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
8f032717
MM
5803
5804 previous_class_type = NULL_TREE;
8d08fdba
MS
5805}
5806
5807/* Get out of the current class scope. If we were in a class scope
b74a0560 5808 previously, that is the one popped to. */
e92cc029 5809
8d08fdba 5810void
b74a0560 5811popclass ()
8d08fdba 5812{
273a708f 5813 poplevel_class ();
8d08fdba 5814 /* Since poplevel_class does the popping of class decls nowadays,
b74a0560
MM
5815 this really only frees the obstack used for these decls. */
5816 pop_class_decls ();
8d08fdba
MS
5817
5818 current_class_depth--;
61a127b3
MM
5819 current_class_name = current_class_stack[current_class_depth].name;
5820 current_class_type = current_class_stack[current_class_depth].type;
5821 current_access_specifier = current_class_stack[current_class_depth].access;
8f032717
MM
5822 if (current_class_stack[current_class_depth].names_used)
5823 splay_tree_delete (current_class_stack[current_class_depth].names_used);
8d08fdba
MS
5824}
5825
70adf8a9
JM
5826/* Returns 1 if current_class_type is either T or a nested type of T.
5827 We start looking from 1 because entry 0 is from global scope, and has
5828 no type. */
b9082e8a
JM
5829
5830int
5831currently_open_class (t)
5832 tree t;
5833{
5834 int i;
5835 if (t == current_class_type)
5836 return 1;
70adf8a9 5837 for (i = 1; i < current_class_depth; ++i)
61a127b3 5838 if (current_class_stack [i].type == t)
b9082e8a
JM
5839 return 1;
5840 return 0;
5841}
5842
70adf8a9
JM
5843/* If either current_class_type or one of its enclosing classes are derived
5844 from T, return the appropriate type. Used to determine how we found
5845 something via unqualified lookup. */
5846
5847tree
5848currently_open_derived_class (t)
5849 tree t;
5850{
5851 int i;
5852
5853 if (DERIVED_FROM_P (t, current_class_type))
5854 return current_class_type;
5855
5856 for (i = current_class_depth - 1; i > 0; --i)
5857 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5858 return current_class_stack[i].type;
5859
5860 return NULL_TREE;
5861}
5862
8d08fdba
MS
5863/* When entering a class scope, all enclosing class scopes' names with
5864 static meaning (static variables, static functions, types and enumerators)
5865 have to be visible. This recursive function calls pushclass for all
5866 enclosing class contexts until global or a local scope is reached.
5867 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5868 formal of the same name. */
5869
5870void
5871push_nested_class (type, modify)
5872 tree type;
5873 int modify;
5874{
a28e3c7f
MS
5875 tree context;
5876
b262d64c 5877 /* A namespace might be passed in error cases, like A::B:C. */
07c88314
MM
5878 if (type == NULL_TREE
5879 || type == error_mark_node
b262d64c 5880 || TREE_CODE (type) == NAMESPACE_DECL
07c88314 5881 || ! IS_AGGR_TYPE (type)
73b0fce8
KL
5882 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5883 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
a28e3c7f
MS
5884 return;
5885
d2e5ee5c 5886 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
8d08fdba 5887
6b400b21 5888 if (context && CLASS_TYPE_P (context))
8d08fdba
MS
5889 push_nested_class (context, 2);
5890 pushclass (type, modify);
5891}
5892
5893/* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5894
5895void
b74a0560 5896pop_nested_class ()
8d08fdba 5897{
d2e5ee5c 5898 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8d08fdba 5899
b74a0560 5900 popclass ();
6b400b21 5901 if (context && CLASS_TYPE_P (context))
b74a0560 5902 pop_nested_class ();
8d08fdba
MS
5903}
5904
5905/* Set global variables CURRENT_LANG_NAME to appropriate value
5906 so that behavior of name-mangling machinery is correct. */
5907
5908void
5909push_lang_context (name)
5910 tree name;
5911{
5912 *current_lang_stack++ = current_lang_name;
9cd64686
MM
5913 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
5914 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
8d08fdba 5915 {
9cd64686
MM
5916 size_t old_size = VARRAY_SIZE (current_lang_base);
5917
5918 VARRAY_GROW (current_lang_base, old_size + 10);
5919 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
8d08fdba
MS
5920 }
5921
e229f2cd 5922 if (name == lang_name_cplusplus)
8d08fdba
MS
5923 {
5924 strict_prototype = strict_prototypes_lang_cplusplus;
5925 current_lang_name = name;
5926 }
e229f2cd
PB
5927 else if (name == lang_name_java)
5928 {
5929 strict_prototype = strict_prototypes_lang_cplusplus;
5930 current_lang_name = name;
5931 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5932 (See record_builtin_java_type in decl.c.) However, that causes
5933 incorrect debug entries if these types are actually used.
5934 So we re-enable debug output after extern "Java". */
5935 DECL_IGNORED_P (java_byte_type_node) = 0;
5936 DECL_IGNORED_P (java_short_type_node) = 0;
5937 DECL_IGNORED_P (java_int_type_node) = 0;
5938 DECL_IGNORED_P (java_long_type_node) = 0;
5939 DECL_IGNORED_P (java_float_type_node) = 0;
5940 DECL_IGNORED_P (java_double_type_node) = 0;
5941 DECL_IGNORED_P (java_char_type_node) = 0;
5942 DECL_IGNORED_P (java_boolean_type_node) = 0;
5943 }
8d08fdba
MS
5944 else if (name == lang_name_c)
5945 {
5946 strict_prototype = strict_prototypes_lang_c;
5947 current_lang_name = name;
5948 }
5949 else
8251199e 5950 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
8d08fdba
MS
5951}
5952
5953/* Get out of the current language scope. */
e92cc029 5954
8d08fdba
MS
5955void
5956pop_lang_context ()
5957{
9cd64686
MM
5958 /* Clear the current entry so that garbage collector won't hold on
5959 to it. */
5960 *current_lang_stack = NULL_TREE;
8d08fdba 5961 current_lang_name = *--current_lang_stack;
eff71ab0
PB
5962 if (current_lang_name == lang_name_cplusplus
5963 || current_lang_name == lang_name_java)
8d08fdba
MS
5964 strict_prototype = strict_prototypes_lang_cplusplus;
5965 else if (current_lang_name == lang_name_c)
5966 strict_prototype = strict_prototypes_lang_c;
5967}
8d08fdba
MS
5968\f
5969/* Type instantiation routines. */
5970
104bf76a
MM
5971/* Given an OVERLOAD and a TARGET_TYPE, return the function that
5972 matches the TARGET_TYPE. If there is no satisfactory match, return
5973 error_mark_node, and issue an error message if COMPLAIN is
5974 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
5975 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5976 template arguments. */
5977
2c73f9f5 5978static tree
104bf76a
MM
5979resolve_address_of_overloaded_function (target_type,
5980 overload,
5981 complain,
5982 template_only,
5983 explicit_targs)
5984 tree target_type;
5985 tree overload;
2c73f9f5 5986 int complain;
104bf76a
MM
5987 int template_only;
5988 tree explicit_targs;
2c73f9f5 5989{
104bf76a
MM
5990 /* Here's what the standard says:
5991
5992 [over.over]
5993
5994 If the name is a function template, template argument deduction
5995 is done, and if the argument deduction succeeds, the deduced
5996 arguments are used to generate a single template function, which
5997 is added to the set of overloaded functions considered.
5998
5999 Non-member functions and static member functions match targets of
6000 type "pointer-to-function" or "reference-to-function." Nonstatic
6001 member functions match targets of type "pointer-to-member
6002 function;" the function type of the pointer to member is used to
6003 select the member function from the set of overloaded member
6004 functions. If a nonstatic member function is selected, the
6005 reference to the overloaded function name is required to have the
6006 form of a pointer to member as described in 5.3.1.
6007
6008 If more than one function is selected, any template functions in
6009 the set are eliminated if the set also contains a non-template
6010 function, and any given template function is eliminated if the
6011 set contains a second template function that is more specialized
6012 than the first according to the partial ordering rules 14.5.5.2.
6013 After such eliminations, if any, there shall remain exactly one
6014 selected function. */
6015
6016 int is_ptrmem = 0;
6017 int is_reference = 0;
6018 /* We store the matches in a TREE_LIST rooted here. The functions
6019 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
6020 interoperability with most_specialized_instantiation. */
6021 tree matches = NULL_TREE;
50714e79 6022 tree fn;
104bf76a 6023
d8f8dca1
MM
6024 /* By the time we get here, we should be seeing only real
6025 pointer-to-member types, not the internal POINTER_TYPE to
6026 METHOD_TYPE representation. */
6027 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
6028 && (TREE_CODE (TREE_TYPE (target_type))
6029 == METHOD_TYPE)), 0);
104bf76a
MM
6030
6031 /* Check that the TARGET_TYPE is reasonable. */
6032 if (TYPE_PTRFN_P (target_type))
6033 /* This is OK. */
6034 ;
6035 else if (TYPE_PTRMEMFUNC_P (target_type))
6036 /* This is OK, too. */
6037 is_ptrmem = 1;
6038 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
6039 {
6040 /* This is OK, too. This comes from a conversion to reference
6041 type. */
6042 target_type = build_reference_type (target_type);
6043 is_reference = 1;
6044 }
6045 else
6046 {
6047 if (complain)
6048 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
6049 DECL_NAME (OVL_FUNCTION (overload)), target_type);
6050 return error_mark_node;
6051 }
6052
6053 /* If we can find a non-template function that matches, we can just
6054 use it. There's no point in generating template instantiations
6055 if we're just going to throw them out anyhow. But, of course, we
6056 can only do this when we don't *need* a template function. */
6057 if (!template_only)
6058 {
6059 tree fns;
6060
6061 for (fns = overload; fns; fns = OVL_CHAIN (fns))
6062 {
6063 tree fn = OVL_FUNCTION (fns);
6064 tree fntype;
2c73f9f5 6065
104bf76a
MM
6066 if (TREE_CODE (fn) == TEMPLATE_DECL)
6067 /* We're not looking for templates just yet. */
6068 continue;
6069
6070 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6071 != is_ptrmem)
6072 /* We're looking for a non-static member, and this isn't
6073 one, or vice versa. */
6074 continue;
6075
6076 /* See if there's a match. */
6077 fntype = TREE_TYPE (fn);
6078 if (is_ptrmem)
6079 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
6080 else if (!is_reference)
6081 fntype = build_pointer_type (fntype);
6082
6083 if (can_convert_arg (target_type, fntype, fn))
e1b3e07d 6084 matches = tree_cons (fn, NULL_TREE, matches);
104bf76a
MM
6085 }
6086 }
6087
6088 /* Now, if we've already got a match (or matches), there's no need
6089 to proceed to the template functions. But, if we don't have a
6090 match we need to look at them, too. */
6091 if (!matches)
2c73f9f5 6092 {
104bf76a
MM
6093 tree target_fn_type;
6094 tree target_arg_types;
8d3631f8 6095 tree target_ret_type;
104bf76a
MM
6096 tree fns;
6097
6098 if (is_ptrmem)
4393e105
MM
6099 target_fn_type
6100 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
2c73f9f5 6101 else
4393e105
MM
6102 target_fn_type = TREE_TYPE (target_type);
6103 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8d3631f8 6104 target_ret_type = TREE_TYPE (target_fn_type);
4393e105 6105
104bf76a
MM
6106 for (fns = overload; fns; fns = OVL_CHAIN (fns))
6107 {
6108 tree fn = OVL_FUNCTION (fns);
104bf76a
MM
6109 tree instantiation;
6110 tree instantiation_type;
6111 tree targs;
6112
6113 if (TREE_CODE (fn) != TEMPLATE_DECL)
6114 /* We're only looking for templates. */
6115 continue;
6116
6117 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6118 != is_ptrmem)
4393e105 6119 /* We're not looking for a non-static member, and this is
104bf76a
MM
6120 one, or vice versa. */
6121 continue;
6122
104bf76a 6123 /* Try to do argument deduction. */
f31c0a32 6124 targs = make_tree_vec (DECL_NTPARMS (fn));
4393e105 6125 if (fn_type_unification (fn, explicit_targs, targs,
8d3631f8 6126 target_arg_types, target_ret_type,
03017874 6127 DEDUCE_EXACT) != 0)
104bf76a
MM
6128 /* Argument deduction failed. */
6129 continue;
6130
6131 /* Instantiate the template. */
6132 instantiation = instantiate_template (fn, targs);
6133 if (instantiation == error_mark_node)
6134 /* Instantiation failed. */
6135 continue;
6136
6137 /* See if there's a match. */
6138 instantiation_type = TREE_TYPE (instantiation);
6139 if (is_ptrmem)
6140 instantiation_type =
6141 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
6142 else if (!is_reference)
6143 instantiation_type = build_pointer_type (instantiation_type);
6144 if (can_convert_arg (target_type, instantiation_type, instantiation))
e1b3e07d 6145 matches = tree_cons (instantiation, fn, matches);
104bf76a
MM
6146 }
6147
6148 /* Now, remove all but the most specialized of the matches. */
6149 if (matches)
6150 {
6151 tree match = most_specialized_instantiation (matches,
6152 explicit_targs);
6153
6154 if (match != error_mark_node)
e1b3e07d 6155 matches = tree_cons (match, NULL_TREE, NULL_TREE);
104bf76a
MM
6156 }
6157 }
6158
6159 /* Now we should have exactly one function in MATCHES. */
6160 if (matches == NULL_TREE)
6161 {
6162 /* There were *no* matches. */
6163 if (complain)
6164 {
6b9b6b15 6165 cp_error ("no matches converting function `%D' to type `%#T'",
104bf76a
MM
6166 DECL_NAME (OVL_FUNCTION (overload)),
6167 target_type);
6b9b6b15
JM
6168
6169 /* print_candidates expects a chain with the functions in
6170 TREE_VALUE slots, so we cons one up here (we're losing anyway,
6171 so why be clever?). */
6172 for (; overload; overload = OVL_NEXT (overload))
e1b3e07d
MM
6173 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6174 matches);
6b9b6b15
JM
6175
6176 print_candidates (matches);
104bf76a
MM
6177 }
6178 return error_mark_node;
2c73f9f5 6179 }
104bf76a
MM
6180 else if (TREE_CHAIN (matches))
6181 {
6182 /* There were too many matches. */
6183
6184 if (complain)
6185 {
6186 tree match;
6187
6188 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6189 DECL_NAME (OVL_FUNCTION (overload)),
6190 target_type);
6191
6192 /* Since print_candidates expects the functions in the
6193 TREE_VALUE slot, we flip them here. */
6194 for (match = matches; match; match = TREE_CHAIN (match))
6195 TREE_VALUE (match) = TREE_PURPOSE (match);
6196
6197 print_candidates (matches);
6198 }
6199
6200 return error_mark_node;
6201 }
6202
50714e79
MM
6203 /* Good, exactly one match. Now, convert it to the correct type. */
6204 fn = TREE_PURPOSE (matches);
6205
a6ecf8b6
JM
6206 mark_used (fn);
6207
50714e79
MM
6208 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6209 return build_unary_op (ADDR_EXPR, fn, 0);
6210 else
6211 {
6212 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6213 will mark the function as addressed, but here we must do it
6214 explicitly. */
6215 mark_addressable (fn);
6216
6217 return fn;
6218 }
2c73f9f5
ML
6219}
6220
ec255269
MS
6221/* This function will instantiate the type of the expression given in
6222 RHS to match the type of LHSTYPE. If errors exist, then return
2036a15c 6223 error_mark_node. We only complain is COMPLAIN is set. If we are
ec255269
MS
6224 not complaining, never modify rhs, as overload resolution wants to
6225 try many possible instantiations, in hopes that at least one will
6226 work.
8d08fdba 6227
940ff223
JM
6228 FLAGS is a bitmask, as we see at the top of the function.
6229
e6e174e5
JM
6230 For non-recursive calls, LHSTYPE should be a function, pointer to
6231 function, or a pointer to member function. */
e92cc029 6232
8d08fdba 6233tree
940ff223 6234instantiate_type (lhstype, rhs, flags)
8d08fdba 6235 tree lhstype, rhs;
940ff223 6236 int flags;
8d08fdba 6237{
940ff223
JM
6238 int complain = (flags & 1);
6239 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6240
8d08fdba
MS
6241 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6242 {
6243 if (complain)
8251199e 6244 error ("not enough type information");
8d08fdba
MS
6245 return error_mark_node;
6246 }
6247
6248 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
abff8e06 6249 {
940ff223 6250 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
abff8e06
JM
6251 return rhs;
6252 if (complain)
8251199e 6253 cp_error ("argument of type `%T' does not match `%T'",
abff8e06
JM
6254 TREE_TYPE (rhs), lhstype);
6255 return error_mark_node;
6256 }
8d08fdba 6257
2c73f9f5
ML
6258 /* We don't overwrite rhs if it is an overloaded function.
6259 Copying it would destroy the tree link. */
6260 if (TREE_CODE (rhs) != OVERLOAD)
6261 rhs = copy_node (rhs);
c73964b2 6262
8d08fdba
MS
6263 /* This should really only be used when attempting to distinguish
6264 what sort of a pointer to function we have. For now, any
6265 arithmetic operation which is not supported on pointers
6266 is rejected as an error. */
6267
6268 switch (TREE_CODE (rhs))
6269 {
6270 case TYPE_EXPR:
6271 case CONVERT_EXPR:
6272 case SAVE_EXPR:
6273 case CONSTRUCTOR:
6274 case BUFFER_REF:
6275 my_friendly_abort (177);
6276 return error_mark_node;
6277
6278 case INDIRECT_REF:
6279 case ARRAY_REF:
ec255269
MS
6280 {
6281 tree new_rhs;
8d08fdba 6282
ec255269 6283 new_rhs = instantiate_type (build_pointer_type (lhstype),
940ff223 6284 TREE_OPERAND (rhs, 0), flags);
ec255269
MS
6285 if (new_rhs == error_mark_node)
6286 return error_mark_node;
6287
6288 TREE_TYPE (rhs) = lhstype;
6289 TREE_OPERAND (rhs, 0) = new_rhs;
6290 return rhs;
6291 }
8d08fdba
MS
6292
6293 case NOP_EXPR:
6294 rhs = copy_node (TREE_OPERAND (rhs, 0));
6295 TREE_TYPE (rhs) = unknown_type_node;
940ff223 6296 return instantiate_type (lhstype, rhs, flags);
8d08fdba
MS
6297
6298 case COMPONENT_REF:
6299 {
d2c192ad 6300 tree r = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
50714e79 6301
d2c192ad
JM
6302 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype)
6303 && complain && !flag_ms_extensions)
50714e79 6304 {
d2c192ad
JM
6305 /* Note: we check this after the recursive call to avoid
6306 complaining about cases where overload resolution fails. */
6307
6308 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
6309 tree fn = PTRMEM_CST_MEMBER (r);
6310
6311 my_friendly_assert (TREE_CODE (r) == PTRMEM_CST, 990811);
6312
6313 cp_pedwarn
6314 ("object-dependent reference to `%E' can only be used in a call",
6315 DECL_NAME (fn));
6316 cp_pedwarn
6317 (" to form a pointer to member function, say `&%T::%E'",
6318 t, DECL_NAME (fn));
8d08fdba 6319 }
d2c192ad 6320
50714e79 6321 return r;
8d08fdba
MS
6322 }
6323
2a238a97 6324 case OFFSET_REF:
05e0b2f4
JM
6325 rhs = TREE_OPERAND (rhs, 1);
6326 if (BASELINK_P (rhs))
6327 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6328
2a238a97
MM
6329 /* This can happen if we are forming a pointer-to-member for a
6330 member template. */
2a238a97 6331 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
05e0b2f4 6332
2a238a97 6333 /* Fall through. */
874503bc 6334
386b8a85 6335 case TEMPLATE_ID_EXPR:
104bf76a
MM
6336 return
6337 resolve_address_of_overloaded_function (lhstype,
6338 TREE_OPERAND (rhs, 0),
6339 complain,
6340 /*template_only=*/1,
6341 TREE_OPERAND (rhs, 1));
386b8a85 6342
2c73f9f5 6343 case OVERLOAD:
104bf76a
MM
6344 return
6345 resolve_address_of_overloaded_function (lhstype,
6346 rhs,
6347 complain,
6348 /*template_only=*/0,
6349 /*explicit_targs=*/NULL_TREE);
2c73f9f5
ML
6350
6351 case TREE_LIST:
940ff223
JM
6352 /* Now we should have a baselink. */
6353 my_friendly_assert (BASELINK_P (rhs), 990412);
e5966228 6354
940ff223 6355 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
8d08fdba
MS
6356
6357 case CALL_EXPR:
6358 /* This is too hard for now. */
6359 my_friendly_abort (183);
6360 return error_mark_node;
6361
6362 case PLUS_EXPR:
6363 case MINUS_EXPR:
6364 case COMPOUND_EXPR:
a0a33927 6365 TREE_OPERAND (rhs, 0)
940ff223 6366 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
8d08fdba
MS
6367 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6368 return error_mark_node;
a0a33927 6369 TREE_OPERAND (rhs, 1)
940ff223 6370 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
8d08fdba
MS
6371 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6372 return error_mark_node;
6373
6374 TREE_TYPE (rhs) = lhstype;
6375 return rhs;
6376
6377 case MULT_EXPR:
6378 case TRUNC_DIV_EXPR:
6379 case FLOOR_DIV_EXPR:
6380 case CEIL_DIV_EXPR:
6381 case ROUND_DIV_EXPR:
6382 case RDIV_EXPR:
6383 case TRUNC_MOD_EXPR:
6384 case FLOOR_MOD_EXPR:
6385 case CEIL_MOD_EXPR:
6386 case ROUND_MOD_EXPR:
6387 case FIX_ROUND_EXPR:
6388 case FIX_FLOOR_EXPR:
6389 case FIX_CEIL_EXPR:
6390 case FIX_TRUNC_EXPR:
6391 case FLOAT_EXPR:
6392 case NEGATE_EXPR:
6393 case ABS_EXPR:
6394 case MAX_EXPR:
6395 case MIN_EXPR:
6396 case FFS_EXPR:
6397
6398 case BIT_AND_EXPR:
6399 case BIT_IOR_EXPR:
6400 case BIT_XOR_EXPR:
6401 case LSHIFT_EXPR:
6402 case RSHIFT_EXPR:
6403 case LROTATE_EXPR:
6404 case RROTATE_EXPR:
6405
6406 case PREINCREMENT_EXPR:
6407 case PREDECREMENT_EXPR:
6408 case POSTINCREMENT_EXPR:
6409 case POSTDECREMENT_EXPR:
6410 if (complain)
8251199e 6411 error ("invalid operation on uninstantiated type");
8d08fdba
MS
6412 return error_mark_node;
6413
6414 case TRUTH_AND_EXPR:
6415 case TRUTH_OR_EXPR:
6416 case TRUTH_XOR_EXPR:
6417 case LT_EXPR:
6418 case LE_EXPR:
6419 case GT_EXPR:
6420 case GE_EXPR:
6421 case EQ_EXPR:
6422 case NE_EXPR:
6423 case TRUTH_ANDIF_EXPR:
6424 case TRUTH_ORIF_EXPR:
6425 case TRUTH_NOT_EXPR:
6426 if (complain)
8251199e 6427 error ("not enough type information");
8d08fdba
MS
6428 return error_mark_node;
6429
6430 case COND_EXPR:
6431 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6432 {
6433 if (complain)
8251199e 6434 error ("not enough type information");
8d08fdba
MS
6435 return error_mark_node;
6436 }
a0a33927 6437 TREE_OPERAND (rhs, 1)
940ff223 6438 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
8d08fdba
MS
6439 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6440 return error_mark_node;
a0a33927 6441 TREE_OPERAND (rhs, 2)
940ff223 6442 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
8d08fdba
MS
6443 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6444 return error_mark_node;
6445
6446 TREE_TYPE (rhs) = lhstype;
6447 return rhs;
6448
6449 case MODIFY_EXPR:
a0a33927 6450 TREE_OPERAND (rhs, 1)
940ff223 6451 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
8d08fdba
MS
6452 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6453 return error_mark_node;
6454
6455 TREE_TYPE (rhs) = lhstype;
6456 return rhs;
6457
6458 case ADDR_EXPR:
940ff223 6459 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
8d08fdba
MS
6460
6461 case ENTRY_VALUE_EXPR:
6462 my_friendly_abort (184);
6463 return error_mark_node;
6464
6465 case ERROR_MARK:
6466 return error_mark_node;
6467
6468 default:
6469 my_friendly_abort (185);
6470 return error_mark_node;
6471 }
6472}
6473\f
6474/* Return the name of the virtual function pointer field
6475 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6476 this may have to look back through base types to find the
6477 ultimate field name. (For single inheritance, these could
6478 all be the same name. Who knows for multiple inheritance). */
e92cc029 6479
8d08fdba
MS
6480static tree
6481get_vfield_name (type)
6482 tree type;
6483{
6484 tree binfo = TYPE_BINFO (type);
6485 char *buf;
6486
6487 while (BINFO_BASETYPES (binfo)
bbd15aac 6488 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
8d08fdba
MS
6489 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6490 binfo = BINFO_BASETYPE (binfo, 0);
6491
6492 type = BINFO_TYPE (binfo);
2636fde4
JM
6493 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6494 + TYPE_NAME_LENGTH (type) + 2);
8d08fdba
MS
6495 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6496 return get_identifier (buf);
6497}
6498
6499void
6500print_class_statistics ()
6501{
6502#ifdef GATHER_STATISTICS
6503 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6504 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6505 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6506 n_build_method_call, n_inner_fields_searched);
6507 if (n_vtables)
6508 {
6509 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6510 n_vtables, n_vtable_searches);
6511 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6512 n_vtable_entries, n_vtable_elems);
6513 }
6514#endif
6515}
6516
c91a56d2
MS
6517/* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6518 according to [class]:
6519 The class-name is also inserted
6520 into the scope of the class itself. For purposes of access checking,
6521 the inserted class name is treated as if it were a public member name. */
6522
d6479fe7 6523void
c91a56d2
MS
6524build_self_reference ()
6525{
6526 tree name = constructor_name (current_class_type);
6527 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
d6479fe7
MM
6528 tree saved_cas;
6529
c91a56d2
MS
6530 DECL_NONLOCAL (value) = 1;
6531 DECL_CONTEXT (value) = current_class_type;
c91a56d2
MS
6532 DECL_ARTIFICIAL (value) = 1;
6533
9188c363
MM
6534 if (processing_template_decl)
6535 value = push_template_decl (value);
6536
d6479fe7
MM
6537 saved_cas = current_access_specifier;
6538 current_access_specifier = access_public_node;
6539 finish_member_declaration (value);
6540 current_access_specifier = saved_cas;
c91a56d2 6541}
570221c2
JM
6542
6543/* Returns 1 if TYPE contains only padding bytes. */
6544
6545int
6546is_empty_class (type)
6547 tree type;
6548{
6549 tree t;
6550
5a11e05b
BK
6551 if (type == error_mark_node)
6552 return 0;
6553
a59ca936
JM
6554 if (! IS_AGGR_TYPE (type))
6555 return 0;
6556
6557 if (flag_new_abi)
06ceef4e 6558 return integer_zerop (CLASSTYPE_SIZE (type));
a59ca936
JM
6559
6560 if (TYPE_BINFO_BASETYPES (type))
570221c2
JM
6561 return 0;
6562 t = TYPE_FIELDS (type);
6563 while (t && TREE_CODE (t) != FIELD_DECL)
6564 t = TREE_CHAIN (t);
6565 return (t == NULL_TREE);
6566}
b54ccf71
JM
6567
6568/* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6569 a *_TYPE node. NODE can also be a local class. */
6570
6571tree
6572get_enclosing_class (type)
6573 tree type;
6574{
6575 tree node = type;
6576
6577 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6578 {
6579 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6580 {
6581 case 'd':
6582 node = DECL_CONTEXT (node);
6583 break;
6584
6585 case 't':
6586 if (node != type)
6587 return node;
6588 node = TYPE_CONTEXT (node);
6589 break;
6590
6591 default:
6592 my_friendly_abort (0);
6593 }
6594 }
6595 return NULL_TREE;
6596}
6597
6598/* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6599
6600int
6601is_base_of_enclosing_class (base, type)
6602 tree base, type;
6603{
6604 while (type)
6605 {
6606 if (get_binfo (base, type, 0))
6607 return 1;
6608
6609 type = get_enclosing_class (type);
6610 }
6611 return 0;
6612}
8f032717
MM
6613
6614/* Note that NAME was looked up while the current class was being
6615 defined and that the result of that lookup was DECL. */
6616
6617void
6618maybe_note_name_used_in_class (name, decl)
6619 tree name;
6620 tree decl;
6621{
6622 splay_tree names_used;
6623
6624 /* If we're not defining a class, there's nothing to do. */
6625 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6626 return;
6627
6628 /* If there's already a binding for this NAME, then we don't have
6629 anything to worry about. */
6630 if (IDENTIFIER_CLASS_VALUE (name))
6631 return;
6632
6633 if (!current_class_stack[current_class_depth - 1].names_used)
6634 current_class_stack[current_class_depth - 1].names_used
6635 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6636 names_used = current_class_stack[current_class_depth - 1].names_used;
6637
6638 splay_tree_insert (names_used,
6639 (splay_tree_key) name,
6640 (splay_tree_value) decl);
6641}
6642
6643/* Note that NAME was declared (as DECL) in the current class. Check
6644 to see that the declaration is legal. */
6645
6646void
6647note_name_declared_in_class (name, decl)
6648 tree name;
6649 tree decl;
6650{
6651 splay_tree names_used;
6652 splay_tree_node n;
6653
6654 /* Look to see if we ever used this name. */
6655 names_used
6656 = current_class_stack[current_class_depth - 1].names_used;
6657 if (!names_used)
6658 return;
6659
6660 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6661 if (n)
6662 {
6663 /* [basic.scope.class]
6664
6665 A name N used in a class S shall refer to the same declaration
6666 in its context and when re-evaluated in the completed scope of
6667 S. */
6668 cp_error ("declaration of `%#D'", decl);
6669 cp_error_at ("changes meaning of `%s' from `%+#D'",
f281dd5a 6670 IDENTIFIER_POINTER (DECL_NAME (OVL_CURRENT (decl))),
8f032717
MM
6671 (tree) n->value);
6672 }
6673}
4a314e0c
MM
6674
6675/* Dump the offsets of all the bases rooted at BINFO to stderr.
6676 INDENT should be zero when called from the top level; it is
6677 incremented recursively. */
6678
6679void
6680dump_class_hierarchy (binfo, indent)
6681 tree binfo;
6682 int indent;
6683{
6684 int i;
6685
92ac3c0f 6686 fprintf (stderr, "%*s0x%lx (%s) ", indent, "",
2984dacf 6687 (unsigned long) binfo,
92ac3c0f
BL
6688 type_as_string (binfo, TS_PLAIN));
6689 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC,
665f2503 6690 tree_low_cst (BINFO_OFFSET (binfo), 0));
92ac3c0f 6691 fprintf (stderr, " %s\n",
dd42e135 6692 BINFO_PRIMARY_MARKED_P (binfo) ? "primary" : "");
4a314e0c
MM
6693
6694 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6695 dump_class_hierarchy (BINFO_BASETYPE (binfo, i), indent + 2);
6696}