]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/class.c
check-init.c (check_init): Removed code accepting to see things falling through defau...
[thirdparty/gcc.git] / gcc / cp / class.c
CommitLineData
8d08fdba 1/* Functions related to building classes and their related objects.
6b9b6b15 2 Copyright (C) 1987, 92-97, 1998, 1999 Free Software Foundation, Inc.
8d08fdba
MS
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
e9fa0c7c
RK
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
8d08fdba
MS
21
22
e92cc029 23/* High-level class interface. */
8d08fdba
MS
24
25#include "config.h"
8d052bc7 26#include "system.h"
e7a587ef 27#include "tree.h"
8d08fdba
MS
28#include "cp-tree.h"
29#include "flags.h"
28cbf42c 30#include "rtl.h"
e8abc66f 31#include "output.h"
54f92bfb 32#include "toplev.h"
8f032717 33#include "splay-tree.h"
8d08fdba
MS
34
35#include "obstack.h"
36#define obstack_chunk_alloc xmalloc
37#define obstack_chunk_free free
38
39extern struct obstack permanent_obstack;
40
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
45extern void set_class_shadows PROTO ((tree));
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
01240200
MM
76/* When we're processing a member function, current_class_ptr is the
77 PARM_DECL for the `this' pointer. The current_class_ref is an
78 expression for `*this'. */
4ac14744 79tree current_class_ptr, current_class_ref;
8d08fdba
MS
80
81/* The following two can be derived from the previous one */
82tree current_class_name; /* IDENTIFIER_NODE: name of current class */
83tree current_class_type; /* _TYPE: the type of the current class */
61a127b3 84tree current_access_specifier;
8d08fdba 85tree previous_class_type; /* _TYPE: the previous type that was a class */
8c1bd4f5 86tree previous_class_values; /* TREE_LIST: copy of the class_shadowed list
8d08fdba 87 when leaving an outermost class scope. */
49c249e1 88
7ddedda4
MM
89/* The obstack on which the cached class declarations are kept. */
90static struct obstack class_cache_obstack;
91/* The first object allocated on that obstack. We can use
92 obstack_free with tis value to free the entire obstack. */
93static char *class_cache_firstobj;
94
49c249e1
JM
95struct base_info;
96
8d08fdba 97static tree get_vfield_name PROTO((tree));
49c249e1
JM
98static void finish_struct_anon PROTO((tree));
99static tree build_vbase_pointer PROTO((tree, tree));
49c249e1
JM
100static tree build_vtable_entry PROTO((tree, tree));
101static tree get_vtable_name PROTO((tree));
102static tree get_derived_offset PROTO((tree, tree));
103static tree get_basefndecls PROTO((tree, tree));
104static void set_rtti_entry PROTO((tree, tree, tree));
105static tree build_vtable PROTO((tree, tree));
49c249e1
JM
106static void prepare_fresh_vtable PROTO((tree, tree));
107static void fixup_vtable_deltas1 PROTO((tree, tree));
108static void fixup_vtable_deltas PROTO((tree, int, tree));
49c249e1
JM
109static void finish_vtbls PROTO((tree, int, tree));
110static void modify_vtable_entry PROTO((tree, tree, tree));
111static tree get_vtable_entry_n PROTO((tree, unsigned HOST_WIDE_INT));
aa598818 112static void add_virtual_function PROTO((tree *, tree *, int *, tree, tree));
49c249e1
JM
113static tree delete_duplicate_fields_1 PROTO((tree, tree));
114static void delete_duplicate_fields PROTO((tree));
115static void finish_struct_bits PROTO((tree, int));
79ad62b2
MM
116static int alter_access PROTO((tree, tree, tree, tree));
117static void handle_using_decl PROTO((tree, tree, tree, tree));
49c249e1
JM
118static int overrides PROTO((tree, tree));
119static int strictly_overrides PROTO((tree, tree));
120static void merge_overrides PROTO((tree, tree, int, tree));
121static void override_one_vtable PROTO((tree, tree, tree));
122static void mark_overriders PROTO((tree, tree));
123static void check_for_override PROTO((tree, tree));
49c249e1
JM
124static tree get_class_offset_1 PROTO((tree, tree, tree, tree, tree));
125static tree get_class_offset PROTO((tree, tree, tree, tree));
126static void modify_one_vtable PROTO((tree, tree, tree, tree));
127static void modify_all_vtables PROTO((tree, tree, tree));
128static void modify_all_direct_vtables PROTO((tree, int, tree, tree,
129 tree));
130static void modify_all_indirect_vtables PROTO((tree, int, int, tree,
131 tree, tree));
9a71c18b 132static int finish_base_struct PROTO((tree, struct base_info *));
b0e0b31f
MM
133static void finish_struct_methods PROTO((tree));
134static void maybe_warn_about_overly_private_class PROTO ((tree));
61a127b3
MM
135static tree make_method_vec PROTO((int));
136static void free_method_vec PROTO((tree));
137static tree add_implicitly_declared_members PROTO((tree, int, int, int));
d8e178a0
KG
138static tree fixed_type_or_null PROTO((tree, int *));
139static tree resolve_address_of_overloaded_function PROTO((tree, tree, int,
140 int, tree));
141static void build_vtable_entry_ref PROTO((tree, tree, tree));
8d08fdba
MS
142
143/* Way of stacking language names. */
144tree *current_lang_base, *current_lang_stack;
51c184be 145int current_lang_stacksize;
8d08fdba
MS
146
147/* Names of languages we recognize. */
a1774733 148tree lang_name_c, lang_name_cplusplus, lang_name_java;
8d08fdba
MS
149tree current_lang_name;
150
151/* When layout out an aggregate type, the size of the
152 basetypes (virtual and non-virtual) is passed to layout_record
153 via this node. */
154static tree base_layout_decl;
155
be99da77
MS
156/* Constants used for access control. */
157tree access_default_node; /* 0 */
158tree access_public_node; /* 1 */
159tree access_protected_node; /* 2 */
160tree access_private_node; /* 3 */
161tree access_default_virtual_node; /* 4 */
162tree access_public_virtual_node; /* 5 */
d8b55a76
JM
163tree access_protected_virtual_node; /* 6 */
164tree access_private_virtual_node; /* 7 */
be99da77 165
51c184be 166/* Variables shared between class.c and call.c. */
8d08fdba 167
5566b478 168#ifdef GATHER_STATISTICS
8d08fdba
MS
169int n_vtables = 0;
170int n_vtable_entries = 0;
171int n_vtable_searches = 0;
172int n_vtable_elems = 0;
173int n_convert_harshness = 0;
174int n_compute_conversion_costs = 0;
175int n_build_method_call = 0;
176int n_inner_fields_searched = 0;
5566b478 177#endif
8d08fdba
MS
178
179/* Virtual baseclass things. */
e92cc029 180
bd6dd845 181static tree
8d08fdba
MS
182build_vbase_pointer (exp, type)
183 tree exp, type;
184{
185 char *name;
38f01e5a 186 FORMAT_VBASE_NAME (name, type);
8d08fdba 187
4dabb379 188 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
8d08fdba
MS
189}
190
8c1bd4f5 191#if 0
8d08fdba 192/* Is the type of the EXPR, the complete type of the object?
e92cc029
MS
193 If we are going to be wrong, we must be conservative, and return 0. */
194
bd6dd845 195static int
8d08fdba
MS
196complete_type_p (expr)
197 tree expr;
198{
199 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
200 while (1)
201 {
202 switch (TREE_CODE (expr))
203 {
204 case SAVE_EXPR:
205 case INDIRECT_REF:
206 case ADDR_EXPR:
207 case NOP_EXPR:
208 case CONVERT_EXPR:
209 expr = TREE_OPERAND (expr, 0);
210 continue;
211
212 case CALL_EXPR:
213 if (! TREE_HAS_CONSTRUCTOR (expr))
214 break;
e92cc029 215 /* fall through... */
8d08fdba
MS
216 case VAR_DECL:
217 case FIELD_DECL:
218 if (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
219 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr)))
220 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
221 return 1;
e92cc029 222 /* fall through... */
8d08fdba
MS
223 case TARGET_EXPR:
224 case PARM_DECL:
225 if (IS_AGGR_TYPE (TREE_TYPE (expr))
226 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
227 return 1;
e92cc029 228 /* fall through... */
8d08fdba
MS
229 case PLUS_EXPR:
230 default:
231 break;
232 }
233 break;
234 }
235 return 0;
236}
8c1bd4f5 237#endif
8d08fdba
MS
238
239/* Build multi-level access to EXPR using hierarchy path PATH.
240 CODE is PLUS_EXPR if we are going with the grain,
241 and MINUS_EXPR if we are not (in which case, we cannot traverse
242 virtual baseclass links).
243
244 TYPE is the type we want this path to have on exit.
245
51ddb82e
JM
246 NONNULL is non-zero if we know (for any reason) that EXPR is
247 not, in fact, zero. */
e92cc029 248
8d08fdba 249tree
51ddb82e 250build_vbase_path (code, type, expr, path, nonnull)
8d08fdba
MS
251 enum tree_code code;
252 tree type, expr, path;
51ddb82e 253 int nonnull;
8d08fdba
MS
254{
255 register int changed = 0;
256 tree last = NULL_TREE, last_virtual = NULL_TREE;
6633d636 257 int fixed_type_p;
8d08fdba
MS
258 tree null_expr = 0, nonnull_expr;
259 tree basetype;
260 tree offset = integer_zero_node;
261
6633d636
MS
262 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
263 return build1 (NOP_EXPR, type, expr);
264
51ddb82e
JM
265 /* If -fthis-is-variable, we might have set nonnull incorrectly. We
266 don't care enough to get this right, so just clear it. */
267 if (flag_this_is_variable > 0)
268 nonnull = 0;
f30432d7 269
51ddb82e
JM
270 /* We could do better if we had additional logic to convert back to the
271 unconverted type (the static type of the complete object), and then
272 convert back to the type we want. Until that is done, we only optimize
273 if the complete type is the same type as expr has. */
6633d636 274 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
8d08fdba
MS
275
276 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
277 expr = save_expr (expr);
278 nonnull_expr = expr;
279
280 if (BINFO_INHERITANCE_CHAIN (path))
dfbcd65a 281 path = reverse_path (path);
8d08fdba
MS
282
283 basetype = BINFO_TYPE (path);
284
285 while (path)
286 {
287 if (TREE_VIA_VIRTUAL (path))
288 {
289 last_virtual = BINFO_TYPE (path);
290 if (code == PLUS_EXPR)
291 {
292 changed = ! fixed_type_p;
293
294 if (changed)
295 {
8d08fdba
MS
296 tree ind;
297
298 /* We already check for ambiguous things in the caller, just
e92cc029 299 find a path. */
8d08fdba
MS
300 if (last)
301 {
302 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
303 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
304 }
305 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
306 nonnull_expr = build_vbase_pointer (ind, last_virtual);
a9aedbc2 307 if (nonnull == 0
84663f74 308 && TREE_CODE (type) == POINTER_TYPE
8d08fdba
MS
309 && null_expr == NULL_TREE)
310 {
f30432d7
MS
311 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
312 expr = build (COND_EXPR, build_pointer_type (last_virtual),
b7484fbe 313 build (EQ_EXPR, boolean_type_node, expr,
8d08fdba
MS
314 integer_zero_node),
315 null_expr, nonnull_expr);
316 }
317 }
318 /* else we'll figure out the offset below. */
319
320 /* Happens in the case of parse errors. */
321 if (nonnull_expr == error_mark_node)
322 return error_mark_node;
323 }
324 else
325 {
8251199e 326 cp_error ("cannot cast up from virtual baseclass `%T'",
8d08fdba
MS
327 last_virtual);
328 return error_mark_node;
329 }
330 }
331 last = path;
332 path = BINFO_INHERITANCE_CHAIN (path);
333 }
334 /* LAST is now the last basetype assoc on the path. */
335
336 /* A pointer to a virtual base member of a non-null object
337 is non-null. Therefore, we only need to test for zeroness once.
338 Make EXPR the canonical expression to deal with here. */
339 if (null_expr)
340 {
341 TREE_OPERAND (expr, 2) = nonnull_expr;
b9ddcfac
JM
342 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
343 = TREE_TYPE (nonnull_expr);
8d08fdba
MS
344 }
345 else
346 expr = nonnull_expr;
347
348 /* If we go through any virtual base pointers, make sure that
349 casts to BASETYPE from the last virtual base class use
350 the right value for BASETYPE. */
351 if (changed)
352 {
353 tree intype = TREE_TYPE (TREE_TYPE (expr));
f30432d7 354 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
8d08fdba
MS
355 {
356 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
8d08fdba
MS
357 offset = BINFO_OFFSET (binfo);
358 }
359 }
360 else
361 {
362 if (last_virtual)
363 {
364 offset = BINFO_OFFSET (binfo_member (last_virtual,
365 CLASSTYPE_VBASECLASSES (basetype)));
366 offset = size_binop (PLUS_EXPR, offset, BINFO_OFFSET (last));
367 }
368 else
369 offset = BINFO_OFFSET (last);
370 }
371
372 if (TREE_INT_CST_LOW (offset))
373 {
59be85d7 374 /* Bash types to make the backend happy. */
37c46b43
MS
375 offset = cp_convert (type, offset);
376#if 0
377 /* This shouldn't be necessary. (mrs) */
59be85d7 378 expr = build1 (NOP_EXPR, type, expr);
37c46b43 379#endif
59be85d7 380
51ddb82e 381 /* If expr might be 0, we need to preserve that zeroness. */
f30432d7 382 if (nonnull == 0)
8d08fdba
MS
383 {
384 if (null_expr)
385 TREE_TYPE (null_expr) = type;
386 else
387 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
388 if (TREE_SIDE_EFFECTS (expr))
389 expr = save_expr (expr);
390
391 return build (COND_EXPR, type,
b7484fbe 392 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
8d08fdba
MS
393 null_expr,
394 build (code, type, expr, offset));
395 }
396 else return build (code, type, expr, offset);
397 }
398
399 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
400 be used multiple times in initialization of multiple inheritance. */
401 if (null_expr)
402 {
403 TREE_TYPE (expr) = type;
404 return expr;
405 }
406 else
407 return build1 (NOP_EXPR, type, expr);
408}
409
410/* Virtual function things. */
411
8d08fdba
MS
412/* Build an entry in the virtual function table.
413 DELTA is the offset for the `this' pointer.
414 PFN is an ADDR_EXPR containing a pointer to the virtual function.
415 Note that the index (DELTA2) in the virtual function table
416 is always 0. */
e92cc029 417
bd6dd845 418static tree
8d08fdba
MS
419build_vtable_entry (delta, pfn)
420 tree delta, pfn;
421{
8926095f
MS
422 if (flag_vtable_thunks)
423 {
424 HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta);
46b02c6d 425 if (idelta && ! DECL_ABSTRACT_VIRTUAL_P (TREE_OPERAND (pfn, 0)))
8926095f 426 {
700f8a87 427 pfn = build1 (ADDR_EXPR, vtable_entry_type,
8926095f
MS
428 make_thunk (pfn, idelta));
429 TREE_READONLY (pfn) = 1;
430 TREE_CONSTANT (pfn) = 1;
431 }
432#ifdef GATHER_STATISTICS
433 n_vtable_entries += 1;
434#endif
435 return pfn;
436 }
437 else
438 {
439 extern int flag_huge_objects;
e66d884e
JM
440 tree elems = expr_tree_cons (NULL_TREE, delta,
441 expr_tree_cons (NULL_TREE, integer_zero_node,
442 build_expr_list (NULL_TREE, pfn)));
8926095f
MS
443 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
444
329745f7
JM
445 /* DELTA used to be constructed by `size_int' and/or size_binop,
446 which caused overflow problems when it was negative. That should
447 be fixed now. */
8926095f 448
329745f7 449 if (! int_fits_type_p (delta, delta_type_node))
a703fb38
KG
450 {
451 if (flag_huge_objects)
452 sorry ("object size exceeds built-in limit for virtual function table implementation");
453 else
454 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
455 }
456
8926095f
MS
457 TREE_CONSTANT (entry) = 1;
458 TREE_STATIC (entry) = 1;
459 TREE_READONLY (entry) = 1;
8d08fdba
MS
460
461#ifdef GATHER_STATISTICS
8926095f 462 n_vtable_entries += 1;
8d08fdba
MS
463#endif
464
8926095f
MS
465 return entry;
466 }
8d08fdba
MS
467}
468
a1dd0d36
JM
469/* We want to give the assembler the vtable identifier as well as
470 the offset to the function pointer. So we generate
471
59fa060f 472 __asm__ __volatile__ (".vtable_entry %c0, %c1"
a1dd0d36
JM
473 : : "s"(&class_vtable),
474 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
475
476static void
477build_vtable_entry_ref (basetype, vtbl, idx)
478 tree basetype, vtbl, idx;
479{
59fa060f 480 static char asm_stmt[] = ".vtable_entry %c0, %c1";
a1dd0d36
JM
481 tree s, i, i2;
482
483 s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
484 s = build_tree_list (build_string (1, "s"), s);
485
486 i = build_array_ref (vtbl, idx);
487 if (!flag_vtable_thunks)
488 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
489 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
490 i2 = build_array_ref (vtbl, build_int_2(0,0));
491 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
337c90cc 492 i = build_binary_op (MINUS_EXPR, i, i2);
a1dd0d36
JM
493 i = build_tree_list (build_string (1, "i"), i);
494
495 expand_asm_operands (build_string (sizeof(asm_stmt)-1, asm_stmt),
496 NULL_TREE, chainon (s, i), NULL_TREE, 1, NULL, 0);
497}
498
8d08fdba 499/* Given an object INSTANCE, return an expression which yields the
6b5fbb55
MS
500 virtual function vtable element corresponding to INDEX. There are
501 many special cases for INSTANCE which we take care of here, mainly
502 to avoid creating extra tree nodes when we don't have to. */
e92cc029 503
8d08fdba 504tree
6b5fbb55
MS
505build_vtbl_ref (instance, idx)
506 tree instance, idx;
8d08fdba 507{
8d08fdba
MS
508 tree vtbl, aref;
509 tree basetype = TREE_TYPE (instance);
510
511 if (TREE_CODE (basetype) == REFERENCE_TYPE)
512 basetype = TREE_TYPE (basetype);
513
4ac14744 514 if (instance == current_class_ref)
849da744 515 vtbl = build_vfield_ref (instance, basetype);
8d08fdba
MS
516 else
517 {
518 if (optimize)
519 {
520 /* Try to figure out what a reference refers to, and
521 access its virtual function table directly. */
522 tree ref = NULL_TREE;
523
524 if (TREE_CODE (instance) == INDIRECT_REF
525 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
526 ref = TREE_OPERAND (instance, 0);
527 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
528 ref = instance;
529
530 if (ref && TREE_CODE (ref) == VAR_DECL
531 && DECL_INITIAL (ref))
532 {
533 tree init = DECL_INITIAL (ref);
534
535 while (TREE_CODE (init) == NOP_EXPR
536 || TREE_CODE (init) == NON_LVALUE_EXPR)
537 init = TREE_OPERAND (init, 0);
538 if (TREE_CODE (init) == ADDR_EXPR)
539 {
540 init = TREE_OPERAND (init, 0);
541 if (IS_AGGR_TYPE (TREE_TYPE (init))
542 && (TREE_CODE (init) == PARM_DECL
543 || TREE_CODE (init) == VAR_DECL))
544 instance = init;
545 }
546 }
547 }
548
549 if (IS_AGGR_TYPE (TREE_TYPE (instance))
8d08fdba
MS
550 && (TREE_CODE (instance) == RESULT_DECL
551 || TREE_CODE (instance) == PARM_DECL
552 || TREE_CODE (instance) == VAR_DECL))
553 vtbl = TYPE_BINFO_VTABLE (basetype);
554 else
849da744 555 vtbl = build_vfield_ref (instance, basetype);
8d08fdba 556 }
a1dd0d36 557
e3417fcd 558 assemble_external (vtbl);
a1dd0d36
JM
559
560 if (flag_vtable_gc)
561 build_vtable_entry_ref (basetype, vtbl, idx);
562
8d08fdba
MS
563 aref = build_array_ref (vtbl, idx);
564
6b5fbb55
MS
565 return aref;
566}
567
568/* Given an object INSTANCE, return an expression which yields the
569 virtual function corresponding to INDEX. There are many special
570 cases for INSTANCE which we take care of here, mainly to avoid
571 creating extra tree nodes when we don't have to. */
e92cc029 572
6b5fbb55
MS
573tree
574build_vfn_ref (ptr_to_instptr, instance, idx)
575 tree *ptr_to_instptr, instance;
576 tree idx;
577{
578 tree aref = build_vtbl_ref (instance, idx);
8d08fdba 579
6b5fbb55
MS
580 /* When using thunks, there is no extra delta, and we get the pfn
581 directly. */
8926095f
MS
582 if (flag_vtable_thunks)
583 return aref;
6b5fbb55
MS
584
585 if (ptr_to_instptr)
8926095f 586 {
6b5fbb55
MS
587 /* Save the intermediate result in a SAVE_EXPR so we don't have to
588 compute each component of the virtual function pointer twice. */
589 if (TREE_CODE (aref) == INDIRECT_REF)
590 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
591
592 *ptr_to_instptr
593 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
594 *ptr_to_instptr,
37c46b43
MS
595 cp_convert (ptrdiff_type_node,
596 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
8926095f 597 }
6b5fbb55
MS
598
599 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
8d08fdba
MS
600}
601
8d08fdba
MS
602/* Return the name of the virtual function table (as an IDENTIFIER_NODE)
603 for the given TYPE. */
e92cc029 604
8d08fdba
MS
605static tree
606get_vtable_name (type)
607 tree type;
608{
609 tree type_id = build_typename_overload (type);
2636fde4
JM
610 char *buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
611 + IDENTIFIER_LENGTH (type_id) + 2);
d8e178a0 612 const char *ptr = IDENTIFIER_POINTER (type_id);
8d08fdba
MS
613 int i;
614 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
615#if 0
616 /* We don't take off the numbers; prepare_fresh_vtable uses the
617 DECL_ASSEMBLER_NAME for the type, which includes the number
618 in `3foo'. If we were to pull them off here, we'd end up with
619 something like `_vt.foo.3bar', instead of a uniform definition. */
620 while (ptr[i] >= '0' && ptr[i] <= '9')
621 i += 1;
622#endif
623 sprintf (buf, VTABLE_NAME_FORMAT, ptr+i);
624 return get_identifier (buf);
625}
626
6b5fbb55 627/* Return the offset to the main vtable for a given base BINFO. */
e92cc029 628
6b5fbb55
MS
629tree
630get_vfield_offset (binfo)
631 tree binfo;
632{
f5426d1e
R
633 tree tmp
634 = size_binop (FLOOR_DIV_EXPR,
635 DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))),
636 size_int (BITS_PER_UNIT));
637 tmp = convert (sizetype, tmp);
638 return size_binop (PLUS_EXPR, tmp, BINFO_OFFSET (binfo));
6b5fbb55
MS
639}
640
641/* Get the offset to the start of the original binfo that we derived
642 this binfo from. If we find TYPE first, return the offset only
643 that far. The shortened search is useful because the this pointer
644 on method calling is expected to point to a DECL_CONTEXT (fndecl)
645 object, and not a baseclass of it. */
e92cc029 646
6b5fbb55
MS
647static tree
648get_derived_offset (binfo, type)
649 tree binfo, type;
650{
651 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
652 tree offset2;
653 int i;
654 while (BINFO_BASETYPES (binfo)
655 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
656 {
657 tree binfos = BINFO_BASETYPES (binfo);
658 if (BINFO_TYPE (binfo) == type)
659 break;
660 binfo = TREE_VEC_ELT (binfos, i);
661 }
662 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
663 return size_binop (MINUS_EXPR, offset1, offset2);
664}
665
666/* Update the rtti info for this class. */
e92cc029 667
6b5fbb55
MS
668static void
669set_rtti_entry (virtuals, offset, type)
670 tree virtuals, offset, type;
671{
672 tree vfn;
673
aff08c18
JM
674 if (CLASSTYPE_COM_INTERFACE (type))
675 return;
676
6b5fbb55
MS
677 if (flag_rtti)
678 vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, get_tinfo_fn (type));
679 else
46b02c6d 680 vfn = build1 (NOP_EXPR, vfunc_ptr_type_node, size_zero_node);
6b5fbb55
MS
681 TREE_CONSTANT (vfn) = 1;
682
683 if (! flag_vtable_thunks)
684 TREE_VALUE (virtuals) = build_vtable_entry (offset, vfn);
685 else
686 {
687 tree voff = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
688 TREE_CONSTANT (voff) = 1;
689
329745f7 690 TREE_VALUE (virtuals) = build_vtable_entry (integer_zero_node, voff);
6b5fbb55
MS
691
692 /* The second slot is for the tdesc pointer when thunks are used. */
693 TREE_VALUE (TREE_CHAIN (virtuals))
329745f7 694 = build_vtable_entry (integer_zero_node, vfn);
6b5fbb55
MS
695 }
696}
697
8d08fdba
MS
698/* Build a virtual function for type TYPE.
699 If BINFO is non-NULL, build the vtable starting with the initial
700 approximation that it is the same as the one which is the head of
701 the association list. */
e92cc029 702
8d08fdba
MS
703static tree
704build_vtable (binfo, type)
705 tree binfo, type;
706{
707 tree name = get_vtable_name (type);
708 tree virtuals, decl;
709
710 if (binfo)
711 {
6b5fbb55
MS
712 tree offset;
713
8d08fdba 714 virtuals = copy_list (BINFO_VIRTUALS (binfo));
0352cfc8 715 decl = build_lang_decl (VAR_DECL, name, TREE_TYPE (BINFO_VTABLE (binfo)));
6b5fbb55
MS
716
717 /* Now do rtti stuff. */
718 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
329745f7 719 offset = ssize_binop (MINUS_EXPR, integer_zero_node, offset);
6b5fbb55 720 set_rtti_entry (virtuals, offset, type);
8d08fdba
MS
721 }
722 else
723 {
724 virtuals = NULL_TREE;
0352cfc8 725 decl = build_lang_decl (VAR_DECL, name, void_type_node);
8d08fdba
MS
726 }
727
728#ifdef GATHER_STATISTICS
729 n_vtables += 1;
730 n_vtable_elems += list_length (virtuals);
731#endif
732
733 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
2455f26f 734 import_export_vtable (decl, type, 0);
8d08fdba 735
2c73f9f5
ML
736 decl = pushdecl_top_level (decl);
737 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
8d08fdba
MS
738 /* Initialize the association list for this type, based
739 on our first approximation. */
740 TYPE_BINFO_VTABLE (type) = decl;
741 TYPE_BINFO_VIRTUALS (type) = virtuals;
742
da20811c 743 DECL_ARTIFICIAL (decl) = 1;
8d08fdba
MS
744 TREE_STATIC (decl) = 1;
745#ifndef WRITABLE_VTABLES
746 /* Make them READONLY by default. (mrs) */
747 TREE_READONLY (decl) = 1;
748#endif
749 /* At one time the vtable info was grabbed 2 words at a time. This
750 fails on sparc unless you have 8-byte alignment. (tiemann) */
751 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
752 DECL_ALIGN (decl));
753
56ae6d77 754 DECL_VIRTUAL_P (decl) = 1;
8d08fdba
MS
755 DECL_CONTEXT (decl) = type;
756
757 binfo = TYPE_BINFO (type);
8d08fdba
MS
758 SET_BINFO_NEW_VTABLE_MARKED (binfo);
759 return decl;
760}
761
e92cc029
MS
762extern tree signed_size_zero_node;
763
8d08fdba
MS
764/* Give TYPE a new virtual function table which is initialized
765 with a skeleton-copy of its original initialization. The only
766 entry that changes is the `delta' entry, so we can really
767 share a lot of structure.
768
769 FOR_TYPE is the derived type which caused this table to
770 be needed.
771
2636fde4
JM
772 BINFO is the type association which provided TYPE for FOR_TYPE.
773
774 The order in which vtables are built (by calling this function) for
775 an object must remain the same, otherwise a binary incompatibility
776 can result. */
e92cc029 777
8d08fdba 778static void
7177d104
MS
779prepare_fresh_vtable (binfo, for_type)
780 tree binfo, for_type;
8d08fdba 781{
2636fde4 782 tree basetype;
8d08fdba 783 tree orig_decl = BINFO_VTABLE (binfo);
2636fde4
JM
784 tree name;
785 tree new_decl;
5566b478 786 tree offset;
2636fde4
JM
787 tree path = binfo;
788 char *buf, *buf2;
789 char joiner = '_';
790 int i;
791
792#ifdef JOINER
793 joiner = JOINER;
794#endif
795
796 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
797
798 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
799 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
800
801 /* We know that the vtable that we are going to create doesn't exist
802 yet in the global namespace, and when we finish, it will be
803 pushed into the global namespace. In complex MI hierarchies, we
804 have to loop while the name we are thinking of adding is globally
805 defined, adding more name components to the vtable name as we
806 loop, until the name is unique. This is because in complex MI
807 cases, we might have the same base more than once. This means
808 that the order in which this function is called for vtables must
809 remain the same, otherwise binary compatibility can be
810 compromised. */
811
812 while (1)
813 {
de35891e
JM
814 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
815 + 1 + i);
2636fde4
JM
816 char *new_buf2;
817
818 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
819 buf2);
820 buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT) + strlen (buf1) + 1);
821 sprintf (buf, VTABLE_NAME_FORMAT, buf1);
822 name = get_identifier (buf);
823
824 /* If this name doesn't clash, then we can use it, otherwise
825 we add more to the name until it is unique. */
826
827 if (! IDENTIFIER_GLOBAL_VALUE (name))
828 break;
829
830 /* Set values for next loop through, if the name isn't unique. */
831
832 path = BINFO_INHERITANCE_CHAIN (path);
833
834 /* We better not run out of stuff to make it unique. */
835 my_friendly_assert (path != NULL_TREE, 368);
836
837 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
838
de35891e
JM
839 if (for_type == basetype)
840 {
841 /* If we run out of basetypes in the path, we have already
842 found created a vtable with that name before, we now
843 resort to tacking on _%d to distinguish them. */
844 int j = 2;
845 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
846 buf1 = (char *) alloca (i);
847 do {
848 sprintf (buf1, "%s%c%s%c%d",
849 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
850 buf2, joiner, j);
851 buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
852 + strlen (buf1) + 1);
853 sprintf (buf, VTABLE_NAME_FORMAT, buf1);
854 name = get_identifier (buf);
855
856 /* If this name doesn't clash, then we can use it,
857 otherwise we add something different to the name until
858 it is unique. */
859 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
860
861 /* Hey, they really like MI don't they? Increase the 3
862 above to 6, and the 999 to 999999. :-) */
863 my_friendly_assert (j <= 999, 369);
864
865 break;
866 }
2636fde4
JM
867
868 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
869 new_buf2 = (char *) alloca (i);
870 sprintf (new_buf2, "%s%c%s",
871 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
872 buf2 = new_buf2;
873 }
8d08fdba 874
0352cfc8 875 new_decl = build_lang_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
8d08fdba 876 /* Remember which class this vtable is really for. */
8d08fdba
MS
877 DECL_CONTEXT (new_decl) = for_type;
878
da20811c 879 DECL_ARTIFICIAL (new_decl) = 1;
8d08fdba
MS
880 TREE_STATIC (new_decl) = 1;
881 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
882 DECL_VIRTUAL_P (new_decl) = 1;
883#ifndef WRITABLE_VTABLES
884 /* Make them READONLY by default. (mrs) */
885 TREE_READONLY (new_decl) = 1;
886#endif
887 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
888
889 /* Make fresh virtual list, so we can smash it later. */
890 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
db5ae43f
MS
891
892 if (TREE_VIA_VIRTUAL (binfo))
6b5fbb55
MS
893 {
894 tree binfo1 = binfo_member (BINFO_TYPE (binfo),
895 CLASSTYPE_VBASECLASSES (for_type));
896
897 /* XXX - This should never happen, if it does, the caller should
898 ensure that the binfo is from for_type's binfos, not from any
899 base type's. We can remove all this code after a while. */
900 if (binfo1 != binfo)
8251199e 901 warning ("internal inconsistency: binfo offset error for rtti");
6b5fbb55
MS
902
903 offset = BINFO_OFFSET (binfo1);
904 }
db5ae43f
MS
905 else
906 offset = BINFO_OFFSET (binfo);
907
f30432d7 908 set_rtti_entry (BINFO_VIRTUALS (binfo),
329745f7 909 ssize_binop (MINUS_EXPR, integer_zero_node, offset),
f30432d7 910 for_type);
8d08fdba
MS
911
912#ifdef GATHER_STATISTICS
913 n_vtables += 1;
914 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
915#endif
916
917 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
2455f26f 918 import_export_vtable (new_decl, for_type, 0);
8d08fdba
MS
919
920 if (TREE_VIA_VIRTUAL (binfo))
921 my_friendly_assert (binfo == binfo_member (BINFO_TYPE (binfo),
922 CLASSTYPE_VBASECLASSES (current_class_type)),
923 170);
924 SET_BINFO_NEW_VTABLE_MARKED (binfo);
8d08fdba
MS
925}
926
5566b478 927#if 0
8d08fdba
MS
928/* Access the virtual function table entry that logically
929 contains BASE_FNDECL. VIRTUALS is the virtual function table's
51c184be
MS
930 initializer. We can run off the end, when dealing with virtual
931 destructors in MI situations, return NULL_TREE in that case. */
e92cc029 932
8d08fdba
MS
933static tree
934get_vtable_entry (virtuals, base_fndecl)
935 tree virtuals, base_fndecl;
936{
f30432d7 937 unsigned HOST_WIDE_INT n = (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
8d08fdba
MS
938 ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
939 & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1))
940 : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
941
942#ifdef GATHER_STATISTICS
f30432d7 943 n_vtable_searches += n;
8d08fdba
MS
944#endif
945
f30432d7 946 while (n > 0 && virtuals)
8d08fdba 947 {
f30432d7 948 --n;
8d08fdba 949 virtuals = TREE_CHAIN (virtuals);
8d08fdba
MS
950 }
951 return virtuals;
952}
5566b478 953#endif
8d08fdba
MS
954
955/* Put new entry ENTRY into virtual function table initializer
956 VIRTUALS.
957
958 Also update DECL_VINDEX (FNDECL). */
959
960static void
961modify_vtable_entry (old_entry_in_list, new_entry, fndecl)
962 tree old_entry_in_list, new_entry, fndecl;
963{
7177d104 964 tree base_fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list)), 0);
8d08fdba
MS
965
966#ifdef NOTQUITE
8251199e 967 cp_warning ("replaced %D with %D", DECL_ASSEMBLER_NAME (base_fndecl),
7177d104 968 DECL_ASSEMBLER_NAME (fndecl));
8d08fdba 969#endif
8d08fdba 970 TREE_VALUE (old_entry_in_list) = new_entry;
8d08fdba 971
7177d104 972 /* Now assign virtual dispatch information, if unset. */
e92cc029 973 /* We can dispatch this, through any overridden base function. */
7177d104 974 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
8d08fdba 975 {
7177d104
MS
976 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
977 DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
8d08fdba 978 }
8d08fdba
MS
979}
980
9a3b49ac 981/* Access the virtual function table entry N. VIRTUALS is the virtual
8d08fdba 982 function table's initializer. */
e92cc029 983
8d08fdba 984static tree
f30432d7 985get_vtable_entry_n (virtuals, n)
8d08fdba 986 tree virtuals;
f30432d7 987 unsigned HOST_WIDE_INT n;
8d08fdba 988{
f30432d7 989 while (n > 0)
8d08fdba 990 {
f30432d7 991 --n;
8d08fdba 992 virtuals = TREE_CHAIN (virtuals);
8d08fdba
MS
993 }
994 return virtuals;
995}
996
7177d104
MS
997/* Add a virtual function to all the appropriate vtables for the class
998 T. DECL_VINDEX(X) should be error_mark_node, if we want to
999 allocate a new slot in our table. If it is error_mark_node, we
1000 know that no other function from another vtable is overridden by X.
1001 HAS_VIRTUAL keeps track of how many virtuals there are in our main
1002 vtable for the type, and we build upon the PENDING_VIRTUALS list
1003 and return it. */
e92cc029 1004
aa598818
JM
1005static void
1006add_virtual_function (pv, phv, has_virtual, fndecl, t)
1007 tree *pv, *phv;
8d08fdba 1008 int *has_virtual;
7177d104 1009 tree fndecl;
e92cc029 1010 tree t; /* Structure type. */
8d08fdba 1011{
aa598818
JM
1012 tree pending_virtuals = *pv;
1013 tree pending_hard_virtuals = *phv;
1014
8d08fdba
MS
1015 /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
1016 convert to void *. Make such a conversion here. */
700f8a87 1017 tree vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
8d08fdba
MS
1018 TREE_CONSTANT (vfn) = 1;
1019
7177d104
MS
1020#ifndef DUMB_USER
1021 if (current_class_type == 0)
8251199e 1022 cp_warning ("internal problem, current_class_type is zero when adding `%D', please report",
7177d104
MS
1023 fndecl);
1024 if (current_class_type && t != current_class_type)
8251199e 1025 cp_warning ("internal problem, current_class_type differs when adding `%D', please report",
7177d104
MS
1026 fndecl);
1027#endif
1028
8d08fdba
MS
1029 /* If the virtual function is a redefinition of a prior one,
1030 figure out in which base class the new definition goes,
1031 and if necessary, make a fresh virtual function table
1032 to hold that entry. */
7177d104 1033 if (DECL_VINDEX (fndecl) == error_mark_node)
8d08fdba 1034 {
8926095f 1035 tree entry;
8d08fdba 1036
6b5fbb55
MS
1037 /* We remember that this was the base sub-object for rtti. */
1038 CLASSTYPE_RTTI (t) = t;
8d08fdba 1039
f30432d7 1040 /* If we are using thunks, use two slots at the front, one
aff08c18
JM
1041 for the offset pointer, one for the tdesc pointer.
1042 For ARM-style vtables, use the same slot for both. */
1043 if (*has_virtual == 0 && ! CLASSTYPE_COM_INTERFACE (t))
f30432d7 1044 {
aff08c18
JM
1045 if (flag_vtable_thunks)
1046 *has_virtual = 2;
1047 else
1048 *has_virtual = 1;
f30432d7
MS
1049 }
1050
8d08fdba 1051 /* Build a new INT_CST for this DECL_VINDEX. */
8d08fdba
MS
1052 {
1053 static tree index_table[256];
e92cc029 1054 tree idx;
f30432d7 1055 /* We skip a slot for the offset/tdesc entry. */
aff08c18 1056 int i = (*has_virtual)++;
8d08fdba
MS
1057
1058 if (i >= 256 || index_table[i] == 0)
1059 {
e92cc029 1060 idx = build_int_2 (i, 0);
8d08fdba 1061 if (i < 256)
e92cc029 1062 index_table[i] = idx;
8d08fdba
MS
1063 }
1064 else
e92cc029 1065 idx = index_table[i];
8d08fdba 1066
e92cc029
MS
1067 /* Now assign virtual dispatch information. */
1068 DECL_VINDEX (fndecl) = idx;
7177d104 1069 DECL_CONTEXT (fndecl) = t;
8d08fdba 1070 }
8926095f 1071 entry = build_vtable_entry (integer_zero_node, vfn);
7177d104 1072 pending_virtuals = tree_cons (DECL_VINDEX (fndecl), entry, pending_virtuals);
8d08fdba 1073 }
7177d104
MS
1074 /* Might already be INTEGER_CST if declared twice in class. We will
1075 give error later or we've already given it. */
1076 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
8d08fdba
MS
1077 {
1078 /* Need an entry in some other virtual function table.
1079 Deal with this after we have laid out our virtual base classes. */
7177d104 1080 pending_hard_virtuals = temp_tree_cons (fndecl, vfn, pending_hard_virtuals);
8d08fdba 1081 }
aa598818
JM
1082 *pv = pending_virtuals;
1083 *phv = pending_hard_virtuals;
8d08fdba
MS
1084}
1085\f
1086/* Obstack on which to build the vector of class methods. */
1087struct obstack class_obstack;
1088extern struct obstack *current_obstack;
1089
61a127b3
MM
1090/* These are method vectors that were too small for the number of
1091 methods in some class, and so were abandoned. */
1092static tree free_method_vecs;
1093
1094/* Returns a method vector with enough room for N methods. N should
1095 be a power of two. */
1096
1097static tree
1098make_method_vec (n)
1099 int n;
1100{
1101 tree new_vec;
1102 tree* t;
1103
1104 for (t = &free_method_vecs; *t; t = &(TREE_CHAIN (*t)))
1105 /* Note that we don't use >= n here because we don't want to
1106 allocate a very large vector where it isn't needed. */
1107 if (TREE_VEC_LENGTH (*t) == n)
1108 {
1109 new_vec = *t;
1110 *t = TREE_CHAIN (new_vec);
1111 TREE_CHAIN (new_vec) = NULL_TREE;
1ddb2906 1112 bzero ((PTR) &TREE_VEC_ELT (new_vec, 0), n * sizeof (tree));
61a127b3
MM
1113 return new_vec;
1114 }
1115
1116 new_vec = make_tree_vec (n);
1117 return new_vec;
1118}
1119
1120/* Free the method vector VEC. */
1121
1122static void
1123free_method_vec (vec)
1124 tree vec;
1125{
1126 TREE_CHAIN (vec) = free_method_vecs;
1127 free_method_vecs = vec;
1128}
1129
6b4b3deb 1130/* Add method METHOD to class TYPE.
8d08fdba 1131
6b4b3deb
MM
1132 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1133 the class type where the method should be added. */
e92cc029 1134
8d08fdba
MS
1135void
1136add_method (type, fields, method)
1137 tree type, *fields, method;
1138{
8d08fdba 1139 push_obstacks (&permanent_obstack, &permanent_obstack);
8d08fdba 1140
61a127b3
MM
1141 /* Setting the DECL_CONTEXT and DECL_CLASS_CONTEXT here is probably
1142 redundant. */
1143 DECL_CONTEXT (method) = type;
1144 DECL_CLASS_CONTEXT (method) = type;
1145
8d08fdba 1146 if (fields && *fields)
61a127b3
MM
1147 *fields = build_overload (method, *fields);
1148 else
1149 {
1150 int len;
03017874 1151 int slot;
61a127b3
MM
1152 tree method_vec;
1153
1154 if (!CLASSTYPE_METHOD_VEC (type))
1155 /* Make a new method vector. We start with 8 entries. We must
1156 allocate at least two (for constructors and destructors), and
1157 we're going to end up with an assignment operator at some
1158 point as well.
1159
1160 We could use a TREE_LIST for now, and convert it to a
1161 TREE_VEC in finish_struct, but we would probably waste more
1162 memory making the links in the list than we would by
1163 over-allocating the size of the vector here. Furthermore,
1164 we would complicate all the code that expects this to be a
1165 vector. We keep a free list of vectors that we outgrew so
1166 that we don't really waste any memory. */
1167 CLASSTYPE_METHOD_VEC (type) = make_method_vec (8);
1168
1169 method_vec = CLASSTYPE_METHOD_VEC (type);
1170 len = TREE_VEC_LENGTH (method_vec);
1171
1172 if (DECL_NAME (method) == constructor_name (type))
03017874
MM
1173 /* A new constructor or destructor. Constructors go in
1174 slot 0; destructors go in slot 1. */
1175 slot = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
8d08fdba
MS
1176 else
1177 {
61a127b3 1178 /* See if we already have an entry with this name. */
03017874
MM
1179 for (slot = 2; slot < len; ++slot)
1180 if (!TREE_VEC_ELT (method_vec, slot)
1181 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1182 slot)))
61a127b3
MM
1183 == DECL_NAME (method)))
1184 break;
1185
03017874 1186 if (slot == len)
8d08fdba 1187 {
61a127b3
MM
1188 /* We need a bigger method vector. */
1189 tree new_vec = make_method_vec (2 * len);
1ddb2906
KG
1190 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1191 (PTR) &TREE_VEC_ELT (new_vec, 0),
61a127b3
MM
1192 len * sizeof (tree));
1193 free_method_vec (method_vec);
1194 len = 2 * len;
1195 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
8d08fdba 1196 }
61a127b3 1197
03017874 1198 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
8d08fdba 1199 {
61a127b3
MM
1200 /* Type conversion operators have to come before
1201 ordinary methods; add_conversions depends on this to
1202 speed up looking for conversion operators. So, if
1203 necessary, we slide some of the vector elements up.
1204 In theory, this makes this algorithm O(N^2) but we
1205 don't expect many conversion operators. */
03017874 1206 for (slot = 2; slot < len; ++slot)
8d08fdba 1207 {
03017874
MM
1208 tree fn = TREE_VEC_ELT (method_vec, slot);
1209
61a127b3
MM
1210 if (!fn)
1211 /* There are no more entries in the vector, so we
1212 can insert the new conversion operator here. */
1213 break;
03017874
MM
1214
1215 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1216 /* We can insert the new function right at the
1217 SLOTth position. */
61a127b3 1218 break;
8d08fdba 1219 }
03017874
MM
1220
1221 if (!TREE_VEC_ELT (method_vec, slot))
61a127b3
MM
1222 /* There is nothing in the Ith slot, so we can avoid
1223 moving anything. */
1224 ;
8d08fdba 1225 else
61a127b3
MM
1226 {
1227 /* We know the last slot in the vector is empty
03017874
MM
1228 because we know that at this point there's room
1229 for a new function. */
1230 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1231 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1232 (len - slot - 1) * sizeof (tree));
1233 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
61a127b3 1234 }
8d08fdba 1235 }
61a127b3
MM
1236 }
1237
03017874
MM
1238 if (template_class_depth (type))
1239 /* TYPE is a template class. Don't issue any errors now; wait
1240 until instantiation time to complain. */
1241 ;
1242 else
1243 {
1244 tree fns;
1245
1246 /* Check to see if we've already got this method. */
1247 for (fns = TREE_VEC_ELT (method_vec, slot);
1248 fns;
1249 fns = OVL_NEXT (fns))
1250 {
1251 tree fn = OVL_CURRENT (fns);
1252
1253 if (TREE_CODE (fn) != TREE_CODE (method))
1254 continue;
1255
1256 if (TREE_CODE (method) != TEMPLATE_DECL)
1257 {
1258 /* [over.load] Member function declarations with the
1259 same name and the same parameter types cannot be
1260 overloaded if any of them is a static member
1261 function declaration. */
1262 if (DECL_STATIC_FUNCTION_P (fn)
1263 != DECL_STATIC_FUNCTION_P (method))
1264 {
1265 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1266 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1267
1268 if (! DECL_STATIC_FUNCTION_P (fn))
1269 parms1 = TREE_CHAIN (parms1);
1270 else
1271 parms2 = TREE_CHAIN (parms2);
1272
1273 if (compparms (parms1, parms2))
1274 cp_error ("`%#D' and `%#D' cannot be overloaded",
1275 fn, method);
1276 }
1277
1278 /* Since this is an ordinary function in a
1279 non-template class, it's mangled name can be used
1280 as a unique identifier. This technique is only
1281 an optimization; we would get the same results if
1282 we just used decls_match here. */
1283 if (DECL_ASSEMBLER_NAME (fn)
1284 != DECL_ASSEMBLER_NAME (method))
1285 continue;
1286 }
1287 else if (!decls_match (fn, method))
1288 continue;
1289
1290 /* There has already been a declaration of this method
1291 or member template. */
1292 cp_error_at ("`%D' has already been declared in `%T'",
1293 method, type);
1294
1295 /* We don't call duplicate_decls here to merge the
1296 declarations because that will confuse things if the
8f032717 1297 methods have inline definitions. In particular, we
03017874
MM
1298 will crash while processing the definitions. */
1299 return;
1300 }
1301 }
1302
1303 /* Actually insert the new method. */
1304 TREE_VEC_ELT (method_vec, slot)
1305 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
8f032717
MM
1306
1307 /* Add the new binding. */
1308 if (!DECL_CONSTRUCTOR_P (method)
1309 && !DECL_DESTRUCTOR_P (method))
1310 push_class_level_binding (DECL_NAME (method),
1311 TREE_VEC_ELT (method_vec, slot));
8d08fdba 1312 }
8d08fdba
MS
1313 pop_obstacks ();
1314}
1315
1316/* Subroutines of finish_struct. */
1317
1318/* Look through the list of fields for this struct, deleting
1319 duplicates as we go. This must be recursive to handle
1320 anonymous unions.
1321
1322 FIELD is the field which may not appear anywhere in FIELDS.
1323 FIELD_PTR, if non-null, is the starting point at which
1324 chained deletions may take place.
1325 The value returned is the first acceptable entry found
1326 in FIELDS.
1327
1328 Note that anonymous fields which are not of UNION_TYPE are
1329 not duplicates, they are just anonymous fields. This happens
1330 when we have unnamed bitfields, for example. */
e92cc029 1331
8d08fdba 1332static tree
00595019
MS
1333delete_duplicate_fields_1 (field, fields)
1334 tree field, fields;
8d08fdba
MS
1335{
1336 tree x;
00595019 1337 tree prev = 0;
8d08fdba
MS
1338 if (DECL_NAME (field) == 0)
1339 {
1340 if (TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)
1341 return fields;
1342
1343 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
00595019 1344 fields = delete_duplicate_fields_1 (x, fields);
8d08fdba
MS
1345 return fields;
1346 }
1347 else
1348 {
1349 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1350 {
1351 if (DECL_NAME (x) == 0)
1352 {
1353 if (TREE_CODE (TREE_TYPE (x)) != UNION_TYPE)
1354 continue;
1355 TYPE_FIELDS (TREE_TYPE (x))
00595019 1356 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
8d08fdba
MS
1357 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1358 {
1359 if (prev == 0)
1360 fields = TREE_CHAIN (fields);
1361 else
1362 TREE_CHAIN (prev) = TREE_CHAIN (x);
1363 }
1364 }
1365 else
1366 {
1367 if (DECL_NAME (field) == DECL_NAME (x))
1368 {
1369 if (TREE_CODE (field) == CONST_DECL
1370 && TREE_CODE (x) == CONST_DECL)
8251199e 1371 cp_error_at ("duplicate enum value `%D'", x);
8d08fdba
MS
1372 else if (TREE_CODE (field) == CONST_DECL
1373 || TREE_CODE (x) == CONST_DECL)
8251199e 1374 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
8d08fdba 1375 x);
35acd3f2
MM
1376 else if (DECL_DECLARES_TYPE_P (field)
1377 && DECL_DECLARES_TYPE_P (x))
fc378698 1378 {
3bfdc719 1379 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
fc378698 1380 continue;
8251199e 1381 cp_error_at ("duplicate nested type `%D'", x);
fc378698 1382 }
35acd3f2
MM
1383 else if (DECL_DECLARES_TYPE_P (field)
1384 || DECL_DECLARES_TYPE_P (x))
5566b478
MS
1385 {
1386 /* Hide tag decls. */
1387 if ((TREE_CODE (field) == TYPE_DECL
1388 && DECL_ARTIFICIAL (field))
1389 || (TREE_CODE (x) == TYPE_DECL
1390 && DECL_ARTIFICIAL (x)))
1391 continue;
8251199e 1392 cp_error_at ("duplicate field `%D' (as type and non-type)",
5566b478
MS
1393 x);
1394 }
8d08fdba 1395 else
8251199e 1396 cp_error_at ("duplicate member `%D'", x);
8d08fdba
MS
1397 if (prev == 0)
1398 fields = TREE_CHAIN (fields);
1399 else
1400 TREE_CHAIN (prev) = TREE_CHAIN (x);
1401 }
1402 }
1403 }
1404 }
1405 return fields;
1406}
1407
1408static void
1409delete_duplicate_fields (fields)
1410 tree fields;
1411{
1412 tree x;
1413 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
00595019 1414 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
8d08fdba
MS
1415}
1416
79ad62b2
MM
1417/* Change the access of FDECL to ACCESS in T. The access to FDECL is
1418 along the path given by BINFO. Return 1 if change was legit,
1419 otherwise return 0. */
e92cc029 1420
8d08fdba 1421static int
79ad62b2 1422alter_access (t, binfo, fdecl, access)
8d08fdba 1423 tree t;
79ad62b2 1424 tree binfo;
8d08fdba 1425 tree fdecl;
be99da77 1426 tree access;
8d08fdba
MS
1427{
1428 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
38afd588 1429 if (elem)
8d08fdba 1430 {
38afd588 1431 if (TREE_VALUE (elem) != access)
8d08fdba 1432 {
38afd588 1433 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
8251199e 1434 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
38afd588 1435 else
8251199e 1436 error ("conflicting access specifications for field `%s', ignored",
38afd588 1437 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
8d08fdba
MS
1438 }
1439 else
430bb96b
JL
1440 {
1441 /* They're changing the access to the same thing they changed
1442 it to before. That's OK. */
1443 ;
1444 }
db5ae43f 1445 }
38afd588 1446 else
8d08fdba 1447 {
79ad62b2 1448 enforce_access (binfo, fdecl);
be99da77 1449 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
8d08fdba
MS
1450 return 1;
1451 }
1452 return 0;
1453}
1454
79ad62b2 1455/* Process the USING_DECL, which is a member of T. The METHOD_VEC, if
2036a15c 1456 non-NULL, is the methods of T. The FIELDS are the fields of T. */
79ad62b2 1457
e9659ab0 1458static void
79ad62b2
MM
1459handle_using_decl (using_decl, t, method_vec, fields)
1460 tree using_decl;
1461 tree t;
1462 tree method_vec;
1463 tree fields;
1464{
1465 tree ctype = DECL_INITIAL (using_decl);
1466 tree name = DECL_NAME (using_decl);
1467 tree access
1468 = TREE_PRIVATE (using_decl) ? access_private_node
1469 : TREE_PROTECTED (using_decl) ? access_protected_node
1470 : access_public_node;
1471 tree fdecl, binfo;
1472 tree flist = NULL_TREE;
1473 tree tmp;
1474 int i;
1475 int n_methods;
1476
1477 binfo = binfo_or_else (ctype, t);
1478 if (! binfo)
1479 return;
1480
1481 if (name == constructor_name (ctype)
1482 || name == constructor_name_full (ctype))
2036a15c
MM
1483 {
1484 cp_error_at ("using-declaration for constructor", using_decl);
1485 return;
1486 }
1487
79ad62b2
MM
1488 fdecl = lookup_member (binfo, name, 0, 0);
1489
1490 if (!fdecl)
1491 {
8251199e 1492 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
79ad62b2
MM
1493 return;
1494 }
1495
1496 /* Functions are represented as TREE_LIST, with the purpose
1497 being the type and the value the functions. Other members
1498 come as themselves. */
1499 if (TREE_CODE (fdecl) == TREE_LIST)
1500 /* Ignore base type this came from. */
1501 fdecl = TREE_VALUE (fdecl);
1502
1503 if (TREE_CODE (fdecl) == OVERLOAD)
1504 {
1505 /* We later iterate over all functions. */
1506 flist = fdecl;
1507 fdecl = OVL_FUNCTION (flist);
1508 }
1509
1510 name = DECL_NAME (fdecl);
1511 n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
61a127b3 1512 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); i++)
79ad62b2
MM
1513 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)))
1514 == name)
1515 {
8251199e
JM
1516 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1517 cp_error_at (" because of local method `%#D' with same name",
79ad62b2
MM
1518 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
1519 return;
1520 }
1c35f5b6
JM
1521
1522 if (! DECL_LANG_SPECIFIC (fdecl))
1523 /* We don't currently handle DECL_ACCESS for TYPE_DECLs; just return. */
1524 return;
79ad62b2
MM
1525
1526 for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
1527 if (DECL_NAME (tmp) == name)
1528 {
8251199e
JM
1529 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1530 cp_error_at (" because of local field `%#D' with same name", tmp);
79ad62b2
MM
1531 return;
1532 }
1533
1534 /* Make type T see field decl FDECL with access ACCESS.*/
1535 if (flist)
1536 {
1537 while (flist)
1538 {
1539 if (alter_access (t, binfo, OVL_FUNCTION (flist),
1540 access) == 0)
1541 return;
1542 flist = OVL_CHAIN (flist);
1543 }
1544 }
1545 else
1546 alter_access (t, binfo, fdecl, access);
1547}
8d08fdba
MS
1548\f
1549struct base_info
1550{
1551 int has_virtual;
1552 int max_has_virtual;
8d08fdba
MS
1553 tree vfield;
1554 tree vfields;
6b5fbb55 1555 tree rtti;
8d08fdba
MS
1556 char cant_have_default_ctor;
1557 char cant_have_const_ctor;
8d08fdba 1558 char no_const_asn_ref;
8d08fdba
MS
1559};
1560
1561/* Record information about type T derived from its base classes.
1562 Store most of that information in T itself, and place the
1563 remaining information in the struct BASE_INFO.
1564
1565 Propagate basetype offsets throughout the lattice. Note that the
1566 lattice topped by T is really a pair: it's a DAG that gives the
1567 structure of the derivation hierarchy, and it's a list of the
1568 virtual baseclasses that appear anywhere in the DAG. When a vbase
1569 type appears in the DAG, it's offset is 0, and it's children start
1570 their offsets from that point. When a vbase type appears in the list,
1571 its offset is the offset it has in the hierarchy, and its children's
1572 offsets include that offset in theirs.
1573
1574 Returns the index of the first base class to have virtual functions,
9a71c18b 1575 or -1 if no such base class. */
8d08fdba
MS
1576
1577static int
9a71c18b 1578finish_base_struct (t, b)
8d08fdba
MS
1579 tree t;
1580 struct base_info *b;
8d08fdba 1581{
9a71c18b 1582 tree binfos = TYPE_BINFO_BASETYPES (t);
8d08fdba
MS
1583 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1584 int first_vfn_base_index = -1;
1daa5dd8 1585 bzero ((char *) b, sizeof (struct base_info));
8d08fdba
MS
1586
1587 for (i = 0; i < n_baseclasses; i++)
1588 {
1589 tree base_binfo = TREE_VEC_ELT (binfos, i);
1590 tree basetype = BINFO_TYPE (base_binfo);
1591
9a71c18b
JM
1592 /* Effective C++ rule 14. We only need to check TYPE_VIRTUAL_P
1593 here because the case of virtual functions but non-virtual
1594 dtor is handled in finish_struct_1. */
1595 if (warn_ecpp && ! TYPE_VIRTUAL_P (basetype)
1596 && TYPE_HAS_DESTRUCTOR (basetype))
8251199e 1597 cp_warning ("base class `%#T' has a non-virtual destructor", basetype);
9a71c18b 1598
8d08fdba
MS
1599 /* If the type of basetype is incomplete, then
1600 we already complained about that fact
1601 (and we should have fixed it up as well). */
1602 if (TYPE_SIZE (basetype) == 0)
1603 {
1604 int j;
1605 /* The base type is of incomplete type. It is
1606 probably best to pretend that it does not
1607 exist. */
1608 if (i == n_baseclasses-1)
1609 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1610 TREE_VEC_LENGTH (binfos) -= 1;
1611 n_baseclasses -= 1;
1612 for (j = i; j+1 < n_baseclasses; j++)
1613 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1614 }
1615
e349ee73 1616 if (! TYPE_HAS_CONST_INIT_REF (basetype))
8d08fdba 1617 b->cant_have_const_ctor = 1;
8d08fdba
MS
1618
1619 if (TYPE_HAS_CONSTRUCTOR (basetype)
1620 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1621 {
1622 b->cant_have_default_ctor = 1;
1623 if (! TYPE_HAS_CONSTRUCTOR (t))
1624 {
8251199e 1625 cp_pedwarn ("base `%T' with only non-default constructor",
8d08fdba 1626 basetype);
8251199e 1627 cp_pedwarn ("in class without a constructor");
8d08fdba
MS
1628 }
1629 }
1630
1631 if (TYPE_HAS_ASSIGN_REF (basetype)
1632 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1633 b->no_const_asn_ref = 1;
8d08fdba 1634
8d08fdba
MS
1635 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1636 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1637 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
e8abc66f 1638 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
8d08fdba
MS
1639
1640 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1641 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1642 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1643
aff08c18
JM
1644 if (CLASSTYPE_COM_INTERFACE (basetype))
1645 {
1646 CLASSTYPE_COM_INTERFACE (t) = 1;
1647 if (i > 0)
1648 cp_error
1649 ("COM interface type `%T' must be the leftmost base class",
1650 basetype);
1651 }
1652 else if (CLASSTYPE_COM_INTERFACE (t))
1653 {
1654 cp_error ("COM interface type `%T' with non-COM base class `%T'",
1655 t, basetype);
1656 CLASSTYPE_COM_INTERFACE (t) = 0;
1657 }
1658
8d08fdba
MS
1659 if (TYPE_VIRTUAL_P (basetype))
1660 {
6b5fbb55
MS
1661 /* Ensure that this is set from at least a virtual base
1662 class. */
1663 if (b->rtti == NULL_TREE)
1664 b->rtti = CLASSTYPE_RTTI (basetype);
1665
8d08fdba
MS
1666 /* Don't borrow virtuals from virtual baseclasses. */
1667 if (TREE_VIA_VIRTUAL (base_binfo))
1668 continue;
1669
1670 if (first_vfn_base_index < 0)
1671 {
1672 tree vfields;
1673 first_vfn_base_index = i;
1674
7177d104
MS
1675 /* Update these two, now that we know what vtable we are
1676 going to extend. This is so that we can add virtual
1677 functions, and override them properly. */
9a71c18b
JM
1678 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1679 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
8d08fdba
MS
1680 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1681 b->vfield = CLASSTYPE_VFIELD (basetype);
1682 b->vfields = copy_list (CLASSTYPE_VFIELDS (basetype));
1683 vfields = b->vfields;
1684 while (vfields)
1685 {
1686 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1687 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1688 {
1689 tree value = VF_BASETYPE_VALUE (vfields);
1690 if (DECL_NAME (CLASSTYPE_VFIELD (value))
1691 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1692 VF_NORMAL_VALUE (b->vfields) = basetype;
1693 else
1694 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1695 }
1696 vfields = TREE_CHAIN (vfields);
1697 }
1698 CLASSTYPE_VFIELD (t) = b->vfield;
1699 }
1700 else
1701 {
1702 /* Only add unique vfields, and flatten them out as we go. */
1703 tree vfields = CLASSTYPE_VFIELDS (basetype);
1704 while (vfields)
1705 {
1706 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1707 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1708 {
1709 tree value = VF_BASETYPE_VALUE (vfields);
1710 b->vfields = tree_cons (base_binfo, value, b->vfields);
1711 if (DECL_NAME (CLASSTYPE_VFIELD (value))
1712 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1713 VF_NORMAL_VALUE (b->vfields) = basetype;
1714 else
1715 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1716 }
1717 vfields = TREE_CHAIN (vfields);
1718 }
1719
1720 if (b->has_virtual == 0)
1721 {
1722 first_vfn_base_index = i;
2986ae00
MS
1723
1724 /* Update these two, now that we know what vtable we are
1725 going to extend. This is so that we can add virtual
1726 functions, and override them properly. */
9a71c18b
JM
1727 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1728 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
8d08fdba
MS
1729 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1730 b->vfield = CLASSTYPE_VFIELD (basetype);
1731 CLASSTYPE_VFIELD (t) = b->vfield;
1732 /* When we install the first one, set the VF_NORMAL_VALUE
1733 to be the current class, as this it is the most derived
1734 class. Hopefully, this is not set to something else
1735 later. (mrs) */
1736 vfields = b->vfields;
1737 while (vfields)
1738 {
1739 if (DECL_NAME (CLASSTYPE_VFIELD (t))
1740 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1741 {
1742 VF_NORMAL_VALUE (vfields) = t;
1743 /* There should only be one of them! And it should
1744 always be found, if we get into here. (mrs) */
1745 break;
1746 }
1747 vfields = TREE_CHAIN (vfields);
1748 }
1749 }
1750 }
1751 }
1752 }
1753
8d08fdba
MS
1754 {
1755 tree vfields;
1756 /* Find the base class with the largest number of virtual functions. */
1757 for (vfields = b->vfields; vfields; vfields = TREE_CHAIN (vfields))
1758 {
1759 if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)) > b->max_has_virtual)
1760 b->max_has_virtual = CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields));
1761 if (VF_DERIVED_VALUE (vfields)
1762 && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)) > b->max_has_virtual)
1763 b->max_has_virtual = CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields));
1764 }
1765 }
1766
1767 if (b->vfield == 0)
1768 /* If all virtual functions come only from virtual baseclasses. */
1769 return -1;
6b5fbb55
MS
1770
1771 /* Update the rtti base if we have a non-virtual base class version
1772 of it. */
1773 b->rtti = CLASSTYPE_RTTI (BINFO_TYPE (TREE_VEC_ELT (binfos, first_vfn_base_index)));
1774
8d08fdba
MS
1775 return first_vfn_base_index;
1776}
8d08fdba
MS
1777\f
1778/* Set memoizing fields and bits of T (and its variants) for later use.
1779 MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables. */
e92cc029 1780
8d08fdba
MS
1781static void
1782finish_struct_bits (t, max_has_virtual)
1783 tree t;
1784 int max_has_virtual;
1785{
1786 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
8d08fdba
MS
1787
1788 /* Fix up variants (if any). */
1789 tree variants = TYPE_NEXT_VARIANT (t);
1790 while (variants)
1791 {
1792 /* These fields are in the _TYPE part of the node, not in
1793 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1794 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1795 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1796 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1797 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
1798
1799 TYPE_USES_COMPLEX_INHERITANCE (variants) = TYPE_USES_COMPLEX_INHERITANCE (t);
1800 TYPE_VIRTUAL_P (variants) = TYPE_VIRTUAL_P (t);
1801 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1802 /* Copy whatever these are holding today. */
1803 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1804 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
5566b478 1805 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
e92cc029 1806 TYPE_SIZE (variants) = TYPE_SIZE (t);
509087ae 1807 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
8d08fdba
MS
1808 variants = TYPE_NEXT_VARIANT (variants);
1809 }
1810
1811 if (n_baseclasses && max_has_virtual)
1812 {
9e0781b5
JM
1813 /* For a class w/o baseclasses, `finish_struct' has set
1814 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by definition). Similarly
1815 for a class who's base classes do not have vtables. When neither
1816 of these is true, we might have removed abstract virtuals (by
1817 providing a definition), added some (by declaring new ones), or
1818 redeclared ones from a base class. We need to recalculate what's
1819 really an abstract virtual at this point (by looking in the
1820 vtables). */
83f660b7 1821 CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
8d08fdba
MS
1822 }
1823
1824 if (n_baseclasses)
1825 {
1826 /* Notice whether this class has type conversion functions defined. */
1827 tree binfo = TYPE_BINFO (t);
1828 tree binfos = BINFO_BASETYPES (binfo);
1829 tree basetype;
1830
1831 for (i = n_baseclasses-1; i >= 0; i--)
1832 {
1833 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1834
0b41abe6 1835 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
8d08fdba
MS
1836 }
1837 }
1838
e8abc66f
MS
1839 /* If this type has a copy constructor, force its mode to be BLKmode, and
1840 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1841 be passed by invisible reference and prevent it from being returned in
72b7eeff
MS
1842 a register.
1843
1844 Also do this if the class has BLKmode but can still be returned in
1845 registers, since function_cannot_inline_p won't let us inline
1846 functions returning such a type. This affects the HP-PA. */
1847 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1848 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1849 && CLASSTYPE_NON_AGGREGATE (t)))
8d08fdba 1850 {
e8abc66f 1851 tree variants;
d2e5ee5c 1852 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
e8abc66f 1853 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
8d08fdba
MS
1854 {
1855 TYPE_MODE (variants) = BLKmode;
1856 TREE_ADDRESSABLE (variants) = 1;
8d08fdba
MS
1857 }
1858 }
1859}
1860
b0e0b31f
MM
1861/* Issue warnings about T having private constructors, but no friends,
1862 and so forth.
aed7b2a6 1863
b0e0b31f
MM
1864 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1865 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1866 non-private static member functions. */
1867
1868static void
1869maybe_warn_about_overly_private_class (t)
1870 tree t;
aed7b2a6 1871{
056a3b12
MM
1872 int has_member_fn = 0;
1873 int has_nonprivate_method = 0;
1874 tree fn;
1875
1876 if (!warn_ctor_dtor_privacy
b0e0b31f
MM
1877 /* If the class has friends, those entities might create and
1878 access instances, so we should not warn. */
056a3b12
MM
1879 || (CLASSTYPE_FRIEND_CLASSES (t)
1880 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
b0e0b31f
MM
1881 /* We will have warned when the template was declared; there's
1882 no need to warn on every instantiation. */
056a3b12
MM
1883 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1884 /* There's no reason to even consider warning about this
1885 class. */
1886 return;
1887
1888 /* We only issue one warning, if more than one applies, because
1889 otherwise, on code like:
1890
1891 class A {
1892 // Oops - forgot `public:'
1893 A();
1894 A(const A&);
1895 ~A();
1896 };
1897
1898 we warn several times about essentially the same problem. */
1899
1900 /* Check to see if all (non-constructor, non-destructor) member
1901 functions are private. (Since there are no friends or
1902 non-private statics, we can't ever call any of the private member
1903 functions.) */
1904 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1905 /* We're not interested in compiler-generated methods; they don't
1906 provide any way to call private members. */
1907 if (!DECL_ARTIFICIAL (fn))
1908 {
1909 if (!TREE_PRIVATE (fn))
b0e0b31f 1910 {
056a3b12
MM
1911 if (DECL_STATIC_FUNCTION_P (fn))
1912 /* A non-private static member function is just like a
1913 friend; it can create and invoke private member
1914 functions, and be accessed without a class
1915 instance. */
1916 return;
b0e0b31f 1917
056a3b12
MM
1918 has_nonprivate_method = 1;
1919 break;
1920 }
ce0a5952 1921 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
056a3b12
MM
1922 has_member_fn = 1;
1923 }
aed7b2a6 1924
056a3b12
MM
1925 if (!has_nonprivate_method && has_member_fn)
1926 {
ce0a5952
MM
1927 /* There are no non-private methods, and there's at least one
1928 private member function that isn't a constructor or
1929 destructor. (If all the private members are
1930 constructors/destructors we want to use the code below that
1931 issues error messages specifically referring to
1932 constructors/destructors.) */
056a3b12
MM
1933 int i;
1934 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1935 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1936 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1937 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1938 {
1939 has_nonprivate_method = 1;
1940 break;
1941 }
1942 if (!has_nonprivate_method)
b0e0b31f 1943 {
056a3b12
MM
1944 cp_warning ("all member functions in class `%T' are private", t);
1945 return;
b0e0b31f 1946 }
056a3b12 1947 }
aed7b2a6 1948
056a3b12
MM
1949 /* Even if some of the member functions are non-private, the class
1950 won't be useful for much if all the constructors or destructors
1951 are private: such an object can never be created or destroyed. */
1952 if (TYPE_HAS_DESTRUCTOR (t))
1953 {
1954 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
b0e0b31f 1955
056a3b12
MM
1956 if (TREE_PRIVATE (dtor))
1957 {
1958 cp_warning ("`%#T' only defines a private destructor and has no friends",
1959 t);
1960 return;
b0e0b31f 1961 }
056a3b12 1962 }
b0e0b31f 1963
056a3b12
MM
1964 if (TYPE_HAS_CONSTRUCTOR (t))
1965 {
1966 int nonprivate_ctor = 0;
b0e0b31f 1967
056a3b12
MM
1968 /* If a non-template class does not define a copy
1969 constructor, one is defined for it, enabling it to avoid
1970 this warning. For a template class, this does not
1971 happen, and so we would normally get a warning on:
b0e0b31f 1972
056a3b12 1973 template <class T> class C { private: C(); };
b0e0b31f 1974
056a3b12
MM
1975 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1976 complete non-template or fully instantiated classes have this
1977 flag set. */
1978 if (!TYPE_HAS_INIT_REF (t))
1979 nonprivate_ctor = 1;
1980 else
1981 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
1982 fn;
1983 fn = OVL_NEXT (fn))
1984 {
1985 tree ctor = OVL_CURRENT (fn);
1986 /* Ideally, we wouldn't count copy constructors (or, in
1987 fact, any constructor that takes an argument of the
1988 class type as a parameter) because such things cannot
1989 be used to construct an instance of the class unless
1990 you already have one. But, for now at least, we're
1991 more generous. */
1992 if (! TREE_PRIVATE (ctor))
b0e0b31f 1993 {
056a3b12
MM
1994 nonprivate_ctor = 1;
1995 break;
b0e0b31f 1996 }
056a3b12 1997 }
aed7b2a6 1998
056a3b12
MM
1999 if (nonprivate_ctor == 0)
2000 {
2001 cp_warning ("`%#T' only defines private constructors and has no friends",
2002 t);
2003 return;
b0e0b31f
MM
2004 }
2005 }
aed7b2a6
MM
2006}
2007
b0e0b31f 2008
8d08fdba
MS
2009/* Warn about duplicate methods in fn_fields. Also compact method
2010 lists so that lookup can be made faster.
2011
8d08fdba
MS
2012 Data Structure: List of method lists. The outer list is a
2013 TREE_LIST, whose TREE_PURPOSE field is the field name and the
e1cd6e56
MS
2014 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2015 links the entire list of methods for TYPE_METHODS. Friends are
2016 chained in the same way as member functions (? TREE_CHAIN or
2017 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2018 list. That allows them to be quickly deleted, and requires no
2019 extra storage.
8d08fdba
MS
2020
2021 If there are any constructors/destructors, they are moved to the
2022 front of the list. This makes pushclass more efficient.
2023
2024 We also link each field which has shares a name with its baseclass
2025 to the head of the list of fields for that base class. This allows
2026 us to reduce search time in places like `build_method_call' to
b0e0b31f 2027 consider only reasonably likely functions. */
8d08fdba 2028
b0e0b31f
MM
2029static void
2030finish_struct_methods (t)
8d08fdba 2031 tree t;
8d08fdba 2032{
b0e0b31f 2033 tree fn_fields;
61a127b3 2034 tree method_vec = CLASSTYPE_METHOD_VEC (t);
fc378698 2035 tree ctor_name = constructor_name (t);
8d08fdba 2036
fc378698
MS
2037 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2038 and the next few with type conversion operators (if any). */
b0e0b31f
MM
2039 for (fn_fields = TYPE_METHODS (t); fn_fields;
2040 fn_fields = TREE_CHAIN (fn_fields))
8d08fdba 2041 {
8d08fdba 2042 tree fn_name = DECL_NAME (fn_fields);
8d08fdba 2043
8d08fdba
MS
2044 /* Clear out this flag.
2045
2046 @@ Doug may figure out how to break
2047 @@ this with nested classes and friends. */
2048 DECL_IN_AGGR_P (fn_fields) = 0;
2049
2050 /* Note here that a copy ctor is private, so we don't dare generate
2051 a default copy constructor for a class that has a member
2052 of this type without making sure they have access to it. */
fc378698 2053 if (fn_name == ctor_name)
8d08fdba
MS
2054 {
2055 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2056 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2057
2058 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2059 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2060 {
2061 if (TREE_CHAIN (parmtypes) == NULL_TREE
2062 || TREE_CHAIN (parmtypes) == void_list_node
2063 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2064 {
2065 if (TREE_PROTECTED (fn_fields))
2066 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2067 else if (TREE_PRIVATE (fn_fields))
2068 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2069 }
2070 }
61a127b3
MM
2071 }
2072 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
8d08fdba
MS
2073 {
2074 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2075
a292b002 2076 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
8d08fdba
MS
2077 {
2078 if (TREE_PROTECTED (fn_fields))
2079 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2080 else if (TREE_PRIVATE (fn_fields))
2081 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2082 }
2083 }
8d08fdba
MS
2084 }
2085
b0e0b31f
MM
2086 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2087 /* We thought there was a destructor, but there wasn't. Some
2088 parse errors cause this anomalous situation. */
2089 TYPE_HAS_DESTRUCTOR (t) = 0;
2090
2091 /* Issue warnings about private constructors and such. If there are
2092 no methods, then some public defaults are generated. */
2093 maybe_warn_about_overly_private_class (t);
8d08fdba
MS
2094}
2095
e92cc029 2096/* Emit error when a duplicate definition of a type is seen. Patch up. */
8d08fdba
MS
2097
2098void
2099duplicate_tag_error (t)
2100 tree t;
2101{
8251199e
JM
2102 cp_error ("redefinition of `%#T'", t);
2103 cp_error_at ("previous definition here", t);
8d08fdba
MS
2104
2105 /* Pretend we haven't defined this type. */
2106
2107 /* All of the component_decl's were TREE_CHAINed together in the parser.
2108 finish_struct_methods walks these chains and assembles all methods with
2109 the same base name into DECL_CHAINs. Now we don't need the parser chains
e92cc029
MS
2110 anymore, so we unravel them. */
2111
2112 /* This used to be in finish_struct, but it turns out that the
2113 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2114 things... */
fc378698 2115 if (CLASSTYPE_METHOD_VEC (t))
8d08fdba 2116 {
fc378698
MS
2117 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2118 int i, len = TREE_VEC_LENGTH (method_vec);
8d08fdba
MS
2119 for (i = 0; i < len; i++)
2120 {
fc378698 2121 tree unchain = TREE_VEC_ELT (method_vec, i);
8d08fdba
MS
2122 while (unchain != NULL_TREE)
2123 {
2c73f9f5
ML
2124 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2125 unchain = OVL_NEXT (unchain);
8d08fdba
MS
2126 }
2127 }
2128 }
2129
2130 if (TYPE_LANG_SPECIFIC (t))
2131 {
8d08fdba 2132 tree binfo = TYPE_BINFO (t);
8d08fdba
MS
2133 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2134 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2135
1daa5dd8 2136 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
8d08fdba
MS
2137 BINFO_BASETYPES(binfo) = NULL_TREE;
2138
8d08fdba 2139 TYPE_BINFO (t) = binfo;
8d08fdba
MS
2140 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2141 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
8d08fdba
MS
2142 TYPE_REDEFINED (t) = 1;
2143 }
2144 TYPE_SIZE (t) = NULL_TREE;
2145 TYPE_MODE (t) = VOIDmode;
2146 TYPE_FIELDS (t) = NULL_TREE;
2147 TYPE_METHODS (t) = NULL_TREE;
2148 TYPE_VFIELD (t) = NULL_TREE;
2149 TYPE_CONTEXT (t) = NULL_TREE;
2150}
2151
e92cc029
MS
2152/* finish up all new vtables. */
2153
7177d104
MS
2154static void
2155finish_vtbls (binfo, do_self, t)
6b5fbb55 2156 tree binfo;
7177d104 2157 int do_self;
6b5fbb55 2158 tree t;
7177d104
MS
2159{
2160 tree binfos = BINFO_BASETYPES (binfo);
2161 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2162
2163 /* Should we use something besides CLASSTYPE_VFIELDS? */
2164 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2165 {
2166 if (BINFO_NEW_VTABLE_MARKED (binfo))
2167 {
2168 tree decl, context;
2169
2170 decl = BINFO_VTABLE (binfo);
2171 context = DECL_CONTEXT (decl);
2172 DECL_CONTEXT (decl) = 0;
56ae6d77 2173 if (DECL_INITIAL (decl) != BINFO_VIRTUALS (binfo))
7177d104
MS
2174 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE,
2175 BINFO_VIRTUALS (binfo));
b3417a04 2176 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0);
7177d104
MS
2177 DECL_CONTEXT (decl) = context;
2178 }
2179 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2180 }
2181
2182 for (i = 0; i < n_baselinks; i++)
2183 {
2184 tree base_binfo = TREE_VEC_ELT (binfos, i);
beb53fb8
JM
2185 int is_not_base_vtable
2186 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
7177d104
MS
2187 if (TREE_VIA_VIRTUAL (base_binfo))
2188 {
2189 base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2190 }
44a8d0b3 2191 finish_vtbls (base_binfo, is_not_base_vtable, t);
7177d104
MS
2192 }
2193}
2194
2195/* True if we should override the given BASE_FNDECL with the given
2196 FNDECL. */
e92cc029 2197
7177d104
MS
2198static int
2199overrides (fndecl, base_fndecl)
2200 tree fndecl, base_fndecl;
2201{
e92cc029 2202 /* Destructors have special names. */
beb53fb8
JM
2203 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2204 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
7177d104 2205 return 1;
beb53fb8
JM
2206 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2207 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
7177d104
MS
2208 return 0;
2209 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2210 {
5566b478 2211 tree types, base_types;
7177d104
MS
2212#if 0
2213 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2214 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2215#endif
2216 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2217 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
91063b51
MM
2218 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2219 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2220 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
7177d104
MS
2221 return 1;
2222 }
2223 return 0;
2224}
2225
a292b002
MS
2226static tree
2227get_class_offset_1 (parent, binfo, context, t, fndecl)
2228 tree parent, binfo, context, t, fndecl;
2229{
2230 tree binfos = BINFO_BASETYPES (binfo);
2231 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2232 tree rval = NULL_TREE;
2233
2234 if (binfo == parent)
2235 return error_mark_node;
2236
2237 for (i = 0; i < n_baselinks; i++)
2238 {
2239 tree base_binfo = TREE_VEC_ELT (binfos, i);
2240 tree nrval;
2241
2242 if (TREE_VIA_VIRTUAL (base_binfo))
2243 base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2244 CLASSTYPE_VBASECLASSES (t));
2245 nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl);
2246 /* See if we have a new value */
2247 if (nrval && (nrval != error_mark_node || rval==0))
2248 {
2249 /* Only compare if we have two offsets */
2250 if (rval && rval != error_mark_node
2251 && ! tree_int_cst_equal (nrval, rval))
2252 {
2253 /* Only give error if the two offsets are different */
8251199e
JM
2254 error ("every virtual function must have a unique final overrider");
2255 cp_error (" found two (or more) `%T' class subobjects in `%T'", context, t);
2256 cp_error (" with virtual `%D' from virtual base class", fndecl);
a292b002
MS
2257 return rval;
2258 }
2259 rval = nrval;
2260 }
2261
2262 if (rval && BINFO_TYPE (binfo) == context)
2263 {
2264 my_friendly_assert (rval == error_mark_node
2265 || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999);
2266 rval = BINFO_OFFSET (binfo);
2267 }
2268 }
2269 return rval;
2270}
2271
2272/* Get the offset to the CONTEXT subobject that is related to the
2273 given BINFO. */
e92cc029 2274
a292b002
MS
2275static tree
2276get_class_offset (context, t, binfo, fndecl)
2277 tree context, t, binfo, fndecl;
2278{
2279 tree first_binfo = binfo;
2280 tree offset;
2281 int i;
2282
2283 if (context == t)
2284 return integer_zero_node;
2285
2286 if (BINFO_TYPE (binfo) == context)
2287 return BINFO_OFFSET (binfo);
2288
2289 /* Check less derived binfos first. */
2290 while (BINFO_BASETYPES (binfo)
2291 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
2292 {
2293 tree binfos = BINFO_BASETYPES (binfo);
2294 binfo = TREE_VEC_ELT (binfos, i);
2295 if (BINFO_TYPE (binfo) == context)
2296 return BINFO_OFFSET (binfo);
2297 }
2298
2299 /* Ok, not found in the less derived binfos, now check the more
e92cc029 2300 derived binfos. */
a292b002
MS
2301 offset = get_class_offset_1 (first_binfo, TYPE_BINFO (t), context, t, fndecl);
2302 if (offset==0 || TREE_CODE (offset) != INTEGER_CST)
2303 my_friendly_abort (999); /* we have to find it. */
2304 return offset;
2305}
2306
f30432d7 2307/* Skip RTTI information at the front of the virtual list. */
e92cc029 2308
f30432d7 2309unsigned HOST_WIDE_INT
aff08c18
JM
2310skip_rtti_stuff (virtuals, t)
2311 tree *virtuals, t;
f30432d7
MS
2312{
2313 int n;
2314
aff08c18
JM
2315 if (CLASSTYPE_COM_INTERFACE (t))
2316 return 0;
2317
f30432d7
MS
2318 n = 0;
2319 if (*virtuals)
2320 {
2321 /* We always reserve a slot for the offset/tdesc entry. */
2322 ++n;
2323 *virtuals = TREE_CHAIN (*virtuals);
2324 }
2325 if (flag_vtable_thunks && *virtuals)
2326 {
2327 /* The second slot is reserved for the tdesc pointer when thunks
2328 are used. */
2329 ++n;
2330 *virtuals = TREE_CHAIN (*virtuals);
2331 }
2332 return n;
2333}
2334
7177d104
MS
2335static void
2336modify_one_vtable (binfo, t, fndecl, pfn)
2337 tree binfo, t, fndecl, pfn;
2338{
39211cd5 2339 tree virtuals = BINFO_VIRTUALS (binfo);
7177d104
MS
2340 unsigned HOST_WIDE_INT n;
2341
db5ae43f
MS
2342 /* update rtti entry */
2343 if (flag_rtti)
2344 {
2345 if (binfo == TYPE_BINFO (t))
2346 {
2347 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2348 build_vtable (TYPE_BINFO (DECL_CONTEXT (CLASSTYPE_VFIELD (t))), t);
2349 }
2350 else
2351 {
2352 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2353 prepare_fresh_vtable (binfo, t);
2354 }
db5ae43f 2355 }
f30432d7
MS
2356 if (fndecl == NULL_TREE)
2357 return;
2358
aff08c18 2359 n = skip_rtti_stuff (&virtuals, t);
db5ae43f 2360
7177d104
MS
2361 while (virtuals)
2362 {
2363 tree current_fndecl = TREE_VALUE (virtuals);
2364 current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl);
2365 current_fndecl = TREE_OPERAND (current_fndecl, 0);
2366 if (current_fndecl && overrides (fndecl, current_fndecl))
2367 {
2368 tree base_offset, offset;
2369 tree context = DECL_CLASS_CONTEXT (fndecl);
2370 tree vfield = CLASSTYPE_VFIELD (t);
2371 tree this_offset;
2372
a292b002 2373 offset = get_class_offset (context, t, binfo, fndecl);
7177d104 2374
2986ae00
MS
2375 /* Find the right offset for the this pointer based on the
2376 base class we just found. We have to take into
2377 consideration the virtual base class pointers that we
a0a33927
MS
2378 stick in before the virtual function table pointer.
2379
ddd5a7c1 2380 Also, we want just the delta between the most base class
a0a33927
MS
2381 that we derived this vfield from and us. */
2382 base_offset = size_binop (PLUS_EXPR,
13306d4f 2383 get_derived_offset (binfo, DECL_CONTEXT (current_fndecl)),
a0a33927 2384 BINFO_OFFSET (binfo));
329745f7 2385 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
7177d104 2386
7177d104
MS
2387 if (binfo == TYPE_BINFO (t))
2388 {
2389 /* In this case, it is *type*'s vtable we are modifying.
2390 We start with the approximation that it's vtable is that
2391 of the immediate base class. */
2392 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2393 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2394 }
2395 else
2396 {
2397 /* This is our very own copy of `basetype' to play with.
2398 Later, we will fill in all the virtual functions
2399 that override the virtual functions in these base classes
2400 which are not defined by the current type. */
2401 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2402 prepare_fresh_vtable (binfo, t);
2403 }
2404
2405#ifdef NOTQUITE
8251199e 2406 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
7177d104
MS
2407#endif
2408 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2409 build_vtable_entry (this_offset, pfn),
2410 fndecl);
2411 }
2412 ++n;
2413 virtuals = TREE_CHAIN (virtuals);
2414 }
2415}
2416
e92cc029
MS
2417/* These are the ones that are not through virtual base classes. */
2418
7177d104
MS
2419static void
2420modify_all_direct_vtables (binfo, do_self, t, fndecl, pfn)
6b5fbb55 2421 tree binfo;
7177d104 2422 int do_self;
6b5fbb55 2423 tree t, fndecl, pfn;
7177d104
MS
2424{
2425 tree binfos = BINFO_BASETYPES (binfo);
2426 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2427
2428 /* Should we use something besides CLASSTYPE_VFIELDS? */
2429 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2430 {
2431 modify_one_vtable (binfo, t, fndecl, pfn);
2432 }
2433
2434 for (i = 0; i < n_baselinks; i++)
2435 {
2436 tree base_binfo = TREE_VEC_ELT (binfos, i);
beb53fb8
JM
2437 int is_not_base_vtable
2438 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
7177d104 2439 if (! TREE_VIA_VIRTUAL (base_binfo))
44a8d0b3 2440 modify_all_direct_vtables (base_binfo, is_not_base_vtable, t, fndecl, pfn);
7177d104
MS
2441 }
2442}
2443
43f2999d 2444/* Fixup all the delta entries in this one vtable that need updating. */
e92cc029 2445
21474714 2446static void
43f2999d 2447fixup_vtable_deltas1 (binfo, t)
21474714
MS
2448 tree binfo, t;
2449{
2450 tree virtuals = BINFO_VIRTUALS (binfo);
2451 unsigned HOST_WIDE_INT n;
2452
aff08c18 2453 n = skip_rtti_stuff (&virtuals, t);
f30432d7 2454
21474714
MS
2455 while (virtuals)
2456 {
2457 tree fndecl = TREE_VALUE (virtuals);
2458 tree pfn = FNADDR_FROM_VTABLE_ENTRY (fndecl);
2459 tree delta = DELTA_FROM_VTABLE_ENTRY (fndecl);
2460 fndecl = TREE_OPERAND (pfn, 0);
2461 if (fndecl)
2462 {
2463 tree base_offset, offset;
2464 tree context = DECL_CLASS_CONTEXT (fndecl);
2465 tree vfield = CLASSTYPE_VFIELD (t);
2466 tree this_offset;
2467
a292b002 2468 offset = get_class_offset (context, t, binfo, fndecl);
21474714
MS
2469
2470 /* Find the right offset for the this pointer based on the
2471 base class we just found. We have to take into
2472 consideration the virtual base class pointers that we
2473 stick in before the virtual function table pointer.
2474
ddd5a7c1 2475 Also, we want just the delta between the most base class
21474714
MS
2476 that we derived this vfield from and us. */
2477 base_offset = size_binop (PLUS_EXPR,
329745f7
JM
2478 get_derived_offset (binfo,
2479 DECL_CONTEXT (fndecl)),
21474714 2480 BINFO_OFFSET (binfo));
329745f7 2481 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
21474714
MS
2482
2483 if (! tree_int_cst_equal (this_offset, delta))
2484 {
2485 /* Make sure we can modify the derived association with immunity. */
21474714
MS
2486 if (binfo == TYPE_BINFO (t))
2487 {
2488 /* In this case, it is *type*'s vtable we are modifying.
2489 We start with the approximation that it's vtable is that
2490 of the immediate base class. */
2491 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2492 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2493 }
2494 else
2495 {
2496 /* This is our very own copy of `basetype' to play with.
2497 Later, we will fill in all the virtual functions
2498 that override the virtual functions in these base classes
2499 which are not defined by the current type. */
2500 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2501 prepare_fresh_vtable (binfo, t);
2502 }
2503
2504 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2505 build_vtable_entry (this_offset, pfn),
2506 fndecl);
2507 }
2508 }
2509 ++n;
2510 virtuals = TREE_CHAIN (virtuals);
2511 }
2512}
2513
43f2999d
MS
2514/* Fixup all the delta entries in all the direct vtables that need updating.
2515 This happens when we have non-overridden virtual functions from a
2516 virtual base class, that are at a different offset, in the new
2517 hierarchy, because the layout of the virtual bases has changed. */
e92cc029 2518
43f2999d
MS
2519static void
2520fixup_vtable_deltas (binfo, init_self, t)
6b5fbb55 2521 tree binfo;
43f2999d 2522 int init_self;
6b5fbb55 2523 tree t;
43f2999d
MS
2524{
2525 tree binfos = BINFO_BASETYPES (binfo);
2526 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2527
2528 for (i = 0; i < n_baselinks; i++)
2529 {
2530 tree base_binfo = TREE_VEC_ELT (binfos, i);
beb53fb8
JM
2531 int is_not_base_vtable
2532 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
43f2999d
MS
2533 if (! TREE_VIA_VIRTUAL (base_binfo))
2534 fixup_vtable_deltas (base_binfo, is_not_base_vtable, t);
2535 }
2536 /* Should we use something besides CLASSTYPE_VFIELDS? */
2537 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2538 {
2539 fixup_vtable_deltas1 (binfo, t);
2540 }
2541}
2542
e92cc029
MS
2543/* These are the ones that are through virtual base classes. */
2544
7177d104
MS
2545static void
2546modify_all_indirect_vtables (binfo, do_self, via_virtual, t, fndecl, pfn)
6b5fbb55 2547 tree binfo;
7177d104 2548 int do_self, via_virtual;
6b5fbb55 2549 tree t, fndecl, pfn;
7177d104
MS
2550{
2551 tree binfos = BINFO_BASETYPES (binfo);
2552 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2553
2554 /* Should we use something besides CLASSTYPE_VFIELDS? */
2555 if (do_self && via_virtual && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2556 {
2557 modify_one_vtable (binfo, t, fndecl, pfn);
2558 }
2559
2560 for (i = 0; i < n_baselinks; i++)
2561 {
2562 tree base_binfo = TREE_VEC_ELT (binfos, i);
beb53fb8
JM
2563 int is_not_base_vtable
2564 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
7177d104
MS
2565 if (TREE_VIA_VIRTUAL (base_binfo))
2566 {
2567 via_virtual = 1;
2568 base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2569 }
2570 modify_all_indirect_vtables (base_binfo, is_not_base_vtable, via_virtual, t, fndecl, pfn);
2571 }
2572}
2573
2574static void
2575modify_all_vtables (t, fndecl, vfn)
2576 tree t, fndecl, vfn;
2577{
2578 /* Do these first, so that we will make use of any non-virtual class's
e92cc029 2579 vtable, over a virtual classes vtable. */
7177d104
MS
2580 modify_all_direct_vtables (TYPE_BINFO (t), 1, t, fndecl, vfn);
2581 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
2582 modify_all_indirect_vtables (TYPE_BINFO (t), 1, 0, t, fndecl, vfn);
2583}
2584
39211cd5
MS
2585/* Here, we already know that they match in every respect.
2586 All we have to check is where they had their declarations. */
e92cc029 2587
39211cd5
MS
2588static int
2589strictly_overrides (fndecl1, fndecl2)
2590 tree fndecl1, fndecl2;
2591{
2592 int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
2593 DECL_CLASS_CONTEXT (fndecl1),
2594 0, (tree *)0);
2595 if (distance == -2 || distance > 0)
2596 return 1;
2597 return 0;
2598}
2599
2600/* Merge overrides for one vtable.
2601 If we want to merge in same function, we are fine.
2602 else
2603 if one has a DECL_CLASS_CONTEXT that is a parent of the
2604 other, than choose the more derived one
2605 else
2606 potentially ill-formed (see 10.3 [class.virtual])
2607 we have to check later to see if there was an
2608 override in this class. If there was ok, if not
2609 then it is ill-formed. (mrs)
2610
2611 We take special care to reuse a vtable, if we can. */
e92cc029 2612
39211cd5
MS
2613static void
2614override_one_vtable (binfo, old, t)
2615 tree binfo, old, t;
2616{
2617 tree virtuals = BINFO_VIRTUALS (binfo);
2618 tree old_virtuals = BINFO_VIRTUALS (old);
2619 enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
2620
2621 /* If we have already committed to modifying it, then don't try and
e92cc029 2622 reuse another vtable. */
39211cd5
MS
2623 if (BINFO_NEW_VTABLE_MARKED (binfo))
2624 choose = NEITHER;
2625
aff08c18
JM
2626 skip_rtti_stuff (&virtuals, t);
2627 skip_rtti_stuff (&old_virtuals, t);
39211cd5
MS
2628
2629 while (virtuals)
2630 {
2631 tree fndecl = TREE_VALUE (virtuals);
2632 tree old_fndecl = TREE_VALUE (old_virtuals);
2633 fndecl = FNADDR_FROM_VTABLE_ENTRY (fndecl);
2634 old_fndecl = FNADDR_FROM_VTABLE_ENTRY (old_fndecl);
2635 fndecl = TREE_OPERAND (fndecl, 0);
2636 old_fndecl = TREE_OPERAND (old_fndecl, 0);
e92cc029 2637 /* First check to see if they are the same. */
39211cd5
MS
2638 if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
2639 {
e92cc029 2640 /* No need to do anything. */
39211cd5
MS
2641 }
2642 else if (strictly_overrides (fndecl, old_fndecl))
2643 {
2644 if (choose == UNDECIDED)
2645 choose = REUSE_NEW;
2646 else if (choose == REUSE_OLD)
2647 {
2648 choose = NEITHER;
2649 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2650 {
2651 prepare_fresh_vtable (binfo, t);
2652 override_one_vtable (binfo, old, t);
2653 return;
2654 }
2655 }
2656 }
2657 else if (strictly_overrides (old_fndecl, fndecl))
2658 {
2659 if (choose == UNDECIDED)
2660 choose = REUSE_OLD;
2661 else if (choose == REUSE_NEW)
2662 {
2663 choose = NEITHER;
2664 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2665 {
2666 prepare_fresh_vtable (binfo, t);
2667 override_one_vtable (binfo, old, t);
2668 return;
2669 }
a0a33927 2670 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
39211cd5 2671 }
a3203465
MS
2672 else if (choose == NEITHER)
2673 {
2674 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2675 }
39211cd5
MS
2676 }
2677 else
2678 {
2679 choose = NEITHER;
2680 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2681 {
2682 prepare_fresh_vtable (binfo, t);
2683 override_one_vtable (binfo, old, t);
2684 return;
2685 }
2686 {
ddd5a7c1 2687 /* This MUST be overridden, or the class is ill-formed. */
39211cd5
MS
2688 tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
2689 tree vfn;
2690
2691 fndecl = copy_node (fndecl);
2692 copy_lang_decl (fndecl);
4a67c9e9 2693 DECL_NEEDS_FINAL_OVERRIDER_P (fndecl) = 1;
e92cc029 2694 /* Make sure we search for it later. */
39211cd5
MS
2695 if (! CLASSTYPE_ABSTRACT_VIRTUALS (t))
2696 CLASSTYPE_ABSTRACT_VIRTUALS (t) = error_mark_node;
2697
700f8a87 2698 vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
39211cd5
MS
2699 TREE_CONSTANT (vfn) = 1;
2700
38e01259 2701 /* We can use integer_zero_node, as we will core dump
e92cc029 2702 if this is used anyway. */
39211cd5
MS
2703 TREE_VALUE (virtuals) = build_vtable_entry (integer_zero_node, vfn);
2704 }
2705 }
2706 virtuals = TREE_CHAIN (virtuals);
2707 old_virtuals = TREE_CHAIN (old_virtuals);
2708 }
2709
e92cc029 2710 /* Let's reuse the old vtable. */
39211cd5
MS
2711 if (choose == REUSE_OLD)
2712 {
2713 BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
2714 BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
2715 }
2716}
2717
2718/* Merge in overrides for virtual bases.
2719 BINFO is the hierarchy we want to modify, and OLD has the potential
2720 overrides. */
e92cc029 2721
39211cd5
MS
2722static void
2723merge_overrides (binfo, old, do_self, t)
6b5fbb55 2724 tree binfo, old;
39211cd5 2725 int do_self;
6b5fbb55 2726 tree t;
39211cd5
MS
2727{
2728 tree binfos = BINFO_BASETYPES (binfo);
2729 tree old_binfos = BINFO_BASETYPES (old);
2730 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2731
2732 /* Should we use something besides CLASSTYPE_VFIELDS? */
2733 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2734 {
2735 override_one_vtable (binfo, old, t);
2736 }
2737
2738 for (i = 0; i < n_baselinks; i++)
2739 {
2740 tree base_binfo = TREE_VEC_ELT (binfos, i);
2741 tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
beb53fb8
JM
2742 int is_not_base_vtable
2743 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
39211cd5
MS
2744 if (! TREE_VIA_VIRTUAL (base_binfo))
2745 merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
2746 }
2747}
2748
9e9ff709
MS
2749/* Get the base virtual function declarations in T that are either
2750 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2751 the overrider/hider. */
e92cc029 2752
5ddc28a5 2753static tree
9e9ff709
MS
2754get_basefndecls (fndecl, t)
2755 tree fndecl, t;
2756{
2757 tree methods = TYPE_METHODS (t);
2758 tree base_fndecls = NULL_TREE;
2759 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2760 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2761
2762 while (methods)
2763 {
9e9ff709
MS
2764 if (TREE_CODE (methods) == FUNCTION_DECL
2765 && DECL_VINDEX (methods) != NULL_TREE
2766 && DECL_NAME (fndecl) == DECL_NAME (methods))
2767 base_fndecls = temp_tree_cons (fndecl, methods, base_fndecls);
2768
2769 methods = TREE_CHAIN (methods);
2770 }
2771
2772 if (base_fndecls)
2773 return base_fndecls;
2774
2775 for (i = 0; i < n_baseclasses; i++)
2776 {
2777 tree base_binfo = TREE_VEC_ELT (binfos, i);
2778 tree basetype = BINFO_TYPE (base_binfo);
9e9ff709
MS
2779
2780 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2781 base_fndecls);
2782 }
2783
2784 return base_fndecls;
2785}
2786
2787/* Mark the functions that have been hidden with their overriders.
2788 Since we start out with all functions already marked with a hider,
2789 no need to mark functions that are just hidden. */
e92cc029 2790
bd6dd845 2791static void
9e9ff709
MS
2792mark_overriders (fndecl, base_fndecls)
2793 tree fndecl, base_fndecls;
2794{
2795 while (base_fndecls)
2796 {
2797 if (overrides (TREE_VALUE (base_fndecls), fndecl))
2798 TREE_PURPOSE (base_fndecls) = fndecl;
2799
2800 base_fndecls = TREE_CHAIN (base_fndecls);
2801 }
2802}
2803
2ee887f2
MS
2804/* If this declaration supersedes the declaration of
2805 a method declared virtual in the base class, then
2806 mark this field as being virtual as well. */
2807
bd6dd845 2808static void
cffa8729 2809check_for_override (decl, ctype)
2ee887f2
MS
2810 tree decl, ctype;
2811{
2812 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
2813 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2814 int virtualp = DECL_VIRTUAL_P (decl);
ed70c426 2815 int found_overriden_fn = 0;
2ee887f2
MS
2816
2817 for (i = 0; i < n_baselinks; i++)
2818 {
2819 tree base_binfo = TREE_VEC_ELT (binfos, i);
84663f74 2820 if (TYPE_VIRTUAL_P (BINFO_TYPE (base_binfo)))
2ee887f2
MS
2821 {
2822 tree tmp = get_matching_virtual
2823 (base_binfo, decl,
2824 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
ed70c426
MM
2825
2826 if (tmp && !found_overriden_fn)
2ee887f2
MS
2827 {
2828 /* If this function overrides some virtual in some base
2829 class, then the function itself is also necessarily
2830 virtual, even if the user didn't explicitly say so. */
2831 DECL_VIRTUAL_P (decl) = 1;
2832
2833 /* The TMP we really want is the one from the deepest
2834 baseclass on this path, taking care not to
2835 duplicate if we have already found it (via another
2836 path to its virtual baseclass. */
2837 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
2838 {
8251199e 2839 cp_error_at ("method `%D' may not be declared static",
2ee887f2 2840 decl);
8251199e 2841 cp_error_at ("(since `%D' declared virtual in base class.)",
2ee887f2
MS
2842 tmp);
2843 break;
2844 }
2845 virtualp = 1;
2846
eb66be0e
MS
2847 DECL_VINDEX (decl)
2848 = tree_cons (NULL_TREE, tmp, DECL_VINDEX (decl));
ed70c426
MM
2849
2850 /* We now know that DECL overrides something,
2851 which is all that is important. But, we must
2852 continue to iterate through all the base-classes
2853 in order to allow get_matching_virtual to check for
2854 various illegal overrides. */
2855 found_overriden_fn = 1;
2ee887f2
MS
2856 }
2857 }
2858 }
2859 if (virtualp)
2860 {
2861 if (DECL_VINDEX (decl) == NULL_TREE)
2862 DECL_VINDEX (decl) = error_mark_node;
2863 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2864 }
2865}
2866
fc378698
MS
2867/* Warn about hidden virtual functions that are not overridden in t.
2868 We know that constructors and destructors don't apply. */
e92cc029 2869
9e9ff709
MS
2870void
2871warn_hidden (t)
2872 tree t;
2873{
2874 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2875 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2876 int i;
2877
2878 /* We go through each separately named virtual function. */
61a127b3 2879 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
9e9ff709 2880 {
2b9dc906
JM
2881 tree fns = TREE_VEC_ELT (method_vec, i);
2882 tree fndecl;
9e9ff709
MS
2883
2884 tree base_fndecls = NULL_TREE;
2885 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2886 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2887
2b9dc906 2888 fndecl = OVL_CURRENT (fns);
9e9ff709
MS
2889 if (DECL_VINDEX (fndecl) == NULL_TREE)
2890 continue;
2891
2892 /* First we get a list of all possible functions that might be
2893 hidden from each base class. */
2894 for (i = 0; i < n_baseclasses; i++)
2895 {
2896 tree base_binfo = TREE_VEC_ELT (binfos, i);
2897 tree basetype = BINFO_TYPE (base_binfo);
2898
2899 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2900 base_fndecls);
2901 }
2902
2b9dc906
JM
2903 fns = OVL_NEXT (fns);
2904 if (fns)
2905 fndecl = OVL_CURRENT (fns);
2906 else
2907 fndecl = NULL_TREE;
9e9ff709
MS
2908
2909 /* ...then mark up all the base functions with overriders, preferring
2910 overriders to hiders. */
2911 if (base_fndecls)
2912 while (fndecl)
2913 {
2914 mark_overriders (fndecl, base_fndecls);
2915
2b9dc906
JM
2916 fns = OVL_NEXT (fns);
2917 if (fns)
2918 fndecl = OVL_CURRENT (fns);
9e9ff709
MS
2919 else
2920 fndecl = NULL_TREE;
2921 }
2922
2923 /* Now give a warning for all base functions without overriders,
2924 as they are hidden. */
2925 while (base_fndecls)
2926 {
2927 if (! overrides (TREE_VALUE (base_fndecls),
2928 TREE_PURPOSE (base_fndecls)))
2929 {
2930 /* Here we know it is a hider, and no overrider exists. */
8251199e
JM
2931 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2932 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
9e9ff709
MS
2933 }
2934
2935 base_fndecls = TREE_CHAIN (base_fndecls);
2936 }
2937 }
2938}
2939
2940/* Check for things that are invalid. There are probably plenty of other
2941 things we should check for also. */
e92cc029 2942
9e9ff709
MS
2943static void
2944finish_struct_anon (t)
2945 tree t;
2946{
2947 tree field;
2948 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2949 {
2950 if (TREE_STATIC (field))
2951 continue;
2952 if (TREE_CODE (field) != FIELD_DECL)
2953 continue;
2954
2955 if (DECL_NAME (field) == NULL_TREE
2956 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2957 {
2958 tree* uelt = &TYPE_FIELDS (TREE_TYPE (field));
2959 for (; *uelt; uelt = &TREE_CHAIN (*uelt))
2960 {
8ebeee52 2961 if (DECL_ARTIFICIAL (*uelt))
9e9ff709
MS
2962 continue;
2963
094fe153 2964 if (DECL_NAME (*uelt) == constructor_name (t))
8ebeee52
JM
2965 cp_pedwarn_at ("ANSI C++ forbids member `%D' with same name as enclosing class",
2966 *uelt);
2967
2968 if (TREE_CODE (*uelt) != FIELD_DECL)
2969 {
2970 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2971 *uelt);
2972 continue;
2973 }
2974
9e9ff709 2975 if (TREE_PRIVATE (*uelt))
8251199e 2976 cp_pedwarn_at ("private member `%#D' in anonymous union",
9e9ff709
MS
2977 *uelt);
2978 else if (TREE_PROTECTED (*uelt))
8251199e 2979 cp_pedwarn_at ("protected member `%#D' in anonymous union",
9e9ff709 2980 *uelt);
fc378698
MS
2981
2982 TREE_PRIVATE (*uelt) = TREE_PRIVATE (field);
2983 TREE_PROTECTED (*uelt) = TREE_PROTECTED (field);
9e9ff709
MS
2984 }
2985 }
2986 }
2987}
2988
f30432d7
MS
2989extern int interface_only, interface_unknown;
2990
61a127b3
MM
2991/* Create default constructors, assignment operators, and so forth for
2992 the type indicated by T, if they are needed.
2993 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2994 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
2995 cannot have a default constructor, copy constructor taking a const
2996 reference argument, or an assignment operator, respectively. If a
2997 virtual destructor is created, its DECL is returned; otherwise the
2998 return value is NULL_TREE. */
2999
3000static tree
3001add_implicitly_declared_members (t, cant_have_default_ctor,
3002 cant_have_const_cctor,
3003 cant_have_assignment)
3004 tree t;
3005 int cant_have_default_ctor;
3006 int cant_have_const_cctor;
3007 int cant_have_assignment;
3008{
3009 tree default_fn;
3010 tree implicit_fns = NULL_TREE;
3011 tree name = TYPE_IDENTIFIER (t);
3012 tree virtual_dtor = NULL_TREE;
3013 tree *f;
3014
3015 /* Destructor. */
3016 if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t)
3017 && !IS_SIGNATURE (t))
3018 {
3019 default_fn = cons_up_default_function (t, name, 0);
3020 check_for_override (default_fn, t);
3021
3022 /* If we couldn't make it work, then pretend we didn't need it. */
3023 if (default_fn == void_type_node)
3024 TYPE_NEEDS_DESTRUCTOR (t) = 0;
3025 else
3026 {
3027 TREE_CHAIN (default_fn) = implicit_fns;
3028 implicit_fns = default_fn;
3029
3030 if (DECL_VINDEX (default_fn))
3031 virtual_dtor = default_fn;
3032 }
3033 }
3034 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3035
3036 /* Default constructor. */
3037 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor
3038 && ! IS_SIGNATURE (t))
3039 {
3040 default_fn = cons_up_default_function (t, name, 2);
3041 TREE_CHAIN (default_fn) = implicit_fns;
3042 implicit_fns = default_fn;
3043 }
3044
3045 /* Copy constructor. */
3046 if (! TYPE_HAS_INIT_REF (t) && ! IS_SIGNATURE (t) && ! TYPE_FOR_JAVA (t))
3047 {
3048 /* ARM 12.18: You get either X(X&) or X(const X&), but
3049 not both. --Chip */
3050 default_fn = cons_up_default_function (t, name,
3051 3 + cant_have_const_cctor);
3052 TREE_CHAIN (default_fn) = implicit_fns;
3053 implicit_fns = default_fn;
3054 }
3055
3056 /* Assignment operator. */
3057 if (! TYPE_HAS_ASSIGN_REF (t) && ! IS_SIGNATURE (t) && ! TYPE_FOR_JAVA (t))
3058 {
3059 default_fn = cons_up_default_function (t, name,
3060 5 + cant_have_assignment);
3061 TREE_CHAIN (default_fn) = implicit_fns;
3062 implicit_fns = default_fn;
3063 }
3064
3065 /* Now, hook all of the new functions on to TYPE_METHODS,
3066 and add them to the CLASSTYPE_METHOD_VEC. */
3067 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3068 add_method (t, 0, *f);
3069 *f = TYPE_METHODS (t);
3070 TYPE_METHODS (t) = implicit_fns;
3071
3072 return virtual_dtor;
3073}
3074
8d08fdba
MS
3075/* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
3076 (or C++ class declaration).
3077
3078 For C++, we must handle the building of derived classes.
3079 Also, C++ allows static class members. The way that this is
3080 handled is to keep the field name where it is (as the DECL_NAME
3081 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
3082 of the field. layout_record and layout_union will know about this.
3083
3084 More C++ hair: inline functions have text in their
3085 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
3086 meaningful tree structure. After the struct has been laid out, set
3087 things up so that this can happen.
3088
3089 And still more: virtual functions. In the case of single inheritance,
3090 when a new virtual function is seen which redefines a virtual function
3091 from the base class, the new virtual function is placed into
3092 the virtual function table at exactly the same address that
3093 it had in the base class. When this is extended to multiple
3094 inheritance, the same thing happens, except that multiple virtual
3095 function tables must be maintained. The first virtual function
3096 table is treated in exactly the same way as in the case of single
3097 inheritance. Additional virtual function tables have different
3098 DELTAs, which tell how to adjust `this' to point to the right thing.
3099
84663f74 3100 ATTRIBUTES is the set of decl attributes to be applied, if any. */
8d08fdba 3101
0aafb128 3102void
6467930b
MS
3103finish_struct_1 (t, warn_anon)
3104 tree t;
8d08fdba
MS
3105 int warn_anon;
3106{
8d08fdba 3107 int old;
8d08fdba 3108 enum tree_code code = TREE_CODE (t);
f30432d7 3109 tree fields = TYPE_FIELDS (t);
f30432d7 3110 tree x, last_x, method_vec;
8d08fdba
MS
3111 int has_virtual;
3112 int max_has_virtual;
3113 tree pending_virtuals = NULL_TREE;
aa598818 3114 tree pending_hard_virtuals = NULL_TREE;
8d08fdba
MS
3115 tree abstract_virtuals = NULL_TREE;
3116 tree vfield;
3117 tree vfields;
61a127b3 3118 tree virtual_dtor;
8d08fdba
MS
3119 int cant_have_default_ctor;
3120 int cant_have_const_ctor;
8d08fdba 3121 int no_const_asn_ref;
a7a7710d 3122 int has_mutable = 0;
8d08fdba
MS
3123
3124 /* The index of the first base class which has virtual
3125 functions. Only applied to non-virtual baseclasses. */
3126 int first_vfn_base_index;
3127
3128 int n_baseclasses;
3129 int any_default_members = 0;
3130 int const_sans_init = 0;
3131 int ref_sans_init = 0;
a0a33927 3132 tree access_decls = NULL_TREE;
f30432d7 3133 int aggregate = 1;
691c003d 3134 int empty = 1;
824b9a4c 3135 int has_pointers = 0;
8d08fdba 3136
f30432d7 3137 if (warn_anon && code != UNION_TYPE && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8251199e 3138 pedwarn ("anonymous class type not used to declare any objects");
8d08fdba 3139
8d08fdba
MS
3140 if (TYPE_SIZE (t))
3141 {
3142 if (IS_AGGR_TYPE (t))
8251199e 3143 cp_error ("redefinition of `%#T'", t);
8d08fdba
MS
3144 else
3145 my_friendly_abort (172);
b74a0560 3146 popclass ();
0aafb128 3147 return;
8d08fdba
MS
3148 }
3149
8d08fdba
MS
3150 GNU_xref_decl (current_function_decl, t);
3151
3152 /* If this type was previously laid out as a forward reference,
3153 make sure we lay it out again. */
3154
a0a33927 3155 TYPE_SIZE (t) = NULL_TREE;
8d08fdba
MS
3156 CLASSTYPE_GOT_SEMICOLON (t) = 0;
3157
f376e137 3158#if 0
00595019
MS
3159 /* This is in general too late to do this. I moved the main case up to
3160 left_curly, what else needs to move? */
3161 if (! IS_SIGNATURE (t))
8d08fdba 3162 {
00595019
MS
3163 my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t) == interface_only, 999);
3164 my_friendly_assert (CLASSTYPE_INTERFACE_KNOWN (t) == ! interface_unknown, 999);
8d08fdba 3165 }
f376e137 3166#endif
8d08fdba 3167
8d08fdba
MS
3168 old = suspend_momentary ();
3169
3170 /* Install struct as DECL_FIELD_CONTEXT of each field decl.
3171 Also process specified field sizes.
3172 Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
3173 The specified size is found in the DECL_INITIAL.
3174 Store 0 there, except for ": 0" fields (so we can find them
3175 and delete them, below). */
3176
9a71c18b
JM
3177 if (TYPE_BINFO_BASETYPES (t))
3178 n_baseclasses = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t));
8d08fdba
MS
3179 else
3180 n_baseclasses = 0;
3181
3182 if (n_baseclasses > 0)
3183 {
3184 struct base_info base_info;
3185
9a71c18b 3186 first_vfn_base_index = finish_base_struct (t, &base_info);
e92cc029 3187 /* Remember where we got our vfield from. */
8d08fdba
MS
3188 CLASSTYPE_VFIELD_PARENT (t) = first_vfn_base_index;
3189 has_virtual = base_info.has_virtual;
3190 max_has_virtual = base_info.max_has_virtual;
8d08fdba
MS
3191 vfield = base_info.vfield;
3192 vfields = base_info.vfields;
6b5fbb55 3193 CLASSTYPE_RTTI (t) = base_info.rtti;
8d08fdba
MS
3194 cant_have_default_ctor = base_info.cant_have_default_ctor;
3195 cant_have_const_ctor = base_info.cant_have_const_ctor;
8d08fdba 3196 no_const_asn_ref = base_info.no_const_asn_ref;
f30432d7 3197 aggregate = 0;
8d08fdba
MS
3198 }
3199 else
3200 {
3201 first_vfn_base_index = -1;
3202 has_virtual = 0;
3203 max_has_virtual = has_virtual;
3204 vfield = NULL_TREE;
3205 vfields = NULL_TREE;
6b5fbb55 3206 CLASSTYPE_RTTI (t) = NULL_TREE;
8d08fdba
MS
3207 cant_have_default_ctor = 0;
3208 cant_have_const_ctor = 0;
8d08fdba 3209 no_const_asn_ref = 0;
8d08fdba
MS
3210 }
3211
f376e137 3212#if 0
00595019 3213 /* Both of these should be done before now. */
8d08fdba 3214 if (write_virtuals == 3 && CLASSTYPE_INTERFACE_KNOWN (t)
700f8a87 3215 && ! IS_SIGNATURE (t))
8d08fdba 3216 {
00595019
MS
3217 my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t) == interface_only, 999);
3218 my_friendly_assert (CLASSTYPE_VTABLE_NEEDS_WRITING (t) == ! interface_only, 999);
8d08fdba 3219 }
f376e137 3220#endif
8d08fdba
MS
3221
3222 /* The three of these are approximations which may later be
3223 modified. Needed at this point to make add_virtual_function
3224 and modify_vtable_entries work. */
8d08fdba
MS
3225 CLASSTYPE_VFIELDS (t) = vfields;
3226 CLASSTYPE_VFIELD (t) = vfield;
3227
72b7eeff 3228 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
8d08fdba 3229 {
f30432d7 3230 GNU_xref_member (current_class_name, x);
8d08fdba 3231
f30432d7
MS
3232 /* If this was an evil function, don't keep it in class. */
3233 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3234 continue;
8d08fdba 3235
f30432d7
MS
3236 /* Do both of these, even though they're in the same union;
3237 if the insn `r' member and the size `i' member are
3238 different sizes, as on the alpha, the larger of the two
3239 will end up with garbage in it. */
3240 DECL_SAVED_INSNS (x) = NULL_RTX;
3241 DECL_FIELD_SIZE (x) = 0;
a4443a08 3242
cffa8729
MS
3243 check_for_override (x, t);
3244 if (DECL_ABSTRACT_VIRTUAL_P (x) && ! DECL_VINDEX (x))
8251199e 3245 cp_error_at ("initializer specified for non-virtual method `%D'", x);
2ee887f2 3246
f30432d7
MS
3247 /* The name of the field is the original field name
3248 Save this in auxiliary field for later overloading. */
84663f74 3249 if (DECL_VINDEX (x))
f30432d7 3250 {
aa598818
JM
3251 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3252 &has_virtual, x, t);
f30432d7
MS
3253 if (DECL_ABSTRACT_VIRTUAL_P (x))
3254 abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
5566b478
MS
3255#if 0
3256 /* XXX Why did I comment this out? (jason) */
f30432d7
MS
3257 else
3258 TREE_USED (x) = 1;
5566b478 3259#endif
f30432d7
MS
3260 }
3261 }
8d08fdba 3262
732dcb6f
JM
3263 if (n_baseclasses)
3264 fields = chainon (build_vbase_pointer_fields (t), fields);
3265
9a71c18b
JM
3266 last_x = NULL_TREE;
3267 for (x = fields; x; x = TREE_CHAIN (x))
f30432d7
MS
3268 {
3269 GNU_xref_member (current_class_name, x);
8d08fdba 3270
c91a56d2 3271 if (TREE_CODE (x) == FIELD_DECL)
691c003d
MS
3272 {
3273 DECL_PACKED (x) |= TYPE_PACKED (t);
e6267549
JM
3274
3275 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3276 /* A zero-width bitfield doesn't do the trick. */;
3277 else
3278 empty = 0;
691c003d 3279 }
c91a56d2 3280
cffa8729 3281 if (TREE_CODE (x) == USING_DECL)
f30432d7 3282 {
79ad62b2 3283 /* Save access declarations for later. */
f30432d7
MS
3284 if (last_x)
3285 TREE_CHAIN (last_x) = TREE_CHAIN (x);
3286 else
3287 fields = TREE_CHAIN (x);
79ad62b2
MM
3288
3289 access_decls = scratch_tree_cons (NULL_TREE, x, access_decls);
f30432d7
MS
3290 continue;
3291 }
8d08fdba 3292
f30432d7 3293 last_x = x;
8d08fdba 3294
050367a3
MM
3295 if (TREE_CODE (x) == TYPE_DECL
3296 || TREE_CODE (x) == TEMPLATE_DECL)
f30432d7 3297 continue;
8d08fdba 3298
f30432d7 3299 /* If we've gotten this far, it's a data member, possibly static,
e92cc029 3300 or an enumerator. */
8d08fdba 3301
f30432d7 3302 DECL_FIELD_CONTEXT (x) = t;
8d08fdba 3303
f30432d7
MS
3304 /* ``A local class cannot have static data members.'' ARM 9.4 */
3305 if (current_function_decl && TREE_STATIC (x))
8251199e 3306 cp_error_at ("field `%D' in local class cannot be static", x);
8d08fdba 3307
f30432d7
MS
3308 /* Perform error checking that did not get done in
3309 grokdeclarator. */
3310 if (TREE_CODE (TREE_TYPE (x)) == FUNCTION_TYPE)
3311 {
8251199e 3312 cp_error_at ("field `%D' invalidly declared function type",
f30432d7
MS
3313 x);
3314 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3315 }
3316 else if (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE)
3317 {
8251199e 3318 cp_error_at ("field `%D' invalidly declared method type", x);
f30432d7
MS
3319 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3320 }
3321 else if (TREE_CODE (TREE_TYPE (x)) == OFFSET_TYPE)
3322 {
8251199e 3323 cp_error_at ("field `%D' invalidly declared offset type", x);
f30432d7
MS
3324 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3325 }
8d08fdba 3326
f714b8e5 3327#if 0
f30432d7 3328 if (DECL_NAME (x) == constructor_name (t))
e349ee73 3329 cant_have_default_ctor = 1;
f714b8e5 3330#endif
f376e137 3331
f30432d7
MS
3332 if (TREE_TYPE (x) == error_mark_node)
3333 continue;
8d08fdba 3334
f30432d7
MS
3335 DECL_SAVED_INSNS (x) = NULL_RTX;
3336 DECL_FIELD_SIZE (x) = 0;
8d08fdba 3337
f30432d7
MS
3338 /* When this goes into scope, it will be a non-local reference. */
3339 DECL_NONLOCAL (x) = 1;
8d08fdba 3340
f30432d7
MS
3341 if (TREE_CODE (x) == CONST_DECL)
3342 continue;
8d08fdba 3343
f30432d7
MS
3344 if (TREE_CODE (x) == VAR_DECL)
3345 {
3346 if (TREE_CODE (t) == UNION_TYPE)
3347 /* Unions cannot have static members. */
8251199e 3348 cp_error_at ("field `%D' declared static in union", x);
8d08fdba 3349
f30432d7
MS
3350 continue;
3351 }
8d08fdba 3352
f30432d7 3353 /* Now it can only be a FIELD_DECL. */
8d08fdba 3354
f30432d7
MS
3355 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3356 aggregate = 0;
8d08fdba 3357
f30432d7
MS
3358 /* If this is of reference type, check if it needs an init.
3359 Also do a little ANSI jig if necessary. */
3360 if (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE)
3361 {
3362 if (DECL_INITIAL (x) == NULL_TREE)
3363 ref_sans_init = 1;
8d08fdba 3364
f30432d7
MS
3365 /* ARM $12.6.2: [A member initializer list] (or, for an
3366 aggregate, initialization by a brace-enclosed list) is the
3367 only way to initialize nonstatic const and reference
3368 members. */
f30432d7 3369 cant_have_default_ctor = 1;
e349ee73 3370 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
f30432d7
MS
3371
3372 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3373 {
3374 if (DECL_NAME (x))
8251199e 3375 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
f30432d7 3376 else
8251199e 3377 cp_warning_at ("non-static reference in class without a constructor", x);
8d08fdba 3378 }
f30432d7 3379 }
8d08fdba 3380
824b9a4c
MS
3381 if (TREE_CODE (TREE_TYPE (x)) == POINTER_TYPE)
3382 has_pointers = 1;
3383
a7a7710d
NS
3384 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (TREE_TYPE (x)))
3385 has_mutable = 1;
3386
f30432d7 3387 /* If any field is const, the structure type is pseudo-const. */
1b8899d1 3388 if (CP_TYPE_CONST_P (TREE_TYPE (x)))
f30432d7
MS
3389 {
3390 C_TYPE_FIELDS_READONLY (t) = 1;
3391 if (DECL_INITIAL (x) == NULL_TREE)
3392 const_sans_init = 1;
3393
3394 /* ARM $12.6.2: [A member initializer list] (or, for an
3395 aggregate, initialization by a brace-enclosed list) is the
3396 only way to initialize nonstatic const and reference
3397 members. */
f30432d7 3398 cant_have_default_ctor = 1;
e349ee73 3399 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
f30432d7
MS
3400
3401 if (! TYPE_HAS_CONSTRUCTOR (t) && !IS_SIGNATURE (t)
3402 && extra_warnings)
3403 {
3404 if (DECL_NAME (x))
8251199e 3405 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
f30432d7 3406 else
8251199e 3407 cp_warning_at ("non-static const member in class without a constructor", x);
f30432d7
MS
3408 }
3409 }
3410 else
3411 {
3412 /* A field that is pseudo-const makes the structure
3413 likewise. */
3414 tree t1 = TREE_TYPE (x);
3415 while (TREE_CODE (t1) == ARRAY_TYPE)
3416 t1 = TREE_TYPE (t1);
3417 if (IS_AGGR_TYPE (t1))
8d08fdba 3418 {
f30432d7
MS
3419 if (C_TYPE_FIELDS_READONLY (t1))
3420 C_TYPE_FIELDS_READONLY (t) = 1;
3421 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1))
8d08fdba 3422 const_sans_init = 1;
f30432d7
MS
3423 }
3424 }
8d08fdba 3425
162bc98d
JM
3426 /* We set DECL_C_BIT_FIELD in grokbitfield.
3427 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3428 if (DECL_C_BIT_FIELD (x))
f30432d7 3429 {
f30432d7
MS
3430 /* Invalid bit-field size done by grokfield. */
3431 /* Detect invalid bit-field type. */
3432 if (DECL_INITIAL (x)
3433 && ! INTEGRAL_TYPE_P (TREE_TYPE (x)))
3434 {
8251199e 3435 cp_error_at ("bit-field `%#D' with non-integral type", x);
f30432d7
MS
3436 DECL_INITIAL (x) = NULL;
3437 }
8d08fdba 3438
f30432d7
MS
3439 /* Detect and ignore out of range field width. */
3440 if (DECL_INITIAL (x))
3441 {
5566b478 3442 tree w = DECL_INITIAL (x);
a703fb38 3443 register int width = 0;
f30432d7 3444
5566b478
MS
3445 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3446 STRIP_NOPS (w);
3447
3448 /* detect invalid field size. */
3449 if (TREE_CODE (w) == CONST_DECL)
3450 w = DECL_INITIAL (w);
3451 else if (TREE_READONLY_DECL_P (w))
3452 w = decl_constant_value (w);
3453
3454 if (TREE_CODE (w) != INTEGER_CST)
3455 {
8251199e 3456 cp_error_at ("bit-field `%D' width not an integer constant",
5566b478
MS
3457 x);
3458 DECL_INITIAL (x) = NULL_TREE;
3459 }
3460 else if (width = TREE_INT_CST_LOW (w),
3461 width < 0)
8d08fdba 3462 {
f30432d7 3463 DECL_INITIAL (x) = NULL;
8251199e 3464 cp_error_at ("negative width in bit-field `%D'", x);
8d08fdba 3465 }
f30432d7 3466 else if (width == 0 && DECL_NAME (x) != 0)
8d08fdba 3467 {
f30432d7 3468 DECL_INITIAL (x) = NULL;
8251199e 3469 cp_error_at ("zero width for bit-field `%D'", x);
8d08fdba 3470 }
f30432d7
MS
3471 else if (width
3472 > TYPE_PRECISION (long_long_unsigned_type_node))
8d08fdba 3473 {
f30432d7
MS
3474 /* The backend will dump if you try to use something
3475 too big; avoid that. */
8d08fdba 3476 DECL_INITIAL (x) = NULL;
f30432d7
MS
3477 sorry ("bit-fields larger than %d bits",
3478 TYPE_PRECISION (long_long_unsigned_type_node));
8251199e 3479 cp_error_at (" in declaration of `%D'", x);
8d08fdba 3480 }
f30432d7 3481 else if (width > TYPE_PRECISION (TREE_TYPE (x))
18922a6b
JM
3482 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE
3483 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE)
8d08fdba 3484 {
8251199e 3485 cp_warning_at ("width of `%D' exceeds its type", x);
8d08fdba 3486 }
f30432d7
MS
3487 else if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
3488 && ((min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
3489 TREE_UNSIGNED (TREE_TYPE (x))) > width)
3490 || (min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
3491 TREE_UNSIGNED (TREE_TYPE (x))) > width)))
8d08fdba 3492 {
8251199e 3493 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
f30432d7
MS
3494 x, TREE_TYPE (x));
3495 }
f30432d7 3496
72a93143 3497 if (DECL_INITIAL (x))
f30432d7 3498 {
72a93143
JM
3499 DECL_INITIAL (x) = NULL_TREE;
3500 DECL_FIELD_SIZE (x) = width;
3501 DECL_BIT_FIELD (x) = 1;
3502
3503 if (width == 0)
3504 {
8d08fdba 3505#ifdef EMPTY_FIELD_BOUNDARY
72a93143
JM
3506 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3507 EMPTY_FIELD_BOUNDARY);
8d08fdba
MS
3508#endif
3509#ifdef PCC_BITFIELD_TYPE_MATTERS
72a93143
JM
3510 if (PCC_BITFIELD_TYPE_MATTERS)
3511 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3512 TYPE_ALIGN (TREE_TYPE (x)));
8d08fdba 3513#endif
72a93143 3514 }
f30432d7 3515 }
8d08fdba
MS
3516 }
3517 else
f30432d7
MS
3518 /* Non-bit-fields are aligned for their type. */
3519 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x)));
3520 }
3521 else
3522 {
3523 tree type = TREE_TYPE (x);
8d08fdba 3524
72b7eeff 3525 while (TREE_CODE (type) == ARRAY_TYPE)
f30432d7 3526 type = TREE_TYPE (type);
8d08fdba 3527
f30432d7
MS
3528 if (TYPE_LANG_SPECIFIC (type) && ! ANON_UNION_P (x)
3529 && ! TYPE_PTRMEMFUNC_P (type))
3530 {
3531 /* Never let anything with uninheritable virtuals
3532 make it through without complaint. */
3533 if (CLASSTYPE_ABSTRACT_VIRTUALS (type))
3534 abstract_virtuals_error (x, type);
8d08fdba 3535
f30432d7
MS
3536 /* Don't let signatures make it through either. */
3537 if (IS_SIGNATURE (type))
3538 signature_error (x, type);
8d08fdba 3539
f30432d7
MS
3540 if (code == UNION_TYPE)
3541 {
d8e178a0 3542 const char *fie = NULL;
f30432d7
MS
3543 if (TYPE_NEEDS_CONSTRUCTING (type))
3544 fie = "constructor";
3545 else if (TYPE_NEEDS_DESTRUCTOR (type))
3546 fie = "destructor";
2455f26f
JM
3547 else if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3548 fie = "copy assignment operator";
f30432d7 3549 if (fie)
8251199e 3550 cp_error_at ("member `%#D' with %s not allowed in union", x,
f30432d7
MS
3551 fie);
3552 }
3553 else
3554 {
3555 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3556 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3557 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3558 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3559 }
8d08fdba 3560
e349ee73 3561 if (!TYPE_HAS_CONST_INIT_REF (type))
f30432d7
MS
3562 cant_have_const_ctor = 1;
3563
e349ee73 3564 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
f30432d7
MS
3565 no_const_asn_ref = 1;
3566
3567 if (TYPE_HAS_CONSTRUCTOR (type)
3568 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3569 {
3570 cant_have_default_ctor = 1;
c19a8067 3571#if 0
f30432d7
MS
3572 /* This is wrong for aggregates. */
3573 if (! TYPE_HAS_CONSTRUCTOR (t))
3574 {
3575 if (DECL_NAME (x))
8251199e 3576 cp_pedwarn_at ("member `%#D' with only non-default constructor", x);
f30432d7 3577 else
8251199e
JM
3578 cp_pedwarn_at ("member with only non-default constructor", x);
3579 cp_pedwarn_at ("in class without a constructor",
f30432d7 3580 x);
8d08fdba 3581 }
f30432d7 3582#endif
8d08fdba
MS
3583 }
3584 }
f30432d7
MS
3585 if (DECL_INITIAL (x) != NULL_TREE)
3586 {
3587 /* `build_class_init_list' does not recognize
3588 non-FIELD_DECLs. */
3589 if (code == UNION_TYPE && any_default_members != 0)
8251199e 3590 cp_error_at ("multiple fields in union `%T' initialized");
f30432d7
MS
3591 any_default_members = 1;
3592 }
8d08fdba
MS
3593 }
3594 }
3595
8d08fdba
MS
3596 /* If this type has any constant members which did not come
3597 with their own initialization, mark that fact here. It is
3598 not an error here, since such types can be saved either by their
3599 constructors, or by fortuitous initialization. */
3600 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
3601 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
3602 CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
a7a7710d 3603 CLASSTYPE_HAS_MUTABLE (t) = has_mutable;
8d08fdba 3604
824b9a4c 3605 /* Effective C++ rule 11. */
7834ab39 3606 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
824b9a4c
MS
3607 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3608 {
8251199e 3609 cp_warning ("`%#T' has pointer data members", t);
824b9a4c
MS
3610
3611 if (! TYPE_HAS_INIT_REF (t))
3612 {
8251199e 3613 cp_warning (" but does not override `%T(const %T&)'", t, t);
824b9a4c 3614 if (! TYPE_HAS_ASSIGN_REF (t))
8251199e 3615 cp_warning (" or `operator=(const %T&)'", t);
824b9a4c
MS
3616 }
3617 else if (! TYPE_HAS_ASSIGN_REF (t))
8251199e 3618 cp_warning (" but does not override `operator=(const %T&)'", t);
824b9a4c 3619 }
61a127b3
MM
3620
3621 /* Do some bookkeeping that will guide the generation of implicitly
3622 declared member functions. */
8d08fdba
MS
3623 TYPE_HAS_COMPLEX_INIT_REF (t)
3624 |= (TYPE_HAS_INIT_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
faf5394a 3625 || has_virtual || any_default_members);
8d08fdba
MS
3626 TYPE_NEEDS_CONSTRUCTING (t)
3627 |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
faf5394a 3628 || has_virtual || any_default_members);
f30432d7
MS
3629 if (! IS_SIGNATURE (t))
3630 CLASSTYPE_NON_AGGREGATE (t)
3631 = ! aggregate || has_virtual || TYPE_HAS_CONSTRUCTOR (t);
8d08fdba
MS
3632 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
3633 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
e8abc66f 3634 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
8d08fdba 3635
61a127b3
MM
3636 /* Synthesize any needed methods. Note that methods will be synthesized
3637 for anonymous unions; grok_x_components undoes that. */
3638 virtual_dtor
3639 = add_implicitly_declared_members (t, cant_have_default_ctor,
3640 cant_have_const_ctor,
3641 no_const_asn_ref);
3642 if (virtual_dtor)
3643 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3644 &has_virtual, virtual_dtor, t);
8d08fdba 3645
61a127b3 3646 if (TYPE_METHODS (t))
8d08fdba 3647 {
b0e0b31f
MM
3648 finish_struct_methods (t);
3649 method_vec = CLASSTYPE_METHOD_VEC (t);
8d08fdba
MS
3650 }
3651 else
3652 {
3653 method_vec = 0;
3654
3655 /* Just in case these got accidentally
3656 filled in by syntax errors. */
3657 TYPE_HAS_CONSTRUCTOR (t) = 0;
3658 TYPE_HAS_DESTRUCTOR (t) = 0;
3659 }
3660
79ad62b2
MM
3661 for (access_decls = nreverse (access_decls); access_decls;
3662 access_decls = TREE_CHAIN (access_decls))
3663 handle_using_decl (TREE_VALUE (access_decls), t, method_vec, fields);
8d08fdba
MS
3664
3665 if (vfield == NULL_TREE && has_virtual)
3666 {
9335e9a3
MM
3667 /* We build this decl with vtbl_ptr_type_node, which is a
3668 `vtable_entry_type*'. It might seem more precise to use
3669 `vtable_entry_type (*)[N]' where N is the number of firtual
3670 functions. However, that would require the vtable pointer in
3671 base classes to have a different type than the vtable pointer
3672 in derived classes. We could make that happen, but that
3673 still wouldn't solve all the problems. In particular, the
3674 type-based alias analysis code would decide that assignments
3675 to the base class vtable pointer can't alias assignments to
3676 the derived class vtable pointer, since they have different
3677 types. Thus, in an derived class destructor, where the base
3678 class constructor was inlined, we could generate bad code for
3679 setting up the vtable pointer.
3680
3681 Therefore, we use one type for all vtable pointers. We still
3682 use a type-correct type; it's just doesn't indicate the array
3683 bounds. That's better than using `void*' or some such; it's
3684 cleaner, and it let's the alias analysis code know that these
3685 stores cannot alias stores to void*! */
8d08fdba 3686 vfield = build_lang_field_decl (FIELD_DECL, get_vfield_name (t),
849da744 3687 vtbl_ptr_type_node);
8d08fdba
MS
3688 /* If you change any of the below, take a look at all the
3689 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
e92cc029 3690 them too. */
8d08fdba
MS
3691 DECL_ASSEMBLER_NAME (vfield) = get_identifier (VFIELD_BASE);
3692 CLASSTYPE_VFIELD (t) = vfield;
3693 DECL_VIRTUAL_P (vfield) = 1;
d2e5ee5c 3694 DECL_ARTIFICIAL (vfield) = 1;
8d08fdba
MS
3695 DECL_FIELD_CONTEXT (vfield) = t;
3696 DECL_CLASS_CONTEXT (vfield) = t;
3697 DECL_FCONTEXT (vfield) = t;
28cbf42c 3698 DECL_SAVED_INSNS (vfield) = NULL_RTX;
8d08fdba
MS
3699 DECL_FIELD_SIZE (vfield) = 0;
3700 DECL_ALIGN (vfield) = TYPE_ALIGN (ptr_type_node);
6b5fbb55
MS
3701#if 0
3702 /* This is more efficient, but breaks binary compatibility, turn
3703 it on sometime when we don't care. If we turn it on, we also
3704 have to enable the code in dfs_init_vbase_pointers. */
3705 /* vfield is always first entry in structure. */
3706 TREE_CHAIN (vfield) = fields;
3707 fields = vfield;
3708#else
3709 if (last_x)
8d08fdba 3710 {
a0a33927 3711 my_friendly_assert (TREE_CHAIN (last_x) == NULL_TREE, 175);
8d08fdba
MS
3712 TREE_CHAIN (last_x) = vfield;
3713 last_x = vfield;
3714 }
3715 else
3716 fields = vfield;
6b5fbb55 3717#endif
691c003d 3718 empty = 0;
9e0781b5 3719 vfields = chainon (vfields, build_tree_list (NULL_TREE, t));
8d08fdba
MS
3720 }
3721
3722 /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
8d08fdba
MS
3723
3724 C++: maybe we will support default field initialization some day... */
3725
8d08fdba
MS
3726 /* Delete all duplicate fields from the fields */
3727 delete_duplicate_fields (fields);
3728
8ebeee52 3729 /* Now we have the nearly final fieldlist for the data fields. Record it,
8d08fdba
MS
3730 then lay out the structure or union (including the fields). */
3731
3732 TYPE_FIELDS (t) = fields;
3733
8d08fdba 3734 if (n_baseclasses)
732dcb6f
JM
3735 {
3736 last_x = build_base_fields (t);
3737
3738 /* If all our bases are empty, we can be empty too. */
3739 for (x = last_x; empty && x; x = TREE_CHAIN (x))
3740 if (DECL_SIZE (x) != integer_zero_node)
3741 empty = 0;
3742 }
3743 if (empty)
691c003d
MS
3744 {
3745 /* C++: do not let empty structures exist. */
3746 tree decl = build_lang_field_decl
3747 (FIELD_DECL, NULL_TREE, char_type_node);
732dcb6f 3748 TREE_CHAIN (decl) = fields;
691c003d
MS
3749 TYPE_FIELDS (t) = decl;
3750 }
732dcb6f
JM
3751 if (n_baseclasses)
3752 TYPE_FIELDS (t) = chainon (last_x, TYPE_FIELDS (t));
8d08fdba
MS
3753
3754 layout_type (t);
3755
9a71c18b 3756 /* Remember the size and alignment of the class before adding
0b41abe6 3757 the virtual bases. */
732dcb6f
JM
3758 if (empty && flag_new_abi)
3759 CLASSTYPE_SIZE (t) = integer_zero_node;
6bc39009
JM
3760 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
3761 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
3762 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
732dcb6f
JM
3763 else
3764 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
0b41abe6
JM
3765 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
3766
9e9ff709 3767 finish_struct_anon (t);
8d08fdba 3768
8d08fdba
MS
3769 /* Set the TYPE_DECL for this type to contain the right
3770 value for DECL_OFFSET, so that we can use it as part
3771 of a COMPONENT_REF for multiple inheritance. */
3772
d2e5ee5c 3773 layout_decl (TYPE_MAIN_DECL (t), 0);
8d08fdba 3774
7177d104
MS
3775 /* Now fix up any virtual base class types that we left lying
3776 around. We must get these done before we try to lay out the
3777 virtual function table. */
8d08fdba
MS
3778 pending_hard_virtuals = nreverse (pending_hard_virtuals);
3779
9a71c18b
JM
3780 if (n_baseclasses)
3781 /* layout_basetypes will remove the base subobject fields. */
3782 max_has_virtual = layout_basetypes (t, max_has_virtual);
5c6e1aa1 3783 if (empty)
9a71c18b
JM
3784 TYPE_FIELDS (t) = fields;
3785
8ebeee52
JM
3786 my_friendly_assert (TYPE_FIELDS (t) == fields, 981117);
3787
3788 /* Delete all zero-width bit-fields from the front of the fieldlist */
3789 while (fields && DECL_C_BIT_FIELD (fields)
3790 && DECL_INITIAL (fields))
3791 fields = TREE_CHAIN (fields);
3792 /* Delete all such fields from the rest of the fields. */
3793 for (x = fields; x;)
3794 {
3795 if (TREE_CHAIN (x) && DECL_C_BIT_FIELD (TREE_CHAIN (x))
3796 && DECL_INITIAL (TREE_CHAIN (x)))
3797 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
3798 else
3799 x = TREE_CHAIN (x);
3800 }
3801 TYPE_FIELDS (t) = fields;
3802
8d08fdba
MS
3803 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
3804 {
3805 tree vbases;
3806
8d08fdba 3807 vbases = CLASSTYPE_VBASECLASSES (t);
8d08fdba 3808
39211cd5
MS
3809 {
3810 /* Now fixup overrides of all functions in vtables from all
3811 direct or indirect virtual base classes. */
3812 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3813 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3814
3815 for (i = 0; i < n_baseclasses; i++)
3816 {
3817 tree base_binfo = TREE_VEC_ELT (binfos, i);
3818 tree basetype = BINFO_TYPE (base_binfo);
3819 tree vbases;
3820
3821 vbases = CLASSTYPE_VBASECLASSES (basetype);
3822 while (vbases)
3823 {
3824 merge_overrides (binfo_member (BINFO_TYPE (vbases),
3825 CLASSTYPE_VBASECLASSES (t)),
3826 vbases, 1, t);
3827 vbases = TREE_CHAIN (vbases);
3828 }
3829 }
3830 }
8d08fdba
MS
3831 }
3832
2986ae00
MS
3833 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
3834 might need to know it for setting up the offsets in the vtable
3835 (or in thunks) below. */
3836 if (vfield != NULL_TREE
3837 && DECL_FIELD_CONTEXT (vfield) != t)
3838 {
3839 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
3840 tree offset = BINFO_OFFSET (binfo);
3841
3842 vfield = copy_node (vfield);
3843 copy_lang_decl (vfield);
3844
3845 if (! integer_zerop (offset))
3846 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
3847 DECL_FIELD_CONTEXT (vfield) = t;
3848 DECL_CLASS_CONTEXT (vfield) = t;
3849 DECL_FIELD_BITPOS (vfield)
3850 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
3851 CLASSTYPE_VFIELD (t) = vfield;
3852 }
3853
8d08fdba 3854#ifdef NOTQUITE
8251199e 3855 cp_warning ("Doing hard virtuals for %T...", t);
8d08fdba 3856#endif
db5ae43f
MS
3857
3858 if (has_virtual > max_has_virtual)
3859 max_has_virtual = has_virtual;
3860 if (max_has_virtual > 0)
3861 TYPE_VIRTUAL_P (t) = 1;
3862
3863 if (flag_rtti && TYPE_VIRTUAL_P (t) && !pending_hard_virtuals)
3864 modify_all_vtables (t, NULL_TREE, NULL_TREE);
3865
8d08fdba
MS
3866 while (pending_hard_virtuals)
3867 {
7177d104
MS
3868 modify_all_vtables (t,
3869 TREE_PURPOSE (pending_hard_virtuals),
3870 TREE_VALUE (pending_hard_virtuals));
8d08fdba
MS
3871 pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
3872 }
72b7eeff
MS
3873
3874 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
3875 {
3876 tree vbases;
3877 /* Now fixup any virtual function entries from virtual bases
3878 that have different deltas. This has to come after we do the
3879 pending hard virtuals, as we might have a function that comes
3880 from multiple virtual base instances that is only overridden
3881 by a hard virtual above. */
3882 vbases = CLASSTYPE_VBASECLASSES (t);
3883 while (vbases)
3884 {
3885 /* We might be able to shorten the amount of work we do by
3886 only doing this for vtables that come from virtual bases
3887 that have differing offsets, but don't want to miss any
3888 entries. */
3889 fixup_vtable_deltas (vbases, 1, t);
3890 vbases = TREE_CHAIN (vbases);
3891 }
3892 }
3893
8d08fdba
MS
3894 /* Under our model of GC, every C++ class gets its own virtual
3895 function table, at least virtually. */
6b5fbb55 3896 if (pending_virtuals)
8d08fdba
MS
3897 {
3898 pending_virtuals = nreverse (pending_virtuals);
3899 /* We must enter these virtuals into the table. */
3900 if (first_vfn_base_index < 0)
3901 {
aff08c18
JM
3902 if (! CLASSTYPE_COM_INTERFACE (t))
3903 {
3904 /* The second slot is for the tdesc pointer when thunks are used. */
3905 if (flag_vtable_thunks)
3906 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
f30432d7 3907
aff08c18
JM
3908 /* The first slot is for the rtti offset. */
3909 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
6b5fbb55 3910
aff08c18
JM
3911 set_rtti_entry (pending_virtuals,
3912 convert (ssizetype, integer_zero_node), t);
3913 }
8d08fdba
MS
3914 build_vtable (NULL_TREE, t);
3915 }
3916 else
3917 {
3918 /* Here we know enough to change the type of our virtual
3919 function table, but we will wait until later this function. */
3920
3921 if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
3922 build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index), t);
8d08fdba
MS
3923 }
3924
3925 /* If this type has basetypes with constructors, then those
3926 constructors might clobber the virtual function table. But
3927 they don't if the derived class shares the exact vtable of the base
3928 class. */
3929
3930 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
3931 }
3932 else if (first_vfn_base_index >= 0)
3933 {
3934 tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index);
8d08fdba
MS
3935 /* This class contributes nothing new to the virtual function
3936 table. However, it may have declared functions which
3937 went into the virtual function table "inherited" from the
3938 base class. If so, we grab a copy of those updated functions,
3939 and pretend they are ours. */
3940
3941 /* See if we should steal the virtual info from base class. */
3942 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
3943 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
3944 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
3945 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
3946 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
3947 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
3948 }
3949
8d08fdba
MS
3950 if (max_has_virtual || first_vfn_base_index >= 0)
3951 {
8d08fdba
MS
3952 CLASSTYPE_VSIZE (t) = has_virtual;
3953 if (first_vfn_base_index >= 0)
3954 {
3955 if (pending_virtuals)
3956 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t),
3957 pending_virtuals);
3958 }
3959 else if (has_virtual)
3960 {
3961 TYPE_BINFO_VIRTUALS (t) = pending_virtuals;
56ae6d77 3962 DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1;
8d08fdba
MS
3963 }
3964 }
3965
3966 /* Now lay out the virtual function table. */
3967 if (has_virtual)
3968 {
849da744
MM
3969 /* Use size_int so values are memoized in common cases. */
3970 tree itype = build_index_type (size_int (has_virtual));
52bf7d5d 3971 tree atype = build_cplus_array_type (vtable_entry_type, itype);
8d08fdba 3972
849da744 3973 layout_type (atype);
8d08fdba
MS
3974
3975 CLASSTYPE_VFIELD (t) = vfield;
849da744
MM
3976
3977 /* We may have to grow the vtable. */
8d08fdba
MS
3978 if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
3979 {
3980 TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
28cbf42c 3981 DECL_SIZE (TYPE_BINFO_VTABLE (t)) = 0;
8d08fdba
MS
3982 layout_decl (TYPE_BINFO_VTABLE (t), 0);
3983 /* At one time the vtable info was grabbed 2 words at a time. This
3984 fails on sparc unless you have 8-byte alignment. (tiemann) */
3985 DECL_ALIGN (TYPE_BINFO_VTABLE (t))
3986 = MAX (TYPE_ALIGN (double_type_node),
3987 DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
3988 }
3989 }
3990 else if (first_vfn_base_index >= 0)
3991 CLASSTYPE_VFIELD (t) = vfield;
3992 CLASSTYPE_VFIELDS (t) = vfields;
3993
3994 finish_struct_bits (t, max_has_virtual);
3995
f30432d7
MS
3996 /* Complete the rtl for any static member objects of the type we're
3997 working on. */
8d08fdba
MS
3998 for (x = fields; x; x = TREE_CHAIN (x))
3999 {
8d08fdba
MS
4000 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4001 && TREE_TYPE (x) == t)
4002 {
4003 DECL_MODE (x) = TYPE_MODE (t);
4004 make_decl_rtl (x, NULL, 0);
4005 }
4006 }
4007
8d08fdba
MS
4008 if (TYPE_HAS_CONSTRUCTOR (t))
4009 {
4010 tree vfields = CLASSTYPE_VFIELDS (t);
4011
4012 while (vfields)
4013 {
4014 /* Mark the fact that constructor for T
4015 could affect anybody inheriting from T
4016 who wants to initialize vtables for VFIELDS's type. */
4017 if (VF_DERIVED_VALUE (vfields))
4018 TREE_ADDRESSABLE (vfields) = 1;
4019 vfields = TREE_CHAIN (vfields);
4020 }
8d08fdba 4021 }
8d08fdba 4022
700f8a87
MS
4023 /* Write out inline function definitions. */
4024 do_inline_function_hair (t, CLASSTYPE_INLINE_FRIENDS (t));
4025 CLASSTYPE_INLINE_FRIENDS (t) = 0;
8d08fdba
MS
4026
4027 if (CLASSTYPE_VSIZE (t) != 0)
4028 {
2986ae00 4029#if 0
e92cc029 4030 /* This is now done above. */
8d08fdba
MS
4031 if (DECL_FIELD_CONTEXT (vfield) != t)
4032 {
4033 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4034 tree offset = BINFO_OFFSET (binfo);
4035
4036 vfield = copy_node (vfield);
4037 copy_lang_decl (vfield);
4038
4039 if (! integer_zerop (offset))
4040 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4041 DECL_FIELD_CONTEXT (vfield) = t;
4042 DECL_CLASS_CONTEXT (vfield) = t;
4043 DECL_FIELD_BITPOS (vfield)
4044 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4045 CLASSTYPE_VFIELD (t) = vfield;
4046 }
2986ae00 4047#endif
8d08fdba 4048
e92cc029 4049 /* In addition to this one, all the other vfields should be listed. */
8d08fdba
MS
4050 /* Before that can be done, we have to have FIELD_DECLs for them, and
4051 a place to find them. */
4052 TYPE_NONCOPIED_PARTS (t) = build_tree_list (default_conversion (TYPE_BINFO_VTABLE (t)), vfield);
4053
4054 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
c91a56d2 4055 && DECL_VINDEX (TREE_VEC_ELT (method_vec, 1)) == NULL_TREE)
8251199e 4056 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
8d08fdba
MS
4057 t);
4058 }
4059
4060 /* Make the rtl for any new vtables we have created, and unmark
4061 the base types we marked. */
7177d104 4062 finish_vtbls (TYPE_BINFO (t), 1, t);
8145f082 4063 hack_incomplete_structures (t);
8d08fdba 4064
a28e3c7f 4065#if 0
8d08fdba
MS
4066 if (TYPE_NAME (t) && TYPE_IDENTIFIER (t))
4067 undo_template_name_overload (TYPE_IDENTIFIER (t), 1);
a28e3c7f 4068#endif
8d08fdba 4069
8d08fdba
MS
4070 resume_momentary (old);
4071
9e9ff709
MS
4072 if (warn_overloaded_virtual)
4073 warn_hidden (t);
8d08fdba
MS
4074
4075#if 0
4076 /* This has to be done after we have sorted out what to do with
4077 the enclosing type. */
4078 if (write_symbols != DWARF_DEBUG)
4079 {
4080 /* Be smarter about nested classes here. If a type is nested,
4081 only output it if we would output the enclosing type. */
6eb3bb27 4082 if (DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (t)))
d2e5ee5c 4083 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = TREE_ASM_WRITTEN (TYPE_MAIN_DECL (t));
8d08fdba
MS
4084 }
4085#endif
4086
faf5394a 4087 if (write_symbols != DWARF_DEBUG && write_symbols != DWARF2_DEBUG)
8d08fdba
MS
4088 {
4089 /* If the type has methods, we want to think about cutting down
4090 the amount of symbol table stuff we output. The value stored in
4091 the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
4092 For example, if a member function is seen and we decide to
4093 write out that member function, then we can change the value
4094 of the DECL_IGNORED_P slot, and the type will be output when
9a3b49ac
MS
4095 that member function's debug info is written out.
4096
4097 We can't do this with DWARF, which does not support name
4098 references between translation units. */
8d08fdba
MS
4099 if (CLASSTYPE_METHOD_VEC (t))
4100 {
8d08fdba
MS
4101 /* Don't output full info about any type
4102 which does not have its implementation defined here. */
56ae6d77 4103 if (CLASSTYPE_INTERFACE_ONLY (t))
d2e5ee5c 4104 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
5566b478
MS
4105#if 0
4106 /* XXX do something about this. */
8d08fdba
MS
4107 else if (CLASSTYPE_INTERFACE_UNKNOWN (t))
4108 /* Only a first approximation! */
d2e5ee5c 4109 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
5566b478 4110#endif
8d08fdba
MS
4111 }
4112 else if (CLASSTYPE_INTERFACE_ONLY (t))
d2e5ee5c 4113 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
9a3b49ac 4114 }
8d08fdba 4115
d2e5ee5c
MS
4116 /* Finish debugging output for this type. */
4117 rest_of_type_compilation (t, toplevel_bindings_p ());
4118
0aafb128 4119 return;
8d08fdba 4120}
f30432d7 4121
61a127b3
MM
4122/* When T was built up, the member declarations were added in reverse
4123 order. Rearrange them to declaration order. */
4124
4125void
4126unreverse_member_declarations (t)
4127 tree t;
4128{
4129 tree next;
4130 tree prev;
4131 tree x;
4132
4133 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
4134 reverse order. Put them in declaration order now. */
4135 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
4136 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
4137
4138 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
4139 reverse order, so we can't just use nreverse. */
4140 prev = NULL_TREE;
4141 for (x = TYPE_FIELDS (t);
4142 x && TREE_CODE (x) != TYPE_DECL;
4143 x = next)
4144 {
4145 next = TREE_CHAIN (x);
4146 TREE_CHAIN (x) = prev;
4147 prev = x;
4148 }
4149 if (prev)
4150 {
4151 TREE_CHAIN (TYPE_FIELDS (t)) = x;
4152 if (prev)
4153 TYPE_FIELDS (t) = prev;
4154 }
4155}
4156
f30432d7 4157tree
61a127b3
MM
4158finish_struct (t, attributes, warn_anon)
4159 tree t, attributes;
f30432d7
MS
4160 int warn_anon;
4161{
f30432d7 4162 tree name = TYPE_NAME (t);
f30432d7
MS
4163
4164 if (TREE_CODE (name) == TYPE_DECL)
4165 {
4166 extern int lineno;
4167
4168 DECL_SOURCE_FILE (name) = input_filename;
4169 /* For TYPE_DECL that are not typedefs (those marked with a line
4170 number of zero, we don't want to mark them as real typedefs.
4171 If this fails one needs to make sure real typedefs have a
4172 previous line number, even if it is wrong, that way the below
4173 will fill in the right line number. (mrs) */
4174 if (DECL_SOURCE_LINE (name))
4175 DECL_SOURCE_LINE (name) = lineno;
f30432d7
MS
4176 name = DECL_NAME (name);
4177 }
4178
4179 /* Append the fields we need for constructing signature tables. */
4180 if (IS_SIGNATURE (t))
61a127b3 4181 append_signature_fields (t);
f30432d7 4182
61a127b3
MM
4183 /* Now that we've got all the field declarations, reverse everything
4184 as necessary. */
4185 unreverse_member_declarations (t);
f30432d7 4186
6467930b
MS
4187 cplus_decl_attributes (t, attributes, NULL_TREE);
4188
5566b478 4189 if (processing_template_decl)
f30432d7 4190 {
5566b478
MS
4191 tree d = getdecls ();
4192 for (; d; d = TREE_CHAIN (d))
4193 {
4194 /* If this is the decl for the class or one of the template
4195 parms, we've seen all the injected decls. */
4196 if ((TREE_CODE (d) == TYPE_DECL
4197 && (TREE_TYPE (d) == t
73b0fce8
KL
4198 || TREE_CODE (TREE_TYPE (d)) == TEMPLATE_TYPE_PARM
4199 || TREE_CODE (TREE_TYPE (d)) == TEMPLATE_TEMPLATE_PARM))
5566b478
MS
4200 || TREE_CODE (d) == CONST_DECL)
4201 break;
a80e4195
MS
4202 /* Don't inject cache decls. */
4203 else if (IDENTIFIER_TEMPLATE (DECL_NAME (d)))
5566b478
MS
4204 continue;
4205 DECL_TEMPLATE_INJECT (CLASSTYPE_TI_TEMPLATE (t))
4206 = tree_cons (NULL_TREE, d,
4207 DECL_TEMPLATE_INJECT (CLASSTYPE_TI_TEMPLATE (t)));
4208 }
b0e0b31f 4209 finish_struct_methods (t);
5566b478
MS
4210 TYPE_SIZE (t) = integer_zero_node;
4211 }
f30432d7 4212 else
0aafb128 4213 finish_struct_1 (t, warn_anon);
5566b478
MS
4214
4215 TYPE_BEING_DEFINED (t) = 0;
8f032717 4216
5566b478 4217 if (current_class_type)
b74a0560 4218 popclass ();
5566b478 4219 else
8251199e 4220 error ("trying to finish struct, but kicked out due to previous parse errors.");
5566b478
MS
4221
4222 return t;
f30432d7 4223}
8d08fdba 4224\f
51ddb82e 4225/* Return the dynamic type of INSTANCE, if known.
8d08fdba
MS
4226 Used to determine whether the virtual function table is needed
4227 or not.
4228
4229 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4230 of our knowledge of its type. */
e92cc029 4231
d8e178a0 4232static tree
51ddb82e 4233fixed_type_or_null (instance, nonnull)
8d08fdba
MS
4234 tree instance;
4235 int *nonnull;
4236{
4237 switch (TREE_CODE (instance))
4238 {
4239 case INDIRECT_REF:
4240 /* Check that we are not going through a cast of some sort. */
4241 if (TREE_TYPE (instance)
4242 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
4243 instance = TREE_OPERAND (instance, 0);
4244 /* fall through... */
4245 case CALL_EXPR:
4246 /* This is a call to a constructor, hence it's never zero. */
4247 if (TREE_HAS_CONSTRUCTOR (instance))
4248 {
4249 if (nonnull)
4250 *nonnull = 1;
51ddb82e 4251 return TREE_TYPE (instance);
8d08fdba 4252 }
51ddb82e 4253 return NULL_TREE;
8d08fdba
MS
4254
4255 case SAVE_EXPR:
4256 /* This is a call to a constructor, hence it's never zero. */
4257 if (TREE_HAS_CONSTRUCTOR (instance))
4258 {
4259 if (nonnull)
4260 *nonnull = 1;
51ddb82e 4261 return TREE_TYPE (instance);
8d08fdba 4262 }
51ddb82e 4263 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
8d08fdba
MS
4264
4265 case RTL_EXPR:
51ddb82e 4266 return NULL_TREE;
8d08fdba
MS
4267
4268 case PLUS_EXPR:
4269 case MINUS_EXPR:
4270 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
4271 /* Propagate nonnull. */
51ddb82e 4272 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
8d08fdba 4273 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
51ddb82e
JM
4274 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4275 return NULL_TREE;
8d08fdba
MS
4276
4277 case NOP_EXPR:
4278 case CONVERT_EXPR:
51ddb82e 4279 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
8d08fdba
MS
4280
4281 case ADDR_EXPR:
4282 if (nonnull)
4283 *nonnull = 1;
51ddb82e 4284 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
8d08fdba
MS
4285
4286 case COMPONENT_REF:
51ddb82e 4287 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
8d08fdba 4288
8d08fdba
MS
4289 case VAR_DECL:
4290 case FIELD_DECL:
4291 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
4292 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
4293 {
4294 if (nonnull)
4295 *nonnull = 1;
51ddb82e 4296 return TREE_TYPE (TREE_TYPE (instance));
8d08fdba 4297 }
e92cc029 4298 /* fall through... */
8d08fdba
MS
4299 case TARGET_EXPR:
4300 case PARM_DECL:
4301 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
4302 {
4303 if (nonnull)
4304 *nonnull = 1;
51ddb82e 4305 return TREE_TYPE (instance);
8d08fdba
MS
4306 }
4307 else if (nonnull)
4308 {
4ac14744 4309 if (instance == current_class_ptr
8d08fdba
MS
4310 && flag_this_is_variable <= 0)
4311 {
51ddb82e
JM
4312 /* Normally, 'this' must be non-null. */
4313 if (flag_this_is_variable == 0)
4314 *nonnull = 1;
4315
4316 /* <0 means we're in a constructor and we know our type. */
8d08fdba 4317 if (flag_this_is_variable < 0)
51ddb82e 4318 return TREE_TYPE (TREE_TYPE (instance));
8d08fdba
MS
4319 }
4320 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4321 /* Reference variables should be references to objects. */
4322 *nonnull = 1;
4323 }
51ddb82e 4324 return NULL_TREE;
8d08fdba
MS
4325
4326 default:
51ddb82e 4327 return NULL_TREE;
8d08fdba
MS
4328 }
4329}
51ddb82e
JM
4330
4331/* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
4332 to the static type. We also handle the case where INSTANCE is really
4333 a pointer.
4334
4335 Used to determine whether the virtual function table is needed
4336 or not.
4337
4338 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4339 of our knowledge of its type. */
4340
4341int
4342resolves_to_fixed_type_p (instance, nonnull)
4343 tree instance;
4344 int *nonnull;
4345{
4346 tree t = TREE_TYPE (instance);
4347 tree fixed = fixed_type_or_null (instance, nonnull);
4348 if (fixed == NULL_TREE)
4349 return 0;
4350 if (POINTER_TYPE_P (t))
4351 t = TREE_TYPE (t);
3bfdc719 4352 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
51ddb82e
JM
4353}
4354
8d08fdba
MS
4355\f
4356void
4357init_class_processing ()
4358{
4359 current_class_depth = 0;
61a127b3
MM
4360 current_class_stack_size = 10;
4361 current_class_stack
4362 = (class_stack_node_t) xmalloc (current_class_stack_size
4363 * sizeof (struct class_stack_node));
8d08fdba
MS
4364
4365 current_lang_stacksize = 10;
4366 current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree));
4367 current_lang_stack = current_lang_base;
4368
be99da77
MS
4369 access_default_node = build_int_2 (0, 0);
4370 access_public_node = build_int_2 (1, 0);
4371 access_protected_node = build_int_2 (2, 0);
4372 access_private_node = build_int_2 (3, 0);
4373 access_default_virtual_node = build_int_2 (4, 0);
4374 access_public_virtual_node = build_int_2 (5, 0);
d8b55a76
JM
4375 access_protected_virtual_node = build_int_2 (6, 0);
4376 access_private_virtual_node = build_int_2 (7, 0);
be99da77 4377
8d08fdba 4378 /* Keep these values lying around. */
8d08fdba
MS
4379 base_layout_decl = build_lang_field_decl (FIELD_DECL, NULL_TREE, error_mark_node);
4380 TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE);
4381
4382 gcc_obstack_init (&class_obstack);
4383}
4384
4385/* Set current scope to NAME. CODE tells us if this is a
4386 STRUCT, UNION, or ENUM environment.
4387
4388 NAME may end up being NULL_TREE if this is an anonymous or
4389 late-bound struct (as in "struct { ... } foo;") */
4390
4391/* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
4392 appropriate values, found by looking up the type definition of
4393 NAME (as a CODE).
4394
4395 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
4396 which can be seen locally to the class. They are shadowed by
4397 any subsequent local declaration (including parameter names).
4398
4399 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
4400 which have static meaning (i.e., static members, static
4401 member functions, enum declarations, etc).
4402
4403 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
4404 which can be seen locally to the class (as in 1), but
4405 know that we are doing this for declaration purposes
4406 (i.e. friend foo::bar (int)).
4407
4408 So that we may avoid calls to lookup_name, we cache the _TYPE
4409 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
4410
4411 For multiple inheritance, we perform a two-pass depth-first search
4412 of the type lattice. The first pass performs a pre-order search,
4413 marking types after the type has had its fields installed in
4414 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
4415 unmarks the marked types. If a field or member function name
4416 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
4417 that name becomes `error_mark_node'. */
4418
4419void
4420pushclass (type, modify)
4421 tree type;
4422 int modify;
4423{
7fb4a8f7 4424 type = TYPE_MAIN_VARIANT (type);
8d08fdba 4425
61a127b3
MM
4426 /* Make sure there is enough room for the new entry on the stack. */
4427 if (current_class_depth + 1 >= current_class_stack_size)
8d08fdba 4428 {
61a127b3
MM
4429 current_class_stack_size *= 2;
4430 current_class_stack
4431 = (class_stack_node_t) xrealloc (current_class_stack,
4432 current_class_stack_size
4433 * sizeof (struct class_stack_node));
8d08fdba
MS
4434 }
4435
61a127b3
MM
4436 /* Insert a new entry on the class stack. */
4437 current_class_stack[current_class_depth].name = current_class_name;
4438 current_class_stack[current_class_depth].type = current_class_type;
4439 current_class_stack[current_class_depth].access = current_access_specifier;
8f032717 4440 current_class_stack[current_class_depth].names_used = 0;
61a127b3
MM
4441 current_class_depth++;
4442
4443 /* Now set up the new type. */
8d08fdba
MS
4444 current_class_name = TYPE_NAME (type);
4445 if (TREE_CODE (current_class_name) == TYPE_DECL)
4446 current_class_name = DECL_NAME (current_class_name);
4447 current_class_type = type;
4448
61a127b3
MM
4449 /* By default, things in classes are private, while things in
4450 structures or unions are public. */
4451 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
4452 ? access_private_node
4453 : access_public_node);
4454
8d08fdba 4455 if (previous_class_type != NULL_TREE
8f032717
MM
4456 && (type != previous_class_type
4457 || TYPE_SIZE (previous_class_type) == NULL_TREE)
8d08fdba
MS
4458 && current_class_depth == 1)
4459 {
4460 /* Forcibly remove any old class remnants. */
8f032717 4461 invalidate_class_lookup_cache ();
7ddedda4
MM
4462
4463 /* Now, free the obstack on which we cached all the values. */
4464 obstack_free (&class_cache_obstack, class_cache_firstobj);
4465 class_cache_firstobj
4466 = (char*) obstack_finish (&class_cache_obstack);
8d08fdba
MS
4467 }
4468
8f032717
MM
4469 /* If we're about to enter a nested class, clear
4470 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
4471 if (modify && current_class_depth > 1)
4472 clear_identifier_class_values ();
4473
8d08fdba
MS
4474 pushlevel_class ();
4475
37c46b43 4476#if 0
5566b478
MS
4477 if (CLASSTYPE_TEMPLATE_INFO (type))
4478 overload_template_name (type);
37c46b43 4479#endif
5566b478 4480
8d08fdba
MS
4481 if (modify)
4482 {
5566b478 4483 if (type != previous_class_type || current_class_depth > 1)
8f032717 4484 push_class_decls (type);
8d08fdba
MS
4485 else
4486 {
4487 tree item;
4488
f181d4ae
MM
4489 /* We are re-entering the same class we just left, so we
4490 don't have to search the whole inheritance matrix to find
4491 all the decls to bind again. Instead, we install the
4492 cached class_shadowed list, and walk through it binding
4493 names and setting up IDENTIFIER_TYPE_VALUEs. */
8d08fdba
MS
4494 set_class_shadows (previous_class_values);
4495 for (item = previous_class_values; item; item = TREE_CHAIN (item))
4496 {
4497 tree id = TREE_PURPOSE (item);
d8f8dca1 4498 tree decl = TREE_TYPE (item);
8d08fdba 4499
f181d4ae 4500 push_class_binding (id, decl);
8d08fdba
MS
4501 if (TREE_CODE (decl) == TYPE_DECL)
4502 set_identifier_type_value (id, TREE_TYPE (decl));
4503 }
4504 unuse_fields (type);
4505 }
4506
280f9385 4507 storetags (CLASSTYPE_TAGS (type));
8f032717
MM
4508 }
4509}
4510
4511/* When we exit a toplevel class scope, we save the
4512 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
4513 reenter the class. Here, we've entered some other class, so we
4514 must invalidate our cache. */
8d08fdba 4515
8f032717
MM
4516void
4517invalidate_class_lookup_cache ()
4518{
8f032717
MM
4519 tree t;
4520
4521 /* This code can be seen as a cache miss. When we've cached a
4522 class' scope's bindings and we can't use them, we need to reset
4523 them. This is it! */
4524 for (t = previous_class_values; t; t = TREE_CHAIN (t))
4525 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
8f032717
MM
4526
4527 previous_class_type = NULL_TREE;
8d08fdba
MS
4528}
4529
4530/* Get out of the current class scope. If we were in a class scope
b74a0560 4531 previously, that is the one popped to. */
e92cc029 4532
8d08fdba 4533void
b74a0560 4534popclass ()
8d08fdba 4535{
b74a0560 4536 poplevel (1, 0, 0);
8d08fdba 4537 /* Since poplevel_class does the popping of class decls nowadays,
b74a0560
MM
4538 this really only frees the obstack used for these decls. */
4539 pop_class_decls ();
8d08fdba
MS
4540
4541 current_class_depth--;
61a127b3
MM
4542 current_class_name = current_class_stack[current_class_depth].name;
4543 current_class_type = current_class_stack[current_class_depth].type;
4544 current_access_specifier = current_class_stack[current_class_depth].access;
8f032717
MM
4545 if (current_class_stack[current_class_depth].names_used)
4546 splay_tree_delete (current_class_stack[current_class_depth].names_used);
8d08fdba
MS
4547}
4548
b9082e8a
JM
4549/* Returns 1 if current_class_type is either T or a nested type of T. */
4550
4551int
4552currently_open_class (t)
4553 tree t;
4554{
4555 int i;
4556 if (t == current_class_type)
4557 return 1;
4558 for (i = 0; i < current_class_depth; ++i)
61a127b3 4559 if (current_class_stack [i].type == t)
b9082e8a
JM
4560 return 1;
4561 return 0;
4562}
4563
8d08fdba
MS
4564/* When entering a class scope, all enclosing class scopes' names with
4565 static meaning (static variables, static functions, types and enumerators)
4566 have to be visible. This recursive function calls pushclass for all
4567 enclosing class contexts until global or a local scope is reached.
4568 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
4569 formal of the same name. */
4570
4571void
4572push_nested_class (type, modify)
4573 tree type;
4574 int modify;
4575{
a28e3c7f
MS
4576 tree context;
4577
b262d64c 4578 /* A namespace might be passed in error cases, like A::B:C. */
5566b478 4579 if (type == NULL_TREE || type == error_mark_node || ! IS_AGGR_TYPE (type)
b262d64c 4580 || TREE_CODE (type) == NAMESPACE_DECL
73b0fce8
KL
4581 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
4582 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
a28e3c7f
MS
4583 return;
4584
d2e5ee5c 4585 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
8d08fdba 4586
6b400b21 4587 if (context && CLASS_TYPE_P (context))
8d08fdba
MS
4588 push_nested_class (context, 2);
4589 pushclass (type, modify);
4590}
4591
4592/* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
4593
4594void
b74a0560 4595pop_nested_class ()
8d08fdba 4596{
d2e5ee5c 4597 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8d08fdba 4598
b74a0560 4599 popclass ();
6b400b21 4600 if (context && CLASS_TYPE_P (context))
b74a0560 4601 pop_nested_class ();
8d08fdba
MS
4602}
4603
4604/* Set global variables CURRENT_LANG_NAME to appropriate value
4605 so that behavior of name-mangling machinery is correct. */
4606
4607void
4608push_lang_context (name)
4609 tree name;
4610{
4611 *current_lang_stack++ = current_lang_name;
4612 if (current_lang_stack >= current_lang_base + current_lang_stacksize)
4613 {
beb53fb8
JM
4614 current_lang_base
4615 = (tree *)xrealloc (current_lang_base,
4616 sizeof (tree) * (current_lang_stacksize + 10));
8d08fdba
MS
4617 current_lang_stack = current_lang_base + current_lang_stacksize;
4618 current_lang_stacksize += 10;
4619 }
4620
a1774733 4621 if (name == lang_name_cplusplus || name == lang_name_java)
8d08fdba
MS
4622 {
4623 strict_prototype = strict_prototypes_lang_cplusplus;
4624 current_lang_name = name;
4625 }
4626 else if (name == lang_name_c)
4627 {
4628 strict_prototype = strict_prototypes_lang_c;
4629 current_lang_name = name;
4630 }
4631 else
8251199e 4632 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
8d08fdba
MS
4633}
4634
4635/* Get out of the current language scope. */
e92cc029 4636
8d08fdba
MS
4637void
4638pop_lang_context ()
4639{
8d08fdba 4640 current_lang_name = *--current_lang_stack;
eff71ab0
PB
4641 if (current_lang_name == lang_name_cplusplus
4642 || current_lang_name == lang_name_java)
8d08fdba
MS
4643 strict_prototype = strict_prototypes_lang_cplusplus;
4644 else if (current_lang_name == lang_name_c)
4645 strict_prototype = strict_prototypes_lang_c;
4646}
8d08fdba
MS
4647\f
4648/* Type instantiation routines. */
4649
104bf76a
MM
4650/* Given an OVERLOAD and a TARGET_TYPE, return the function that
4651 matches the TARGET_TYPE. If there is no satisfactory match, return
4652 error_mark_node, and issue an error message if COMPLAIN is
4653 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
4654 was a template-id, and EXPLICIT_TARGS are the explicitly provided
4655 template arguments. */
4656
2c73f9f5 4657static tree
104bf76a
MM
4658resolve_address_of_overloaded_function (target_type,
4659 overload,
4660 complain,
4661 template_only,
4662 explicit_targs)
4663 tree target_type;
4664 tree overload;
2c73f9f5 4665 int complain;
104bf76a
MM
4666 int template_only;
4667 tree explicit_targs;
2c73f9f5 4668{
104bf76a
MM
4669 /* Here's what the standard says:
4670
4671 [over.over]
4672
4673 If the name is a function template, template argument deduction
4674 is done, and if the argument deduction succeeds, the deduced
4675 arguments are used to generate a single template function, which
4676 is added to the set of overloaded functions considered.
4677
4678 Non-member functions and static member functions match targets of
4679 type "pointer-to-function" or "reference-to-function." Nonstatic
4680 member functions match targets of type "pointer-to-member
4681 function;" the function type of the pointer to member is used to
4682 select the member function from the set of overloaded member
4683 functions. If a nonstatic member function is selected, the
4684 reference to the overloaded function name is required to have the
4685 form of a pointer to member as described in 5.3.1.
4686
4687 If more than one function is selected, any template functions in
4688 the set are eliminated if the set also contains a non-template
4689 function, and any given template function is eliminated if the
4690 set contains a second template function that is more specialized
4691 than the first according to the partial ordering rules 14.5.5.2.
4692 After such eliminations, if any, there shall remain exactly one
4693 selected function. */
4694
4695 int is_ptrmem = 0;
4696 int is_reference = 0;
4697 /* We store the matches in a TREE_LIST rooted here. The functions
4698 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
4699 interoperability with most_specialized_instantiation. */
4700 tree matches = NULL_TREE;
50714e79 4701 tree fn;
104bf76a 4702
d8f8dca1
MM
4703 /* By the time we get here, we should be seeing only real
4704 pointer-to-member types, not the internal POINTER_TYPE to
4705 METHOD_TYPE representation. */
4706 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
4707 && (TREE_CODE (TREE_TYPE (target_type))
4708 == METHOD_TYPE)), 0);
104bf76a
MM
4709
4710 /* Check that the TARGET_TYPE is reasonable. */
4711 if (TYPE_PTRFN_P (target_type))
4712 /* This is OK. */
4713 ;
4714 else if (TYPE_PTRMEMFUNC_P (target_type))
4715 /* This is OK, too. */
4716 is_ptrmem = 1;
4717 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
4718 {
4719 /* This is OK, too. This comes from a conversion to reference
4720 type. */
4721 target_type = build_reference_type (target_type);
4722 is_reference = 1;
4723 }
4724 else
4725 {
4726 if (complain)
4727 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
4728 DECL_NAME (OVL_FUNCTION (overload)), target_type);
4729 return error_mark_node;
4730 }
4731
4732 /* If we can find a non-template function that matches, we can just
4733 use it. There's no point in generating template instantiations
4734 if we're just going to throw them out anyhow. But, of course, we
4735 can only do this when we don't *need* a template function. */
4736 if (!template_only)
4737 {
4738 tree fns;
4739
4740 for (fns = overload; fns; fns = OVL_CHAIN (fns))
4741 {
4742 tree fn = OVL_FUNCTION (fns);
4743 tree fntype;
2c73f9f5 4744
104bf76a
MM
4745 if (TREE_CODE (fn) == TEMPLATE_DECL)
4746 /* We're not looking for templates just yet. */
4747 continue;
4748
4749 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4750 != is_ptrmem)
4751 /* We're looking for a non-static member, and this isn't
4752 one, or vice versa. */
4753 continue;
4754
4755 /* See if there's a match. */
4756 fntype = TREE_TYPE (fn);
4757 if (is_ptrmem)
4758 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
4759 else if (!is_reference)
4760 fntype = build_pointer_type (fntype);
4761
4762 if (can_convert_arg (target_type, fntype, fn))
4763 matches = scratch_tree_cons (fn, NULL_TREE, matches);
4764 }
4765 }
4766
4767 /* Now, if we've already got a match (or matches), there's no need
4768 to proceed to the template functions. But, if we don't have a
4769 match we need to look at them, too. */
4770 if (!matches)
2c73f9f5 4771 {
104bf76a
MM
4772 tree target_fn_type;
4773 tree target_arg_types;
4774 tree fns;
4775
4776 if (is_ptrmem)
4393e105
MM
4777 target_fn_type
4778 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
2c73f9f5 4779 else
4393e105
MM
4780 target_fn_type = TREE_TYPE (target_type);
4781 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
4782
104bf76a
MM
4783 for (fns = overload; fns; fns = OVL_CHAIN (fns))
4784 {
4785 tree fn = OVL_FUNCTION (fns);
104bf76a
MM
4786 tree instantiation;
4787 tree instantiation_type;
4788 tree targs;
4789
4790 if (TREE_CODE (fn) != TEMPLATE_DECL)
4791 /* We're only looking for templates. */
4792 continue;
4793
4794 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4795 != is_ptrmem)
4393e105 4796 /* We're not looking for a non-static member, and this is
104bf76a
MM
4797 one, or vice versa. */
4798 continue;
4799
104bf76a
MM
4800 /* Try to do argument deduction. */
4801 targs = make_scratch_vec (DECL_NTPARMS (fn));
4393e105
MM
4802 if (fn_type_unification (fn, explicit_targs, targs,
4803 target_arg_types, NULL_TREE,
03017874 4804 DEDUCE_EXACT) != 0)
104bf76a
MM
4805 /* Argument deduction failed. */
4806 continue;
4807
4808 /* Instantiate the template. */
4809 instantiation = instantiate_template (fn, targs);
4810 if (instantiation == error_mark_node)
4811 /* Instantiation failed. */
4812 continue;
4813
4814 /* See if there's a match. */
4815 instantiation_type = TREE_TYPE (instantiation);
4816 if (is_ptrmem)
4817 instantiation_type =
4818 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
4819 else if (!is_reference)
4820 instantiation_type = build_pointer_type (instantiation_type);
4821 if (can_convert_arg (target_type, instantiation_type, instantiation))
4822 matches = scratch_tree_cons (instantiation, fn, matches);
4823 }
4824
4825 /* Now, remove all but the most specialized of the matches. */
4826 if (matches)
4827 {
4828 tree match = most_specialized_instantiation (matches,
4829 explicit_targs);
4830
4831 if (match != error_mark_node)
4832 matches = scratch_tree_cons (match, NULL_TREE, NULL_TREE);
4833 }
4834 }
4835
4836 /* Now we should have exactly one function in MATCHES. */
4837 if (matches == NULL_TREE)
4838 {
4839 /* There were *no* matches. */
4840 if (complain)
4841 {
6b9b6b15 4842 cp_error ("no matches converting function `%D' to type `%#T'",
104bf76a
MM
4843 DECL_NAME (OVL_FUNCTION (overload)),
4844 target_type);
6b9b6b15
JM
4845
4846 /* print_candidates expects a chain with the functions in
4847 TREE_VALUE slots, so we cons one up here (we're losing anyway,
4848 so why be clever?). */
4849 for (; overload; overload = OVL_NEXT (overload))
4850 matches = scratch_tree_cons (NULL_TREE, OVL_CURRENT (overload),
4851 matches);
4852
4853 print_candidates (matches);
104bf76a
MM
4854 }
4855 return error_mark_node;
2c73f9f5 4856 }
104bf76a
MM
4857 else if (TREE_CHAIN (matches))
4858 {
4859 /* There were too many matches. */
4860
4861 if (complain)
4862 {
4863 tree match;
4864
4865 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
4866 DECL_NAME (OVL_FUNCTION (overload)),
4867 target_type);
4868
4869 /* Since print_candidates expects the functions in the
4870 TREE_VALUE slot, we flip them here. */
4871 for (match = matches; match; match = TREE_CHAIN (match))
4872 TREE_VALUE (match) = TREE_PURPOSE (match);
4873
4874 print_candidates (matches);
4875 }
4876
4877 return error_mark_node;
4878 }
4879
50714e79
MM
4880 /* Good, exactly one match. Now, convert it to the correct type. */
4881 fn = TREE_PURPOSE (matches);
4882
a6ecf8b6
JM
4883 mark_used (fn);
4884
50714e79
MM
4885 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
4886 return build_unary_op (ADDR_EXPR, fn, 0);
4887 else
4888 {
4889 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
4890 will mark the function as addressed, but here we must do it
4891 explicitly. */
4892 mark_addressable (fn);
4893
4894 return fn;
4895 }
2c73f9f5
ML
4896}
4897
ec255269
MS
4898/* This function will instantiate the type of the expression given in
4899 RHS to match the type of LHSTYPE. If errors exist, then return
2036a15c 4900 error_mark_node. We only complain is COMPLAIN is set. If we are
ec255269
MS
4901 not complaining, never modify rhs, as overload resolution wants to
4902 try many possible instantiations, in hopes that at least one will
4903 work.
8d08fdba 4904
940ff223
JM
4905 FLAGS is a bitmask, as we see at the top of the function.
4906
e6e174e5
JM
4907 For non-recursive calls, LHSTYPE should be a function, pointer to
4908 function, or a pointer to member function. */
e92cc029 4909
8d08fdba 4910tree
940ff223 4911instantiate_type (lhstype, rhs, flags)
8d08fdba 4912 tree lhstype, rhs;
940ff223 4913 int flags;
8d08fdba 4914{
940ff223
JM
4915 int complain = (flags & 1);
4916 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
4917
8d08fdba
MS
4918 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
4919 {
4920 if (complain)
8251199e 4921 error ("not enough type information");
8d08fdba
MS
4922 return error_mark_node;
4923 }
4924
4925 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
abff8e06 4926 {
940ff223 4927 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
abff8e06
JM
4928 return rhs;
4929 if (complain)
8251199e 4930 cp_error ("argument of type `%T' does not match `%T'",
abff8e06
JM
4931 TREE_TYPE (rhs), lhstype);
4932 return error_mark_node;
4933 }
8d08fdba 4934
2c73f9f5
ML
4935 /* We don't overwrite rhs if it is an overloaded function.
4936 Copying it would destroy the tree link. */
4937 if (TREE_CODE (rhs) != OVERLOAD)
4938 rhs = copy_node (rhs);
c73964b2 4939
8d08fdba
MS
4940 /* This should really only be used when attempting to distinguish
4941 what sort of a pointer to function we have. For now, any
4942 arithmetic operation which is not supported on pointers
4943 is rejected as an error. */
4944
4945 switch (TREE_CODE (rhs))
4946 {
4947 case TYPE_EXPR:
4948 case CONVERT_EXPR:
4949 case SAVE_EXPR:
4950 case CONSTRUCTOR:
4951 case BUFFER_REF:
4952 my_friendly_abort (177);
4953 return error_mark_node;
4954
4955 case INDIRECT_REF:
4956 case ARRAY_REF:
ec255269
MS
4957 {
4958 tree new_rhs;
8d08fdba 4959
ec255269 4960 new_rhs = instantiate_type (build_pointer_type (lhstype),
940ff223 4961 TREE_OPERAND (rhs, 0), flags);
ec255269
MS
4962 if (new_rhs == error_mark_node)
4963 return error_mark_node;
4964
4965 TREE_TYPE (rhs) = lhstype;
4966 TREE_OPERAND (rhs, 0) = new_rhs;
4967 return rhs;
4968 }
8d08fdba
MS
4969
4970 case NOP_EXPR:
4971 rhs = copy_node (TREE_OPERAND (rhs, 0));
4972 TREE_TYPE (rhs) = unknown_type_node;
940ff223 4973 return instantiate_type (lhstype, rhs, flags);
8d08fdba
MS
4974
4975 case COMPONENT_REF:
4976 {
4977 tree field = TREE_OPERAND (rhs, 1);
50714e79
MM
4978 tree r;
4979
940ff223 4980 r = instantiate_type (lhstype, field, flags);
50714e79
MM
4981
4982 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype))
4983 {
e6f62286 4984 if (complain)
8452b1d3 4985 {
e6f62286 4986 tree t = TYPE_PTRMEMFUNC_OBJECT_TYPE (lhstype);
8f032717
MM
4987
4988 if (TREE_CODE (field) == OVERLOAD)
4989 field = OVL_FUNCTION (field);
4990 if (TREE_CODE (field) == FUNCTION_DECL)
e6f62286
JM
4991 {
4992 cp_error ("object-dependent reference `%E' can only be used in a call",
8f032717 4993 DECL_NAME (field));
e6f62286 4994 cp_error (" to form a pointer to member function, say `&%T::%E'",
8f032717 4995 t, DECL_NAME (field));
e6f62286
JM
4996 }
4997 else
4998 cp_error ("object-dependent reference can only be used in a call");
8452b1d3 4999 }
50714e79 5000 return error_mark_node;
8d08fdba 5001 }
50714e79
MM
5002
5003 return r;
8d08fdba
MS
5004 }
5005
2a238a97 5006 case OFFSET_REF:
05e0b2f4
JM
5007 rhs = TREE_OPERAND (rhs, 1);
5008 if (BASELINK_P (rhs))
5009 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5010
2a238a97
MM
5011 /* This can happen if we are forming a pointer-to-member for a
5012 member template. */
2a238a97 5013 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
05e0b2f4 5014
2a238a97 5015 /* Fall through. */
874503bc 5016
386b8a85 5017 case TEMPLATE_ID_EXPR:
104bf76a
MM
5018 return
5019 resolve_address_of_overloaded_function (lhstype,
5020 TREE_OPERAND (rhs, 0),
5021 complain,
5022 /*template_only=*/1,
5023 TREE_OPERAND (rhs, 1));
386b8a85 5024
2c73f9f5 5025 case OVERLOAD:
104bf76a
MM
5026 return
5027 resolve_address_of_overloaded_function (lhstype,
5028 rhs,
5029 complain,
5030 /*template_only=*/0,
5031 /*explicit_targs=*/NULL_TREE);
2c73f9f5
ML
5032
5033 case TREE_LIST:
940ff223
JM
5034 /* Now we should have a baselink. */
5035 my_friendly_assert (BASELINK_P (rhs), 990412);
e5966228 5036
940ff223 5037 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
8d08fdba
MS
5038
5039 case CALL_EXPR:
5040 /* This is too hard for now. */
5041 my_friendly_abort (183);
5042 return error_mark_node;
5043
5044 case PLUS_EXPR:
5045 case MINUS_EXPR:
5046 case COMPOUND_EXPR:
a0a33927 5047 TREE_OPERAND (rhs, 0)
940ff223 5048 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
8d08fdba
MS
5049 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5050 return error_mark_node;
a0a33927 5051 TREE_OPERAND (rhs, 1)
940ff223 5052 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
8d08fdba
MS
5053 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5054 return error_mark_node;
5055
5056 TREE_TYPE (rhs) = lhstype;
5057 return rhs;
5058
5059 case MULT_EXPR:
5060 case TRUNC_DIV_EXPR:
5061 case FLOOR_DIV_EXPR:
5062 case CEIL_DIV_EXPR:
5063 case ROUND_DIV_EXPR:
5064 case RDIV_EXPR:
5065 case TRUNC_MOD_EXPR:
5066 case FLOOR_MOD_EXPR:
5067 case CEIL_MOD_EXPR:
5068 case ROUND_MOD_EXPR:
5069 case FIX_ROUND_EXPR:
5070 case FIX_FLOOR_EXPR:
5071 case FIX_CEIL_EXPR:
5072 case FIX_TRUNC_EXPR:
5073 case FLOAT_EXPR:
5074 case NEGATE_EXPR:
5075 case ABS_EXPR:
5076 case MAX_EXPR:
5077 case MIN_EXPR:
5078 case FFS_EXPR:
5079
5080 case BIT_AND_EXPR:
5081 case BIT_IOR_EXPR:
5082 case BIT_XOR_EXPR:
5083 case LSHIFT_EXPR:
5084 case RSHIFT_EXPR:
5085 case LROTATE_EXPR:
5086 case RROTATE_EXPR:
5087
5088 case PREINCREMENT_EXPR:
5089 case PREDECREMENT_EXPR:
5090 case POSTINCREMENT_EXPR:
5091 case POSTDECREMENT_EXPR:
5092 if (complain)
8251199e 5093 error ("invalid operation on uninstantiated type");
8d08fdba
MS
5094 return error_mark_node;
5095
5096 case TRUTH_AND_EXPR:
5097 case TRUTH_OR_EXPR:
5098 case TRUTH_XOR_EXPR:
5099 case LT_EXPR:
5100 case LE_EXPR:
5101 case GT_EXPR:
5102 case GE_EXPR:
5103 case EQ_EXPR:
5104 case NE_EXPR:
5105 case TRUTH_ANDIF_EXPR:
5106 case TRUTH_ORIF_EXPR:
5107 case TRUTH_NOT_EXPR:
5108 if (complain)
8251199e 5109 error ("not enough type information");
8d08fdba
MS
5110 return error_mark_node;
5111
5112 case COND_EXPR:
5113 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
5114 {
5115 if (complain)
8251199e 5116 error ("not enough type information");
8d08fdba
MS
5117 return error_mark_node;
5118 }
a0a33927 5119 TREE_OPERAND (rhs, 1)
940ff223 5120 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
8d08fdba
MS
5121 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5122 return error_mark_node;
a0a33927 5123 TREE_OPERAND (rhs, 2)
940ff223 5124 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
8d08fdba
MS
5125 if (TREE_OPERAND (rhs, 2) == error_mark_node)
5126 return error_mark_node;
5127
5128 TREE_TYPE (rhs) = lhstype;
5129 return rhs;
5130
5131 case MODIFY_EXPR:
a0a33927 5132 TREE_OPERAND (rhs, 1)
940ff223 5133 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
8d08fdba
MS
5134 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5135 return error_mark_node;
5136
5137 TREE_TYPE (rhs) = lhstype;
5138 return rhs;
5139
5140 case ADDR_EXPR:
940ff223 5141 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
8d08fdba
MS
5142
5143 case ENTRY_VALUE_EXPR:
5144 my_friendly_abort (184);
5145 return error_mark_node;
5146
5147 case ERROR_MARK:
5148 return error_mark_node;
5149
5150 default:
5151 my_friendly_abort (185);
5152 return error_mark_node;
5153 }
5154}
5155\f
5156/* Return the name of the virtual function pointer field
5157 (as an IDENTIFIER_NODE) for the given TYPE. Note that
5158 this may have to look back through base types to find the
5159 ultimate field name. (For single inheritance, these could
5160 all be the same name. Who knows for multiple inheritance). */
e92cc029 5161
8d08fdba
MS
5162static tree
5163get_vfield_name (type)
5164 tree type;
5165{
5166 tree binfo = TYPE_BINFO (type);
5167 char *buf;
5168
5169 while (BINFO_BASETYPES (binfo)
5170 && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
5171 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
5172 binfo = BINFO_BASETYPE (binfo, 0);
5173
5174 type = BINFO_TYPE (binfo);
2636fde4
JM
5175 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
5176 + TYPE_NAME_LENGTH (type) + 2);
8d08fdba
MS
5177 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
5178 return get_identifier (buf);
5179}
5180
5181void
5182print_class_statistics ()
5183{
5184#ifdef GATHER_STATISTICS
5185 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
5186 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
5187 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
5188 n_build_method_call, n_inner_fields_searched);
5189 if (n_vtables)
5190 {
5191 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
5192 n_vtables, n_vtable_searches);
5193 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
5194 n_vtable_entries, n_vtable_elems);
5195 }
5196#endif
5197}
5198
5199/* Push an obstack which is sufficiently long-lived to hold such class
7ddedda4
MM
5200 decls that may be cached in the previous_class_values list. The
5201 effect is undone by pop_obstacks. */
c91a56d2 5202
8d08fdba
MS
5203void
5204maybe_push_cache_obstack ()
5205{
7ddedda4
MM
5206 static int cache_obstack_initialized;
5207
5208 if (!cache_obstack_initialized)
5209 {
5210 gcc_obstack_init (&class_cache_obstack);
5211 class_cache_firstobj
5212 = (char*) obstack_finish (&class_cache_obstack);
5213 cache_obstack_initialized = 1;
5214 }
5215
8d08fdba 5216 push_obstacks_nochange ();
7ddedda4 5217 current_obstack = &class_cache_obstack;
8d08fdba 5218}
c91a56d2
MS
5219
5220/* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
5221 according to [class]:
5222 The class-name is also inserted
5223 into the scope of the class itself. For purposes of access checking,
5224 the inserted class name is treated as if it were a public member name. */
5225
d6479fe7 5226void
c91a56d2
MS
5227build_self_reference ()
5228{
5229 tree name = constructor_name (current_class_type);
5230 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
d6479fe7
MM
5231 tree saved_cas;
5232
c91a56d2
MS
5233 DECL_NONLOCAL (value) = 1;
5234 DECL_CONTEXT (value) = current_class_type;
5235 DECL_CLASS_CONTEXT (value) = current_class_type;
c91a56d2
MS
5236 DECL_ARTIFICIAL (value) = 1;
5237
d6479fe7
MM
5238 saved_cas = current_access_specifier;
5239 current_access_specifier = access_public_node;
5240 finish_member_declaration (value);
5241 current_access_specifier = saved_cas;
c91a56d2 5242}
570221c2
JM
5243
5244/* Returns 1 if TYPE contains only padding bytes. */
5245
5246int
5247is_empty_class (type)
5248 tree type;
5249{
5250 tree t;
5251
5a11e05b
BK
5252 if (type == error_mark_node)
5253 return 0;
5254
a59ca936
JM
5255 if (! IS_AGGR_TYPE (type))
5256 return 0;
5257
5258 if (flag_new_abi)
5259 return CLASSTYPE_SIZE (type) == integer_zero_node;
5260
5261 if (TYPE_BINFO_BASETYPES (type))
570221c2
JM
5262 return 0;
5263 t = TYPE_FIELDS (type);
5264 while (t && TREE_CODE (t) != FIELD_DECL)
5265 t = TREE_CHAIN (t);
5266 return (t == NULL_TREE);
5267}
b54ccf71
JM
5268
5269/* Find the enclosing class of the given NODE. NODE can be a *_DECL or
5270 a *_TYPE node. NODE can also be a local class. */
5271
5272tree
5273get_enclosing_class (type)
5274 tree type;
5275{
5276 tree node = type;
5277
5278 while (node && TREE_CODE (node) != NAMESPACE_DECL)
5279 {
5280 switch (TREE_CODE_CLASS (TREE_CODE (node)))
5281 {
5282 case 'd':
5283 node = DECL_CONTEXT (node);
5284 break;
5285
5286 case 't':
5287 if (node != type)
5288 return node;
5289 node = TYPE_CONTEXT (node);
5290 break;
5291
5292 default:
5293 my_friendly_abort (0);
5294 }
5295 }
5296 return NULL_TREE;
5297}
5298
5299/* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
5300
5301int
5302is_base_of_enclosing_class (base, type)
5303 tree base, type;
5304{
5305 while (type)
5306 {
5307 if (get_binfo (base, type, 0))
5308 return 1;
5309
5310 type = get_enclosing_class (type);
5311 }
5312 return 0;
5313}
8f032717
MM
5314
5315/* Note that NAME was looked up while the current class was being
5316 defined and that the result of that lookup was DECL. */
5317
5318void
5319maybe_note_name_used_in_class (name, decl)
5320 tree name;
5321 tree decl;
5322{
5323 splay_tree names_used;
5324
5325 /* If we're not defining a class, there's nothing to do. */
5326 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
5327 return;
5328
5329 /* If there's already a binding for this NAME, then we don't have
5330 anything to worry about. */
5331 if (IDENTIFIER_CLASS_VALUE (name))
5332 return;
5333
5334 if (!current_class_stack[current_class_depth - 1].names_used)
5335 current_class_stack[current_class_depth - 1].names_used
5336 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
5337 names_used = current_class_stack[current_class_depth - 1].names_used;
5338
5339 splay_tree_insert (names_used,
5340 (splay_tree_key) name,
5341 (splay_tree_value) decl);
5342}
5343
5344/* Note that NAME was declared (as DECL) in the current class. Check
5345 to see that the declaration is legal. */
5346
5347void
5348note_name_declared_in_class (name, decl)
5349 tree name;
5350 tree decl;
5351{
5352 splay_tree names_used;
5353 splay_tree_node n;
5354
5355 /* Look to see if we ever used this name. */
5356 names_used
5357 = current_class_stack[current_class_depth - 1].names_used;
5358 if (!names_used)
5359 return;
5360
5361 n = splay_tree_lookup (names_used, (splay_tree_key) name);
5362 if (n)
5363 {
5364 /* [basic.scope.class]
5365
5366 A name N used in a class S shall refer to the same declaration
5367 in its context and when re-evaluated in the completed scope of
5368 S. */
5369 cp_error ("declaration of `%#D'", decl);
5370 cp_error_at ("changes meaning of `%s' from `%+#D'",
5371 IDENTIFIER_POINTER (DECL_NAME (decl)),
5372 (tree) n->value);
5373 }
5374}