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