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