* class.c (pushclass): Only use the mi_matrix stuff #ifdef MI_MATRIX.
[official-gcc.git] / gcc / cp / class.c
blob3cbf9ea3e689cc410d5d3454c97a7994f2fae17f
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
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.
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.
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. */
23 /* High-level class interface. */
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"
33 #include "obstack.h"
34 #define obstack_chunk_alloc xmalloc
35 #define obstack_chunk_free free
37 extern struct obstack permanent_obstack;
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))
43 extern void set_class_shadows PROTO ((tree));
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;
50 struct class_level
52 /* The previous class level. */
53 struct class_level *level_chain;
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;
63 /* Name of the current class. */
64 tree name;
65 /* Type of the current class. */
66 tree type;
68 /* Flags for this class level. */
69 int this_is_variable;
70 int memoized_lookups;
71 int save_memoized;
72 int unused;
75 /* The current_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;
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. */
86 struct base_info;
88 static tree get_vfield_name PROTO((tree));
89 static void finish_struct_anon PROTO((tree));
90 static tree build_vbase_pointer PROTO((tree, tree));
91 static int complete_type_p PROTO((tree));
92 static int typecode_p PROTO((tree, enum tree_code));
93 static tree build_vtable_entry PROTO((tree, tree));
94 static tree get_vtable_name PROTO((tree));
95 static tree get_derived_offset PROTO((tree, tree));
96 static tree get_basefndecls PROTO((tree, tree));
97 static void set_rtti_entry PROTO((tree, tree, tree));
98 static tree build_vtable PROTO((tree, tree));
99 static void prepare_fresh_vtable PROTO((tree, tree));
100 static void fixup_vtable_deltas1 PROTO((tree, tree));
101 static void fixup_vtable_deltas PROTO((tree, int, tree));
102 static void grow_method PROTO((tree, tree *));
103 static void finish_vtbls PROTO((tree, int, tree));
104 static void modify_vtable_entry PROTO((tree, tree, tree));
105 static tree get_vtable_entry_n PROTO((tree, unsigned HOST_WIDE_INT));
106 static void add_virtual_function PROTO((tree *, tree *, int *, tree, tree));
107 static tree delete_duplicate_fields_1 PROTO((tree, tree));
108 static void delete_duplicate_fields PROTO((tree));
109 static void finish_struct_bits PROTO((tree, int));
110 static int alter_access PROTO((tree, tree, tree));
111 static int overrides PROTO((tree, tree));
112 static int strictly_overrides PROTO((tree, tree));
113 static void merge_overrides PROTO((tree, tree, int, tree));
114 static void override_one_vtable PROTO((tree, tree, tree));
115 static void mark_overriders PROTO((tree, tree));
116 static void check_for_override PROTO((tree, tree));
117 static tree maybe_fixup_vptrs PROTO((tree, tree, tree));
118 static tree get_class_offset_1 PROTO((tree, tree, tree, tree, tree));
119 static tree get_class_offset PROTO((tree, tree, tree, tree));
120 static void modify_one_vtable PROTO((tree, tree, tree, tree));
121 static void modify_all_vtables PROTO((tree, tree, tree));
122 static void modify_all_direct_vtables PROTO((tree, int, tree, tree,
123 tree));
124 static void modify_all_indirect_vtables PROTO((tree, int, int, tree,
125 tree, tree));
126 static void build_class_init_list PROTO((tree));
127 static int finish_base_struct PROTO((tree, struct base_info *, tree));
129 /* Way of stacking language names. */
130 tree *current_lang_base, *current_lang_stack;
131 int current_lang_stacksize;
133 /* Names of languages we recognize. */
134 tree lang_name_c, lang_name_cplusplus;
135 tree current_lang_name;
137 /* When layout out an aggregate type, the size of the
138 basetypes (virtual and non-virtual) is passed to layout_record
139 via this node. */
140 static tree base_layout_decl;
142 /* Constants used for access control. */
143 tree access_default_node; /* 0 */
144 tree access_public_node; /* 1 */
145 tree access_protected_node; /* 2 */
146 tree access_private_node; /* 3 */
147 tree access_default_virtual_node; /* 4 */
148 tree access_public_virtual_node; /* 5 */
149 tree access_protected_virtual_node; /* 6 */
150 tree access_private_virtual_node; /* 7 */
152 /* Variables shared between class.c and call.c. */
154 #ifdef GATHER_STATISTICS
155 int n_vtables = 0;
156 int n_vtable_entries = 0;
157 int n_vtable_searches = 0;
158 int n_vtable_elems = 0;
159 int n_convert_harshness = 0;
160 int n_compute_conversion_costs = 0;
161 int n_build_method_call = 0;
162 int n_inner_fields_searched = 0;
163 #endif
165 /* Virtual baseclass things. */
167 static tree
168 build_vbase_pointer (exp, type)
169 tree exp, type;
171 char *name;
173 name = (char *) alloca (TYPE_NAME_LENGTH (type) + sizeof (VBASE_NAME) + 1);
174 sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (type));
175 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
178 /* Is the type of the EXPR, the complete type of the object?
179 If we are going to be wrong, we must be conservative, and return 0. */
181 static int
182 complete_type_p (expr)
183 tree expr;
185 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
186 while (1)
188 switch (TREE_CODE (expr))
190 case SAVE_EXPR:
191 case INDIRECT_REF:
192 case ADDR_EXPR:
193 case NOP_EXPR:
194 case CONVERT_EXPR:
195 expr = TREE_OPERAND (expr, 0);
196 continue;
198 case CALL_EXPR:
199 if (! TREE_HAS_CONSTRUCTOR (expr))
200 break;
201 /* fall through... */
202 case VAR_DECL:
203 case FIELD_DECL:
204 if (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
205 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr)))
206 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
207 return 1;
208 /* fall through... */
209 case TARGET_EXPR:
210 case PARM_DECL:
211 if (IS_AGGR_TYPE (TREE_TYPE (expr))
212 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
213 return 1;
214 /* fall through... */
215 case PLUS_EXPR:
216 default:
217 break;
219 break;
221 return 0;
224 /* Build multi-level access to EXPR using hierarchy path PATH.
225 CODE is PLUS_EXPR if we are going with the grain,
226 and MINUS_EXPR if we are not (in which case, we cannot traverse
227 virtual baseclass links).
229 TYPE is the type we want this path to have on exit.
231 ALIAS_THIS is non-zero if EXPR in an expression involving `this'. */
233 tree
234 build_vbase_path (code, type, expr, path, alias_this)
235 enum tree_code code;
236 tree type, expr, path;
237 int alias_this;
239 register int changed = 0;
240 tree last = NULL_TREE, last_virtual = NULL_TREE;
241 int nonnull = 0;
242 int fixed_type_p;
243 tree null_expr = 0, nonnull_expr;
244 tree basetype;
245 tree offset = integer_zero_node;
247 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
248 return build1 (NOP_EXPR, type, expr);
250 if (nonnull == 0 && (alias_this && flag_this_is_variable <= 0))
251 nonnull = 1;
253 #if 0
254 /* We need additional logic to convert back to the unconverted type
255 (the static type of the complete object), and then convert back
256 to the type we want. Until that is done, or until we can
257 recognize when that is, we cannot do the short cut logic. (mrs) */
258 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
259 #else
260 /* Do this, until we can undo any previous conversions. See net35.C
261 for a testcase. */
262 fixed_type_p = complete_type_p (expr);
263 #endif
265 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
266 expr = save_expr (expr);
267 nonnull_expr = expr;
269 if (BINFO_INHERITANCE_CHAIN (path))
271 tree reverse_path = NULL_TREE;
273 push_expression_obstack ();
274 while (path)
276 tree r = copy_node (path);
277 BINFO_INHERITANCE_CHAIN (r) = reverse_path;
278 reverse_path = r;
279 path = BINFO_INHERITANCE_CHAIN (path);
281 path = reverse_path;
282 pop_obstacks ();
285 basetype = BINFO_TYPE (path);
287 while (path)
289 if (TREE_VIA_VIRTUAL (path))
291 last_virtual = BINFO_TYPE (path);
292 if (code == PLUS_EXPR)
294 changed = ! fixed_type_p;
296 if (changed)
298 tree ind;
300 /* We already check for ambiguous things in the caller, just
301 find a path. */
302 if (last)
304 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
305 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
307 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
308 nonnull_expr = build_vbase_pointer (ind, last_virtual);
309 if (nonnull == 0
310 && (TREE_CODE (type) == POINTER_TYPE
311 || !flag_assume_nonnull_objects)
312 && null_expr == NULL_TREE)
314 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
315 expr = build (COND_EXPR, build_pointer_type (last_virtual),
316 build (EQ_EXPR, boolean_type_node, expr,
317 integer_zero_node),
318 null_expr, nonnull_expr);
321 /* else we'll figure out the offset below. */
323 /* Happens in the case of parse errors. */
324 if (nonnull_expr == error_mark_node)
325 return error_mark_node;
327 else
329 cp_error ("cannot cast up from virtual baseclass `%T'",
330 last_virtual);
331 return error_mark_node;
334 last = path;
335 path = BINFO_INHERITANCE_CHAIN (path);
337 /* LAST is now the last basetype assoc on the path. */
339 /* A pointer to a virtual base member of a non-null object
340 is non-null. Therefore, we only need to test for zeroness once.
341 Make EXPR the canonical expression to deal with here. */
342 if (null_expr)
344 TREE_OPERAND (expr, 2) = nonnull_expr;
345 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
346 = TREE_TYPE (nonnull_expr);
348 else
349 expr = nonnull_expr;
351 /* If we go through any virtual base pointers, make sure that
352 casts to BASETYPE from the last virtual base class use
353 the right value for BASETYPE. */
354 if (changed)
356 tree intype = TREE_TYPE (TREE_TYPE (expr));
357 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
359 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
360 offset = BINFO_OFFSET (binfo);
363 else
365 if (last_virtual)
367 offset = BINFO_OFFSET (binfo_member (last_virtual,
368 CLASSTYPE_VBASECLASSES (basetype)));
369 offset = size_binop (PLUS_EXPR, offset, BINFO_OFFSET (last));
371 else
372 offset = BINFO_OFFSET (last);
375 if (TREE_INT_CST_LOW (offset))
377 /* Bash types to make the backend happy. */
378 offset = cp_convert (type, offset);
379 #if 0
380 /* This shouldn't be necessary. (mrs) */
381 expr = build1 (NOP_EXPR, type, expr);
382 #endif
384 /* For multiple inheritance: if `this' can be set by any
385 function, then it could be 0 on entry to any function.
386 Preserve such zeroness here. Otherwise, only in the
387 case of constructors need we worry, and in those cases,
388 it will be zero, or initialized to some valid value to
389 which we may add. */
390 if (nonnull == 0)
392 if (null_expr)
393 TREE_TYPE (null_expr) = type;
394 else
395 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
396 if (TREE_SIDE_EFFECTS (expr))
397 expr = save_expr (expr);
399 return build (COND_EXPR, type,
400 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
401 null_expr,
402 build (code, type, expr, offset));
404 else return build (code, type, expr, offset);
407 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
408 be used multiple times in initialization of multiple inheritance. */
409 if (null_expr)
411 TREE_TYPE (expr) = type;
412 return expr;
414 else
415 return build1 (NOP_EXPR, type, expr);
418 /* Virtual function things. */
420 /* Build an entry in the virtual function table.
421 DELTA is the offset for the `this' pointer.
422 PFN is an ADDR_EXPR containing a pointer to the virtual function.
423 Note that the index (DELTA2) in the virtual function table
424 is always 0. */
426 static tree
427 build_vtable_entry (delta, pfn)
428 tree delta, pfn;
430 if (flag_vtable_thunks)
432 HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta);
433 if (idelta && ! DECL_ABSTRACT_VIRTUAL_P (TREE_OPERAND (pfn, 0)))
435 pfn = build1 (ADDR_EXPR, vtable_entry_type,
436 make_thunk (pfn, idelta));
437 TREE_READONLY (pfn) = 1;
438 TREE_CONSTANT (pfn) = 1;
440 #ifdef GATHER_STATISTICS
441 n_vtable_entries += 1;
442 #endif
443 return pfn;
445 else
447 extern int flag_huge_objects;
448 tree elems = expr_tree_cons (NULL_TREE, delta,
449 expr_tree_cons (NULL_TREE, integer_zero_node,
450 build_expr_list (NULL_TREE, pfn)));
451 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
453 /* DELTA is constructed by `size_int', which means it may be an
454 unsigned quantity on some platforms. Therefore, we cannot use
455 `int_fits_type_p', because when DELTA is really negative,
456 `force_fit_type' will make it look like a very large number. */
458 if ((TREE_INT_CST_LOW (TYPE_MAX_VALUE (delta_type_node))
459 < TREE_INT_CST_LOW (delta))
460 || (TREE_INT_CST_LOW (delta)
461 < TREE_INT_CST_LOW (TYPE_MIN_VALUE (delta_type_node))))
463 if (flag_huge_objects)
464 sorry ("object size exceeds built-in limit for virtual function table implementation");
465 else
466 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
469 TREE_CONSTANT (entry) = 1;
470 TREE_STATIC (entry) = 1;
471 TREE_READONLY (entry) = 1;
473 #ifdef GATHER_STATISTICS
474 n_vtable_entries += 1;
475 #endif
477 return entry;
481 /* Given an object INSTANCE, return an expression which yields the
482 virtual function vtable element corresponding to INDEX. There are
483 many special cases for INSTANCE which we take care of here, mainly
484 to avoid creating extra tree nodes when we don't have to. */
486 tree
487 build_vtbl_ref (instance, idx)
488 tree instance, idx;
490 tree vtbl, aref;
491 tree basetype = TREE_TYPE (instance);
493 if (TREE_CODE (basetype) == REFERENCE_TYPE)
494 basetype = TREE_TYPE (basetype);
496 if (instance == current_class_ref)
497 vtbl = build_indirect_ref (build_vfield_ref (instance, basetype),
498 NULL_PTR);
499 else
501 if (optimize)
503 /* Try to figure out what a reference refers to, and
504 access its virtual function table directly. */
505 tree ref = NULL_TREE;
507 if (TREE_CODE (instance) == INDIRECT_REF
508 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
509 ref = TREE_OPERAND (instance, 0);
510 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
511 ref = instance;
513 if (ref && TREE_CODE (ref) == VAR_DECL
514 && DECL_INITIAL (ref))
516 tree init = DECL_INITIAL (ref);
518 while (TREE_CODE (init) == NOP_EXPR
519 || TREE_CODE (init) == NON_LVALUE_EXPR)
520 init = TREE_OPERAND (init, 0);
521 if (TREE_CODE (init) == ADDR_EXPR)
523 init = TREE_OPERAND (init, 0);
524 if (IS_AGGR_TYPE (TREE_TYPE (init))
525 && (TREE_CODE (init) == PARM_DECL
526 || TREE_CODE (init) == VAR_DECL))
527 instance = init;
532 if (IS_AGGR_TYPE (TREE_TYPE (instance))
533 && (TREE_CODE (instance) == RESULT_DECL
534 || TREE_CODE (instance) == PARM_DECL
535 || TREE_CODE (instance) == VAR_DECL))
536 vtbl = TYPE_BINFO_VTABLE (basetype);
537 else
538 vtbl = build_indirect_ref (build_vfield_ref (instance, basetype),
539 NULL_PTR);
541 assemble_external (vtbl);
542 aref = build_array_ref (vtbl, idx);
544 return aref;
547 /* Given an object INSTANCE, return an expression which yields the
548 virtual function corresponding to INDEX. There are many special
549 cases for INSTANCE which we take care of here, mainly to avoid
550 creating extra tree nodes when we don't have to. */
552 tree
553 build_vfn_ref (ptr_to_instptr, instance, idx)
554 tree *ptr_to_instptr, instance;
555 tree idx;
557 tree aref = build_vtbl_ref (instance, idx);
559 /* When using thunks, there is no extra delta, and we get the pfn
560 directly. */
561 if (flag_vtable_thunks)
562 return aref;
564 if (ptr_to_instptr)
566 /* Save the intermediate result in a SAVE_EXPR so we don't have to
567 compute each component of the virtual function pointer twice. */
568 if (TREE_CODE (aref) == INDIRECT_REF)
569 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
571 *ptr_to_instptr
572 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
573 *ptr_to_instptr,
574 cp_convert (ptrdiff_type_node,
575 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
578 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
581 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
582 for the given TYPE. */
584 static tree
585 get_vtable_name (type)
586 tree type;
588 tree type_id = build_typename_overload (type);
589 char *buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
590 + IDENTIFIER_LENGTH (type_id) + 2);
591 char *ptr = IDENTIFIER_POINTER (type_id);
592 int i;
593 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
594 #if 0
595 /* We don't take off the numbers; prepare_fresh_vtable uses the
596 DECL_ASSEMBLER_NAME for the type, which includes the number
597 in `3foo'. If we were to pull them off here, we'd end up with
598 something like `_vt.foo.3bar', instead of a uniform definition. */
599 while (ptr[i] >= '0' && ptr[i] <= '9')
600 i += 1;
601 #endif
602 sprintf (buf, VTABLE_NAME_FORMAT, ptr+i);
603 return get_identifier (buf);
606 /* Return the offset to the main vtable for a given base BINFO. */
608 tree
609 get_vfield_offset (binfo)
610 tree binfo;
612 tree tmp
613 = size_binop (FLOOR_DIV_EXPR,
614 DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))),
615 size_int (BITS_PER_UNIT));
616 tmp = convert (sizetype, tmp);
617 return size_binop (PLUS_EXPR, tmp, BINFO_OFFSET (binfo));
620 /* Get the offset to the start of the original binfo that we derived
621 this binfo from. If we find TYPE first, return the offset only
622 that far. The shortened search is useful because the this pointer
623 on method calling is expected to point to a DECL_CONTEXT (fndecl)
624 object, and not a baseclass of it. */
626 static tree
627 get_derived_offset (binfo, type)
628 tree binfo, type;
630 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
631 tree offset2;
632 int i;
633 while (BINFO_BASETYPES (binfo)
634 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
636 tree binfos = BINFO_BASETYPES (binfo);
637 if (BINFO_TYPE (binfo) == type)
638 break;
639 binfo = TREE_VEC_ELT (binfos, i);
641 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
642 return size_binop (MINUS_EXPR, offset1, offset2);
645 /* Update the rtti info for this class. */
647 static void
648 set_rtti_entry (virtuals, offset, type)
649 tree virtuals, offset, type;
651 tree vfn;
653 if (flag_rtti)
654 vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, get_tinfo_fn (type));
655 else
656 vfn = build1 (NOP_EXPR, vfunc_ptr_type_node, size_zero_node);
657 TREE_CONSTANT (vfn) = 1;
659 if (! flag_vtable_thunks)
660 TREE_VALUE (virtuals) = build_vtable_entry (offset, vfn);
661 else
663 tree voff = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
664 TREE_CONSTANT (voff) = 1;
666 TREE_VALUE (virtuals) = build_vtable_entry (size_zero_node, voff);
668 /* The second slot is for the tdesc pointer when thunks are used. */
669 TREE_VALUE (TREE_CHAIN (virtuals))
670 = build_vtable_entry (size_zero_node, vfn);
674 /* Build a virtual function for type TYPE.
675 If BINFO is non-NULL, build the vtable starting with the initial
676 approximation that it is the same as the one which is the head of
677 the association list. */
679 static tree
680 build_vtable (binfo, type)
681 tree binfo, type;
683 tree name = get_vtable_name (type);
684 tree virtuals, decl;
686 if (binfo)
688 tree offset;
690 virtuals = copy_list (BINFO_VIRTUALS (binfo));
691 decl = build_decl (VAR_DECL, name, TREE_TYPE (BINFO_VTABLE (binfo)));
693 /* Now do rtti stuff. */
694 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
695 offset = size_binop (MINUS_EXPR, size_zero_node, offset);
696 set_rtti_entry (virtuals, offset, type);
698 else
700 virtuals = NULL_TREE;
701 decl = build_decl (VAR_DECL, name, void_type_node);
704 #ifdef GATHER_STATISTICS
705 n_vtables += 1;
706 n_vtable_elems += list_length (virtuals);
707 #endif
709 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
710 import_export_vtable (decl, type, 0);
712 IDENTIFIER_GLOBAL_VALUE (name) = decl = pushdecl_top_level (decl);
713 /* Initialize the association list for this type, based
714 on our first approximation. */
715 TYPE_BINFO_VTABLE (type) = decl;
716 TYPE_BINFO_VIRTUALS (type) = virtuals;
718 DECL_ARTIFICIAL (decl) = 1;
719 TREE_STATIC (decl) = 1;
720 #ifndef WRITABLE_VTABLES
721 /* Make them READONLY by default. (mrs) */
722 TREE_READONLY (decl) = 1;
723 #endif
724 /* At one time the vtable info was grabbed 2 words at a time. This
725 fails on sparc unless you have 8-byte alignment. (tiemann) */
726 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
727 DECL_ALIGN (decl));
729 /* Why is this conditional? (mrs) */
730 if (binfo && write_virtuals >= 0)
731 DECL_VIRTUAL_P (decl) = 1;
732 DECL_CONTEXT (decl) = type;
734 binfo = TYPE_BINFO (type);
735 SET_BINFO_NEW_VTABLE_MARKED (binfo);
736 return decl;
739 extern tree signed_size_zero_node;
741 /* Give TYPE a new virtual function table which is initialized
742 with a skeleton-copy of its original initialization. The only
743 entry that changes is the `delta' entry, so we can really
744 share a lot of structure.
746 FOR_TYPE is the derived type which caused this table to
747 be needed.
749 BINFO is the type association which provided TYPE for FOR_TYPE.
751 The order in which vtables are built (by calling this function) for
752 an object must remain the same, otherwise a binary incompatibility
753 can result. */
755 static void
756 prepare_fresh_vtable (binfo, for_type)
757 tree binfo, for_type;
759 tree basetype;
760 tree orig_decl = BINFO_VTABLE (binfo);
761 tree name;
762 tree new_decl;
763 tree offset;
764 tree path = binfo;
765 char *buf, *buf2;
766 char joiner = '_';
767 int i;
769 #ifdef JOINER
770 joiner = JOINER;
771 #endif
773 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
775 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
776 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
778 /* We know that the vtable that we are going to create doesn't exist
779 yet in the global namespace, and when we finish, it will be
780 pushed into the global namespace. In complex MI hierarchies, we
781 have to loop while the name we are thinking of adding is globally
782 defined, adding more name components to the vtable name as we
783 loop, until the name is unique. This is because in complex MI
784 cases, we might have the same base more than once. This means
785 that the order in which this function is called for vtables must
786 remain the same, otherwise binary compatibility can be
787 compromised. */
789 while (1)
791 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
792 + 1 + i);
793 char *new_buf2;
795 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
796 buf2);
797 buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT) + strlen (buf1) + 1);
798 sprintf (buf, VTABLE_NAME_FORMAT, buf1);
799 name = get_identifier (buf);
801 /* If this name doesn't clash, then we can use it, otherwise
802 we add more to the name until it is unique. */
804 if (! IDENTIFIER_GLOBAL_VALUE (name))
805 break;
807 /* Set values for next loop through, if the name isn't unique. */
809 path = BINFO_INHERITANCE_CHAIN (path);
811 /* We better not run out of stuff to make it unique. */
812 my_friendly_assert (path != NULL_TREE, 368);
814 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
816 if (for_type == basetype)
818 /* If we run out of basetypes in the path, we have already
819 found created a vtable with that name before, we now
820 resort to tacking on _%d to distinguish them. */
821 int j = 2;
822 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
823 buf1 = (char *) alloca (i);
824 do {
825 sprintf (buf1, "%s%c%s%c%d",
826 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
827 buf2, joiner, j);
828 buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
829 + strlen (buf1) + 1);
830 sprintf (buf, VTABLE_NAME_FORMAT, buf1);
831 name = get_identifier (buf);
833 /* If this name doesn't clash, then we can use it,
834 otherwise we add something different to the name until
835 it is unique. */
836 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
838 /* Hey, they really like MI don't they? Increase the 3
839 above to 6, and the 999 to 999999. :-) */
840 my_friendly_assert (j <= 999, 369);
842 break;
845 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
846 new_buf2 = (char *) alloca (i);
847 sprintf (new_buf2, "%s%c%s",
848 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
849 buf2 = new_buf2;
852 new_decl = build_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
853 /* Remember which class this vtable is really for. */
854 DECL_CONTEXT (new_decl) = for_type;
856 DECL_ARTIFICIAL (new_decl) = 1;
857 TREE_STATIC (new_decl) = 1;
858 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
859 DECL_VIRTUAL_P (new_decl) = 1;
860 #ifndef WRITABLE_VTABLES
861 /* Make them READONLY by default. (mrs) */
862 TREE_READONLY (new_decl) = 1;
863 #endif
864 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
866 /* Make fresh virtual list, so we can smash it later. */
867 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
869 if (TREE_VIA_VIRTUAL (binfo))
871 tree binfo1 = binfo_member (BINFO_TYPE (binfo),
872 CLASSTYPE_VBASECLASSES (for_type));
874 /* XXX - This should never happen, if it does, the caller should
875 ensure that the binfo is from for_type's binfos, not from any
876 base type's. We can remove all this code after a while. */
877 if (binfo1 != binfo)
878 warning ("internal inconsistency: binfo offset error for rtti");
880 offset = BINFO_OFFSET (binfo1);
882 else
883 offset = BINFO_OFFSET (binfo);
885 set_rtti_entry (BINFO_VIRTUALS (binfo),
886 size_binop (MINUS_EXPR, signed_size_zero_node, offset),
887 for_type);
889 #ifdef GATHER_STATISTICS
890 n_vtables += 1;
891 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
892 #endif
894 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
895 import_export_vtable (new_decl, for_type, 0);
897 if (TREE_VIA_VIRTUAL (binfo))
898 my_friendly_assert (binfo == binfo_member (BINFO_TYPE (binfo),
899 CLASSTYPE_VBASECLASSES (current_class_type)),
900 170);
901 SET_BINFO_NEW_VTABLE_MARKED (binfo);
904 #if 0
905 /* Access the virtual function table entry that logically
906 contains BASE_FNDECL. VIRTUALS is the virtual function table's
907 initializer. We can run off the end, when dealing with virtual
908 destructors in MI situations, return NULL_TREE in that case. */
910 static tree
911 get_vtable_entry (virtuals, base_fndecl)
912 tree virtuals, base_fndecl;
914 unsigned HOST_WIDE_INT n = (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
915 ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
916 & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1))
917 : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
919 #ifdef GATHER_STATISTICS
920 n_vtable_searches += n;
921 #endif
923 while (n > 0 && virtuals)
925 --n;
926 virtuals = TREE_CHAIN (virtuals);
928 return virtuals;
930 #endif
932 /* Put new entry ENTRY into virtual function table initializer
933 VIRTUALS.
935 Also update DECL_VINDEX (FNDECL). */
937 static void
938 modify_vtable_entry (old_entry_in_list, new_entry, fndecl)
939 tree old_entry_in_list, new_entry, fndecl;
941 tree base_fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list)), 0);
943 #ifdef NOTQUITE
944 cp_warning ("replaced %D with %D", DECL_ASSEMBLER_NAME (base_fndecl),
945 DECL_ASSEMBLER_NAME (fndecl));
946 #endif
947 TREE_VALUE (old_entry_in_list) = new_entry;
949 /* Now assign virtual dispatch information, if unset. */
950 /* We can dispatch this, through any overridden base function. */
951 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
953 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
954 DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
958 /* Access the virtual function table entry N. VIRTUALS is the virtual
959 function table's initializer. */
961 static tree
962 get_vtable_entry_n (virtuals, n)
963 tree virtuals;
964 unsigned HOST_WIDE_INT n;
966 while (n > 0)
968 --n;
969 virtuals = TREE_CHAIN (virtuals);
971 return virtuals;
974 /* Add a virtual function to all the appropriate vtables for the class
975 T. DECL_VINDEX(X) should be error_mark_node, if we want to
976 allocate a new slot in our table. If it is error_mark_node, we
977 know that no other function from another vtable is overridden by X.
978 HAS_VIRTUAL keeps track of how many virtuals there are in our main
979 vtable for the type, and we build upon the PENDING_VIRTUALS list
980 and return it. */
982 static void
983 add_virtual_function (pv, phv, has_virtual, fndecl, t)
984 tree *pv, *phv;
985 int *has_virtual;
986 tree fndecl;
987 tree t; /* Structure type. */
989 tree pending_virtuals = *pv;
990 tree pending_hard_virtuals = *phv;
992 /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
993 convert to void *. Make such a conversion here. */
994 tree vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
995 TREE_CONSTANT (vfn) = 1;
997 #ifndef DUMB_USER
998 if (current_class_type == 0)
999 cp_warning ("internal problem, current_class_type is zero when adding `%D', please report",
1000 fndecl);
1001 if (current_class_type && t != current_class_type)
1002 cp_warning ("internal problem, current_class_type differs when adding `%D', please report",
1003 fndecl);
1004 #endif
1006 /* If the virtual function is a redefinition of a prior one,
1007 figure out in which base class the new definition goes,
1008 and if necessary, make a fresh virtual function table
1009 to hold that entry. */
1010 if (DECL_VINDEX (fndecl) == error_mark_node)
1012 tree entry;
1014 /* We remember that this was the base sub-object for rtti. */
1015 CLASSTYPE_RTTI (t) = t;
1017 /* If we are using thunks, use two slots at the front, one
1018 for the offset pointer, one for the tdesc pointer. */
1019 if (*has_virtual == 0 && flag_vtable_thunks)
1021 *has_virtual = 1;
1024 /* Build a new INT_CST for this DECL_VINDEX. */
1026 static tree index_table[256];
1027 tree idx;
1028 /* We skip a slot for the offset/tdesc entry. */
1029 int i = ++(*has_virtual);
1031 if (i >= 256 || index_table[i] == 0)
1033 idx = build_int_2 (i, 0);
1034 if (i < 256)
1035 index_table[i] = idx;
1037 else
1038 idx = index_table[i];
1040 /* Now assign virtual dispatch information. */
1041 DECL_VINDEX (fndecl) = idx;
1042 DECL_CONTEXT (fndecl) = t;
1044 entry = build_vtable_entry (integer_zero_node, vfn);
1045 pending_virtuals = tree_cons (DECL_VINDEX (fndecl), entry, pending_virtuals);
1047 /* Might already be INTEGER_CST if declared twice in class. We will
1048 give error later or we've already given it. */
1049 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1051 /* Need an entry in some other virtual function table.
1052 Deal with this after we have laid out our virtual base classes. */
1053 pending_hard_virtuals = temp_tree_cons (fndecl, vfn, pending_hard_virtuals);
1055 *pv = pending_virtuals;
1056 *phv = pending_hard_virtuals;
1059 /* Obstack on which to build the vector of class methods. */
1060 struct obstack class_obstack;
1061 extern struct obstack *current_obstack;
1063 /* Add method METHOD to class TYPE. This is used when a method
1064 has been defined which did not initially appear in the class definition,
1065 and helps cut down on spurious error messages.
1067 FIELDS is the entry in the METHOD_VEC vector entry of the class type where
1068 the method should be added. */
1070 void
1071 add_method (type, fields, method)
1072 tree type, *fields, method;
1074 /* We must make a copy of METHOD here, since we must be sure that
1075 we have exclusive title to this method's DECL_CHAIN. */
1076 tree decl;
1078 push_obstacks (&permanent_obstack, &permanent_obstack);
1080 decl = copy_node (method);
1081 if (DECL_RTL (decl) == 0
1082 && (!processing_template_decl
1083 || !uses_template_parms (decl)))
1085 make_function_rtl (decl);
1086 DECL_RTL (method) = DECL_RTL (decl);
1090 if (fields && *fields)
1092 /* Take care not to hide destructor. */
1093 DECL_CHAIN (decl) = DECL_CHAIN (*fields);
1094 DECL_CHAIN (*fields) = decl;
1096 else if (CLASSTYPE_METHOD_VEC (type) == 0)
1098 tree method_vec = make_node (TREE_VEC);
1099 if (TYPE_IDENTIFIER (type) == DECL_NAME (decl))
1101 /* ??? Is it possible for there to have been enough room in the
1102 current chunk for the tree_vec structure but not a tree_vec
1103 plus a tree*? Will this work in that case? */
1104 obstack_free (current_obstack, method_vec);
1105 obstack_blank (current_obstack, sizeof (struct tree_vec) + sizeof (tree *));
1106 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)))
1107 TREE_VEC_ELT (method_vec, 1) = decl;
1108 else
1109 TREE_VEC_ELT (method_vec, 0) = decl;
1110 TREE_VEC_LENGTH (method_vec) = 2;
1112 else
1114 /* ??? Is it possible for there to have been enough room in the
1115 current chunk for the tree_vec structure but not a tree_vec
1116 plus a tree*? Will this work in that case? */
1117 obstack_free (current_obstack, method_vec);
1118 obstack_blank (current_obstack, sizeof (struct tree_vec) + 2*sizeof (tree *));
1119 TREE_VEC_ELT (method_vec, 2) = decl;
1120 TREE_VEC_LENGTH (method_vec) = 3;
1121 obstack_finish (current_obstack);
1123 CLASSTYPE_METHOD_VEC (type) = method_vec;
1125 else
1127 tree method_vec = CLASSTYPE_METHOD_VEC (type);
1128 int len = TREE_VEC_LENGTH (method_vec);
1130 /* Adding a new ctor or dtor. This is easy because our
1131 METHOD_VEC always has a slot for such entries. */
1132 if (TYPE_IDENTIFIER (type) == DECL_NAME (decl))
1134 int idx = !!DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl));
1135 /* TREE_VEC_ELT (method_vec, idx) = decl; */
1136 if (decl != TREE_VEC_ELT (method_vec, idx))
1138 DECL_CHAIN (decl) = TREE_VEC_ELT (method_vec, idx);
1139 TREE_VEC_ELT (method_vec, idx) = decl;
1142 else
1144 /* This is trickier. We try to extend the TREE_VEC in-place,
1145 but if that does not work, we copy all its data to a new
1146 TREE_VEC that's large enough. */
1147 struct obstack *ob = &class_obstack;
1148 tree *end = (tree *)obstack_next_free (ob);
1150 if (end != TREE_VEC_END (method_vec))
1152 ob = current_obstack;
1153 TREE_VEC_LENGTH (method_vec) += 1;
1154 TREE_VEC_ELT (method_vec, len) = NULL_TREE;
1155 method_vec = copy_node (method_vec);
1156 TREE_VEC_LENGTH (method_vec) -= 1;
1158 else
1160 tree tmp_vec = (tree) obstack_base (ob);
1161 if (obstack_room (ob) < sizeof (tree))
1163 obstack_blank (ob, sizeof (struct tree_common)
1164 + tree_code_length[(int) TREE_VEC]
1165 * sizeof (char *)
1166 + len * sizeof (tree));
1167 tmp_vec = (tree) obstack_base (ob);
1168 bcopy ((char *) method_vec, (char *) tmp_vec,
1169 (sizeof (struct tree_common)
1170 + tree_code_length[(int) TREE_VEC] * sizeof (char *)
1171 + (len-1) * sizeof (tree)));
1172 method_vec = tmp_vec;
1174 else
1175 obstack_blank (ob, sizeof (tree));
1178 obstack_finish (ob);
1179 TREE_VEC_ELT (method_vec, len) = decl;
1180 TREE_VEC_LENGTH (method_vec) = len + 1;
1181 CLASSTYPE_METHOD_VEC (type) = method_vec;
1183 if (TYPE_BINFO_BASETYPES (type) && CLASSTYPE_BASELINK_VEC (type))
1185 /* ??? May be better to know whether these can be extended? */
1186 tree baselink_vec = CLASSTYPE_BASELINK_VEC (type);
1188 TREE_VEC_LENGTH (baselink_vec) += 1;
1189 CLASSTYPE_BASELINK_VEC (type) = copy_node (baselink_vec);
1190 TREE_VEC_LENGTH (baselink_vec) -= 1;
1192 TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), len) = 0;
1196 DECL_CONTEXT (decl) = type;
1197 DECL_CLASS_CONTEXT (decl) = type;
1199 pop_obstacks ();
1202 /* Subroutines of finish_struct. */
1204 /* Look through the list of fields for this struct, deleting
1205 duplicates as we go. This must be recursive to handle
1206 anonymous unions.
1208 FIELD is the field which may not appear anywhere in FIELDS.
1209 FIELD_PTR, if non-null, is the starting point at which
1210 chained deletions may take place.
1211 The value returned is the first acceptable entry found
1212 in FIELDS.
1214 Note that anonymous fields which are not of UNION_TYPE are
1215 not duplicates, they are just anonymous fields. This happens
1216 when we have unnamed bitfields, for example. */
1218 static tree
1219 delete_duplicate_fields_1 (field, fields)
1220 tree field, fields;
1222 tree x;
1223 tree prev = 0;
1224 if (DECL_NAME (field) == 0)
1226 if (TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)
1227 return fields;
1229 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1230 fields = delete_duplicate_fields_1 (x, fields);
1231 return fields;
1233 else
1235 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1237 if (DECL_NAME (x) == 0)
1239 if (TREE_CODE (TREE_TYPE (x)) != UNION_TYPE)
1240 continue;
1241 TYPE_FIELDS (TREE_TYPE (x))
1242 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1243 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1245 if (prev == 0)
1246 fields = TREE_CHAIN (fields);
1247 else
1248 TREE_CHAIN (prev) = TREE_CHAIN (x);
1251 else
1253 if (DECL_NAME (field) == DECL_NAME (x))
1255 if (TREE_CODE (field) == CONST_DECL
1256 && TREE_CODE (x) == CONST_DECL)
1257 cp_error_at ("duplicate enum value `%D'", x);
1258 else if (TREE_CODE (field) == CONST_DECL
1259 || TREE_CODE (x) == CONST_DECL)
1260 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1262 else if (TREE_CODE (field) == TYPE_DECL
1263 && TREE_CODE (x) == TYPE_DECL)
1265 if (TREE_TYPE (field) == TREE_TYPE (x))
1266 continue;
1267 cp_error_at ("duplicate nested type `%D'", x);
1269 else if (TREE_CODE (field) == TYPE_DECL
1270 || TREE_CODE (x) == TYPE_DECL)
1272 /* Hide tag decls. */
1273 if ((TREE_CODE (field) == TYPE_DECL
1274 && DECL_ARTIFICIAL (field))
1275 || (TREE_CODE (x) == TYPE_DECL
1276 && DECL_ARTIFICIAL (x)))
1277 continue;
1278 cp_error_at ("duplicate field `%D' (as type and non-type)",
1281 else
1282 cp_error_at ("duplicate member `%D'", x);
1283 if (prev == 0)
1284 fields = TREE_CHAIN (fields);
1285 else
1286 TREE_CHAIN (prev) = TREE_CHAIN (x);
1291 return fields;
1294 static void
1295 delete_duplicate_fields (fields)
1296 tree fields;
1298 tree x;
1299 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1300 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1303 /* Change the access of FDECL to ACCESS in T.
1304 Return 1 if change was legit, otherwise return 0. */
1306 static int
1307 alter_access (t, fdecl, access)
1308 tree t;
1309 tree fdecl;
1310 tree access;
1312 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1313 if (elem && TREE_VALUE (elem) != access)
1315 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1317 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1319 else
1320 error ("conflicting access specifications for field `%s', ignored",
1321 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1323 else if (TREE_PRIVATE (fdecl))
1325 if (access != access_private_node)
1326 cp_error_at ("cannot make private `%D' non-private", fdecl);
1327 goto alter;
1329 else if (TREE_PROTECTED (fdecl))
1331 if (access != access_protected_node)
1332 cp_error_at ("cannot make protected `%D' non-protected", fdecl);
1333 goto alter;
1335 /* ARM 11.3: an access declaration may not be used to restrict access
1336 to a member that is accessible in the base class. */
1337 else if (access != access_public_node)
1338 cp_error_at ("cannot reduce access of public member `%D'", fdecl);
1339 else if (elem == NULL_TREE)
1341 alter:
1342 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1343 return 1;
1345 return 0;
1348 /* If FOR_TYPE needs to reinitialize virtual function table pointers
1349 for TYPE's sub-objects, add such reinitializations to BASE_INIT_LIST.
1350 Returns BASE_INIT_LIST appropriately modified. */
1352 static tree
1353 maybe_fixup_vptrs (for_type, binfo, base_init_list)
1354 tree for_type, binfo, base_init_list;
1356 /* Now reinitialize any slots that don't fall under our virtual
1357 function table pointer. */
1358 tree vfields = CLASSTYPE_VFIELDS (BINFO_TYPE (binfo));
1359 while (vfields)
1361 tree basetype = VF_NORMAL_VALUE (vfields)
1362 ? TYPE_MAIN_VARIANT (VF_NORMAL_VALUE (vfields))
1363 : VF_BASETYPE_VALUE (vfields);
1365 tree base_binfo = get_binfo (basetype, for_type, 0);
1366 /* Punt until this is implemented. */
1367 if (1 /* BINFO_MODIFIED (base_binfo) */)
1369 tree base_offset = get_vfield_offset (base_binfo);
1370 if (! tree_int_cst_equal (base_offset, get_vfield_offset (TYPE_BINFO (for_type)))
1371 && ! tree_int_cst_equal (base_offset, get_vfield_offset (binfo)))
1372 base_init_list = tree_cons (error_mark_node, base_binfo,
1373 base_init_list);
1375 vfields = TREE_CHAIN (vfields);
1377 return base_init_list;
1380 /* If TYPE does not have a constructor, then the compiler must
1381 manually deal with all of the initialization this type requires.
1383 If a base initializer exists only to fill in the virtual function
1384 table pointer, then we mark that fact with the TREE_VIRTUAL bit.
1385 This way, we avoid multiple initializations of the same field by
1386 each virtual function table up the class hierarchy.
1388 Virtual base class pointers are not initialized here. They are
1389 initialized only at the "top level" of object creation. If we
1390 initialized them here, we would have to skip a lot of work. */
1392 static void
1393 build_class_init_list (type)
1394 tree type;
1396 tree base_init_list = NULL_TREE;
1397 tree member_init_list = NULL_TREE;
1399 /* Since we build member_init_list and base_init_list using
1400 tree_cons, backwards fields the all through work. */
1401 tree x;
1402 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
1403 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1405 for (x = TYPE_FIELDS (type); x; x = TREE_CHAIN (x))
1407 if (TREE_CODE (x) != FIELD_DECL)
1408 continue;
1410 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (x))
1411 || DECL_INITIAL (x) != NULL_TREE)
1412 member_init_list = tree_cons (x, type, member_init_list);
1414 member_init_list = nreverse (member_init_list);
1416 /* We will end up doing this last. Need special marker
1417 to avoid infinite regress. */
1418 if (TYPE_VIRTUAL_P (type))
1420 base_init_list = build_tree_list (error_mark_node, TYPE_BINFO (type));
1421 if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (type) == 0)
1422 TREE_VALUE (base_init_list) = NULL_TREE;
1423 TREE_ADDRESSABLE (base_init_list) = 1;
1426 /* Each base class which needs to have initialization
1427 of some kind gets to make such requests known here. */
1428 for (i = n_baseclasses-1; i >= 0; i--)
1430 tree base_binfo = TREE_VEC_ELT (binfos, i);
1431 tree blist;
1433 /* Don't initialize virtual baseclasses this way. */
1434 if (TREE_VIA_VIRTUAL (base_binfo))
1435 continue;
1437 if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (base_binfo)))
1439 /* ...and the last shall come first... */
1440 base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
1441 base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
1442 continue;
1445 if ((blist = CLASSTYPE_BASE_INIT_LIST (BINFO_TYPE (base_binfo))) == NULL_TREE)
1446 /* Nothing to initialize. */
1447 continue;
1449 /* ...ditto... */
1450 base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
1452 /* This is normally true for single inheritance.
1453 The win is we can shrink the chain of initializations
1454 to be done by only converting to the actual type
1455 we are interested in. */
1456 if (TREE_VALUE (blist)
1457 && TREE_CODE (TREE_VALUE (blist)) == TREE_VEC
1458 && tree_int_cst_equal (BINFO_OFFSET (base_binfo),
1459 BINFO_OFFSET (TREE_VALUE (blist))))
1461 if (base_init_list)
1463 /* Does it do more than just fill in a
1464 virtual function table pointer? */
1465 if (! TREE_ADDRESSABLE (blist))
1466 base_init_list = build_tree_list (blist, base_init_list);
1467 /* Can we get by just with the virtual function table
1468 pointer that it fills in? */
1469 else if (TREE_ADDRESSABLE (base_init_list)
1470 && TREE_VALUE (base_init_list) == 0)
1471 base_init_list = blist;
1472 /* Maybe, but it is not obvious as the previous case. */
1473 else if (! CLASSTYPE_NEEDS_VIRTUAL_REINIT (type))
1475 tree last = tree_last (base_init_list);
1476 while (TREE_VALUE (last)
1477 && TREE_CODE (TREE_VALUE (last)) == TREE_LIST)
1478 last = tree_last (TREE_VALUE (last));
1479 if (TREE_VALUE (last) == 0)
1480 base_init_list = build_tree_list (blist, base_init_list);
1483 else
1484 base_init_list = blist;
1486 else
1488 /* The function expand_aggr_init knows how to do the
1489 initialization of `basetype' without getting
1490 an explicit `blist'. */
1491 if (base_init_list)
1492 base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
1493 else
1494 base_init_list = CLASSTYPE_BINFO_AS_LIST (BINFO_TYPE (base_binfo));
1498 if (base_init_list)
1500 if (member_init_list)
1501 CLASSTYPE_BASE_INIT_LIST (type) =
1502 build_tree_list (base_init_list, member_init_list);
1503 else
1504 CLASSTYPE_BASE_INIT_LIST (type) = base_init_list;
1506 else if (member_init_list)
1507 CLASSTYPE_BASE_INIT_LIST (type) = member_init_list;
1510 struct base_info
1512 int has_virtual;
1513 int max_has_virtual;
1514 int n_ancestors;
1515 tree vfield;
1516 tree vfields;
1517 tree rtti;
1518 char cant_have_default_ctor;
1519 char cant_have_const_ctor;
1520 char no_const_asn_ref;
1523 /* Record information about type T derived from its base classes.
1524 Store most of that information in T itself, and place the
1525 remaining information in the struct BASE_INFO.
1527 Propagate basetype offsets throughout the lattice. Note that the
1528 lattice topped by T is really a pair: it's a DAG that gives the
1529 structure of the derivation hierarchy, and it's a list of the
1530 virtual baseclasses that appear anywhere in the DAG. When a vbase
1531 type appears in the DAG, it's offset is 0, and it's children start
1532 their offsets from that point. When a vbase type appears in the list,
1533 its offset is the offset it has in the hierarchy, and its children's
1534 offsets include that offset in theirs.
1536 Returns the index of the first base class to have virtual functions,
1537 or -1 if no such base class.
1539 Note that at this point TYPE_BINFO (t) != t_binfo. */
1541 static int
1542 finish_base_struct (t, b, t_binfo)
1543 tree t;
1544 struct base_info *b;
1545 tree t_binfo;
1547 tree binfos = BINFO_BASETYPES (t_binfo);
1548 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1549 int first_vfn_base_index = -1;
1550 bzero ((char *) b, sizeof (struct base_info));
1552 for (i = 0; i < n_baseclasses; i++)
1554 tree base_binfo = TREE_VEC_ELT (binfos, i);
1555 tree basetype = BINFO_TYPE (base_binfo);
1557 /* If the type of basetype is incomplete, then
1558 we already complained about that fact
1559 (and we should have fixed it up as well). */
1560 if (TYPE_SIZE (basetype) == 0)
1562 int j;
1563 /* The base type is of incomplete type. It is
1564 probably best to pretend that it does not
1565 exist. */
1566 if (i == n_baseclasses-1)
1567 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1568 TREE_VEC_LENGTH (binfos) -= 1;
1569 n_baseclasses -= 1;
1570 for (j = i; j+1 < n_baseclasses; j++)
1571 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1574 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1575 b->cant_have_const_ctor = 1;
1577 if (TYPE_HAS_CONSTRUCTOR (basetype)
1578 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1580 b->cant_have_default_ctor = 1;
1581 if (! TYPE_HAS_CONSTRUCTOR (t))
1583 cp_pedwarn ("base `%T' with only non-default constructor",
1584 basetype);
1585 cp_pedwarn ("in class without a constructor");
1589 if (TYPE_HAS_ASSIGN_REF (basetype)
1590 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1591 b->no_const_asn_ref = 1;
1593 b->n_ancestors += CLASSTYPE_N_SUPERCLASSES (basetype);
1594 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1595 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1596 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1597 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1599 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1600 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1601 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1603 if (! TREE_VIA_VIRTUAL (base_binfo)
1604 && BINFO_BASETYPES (base_binfo))
1606 tree base_binfos = BINFO_BASETYPES (base_binfo);
1607 tree chain = NULL_TREE;
1608 int j;
1610 /* Now unshare the structure beneath BASE_BINFO. */
1611 for (j = TREE_VEC_LENGTH (base_binfos)-1;
1612 j >= 0; j--)
1614 tree base_base_binfo = TREE_VEC_ELT (base_binfos, j);
1615 if (! TREE_VIA_VIRTUAL (base_base_binfo))
1616 TREE_VEC_ELT (base_binfos, j)
1617 = make_binfo (BINFO_OFFSET (base_base_binfo),
1618 base_base_binfo,
1619 BINFO_VTABLE (base_base_binfo),
1620 BINFO_VIRTUALS (base_base_binfo),
1621 chain);
1622 chain = TREE_VEC_ELT (base_binfos, j);
1623 TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo);
1624 TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo);
1625 BINFO_INHERITANCE_CHAIN (chain) = base_binfo;
1628 /* Completely unshare potentially shared data, and
1629 update what is ours. */
1630 propagate_binfo_offsets (base_binfo, BINFO_OFFSET (base_binfo));
1633 if (! TREE_VIA_VIRTUAL (base_binfo))
1634 CLASSTYPE_N_SUPERCLASSES (t) += 1;
1636 if (TYPE_VIRTUAL_P (basetype))
1638 /* Ensure that this is set from at least a virtual base
1639 class. */
1640 if (b->rtti == NULL_TREE)
1641 b->rtti = CLASSTYPE_RTTI (basetype);
1643 /* Don't borrow virtuals from virtual baseclasses. */
1644 if (TREE_VIA_VIRTUAL (base_binfo))
1645 continue;
1647 if (first_vfn_base_index < 0)
1649 tree vfields;
1650 first_vfn_base_index = i;
1652 /* Update these two, now that we know what vtable we are
1653 going to extend. This is so that we can add virtual
1654 functions, and override them properly. */
1655 BINFO_VTABLE (t_binfo) = TYPE_BINFO_VTABLE (basetype);
1656 BINFO_VIRTUALS (t_binfo) = TYPE_BINFO_VIRTUALS (basetype);
1657 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1658 b->vfield = CLASSTYPE_VFIELD (basetype);
1659 b->vfields = copy_list (CLASSTYPE_VFIELDS (basetype));
1660 vfields = b->vfields;
1661 while (vfields)
1663 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1664 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1666 tree value = VF_BASETYPE_VALUE (vfields);
1667 if (DECL_NAME (CLASSTYPE_VFIELD (value))
1668 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1669 VF_NORMAL_VALUE (b->vfields) = basetype;
1670 else
1671 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1673 vfields = TREE_CHAIN (vfields);
1675 CLASSTYPE_VFIELD (t) = b->vfield;
1677 else
1679 /* Only add unique vfields, and flatten them out as we go. */
1680 tree vfields = CLASSTYPE_VFIELDS (basetype);
1681 while (vfields)
1683 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1684 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1686 tree value = VF_BASETYPE_VALUE (vfields);
1687 b->vfields = tree_cons (base_binfo, value, b->vfields);
1688 if (DECL_NAME (CLASSTYPE_VFIELD (value))
1689 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1690 VF_NORMAL_VALUE (b->vfields) = basetype;
1691 else
1692 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1694 vfields = TREE_CHAIN (vfields);
1697 if (b->has_virtual == 0)
1699 first_vfn_base_index = i;
1701 /* Update these two, now that we know what vtable we are
1702 going to extend. This is so that we can add virtual
1703 functions, and override them properly. */
1704 BINFO_VTABLE (t_binfo) = TYPE_BINFO_VTABLE (basetype);
1705 BINFO_VIRTUALS (t_binfo) = TYPE_BINFO_VIRTUALS (basetype);
1706 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1707 b->vfield = CLASSTYPE_VFIELD (basetype);
1708 CLASSTYPE_VFIELD (t) = b->vfield;
1709 /* When we install the first one, set the VF_NORMAL_VALUE
1710 to be the current class, as this it is the most derived
1711 class. Hopefully, this is not set to something else
1712 later. (mrs) */
1713 vfields = b->vfields;
1714 while (vfields)
1716 if (DECL_NAME (CLASSTYPE_VFIELD (t))
1717 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1719 VF_NORMAL_VALUE (vfields) = t;
1720 /* There should only be one of them! And it should
1721 always be found, if we get into here. (mrs) */
1722 break;
1724 vfields = TREE_CHAIN (vfields);
1731 /* Must come after offsets are fixed for all bases. */
1732 for (i = 0; i < n_baseclasses; i++)
1734 tree base_binfo = TREE_VEC_ELT (binfos, i);
1735 tree basetype = BINFO_TYPE (base_binfo);
1737 if (get_base_distance (basetype, t_binfo, 0, (tree*)0) == -2)
1739 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
1740 basetype, t);
1744 tree v = get_vbase_types (t_binfo);
1746 for (; v; v = TREE_CHAIN (v))
1748 tree basetype = BINFO_TYPE (v);
1749 if (get_base_distance (basetype, t_binfo, 0, (tree*)0) == -2)
1751 if (extra_warnings)
1752 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
1753 basetype, t);
1759 tree vfields;
1760 /* Find the base class with the largest number of virtual functions. */
1761 for (vfields = b->vfields; vfields; vfields = TREE_CHAIN (vfields))
1763 if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)) > b->max_has_virtual)
1764 b->max_has_virtual = CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields));
1765 if (VF_DERIVED_VALUE (vfields)
1766 && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)) > b->max_has_virtual)
1767 b->max_has_virtual = CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields));
1771 if (b->vfield == 0)
1772 /* If all virtual functions come only from virtual baseclasses. */
1773 return -1;
1775 /* Update the rtti base if we have a non-virtual base class version
1776 of it. */
1777 b->rtti = CLASSTYPE_RTTI (BINFO_TYPE (TREE_VEC_ELT (binfos, first_vfn_base_index)));
1779 return first_vfn_base_index;
1782 static int
1783 typecode_p (type, code)
1784 tree type;
1785 enum tree_code code;
1787 return (TREE_CODE (type) == code
1788 || (TREE_CODE (type) == REFERENCE_TYPE
1789 && TREE_CODE (TREE_TYPE (type)) == code));
1792 /* Set memoizing fields and bits of T (and its variants) for later use.
1793 MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables. */
1795 static void
1796 finish_struct_bits (t, max_has_virtual)
1797 tree t;
1798 int max_has_virtual;
1800 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1802 /* Fix up variants (if any). */
1803 tree variants = TYPE_NEXT_VARIANT (t);
1804 while (variants)
1806 /* These fields are in the _TYPE part of the node, not in
1807 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1808 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1809 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1810 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1811 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
1813 TYPE_USES_COMPLEX_INHERITANCE (variants) = TYPE_USES_COMPLEX_INHERITANCE (t);
1814 TYPE_VIRTUAL_P (variants) = TYPE_VIRTUAL_P (t);
1815 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1816 /* Copy whatever these are holding today. */
1817 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1818 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1819 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1820 TYPE_SIZE (variants) = TYPE_SIZE (t);
1821 variants = TYPE_NEXT_VARIANT (variants);
1824 if (n_baseclasses && max_has_virtual)
1826 /* Done by `finish_struct' for classes without baseclasses. */
1827 int might_have_abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (t) != 0;
1828 tree binfos = TYPE_BINFO_BASETYPES (t);
1829 for (i = n_baseclasses-1; i >= 0; i--)
1831 might_have_abstract_virtuals
1832 |= (CLASSTYPE_ABSTRACT_VIRTUALS (BINFO_TYPE (TREE_VEC_ELT (binfos, i))) != 0);
1833 if (might_have_abstract_virtuals)
1834 break;
1836 if (might_have_abstract_virtuals)
1838 /* We use error_mark_node from override_one_vtable to signal
1839 an artificial abstract. */
1840 if (CLASSTYPE_ABSTRACT_VIRTUALS (t) == error_mark_node)
1841 CLASSTYPE_ABSTRACT_VIRTUALS (t) = NULL_TREE;
1842 CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
1846 if (n_baseclasses)
1848 /* Notice whether this class has type conversion functions defined. */
1849 tree binfo = TYPE_BINFO (t);
1850 tree binfos = BINFO_BASETYPES (binfo);
1851 tree basetype;
1853 for (i = n_baseclasses-1; i >= 0; i--)
1855 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1857 if (TYPE_HAS_CONVERSION (basetype))
1859 TYPE_HAS_CONVERSION (t) = 1;
1860 TYPE_HAS_INT_CONVERSION (t) |= TYPE_HAS_INT_CONVERSION (basetype);
1861 TYPE_HAS_REAL_CONVERSION (t) |= TYPE_HAS_REAL_CONVERSION (basetype);
1863 if (CLASSTYPE_MAX_DEPTH (basetype) >= CLASSTYPE_MAX_DEPTH (t))
1864 CLASSTYPE_MAX_DEPTH (t) = CLASSTYPE_MAX_DEPTH (basetype) + 1;
1868 /* If this type has a copy constructor, force its mode to be BLKmode, and
1869 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1870 be passed by invisible reference and prevent it from being returned in
1871 a register.
1873 Also do this if the class has BLKmode but can still be returned in
1874 registers, since function_cannot_inline_p won't let us inline
1875 functions returning such a type. This affects the HP-PA. */
1876 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1877 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1878 && CLASSTYPE_NON_AGGREGATE (t)))
1880 tree variants;
1881 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1882 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1884 TYPE_MODE (variants) = BLKmode;
1885 TREE_ADDRESSABLE (variants) = 1;
1890 /* Add FNDECL to the method_vec growing on the class_obstack. Used by
1891 finish_struct_methods. Note, FNDECL cannot be a constructor or
1892 destructor, those cases are handled by the caller. */
1894 static void
1895 grow_method (fndecl, method_vec_ptr)
1896 tree fndecl;
1897 tree *method_vec_ptr;
1899 tree method_vec = (tree)obstack_base (&class_obstack);
1901 /* Start off past the constructors and destructor. */
1902 tree *testp = &TREE_VEC_ELT (method_vec, 2);
1904 while (testp < (tree *) obstack_next_free (&class_obstack)
1905 && (*testp == NULL_TREE || DECL_NAME (*testp) != DECL_NAME (fndecl)))
1906 testp++;
1908 if (testp < (tree *) obstack_next_free (&class_obstack))
1910 tree *p;
1911 for (p = testp; *p; )
1912 p = &DECL_CHAIN (*p);
1913 *p = fndecl;
1915 else
1917 obstack_ptr_grow (&class_obstack, fndecl);
1918 *method_vec_ptr = (tree)obstack_base (&class_obstack);
1922 /* Warn about duplicate methods in fn_fields. Also compact method
1923 lists so that lookup can be made faster.
1925 Algorithm: Outer loop builds lists by method name. Inner loop
1926 checks for redundant method names within a list.
1928 Data Structure: List of method lists. The outer list is a
1929 TREE_LIST, whose TREE_PURPOSE field is the field name and the
1930 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
1931 links the entire list of methods for TYPE_METHODS. Friends are
1932 chained in the same way as member functions (? TREE_CHAIN or
1933 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
1934 list. That allows them to be quickly deleted, and requires no
1935 extra storage.
1937 If there are any constructors/destructors, they are moved to the
1938 front of the list. This makes pushclass more efficient.
1940 We also link each field which has shares a name with its baseclass
1941 to the head of the list of fields for that base class. This allows
1942 us to reduce search time in places like `build_method_call' to
1943 consider only reasonably likely functions. */
1945 tree
1946 finish_struct_methods (t, fn_fields, nonprivate_method)
1947 tree t;
1948 tree fn_fields;
1949 int nonprivate_method;
1951 tree method_vec;
1952 tree save_fn_fields = fn_fields;
1953 tree ctor_name = constructor_name (t);
1954 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1956 /* Now prepare to gather fn_fields into vector. */
1957 struct obstack *ambient_obstack = current_obstack;
1958 current_obstack = &class_obstack;
1959 method_vec = make_tree_vec (2);
1960 current_obstack = ambient_obstack;
1962 /* Now make this a live vector. */
1963 obstack_free (&class_obstack, method_vec);
1965 /* Save room for constructors and destructors. */
1966 obstack_blank (&class_obstack, sizeof (struct tree_vec) + sizeof (struct tree *));
1968 /* First fill in entry 0 with the constructors, entry 1 with destructors,
1969 and the next few with type conversion operators (if any). */
1971 for (; fn_fields; fn_fields = TREE_CHAIN (fn_fields))
1973 tree fn_name = DECL_NAME (fn_fields);
1975 /* Clear out this flag.
1977 @@ Doug may figure out how to break
1978 @@ this with nested classes and friends. */
1979 DECL_IN_AGGR_P (fn_fields) = 0;
1981 /* Note here that a copy ctor is private, so we don't dare generate
1982 a default copy constructor for a class that has a member
1983 of this type without making sure they have access to it. */
1984 if (fn_name == ctor_name)
1986 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
1987 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
1989 if (TREE_CODE (parmtype) == REFERENCE_TYPE
1990 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
1992 if (TREE_CHAIN (parmtypes) == NULL_TREE
1993 || TREE_CHAIN (parmtypes) == void_list_node
1994 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
1996 if (TREE_PROTECTED (fn_fields))
1997 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
1998 else if (TREE_PRIVATE (fn_fields))
1999 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2002 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fn_fields)))
2004 /* Destructors go in slot 1. */
2005 DECL_CHAIN (fn_fields) = TREE_VEC_ELT (method_vec, 1);
2006 TREE_VEC_ELT (method_vec, 1) = fn_fields;
2008 else
2010 /* Constructors go in slot 0. */
2011 DECL_CHAIN (fn_fields) = TREE_VEC_ELT (method_vec, 0);
2012 TREE_VEC_ELT (method_vec, 0) = fn_fields;
2015 else if (IDENTIFIER_TYPENAME_P (fn_name))
2017 tree return_type = TREE_TYPE (TREE_TYPE (fn_fields));
2019 if (typecode_p (return_type, INTEGER_TYPE)
2020 || typecode_p (return_type, BOOLEAN_TYPE)
2021 || typecode_p (return_type, ENUMERAL_TYPE))
2022 TYPE_HAS_INT_CONVERSION (t) = 1;
2023 else if (typecode_p (return_type, REAL_TYPE))
2024 TYPE_HAS_REAL_CONVERSION (t) = 1;
2026 grow_method (fn_fields, &method_vec);
2030 fn_fields = save_fn_fields;
2031 for (; fn_fields; fn_fields = TREE_CHAIN (fn_fields))
2033 tree fn_name = DECL_NAME (fn_fields);
2035 if (fn_name == ctor_name || IDENTIFIER_TYPENAME_P (fn_name))
2036 continue;
2038 if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2040 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2042 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2044 if (TREE_PROTECTED (fn_fields))
2045 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2046 else if (TREE_PRIVATE (fn_fields))
2047 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2051 grow_method (fn_fields, &method_vec);
2054 TREE_VEC_LENGTH (method_vec) = (tree *)obstack_next_free (&class_obstack)
2055 - (&TREE_VEC_ELT (method_vec, 0));
2056 obstack_finish (&class_obstack);
2057 CLASSTYPE_METHOD_VEC (t) = method_vec;
2059 if (nonprivate_method == 0
2060 && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
2061 && DECL_FRIENDLIST (TYPE_MAIN_DECL (t)) == NULL_TREE)
2063 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2064 for (i = 0; i < n_baseclasses; i++)
2065 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2066 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2068 nonprivate_method = 1;
2069 break;
2071 if (nonprivate_method == 0
2072 && warn_ctor_dtor_privacy)
2073 cp_warning ("all member functions in class `%T' are private", t);
2076 /* Warn if all destructors are private (in which case this class is
2077 effectively unusable. */
2078 if (TYPE_HAS_DESTRUCTOR (t))
2080 tree dtor = TREE_VEC_ELT (method_vec, 1);
2082 /* Wild parse errors can cause this to happen. */
2083 if (dtor == NULL_TREE)
2084 TYPE_HAS_DESTRUCTOR (t) = 0;
2085 else if (TREE_PRIVATE (dtor)
2086 && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
2087 && DECL_FRIENDLIST (TYPE_MAIN_DECL (t)) == NULL_TREE
2088 && warn_ctor_dtor_privacy)
2089 cp_warning ("`%#T' only defines a private destructor and has no friends",
2093 /* Now for each member function (except for constructors and
2094 destructors), compute where member functions of the same
2095 name reside in base classes. */
2096 if (n_baseclasses != 0
2097 && TREE_VEC_LENGTH (method_vec) > 2)
2099 int len = TREE_VEC_LENGTH (method_vec);
2100 tree baselink_vec = make_tree_vec (len);
2101 int any_links = 0;
2102 tree baselink_binfo = build_tree_list (NULL_TREE, TYPE_BINFO (t));
2104 for (i = 2; i < len; i++)
2106 TREE_VEC_ELT (baselink_vec, i)
2107 = get_baselinks (baselink_binfo, t, DECL_NAME (TREE_VEC_ELT (method_vec, i)));
2108 if (TREE_VEC_ELT (baselink_vec, i) != 0)
2109 any_links = 1;
2111 if (any_links != 0)
2112 CLASSTYPE_BASELINK_VEC (t) = baselink_vec;
2113 else
2114 obstack_free (current_obstack, baselink_vec);
2117 return method_vec;
2120 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2122 void
2123 duplicate_tag_error (t)
2124 tree t;
2126 cp_error ("redefinition of `%#T'", t);
2127 cp_error_at ("previous definition here", t);
2129 /* Pretend we haven't defined this type. */
2131 /* All of the component_decl's were TREE_CHAINed together in the parser.
2132 finish_struct_methods walks these chains and assembles all methods with
2133 the same base name into DECL_CHAINs. Now we don't need the parser chains
2134 anymore, so we unravel them. */
2136 /* This used to be in finish_struct, but it turns out that the
2137 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2138 things... */
2139 if (CLASSTYPE_METHOD_VEC (t))
2141 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2142 int i, len = TREE_VEC_LENGTH (method_vec);
2143 for (i = 0; i < len; i++)
2145 tree unchain = TREE_VEC_ELT (method_vec, i);
2146 while (unchain != NULL_TREE)
2148 TREE_CHAIN (unchain) = NULL_TREE;
2149 unchain = DECL_CHAIN (unchain);
2154 if (TYPE_LANG_SPECIFIC (t))
2156 tree as_list = CLASSTYPE_AS_LIST (t);
2157 tree binfo = TYPE_BINFO (t);
2158 tree binfo_as_list = CLASSTYPE_BINFO_AS_LIST (t);
2159 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2160 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2162 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2163 BINFO_BASETYPES(binfo) = NULL_TREE;
2165 CLASSTYPE_AS_LIST (t) = as_list;
2166 TYPE_BINFO (t) = binfo;
2167 CLASSTYPE_BINFO_AS_LIST (t) = binfo_as_list;
2168 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2169 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2170 CLASSTYPE_VBASE_SIZE (t) = integer_zero_node;
2171 TYPE_REDEFINED (t) = 1;
2173 TYPE_SIZE (t) = NULL_TREE;
2174 TYPE_MODE (t) = VOIDmode;
2175 TYPE_FIELDS (t) = NULL_TREE;
2176 TYPE_METHODS (t) = NULL_TREE;
2177 TYPE_VFIELD (t) = NULL_TREE;
2178 TYPE_CONTEXT (t) = NULL_TREE;
2181 /* finish up all new vtables. */
2183 static void
2184 finish_vtbls (binfo, do_self, t)
2185 tree binfo;
2186 int do_self;
2187 tree t;
2189 tree binfos = BINFO_BASETYPES (binfo);
2190 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2192 /* Should we use something besides CLASSTYPE_VFIELDS? */
2193 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2195 if (BINFO_NEW_VTABLE_MARKED (binfo))
2197 tree decl, context;
2199 decl = BINFO_VTABLE (binfo);
2200 context = DECL_CONTEXT (decl);
2201 DECL_CONTEXT (decl) = 0;
2202 if (write_virtuals >= 0
2203 && DECL_INITIAL (decl) != BINFO_VIRTUALS (binfo))
2204 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE,
2205 BINFO_VIRTUALS (binfo));
2206 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0);
2207 DECL_CONTEXT (decl) = context;
2209 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2212 for (i = 0; i < n_baselinks; i++)
2214 tree base_binfo = TREE_VEC_ELT (binfos, i);
2215 int is_not_base_vtable
2216 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2217 if (TREE_VIA_VIRTUAL (base_binfo))
2219 base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2221 finish_vtbls (base_binfo, is_not_base_vtable, t);
2225 /* True if we should override the given BASE_FNDECL with the given
2226 FNDECL. */
2228 static int
2229 overrides (fndecl, base_fndecl)
2230 tree fndecl, base_fndecl;
2232 /* Destructors have special names. */
2233 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2234 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2235 return 1;
2236 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2237 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2238 return 0;
2239 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2241 tree types, base_types;
2242 #if 0
2243 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2244 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2245 #endif
2246 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2247 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2248 if ((TYPE_READONLY (TREE_TYPE (TREE_VALUE (base_types)))
2249 == TYPE_READONLY (TREE_TYPE (TREE_VALUE (types))))
2250 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types), 3))
2251 return 1;
2253 return 0;
2256 static tree
2257 get_class_offset_1 (parent, binfo, context, t, fndecl)
2258 tree parent, binfo, context, t, fndecl;
2260 tree binfos = BINFO_BASETYPES (binfo);
2261 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2262 tree rval = NULL_TREE;
2264 if (binfo == parent)
2265 return error_mark_node;
2267 for (i = 0; i < n_baselinks; i++)
2269 tree base_binfo = TREE_VEC_ELT (binfos, i);
2270 tree nrval;
2272 if (TREE_VIA_VIRTUAL (base_binfo))
2273 base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2274 CLASSTYPE_VBASECLASSES (t));
2275 nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl);
2276 /* See if we have a new value */
2277 if (nrval && (nrval != error_mark_node || rval==0))
2279 /* Only compare if we have two offsets */
2280 if (rval && rval != error_mark_node
2281 && ! tree_int_cst_equal (nrval, rval))
2283 /* Only give error if the two offsets are different */
2284 error ("every virtual function must have a unique final overrider");
2285 cp_error (" found two (or more) `%T' class subobjects in `%T'", context, t);
2286 cp_error (" with virtual `%D' from virtual base class", fndecl);
2287 return rval;
2289 rval = nrval;
2292 if (rval && BINFO_TYPE (binfo) == context)
2294 my_friendly_assert (rval == error_mark_node
2295 || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999);
2296 rval = BINFO_OFFSET (binfo);
2299 return rval;
2302 /* Get the offset to the CONTEXT subobject that is related to the
2303 given BINFO. */
2305 static tree
2306 get_class_offset (context, t, binfo, fndecl)
2307 tree context, t, binfo, fndecl;
2309 tree first_binfo = binfo;
2310 tree offset;
2311 int i;
2313 if (context == t)
2314 return integer_zero_node;
2316 if (BINFO_TYPE (binfo) == context)
2317 return BINFO_OFFSET (binfo);
2319 /* Check less derived binfos first. */
2320 while (BINFO_BASETYPES (binfo)
2321 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
2323 tree binfos = BINFO_BASETYPES (binfo);
2324 binfo = TREE_VEC_ELT (binfos, i);
2325 if (BINFO_TYPE (binfo) == context)
2326 return BINFO_OFFSET (binfo);
2329 /* Ok, not found in the less derived binfos, now check the more
2330 derived binfos. */
2331 offset = get_class_offset_1 (first_binfo, TYPE_BINFO (t), context, t, fndecl);
2332 if (offset==0 || TREE_CODE (offset) != INTEGER_CST)
2333 my_friendly_abort (999); /* we have to find it. */
2334 return offset;
2337 /* Skip RTTI information at the front of the virtual list. */
2339 unsigned HOST_WIDE_INT
2340 skip_rtti_stuff (virtuals)
2341 tree *virtuals;
2343 int n;
2345 n = 0;
2346 if (*virtuals)
2348 /* We always reserve a slot for the offset/tdesc entry. */
2349 ++n;
2350 *virtuals = TREE_CHAIN (*virtuals);
2352 if (flag_vtable_thunks && *virtuals)
2354 /* The second slot is reserved for the tdesc pointer when thunks
2355 are used. */
2356 ++n;
2357 *virtuals = TREE_CHAIN (*virtuals);
2359 return n;
2362 static void
2363 modify_one_vtable (binfo, t, fndecl, pfn)
2364 tree binfo, t, fndecl, pfn;
2366 tree virtuals = BINFO_VIRTUALS (binfo);
2367 unsigned HOST_WIDE_INT n;
2369 /* update rtti entry */
2370 if (flag_rtti)
2372 if (binfo == TYPE_BINFO (t))
2374 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2375 build_vtable (TYPE_BINFO (DECL_CONTEXT (CLASSTYPE_VFIELD (t))), t);
2377 else
2379 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2380 prepare_fresh_vtable (binfo, t);
2383 if (fndecl == NULL_TREE)
2384 return;
2386 n = skip_rtti_stuff (&virtuals);
2388 while (virtuals)
2390 tree current_fndecl = TREE_VALUE (virtuals);
2391 current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl);
2392 current_fndecl = TREE_OPERAND (current_fndecl, 0);
2393 if (current_fndecl && overrides (fndecl, current_fndecl))
2395 tree base_offset, offset;
2396 tree context = DECL_CLASS_CONTEXT (fndecl);
2397 tree vfield = CLASSTYPE_VFIELD (t);
2398 tree this_offset;
2400 offset = get_class_offset (context, t, binfo, fndecl);
2402 /* Find the right offset for the this pointer based on the
2403 base class we just found. We have to take into
2404 consideration the virtual base class pointers that we
2405 stick in before the virtual function table pointer.
2407 Also, we want just the delta between the most base class
2408 that we derived this vfield from and us. */
2409 base_offset = size_binop (PLUS_EXPR,
2410 get_derived_offset (binfo, DECL_CONTEXT (current_fndecl)),
2411 BINFO_OFFSET (binfo));
2412 this_offset = size_binop (MINUS_EXPR, offset, base_offset);
2414 /* Make sure we can modify the derived association with immunity. */
2415 if (TREE_USED (binfo))
2416 my_friendly_assert (0, 999);
2418 if (binfo == TYPE_BINFO (t))
2420 /* In this case, it is *type*'s vtable we are modifying.
2421 We start with the approximation that it's vtable is that
2422 of the immediate base class. */
2423 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2424 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2426 else
2428 /* This is our very own copy of `basetype' to play with.
2429 Later, we will fill in all the virtual functions
2430 that override the virtual functions in these base classes
2431 which are not defined by the current type. */
2432 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2433 prepare_fresh_vtable (binfo, t);
2436 #ifdef NOTQUITE
2437 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
2438 #endif
2439 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2440 build_vtable_entry (this_offset, pfn),
2441 fndecl);
2443 ++n;
2444 virtuals = TREE_CHAIN (virtuals);
2448 /* These are the ones that are not through virtual base classes. */
2450 static void
2451 modify_all_direct_vtables (binfo, do_self, t, fndecl, pfn)
2452 tree binfo;
2453 int do_self;
2454 tree t, fndecl, pfn;
2456 tree binfos = BINFO_BASETYPES (binfo);
2457 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2459 /* Should we use something besides CLASSTYPE_VFIELDS? */
2460 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2462 modify_one_vtable (binfo, t, fndecl, pfn);
2465 for (i = 0; i < n_baselinks; i++)
2467 tree base_binfo = TREE_VEC_ELT (binfos, i);
2468 int is_not_base_vtable
2469 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2470 if (! TREE_VIA_VIRTUAL (base_binfo))
2471 modify_all_direct_vtables (base_binfo, is_not_base_vtable, t, fndecl, pfn);
2475 /* Fixup all the delta entries in this one vtable that need updating. */
2477 static void
2478 fixup_vtable_deltas1 (binfo, t)
2479 tree binfo, t;
2481 tree virtuals = BINFO_VIRTUALS (binfo);
2482 unsigned HOST_WIDE_INT n;
2484 n = skip_rtti_stuff (&virtuals);
2486 while (virtuals)
2488 tree fndecl = TREE_VALUE (virtuals);
2489 tree pfn = FNADDR_FROM_VTABLE_ENTRY (fndecl);
2490 tree delta = DELTA_FROM_VTABLE_ENTRY (fndecl);
2491 fndecl = TREE_OPERAND (pfn, 0);
2492 if (fndecl)
2494 tree base_offset, offset;
2495 tree context = DECL_CLASS_CONTEXT (fndecl);
2496 tree vfield = CLASSTYPE_VFIELD (t);
2497 tree this_offset;
2499 offset = get_class_offset (context, t, binfo, fndecl);
2501 /* Find the right offset for the this pointer based on the
2502 base class we just found. We have to take into
2503 consideration the virtual base class pointers that we
2504 stick in before the virtual function table pointer.
2506 Also, we want just the delta between the most base class
2507 that we derived this vfield from and us. */
2508 base_offset = size_binop (PLUS_EXPR,
2509 get_derived_offset (binfo, DECL_CONTEXT (fndecl)),
2510 BINFO_OFFSET (binfo));
2511 this_offset = size_binop (MINUS_EXPR, offset, base_offset);
2513 if (! tree_int_cst_equal (this_offset, delta))
2515 /* Make sure we can modify the derived association with immunity. */
2516 if (TREE_USED (binfo))
2517 my_friendly_assert (0, 999);
2519 if (binfo == TYPE_BINFO (t))
2521 /* In this case, it is *type*'s vtable we are modifying.
2522 We start with the approximation that it's vtable is that
2523 of the immediate base class. */
2524 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2525 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2527 else
2529 /* This is our very own copy of `basetype' to play with.
2530 Later, we will fill in all the virtual functions
2531 that override the virtual functions in these base classes
2532 which are not defined by the current type. */
2533 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2534 prepare_fresh_vtable (binfo, t);
2537 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2538 build_vtable_entry (this_offset, pfn),
2539 fndecl);
2542 ++n;
2543 virtuals = TREE_CHAIN (virtuals);
2547 /* Fixup all the delta entries in all the direct vtables that need updating.
2548 This happens when we have non-overridden virtual functions from a
2549 virtual base class, that are at a different offset, in the new
2550 hierarchy, because the layout of the virtual bases has changed. */
2552 static void
2553 fixup_vtable_deltas (binfo, init_self, t)
2554 tree binfo;
2555 int init_self;
2556 tree t;
2558 tree binfos = BINFO_BASETYPES (binfo);
2559 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2561 for (i = 0; i < n_baselinks; i++)
2563 tree base_binfo = TREE_VEC_ELT (binfos, i);
2564 int is_not_base_vtable
2565 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2566 if (! TREE_VIA_VIRTUAL (base_binfo))
2567 fixup_vtable_deltas (base_binfo, is_not_base_vtable, t);
2569 /* Should we use something besides CLASSTYPE_VFIELDS? */
2570 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2572 fixup_vtable_deltas1 (binfo, t);
2576 /* These are the ones that are through virtual base classes. */
2578 static void
2579 modify_all_indirect_vtables (binfo, do_self, via_virtual, t, fndecl, pfn)
2580 tree binfo;
2581 int do_self, via_virtual;
2582 tree t, fndecl, pfn;
2584 tree binfos = BINFO_BASETYPES (binfo);
2585 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2587 /* Should we use something besides CLASSTYPE_VFIELDS? */
2588 if (do_self && via_virtual && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2590 modify_one_vtable (binfo, t, fndecl, pfn);
2593 for (i = 0; i < n_baselinks; i++)
2595 tree base_binfo = TREE_VEC_ELT (binfos, i);
2596 int is_not_base_vtable
2597 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2598 if (TREE_VIA_VIRTUAL (base_binfo))
2600 via_virtual = 1;
2601 base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2603 modify_all_indirect_vtables (base_binfo, is_not_base_vtable, via_virtual, t, fndecl, pfn);
2607 static void
2608 modify_all_vtables (t, fndecl, vfn)
2609 tree t, fndecl, vfn;
2611 /* Do these first, so that we will make use of any non-virtual class's
2612 vtable, over a virtual classes vtable. */
2613 modify_all_direct_vtables (TYPE_BINFO (t), 1, t, fndecl, vfn);
2614 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
2615 modify_all_indirect_vtables (TYPE_BINFO (t), 1, 0, t, fndecl, vfn);
2618 /* Here, we already know that they match in every respect.
2619 All we have to check is where they had their declarations. */
2621 static int
2622 strictly_overrides (fndecl1, fndecl2)
2623 tree fndecl1, fndecl2;
2625 int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
2626 DECL_CLASS_CONTEXT (fndecl1),
2627 0, (tree *)0);
2628 if (distance == -2 || distance > 0)
2629 return 1;
2630 return 0;
2633 /* Merge overrides for one vtable.
2634 If we want to merge in same function, we are fine.
2635 else
2636 if one has a DECL_CLASS_CONTEXT that is a parent of the
2637 other, than choose the more derived one
2638 else
2639 potentially ill-formed (see 10.3 [class.virtual])
2640 we have to check later to see if there was an
2641 override in this class. If there was ok, if not
2642 then it is ill-formed. (mrs)
2644 We take special care to reuse a vtable, if we can. */
2646 static void
2647 override_one_vtable (binfo, old, t)
2648 tree binfo, old, t;
2650 tree virtuals = BINFO_VIRTUALS (binfo);
2651 tree old_virtuals = BINFO_VIRTUALS (old);
2652 enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
2654 /* If we have already committed to modifying it, then don't try and
2655 reuse another vtable. */
2656 if (BINFO_NEW_VTABLE_MARKED (binfo))
2657 choose = NEITHER;
2659 skip_rtti_stuff (&virtuals);
2660 skip_rtti_stuff (&old_virtuals);
2662 while (virtuals)
2664 tree fndecl = TREE_VALUE (virtuals);
2665 tree old_fndecl = TREE_VALUE (old_virtuals);
2666 fndecl = FNADDR_FROM_VTABLE_ENTRY (fndecl);
2667 old_fndecl = FNADDR_FROM_VTABLE_ENTRY (old_fndecl);
2668 fndecl = TREE_OPERAND (fndecl, 0);
2669 old_fndecl = TREE_OPERAND (old_fndecl, 0);
2670 /* First check to see if they are the same. */
2671 if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
2673 /* No need to do anything. */
2675 else if (strictly_overrides (fndecl, old_fndecl))
2677 if (choose == UNDECIDED)
2678 choose = REUSE_NEW;
2679 else if (choose == REUSE_OLD)
2681 choose = NEITHER;
2682 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2684 prepare_fresh_vtable (binfo, t);
2685 override_one_vtable (binfo, old, t);
2686 return;
2690 else if (strictly_overrides (old_fndecl, fndecl))
2692 if (choose == UNDECIDED)
2693 choose = REUSE_OLD;
2694 else if (choose == REUSE_NEW)
2696 choose = NEITHER;
2697 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2699 prepare_fresh_vtable (binfo, t);
2700 override_one_vtable (binfo, old, t);
2701 return;
2703 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2705 else if (choose == NEITHER)
2707 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2710 else
2712 choose = NEITHER;
2713 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2715 prepare_fresh_vtable (binfo, t);
2716 override_one_vtable (binfo, old, t);
2717 return;
2720 /* This MUST be overridden, or the class is ill-formed. */
2721 /* For now, we just make it abstract. */
2722 tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
2723 tree vfn;
2725 fndecl = copy_node (fndecl);
2726 copy_lang_decl (fndecl);
2727 DECL_ABSTRACT_VIRTUAL_P (fndecl) = 1;
2728 DECL_NEEDS_FINAL_OVERRIDER_P (fndecl) = 1;
2729 /* Make sure we search for it later. */
2730 if (! CLASSTYPE_ABSTRACT_VIRTUALS (t))
2731 CLASSTYPE_ABSTRACT_VIRTUALS (t) = error_mark_node;
2733 vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
2734 TREE_CONSTANT (vfn) = 1;
2736 /* We can use integer_zero_node, as we will will core dump
2737 if this is used anyway. */
2738 TREE_VALUE (virtuals) = build_vtable_entry (integer_zero_node, vfn);
2741 virtuals = TREE_CHAIN (virtuals);
2742 old_virtuals = TREE_CHAIN (old_virtuals);
2745 /* Let's reuse the old vtable. */
2746 if (choose == REUSE_OLD)
2748 BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
2749 BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
2753 /* Merge in overrides for virtual bases.
2754 BINFO is the hierarchy we want to modify, and OLD has the potential
2755 overrides. */
2757 static void
2758 merge_overrides (binfo, old, do_self, t)
2759 tree binfo, old;
2760 int do_self;
2761 tree t;
2763 tree binfos = BINFO_BASETYPES (binfo);
2764 tree old_binfos = BINFO_BASETYPES (old);
2765 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2767 /* Should we use something besides CLASSTYPE_VFIELDS? */
2768 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2770 override_one_vtable (binfo, old, t);
2773 for (i = 0; i < n_baselinks; i++)
2775 tree base_binfo = TREE_VEC_ELT (binfos, i);
2776 tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
2777 int is_not_base_vtable
2778 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2779 if (! TREE_VIA_VIRTUAL (base_binfo))
2780 merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
2784 /* Get the base virtual function declarations in T that are either
2785 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2786 the overrider/hider. */
2788 static tree
2789 get_basefndecls (fndecl, t)
2790 tree fndecl, t;
2792 tree methods = TYPE_METHODS (t);
2793 tree base_fndecls = NULL_TREE;
2794 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2795 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2797 while (methods)
2799 if (TREE_CODE (methods) == FUNCTION_DECL
2800 && DECL_VINDEX (methods) != NULL_TREE
2801 && DECL_NAME (fndecl) == DECL_NAME (methods))
2802 base_fndecls = temp_tree_cons (fndecl, methods, base_fndecls);
2804 methods = TREE_CHAIN (methods);
2807 if (base_fndecls)
2808 return base_fndecls;
2810 for (i = 0; i < n_baseclasses; i++)
2812 tree base_binfo = TREE_VEC_ELT (binfos, i);
2813 tree basetype = BINFO_TYPE (base_binfo);
2815 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2816 base_fndecls);
2819 return base_fndecls;
2822 /* Mark the functions that have been hidden with their overriders.
2823 Since we start out with all functions already marked with a hider,
2824 no need to mark functions that are just hidden. */
2826 static void
2827 mark_overriders (fndecl, base_fndecls)
2828 tree fndecl, base_fndecls;
2830 while (base_fndecls)
2832 if (overrides (TREE_VALUE (base_fndecls), fndecl))
2833 TREE_PURPOSE (base_fndecls) = fndecl;
2835 base_fndecls = TREE_CHAIN (base_fndecls);
2839 /* If this declaration supersedes the declaration of
2840 a method declared virtual in the base class, then
2841 mark this field as being virtual as well. */
2843 static void
2844 check_for_override (decl, ctype)
2845 tree decl, ctype;
2847 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
2848 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2849 int virtualp = DECL_VIRTUAL_P (decl);
2851 for (i = 0; i < n_baselinks; i++)
2853 tree base_binfo = TREE_VEC_ELT (binfos, i);
2854 if (TYPE_VIRTUAL_P (BINFO_TYPE (base_binfo))
2855 || flag_all_virtual == 1)
2857 tree tmp = get_matching_virtual
2858 (base_binfo, decl,
2859 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
2860 if (tmp)
2862 /* If this function overrides some virtual in some base
2863 class, then the function itself is also necessarily
2864 virtual, even if the user didn't explicitly say so. */
2865 DECL_VIRTUAL_P (decl) = 1;
2867 /* The TMP we really want is the one from the deepest
2868 baseclass on this path, taking care not to
2869 duplicate if we have already found it (via another
2870 path to its virtual baseclass. */
2871 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
2873 cp_error_at ("method `%D' may not be declared static",
2874 decl);
2875 cp_error_at ("(since `%D' declared virtual in base class.)",
2876 tmp);
2877 break;
2879 virtualp = 1;
2881 #if 0 /* The signature of an overriding function is not changed. */
2883 /* The argument types may have changed... */
2884 tree type = TREE_TYPE (decl);
2885 tree argtypes = TYPE_ARG_TYPES (type);
2886 tree base_variant = TREE_TYPE (TREE_VALUE (argtypes));
2887 tree raises = TYPE_RAISES_EXCEPTIONS (type);
2889 argtypes = commonparms (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (tmp))),
2890 TREE_CHAIN (argtypes));
2891 /* But the return type has not. */
2892 type = build_cplus_method_type (base_variant, TREE_TYPE (type), argtypes);
2893 if (raises)
2894 type = build_exception_variant (type, raises);
2895 TREE_TYPE (decl) = type;
2897 #endif
2898 DECL_VINDEX (decl)
2899 = tree_cons (NULL_TREE, tmp, DECL_VINDEX (decl));
2900 break;
2904 if (virtualp)
2906 if (DECL_VINDEX (decl) == NULL_TREE)
2907 DECL_VINDEX (decl) = error_mark_node;
2908 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2912 /* Warn about hidden virtual functions that are not overridden in t.
2913 We know that constructors and destructors don't apply. */
2915 void
2916 warn_hidden (t)
2917 tree t;
2919 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2920 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2921 int i;
2923 /* We go through each separately named virtual function. */
2924 for (i = 2; i < n_methods; ++i)
2926 tree fndecl = TREE_VEC_ELT (method_vec, i);
2928 tree base_fndecls = NULL_TREE;
2929 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2930 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2932 if (DECL_VINDEX (fndecl) == NULL_TREE)
2933 continue;
2935 /* First we get a list of all possible functions that might be
2936 hidden from each base class. */
2937 for (i = 0; i < n_baseclasses; i++)
2939 tree base_binfo = TREE_VEC_ELT (binfos, i);
2940 tree basetype = BINFO_TYPE (base_binfo);
2942 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2943 base_fndecls);
2946 if (TREE_CHAIN (fndecl)
2947 && DECL_NAME (TREE_CHAIN (fndecl)) == DECL_NAME (fndecl))
2948 fndecl = TREE_CHAIN (fndecl);
2949 else
2950 fndecl = NULL_TREE;
2952 /* ...then mark up all the base functions with overriders, preferring
2953 overriders to hiders. */
2954 if (base_fndecls)
2955 while (fndecl)
2957 mark_overriders (fndecl, base_fndecls);
2959 if (TREE_CHAIN (fndecl)
2960 && DECL_NAME (TREE_CHAIN (fndecl)) == DECL_NAME (fndecl))
2961 fndecl = TREE_CHAIN (fndecl);
2962 else
2963 fndecl = NULL_TREE;
2966 /* Now give a warning for all base functions without overriders,
2967 as they are hidden. */
2968 while (base_fndecls)
2970 if (! overrides (TREE_VALUE (base_fndecls),
2971 TREE_PURPOSE (base_fndecls)))
2973 /* Here we know it is a hider, and no overrider exists. */
2974 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2975 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
2978 base_fndecls = TREE_CHAIN (base_fndecls);
2983 /* Check for things that are invalid. There are probably plenty of other
2984 things we should check for also. */
2986 static void
2987 finish_struct_anon (t)
2988 tree t;
2990 tree field;
2991 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2993 if (TREE_STATIC (field))
2994 continue;
2995 if (TREE_CODE (field) != FIELD_DECL)
2996 continue;
2998 if (DECL_NAME (field) == NULL_TREE
2999 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
3001 tree* uelt = &TYPE_FIELDS (TREE_TYPE (field));
3002 for (; *uelt; uelt = &TREE_CHAIN (*uelt))
3004 if (TREE_CODE (*uelt) != FIELD_DECL)
3005 continue;
3007 if (TREE_PRIVATE (*uelt))
3008 cp_pedwarn_at ("private member `%#D' in anonymous union",
3009 *uelt);
3010 else if (TREE_PROTECTED (*uelt))
3011 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3012 *uelt);
3014 TREE_PRIVATE (*uelt) = TREE_PRIVATE (field);
3015 TREE_PROTECTED (*uelt) = TREE_PROTECTED (field);
3021 extern int interface_only, interface_unknown;
3023 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
3024 (or C++ class declaration).
3026 For C++, we must handle the building of derived classes.
3027 Also, C++ allows static class members. The way that this is
3028 handled is to keep the field name where it is (as the DECL_NAME
3029 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
3030 of the field. layout_record and layout_union will know about this.
3032 More C++ hair: inline functions have text in their
3033 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
3034 meaningful tree structure. After the struct has been laid out, set
3035 things up so that this can happen.
3037 And still more: virtual functions. In the case of single inheritance,
3038 when a new virtual function is seen which redefines a virtual function
3039 from the base class, the new virtual function is placed into
3040 the virtual function table at exactly the same address that
3041 it had in the base class. When this is extended to multiple
3042 inheritance, the same thing happens, except that multiple virtual
3043 function tables must be maintained. The first virtual function
3044 table is treated in exactly the same way as in the case of single
3045 inheritance. Additional virtual function tables have different
3046 DELTAs, which tell how to adjust `this' to point to the right thing.
3048 LIST_OF_FIELDLISTS is just that. The elements of the list are
3049 TREE_LIST elements, whose TREE_PURPOSE field tells what access
3050 the list has, and the TREE_VALUE slot gives the actual fields.
3052 ATTRIBUTES is the set of decl attributes to be applied, if any.
3054 If flag_all_virtual == 1, then we lay all functions into
3055 the virtual function table, as though they were declared
3056 virtual. Constructors do not lay down in the virtual function table.
3058 If flag_all_virtual == 2, then we lay all functions into
3059 the virtual function table, such that virtual functions
3060 occupy a space by themselves, and then all functions
3061 of the class occupy a space by themselves. This is illustrated
3062 in the following diagram:
3064 class A; class B : A;
3066 Class A's vtbl: Class B's vtbl:
3067 --------------------------------------------------------------------
3068 | A's virtual functions| | B's virtual functions |
3069 | | | (may inherit some from A). |
3070 --------------------------------------------------------------------
3071 | All of A's functions | | All of A's functions |
3072 | (such as a->A::f). | | (such as b->A::f) |
3073 --------------------------------------------------------------------
3074 | B's new virtual functions |
3075 | (not defined in A.) |
3076 -------------------------------
3077 | All of B's functions |
3078 | (such as b->B::f) |
3079 -------------------------------
3081 this allows the program to make references to any function, virtual
3082 or otherwise in a type-consistent manner. */
3084 tree
3085 finish_struct_1 (t, warn_anon)
3086 tree t;
3087 int warn_anon;
3089 int old;
3090 tree name = TYPE_IDENTIFIER (t);
3091 enum tree_code code = TREE_CODE (t);
3092 tree fields = TYPE_FIELDS (t);
3093 tree fn_fields = TYPE_METHODS (t);
3094 tree x, last_x, method_vec;
3095 int all_virtual;
3096 int has_virtual;
3097 int max_has_virtual;
3098 tree pending_virtuals = NULL_TREE;
3099 tree pending_hard_virtuals = NULL_TREE;
3100 tree abstract_virtuals = NULL_TREE;
3101 tree vfield;
3102 tree vfields;
3103 int cant_have_default_ctor;
3104 int cant_have_const_ctor;
3105 int no_const_asn_ref;
3107 /* The index of the first base class which has virtual
3108 functions. Only applied to non-virtual baseclasses. */
3109 int first_vfn_base_index;
3111 int n_baseclasses;
3112 int any_default_members = 0;
3113 int const_sans_init = 0;
3114 int ref_sans_init = 0;
3115 int nonprivate_method = 0;
3116 tree t_binfo = TYPE_BINFO (t);
3117 tree access_decls = NULL_TREE;
3118 int aggregate = 1;
3119 int empty = 1;
3120 int has_pointers = 0;
3122 if (warn_anon && code != UNION_TYPE && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
3123 pedwarn ("anonymous class type not used to declare any objects");
3125 if (TYPE_SIZE (t))
3127 if (IS_AGGR_TYPE (t))
3128 cp_error ("redefinition of `%#T'", t);
3129 else
3130 my_friendly_abort (172);
3131 popclass (0);
3132 return t;
3135 GNU_xref_decl (current_function_decl, t);
3137 /* If this type was previously laid out as a forward reference,
3138 make sure we lay it out again. */
3140 TYPE_SIZE (t) = NULL_TREE;
3141 CLASSTYPE_GOT_SEMICOLON (t) = 0;
3143 #if 0
3144 /* This is in general too late to do this. I moved the main case up to
3145 left_curly, what else needs to move? */
3146 if (! IS_SIGNATURE (t))
3148 my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t) == interface_only, 999);
3149 my_friendly_assert (CLASSTYPE_INTERFACE_KNOWN (t) == ! interface_unknown, 999);
3151 #endif
3153 #if 0
3154 if (flag_rtti)
3155 build_t_desc (t, 0);
3156 #endif
3158 TYPE_BINFO (t) = NULL_TREE;
3160 old = suspend_momentary ();
3162 /* Install struct as DECL_FIELD_CONTEXT of each field decl.
3163 Also process specified field sizes.
3164 Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
3165 The specified size is found in the DECL_INITIAL.
3166 Store 0 there, except for ": 0" fields (so we can find them
3167 and delete them, below). */
3169 if (t_binfo && BINFO_BASETYPES (t_binfo))
3170 n_baseclasses = TREE_VEC_LENGTH (BINFO_BASETYPES (t_binfo));
3171 else
3172 n_baseclasses = 0;
3174 if (n_baseclasses > 0)
3176 struct base_info base_info;
3178 /* If using multiple inheritance, this may cause variants of our
3179 basetypes to be used (instead of their canonical forms). */
3180 tree vf = layout_basetypes (t, BINFO_BASETYPES (t_binfo));
3181 last_x = tree_last (vf);
3182 fields = chainon (vf, fields);
3184 first_vfn_base_index = finish_base_struct (t, &base_info, t_binfo);
3185 /* Remember where we got our vfield from. */
3186 CLASSTYPE_VFIELD_PARENT (t) = first_vfn_base_index;
3187 has_virtual = base_info.has_virtual;
3188 max_has_virtual = base_info.max_has_virtual;
3189 CLASSTYPE_N_SUPERCLASSES (t) += base_info.n_ancestors;
3190 vfield = base_info.vfield;
3191 vfields = base_info.vfields;
3192 CLASSTYPE_RTTI (t) = base_info.rtti;
3193 cant_have_default_ctor = base_info.cant_have_default_ctor;
3194 cant_have_const_ctor = base_info.cant_have_const_ctor;
3195 no_const_asn_ref = base_info.no_const_asn_ref;
3196 n_baseclasses = TREE_VEC_LENGTH (BINFO_BASETYPES (t_binfo));
3197 aggregate = 0;
3199 else
3201 first_vfn_base_index = -1;
3202 has_virtual = 0;
3203 max_has_virtual = has_virtual;
3204 vfield = NULL_TREE;
3205 vfields = NULL_TREE;
3206 CLASSTYPE_RTTI (t) = NULL_TREE;
3207 last_x = NULL_TREE;
3208 cant_have_default_ctor = 0;
3209 cant_have_const_ctor = 0;
3210 no_const_asn_ref = 0;
3213 #if 0
3214 /* Both of these should be done before now. */
3215 if (write_virtuals == 3 && CLASSTYPE_INTERFACE_KNOWN (t)
3216 && ! IS_SIGNATURE (t))
3218 my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t) == interface_only, 999);
3219 my_friendly_assert (CLASSTYPE_VTABLE_NEEDS_WRITING (t) == ! interface_only, 999);
3221 #endif
3223 /* The three of these are approximations which may later be
3224 modified. Needed at this point to make add_virtual_function
3225 and modify_vtable_entries work. */
3226 TREE_CHAIN (t_binfo) = TYPE_BINFO (t);
3227 TYPE_BINFO (t) = t_binfo;
3228 CLASSTYPE_VFIELDS (t) = vfields;
3229 CLASSTYPE_VFIELD (t) = vfield;
3231 if (IS_SIGNATURE (t))
3232 all_virtual = 0;
3233 else if (flag_all_virtual == 1)
3234 all_virtual = 1;
3235 else
3236 all_virtual = 0;
3238 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3240 GNU_xref_member (current_class_name, x);
3242 nonprivate_method |= ! TREE_PRIVATE (x);
3244 /* If this was an evil function, don't keep it in class. */
3245 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3246 continue;
3248 DECL_CLASS_CONTEXT (x) = t;
3250 /* Do both of these, even though they're in the same union;
3251 if the insn `r' member and the size `i' member are
3252 different sizes, as on the alpha, the larger of the two
3253 will end up with garbage in it. */
3254 DECL_SAVED_INSNS (x) = NULL_RTX;
3255 DECL_FIELD_SIZE (x) = 0;
3257 check_for_override (x, t);
3258 if (DECL_ABSTRACT_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3259 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3261 /* The name of the field is the original field name
3262 Save this in auxiliary field for later overloading. */
3263 if (DECL_VINDEX (x)
3264 || (all_virtual == 1 && ! DECL_CONSTRUCTOR_P (x)))
3266 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3267 &has_virtual, x, t);
3268 if (DECL_ABSTRACT_VIRTUAL_P (x))
3269 abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
3270 #if 0
3271 /* XXX Why did I comment this out? (jason) */
3272 else
3273 TREE_USED (x) = 1;
3274 #endif
3278 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
3280 GNU_xref_member (current_class_name, x);
3282 if (TREE_CODE (x) == FIELD_DECL)
3284 DECL_PACKED (x) |= TYPE_PACKED (t);
3285 empty = 0;
3288 /* Handle access declarations. */
3289 if (TREE_CODE (x) == USING_DECL)
3291 tree ctype = DECL_INITIAL (x);
3292 tree sname = DECL_NAME (x);
3293 tree access
3294 = TREE_PRIVATE (x) ? access_private_node
3295 : TREE_PROTECTED (x) ? access_protected_node
3296 : access_public_node;
3297 tree fdecl, binfo;
3299 if (last_x)
3300 TREE_CHAIN (last_x) = TREE_CHAIN (x);
3301 else
3302 fields = TREE_CHAIN (x);
3304 binfo = binfo_or_else (ctype, t);
3305 if (! binfo)
3306 continue;
3308 if (sname == constructor_name (ctype)
3309 || sname == constructor_name_full (ctype))
3310 cp_error_at ("using-declaration for constructor", x);
3312 fdecl = lookup_field (binfo, sname, 0, 0);
3313 if (! fdecl)
3314 fdecl = lookup_fnfields (binfo, sname, 0);
3316 if (fdecl)
3317 access_decls = scratch_tree_cons (access, fdecl, access_decls);
3318 else
3319 cp_error_at ("no members matching `%D' in `%#T'", x, ctype);
3320 continue;
3323 last_x = x;
3325 if (TREE_CODE (x) == TYPE_DECL)
3326 continue;
3328 /* If we've gotten this far, it's a data member, possibly static,
3329 or an enumerator. */
3331 DECL_FIELD_CONTEXT (x) = t;
3333 /* ``A local class cannot have static data members.'' ARM 9.4 */
3334 if (current_function_decl && TREE_STATIC (x))
3335 cp_error_at ("field `%D' in local class cannot be static", x);
3337 /* Perform error checking that did not get done in
3338 grokdeclarator. */
3339 if (TREE_CODE (TREE_TYPE (x)) == FUNCTION_TYPE)
3341 cp_error_at ("field `%D' invalidly declared function type",
3343 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3345 else if (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE)
3347 cp_error_at ("field `%D' invalidly declared method type", x);
3348 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3350 else if (TREE_CODE (TREE_TYPE (x)) == OFFSET_TYPE)
3352 cp_error_at ("field `%D' invalidly declared offset type", x);
3353 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3356 #if 0
3357 if (DECL_NAME (x) == constructor_name (t))
3358 cant_have_default_ctor = 1;
3359 #endif
3361 if (TREE_TYPE (x) == error_mark_node)
3362 continue;
3364 DECL_SAVED_INSNS (x) = NULL_RTX;
3365 DECL_FIELD_SIZE (x) = 0;
3367 /* When this goes into scope, it will be a non-local reference. */
3368 DECL_NONLOCAL (x) = 1;
3370 if (TREE_CODE (x) == CONST_DECL)
3371 continue;
3373 if (TREE_CODE (x) == VAR_DECL)
3375 if (TREE_CODE (t) == UNION_TYPE)
3376 /* Unions cannot have static members. */
3377 cp_error_at ("field `%D' declared static in union", x);
3379 continue;
3382 /* Now it can only be a FIELD_DECL. */
3384 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3385 aggregate = 0;
3387 /* If this is of reference type, check if it needs an init.
3388 Also do a little ANSI jig if necessary. */
3389 if (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE)
3391 if (DECL_INITIAL (x) == NULL_TREE)
3392 ref_sans_init = 1;
3394 /* ARM $12.6.2: [A member initializer list] (or, for an
3395 aggregate, initialization by a brace-enclosed list) is the
3396 only way to initialize nonstatic const and reference
3397 members. */
3398 cant_have_default_ctor = 1;
3399 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3401 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3403 if (DECL_NAME (x))
3404 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3405 else
3406 cp_warning_at ("non-static reference in class without a constructor", x);
3410 if (TREE_CODE (TREE_TYPE (x)) == POINTER_TYPE)
3411 has_pointers = 1;
3413 /* If any field is const, the structure type is pseudo-const. */
3414 if (TREE_READONLY (x))
3416 C_TYPE_FIELDS_READONLY (t) = 1;
3417 if (DECL_INITIAL (x) == NULL_TREE)
3418 const_sans_init = 1;
3420 /* ARM $12.6.2: [A member initializer list] (or, for an
3421 aggregate, initialization by a brace-enclosed list) is the
3422 only way to initialize nonstatic const and reference
3423 members. */
3424 cant_have_default_ctor = 1;
3425 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3427 if (! TYPE_HAS_CONSTRUCTOR (t) && !IS_SIGNATURE (t)
3428 && extra_warnings)
3430 if (DECL_NAME (x))
3431 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3432 else
3433 cp_warning_at ("non-static const member in class without a constructor", x);
3436 else
3438 /* A field that is pseudo-const makes the structure
3439 likewise. */
3440 tree t1 = TREE_TYPE (x);
3441 while (TREE_CODE (t1) == ARRAY_TYPE)
3442 t1 = TREE_TYPE (t1);
3443 if (IS_AGGR_TYPE (t1))
3445 if (C_TYPE_FIELDS_READONLY (t1))
3446 C_TYPE_FIELDS_READONLY (t) = 1;
3447 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1))
3448 const_sans_init = 1;
3452 /* We set DECL_BIT_FIELD tentatively in grokbitfield.
3453 If the type and width are valid, we'll keep it set.
3454 Otherwise, the flag is cleared. */
3455 if (DECL_BIT_FIELD (x))
3457 DECL_BIT_FIELD (x) = 0;
3458 /* Invalid bit-field size done by grokfield. */
3459 /* Detect invalid bit-field type. */
3460 if (DECL_INITIAL (x)
3461 && ! INTEGRAL_TYPE_P (TREE_TYPE (x)))
3463 cp_error_at ("bit-field `%#D' with non-integral type", x);
3464 DECL_INITIAL (x) = NULL;
3467 /* Detect and ignore out of range field width. */
3468 if (DECL_INITIAL (x))
3470 tree w = DECL_INITIAL (x);
3471 register int width = 0;
3473 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3474 STRIP_NOPS (w);
3476 /* detect invalid field size. */
3477 if (TREE_CODE (w) == CONST_DECL)
3478 w = DECL_INITIAL (w);
3479 else if (TREE_READONLY_DECL_P (w))
3480 w = decl_constant_value (w);
3482 if (TREE_CODE (w) != INTEGER_CST)
3484 cp_error_at ("bit-field `%D' width not an integer constant",
3486 DECL_INITIAL (x) = NULL_TREE;
3488 else if (width = TREE_INT_CST_LOW (w),
3489 width < 0)
3491 DECL_INITIAL (x) = NULL;
3492 cp_error_at ("negative width in bit-field `%D'", x);
3494 else if (width == 0 && DECL_NAME (x) != 0)
3496 DECL_INITIAL (x) = NULL;
3497 cp_error_at ("zero width for bit-field `%D'", x);
3499 else if (width
3500 > TYPE_PRECISION (long_long_unsigned_type_node))
3502 /* The backend will dump if you try to use something
3503 too big; avoid that. */
3504 DECL_INITIAL (x) = NULL;
3505 sorry ("bit-fields larger than %d bits",
3506 TYPE_PRECISION (long_long_unsigned_type_node));
3507 cp_error_at (" in declaration of `%D'", x);
3509 else if (width > TYPE_PRECISION (TREE_TYPE (x))
3510 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
3512 cp_warning_at ("width of `%D' exceeds its type", x);
3514 else if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
3515 && ((min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
3516 TREE_UNSIGNED (TREE_TYPE (x))) > width)
3517 || (min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
3518 TREE_UNSIGNED (TREE_TYPE (x))) > width)))
3520 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3521 x, TREE_TYPE (x));
3524 if (DECL_INITIAL (x) == NULL_TREE)
3526 else if (width == 0)
3528 #ifdef EMPTY_FIELD_BOUNDARY
3529 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), EMPTY_FIELD_BOUNDARY);
3530 #endif
3531 #ifdef PCC_BITFIELD_TYPE_MATTERS
3532 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3533 TYPE_ALIGN (TREE_TYPE (x)));
3534 #endif
3536 else
3538 DECL_INITIAL (x) = NULL_TREE;
3539 DECL_FIELD_SIZE (x) = width;
3540 DECL_BIT_FIELD (x) = 1;
3543 else
3544 /* Non-bit-fields are aligned for their type. */
3545 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x)));
3547 else
3549 tree type = TREE_TYPE (x);
3551 while (TREE_CODE (type) == ARRAY_TYPE)
3552 type = TREE_TYPE (type);
3554 if (TYPE_LANG_SPECIFIC (type) && ! ANON_UNION_P (x)
3555 && ! TYPE_PTRMEMFUNC_P (type))
3557 /* Never let anything with uninheritable virtuals
3558 make it through without complaint. */
3559 if (CLASSTYPE_ABSTRACT_VIRTUALS (type))
3560 abstract_virtuals_error (x, type);
3562 /* Don't let signatures make it through either. */
3563 if (IS_SIGNATURE (type))
3564 signature_error (x, type);
3566 if (code == UNION_TYPE)
3568 char *fie = NULL;
3569 if (TYPE_NEEDS_CONSTRUCTING (type))
3570 fie = "constructor";
3571 else if (TYPE_NEEDS_DESTRUCTOR (type))
3572 fie = "destructor";
3573 else if (TYPE_HAS_REAL_ASSIGNMENT (type))
3574 fie = "assignment operator";
3575 if (fie)
3576 cp_error_at ("member `%#D' with %s not allowed in union", x,
3577 fie);
3579 else
3581 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3582 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3583 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3584 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3587 if (!TYPE_HAS_CONST_INIT_REF (type))
3588 cant_have_const_ctor = 1;
3590 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3591 no_const_asn_ref = 1;
3593 if (TYPE_HAS_CONSTRUCTOR (type)
3594 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3596 cant_have_default_ctor = 1;
3597 #if 0
3598 /* This is wrong for aggregates. */
3599 if (! TYPE_HAS_CONSTRUCTOR (t))
3601 if (DECL_NAME (x))
3602 cp_pedwarn_at ("member `%#D' with only non-default constructor", x);
3603 else
3604 cp_pedwarn_at ("member with only non-default constructor", x);
3605 cp_pedwarn_at ("in class without a constructor",
3608 #endif
3611 if (DECL_INITIAL (x) != NULL_TREE)
3613 /* `build_class_init_list' does not recognize
3614 non-FIELD_DECLs. */
3615 if (code == UNION_TYPE && any_default_members != 0)
3616 cp_error_at ("multiple fields in union `%T' initialized");
3617 any_default_members = 1;
3622 /* If this type has any constant members which did not come
3623 with their own initialization, mark that fact here. It is
3624 not an error here, since such types can be saved either by their
3625 constructors, or by fortuitous initialization. */
3626 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
3627 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
3628 CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
3630 /* Synthesize any needed methods. Note that methods will be synthesized
3631 for anonymous unions; grok_x_components undoes that. */
3633 if (! fn_fields)
3634 nonprivate_method = 1;
3636 if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t)
3637 && !IS_SIGNATURE (t))
3639 /* Here we must cons up a destructor on the fly. */
3640 tree dtor = cons_up_default_function (t, name, 0);
3641 check_for_override (dtor, t);
3643 /* If we couldn't make it work, then pretend we didn't need it. */
3644 if (dtor == void_type_node)
3645 TYPE_NEEDS_DESTRUCTOR (t) = 0;
3646 else
3648 /* Link dtor onto end of fn_fields. */
3650 TREE_CHAIN (dtor) = fn_fields;
3651 fn_fields = dtor;
3653 if (DECL_VINDEX (dtor))
3654 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3655 &has_virtual, dtor, t);
3656 nonprivate_method = 1;
3660 /* Effective C++ rule 11. */
3661 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3662 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3664 cp_warning ("`%#T' has pointer data members", t);
3666 if (! TYPE_HAS_INIT_REF (t))
3668 cp_warning (" but does not override `%T(const %T&)'", t, t);
3669 if (! TYPE_HAS_ASSIGN_REF (t))
3670 cp_warning (" or `operator=(const %T&)'", t);
3672 else if (! TYPE_HAS_ASSIGN_REF (t))
3673 cp_warning (" but does not override `operator=(const %T&)'", t);
3676 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3678 TYPE_HAS_COMPLEX_INIT_REF (t)
3679 |= (TYPE_HAS_INIT_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3680 || has_virtual || any_default_members);
3681 TYPE_NEEDS_CONSTRUCTING (t)
3682 |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3683 || has_virtual || any_default_members);
3684 if (! IS_SIGNATURE (t))
3685 CLASSTYPE_NON_AGGREGATE (t)
3686 = ! aggregate || has_virtual || TYPE_HAS_CONSTRUCTOR (t);
3688 /* ARM $12.1: A default constructor will be generated for a class X
3689 only if no constructor has been declared for class X. So we
3690 check TYPE_HAS_CONSTRUCTOR also, to make sure we don't generate
3691 one if they declared a constructor in this class. */
3692 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor
3693 && ! IS_SIGNATURE (t))
3695 tree default_fn = cons_up_default_function (t, name, 2);
3696 TREE_CHAIN (default_fn) = fn_fields;
3697 fn_fields = default_fn;
3700 /* Create default copy constructor, if needed. */
3701 if (! TYPE_HAS_INIT_REF (t) && ! IS_SIGNATURE (t))
3703 /* ARM 12.18: You get either X(X&) or X(const X&), but
3704 not both. --Chip */
3705 tree default_fn = cons_up_default_function (t, name,
3706 3 + cant_have_const_ctor);
3707 TREE_CHAIN (default_fn) = fn_fields;
3708 fn_fields = default_fn;
3711 TYPE_HAS_REAL_ASSIGNMENT (t) |= TYPE_HAS_ASSIGNMENT (t);
3712 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
3713 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
3714 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
3716 if (! TYPE_HAS_ASSIGN_REF (t) && ! IS_SIGNATURE (t))
3718 tree default_fn = cons_up_default_function (t, name,
3719 5 + no_const_asn_ref);
3720 TREE_CHAIN (default_fn) = fn_fields;
3721 fn_fields = default_fn;
3724 if (fn_fields)
3726 TYPE_METHODS (t) = fn_fields;
3727 method_vec = finish_struct_methods (t, fn_fields, nonprivate_method);
3729 if (TYPE_HAS_CONSTRUCTOR (t)
3730 && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
3731 && DECL_FRIENDLIST (TYPE_MAIN_DECL (t)) == NULL_TREE)
3733 int nonprivate_ctor = 0;
3734 tree ctor;
3736 for (ctor = TREE_VEC_ELT (method_vec, 0);
3737 ctor;
3738 ctor = DECL_CHAIN (ctor))
3739 if (! TREE_PRIVATE (ctor))
3741 nonprivate_ctor = 1;
3742 break;
3745 if (nonprivate_ctor == 0 && warn_ctor_dtor_privacy)
3746 cp_warning ("`%#T' only defines private constructors and has no friends",
3750 else
3752 method_vec = 0;
3754 /* Just in case these got accidentally
3755 filled in by syntax errors. */
3756 TYPE_HAS_CONSTRUCTOR (t) = 0;
3757 TYPE_HAS_DESTRUCTOR (t) = 0;
3761 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3763 for (access_decls = nreverse (access_decls); access_decls;
3764 access_decls = TREE_CHAIN (access_decls))
3766 tree fdecl = TREE_VALUE (access_decls);
3767 tree flist = NULL_TREE;
3768 tree name;
3769 tree access = TREE_PURPOSE (access_decls);
3770 int i = 2;
3771 tree tmp;
3773 if (TREE_CODE (fdecl) == TREE_LIST)
3775 flist = fdecl;
3776 fdecl = TREE_VALUE (flist);
3779 name = DECL_NAME (fdecl);
3781 for (; i < n_methods; i++)
3782 if (DECL_NAME (TREE_VEC_ELT (method_vec, i)) == name)
3784 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
3785 cp_error_at (" because of local method `%#D' with same name",
3786 TREE_VEC_ELT (method_vec, i));
3787 fdecl = NULL_TREE;
3788 break;
3791 if (! fdecl)
3792 continue;
3794 for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
3795 if (DECL_NAME (tmp) == name)
3797 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
3798 cp_error_at (" because of local field `%#D' with same name", tmp);
3799 fdecl = NULL_TREE;
3800 break;
3803 if (!fdecl)
3804 continue;
3806 /* Make type T see field decl FDECL with access ACCESS.*/
3807 if (flist)
3809 fdecl = TREE_VALUE (flist);
3810 while (fdecl)
3812 if (alter_access (t, fdecl, access) == 0)
3813 break;
3814 fdecl = DECL_CHAIN (fdecl);
3817 else
3818 alter_access (t, fdecl, access);
3823 if (vfield == NULL_TREE && has_virtual)
3825 /* We build this decl with ptr_type_node, and
3826 change the type when we know what it should be. */
3827 vfield = build_lang_field_decl (FIELD_DECL, get_vfield_name (t),
3828 ptr_type_node);
3829 /* If you change any of the below, take a look at all the
3830 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
3831 them too. */
3832 DECL_ASSEMBLER_NAME (vfield) = get_identifier (VFIELD_BASE);
3833 CLASSTYPE_VFIELD (t) = vfield;
3834 DECL_VIRTUAL_P (vfield) = 1;
3835 DECL_ARTIFICIAL (vfield) = 1;
3836 DECL_FIELD_CONTEXT (vfield) = t;
3837 DECL_CLASS_CONTEXT (vfield) = t;
3838 DECL_FCONTEXT (vfield) = t;
3839 DECL_SAVED_INSNS (vfield) = NULL_RTX;
3840 DECL_FIELD_SIZE (vfield) = 0;
3841 DECL_ALIGN (vfield) = TYPE_ALIGN (ptr_type_node);
3842 #if 0
3843 /* This is more efficient, but breaks binary compatibility, turn
3844 it on sometime when we don't care. If we turn it on, we also
3845 have to enable the code in dfs_init_vbase_pointers. */
3846 /* vfield is always first entry in structure. */
3847 TREE_CHAIN (vfield) = fields;
3848 fields = vfield;
3849 #else
3850 if (last_x)
3852 my_friendly_assert (TREE_CHAIN (last_x) == NULL_TREE, 175);
3853 TREE_CHAIN (last_x) = vfield;
3854 last_x = vfield;
3856 else
3857 fields = vfield;
3858 #endif
3859 empty = 0;
3860 vfields = chainon (vfields, CLASSTYPE_AS_LIST (t));
3863 /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
3864 And they have already done their work.
3866 C++: maybe we will support default field initialization some day... */
3868 /* Delete all zero-width bit-fields from the front of the fieldlist */
3869 while (fields && DECL_BIT_FIELD (fields)
3870 && DECL_INITIAL (fields))
3871 fields = TREE_CHAIN (fields);
3872 /* Delete all such fields from the rest of the fields. */
3873 for (x = fields; x;)
3875 if (TREE_CHAIN (x) && DECL_BIT_FIELD (TREE_CHAIN (x))
3876 && DECL_INITIAL (TREE_CHAIN (x)))
3877 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
3878 else
3879 x = TREE_CHAIN (x);
3881 /* Delete all duplicate fields from the fields */
3882 delete_duplicate_fields (fields);
3884 /* Catch function/field name conflict. We don't need to do this for a
3885 signature, since it can only contain the fields constructed in
3886 append_signature_fields. */
3887 if (! IS_SIGNATURE (t))
3889 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3890 for (x = fields; x; x = TREE_CHAIN (x))
3892 tree name = DECL_NAME (x);
3893 int i = 2;
3895 if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))
3896 continue;
3898 for (; i < n_methods; ++i)
3899 if (DECL_NAME (TREE_VEC_ELT (method_vec, i)) == name)
3901 cp_error_at ("data member `%#D' conflicts with", x);
3902 cp_error_at ("function member `%#D'",
3903 TREE_VEC_ELT (method_vec, i));
3904 break;
3909 /* Now we have the final fieldlist for the data fields. Record it,
3910 then lay out the structure or union (including the fields). */
3912 TYPE_FIELDS (t) = fields;
3914 /* Pass layout information about base classes to layout_type, if any. */
3915 if (n_baseclasses)
3917 tree pseudo_basetype = TREE_TYPE (base_layout_decl);
3919 TREE_CHAIN (base_layout_decl) = TYPE_FIELDS (t);
3920 TYPE_FIELDS (t) = base_layout_decl;
3922 TYPE_SIZE (pseudo_basetype) = CLASSTYPE_SIZE (t);
3923 TYPE_MODE (pseudo_basetype) = TYPE_MODE (t);
3924 TYPE_ALIGN (pseudo_basetype) = CLASSTYPE_ALIGN (t);
3925 DECL_ALIGN (base_layout_decl) = TYPE_ALIGN (pseudo_basetype);
3926 /* Don't re-use old size. */
3927 DECL_SIZE (base_layout_decl) = NULL_TREE;
3929 else if (empty)
3931 /* C++: do not let empty structures exist. */
3932 tree decl = build_lang_field_decl
3933 (FIELD_DECL, NULL_TREE, char_type_node);
3934 TREE_CHAIN (decl) = TYPE_FIELDS (t);
3935 TYPE_FIELDS (t) = decl;
3938 layout_type (t);
3940 finish_struct_anon (t);
3942 if (n_baseclasses || empty)
3943 TYPE_FIELDS (t) = TREE_CHAIN (TYPE_FIELDS (t));
3945 /* Set the TYPE_DECL for this type to contain the right
3946 value for DECL_OFFSET, so that we can use it as part
3947 of a COMPONENT_REF for multiple inheritance. */
3949 layout_decl (TYPE_MAIN_DECL (t), 0);
3951 /* Now fix up any virtual base class types that we left lying
3952 around. We must get these done before we try to lay out the
3953 virtual function table. */
3954 pending_hard_virtuals = nreverse (pending_hard_virtuals);
3956 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
3958 tree vbases;
3960 max_has_virtual = layout_vbasetypes (t, max_has_virtual);
3961 vbases = CLASSTYPE_VBASECLASSES (t);
3962 CLASSTYPE_N_VBASECLASSES (t) = list_length (vbases);
3965 /* Now fixup overrides of all functions in vtables from all
3966 direct or indirect virtual base classes. */
3967 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3968 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3970 for (i = 0; i < n_baseclasses; i++)
3972 tree base_binfo = TREE_VEC_ELT (binfos, i);
3973 tree basetype = BINFO_TYPE (base_binfo);
3974 tree vbases;
3976 vbases = CLASSTYPE_VBASECLASSES (basetype);
3977 while (vbases)
3979 merge_overrides (binfo_member (BINFO_TYPE (vbases),
3980 CLASSTYPE_VBASECLASSES (t)),
3981 vbases, 1, t);
3982 vbases = TREE_CHAIN (vbases);
3988 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
3989 might need to know it for setting up the offsets in the vtable
3990 (or in thunks) below. */
3991 if (vfield != NULL_TREE
3992 && DECL_FIELD_CONTEXT (vfield) != t)
3994 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
3995 tree offset = BINFO_OFFSET (binfo);
3997 vfield = copy_node (vfield);
3998 copy_lang_decl (vfield);
4000 if (! integer_zerop (offset))
4001 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4002 DECL_FIELD_CONTEXT (vfield) = t;
4003 DECL_CLASS_CONTEXT (vfield) = t;
4004 DECL_FIELD_BITPOS (vfield)
4005 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4006 CLASSTYPE_VFIELD (t) = vfield;
4009 #ifdef NOTQUITE
4010 cp_warning ("Doing hard virtuals for %T...", t);
4011 #endif
4013 if (has_virtual > max_has_virtual)
4014 max_has_virtual = has_virtual;
4015 if (max_has_virtual > 0)
4016 TYPE_VIRTUAL_P (t) = 1;
4018 if (flag_rtti && TYPE_VIRTUAL_P (t) && !pending_hard_virtuals)
4019 modify_all_vtables (t, NULL_TREE, NULL_TREE);
4021 while (pending_hard_virtuals)
4023 modify_all_vtables (t,
4024 TREE_PURPOSE (pending_hard_virtuals),
4025 TREE_VALUE (pending_hard_virtuals));
4026 pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
4029 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
4031 tree vbases;
4032 /* Now fixup any virtual function entries from virtual bases
4033 that have different deltas. This has to come after we do the
4034 pending hard virtuals, as we might have a function that comes
4035 from multiple virtual base instances that is only overridden
4036 by a hard virtual above. */
4037 vbases = CLASSTYPE_VBASECLASSES (t);
4038 while (vbases)
4040 /* We might be able to shorten the amount of work we do by
4041 only doing this for vtables that come from virtual bases
4042 that have differing offsets, but don't want to miss any
4043 entries. */
4044 fixup_vtable_deltas (vbases, 1, t);
4045 vbases = TREE_CHAIN (vbases);
4049 /* Under our model of GC, every C++ class gets its own virtual
4050 function table, at least virtually. */
4051 if (pending_virtuals)
4053 pending_virtuals = nreverse (pending_virtuals);
4054 /* We must enter these virtuals into the table. */
4055 if (first_vfn_base_index < 0)
4057 /* The second slot is for the tdesc pointer when thunks are used. */
4058 if (flag_vtable_thunks)
4059 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4061 /* The first slot is for the rtti offset. */
4062 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4064 set_rtti_entry (pending_virtuals, size_zero_node, t);
4065 build_vtable (NULL_TREE, t);
4067 else
4069 /* Here we know enough to change the type of our virtual
4070 function table, but we will wait until later this function. */
4072 if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
4073 build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index), t);
4076 /* If this type has basetypes with constructors, then those
4077 constructors might clobber the virtual function table. But
4078 they don't if the derived class shares the exact vtable of the base
4079 class. */
4081 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4083 else if (first_vfn_base_index >= 0)
4085 tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index);
4086 /* This class contributes nothing new to the virtual function
4087 table. However, it may have declared functions which
4088 went into the virtual function table "inherited" from the
4089 base class. If so, we grab a copy of those updated functions,
4090 and pretend they are ours. */
4092 /* See if we should steal the virtual info from base class. */
4093 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
4094 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
4095 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
4096 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
4097 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
4098 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4101 if (max_has_virtual || first_vfn_base_index >= 0)
4103 CLASSTYPE_VSIZE (t) = has_virtual;
4104 if (first_vfn_base_index >= 0)
4106 if (pending_virtuals)
4107 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t),
4108 pending_virtuals);
4110 else if (has_virtual)
4112 TYPE_BINFO_VIRTUALS (t) = pending_virtuals;
4113 if (write_virtuals >= 0)
4114 DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1;
4118 /* Now lay out the virtual function table. */
4119 if (has_virtual)
4121 tree atype, itype;
4123 if (TREE_TYPE (vfield) == ptr_type_node)
4125 /* We must create a pointer to this table because
4126 the one inherited from base class does not exist.
4127 We will fill in the type when we know what it
4128 should really be. Use `size_int' so values are memoized
4129 in common cases. */
4130 itype = build_index_type (size_int (has_virtual));
4131 atype = build_array_type (vtable_entry_type, itype);
4132 layout_type (atype);
4133 TREE_TYPE (vfield) = build_pointer_type (atype);
4135 else
4137 atype = TREE_TYPE (TREE_TYPE (vfield));
4139 if (has_virtual != TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))))
4141 /* We must extend (or create) the boundaries on this array,
4142 because we picked up virtual functions from multiple
4143 base classes. */
4144 itype = build_index_type (size_int (has_virtual));
4145 atype = build_array_type (vtable_entry_type, itype);
4146 layout_type (atype);
4147 vfield = copy_node (vfield);
4148 TREE_TYPE (vfield) = build_pointer_type (atype);
4152 CLASSTYPE_VFIELD (t) = vfield;
4153 if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
4155 TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
4156 DECL_SIZE (TYPE_BINFO_VTABLE (t)) = 0;
4157 layout_decl (TYPE_BINFO_VTABLE (t), 0);
4158 /* At one time the vtable info was grabbed 2 words at a time. This
4159 fails on sparc unless you have 8-byte alignment. (tiemann) */
4160 DECL_ALIGN (TYPE_BINFO_VTABLE (t))
4161 = MAX (TYPE_ALIGN (double_type_node),
4162 DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
4165 else if (first_vfn_base_index >= 0)
4166 CLASSTYPE_VFIELD (t) = vfield;
4167 CLASSTYPE_VFIELDS (t) = vfields;
4169 finish_struct_bits (t, max_has_virtual);
4171 /* Complete the rtl for any static member objects of the type we're
4172 working on. */
4173 for (x = fields; x; x = TREE_CHAIN (x))
4175 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4176 && TREE_TYPE (x) == t)
4178 DECL_MODE (x) = TYPE_MODE (t);
4179 make_decl_rtl (x, NULL, 0);
4183 if (TYPE_HAS_CONSTRUCTOR (t))
4185 tree vfields = CLASSTYPE_VFIELDS (t);
4187 while (vfields)
4189 /* Mark the fact that constructor for T
4190 could affect anybody inheriting from T
4191 who wants to initialize vtables for VFIELDS's type. */
4192 if (VF_DERIVED_VALUE (vfields))
4193 TREE_ADDRESSABLE (vfields) = 1;
4194 vfields = TREE_CHAIN (vfields);
4196 if (any_default_members != 0)
4197 build_class_init_list (t);
4199 else if (TYPE_NEEDS_CONSTRUCTING (t))
4200 build_class_init_list (t);
4202 /* Write out inline function definitions. */
4203 do_inline_function_hair (t, CLASSTYPE_INLINE_FRIENDS (t));
4204 CLASSTYPE_INLINE_FRIENDS (t) = 0;
4206 if (CLASSTYPE_VSIZE (t) != 0)
4208 #if 0
4209 /* This is now done above. */
4210 if (DECL_FIELD_CONTEXT (vfield) != t)
4212 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4213 tree offset = BINFO_OFFSET (binfo);
4215 vfield = copy_node (vfield);
4216 copy_lang_decl (vfield);
4218 if (! integer_zerop (offset))
4219 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4220 DECL_FIELD_CONTEXT (vfield) = t;
4221 DECL_CLASS_CONTEXT (vfield) = t;
4222 DECL_FIELD_BITPOS (vfield)
4223 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4224 CLASSTYPE_VFIELD (t) = vfield;
4226 #endif
4228 /* In addition to this one, all the other vfields should be listed. */
4229 /* Before that can be done, we have to have FIELD_DECLs for them, and
4230 a place to find them. */
4231 TYPE_NONCOPIED_PARTS (t) = build_tree_list (default_conversion (TYPE_BINFO_VTABLE (t)), vfield);
4233 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
4234 && DECL_VINDEX (TREE_VEC_ELT (method_vec, 1)) == NULL_TREE)
4235 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
4239 /* Make the rtl for any new vtables we have created, and unmark
4240 the base types we marked. */
4241 finish_vtbls (TYPE_BINFO (t), 1, t);
4242 hack_incomplete_structures (t);
4244 #if 0
4245 if (TYPE_NAME (t) && TYPE_IDENTIFIER (t))
4246 undo_template_name_overload (TYPE_IDENTIFIER (t), 1);
4247 #endif
4249 resume_momentary (old);
4251 if (warn_overloaded_virtual)
4252 warn_hidden (t);
4254 #if 0
4255 /* This has to be done after we have sorted out what to do with
4256 the enclosing type. */
4257 if (write_symbols != DWARF_DEBUG)
4259 /* Be smarter about nested classes here. If a type is nested,
4260 only output it if we would output the enclosing type. */
4261 if (DECL_CONTEXT (TYPE_MAIN_DECL (t))
4262 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (TYPE_MAIN_DECL (t)))) == 't')
4263 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = TREE_ASM_WRITTEN (TYPE_MAIN_DECL (t));
4265 #endif
4267 if (write_symbols != DWARF_DEBUG && write_symbols != DWARF2_DEBUG)
4269 /* If the type has methods, we want to think about cutting down
4270 the amount of symbol table stuff we output. The value stored in
4271 the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
4272 For example, if a member function is seen and we decide to
4273 write out that member function, then we can change the value
4274 of the DECL_IGNORED_P slot, and the type will be output when
4275 that member function's debug info is written out.
4277 We can't do this with DWARF, which does not support name
4278 references between translation units. */
4279 if (CLASSTYPE_METHOD_VEC (t))
4281 extern tree pending_vtables;
4283 /* Don't output full info about any type
4284 which does not have its implementation defined here. */
4285 if (TYPE_VIRTUAL_P (t) && write_virtuals == 2)
4286 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t))
4287 = (value_member (TYPE_IDENTIFIER (t), pending_vtables) == 0);
4288 else if (CLASSTYPE_INTERFACE_ONLY (t))
4289 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4290 #if 0
4291 /* XXX do something about this. */
4292 else if (CLASSTYPE_INTERFACE_UNKNOWN (t))
4293 /* Only a first approximation! */
4294 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4295 #endif
4297 else if (CLASSTYPE_INTERFACE_ONLY (t))
4298 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4301 /* Finish debugging output for this type. */
4302 rest_of_type_compilation (t, toplevel_bindings_p ());
4304 return t;
4307 tree
4308 finish_struct (t, list_of_fieldlists, attributes, warn_anon)
4309 tree t, list_of_fieldlists, attributes;
4310 int warn_anon;
4312 tree fields = NULL_TREE;
4313 tree *tail = &TYPE_METHODS (t);
4314 tree specializations = NULL_TREE;
4315 tree *specialization_tail = &specializations;
4316 tree name = TYPE_NAME (t);
4317 tree x, last_x = NULL_TREE;
4318 tree access;
4319 tree dummy = NULL_TREE;
4320 tree next_x = NULL_TREE;
4322 if (TREE_CODE (name) == TYPE_DECL)
4324 extern int lineno;
4326 DECL_SOURCE_FILE (name) = input_filename;
4327 /* For TYPE_DECL that are not typedefs (those marked with a line
4328 number of zero, we don't want to mark them as real typedefs.
4329 If this fails one needs to make sure real typedefs have a
4330 previous line number, even if it is wrong, that way the below
4331 will fill in the right line number. (mrs) */
4332 if (DECL_SOURCE_LINE (name))
4333 DECL_SOURCE_LINE (name) = lineno;
4334 CLASSTYPE_SOURCE_LINE (t) = lineno;
4335 name = DECL_NAME (name);
4338 /* Append the fields we need for constructing signature tables. */
4339 if (IS_SIGNATURE (t))
4340 append_signature_fields (list_of_fieldlists);
4342 /* Move our self-reference declaration to the end of the field list so
4343 any real field with the same name takes precedence. */
4344 if (list_of_fieldlists
4345 && TREE_VALUE (list_of_fieldlists)
4346 && DECL_ARTIFICIAL (TREE_VALUE (list_of_fieldlists)))
4348 dummy = TREE_VALUE (list_of_fieldlists);
4349 list_of_fieldlists = TREE_CHAIN (list_of_fieldlists);
4352 if (last_x && list_of_fieldlists)
4353 TREE_CHAIN (last_x) = TREE_VALUE (list_of_fieldlists);
4355 while (list_of_fieldlists)
4357 access = TREE_PURPOSE (list_of_fieldlists);
4359 /* For signatures, we made all methods `public' in the parser and
4360 reported an error if a access specifier was used. */
4361 if (access == access_default_node)
4363 if (CLASSTYPE_DECLARED_CLASS (t) == 0)
4364 access = access_public_node;
4365 else
4366 access = access_private_node;
4369 for (x = TREE_VALUE (list_of_fieldlists); x; x = next_x)
4371 next_x = TREE_CHAIN (x);
4373 TREE_PRIVATE (x) = access == access_private_node;
4374 TREE_PROTECTED (x) = access == access_protected_node;
4376 /* Check for inconsistent use of this name in the class body.
4377 Enums, types and static vars have already been checked. */
4378 if (TREE_CODE (x) != TYPE_DECL && TREE_CODE (x) != USING_DECL
4379 && ! (TREE_CODE (x) == TEMPLATE_DECL
4380 && TREE_CODE (DECL_RESULT (x)) == TYPE_DECL)
4381 && TREE_CODE (x) != CONST_DECL && TREE_CODE (x) != VAR_DECL)
4383 tree name = DECL_NAME (x);
4384 tree icv;
4386 /* Don't get confused by access decls. */
4387 if (name && TREE_CODE (name) == IDENTIFIER_NODE)
4388 icv = IDENTIFIER_CLASS_VALUE (name);
4389 else
4390 icv = NULL_TREE;
4392 if (icv
4393 /* Don't complain about constructors. */
4394 && name != constructor_name (current_class_type)
4395 /* Or inherited names. */
4396 && id_in_current_class (name)
4397 /* Or shadowed tags. */
4398 && !(TREE_CODE (icv) == TYPE_DECL
4399 && DECL_CONTEXT (icv) == t))
4401 cp_error_at ("declaration of identifier `%D' as `%+#D'",
4402 name, x);
4403 cp_error_at ("conflicts with other use in class as `%#D'",
4404 icv);
4408 if (TREE_CODE (x) == FUNCTION_DECL
4409 || DECL_FUNCTION_TEMPLATE_P (x))
4411 DECL_CLASS_CONTEXT (x) = t;
4413 if (last_x)
4414 TREE_CHAIN (last_x) = next_x;
4416 if (DECL_TEMPLATE_SPECIALIZATION (x))
4417 /* We don't enter the specialization into the class
4418 method vector since specializations don't affect
4419 overloading. Instead we keep track of the
4420 specializations, and process them after the method
4421 vector is complete. */
4423 *specialization_tail = x;
4424 specialization_tail = &TREE_CHAIN (x);
4425 TREE_CHAIN (x) = NULL_TREE;
4426 continue;
4429 /* Link x onto end of TYPE_METHODS. */
4430 *tail = x;
4431 tail = &TREE_CHAIN (x);
4432 continue;
4435 if (TREE_CODE (x) != TYPE_DECL)
4436 DECL_FIELD_CONTEXT (x) = t;
4438 if (! fields)
4439 fields = x;
4440 last_x = x;
4442 list_of_fieldlists = TREE_CHAIN (list_of_fieldlists);
4443 /* link the tail while we have it! */
4444 if (last_x)
4446 TREE_CHAIN (last_x) = NULL_TREE;
4448 if (list_of_fieldlists
4449 && TREE_VALUE (list_of_fieldlists)
4450 && TREE_CODE (TREE_VALUE (list_of_fieldlists)) != FUNCTION_DECL)
4451 TREE_CHAIN (last_x) = TREE_VALUE (list_of_fieldlists);
4455 /* Now add the tags, if any, to the list of TYPE_DECLs
4456 defined for this type. */
4457 if (CLASSTYPE_TAGS (t) || dummy)
4459 /* The list of tags was built up in pushtag in reverse order; we need
4460 to fix that so that enumerators will be processed in forward order
4461 in template instantiation. */
4462 CLASSTYPE_TAGS (t) = x = nreverse (CLASSTYPE_TAGS (t));
4463 while (x)
4465 tree tag = TYPE_MAIN_DECL (TREE_VALUE (x));
4467 TREE_NONLOCAL_FLAG (TREE_VALUE (x)) = 0;
4468 x = TREE_CHAIN (x);
4469 last_x = chainon (last_x, tag);
4471 if (dummy)
4472 last_x = chainon (last_x, dummy);
4473 if (fields == NULL_TREE)
4474 fields = last_x;
4475 CLASSTYPE_LOCAL_TYPEDECLS (t) = 1;
4478 *tail = NULL_TREE;
4479 TYPE_FIELDS (t) = fields;
4481 cplus_decl_attributes (t, attributes, NULL_TREE);
4483 if (processing_template_decl)
4485 tree d = getdecls ();
4486 for (; d; d = TREE_CHAIN (d))
4488 /* If this is the decl for the class or one of the template
4489 parms, we've seen all the injected decls. */
4490 if ((TREE_CODE (d) == TYPE_DECL
4491 && (TREE_TYPE (d) == t
4492 || TREE_CODE (TREE_TYPE (d)) == TEMPLATE_TYPE_PARM
4493 || TREE_CODE (TREE_TYPE (d)) == TEMPLATE_TEMPLATE_PARM))
4494 || TREE_CODE (d) == CONST_DECL)
4495 break;
4496 /* Don't inject cache decls. */
4497 else if (IDENTIFIER_TEMPLATE (DECL_NAME (d)))
4498 continue;
4499 DECL_TEMPLATE_INJECT (CLASSTYPE_TI_TEMPLATE (t))
4500 = tree_cons (NULL_TREE, d,
4501 DECL_TEMPLATE_INJECT (CLASSTYPE_TI_TEMPLATE (t)));
4503 CLASSTYPE_METHOD_VEC (t)
4504 = finish_struct_methods (t, TYPE_METHODS (t), 1);
4505 TYPE_SIZE (t) = integer_zero_node;
4507 else
4508 t = finish_struct_1 (t, warn_anon);
4510 TYPE_BEING_DEFINED (t) = 0;
4512 /* Now, figure out which member templates we're specializing. */
4513 for (x = specializations; x != NULL_TREE; x = TREE_CHAIN (x))
4515 tree spec_args;
4516 tree fn;
4517 int pending_specialization;
4519 if (uses_template_parms (t))
4520 /* If t is a template class, and x is a specialization, then x
4521 is itself really a template. Due to the vagaries of the
4522 parser, however, we will have a handle to a function
4523 declaration, rather than the template declaration, at this
4524 point. */
4526 my_friendly_assert (DECL_TEMPLATE_INFO (x) != NULL_TREE, 0);
4527 my_friendly_assert (DECL_TI_TEMPLATE (x) != NULL_TREE, 0);
4528 fn = DECL_TI_TEMPLATE (x);
4530 else
4531 fn = x;
4533 /* We want the specialization arguments, which will be the
4534 innermost ones. */
4535 if (DECL_TI_ARGS (fn) && TREE_CODE (DECL_TI_ARGS (fn)) == TREE_VEC)
4536 spec_args
4537 = TREE_VEC_ELT (DECL_TI_ARGS (fn), 0);
4538 else
4539 spec_args = DECL_TI_ARGS (fn);
4541 pending_specialization
4542 = TI_PENDING_SPECIALIZATION_FLAG (DECL_TEMPLATE_INFO (fn));
4543 check_explicit_specialization
4544 (lookup_template_function (DECL_NAME (fn), spec_args),
4545 fn, 0, 1 | (8 * pending_specialization));
4546 TI_PENDING_SPECIALIZATION_FLAG (DECL_TEMPLATE_INFO (fn)) = 0;
4548 /* Now, the assembler name will be correct for fn, so we
4549 make its RTL. */
4550 DECL_RTL (fn) = 0;
4551 make_decl_rtl (fn, NULL_PTR, 1);
4553 if (x != fn)
4555 DECL_RTL (x) = 0;
4556 make_decl_rtl (x, NULL_PTR, 1);
4560 if (current_class_type)
4561 popclass (0);
4562 else
4563 error ("trying to finish struct, but kicked out due to previous parse errors.");
4565 return t;
4568 /* Return non-zero if the effective type of INSTANCE is static.
4569 Used to determine whether the virtual function table is needed
4570 or not.
4572 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4573 of our knowledge of its type. */
4576 resolves_to_fixed_type_p (instance, nonnull)
4577 tree instance;
4578 int *nonnull;
4580 switch (TREE_CODE (instance))
4582 case INDIRECT_REF:
4583 /* Check that we are not going through a cast of some sort. */
4584 if (TREE_TYPE (instance)
4585 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
4586 instance = TREE_OPERAND (instance, 0);
4587 /* fall through... */
4588 case CALL_EXPR:
4589 /* This is a call to a constructor, hence it's never zero. */
4590 if (TREE_HAS_CONSTRUCTOR (instance))
4592 if (nonnull)
4593 *nonnull = 1;
4594 return 1;
4596 return 0;
4598 case SAVE_EXPR:
4599 /* This is a call to a constructor, hence it's never zero. */
4600 if (TREE_HAS_CONSTRUCTOR (instance))
4602 if (nonnull)
4603 *nonnull = 1;
4604 return 1;
4606 return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4608 case RTL_EXPR:
4609 return 0;
4611 case PLUS_EXPR:
4612 case MINUS_EXPR:
4613 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
4614 /* Propagate nonnull. */
4615 resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4616 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
4617 return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4618 return 0;
4620 case NOP_EXPR:
4621 case CONVERT_EXPR:
4622 return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4624 case ADDR_EXPR:
4625 if (nonnull)
4626 *nonnull = 1;
4627 return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4629 case COMPONENT_REF:
4630 return resolves_to_fixed_type_p (TREE_OPERAND (instance, 1), nonnull);
4632 case VAR_DECL:
4633 case FIELD_DECL:
4634 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
4635 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
4637 if (nonnull)
4638 *nonnull = 1;
4639 return 1;
4641 /* fall through... */
4642 case TARGET_EXPR:
4643 case PARM_DECL:
4644 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
4646 if (nonnull)
4647 *nonnull = 1;
4648 return 1;
4650 else if (nonnull)
4652 if (instance == current_class_ptr
4653 && flag_this_is_variable <= 0)
4655 /* Some people still use `this = 0' inside destructors. */
4656 *nonnull = ! DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (current_function_decl));
4657 /* In a constructor, we know our type. */
4658 if (flag_this_is_variable < 0)
4659 return 1;
4661 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4662 /* Reference variables should be references to objects. */
4663 *nonnull = 1;
4665 return 0;
4667 default:
4668 return 0;
4672 void
4673 init_class_processing ()
4675 current_class_depth = 0;
4676 current_class_stacksize = 10;
4677 current_class_base = (tree *)xmalloc(current_class_stacksize * sizeof (tree));
4678 current_class_stack = current_class_base;
4680 current_lang_stacksize = 10;
4681 current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree));
4682 current_lang_stack = current_lang_base;
4684 access_default_node = build_int_2 (0, 0);
4685 access_public_node = build_int_2 (1, 0);
4686 access_protected_node = build_int_2 (2, 0);
4687 access_private_node = build_int_2 (3, 0);
4688 access_default_virtual_node = build_int_2 (4, 0);
4689 access_public_virtual_node = build_int_2 (5, 0);
4690 access_protected_virtual_node = build_int_2 (6, 0);
4691 access_private_virtual_node = build_int_2 (7, 0);
4693 /* Keep these values lying around. */
4694 base_layout_decl = build_lang_field_decl (FIELD_DECL, NULL_TREE, error_mark_node);
4695 TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE);
4697 gcc_obstack_init (&class_obstack);
4700 /* Set current scope to NAME. CODE tells us if this is a
4701 STRUCT, UNION, or ENUM environment.
4703 NAME may end up being NULL_TREE if this is an anonymous or
4704 late-bound struct (as in "struct { ... } foo;") */
4706 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
4707 appropriate values, found by looking up the type definition of
4708 NAME (as a CODE).
4710 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
4711 which can be seen locally to the class. They are shadowed by
4712 any subsequent local declaration (including parameter names).
4714 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
4715 which have static meaning (i.e., static members, static
4716 member functions, enum declarations, etc).
4718 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
4719 which can be seen locally to the class (as in 1), but
4720 know that we are doing this for declaration purposes
4721 (i.e. friend foo::bar (int)).
4723 So that we may avoid calls to lookup_name, we cache the _TYPE
4724 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
4726 For multiple inheritance, we perform a two-pass depth-first search
4727 of the type lattice. The first pass performs a pre-order search,
4728 marking types after the type has had its fields installed in
4729 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
4730 unmarks the marked types. If a field or member function name
4731 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
4732 that name becomes `error_mark_node'. */
4734 void
4735 pushclass (type, modify)
4736 tree type;
4737 int modify;
4739 push_memoized_context (type, modify);
4741 current_class_depth++;
4742 *current_class_stack++ = current_class_name;
4743 *current_class_stack++ = current_class_type;
4744 if (current_class_stack >= current_class_base + current_class_stacksize)
4746 current_class_base
4747 = (tree *)xrealloc (current_class_base,
4748 sizeof (tree) * (current_class_stacksize + 10));
4749 current_class_stack = current_class_base + current_class_stacksize;
4750 current_class_stacksize += 10;
4753 current_class_name = TYPE_NAME (type);
4754 if (TREE_CODE (current_class_name) == TYPE_DECL)
4755 current_class_name = DECL_NAME (current_class_name);
4756 current_class_type = type;
4758 if (previous_class_type != NULL_TREE
4759 && (type != previous_class_type || TYPE_SIZE (previous_class_type) == NULL_TREE)
4760 && current_class_depth == 1)
4762 /* Forcibly remove any old class remnants. */
4763 popclass (-1);
4764 previous_class_type = NULL_TREE;
4767 pushlevel_class ();
4769 #if 0
4770 if (CLASSTYPE_TEMPLATE_INFO (type))
4771 overload_template_name (type);
4772 #endif
4774 if (modify)
4776 tree tags;
4777 tree this_fndecl = current_function_decl;
4779 if (current_function_decl
4780 && DECL_CONTEXT (current_function_decl)
4781 && TREE_CODE (DECL_CONTEXT (current_function_decl)) == FUNCTION_DECL)
4782 current_function_decl = DECL_CONTEXT (current_function_decl);
4783 else
4784 current_function_decl = NULL_TREE;
4786 if (type != previous_class_type || current_class_depth > 1)
4788 #ifdef MI_MATRIX
4789 build_mi_matrix (type);
4790 push_class_decls (type);
4791 free_mi_matrix ();
4792 #else
4793 push_class_decls (type);
4794 #endif
4796 else
4798 tree item;
4800 /* Hooray, we successfully cached; let's just install the
4801 cached class_shadowed list, and walk through it to get the
4802 IDENTIFIER_TYPE_VALUEs correct. */
4803 set_class_shadows (previous_class_values);
4804 for (item = previous_class_values; item; item = TREE_CHAIN (item))
4806 tree id = TREE_PURPOSE (item);
4807 tree decl = IDENTIFIER_CLASS_VALUE (id);
4809 if (TREE_CODE (decl) == TYPE_DECL)
4810 set_identifier_type_value (id, TREE_TYPE (decl));
4812 unuse_fields (type);
4815 for (tags = CLASSTYPE_TAGS (type); tags; tags = TREE_CHAIN (tags))
4817 TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 1;
4818 if (! TREE_PURPOSE (tags))
4819 continue;
4820 pushtag (TREE_PURPOSE (tags), TREE_VALUE (tags), 0);
4823 current_function_decl = this_fndecl;
4827 /* Get out of the current class scope. If we were in a class scope
4828 previously, that is the one popped to. The flag MODIFY tells whether
4829 the current scope declarations needs to be modified as a result of
4830 popping to the previous scope. 0 is used for class definitions. */
4832 void
4833 popclass (modify)
4834 int modify;
4836 if (modify < 0)
4838 /* Back this old class out completely. */
4839 tree tags = CLASSTYPE_TAGS (previous_class_type);
4840 tree t;
4842 /* This code can be seen as a cache miss. When we've cached a
4843 class' scope's bindings and we can't use them, we need to reset
4844 them. This is it! */
4845 for (t = previous_class_values; t; t = TREE_CHAIN (t))
4846 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
4847 while (tags)
4849 TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
4850 tags = TREE_CHAIN (tags);
4852 goto ret;
4855 if (modify)
4857 /* Just remove from this class what didn't make
4858 it into IDENTIFIER_CLASS_VALUE. */
4859 tree tags = CLASSTYPE_TAGS (current_class_type);
4861 while (tags)
4863 TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
4864 tags = TREE_CHAIN (tags);
4868 /* Force clearing of IDENTIFIER_CLASS_VALUEs after a class definition,
4869 since not all class decls make it there currently. */
4870 poplevel_class (! modify);
4872 /* Since poplevel_class does the popping of class decls nowadays,
4873 this really only frees the obstack used for these decls.
4874 That's why it had to be moved down here. */
4875 if (modify)
4876 pop_class_decls ();
4878 current_class_depth--;
4879 current_class_type = *--current_class_stack;
4880 current_class_name = *--current_class_stack;
4882 pop_memoized_context (modify);
4884 ret:
4888 /* When entering a class scope, all enclosing class scopes' names with
4889 static meaning (static variables, static functions, types and enumerators)
4890 have to be visible. This recursive function calls pushclass for all
4891 enclosing class contexts until global or a local scope is reached.
4892 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
4893 formal of the same name. */
4895 void
4896 push_nested_class (type, modify)
4897 tree type;
4898 int modify;
4900 tree context;
4902 if (type == NULL_TREE || type == error_mark_node || ! IS_AGGR_TYPE (type)
4903 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
4904 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
4905 return;
4907 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
4909 if (context && TREE_CODE (context) == RECORD_TYPE)
4910 push_nested_class (context, 2);
4911 pushclass (type, modify);
4914 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
4916 void
4917 pop_nested_class (modify)
4918 int modify;
4920 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
4922 popclass (modify);
4923 if (context && TREE_CODE (context) == RECORD_TYPE)
4924 pop_nested_class (modify);
4927 /* Set global variables CURRENT_LANG_NAME to appropriate value
4928 so that behavior of name-mangling machinery is correct. */
4930 void
4931 push_lang_context (name)
4932 tree name;
4934 *current_lang_stack++ = current_lang_name;
4935 if (current_lang_stack >= current_lang_base + current_lang_stacksize)
4937 current_lang_base
4938 = (tree *)xrealloc (current_lang_base,
4939 sizeof (tree) * (current_lang_stacksize + 10));
4940 current_lang_stack = current_lang_base + current_lang_stacksize;
4941 current_lang_stacksize += 10;
4944 if (name == lang_name_cplusplus)
4946 strict_prototype = strict_prototypes_lang_cplusplus;
4947 current_lang_name = name;
4949 else if (name == lang_name_c)
4951 strict_prototype = strict_prototypes_lang_c;
4952 current_lang_name = name;
4954 else
4955 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
4958 /* Get out of the current language scope. */
4960 void
4961 pop_lang_context ()
4963 current_lang_name = *--current_lang_stack;
4964 if (current_lang_name == lang_name_cplusplus)
4965 strict_prototype = strict_prototypes_lang_cplusplus;
4966 else if (current_lang_name == lang_name_c)
4967 strict_prototype = strict_prototypes_lang_c;
4970 /* Type instantiation routines. */
4972 /* This function will instantiate the type of the expression given in
4973 RHS to match the type of LHSTYPE. If errors exist, then return
4974 error_mark_node. If only complain is COMPLAIN is set. If we are
4975 not complaining, never modify rhs, as overload resolution wants to
4976 try many possible instantiations, in hopes that at least one will
4977 work.
4979 This function is used in build_modify_expr, convert_arguments,
4980 build_c_cast, and compute_conversion_costs. */
4982 tree
4983 instantiate_type (lhstype, rhs, complain)
4984 tree lhstype, rhs;
4985 int complain;
4987 tree explicit_targs = NULL_TREE;
4989 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
4991 if (complain)
4992 error ("not enough type information");
4993 return error_mark_node;
4996 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
4998 if (comptypes (lhstype, TREE_TYPE (rhs), 1))
4999 return rhs;
5000 if (complain)
5001 cp_error ("argument of type `%T' does not match `%T'",
5002 TREE_TYPE (rhs), lhstype);
5003 return error_mark_node;
5006 rhs = copy_node (rhs);
5008 /* This should really only be used when attempting to distinguish
5009 what sort of a pointer to function we have. For now, any
5010 arithmetic operation which is not supported on pointers
5011 is rejected as an error. */
5013 switch (TREE_CODE (rhs))
5015 case TYPE_EXPR:
5016 case CONVERT_EXPR:
5017 case SAVE_EXPR:
5018 case CONSTRUCTOR:
5019 case BUFFER_REF:
5020 my_friendly_abort (177);
5021 return error_mark_node;
5023 case INDIRECT_REF:
5024 case ARRAY_REF:
5026 tree new_rhs;
5028 new_rhs = instantiate_type (build_pointer_type (lhstype),
5029 TREE_OPERAND (rhs, 0), complain);
5030 if (new_rhs == error_mark_node)
5031 return error_mark_node;
5033 TREE_TYPE (rhs) = lhstype;
5034 TREE_OPERAND (rhs, 0) = new_rhs;
5035 return rhs;
5038 case NOP_EXPR:
5039 rhs = copy_node (TREE_OPERAND (rhs, 0));
5040 TREE_TYPE (rhs) = unknown_type_node;
5041 return instantiate_type (lhstype, rhs, complain);
5043 case COMPONENT_REF:
5045 tree field = TREE_OPERAND (rhs, 1);
5046 if (TREE_CODE (field) == TREE_LIST)
5048 tree function = instantiate_type (lhstype, field, complain);
5049 if (function == error_mark_node)
5050 return error_mark_node;
5051 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 185);
5052 if (DECL_VINDEX (function))
5054 tree base = TREE_OPERAND (rhs, 0);
5055 tree base_ptr = build_unary_op (ADDR_EXPR, base, 0);
5056 if (base_ptr == error_mark_node)
5057 return error_mark_node;
5058 base_ptr = convert_pointer_to (DECL_CONTEXT (function), base_ptr);
5059 if (base_ptr == error_mark_node)
5060 return error_mark_node;
5061 return build_vfn_ref (&base_ptr, base, DECL_VINDEX (function));
5063 mark_used (function);
5064 return function;
5067 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 178);
5068 my_friendly_assert (!(TREE_CODE (TREE_TYPE (field)) == FUNCTION_TYPE
5069 || TREE_CODE (TREE_TYPE (field)) == METHOD_TYPE),
5070 179);
5072 TREE_TYPE (rhs) = lhstype;
5073 /* First look for an exact match */
5075 while (field && TREE_TYPE (field) != lhstype)
5076 field = DECL_CHAIN (field);
5077 if (field)
5079 TREE_OPERAND (rhs, 1) = field;
5080 mark_used (field);
5081 return rhs;
5084 /* No exact match found, look for a compatible function. */
5085 field = TREE_OPERAND (rhs, 1);
5086 while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
5087 field = DECL_CHAIN (field);
5088 if (field)
5090 TREE_OPERAND (rhs, 1) = field;
5091 field = DECL_CHAIN (field);
5092 while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
5093 field = DECL_CHAIN (field);
5094 if (field)
5096 if (complain)
5097 error ("ambiguous overload for COMPONENT_REF requested");
5098 return error_mark_node;
5101 else
5103 if (complain)
5104 error ("no appropriate overload exists for COMPONENT_REF");
5105 return error_mark_node;
5107 return rhs;
5110 case TEMPLATE_ID_EXPR:
5112 explicit_targs = TREE_OPERAND (rhs, 1);
5113 rhs = TREE_OPERAND (rhs, 0);
5115 /* fall through */
5117 case TREE_LIST:
5119 tree elem, baselink, name = NULL_TREE;
5120 int globals = overloaded_globals_p (rhs);
5122 /* First look for an exact match. Search either overloaded
5123 functions or member functions. May have to undo what
5124 `default_conversion' might do to lhstype. */
5126 if (TYPE_PTRMEMFUNC_P (lhstype))
5127 lhstype = TYPE_PTRMEMFUNC_FN_TYPE (lhstype);
5129 if (TREE_CODE (lhstype) == POINTER_TYPE)
5131 if (TREE_CODE (TREE_TYPE (lhstype)) == FUNCTION_TYPE
5132 || TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE)
5133 lhstype = TREE_TYPE (lhstype);
5134 else
5136 if (complain)
5137 error ("invalid type combination for overload");
5138 return error_mark_node;
5142 if (TREE_CODE (lhstype) != FUNCTION_TYPE && globals > 0)
5144 if (complain)
5145 cp_error ("cannot resolve overloaded function `%D' based on non-function type",
5146 TREE_PURPOSE (rhs));
5147 return error_mark_node;
5150 if (globals > 0)
5152 elem = get_first_fn (rhs);
5153 /* If there are explicit_targs, only a template function
5154 can match. */
5155 if (explicit_targs == NULL_TREE)
5156 while (elem)
5158 if (! comptypes (lhstype, TREE_TYPE (elem), 1))
5159 elem = DECL_CHAIN (elem);
5160 else
5162 mark_used (elem);
5163 return elem;
5167 /* No exact match found, look for a compatible template. */
5169 tree save_elem = 0;
5170 for (elem = get_first_fn (rhs); elem; elem = DECL_CHAIN (elem))
5171 if (TREE_CODE (elem) == TEMPLATE_DECL)
5173 int n = DECL_NTPARMS (elem);
5174 tree t = make_scratch_vec (n);
5175 int i;
5176 i = type_unification
5177 (DECL_INNERMOST_TEMPLATE_PARMS (elem),
5178 &TREE_VEC_ELT (t, 0), TYPE_ARG_TYPES (TREE_TYPE (elem)),
5179 TYPE_ARG_TYPES (lhstype), explicit_targs, 1, 1);
5180 if (i == 0)
5182 if (save_elem)
5184 cp_error ("ambiguous template instantiation converting to `%#T'", lhstype);
5185 return error_mark_node;
5187 save_elem = instantiate_template (elem, t);
5188 /* Check the return type. */
5189 if (! comptypes (TREE_TYPE (lhstype),
5190 TREE_TYPE (TREE_TYPE (save_elem)), 1))
5191 save_elem = 0;
5194 if (save_elem)
5196 mark_used (save_elem);
5197 return save_elem;
5201 /* If there are explicit_targs, only a template function
5202 can match. */
5203 if (explicit_targs == NULL_TREE)
5205 /* No match found, look for a compatible function. */
5206 elem = get_first_fn (rhs);
5207 while (elem && comp_target_types (lhstype,
5208 TREE_TYPE (elem), 1) <= 0)
5209 elem = DECL_CHAIN (elem);
5210 if (elem)
5212 tree save_elem = elem;
5213 elem = DECL_CHAIN (elem);
5214 while (elem
5215 && comp_target_types (lhstype,
5216 TREE_TYPE (elem), 0) <= 0)
5217 elem = DECL_CHAIN (elem);
5218 if (elem)
5220 if (complain)
5222 cp_error
5223 ("cannot resolve overload to target type `%#T'",
5224 lhstype);
5225 cp_error_at (" ambiguity between `%#D'",
5226 save_elem);
5227 cp_error_at (" and `%#D', at least", elem);
5229 return error_mark_node;
5231 mark_used (save_elem);
5232 return save_elem;
5235 if (complain)
5237 cp_error ("cannot resolve overload to target type `%#T'",
5238 lhstype);
5239 cp_error
5240 (" because no suitable overload of function `%D' exists",
5241 TREE_PURPOSE (rhs));
5243 return error_mark_node;
5246 if (TREE_NONLOCAL_FLAG (rhs))
5248 /* Got to get it as a baselink. */
5249 rhs = lookup_fnfields (TYPE_BINFO (current_class_type),
5250 TREE_PURPOSE (rhs), 0);
5252 else
5254 my_friendly_assert (TREE_CHAIN (rhs) == NULL_TREE, 181);
5255 if (TREE_CODE (TREE_VALUE (rhs)) == TREE_LIST)
5256 rhs = TREE_VALUE (rhs);
5257 my_friendly_assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL,
5258 182);
5261 for (baselink = rhs; baselink;
5262 baselink = next_baselink (baselink))
5264 elem = TREE_VALUE (baselink);
5265 while (elem)
5266 if (comptypes (lhstype, TREE_TYPE (elem), 1))
5268 mark_used (elem);
5269 return elem;
5271 else
5272 elem = DECL_CHAIN (elem);
5275 /* No exact match found, look for a compatible method. */
5276 for (baselink = rhs; baselink;
5277 baselink = next_baselink (baselink))
5279 elem = TREE_VALUE (baselink);
5280 while (elem && comp_target_types (lhstype,
5281 TREE_TYPE (elem), 1) <= 0)
5282 elem = DECL_CHAIN (elem);
5283 if (elem)
5285 tree save_elem = elem;
5286 elem = DECL_CHAIN (elem);
5287 while (elem && comp_target_types (lhstype,
5288 TREE_TYPE (elem), 0) <= 0)
5289 elem = DECL_CHAIN (elem);
5290 if (elem)
5292 if (complain)
5293 error ("ambiguous overload for overloaded method requested");
5294 return error_mark_node;
5296 mark_used (save_elem);
5297 return save_elem;
5299 name = DECL_NAME (TREE_VALUE (rhs));
5300 #if 0
5301 if (TREE_CODE (lhstype) == FUNCTION_TYPE && globals < 0)
5303 /* Try to instantiate from non-member functions. */
5304 rhs = lookup_name_nonclass (name);
5305 if (rhs && TREE_CODE (rhs) == TREE_LIST)
5307 /* This code seems to be missing a `return'. */
5308 my_friendly_abort (4);
5309 instantiate_type (lhstype, rhs, complain);
5312 #endif
5314 if (complain)
5315 cp_error ("no compatible member functions named `%D'", name);
5316 return error_mark_node;
5319 case CALL_EXPR:
5320 /* This is too hard for now. */
5321 my_friendly_abort (183);
5322 return error_mark_node;
5324 case PLUS_EXPR:
5325 case MINUS_EXPR:
5326 case COMPOUND_EXPR:
5327 TREE_OPERAND (rhs, 0)
5328 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
5329 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5330 return error_mark_node;
5331 TREE_OPERAND (rhs, 1)
5332 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
5333 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5334 return error_mark_node;
5336 TREE_TYPE (rhs) = lhstype;
5337 return rhs;
5339 case MULT_EXPR:
5340 case TRUNC_DIV_EXPR:
5341 case FLOOR_DIV_EXPR:
5342 case CEIL_DIV_EXPR:
5343 case ROUND_DIV_EXPR:
5344 case RDIV_EXPR:
5345 case TRUNC_MOD_EXPR:
5346 case FLOOR_MOD_EXPR:
5347 case CEIL_MOD_EXPR:
5348 case ROUND_MOD_EXPR:
5349 case FIX_ROUND_EXPR:
5350 case FIX_FLOOR_EXPR:
5351 case FIX_CEIL_EXPR:
5352 case FIX_TRUNC_EXPR:
5353 case FLOAT_EXPR:
5354 case NEGATE_EXPR:
5355 case ABS_EXPR:
5356 case MAX_EXPR:
5357 case MIN_EXPR:
5358 case FFS_EXPR:
5360 case BIT_AND_EXPR:
5361 case BIT_IOR_EXPR:
5362 case BIT_XOR_EXPR:
5363 case LSHIFT_EXPR:
5364 case RSHIFT_EXPR:
5365 case LROTATE_EXPR:
5366 case RROTATE_EXPR:
5368 case PREINCREMENT_EXPR:
5369 case PREDECREMENT_EXPR:
5370 case POSTINCREMENT_EXPR:
5371 case POSTDECREMENT_EXPR:
5372 if (complain)
5373 error ("invalid operation on uninstantiated type");
5374 return error_mark_node;
5376 case TRUTH_AND_EXPR:
5377 case TRUTH_OR_EXPR:
5378 case TRUTH_XOR_EXPR:
5379 case LT_EXPR:
5380 case LE_EXPR:
5381 case GT_EXPR:
5382 case GE_EXPR:
5383 case EQ_EXPR:
5384 case NE_EXPR:
5385 case TRUTH_ANDIF_EXPR:
5386 case TRUTH_ORIF_EXPR:
5387 case TRUTH_NOT_EXPR:
5388 if (complain)
5389 error ("not enough type information");
5390 return error_mark_node;
5392 case COND_EXPR:
5393 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
5395 if (complain)
5396 error ("not enough type information");
5397 return error_mark_node;
5399 TREE_OPERAND (rhs, 1)
5400 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
5401 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5402 return error_mark_node;
5403 TREE_OPERAND (rhs, 2)
5404 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), complain);
5405 if (TREE_OPERAND (rhs, 2) == error_mark_node)
5406 return error_mark_node;
5408 TREE_TYPE (rhs) = lhstype;
5409 return rhs;
5411 case MODIFY_EXPR:
5412 TREE_OPERAND (rhs, 1)
5413 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
5414 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5415 return error_mark_node;
5417 TREE_TYPE (rhs) = lhstype;
5418 return rhs;
5420 case ADDR_EXPR:
5421 if (TYPE_PTRMEMFUNC_P (lhstype))
5422 lhstype = TYPE_PTRMEMFUNC_FN_TYPE (lhstype);
5423 else if (TREE_CODE (lhstype) != POINTER_TYPE)
5425 if (complain)
5426 error ("type for resolving address of overloaded function must be pointer type");
5427 return error_mark_node;
5430 tree fn = instantiate_type (TREE_TYPE (lhstype), TREE_OPERAND (rhs, 0), complain);
5431 if (fn == error_mark_node)
5432 return error_mark_node;
5433 mark_addressable (fn);
5434 TREE_TYPE (rhs) = lhstype;
5435 TREE_OPERAND (rhs, 0) = fn;
5436 TREE_CONSTANT (rhs) = staticp (fn);
5437 if (TREE_CODE (lhstype) == POINTER_TYPE
5438 && TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE)
5440 build_ptrmemfunc_type (lhstype);
5441 rhs = build_ptrmemfunc (lhstype, rhs, 0);
5444 return rhs;
5446 case ENTRY_VALUE_EXPR:
5447 my_friendly_abort (184);
5448 return error_mark_node;
5450 case ERROR_MARK:
5451 return error_mark_node;
5453 default:
5454 my_friendly_abort (185);
5455 return error_mark_node;
5459 /* Return the name of the virtual function pointer field
5460 (as an IDENTIFIER_NODE) for the given TYPE. Note that
5461 this may have to look back through base types to find the
5462 ultimate field name. (For single inheritance, these could
5463 all be the same name. Who knows for multiple inheritance). */
5465 static tree
5466 get_vfield_name (type)
5467 tree type;
5469 tree binfo = TYPE_BINFO (type);
5470 char *buf;
5472 while (BINFO_BASETYPES (binfo)
5473 && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
5474 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
5475 binfo = BINFO_BASETYPE (binfo, 0);
5477 type = BINFO_TYPE (binfo);
5478 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
5479 + TYPE_NAME_LENGTH (type) + 2);
5480 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
5481 return get_identifier (buf);
5484 void
5485 print_class_statistics ()
5487 #ifdef GATHER_STATISTICS
5488 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
5489 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
5490 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
5491 n_build_method_call, n_inner_fields_searched);
5492 if (n_vtables)
5494 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
5495 n_vtables, n_vtable_searches);
5496 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
5497 n_vtable_entries, n_vtable_elems);
5499 #endif
5502 /* Push an obstack which is sufficiently long-lived to hold such class
5503 decls that may be cached in the previous_class_values list. For now, let's
5504 use the permanent obstack, later we may create a dedicated obstack just
5505 for this purpose. The effect is undone by pop_obstacks. */
5507 void
5508 maybe_push_cache_obstack ()
5510 push_obstacks_nochange ();
5511 if (current_class_depth == 1)
5512 current_obstack = &permanent_obstack;
5515 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
5516 according to [class]:
5517 The class-name is also inserted
5518 into the scope of the class itself. For purposes of access checking,
5519 the inserted class name is treated as if it were a public member name. */
5521 tree
5522 build_self_reference ()
5524 tree name = constructor_name (current_class_type);
5525 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
5526 DECL_NONLOCAL (value) = 1;
5527 DECL_CONTEXT (value) = current_class_type;
5528 DECL_CLASS_CONTEXT (value) = current_class_type;
5529 CLASSTYPE_LOCAL_TYPEDECLS (current_class_type) = 1;
5530 DECL_ARTIFICIAL (value) = 1;
5532 pushdecl_class_level (value);
5533 return value;