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