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