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