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