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