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