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