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