Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / cp / class.c
blob01a736ac7ac096362a2dd964aee77dc9b63ec27e
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_private_virtual_node; /* 6 */
151 /* Variables shared between class.c and call.c. */
153 #ifdef GATHER_STATISTICS
154 int n_vtables = 0;
155 int n_vtable_entries = 0;
156 int n_vtable_searches = 0;
157 int n_vtable_elems = 0;
158 int n_convert_harshness = 0;
159 int n_compute_conversion_costs = 0;
160 int n_build_method_call = 0;
161 int n_inner_fields_searched = 0;
162 #endif
164 /* Virtual baseclass things. */
166 static tree
167 build_vbase_pointer (exp, type)
168 tree exp, type;
170 char *name;
172 name = (char *) alloca (TYPE_NAME_LENGTH (type) + sizeof (VBASE_NAME) + 1);
173 sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (type));
174 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
177 /* Is the type of the EXPR, the complete type of the object?
178 If we are going to be wrong, we must be conservative, and return 0. */
180 static int
181 complete_type_p (expr)
182 tree expr;
184 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
185 while (1)
187 switch (TREE_CODE (expr))
189 case SAVE_EXPR:
190 case INDIRECT_REF:
191 case ADDR_EXPR:
192 case NOP_EXPR:
193 case CONVERT_EXPR:
194 expr = TREE_OPERAND (expr, 0);
195 continue;
197 case CALL_EXPR:
198 if (! TREE_HAS_CONSTRUCTOR (expr))
199 break;
200 /* fall through... */
201 case VAR_DECL:
202 case FIELD_DECL:
203 if (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
204 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr)))
205 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
206 return 1;
207 /* fall through... */
208 case TARGET_EXPR:
209 case PARM_DECL:
210 if (IS_AGGR_TYPE (TREE_TYPE (expr))
211 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
212 return 1;
213 /* fall through... */
214 case PLUS_EXPR:
215 default:
216 break;
218 break;
220 return 0;
223 /* Build multi-level access to EXPR using hierarchy path PATH.
224 CODE is PLUS_EXPR if we are going with the grain,
225 and MINUS_EXPR if we are not (in which case, we cannot traverse
226 virtual baseclass links).
228 TYPE is the type we want this path to have on exit.
230 ALIAS_THIS is non-zero if EXPR in an expression involving `this'. */
232 tree
233 build_vbase_path (code, type, expr, path, alias_this)
234 enum tree_code code;
235 tree type, expr, path;
236 int alias_this;
238 register int changed = 0;
239 tree last = NULL_TREE, last_virtual = NULL_TREE;
240 int nonnull = 0;
241 int fixed_type_p;
242 tree null_expr = 0, nonnull_expr;
243 tree basetype;
244 tree offset = integer_zero_node;
246 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
247 return build1 (NOP_EXPR, type, expr);
249 if (nonnull == 0 && (alias_this && flag_this_is_variable <= 0))
250 nonnull = 1;
252 #if 0
253 /* We need additional logic to convert back to the unconverted type
254 (the static type of the complete object), and then convert back
255 to the type we want. Until that is done, or until we can
256 recognize when that is, we cannot do the short cut logic. (mrs) */
257 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
258 #else
259 /* Do this, until we can undo any previous conversions. See net35.C
260 for a testcase. */
261 fixed_type_p = complete_type_p (expr);
262 #endif
264 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
265 expr = save_expr (expr);
266 nonnull_expr = expr;
268 if (BINFO_INHERITANCE_CHAIN (path))
270 tree reverse_path = NULL_TREE;
272 push_expression_obstack ();
273 while (path)
275 tree r = copy_node (path);
276 BINFO_INHERITANCE_CHAIN (r) = reverse_path;
277 reverse_path = r;
278 path = BINFO_INHERITANCE_CHAIN (path);
280 path = reverse_path;
281 pop_obstacks ();
284 basetype = BINFO_TYPE (path);
286 while (path)
288 if (TREE_VIA_VIRTUAL (path))
290 last_virtual = BINFO_TYPE (path);
291 if (code == PLUS_EXPR)
293 changed = ! fixed_type_p;
295 if (changed)
297 tree ind;
299 /* We already check for ambiguous things in the caller, just
300 find a path. */
301 if (last)
303 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
304 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
306 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
307 nonnull_expr = build_vbase_pointer (ind, last_virtual);
308 if (nonnull == 0
309 && (TREE_CODE (type) == POINTER_TYPE
310 || !flag_assume_nonnull_objects)
311 && null_expr == NULL_TREE)
313 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
314 expr = build (COND_EXPR, build_pointer_type (last_virtual),
315 build (EQ_EXPR, boolean_type_node, expr,
316 integer_zero_node),
317 null_expr, nonnull_expr);
320 /* else we'll figure out the offset below. */
322 /* Happens in the case of parse errors. */
323 if (nonnull_expr == error_mark_node)
324 return error_mark_node;
326 else
328 cp_error ("cannot cast up from virtual baseclass `%T'",
329 last_virtual);
330 return error_mark_node;
333 last = path;
334 path = BINFO_INHERITANCE_CHAIN (path);
336 /* LAST is now the last basetype assoc on the path. */
338 /* A pointer to a virtual base member of a non-null object
339 is non-null. Therefore, we only need to test for zeroness once.
340 Make EXPR the canonical expression to deal with here. */
341 if (null_expr)
343 TREE_OPERAND (expr, 2) = nonnull_expr;
344 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
345 = TREE_TYPE (nonnull_expr);
347 else
348 expr = nonnull_expr;
350 /* If we go through any virtual base pointers, make sure that
351 casts to BASETYPE from the last virtual base class use
352 the right value for BASETYPE. */
353 if (changed)
355 tree intype = TREE_TYPE (TREE_TYPE (expr));
356 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
358 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
359 offset = BINFO_OFFSET (binfo);
362 else
364 if (last_virtual)
366 offset = BINFO_OFFSET (binfo_member (last_virtual,
367 CLASSTYPE_VBASECLASSES (basetype)));
368 offset = size_binop (PLUS_EXPR, offset, BINFO_OFFSET (last));
370 else
371 offset = BINFO_OFFSET (last);
374 if (TREE_INT_CST_LOW (offset))
376 /* Bash types to make the backend happy. */
377 offset = cp_convert (type, offset);
378 #if 0
379 /* This shouldn't be necessary. (mrs) */
380 expr = build1 (NOP_EXPR, type, expr);
381 #endif
383 /* For multiple inheritance: if `this' can be set by any
384 function, then it could be 0 on entry to any function.
385 Preserve such zeroness here. Otherwise, only in the
386 case of constructors need we worry, and in those cases,
387 it will be zero, or initialized to some valid value to
388 which we may add. */
389 if (nonnull == 0)
391 if (null_expr)
392 TREE_TYPE (null_expr) = type;
393 else
394 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
395 if (TREE_SIDE_EFFECTS (expr))
396 expr = save_expr (expr);
398 return build (COND_EXPR, type,
399 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
400 null_expr,
401 build (code, type, expr, offset));
403 else return build (code, type, expr, offset);
406 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
407 be used multiple times in initialization of multiple inheritance. */
408 if (null_expr)
410 TREE_TYPE (expr) = type;
411 return expr;
413 else
414 return build1 (NOP_EXPR, type, expr);
417 /* Virtual function things. */
419 /* Build an entry in the virtual function table.
420 DELTA is the offset for the `this' pointer.
421 PFN is an ADDR_EXPR containing a pointer to the virtual function.
422 Note that the index (DELTA2) in the virtual function table
423 is always 0. */
425 static tree
426 build_vtable_entry (delta, pfn)
427 tree delta, pfn;
429 if (flag_vtable_thunks)
431 HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta);
432 if (idelta && ! DECL_ABSTRACT_VIRTUAL_P (TREE_OPERAND (pfn, 0)))
434 pfn = build1 (ADDR_EXPR, vtable_entry_type,
435 make_thunk (pfn, idelta));
436 TREE_READONLY (pfn) = 1;
437 TREE_CONSTANT (pfn) = 1;
439 #ifdef GATHER_STATISTICS
440 n_vtable_entries += 1;
441 #endif
442 return pfn;
444 else
446 extern int flag_huge_objects;
447 tree elems = expr_tree_cons (NULL_TREE, delta,
448 expr_tree_cons (NULL_TREE, integer_zero_node,
449 build_expr_list (NULL_TREE, pfn)));
450 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
452 /* DELTA is constructed by `size_int', which means it may be an
453 unsigned quantity on some platforms. Therefore, we cannot use
454 `int_fits_type_p', because when DELTA is really negative,
455 `force_fit_type' will make it look like a very large number. */
457 if ((TREE_INT_CST_LOW (TYPE_MAX_VALUE (delta_type_node))
458 < TREE_INT_CST_LOW (delta))
459 || (TREE_INT_CST_LOW (delta)
460 < TREE_INT_CST_LOW (TYPE_MIN_VALUE (delta_type_node))))
461 if (flag_huge_objects)
462 sorry ("object size exceeds built-in limit for virtual function table implementation");
463 else
464 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
466 TREE_CONSTANT (entry) = 1;
467 TREE_STATIC (entry) = 1;
468 TREE_READONLY (entry) = 1;
470 #ifdef GATHER_STATISTICS
471 n_vtable_entries += 1;
472 #endif
474 return entry;
478 /* Given an object INSTANCE, return an expression which yields the
479 virtual function vtable element corresponding to INDEX. There are
480 many special cases for INSTANCE which we take care of here, mainly
481 to avoid creating extra tree nodes when we don't have to. */
483 tree
484 build_vtbl_ref (instance, idx)
485 tree instance, idx;
487 tree vtbl, aref;
488 tree basetype = TREE_TYPE (instance);
490 if (TREE_CODE (basetype) == REFERENCE_TYPE)
491 basetype = TREE_TYPE (basetype);
493 if (instance == current_class_ref)
494 vtbl = build_indirect_ref (build_vfield_ref (instance, basetype),
495 NULL_PTR);
496 else
498 if (optimize)
500 /* Try to figure out what a reference refers to, and
501 access its virtual function table directly. */
502 tree ref = NULL_TREE;
504 if (TREE_CODE (instance) == INDIRECT_REF
505 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
506 ref = TREE_OPERAND (instance, 0);
507 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
508 ref = instance;
510 if (ref && TREE_CODE (ref) == VAR_DECL
511 && DECL_INITIAL (ref))
513 tree init = DECL_INITIAL (ref);
515 while (TREE_CODE (init) == NOP_EXPR
516 || TREE_CODE (init) == NON_LVALUE_EXPR)
517 init = TREE_OPERAND (init, 0);
518 if (TREE_CODE (init) == ADDR_EXPR)
520 init = TREE_OPERAND (init, 0);
521 if (IS_AGGR_TYPE (TREE_TYPE (init))
522 && (TREE_CODE (init) == PARM_DECL
523 || TREE_CODE (init) == VAR_DECL))
524 instance = init;
529 if (IS_AGGR_TYPE (TREE_TYPE (instance))
530 && (TREE_CODE (instance) == RESULT_DECL
531 || TREE_CODE (instance) == PARM_DECL
532 || TREE_CODE (instance) == VAR_DECL))
533 vtbl = TYPE_BINFO_VTABLE (basetype);
534 else
535 vtbl = build_indirect_ref (build_vfield_ref (instance, basetype),
536 NULL_PTR);
538 assemble_external (vtbl);
539 aref = build_array_ref (vtbl, idx);
541 return aref;
544 /* Given an object INSTANCE, return an expression which yields the
545 virtual function corresponding to INDEX. There are many special
546 cases for INSTANCE which we take care of here, mainly to avoid
547 creating extra tree nodes when we don't have to. */
549 tree
550 build_vfn_ref (ptr_to_instptr, instance, idx)
551 tree *ptr_to_instptr, instance;
552 tree idx;
554 tree aref = build_vtbl_ref (instance, idx);
556 /* When using thunks, there is no extra delta, and we get the pfn
557 directly. */
558 if (flag_vtable_thunks)
559 return aref;
561 if (ptr_to_instptr)
563 /* Save the intermediate result in a SAVE_EXPR so we don't have to
564 compute each component of the virtual function pointer twice. */
565 if (TREE_CODE (aref) == INDIRECT_REF)
566 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
568 *ptr_to_instptr
569 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
570 *ptr_to_instptr,
571 cp_convert (ptrdiff_type_node,
572 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
575 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
578 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
579 for the given TYPE. */
581 static tree
582 get_vtable_name (type)
583 tree type;
585 tree type_id = build_typename_overload (type);
586 char *buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
587 + IDENTIFIER_LENGTH (type_id) + 2);
588 char *ptr = IDENTIFIER_POINTER (type_id);
589 int i;
590 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
591 #if 0
592 /* We don't take off the numbers; prepare_fresh_vtable uses the
593 DECL_ASSEMBLER_NAME for the type, which includes the number
594 in `3foo'. If we were to pull them off here, we'd end up with
595 something like `_vt.foo.3bar', instead of a uniform definition. */
596 while (ptr[i] >= '0' && ptr[i] <= '9')
597 i += 1;
598 #endif
599 sprintf (buf, VTABLE_NAME_FORMAT, ptr+i);
600 return get_identifier (buf);
603 /* Return the offset to the main vtable for a given base BINFO. */
605 tree
606 get_vfield_offset (binfo)
607 tree binfo;
609 return size_binop (PLUS_EXPR,
610 size_binop (FLOOR_DIV_EXPR,
611 DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))),
612 size_int (BITS_PER_UNIT)),
613 BINFO_OFFSET (binfo));
616 /* Get the offset to the start of the original binfo that we derived
617 this binfo from. If we find TYPE first, return the offset only
618 that far. The shortened search is useful because the this pointer
619 on method calling is expected to point to a DECL_CONTEXT (fndecl)
620 object, and not a baseclass of it. */
622 static tree
623 get_derived_offset (binfo, type)
624 tree binfo, type;
626 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
627 tree offset2;
628 int i;
629 while (BINFO_BASETYPES (binfo)
630 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
632 tree binfos = BINFO_BASETYPES (binfo);
633 if (BINFO_TYPE (binfo) == type)
634 break;
635 binfo = TREE_VEC_ELT (binfos, i);
637 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
638 return size_binop (MINUS_EXPR, offset1, offset2);
641 /* Update the rtti info for this class. */
643 static void
644 set_rtti_entry (virtuals, offset, type)
645 tree virtuals, offset, type;
647 tree vfn;
649 if (flag_rtti)
650 vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, get_tinfo_fn (type));
651 else
652 vfn = build1 (NOP_EXPR, vfunc_ptr_type_node, size_zero_node);
653 TREE_CONSTANT (vfn) = 1;
655 if (! flag_vtable_thunks)
656 TREE_VALUE (virtuals) = build_vtable_entry (offset, vfn);
657 else
659 tree voff = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
660 TREE_CONSTANT (voff) = 1;
662 TREE_VALUE (virtuals) = build_vtable_entry (size_zero_node, voff);
664 /* The second slot is for the tdesc pointer when thunks are used. */
665 TREE_VALUE (TREE_CHAIN (virtuals))
666 = build_vtable_entry (size_zero_node, vfn);
670 /* Build a virtual function for type TYPE.
671 If BINFO is non-NULL, build the vtable starting with the initial
672 approximation that it is the same as the one which is the head of
673 the association list. */
675 static tree
676 build_vtable (binfo, type)
677 tree binfo, type;
679 tree name = get_vtable_name (type);
680 tree virtuals, decl;
682 if (binfo)
684 tree offset;
686 virtuals = copy_list (BINFO_VIRTUALS (binfo));
687 decl = build_decl (VAR_DECL, name, TREE_TYPE (BINFO_VTABLE (binfo)));
689 /* Now do rtti stuff. */
690 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
691 offset = size_binop (MINUS_EXPR, size_zero_node, offset);
692 set_rtti_entry (virtuals, offset, type);
694 else
696 virtuals = NULL_TREE;
697 decl = build_decl (VAR_DECL, name, void_type_node);
700 #ifdef GATHER_STATISTICS
701 n_vtables += 1;
702 n_vtable_elems += list_length (virtuals);
703 #endif
705 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
706 import_export_vtable (decl, type, 0);
708 IDENTIFIER_GLOBAL_VALUE (name) = decl = pushdecl_top_level (decl);
709 /* Initialize the association list for this type, based
710 on our first approximation. */
711 TYPE_BINFO_VTABLE (type) = decl;
712 TYPE_BINFO_VIRTUALS (type) = virtuals;
714 DECL_ARTIFICIAL (decl) = 1;
715 TREE_STATIC (decl) = 1;
716 #ifndef WRITABLE_VTABLES
717 /* Make them READONLY by default. (mrs) */
718 TREE_READONLY (decl) = 1;
719 #endif
720 /* At one time the vtable info was grabbed 2 words at a time. This
721 fails on sparc unless you have 8-byte alignment. (tiemann) */
722 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
723 DECL_ALIGN (decl));
725 /* Why is this conditional? (mrs) */
726 if (binfo && write_virtuals >= 0)
727 DECL_VIRTUAL_P (decl) = 1;
728 DECL_CONTEXT (decl) = type;
730 binfo = TYPE_BINFO (type);
731 SET_BINFO_NEW_VTABLE_MARKED (binfo);
732 return decl;
735 extern tree signed_size_zero_node;
737 /* Give TYPE a new virtual function table which is initialized
738 with a skeleton-copy of its original initialization. The only
739 entry that changes is the `delta' entry, so we can really
740 share a lot of structure.
742 FOR_TYPE is the derived type which caused this table to
743 be needed.
745 BINFO is the type association which provided TYPE for FOR_TYPE.
747 The order in which vtables are built (by calling this function) for
748 an object must remain the same, otherwise a binary incompatibility
749 can result. */
751 static void
752 prepare_fresh_vtable (binfo, for_type)
753 tree binfo, for_type;
755 tree basetype;
756 tree orig_decl = BINFO_VTABLE (binfo);
757 tree name;
758 tree new_decl;
759 tree offset;
760 tree path = binfo;
761 char *buf, *buf2;
762 char joiner = '_';
763 int i;
765 #ifdef JOINER
766 joiner = JOINER;
767 #endif
769 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
771 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
772 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
774 /* We know that the vtable that we are going to create doesn't exist
775 yet in the global namespace, and when we finish, it will be
776 pushed into the global namespace. In complex MI hierarchies, we
777 have to loop while the name we are thinking of adding is globally
778 defined, adding more name components to the vtable name as we
779 loop, until the name is unique. This is because in complex MI
780 cases, we might have the same base more than once. This means
781 that the order in which this function is called for vtables must
782 remain the same, otherwise binary compatibility can be
783 compromised. */
785 while (1)
787 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type) + 1 + i);
788 char *new_buf2;
790 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
791 buf2);
792 buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT) + strlen (buf1) + 1);
793 sprintf (buf, VTABLE_NAME_FORMAT, buf1);
794 name = get_identifier (buf);
796 /* If this name doesn't clash, then we can use it, otherwise
797 we add more to the name until it is unique. */
799 if (! IDENTIFIER_GLOBAL_VALUE (name))
800 break;
802 /* Set values for next loop through, if the name isn't unique. */
804 path = BINFO_INHERITANCE_CHAIN (path);
806 /* We better not run out of stuff to make it unique. */
807 my_friendly_assert (path != NULL_TREE, 368);
809 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
811 /* We better not run out of stuff to make it unique. */
812 my_friendly_assert (for_type != basetype, 369);
814 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
815 new_buf2 = (char *) alloca (i);
816 sprintf (new_buf2, "%s%c%s",
817 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
818 buf2 = new_buf2;
821 new_decl = build_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
822 /* Remember which class this vtable is really for. */
823 DECL_CONTEXT (new_decl) = for_type;
825 DECL_ARTIFICIAL (new_decl) = 1;
826 TREE_STATIC (new_decl) = 1;
827 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
828 DECL_VIRTUAL_P (new_decl) = 1;
829 #ifndef WRITABLE_VTABLES
830 /* Make them READONLY by default. (mrs) */
831 TREE_READONLY (new_decl) = 1;
832 #endif
833 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
835 /* Make fresh virtual list, so we can smash it later. */
836 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
838 if (TREE_VIA_VIRTUAL (binfo))
840 tree binfo1 = binfo_member (BINFO_TYPE (binfo),
841 CLASSTYPE_VBASECLASSES (for_type));
843 /* XXX - This should never happen, if it does, the caller should
844 ensure that the binfo is from for_type's binfos, not from any
845 base type's. We can remove all this code after a while. */
846 if (binfo1 != binfo)
847 warning ("internal inconsistency: binfo offset error for rtti");
849 offset = BINFO_OFFSET (binfo1);
851 else
852 offset = BINFO_OFFSET (binfo);
854 set_rtti_entry (BINFO_VIRTUALS (binfo),
855 size_binop (MINUS_EXPR, signed_size_zero_node, offset),
856 for_type);
858 #ifdef GATHER_STATISTICS
859 n_vtables += 1;
860 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
861 #endif
863 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
864 import_export_vtable (new_decl, for_type, 0);
866 if (TREE_VIA_VIRTUAL (binfo))
867 my_friendly_assert (binfo == binfo_member (BINFO_TYPE (binfo),
868 CLASSTYPE_VBASECLASSES (current_class_type)),
869 170);
870 SET_BINFO_NEW_VTABLE_MARKED (binfo);
873 #if 0
874 /* Access the virtual function table entry that logically
875 contains BASE_FNDECL. VIRTUALS is the virtual function table's
876 initializer. We can run off the end, when dealing with virtual
877 destructors in MI situations, return NULL_TREE in that case. */
879 static tree
880 get_vtable_entry (virtuals, base_fndecl)
881 tree virtuals, base_fndecl;
883 unsigned HOST_WIDE_INT n = (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
884 ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
885 & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1))
886 : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
888 #ifdef GATHER_STATISTICS
889 n_vtable_searches += n;
890 #endif
892 while (n > 0 && virtuals)
894 --n;
895 virtuals = TREE_CHAIN (virtuals);
897 return virtuals;
899 #endif
901 /* Put new entry ENTRY into virtual function table initializer
902 VIRTUALS.
904 Also update DECL_VINDEX (FNDECL). */
906 static void
907 modify_vtable_entry (old_entry_in_list, new_entry, fndecl)
908 tree old_entry_in_list, new_entry, fndecl;
910 tree base_fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list)), 0);
912 #ifdef NOTQUITE
913 cp_warning ("replaced %D with %D", DECL_ASSEMBLER_NAME (base_fndecl),
914 DECL_ASSEMBLER_NAME (fndecl));
915 #endif
916 TREE_VALUE (old_entry_in_list) = new_entry;
918 /* Now assign virtual dispatch information, if unset. */
919 /* We can dispatch this, through any overridden base function. */
920 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
922 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
923 DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
927 /* Access the virtual function table entry N. VIRTUALS is the virtual
928 function table's initializer. */
930 static tree
931 get_vtable_entry_n (virtuals, n)
932 tree virtuals;
933 unsigned HOST_WIDE_INT n;
935 while (n > 0)
937 --n;
938 virtuals = TREE_CHAIN (virtuals);
940 return virtuals;
943 /* Add a virtual function to all the appropriate vtables for the class
944 T. DECL_VINDEX(X) should be error_mark_node, if we want to
945 allocate a new slot in our table. If it is error_mark_node, we
946 know that no other function from another vtable is overridden by X.
947 HAS_VIRTUAL keeps track of how many virtuals there are in our main
948 vtable for the type, and we build upon the PENDING_VIRTUALS list
949 and return it. */
951 static void
952 add_virtual_function (pv, phv, has_virtual, fndecl, t)
953 tree *pv, *phv;
954 int *has_virtual;
955 tree fndecl;
956 tree t; /* Structure type. */
958 tree pending_virtuals = *pv;
959 tree pending_hard_virtuals = *phv;
961 /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
962 convert to void *. Make such a conversion here. */
963 tree vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
964 TREE_CONSTANT (vfn) = 1;
966 #ifndef DUMB_USER
967 if (current_class_type == 0)
968 cp_warning ("internal problem, current_class_type is zero when adding `%D', please report",
969 fndecl);
970 if (current_class_type && t != current_class_type)
971 cp_warning ("internal problem, current_class_type differs when adding `%D', please report",
972 fndecl);
973 #endif
975 /* If the virtual function is a redefinition of a prior one,
976 figure out in which base class the new definition goes,
977 and if necessary, make a fresh virtual function table
978 to hold that entry. */
979 if (DECL_VINDEX (fndecl) == error_mark_node)
981 tree entry;
983 /* We remember that this was the base sub-object for rtti. */
984 CLASSTYPE_RTTI (t) = t;
986 /* If we are using thunks, use two slots at the front, one
987 for the offset pointer, one for the tdesc pointer. */
988 if (*has_virtual == 0 && flag_vtable_thunks)
990 *has_virtual = 1;
993 /* Build a new INT_CST for this DECL_VINDEX. */
995 static tree index_table[256];
996 tree idx;
997 /* We skip a slot for the offset/tdesc entry. */
998 int i = ++(*has_virtual);
1000 if (i >= 256 || index_table[i] == 0)
1002 idx = build_int_2 (i, 0);
1003 if (i < 256)
1004 index_table[i] = idx;
1006 else
1007 idx = index_table[i];
1009 /* Now assign virtual dispatch information. */
1010 DECL_VINDEX (fndecl) = idx;
1011 DECL_CONTEXT (fndecl) = t;
1013 entry = build_vtable_entry (integer_zero_node, vfn);
1014 pending_virtuals = tree_cons (DECL_VINDEX (fndecl), entry, pending_virtuals);
1016 /* Might already be INTEGER_CST if declared twice in class. We will
1017 give error later or we've already given it. */
1018 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1020 /* Need an entry in some other virtual function table.
1021 Deal with this after we have laid out our virtual base classes. */
1022 pending_hard_virtuals = temp_tree_cons (fndecl, vfn, pending_hard_virtuals);
1024 *pv = pending_virtuals;
1025 *phv = pending_hard_virtuals;
1028 /* Obstack on which to build the vector of class methods. */
1029 struct obstack class_obstack;
1030 extern struct obstack *current_obstack;
1032 /* Add method METHOD to class TYPE. This is used when a method
1033 has been defined which did not initially appear in the class definition,
1034 and helps cut down on spurious error messages.
1036 FIELDS is the entry in the METHOD_VEC vector entry of the class type where
1037 the method should be added. */
1039 void
1040 add_method (type, fields, method)
1041 tree type, *fields, method;
1043 /* We must make a copy of METHOD here, since we must be sure that
1044 we have exclusive title to this method's DECL_CHAIN. */
1045 tree decl;
1047 push_obstacks (&permanent_obstack, &permanent_obstack);
1049 decl = copy_node (method);
1050 if (DECL_RTL (decl) == 0
1051 && (!processing_template_decl
1052 || !uses_template_parms (decl)))
1054 make_function_rtl (decl);
1055 DECL_RTL (method) = DECL_RTL (decl);
1059 if (fields && *fields)
1061 /* Take care not to hide destructor. */
1062 DECL_CHAIN (decl) = DECL_CHAIN (*fields);
1063 DECL_CHAIN (*fields) = decl;
1065 else if (CLASSTYPE_METHOD_VEC (type) == 0)
1067 tree method_vec = make_node (TREE_VEC);
1068 if (TYPE_IDENTIFIER (type) == DECL_NAME (decl))
1070 /* ??? Is it possible for there to have been enough room in the
1071 current chunk for the tree_vec structure but not a tree_vec
1072 plus a tree*? Will this work in that case? */
1073 obstack_free (current_obstack, method_vec);
1074 obstack_blank (current_obstack, sizeof (struct tree_vec) + sizeof (tree *));
1075 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)))
1076 TREE_VEC_ELT (method_vec, 1) = decl;
1077 else
1078 TREE_VEC_ELT (method_vec, 0) = decl;
1079 TREE_VEC_LENGTH (method_vec) = 2;
1081 else
1083 /* ??? Is it possible for there to have been enough room in the
1084 current chunk for the tree_vec structure but not a tree_vec
1085 plus a tree*? Will this work in that case? */
1086 obstack_free (current_obstack, method_vec);
1087 obstack_blank (current_obstack, sizeof (struct tree_vec) + 2*sizeof (tree *));
1088 TREE_VEC_ELT (method_vec, 2) = decl;
1089 TREE_VEC_LENGTH (method_vec) = 3;
1090 obstack_finish (current_obstack);
1092 CLASSTYPE_METHOD_VEC (type) = method_vec;
1094 else
1096 tree method_vec = CLASSTYPE_METHOD_VEC (type);
1097 int len = TREE_VEC_LENGTH (method_vec);
1099 /* Adding a new ctor or dtor. This is easy because our
1100 METHOD_VEC always has a slot for such entries. */
1101 if (TYPE_IDENTIFIER (type) == DECL_NAME (decl))
1103 int idx = !!DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl));
1104 /* TREE_VEC_ELT (method_vec, idx) = decl; */
1105 if (decl != TREE_VEC_ELT (method_vec, idx))
1107 DECL_CHAIN (decl) = TREE_VEC_ELT (method_vec, idx);
1108 TREE_VEC_ELT (method_vec, idx) = decl;
1111 else
1113 /* This is trickier. We try to extend the TREE_VEC in-place,
1114 but if that does not work, we copy all its data to a new
1115 TREE_VEC that's large enough. */
1116 struct obstack *ob = &class_obstack;
1117 tree *end = (tree *)obstack_next_free (ob);
1119 if (end != TREE_VEC_END (method_vec))
1121 ob = current_obstack;
1122 TREE_VEC_LENGTH (method_vec) += 1;
1123 TREE_VEC_ELT (method_vec, len) = NULL_TREE;
1124 method_vec = copy_node (method_vec);
1125 TREE_VEC_LENGTH (method_vec) -= 1;
1127 else
1129 tree tmp_vec = (tree) obstack_base (ob);
1130 if (obstack_room (ob) < sizeof (tree))
1132 obstack_blank (ob, sizeof (struct tree_common)
1133 + tree_code_length[(int) TREE_VEC]
1134 * sizeof (char *)
1135 + len * sizeof (tree));
1136 tmp_vec = (tree) obstack_base (ob);
1137 bcopy ((char *) method_vec, (char *) tmp_vec,
1138 (sizeof (struct tree_common)
1139 + tree_code_length[(int) TREE_VEC] * sizeof (char *)
1140 + (len-1) * sizeof (tree)));
1141 method_vec = tmp_vec;
1143 else
1144 obstack_blank (ob, sizeof (tree));
1147 obstack_finish (ob);
1148 TREE_VEC_ELT (method_vec, len) = decl;
1149 TREE_VEC_LENGTH (method_vec) = len + 1;
1150 CLASSTYPE_METHOD_VEC (type) = method_vec;
1152 if (TYPE_BINFO_BASETYPES (type) && CLASSTYPE_BASELINK_VEC (type))
1154 /* ??? May be better to know whether these can be extended? */
1155 tree baselink_vec = CLASSTYPE_BASELINK_VEC (type);
1157 TREE_VEC_LENGTH (baselink_vec) += 1;
1158 CLASSTYPE_BASELINK_VEC (type) = copy_node (baselink_vec);
1159 TREE_VEC_LENGTH (baselink_vec) -= 1;
1161 TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), len) = 0;
1165 DECL_CONTEXT (decl) = type;
1166 DECL_CLASS_CONTEXT (decl) = type;
1168 pop_obstacks ();
1171 /* Subroutines of finish_struct. */
1173 /* Look through the list of fields for this struct, deleting
1174 duplicates as we go. This must be recursive to handle
1175 anonymous unions.
1177 FIELD is the field which may not appear anywhere in FIELDS.
1178 FIELD_PTR, if non-null, is the starting point at which
1179 chained deletions may take place.
1180 The value returned is the first acceptable entry found
1181 in FIELDS.
1183 Note that anonymous fields which are not of UNION_TYPE are
1184 not duplicates, they are just anonymous fields. This happens
1185 when we have unnamed bitfields, for example. */
1187 static tree
1188 delete_duplicate_fields_1 (field, fields)
1189 tree field, fields;
1191 tree x;
1192 tree prev = 0;
1193 if (DECL_NAME (field) == 0)
1195 if (TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)
1196 return fields;
1198 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1199 fields = delete_duplicate_fields_1 (x, fields);
1200 return fields;
1202 else
1204 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1206 if (DECL_NAME (x) == 0)
1208 if (TREE_CODE (TREE_TYPE (x)) != UNION_TYPE)
1209 continue;
1210 TYPE_FIELDS (TREE_TYPE (x))
1211 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1212 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1214 if (prev == 0)
1215 fields = TREE_CHAIN (fields);
1216 else
1217 TREE_CHAIN (prev) = TREE_CHAIN (x);
1220 else
1222 if (DECL_NAME (field) == DECL_NAME (x))
1224 if (TREE_CODE (field) == CONST_DECL
1225 && TREE_CODE (x) == CONST_DECL)
1226 cp_error_at ("duplicate enum value `%D'", x);
1227 else if (TREE_CODE (field) == CONST_DECL
1228 || TREE_CODE (x) == CONST_DECL)
1229 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1231 else if (TREE_CODE (field) == TYPE_DECL
1232 && TREE_CODE (x) == TYPE_DECL)
1234 if (TREE_TYPE (field) == TREE_TYPE (x))
1235 continue;
1236 cp_error_at ("duplicate nested type `%D'", x);
1238 else if (TREE_CODE (field) == TYPE_DECL
1239 || TREE_CODE (x) == TYPE_DECL)
1241 /* Hide tag decls. */
1242 if ((TREE_CODE (field) == TYPE_DECL
1243 && DECL_ARTIFICIAL (field))
1244 || (TREE_CODE (x) == TYPE_DECL
1245 && DECL_ARTIFICIAL (x)))
1246 continue;
1247 cp_error_at ("duplicate field `%D' (as type and non-type)",
1250 else
1251 cp_error_at ("duplicate member `%D'", x);
1252 if (prev == 0)
1253 fields = TREE_CHAIN (fields);
1254 else
1255 TREE_CHAIN (prev) = TREE_CHAIN (x);
1260 return fields;
1263 static void
1264 delete_duplicate_fields (fields)
1265 tree fields;
1267 tree x;
1268 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1269 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1272 /* Change the access of FDECL to ACCESS in T.
1273 Return 1 if change was legit, otherwise return 0. */
1275 static int
1276 alter_access (t, fdecl, access)
1277 tree t;
1278 tree fdecl;
1279 tree access;
1281 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1282 if (elem && TREE_VALUE (elem) != access)
1284 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1286 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1288 else
1289 error ("conflicting access specifications for field `%s', ignored",
1290 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1292 else if (TREE_PRIVATE (fdecl))
1294 if (access != access_private_node)
1295 cp_error_at ("cannot make private `%D' non-private", fdecl);
1296 goto alter;
1298 else if (TREE_PROTECTED (fdecl))
1300 if (access != access_protected_node)
1301 cp_error_at ("cannot make protected `%D' non-protected", fdecl);
1302 goto alter;
1304 /* ARM 11.3: an access declaration may not be used to restrict access
1305 to a member that is accessible in the base class. */
1306 else if (access != access_public_node)
1307 cp_error_at ("cannot reduce access of public member `%D'", fdecl);
1308 else if (elem == NULL_TREE)
1310 alter:
1311 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1312 return 1;
1314 return 0;
1317 /* If FOR_TYPE needs to reinitialize virtual function table pointers
1318 for TYPE's sub-objects, add such reinitializations to BASE_INIT_LIST.
1319 Returns BASE_INIT_LIST appropriately modified. */
1321 static tree
1322 maybe_fixup_vptrs (for_type, binfo, base_init_list)
1323 tree for_type, binfo, base_init_list;
1325 /* Now reinitialize any slots that don't fall under our virtual
1326 function table pointer. */
1327 tree vfields = CLASSTYPE_VFIELDS (BINFO_TYPE (binfo));
1328 while (vfields)
1330 tree basetype = VF_NORMAL_VALUE (vfields)
1331 ? TYPE_MAIN_VARIANT (VF_NORMAL_VALUE (vfields))
1332 : VF_BASETYPE_VALUE (vfields);
1334 tree base_binfo = get_binfo (basetype, for_type, 0);
1335 /* Punt until this is implemented. */
1336 if (1 /* BINFO_MODIFIED (base_binfo) */)
1338 tree base_offset = get_vfield_offset (base_binfo);
1339 if (! tree_int_cst_equal (base_offset, get_vfield_offset (TYPE_BINFO (for_type)))
1340 && ! tree_int_cst_equal (base_offset, get_vfield_offset (binfo)))
1341 base_init_list = tree_cons (error_mark_node, base_binfo,
1342 base_init_list);
1344 vfields = TREE_CHAIN (vfields);
1346 return base_init_list;
1349 /* If TYPE does not have a constructor, then the compiler must
1350 manually deal with all of the initialization this type requires.
1352 If a base initializer exists only to fill in the virtual function
1353 table pointer, then we mark that fact with the TREE_VIRTUAL bit.
1354 This way, we avoid multiple initializations of the same field by
1355 each virtual function table up the class hierarchy.
1357 Virtual base class pointers are not initialized here. They are
1358 initialized only at the "top level" of object creation. If we
1359 initialized them here, we would have to skip a lot of work. */
1361 static void
1362 build_class_init_list (type)
1363 tree type;
1365 tree base_init_list = NULL_TREE;
1366 tree member_init_list = NULL_TREE;
1368 /* Since we build member_init_list and base_init_list using
1369 tree_cons, backwards fields the all through work. */
1370 tree x;
1371 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
1372 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1374 for (x = TYPE_FIELDS (type); x; x = TREE_CHAIN (x))
1376 if (TREE_CODE (x) != FIELD_DECL)
1377 continue;
1379 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (x))
1380 || DECL_INITIAL (x) != NULL_TREE)
1381 member_init_list = tree_cons (x, type, member_init_list);
1383 member_init_list = nreverse (member_init_list);
1385 /* We will end up doing this last. Need special marker
1386 to avoid infinite regress. */
1387 if (TYPE_VIRTUAL_P (type))
1389 base_init_list = build_tree_list (error_mark_node, TYPE_BINFO (type));
1390 if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (type) == 0)
1391 TREE_VALUE (base_init_list) = NULL_TREE;
1392 TREE_ADDRESSABLE (base_init_list) = 1;
1395 /* Each base class which needs to have initialization
1396 of some kind gets to make such requests known here. */
1397 for (i = n_baseclasses-1; i >= 0; i--)
1399 tree base_binfo = TREE_VEC_ELT (binfos, i);
1400 tree blist;
1402 /* Don't initialize virtual baseclasses this way. */
1403 if (TREE_VIA_VIRTUAL (base_binfo))
1404 continue;
1406 if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (base_binfo)))
1408 /* ...and the last shall come first... */
1409 base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
1410 base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
1411 continue;
1414 if ((blist = CLASSTYPE_BASE_INIT_LIST (BINFO_TYPE (base_binfo))) == NULL_TREE)
1415 /* Nothing to initialize. */
1416 continue;
1418 /* ...ditto... */
1419 base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
1421 /* This is normally true for single inheritance.
1422 The win is we can shrink the chain of initializations
1423 to be done by only converting to the actual type
1424 we are interested in. */
1425 if (TREE_VALUE (blist)
1426 && TREE_CODE (TREE_VALUE (blist)) == TREE_VEC
1427 && tree_int_cst_equal (BINFO_OFFSET (base_binfo),
1428 BINFO_OFFSET (TREE_VALUE (blist))))
1430 if (base_init_list)
1432 /* Does it do more than just fill in a
1433 virtual function table pointer? */
1434 if (! TREE_ADDRESSABLE (blist))
1435 base_init_list = build_tree_list (blist, base_init_list);
1436 /* Can we get by just with the virtual function table
1437 pointer that it fills in? */
1438 else if (TREE_ADDRESSABLE (base_init_list)
1439 && TREE_VALUE (base_init_list) == 0)
1440 base_init_list = blist;
1441 /* Maybe, but it is not obvious as the previous case. */
1442 else if (! CLASSTYPE_NEEDS_VIRTUAL_REINIT (type))
1444 tree last = tree_last (base_init_list);
1445 while (TREE_VALUE (last)
1446 && TREE_CODE (TREE_VALUE (last)) == TREE_LIST)
1447 last = tree_last (TREE_VALUE (last));
1448 if (TREE_VALUE (last) == 0)
1449 base_init_list = build_tree_list (blist, base_init_list);
1452 else
1453 base_init_list = blist;
1455 else
1457 /* The function expand_aggr_init knows how to do the
1458 initialization of `basetype' without getting
1459 an explicit `blist'. */
1460 if (base_init_list)
1461 base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
1462 else
1463 base_init_list = CLASSTYPE_BINFO_AS_LIST (BINFO_TYPE (base_binfo));
1467 if (base_init_list)
1468 if (member_init_list)
1469 CLASSTYPE_BASE_INIT_LIST (type) = build_tree_list (base_init_list, member_init_list);
1470 else
1471 CLASSTYPE_BASE_INIT_LIST (type) = base_init_list;
1472 else if (member_init_list)
1473 CLASSTYPE_BASE_INIT_LIST (type) = member_init_list;
1476 struct base_info
1478 int has_virtual;
1479 int max_has_virtual;
1480 int n_ancestors;
1481 tree vfield;
1482 tree vfields;
1483 tree rtti;
1484 char cant_have_default_ctor;
1485 char cant_have_const_ctor;
1486 char no_const_asn_ref;
1489 /* Record information about type T derived from its base classes.
1490 Store most of that information in T itself, and place the
1491 remaining information in the struct BASE_INFO.
1493 Propagate basetype offsets throughout the lattice. Note that the
1494 lattice topped by T is really a pair: it's a DAG that gives the
1495 structure of the derivation hierarchy, and it's a list of the
1496 virtual baseclasses that appear anywhere in the DAG. When a vbase
1497 type appears in the DAG, it's offset is 0, and it's children start
1498 their offsets from that point. When a vbase type appears in the list,
1499 its offset is the offset it has in the hierarchy, and its children's
1500 offsets include that offset in theirs.
1502 Returns the index of the first base class to have virtual functions,
1503 or -1 if no such base class.
1505 Note that at this point TYPE_BINFO (t) != t_binfo. */
1507 static int
1508 finish_base_struct (t, b, t_binfo)
1509 tree t;
1510 struct base_info *b;
1511 tree t_binfo;
1513 tree binfos = BINFO_BASETYPES (t_binfo);
1514 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1515 int first_vfn_base_index = -1;
1516 bzero ((char *) b, sizeof (struct base_info));
1518 for (i = 0; i < n_baseclasses; i++)
1520 tree base_binfo = TREE_VEC_ELT (binfos, i);
1521 tree basetype = BINFO_TYPE (base_binfo);
1523 /* If the type of basetype is incomplete, then
1524 we already complained about that fact
1525 (and we should have fixed it up as well). */
1526 if (TYPE_SIZE (basetype) == 0)
1528 int j;
1529 /* The base type is of incomplete type. It is
1530 probably best to pretend that it does not
1531 exist. */
1532 if (i == n_baseclasses-1)
1533 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1534 TREE_VEC_LENGTH (binfos) -= 1;
1535 n_baseclasses -= 1;
1536 for (j = i; j+1 < n_baseclasses; j++)
1537 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1540 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1541 b->cant_have_const_ctor = 1;
1543 if (TYPE_HAS_CONSTRUCTOR (basetype)
1544 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1546 b->cant_have_default_ctor = 1;
1547 if (! TYPE_HAS_CONSTRUCTOR (t))
1549 cp_pedwarn ("base `%T' with only non-default constructor",
1550 basetype);
1551 cp_pedwarn ("in class without a constructor");
1555 if (TYPE_HAS_ASSIGN_REF (basetype)
1556 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1557 b->no_const_asn_ref = 1;
1559 b->n_ancestors += CLASSTYPE_N_SUPERCLASSES (basetype);
1560 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1561 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1562 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1563 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1565 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1566 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1567 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1569 if (! TREE_VIA_VIRTUAL (base_binfo)
1570 && BINFO_BASETYPES (base_binfo))
1572 tree base_binfos = BINFO_BASETYPES (base_binfo);
1573 tree chain = NULL_TREE;
1574 int j;
1576 /* Now unshare the structure beneath BASE_BINFO. */
1577 for (j = TREE_VEC_LENGTH (base_binfos)-1;
1578 j >= 0; j--)
1580 tree base_base_binfo = TREE_VEC_ELT (base_binfos, j);
1581 if (! TREE_VIA_VIRTUAL (base_base_binfo))
1582 TREE_VEC_ELT (base_binfos, j)
1583 = make_binfo (BINFO_OFFSET (base_base_binfo),
1584 base_base_binfo,
1585 BINFO_VTABLE (base_base_binfo),
1586 BINFO_VIRTUALS (base_base_binfo),
1587 chain);
1588 chain = TREE_VEC_ELT (base_binfos, j);
1589 TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo);
1590 TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo);
1591 BINFO_INHERITANCE_CHAIN (chain) = base_binfo;
1594 /* Completely unshare potentially shared data, and
1595 update what is ours. */
1596 propagate_binfo_offsets (base_binfo, BINFO_OFFSET (base_binfo));
1599 if (! TREE_VIA_VIRTUAL (base_binfo))
1600 CLASSTYPE_N_SUPERCLASSES (t) += 1;
1602 if (TYPE_VIRTUAL_P (basetype))
1604 /* Ensure that this is set from at least a virtual base
1605 class. */
1606 if (b->rtti == NULL_TREE)
1607 b->rtti = CLASSTYPE_RTTI (basetype);
1609 /* Don't borrow virtuals from virtual baseclasses. */
1610 if (TREE_VIA_VIRTUAL (base_binfo))
1611 continue;
1613 if (first_vfn_base_index < 0)
1615 tree vfields;
1616 first_vfn_base_index = i;
1618 /* Update these two, now that we know what vtable we are
1619 going to extend. This is so that we can add virtual
1620 functions, and override them properly. */
1621 BINFO_VTABLE (t_binfo) = TYPE_BINFO_VTABLE (basetype);
1622 BINFO_VIRTUALS (t_binfo) = TYPE_BINFO_VIRTUALS (basetype);
1623 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1624 b->vfield = CLASSTYPE_VFIELD (basetype);
1625 b->vfields = copy_list (CLASSTYPE_VFIELDS (basetype));
1626 vfields = b->vfields;
1627 while (vfields)
1629 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1630 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1632 tree value = VF_BASETYPE_VALUE (vfields);
1633 if (DECL_NAME (CLASSTYPE_VFIELD (value))
1634 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1635 VF_NORMAL_VALUE (b->vfields) = basetype;
1636 else
1637 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1639 vfields = TREE_CHAIN (vfields);
1641 CLASSTYPE_VFIELD (t) = b->vfield;
1643 else
1645 /* Only add unique vfields, and flatten them out as we go. */
1646 tree vfields = CLASSTYPE_VFIELDS (basetype);
1647 while (vfields)
1649 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1650 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1652 tree value = VF_BASETYPE_VALUE (vfields);
1653 b->vfields = tree_cons (base_binfo, value, b->vfields);
1654 if (DECL_NAME (CLASSTYPE_VFIELD (value))
1655 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1656 VF_NORMAL_VALUE (b->vfields) = basetype;
1657 else
1658 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1660 vfields = TREE_CHAIN (vfields);
1663 if (b->has_virtual == 0)
1665 first_vfn_base_index = i;
1667 /* Update these two, now that we know what vtable we are
1668 going to extend. This is so that we can add virtual
1669 functions, and override them properly. */
1670 BINFO_VTABLE (t_binfo) = TYPE_BINFO_VTABLE (basetype);
1671 BINFO_VIRTUALS (t_binfo) = TYPE_BINFO_VIRTUALS (basetype);
1672 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1673 b->vfield = CLASSTYPE_VFIELD (basetype);
1674 CLASSTYPE_VFIELD (t) = b->vfield;
1675 /* When we install the first one, set the VF_NORMAL_VALUE
1676 to be the current class, as this it is the most derived
1677 class. Hopefully, this is not set to something else
1678 later. (mrs) */
1679 vfields = b->vfields;
1680 while (vfields)
1682 if (DECL_NAME (CLASSTYPE_VFIELD (t))
1683 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1685 VF_NORMAL_VALUE (vfields) = t;
1686 /* There should only be one of them! And it should
1687 always be found, if we get into here. (mrs) */
1688 break;
1690 vfields = TREE_CHAIN (vfields);
1697 /* Must come after offsets are fixed for all bases. */
1698 for (i = 0; i < n_baseclasses; i++)
1700 tree base_binfo = TREE_VEC_ELT (binfos, i);
1701 tree basetype = BINFO_TYPE (base_binfo);
1703 if (get_base_distance (basetype, t_binfo, 0, (tree*)0) == -2)
1705 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
1706 basetype, t);
1710 tree v = get_vbase_types (t_binfo);
1712 for (; v; v = TREE_CHAIN (v))
1714 tree basetype = BINFO_TYPE (v);
1715 if (get_base_distance (basetype, t_binfo, 0, (tree*)0) == -2)
1717 if (extra_warnings)
1718 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
1719 basetype, t);
1725 tree vfields;
1726 /* Find the base class with the largest number of virtual functions. */
1727 for (vfields = b->vfields; vfields; vfields = TREE_CHAIN (vfields))
1729 if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)) > b->max_has_virtual)
1730 b->max_has_virtual = CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields));
1731 if (VF_DERIVED_VALUE (vfields)
1732 && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)) > b->max_has_virtual)
1733 b->max_has_virtual = CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields));
1737 if (b->vfield == 0)
1738 /* If all virtual functions come only from virtual baseclasses. */
1739 return -1;
1741 /* Update the rtti base if we have a non-virtual base class version
1742 of it. */
1743 b->rtti = CLASSTYPE_RTTI (BINFO_TYPE (TREE_VEC_ELT (binfos, first_vfn_base_index)));
1745 return first_vfn_base_index;
1748 static int
1749 typecode_p (type, code)
1750 tree type;
1751 enum tree_code code;
1753 return (TREE_CODE (type) == code
1754 || (TREE_CODE (type) == REFERENCE_TYPE
1755 && TREE_CODE (TREE_TYPE (type)) == code));
1758 /* Set memoizing fields and bits of T (and its variants) for later use.
1759 MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables. */
1761 static void
1762 finish_struct_bits (t, max_has_virtual)
1763 tree t;
1764 int max_has_virtual;
1766 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1768 /* Fix up variants (if any). */
1769 tree variants = TYPE_NEXT_VARIANT (t);
1770 while (variants)
1772 /* These fields are in the _TYPE part of the node, not in
1773 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1774 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1775 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1776 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1777 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
1779 TYPE_USES_COMPLEX_INHERITANCE (variants) = TYPE_USES_COMPLEX_INHERITANCE (t);
1780 TYPE_VIRTUAL_P (variants) = TYPE_VIRTUAL_P (t);
1781 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1782 /* Copy whatever these are holding today. */
1783 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1784 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1785 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1786 TYPE_SIZE (variants) = TYPE_SIZE (t);
1787 variants = TYPE_NEXT_VARIANT (variants);
1790 if (n_baseclasses && max_has_virtual)
1792 /* Done by `finish_struct' for classes without baseclasses. */
1793 int might_have_abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (t) != 0;
1794 tree binfos = TYPE_BINFO_BASETYPES (t);
1795 for (i = n_baseclasses-1; i >= 0; i--)
1797 might_have_abstract_virtuals
1798 |= (CLASSTYPE_ABSTRACT_VIRTUALS (BINFO_TYPE (TREE_VEC_ELT (binfos, i))) != 0);
1799 if (might_have_abstract_virtuals)
1800 break;
1802 if (might_have_abstract_virtuals)
1804 /* We use error_mark_node from override_one_vtable to signal
1805 an artificial abstract. */
1806 if (CLASSTYPE_ABSTRACT_VIRTUALS (t) == error_mark_node)
1807 CLASSTYPE_ABSTRACT_VIRTUALS (t) = NULL_TREE;
1808 CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
1812 if (n_baseclasses)
1814 /* Notice whether this class has type conversion functions defined. */
1815 tree binfo = TYPE_BINFO (t);
1816 tree binfos = BINFO_BASETYPES (binfo);
1817 tree basetype;
1819 for (i = n_baseclasses-1; i >= 0; i--)
1821 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1823 if (TYPE_HAS_CONVERSION (basetype))
1825 TYPE_HAS_CONVERSION (t) = 1;
1826 TYPE_HAS_INT_CONVERSION (t) |= TYPE_HAS_INT_CONVERSION (basetype);
1827 TYPE_HAS_REAL_CONVERSION (t) |= TYPE_HAS_REAL_CONVERSION (basetype);
1829 if (CLASSTYPE_MAX_DEPTH (basetype) >= CLASSTYPE_MAX_DEPTH (t))
1830 CLASSTYPE_MAX_DEPTH (t) = CLASSTYPE_MAX_DEPTH (basetype) + 1;
1834 /* If this type has a copy constructor, force its mode to be BLKmode, and
1835 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1836 be passed by invisible reference and prevent it from being returned in
1837 a register.
1839 Also do this if the class has BLKmode but can still be returned in
1840 registers, since function_cannot_inline_p won't let us inline
1841 functions returning such a type. This affects the HP-PA. */
1842 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1843 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1844 && CLASSTYPE_NON_AGGREGATE (t)))
1846 tree variants;
1847 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1848 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1850 TYPE_MODE (variants) = BLKmode;
1851 TREE_ADDRESSABLE (variants) = 1;
1856 /* Add FNDECL to the method_vec growing on the class_obstack. Used by
1857 finish_struct_methods. Note, FNDECL cannot be a constructor or
1858 destructor, those cases are handled by the caller. */
1860 static void
1861 grow_method (fndecl, method_vec_ptr)
1862 tree fndecl;
1863 tree *method_vec_ptr;
1865 tree method_vec = (tree)obstack_base (&class_obstack);
1867 /* Start off past the constructors and destructor. */
1868 tree *testp = &TREE_VEC_ELT (method_vec, 2);
1870 while (testp < (tree *) obstack_next_free (&class_obstack)
1871 && (*testp == NULL_TREE || DECL_NAME (*testp) != DECL_NAME (fndecl)))
1872 testp++;
1874 if (testp < (tree *) obstack_next_free (&class_obstack))
1876 tree *p;
1877 for (p = testp; *p; )
1878 p = &DECL_CHAIN (*p);
1879 *p = fndecl;
1881 else
1883 obstack_ptr_grow (&class_obstack, fndecl);
1884 *method_vec_ptr = (tree)obstack_base (&class_obstack);
1888 /* Warn about duplicate methods in fn_fields. Also compact method
1889 lists so that lookup can be made faster.
1891 Algorithm: Outer loop builds lists by method name. Inner loop
1892 checks for redundant method names within a list.
1894 Data Structure: List of method lists. The outer list is a
1895 TREE_LIST, whose TREE_PURPOSE field is the field name and the
1896 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
1897 links the entire list of methods for TYPE_METHODS. Friends are
1898 chained in the same way as member functions (? TREE_CHAIN or
1899 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
1900 list. That allows them to be quickly deleted, and requires no
1901 extra storage.
1903 If there are any constructors/destructors, they are moved to the
1904 front of the list. This makes pushclass more efficient.
1906 We also link each field which has shares a name with its baseclass
1907 to the head of the list of fields for that base class. This allows
1908 us to reduce search time in places like `build_method_call' to
1909 consider only reasonably likely functions. */
1911 tree
1912 finish_struct_methods (t, fn_fields, nonprivate_method)
1913 tree t;
1914 tree fn_fields;
1915 int nonprivate_method;
1917 tree method_vec;
1918 tree save_fn_fields = fn_fields;
1919 tree ctor_name = constructor_name (t);
1920 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1922 /* Now prepare to gather fn_fields into vector. */
1923 struct obstack *ambient_obstack = current_obstack;
1924 current_obstack = &class_obstack;
1925 method_vec = make_tree_vec (2);
1926 current_obstack = ambient_obstack;
1928 /* Now make this a live vector. */
1929 obstack_free (&class_obstack, method_vec);
1931 /* Save room for constructors and destructors. */
1932 obstack_blank (&class_obstack, sizeof (struct tree_vec) + sizeof (struct tree *));
1934 /* First fill in entry 0 with the constructors, entry 1 with destructors,
1935 and the next few with type conversion operators (if any). */
1937 for (; fn_fields; fn_fields = TREE_CHAIN (fn_fields))
1939 tree fn_name = DECL_NAME (fn_fields);
1941 /* Clear out this flag.
1943 @@ Doug may figure out how to break
1944 @@ this with nested classes and friends. */
1945 DECL_IN_AGGR_P (fn_fields) = 0;
1947 /* Note here that a copy ctor is private, so we don't dare generate
1948 a default copy constructor for a class that has a member
1949 of this type without making sure they have access to it. */
1950 if (fn_name == ctor_name)
1952 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
1953 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
1955 if (TREE_CODE (parmtype) == REFERENCE_TYPE
1956 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
1958 if (TREE_CHAIN (parmtypes) == NULL_TREE
1959 || TREE_CHAIN (parmtypes) == void_list_node
1960 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
1962 if (TREE_PROTECTED (fn_fields))
1963 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
1964 else if (TREE_PRIVATE (fn_fields))
1965 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
1968 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fn_fields)))
1970 /* Destructors go in slot 1. */
1971 DECL_CHAIN (fn_fields) = TREE_VEC_ELT (method_vec, 1);
1972 TREE_VEC_ELT (method_vec, 1) = fn_fields;
1974 else
1976 /* Constructors go in slot 0. */
1977 DECL_CHAIN (fn_fields) = TREE_VEC_ELT (method_vec, 0);
1978 TREE_VEC_ELT (method_vec, 0) = fn_fields;
1981 else if (IDENTIFIER_TYPENAME_P (fn_name))
1983 tree return_type = TREE_TYPE (TREE_TYPE (fn_fields));
1985 if (typecode_p (return_type, INTEGER_TYPE)
1986 || typecode_p (return_type, BOOLEAN_TYPE)
1987 || typecode_p (return_type, ENUMERAL_TYPE))
1988 TYPE_HAS_INT_CONVERSION (t) = 1;
1989 else if (typecode_p (return_type, REAL_TYPE))
1990 TYPE_HAS_REAL_CONVERSION (t) = 1;
1992 grow_method (fn_fields, &method_vec);
1996 fn_fields = save_fn_fields;
1997 for (; fn_fields; fn_fields = TREE_CHAIN (fn_fields))
1999 tree fn_name = DECL_NAME (fn_fields);
2001 if (fn_name == ctor_name || IDENTIFIER_TYPENAME_P (fn_name))
2002 continue;
2004 if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2006 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2008 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2010 if (TREE_PROTECTED (fn_fields))
2011 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2012 else if (TREE_PRIVATE (fn_fields))
2013 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2017 grow_method (fn_fields, &method_vec);
2020 TREE_VEC_LENGTH (method_vec) = (tree *)obstack_next_free (&class_obstack)
2021 - (&TREE_VEC_ELT (method_vec, 0));
2022 obstack_finish (&class_obstack);
2023 CLASSTYPE_METHOD_VEC (t) = method_vec;
2025 if (nonprivate_method == 0
2026 && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
2027 && DECL_FRIENDLIST (TYPE_MAIN_DECL (t)) == NULL_TREE)
2029 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2030 for (i = 0; i < n_baseclasses; i++)
2031 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2032 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2034 nonprivate_method = 1;
2035 break;
2037 if (nonprivate_method == 0
2038 && warn_ctor_dtor_privacy)
2039 cp_warning ("all member functions in class `%T' are private", t);
2042 /* Warn if all destructors are private (in which case this class is
2043 effectively unusable. */
2044 if (TYPE_HAS_DESTRUCTOR (t))
2046 tree dtor = TREE_VEC_ELT (method_vec, 1);
2048 /* Wild parse errors can cause this to happen. */
2049 if (dtor == NULL_TREE)
2050 TYPE_HAS_DESTRUCTOR (t) = 0;
2051 else if (TREE_PRIVATE (dtor)
2052 && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
2053 && DECL_FRIENDLIST (TYPE_MAIN_DECL (t)) == NULL_TREE
2054 && warn_ctor_dtor_privacy)
2055 cp_warning ("`%#T' only defines a private destructor and has no friends",
2059 /* Now for each member function (except for constructors and
2060 destructors), compute where member functions of the same
2061 name reside in base classes. */
2062 if (n_baseclasses != 0
2063 && TREE_VEC_LENGTH (method_vec) > 2)
2065 int len = TREE_VEC_LENGTH (method_vec);
2066 tree baselink_vec = make_tree_vec (len);
2067 int any_links = 0;
2068 tree baselink_binfo = build_tree_list (NULL_TREE, TYPE_BINFO (t));
2070 for (i = 2; i < len; i++)
2072 TREE_VEC_ELT (baselink_vec, i)
2073 = get_baselinks (baselink_binfo, t, DECL_NAME (TREE_VEC_ELT (method_vec, i)));
2074 if (TREE_VEC_ELT (baselink_vec, i) != 0)
2075 any_links = 1;
2077 if (any_links != 0)
2078 CLASSTYPE_BASELINK_VEC (t) = baselink_vec;
2079 else
2080 obstack_free (current_obstack, baselink_vec);
2083 /* Now, figure out what any member template specializations were
2084 specializing. */
2085 for (i = 0; i < TREE_VEC_LENGTH (method_vec); ++i)
2087 tree fn;
2088 for (fn = TREE_VEC_ELT (method_vec, i);
2089 fn != NULL_TREE;
2090 fn = DECL_CHAIN (fn))
2091 if (DECL_TEMPLATE_SPECIALIZATION (fn))
2093 tree f;
2094 tree spec_args;
2096 /* If there is a template, and t uses template parms, we
2097 are dealing with a specialization of a member
2098 template in a template class, and we must grab the
2099 template, rather than the function. */
2100 if (DECL_TI_TEMPLATE (fn) && uses_template_parms (t))
2101 f = DECL_TI_TEMPLATE (fn);
2102 else
2103 f = fn;
2105 /* We want the specialization arguments, which will be the
2106 innermost ones. */
2107 if (DECL_TI_ARGS (f)
2108 && TREE_CODE (DECL_TI_ARGS (f)) == TREE_VEC)
2109 spec_args
2110 = TREE_VEC_ELT (DECL_TI_ARGS (f), 0);
2111 else
2112 spec_args = DECL_TI_ARGS (f);
2114 check_explicit_specialization
2115 (lookup_template_function (DECL_NAME (f), spec_args),
2116 f, 0, 1);
2118 /* Now, the assembler name will be correct for fn, so we
2119 make its RTL. */
2120 DECL_RTL (f) = 0;
2121 make_decl_rtl (f, NULL_PTR, 1);
2122 if (f != fn)
2124 DECL_RTL (fn) = 0;
2125 make_decl_rtl (fn, NULL_PTR, 1);
2130 return method_vec;
2133 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2135 void
2136 duplicate_tag_error (t)
2137 tree t;
2139 cp_error ("redefinition of `%#T'", t);
2140 cp_error_at ("previous definition here", t);
2142 /* Pretend we haven't defined this type. */
2144 /* All of the component_decl's were TREE_CHAINed together in the parser.
2145 finish_struct_methods walks these chains and assembles all methods with
2146 the same base name into DECL_CHAINs. Now we don't need the parser chains
2147 anymore, so we unravel them. */
2149 /* This used to be in finish_struct, but it turns out that the
2150 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2151 things... */
2152 if (CLASSTYPE_METHOD_VEC (t))
2154 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2155 int i, len = TREE_VEC_LENGTH (method_vec);
2156 for (i = 0; i < len; i++)
2158 tree unchain = TREE_VEC_ELT (method_vec, i);
2159 while (unchain != NULL_TREE)
2161 TREE_CHAIN (unchain) = NULL_TREE;
2162 unchain = DECL_CHAIN (unchain);
2167 if (TYPE_LANG_SPECIFIC (t))
2169 tree as_list = CLASSTYPE_AS_LIST (t);
2170 tree binfo = TYPE_BINFO (t);
2171 tree binfo_as_list = CLASSTYPE_BINFO_AS_LIST (t);
2172 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2173 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2175 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2176 BINFO_BASETYPES(binfo) = NULL_TREE;
2178 CLASSTYPE_AS_LIST (t) = as_list;
2179 TYPE_BINFO (t) = binfo;
2180 CLASSTYPE_BINFO_AS_LIST (t) = binfo_as_list;
2181 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2182 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2183 CLASSTYPE_VBASE_SIZE (t) = integer_zero_node;
2184 TYPE_REDEFINED (t) = 1;
2186 TYPE_SIZE (t) = NULL_TREE;
2187 TYPE_MODE (t) = VOIDmode;
2188 TYPE_FIELDS (t) = NULL_TREE;
2189 TYPE_METHODS (t) = NULL_TREE;
2190 TYPE_VFIELD (t) = NULL_TREE;
2191 TYPE_CONTEXT (t) = NULL_TREE;
2194 /* finish up all new vtables. */
2196 static void
2197 finish_vtbls (binfo, do_self, t)
2198 tree binfo;
2199 int do_self;
2200 tree t;
2202 tree binfos = BINFO_BASETYPES (binfo);
2203 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2205 /* Should we use something besides CLASSTYPE_VFIELDS? */
2206 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2208 if (BINFO_NEW_VTABLE_MARKED (binfo))
2210 tree decl, context;
2212 decl = BINFO_VTABLE (binfo);
2213 context = DECL_CONTEXT (decl);
2214 DECL_CONTEXT (decl) = 0;
2215 if (write_virtuals >= 0
2216 && DECL_INITIAL (decl) != BINFO_VIRTUALS (binfo))
2217 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE,
2218 BINFO_VIRTUALS (binfo));
2219 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0);
2220 DECL_CONTEXT (decl) = context;
2222 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2225 for (i = 0; i < n_baselinks; i++)
2227 tree base_binfo = TREE_VEC_ELT (binfos, i);
2228 int is_not_base_vtable
2229 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2230 if (TREE_VIA_VIRTUAL (base_binfo))
2232 base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2234 finish_vtbls (base_binfo, is_not_base_vtable, t);
2238 /* True if we should override the given BASE_FNDECL with the given
2239 FNDECL. */
2241 static int
2242 overrides (fndecl, base_fndecl)
2243 tree fndecl, base_fndecl;
2245 /* Destructors have special names. */
2246 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2247 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2248 return 1;
2249 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2250 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2251 return 0;
2252 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2254 tree types, base_types;
2255 #if 0
2256 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2257 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2258 #endif
2259 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2260 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2261 if ((TYPE_READONLY (TREE_TYPE (TREE_VALUE (base_types)))
2262 == TYPE_READONLY (TREE_TYPE (TREE_VALUE (types))))
2263 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types), 3))
2264 return 1;
2266 return 0;
2269 static tree
2270 get_class_offset_1 (parent, binfo, context, t, fndecl)
2271 tree parent, binfo, context, t, fndecl;
2273 tree binfos = BINFO_BASETYPES (binfo);
2274 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2275 tree rval = NULL_TREE;
2277 if (binfo == parent)
2278 return error_mark_node;
2280 for (i = 0; i < n_baselinks; i++)
2282 tree base_binfo = TREE_VEC_ELT (binfos, i);
2283 tree nrval;
2285 if (TREE_VIA_VIRTUAL (base_binfo))
2286 base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2287 CLASSTYPE_VBASECLASSES (t));
2288 nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl);
2289 /* See if we have a new value */
2290 if (nrval && (nrval != error_mark_node || rval==0))
2292 /* Only compare if we have two offsets */
2293 if (rval && rval != error_mark_node
2294 && ! tree_int_cst_equal (nrval, rval))
2296 /* Only give error if the two offsets are different */
2297 error ("every virtual function must have a unique final overrider");
2298 cp_error (" found two (or more) `%T' class subobjects in `%T'", context, t);
2299 cp_error (" with virtual `%D' from virtual base class", fndecl);
2300 return rval;
2302 rval = nrval;
2305 if (rval && BINFO_TYPE (binfo) == context)
2307 my_friendly_assert (rval == error_mark_node
2308 || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999);
2309 rval = BINFO_OFFSET (binfo);
2312 return rval;
2315 /* Get the offset to the CONTEXT subobject that is related to the
2316 given BINFO. */
2318 static tree
2319 get_class_offset (context, t, binfo, fndecl)
2320 tree context, t, binfo, fndecl;
2322 tree first_binfo = binfo;
2323 tree offset;
2324 int i;
2326 if (context == t)
2327 return integer_zero_node;
2329 if (BINFO_TYPE (binfo) == context)
2330 return BINFO_OFFSET (binfo);
2332 /* Check less derived binfos first. */
2333 while (BINFO_BASETYPES (binfo)
2334 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
2336 tree binfos = BINFO_BASETYPES (binfo);
2337 binfo = TREE_VEC_ELT (binfos, i);
2338 if (BINFO_TYPE (binfo) == context)
2339 return BINFO_OFFSET (binfo);
2342 /* Ok, not found in the less derived binfos, now check the more
2343 derived binfos. */
2344 offset = get_class_offset_1 (first_binfo, TYPE_BINFO (t), context, t, fndecl);
2345 if (offset==0 || TREE_CODE (offset) != INTEGER_CST)
2346 my_friendly_abort (999); /* we have to find it. */
2347 return offset;
2350 /* Skip RTTI information at the front of the virtual list. */
2352 unsigned HOST_WIDE_INT
2353 skip_rtti_stuff (virtuals)
2354 tree *virtuals;
2356 int n;
2358 n = 0;
2359 if (*virtuals)
2361 /* We always reserve a slot for the offset/tdesc entry. */
2362 ++n;
2363 *virtuals = TREE_CHAIN (*virtuals);
2365 if (flag_vtable_thunks && *virtuals)
2367 /* The second slot is reserved for the tdesc pointer when thunks
2368 are used. */
2369 ++n;
2370 *virtuals = TREE_CHAIN (*virtuals);
2372 return n;
2375 static void
2376 modify_one_vtable (binfo, t, fndecl, pfn)
2377 tree binfo, t, fndecl, pfn;
2379 tree virtuals = BINFO_VIRTUALS (binfo);
2380 unsigned HOST_WIDE_INT n;
2382 /* update rtti entry */
2383 if (flag_rtti)
2385 if (binfo == TYPE_BINFO (t))
2387 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2388 build_vtable (TYPE_BINFO (DECL_CONTEXT (CLASSTYPE_VFIELD (t))), t);
2390 else
2392 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2393 prepare_fresh_vtable (binfo, t);
2396 if (fndecl == NULL_TREE)
2397 return;
2399 n = skip_rtti_stuff (&virtuals);
2401 while (virtuals)
2403 tree current_fndecl = TREE_VALUE (virtuals);
2404 current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl);
2405 current_fndecl = TREE_OPERAND (current_fndecl, 0);
2406 if (current_fndecl && overrides (fndecl, current_fndecl))
2408 tree base_offset, offset;
2409 tree context = DECL_CLASS_CONTEXT (fndecl);
2410 tree vfield = CLASSTYPE_VFIELD (t);
2411 tree this_offset;
2413 offset = get_class_offset (context, t, binfo, fndecl);
2415 /* Find the right offset for the this pointer based on the
2416 base class we just found. We have to take into
2417 consideration the virtual base class pointers that we
2418 stick in before the virtual function table pointer.
2420 Also, we want just the delta between the most base class
2421 that we derived this vfield from and us. */
2422 base_offset = size_binop (PLUS_EXPR,
2423 get_derived_offset (binfo, DECL_CONTEXT (current_fndecl)),
2424 BINFO_OFFSET (binfo));
2425 this_offset = size_binop (MINUS_EXPR, offset, base_offset);
2427 /* Make sure we can modify the derived association with immunity. */
2428 if (TREE_USED (binfo))
2429 my_friendly_assert (0, 999);
2431 if (binfo == TYPE_BINFO (t))
2433 /* In this case, it is *type*'s vtable we are modifying.
2434 We start with the approximation that it's vtable is that
2435 of the immediate base class. */
2436 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2437 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2439 else
2441 /* This is our very own copy of `basetype' to play with.
2442 Later, we will fill in all the virtual functions
2443 that override the virtual functions in these base classes
2444 which are not defined by the current type. */
2445 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2446 prepare_fresh_vtable (binfo, t);
2449 #ifdef NOTQUITE
2450 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
2451 #endif
2452 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2453 build_vtable_entry (this_offset, pfn),
2454 fndecl);
2456 ++n;
2457 virtuals = TREE_CHAIN (virtuals);
2461 /* These are the ones that are not through virtual base classes. */
2463 static void
2464 modify_all_direct_vtables (binfo, do_self, t, fndecl, pfn)
2465 tree binfo;
2466 int do_self;
2467 tree t, fndecl, pfn;
2469 tree binfos = BINFO_BASETYPES (binfo);
2470 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2472 /* Should we use something besides CLASSTYPE_VFIELDS? */
2473 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2475 modify_one_vtable (binfo, t, fndecl, pfn);
2478 for (i = 0; i < n_baselinks; i++)
2480 tree base_binfo = TREE_VEC_ELT (binfos, i);
2481 int is_not_base_vtable
2482 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2483 if (! TREE_VIA_VIRTUAL (base_binfo))
2484 modify_all_direct_vtables (base_binfo, is_not_base_vtable, t, fndecl, pfn);
2488 /* Fixup all the delta entries in this one vtable that need updating. */
2490 static void
2491 fixup_vtable_deltas1 (binfo, t)
2492 tree binfo, t;
2494 tree virtuals = BINFO_VIRTUALS (binfo);
2495 unsigned HOST_WIDE_INT n;
2497 n = skip_rtti_stuff (&virtuals);
2499 while (virtuals)
2501 tree fndecl = TREE_VALUE (virtuals);
2502 tree pfn = FNADDR_FROM_VTABLE_ENTRY (fndecl);
2503 tree delta = DELTA_FROM_VTABLE_ENTRY (fndecl);
2504 fndecl = TREE_OPERAND (pfn, 0);
2505 if (fndecl)
2507 tree base_offset, offset;
2508 tree context = DECL_CLASS_CONTEXT (fndecl);
2509 tree vfield = CLASSTYPE_VFIELD (t);
2510 tree this_offset;
2512 offset = get_class_offset (context, t, binfo, fndecl);
2514 /* Find the right offset for the this pointer based on the
2515 base class we just found. We have to take into
2516 consideration the virtual base class pointers that we
2517 stick in before the virtual function table pointer.
2519 Also, we want just the delta between the most base class
2520 that we derived this vfield from and us. */
2521 base_offset = size_binop (PLUS_EXPR,
2522 get_derived_offset (binfo, DECL_CONTEXT (fndecl)),
2523 BINFO_OFFSET (binfo));
2524 this_offset = size_binop (MINUS_EXPR, offset, base_offset);
2526 if (! tree_int_cst_equal (this_offset, delta))
2528 /* Make sure we can modify the derived association with immunity. */
2529 if (TREE_USED (binfo))
2530 my_friendly_assert (0, 999);
2532 if (binfo == TYPE_BINFO (t))
2534 /* In this case, it is *type*'s vtable we are modifying.
2535 We start with the approximation that it's vtable is that
2536 of the immediate base class. */
2537 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2538 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2540 else
2542 /* This is our very own copy of `basetype' to play with.
2543 Later, we will fill in all the virtual functions
2544 that override the virtual functions in these base classes
2545 which are not defined by the current type. */
2546 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2547 prepare_fresh_vtable (binfo, t);
2550 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2551 build_vtable_entry (this_offset, pfn),
2552 fndecl);
2555 ++n;
2556 virtuals = TREE_CHAIN (virtuals);
2560 /* Fixup all the delta entries in all the direct vtables that need updating.
2561 This happens when we have non-overridden virtual functions from a
2562 virtual base class, that are at a different offset, in the new
2563 hierarchy, because the layout of the virtual bases has changed. */
2565 static void
2566 fixup_vtable_deltas (binfo, init_self, t)
2567 tree binfo;
2568 int init_self;
2569 tree t;
2571 tree binfos = BINFO_BASETYPES (binfo);
2572 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2574 for (i = 0; i < n_baselinks; i++)
2576 tree base_binfo = TREE_VEC_ELT (binfos, i);
2577 int is_not_base_vtable
2578 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2579 if (! TREE_VIA_VIRTUAL (base_binfo))
2580 fixup_vtable_deltas (base_binfo, is_not_base_vtable, t);
2582 /* Should we use something besides CLASSTYPE_VFIELDS? */
2583 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2585 fixup_vtable_deltas1 (binfo, t);
2589 /* These are the ones that are through virtual base classes. */
2591 static void
2592 modify_all_indirect_vtables (binfo, do_self, via_virtual, t, fndecl, pfn)
2593 tree binfo;
2594 int do_self, via_virtual;
2595 tree t, fndecl, pfn;
2597 tree binfos = BINFO_BASETYPES (binfo);
2598 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2600 /* Should we use something besides CLASSTYPE_VFIELDS? */
2601 if (do_self && via_virtual && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2603 modify_one_vtable (binfo, t, fndecl, pfn);
2606 for (i = 0; i < n_baselinks; i++)
2608 tree base_binfo = TREE_VEC_ELT (binfos, i);
2609 int is_not_base_vtable
2610 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2611 if (TREE_VIA_VIRTUAL (base_binfo))
2613 via_virtual = 1;
2614 base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2616 modify_all_indirect_vtables (base_binfo, is_not_base_vtable, via_virtual, t, fndecl, pfn);
2620 static void
2621 modify_all_vtables (t, fndecl, vfn)
2622 tree t, fndecl, vfn;
2624 /* Do these first, so that we will make use of any non-virtual class's
2625 vtable, over a virtual classes vtable. */
2626 modify_all_direct_vtables (TYPE_BINFO (t), 1, t, fndecl, vfn);
2627 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
2628 modify_all_indirect_vtables (TYPE_BINFO (t), 1, 0, t, fndecl, vfn);
2631 /* Here, we already know that they match in every respect.
2632 All we have to check is where they had their declarations. */
2634 static int
2635 strictly_overrides (fndecl1, fndecl2)
2636 tree fndecl1, fndecl2;
2638 int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
2639 DECL_CLASS_CONTEXT (fndecl1),
2640 0, (tree *)0);
2641 if (distance == -2 || distance > 0)
2642 return 1;
2643 return 0;
2646 /* Merge overrides for one vtable.
2647 If we want to merge in same function, we are fine.
2648 else
2649 if one has a DECL_CLASS_CONTEXT that is a parent of the
2650 other, than choose the more derived one
2651 else
2652 potentially ill-formed (see 10.3 [class.virtual])
2653 we have to check later to see if there was an
2654 override in this class. If there was ok, if not
2655 then it is ill-formed. (mrs)
2657 We take special care to reuse a vtable, if we can. */
2659 static void
2660 override_one_vtable (binfo, old, t)
2661 tree binfo, old, t;
2663 tree virtuals = BINFO_VIRTUALS (binfo);
2664 tree old_virtuals = BINFO_VIRTUALS (old);
2665 enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
2667 /* If we have already committed to modifying it, then don't try and
2668 reuse another vtable. */
2669 if (BINFO_NEW_VTABLE_MARKED (binfo))
2670 choose = NEITHER;
2672 skip_rtti_stuff (&virtuals);
2673 skip_rtti_stuff (&old_virtuals);
2675 while (virtuals)
2677 tree fndecl = TREE_VALUE (virtuals);
2678 tree old_fndecl = TREE_VALUE (old_virtuals);
2679 fndecl = FNADDR_FROM_VTABLE_ENTRY (fndecl);
2680 old_fndecl = FNADDR_FROM_VTABLE_ENTRY (old_fndecl);
2681 fndecl = TREE_OPERAND (fndecl, 0);
2682 old_fndecl = TREE_OPERAND (old_fndecl, 0);
2683 /* First check to see if they are the same. */
2684 if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
2686 /* No need to do anything. */
2688 else if (strictly_overrides (fndecl, old_fndecl))
2690 if (choose == UNDECIDED)
2691 choose = REUSE_NEW;
2692 else if (choose == REUSE_OLD)
2694 choose = NEITHER;
2695 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2697 prepare_fresh_vtable (binfo, t);
2698 override_one_vtable (binfo, old, t);
2699 return;
2703 else if (strictly_overrides (old_fndecl, fndecl))
2705 if (choose == UNDECIDED)
2706 choose = REUSE_OLD;
2707 else if (choose == REUSE_NEW)
2709 choose = NEITHER;
2710 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2712 prepare_fresh_vtable (binfo, t);
2713 override_one_vtable (binfo, old, t);
2714 return;
2716 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2718 else if (choose == NEITHER)
2720 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2723 else
2725 choose = NEITHER;
2726 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2728 prepare_fresh_vtable (binfo, t);
2729 override_one_vtable (binfo, old, t);
2730 return;
2733 /* This MUST be overridden, or the class is ill-formed. */
2734 /* For now, we just make it abstract. */
2735 tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
2736 tree vfn;
2738 fndecl = copy_node (fndecl);
2739 copy_lang_decl (fndecl);
2740 DECL_ABSTRACT_VIRTUAL_P (fndecl) = 1;
2741 /* Make sure we search for it later. */
2742 if (! CLASSTYPE_ABSTRACT_VIRTUALS (t))
2743 CLASSTYPE_ABSTRACT_VIRTUALS (t) = error_mark_node;
2745 vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
2746 TREE_CONSTANT (vfn) = 1;
2748 /* We can use integer_zero_node, as we will will core dump
2749 if this is used anyway. */
2750 TREE_VALUE (virtuals) = build_vtable_entry (integer_zero_node, vfn);
2753 virtuals = TREE_CHAIN (virtuals);
2754 old_virtuals = TREE_CHAIN (old_virtuals);
2757 /* Let's reuse the old vtable. */
2758 if (choose == REUSE_OLD)
2760 BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
2761 BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
2765 /* Merge in overrides for virtual bases.
2766 BINFO is the hierarchy we want to modify, and OLD has the potential
2767 overrides. */
2769 static void
2770 merge_overrides (binfo, old, do_self, t)
2771 tree binfo, old;
2772 int do_self;
2773 tree t;
2775 tree binfos = BINFO_BASETYPES (binfo);
2776 tree old_binfos = BINFO_BASETYPES (old);
2777 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2779 /* Should we use something besides CLASSTYPE_VFIELDS? */
2780 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2782 override_one_vtable (binfo, old, t);
2785 for (i = 0; i < n_baselinks; i++)
2787 tree base_binfo = TREE_VEC_ELT (binfos, i);
2788 tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
2789 int is_not_base_vtable
2790 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2791 if (! TREE_VIA_VIRTUAL (base_binfo))
2792 merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
2796 /* Get the base virtual function declarations in T that are either
2797 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2798 the overrider/hider. */
2800 static tree
2801 get_basefndecls (fndecl, t)
2802 tree fndecl, t;
2804 tree methods = TYPE_METHODS (t);
2805 tree base_fndecls = NULL_TREE;
2806 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2807 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2809 while (methods)
2811 if (TREE_CODE (methods) == FUNCTION_DECL
2812 && DECL_VINDEX (methods) != NULL_TREE
2813 && DECL_NAME (fndecl) == DECL_NAME (methods))
2814 base_fndecls = temp_tree_cons (fndecl, methods, base_fndecls);
2816 methods = TREE_CHAIN (methods);
2819 if (base_fndecls)
2820 return base_fndecls;
2822 for (i = 0; i < n_baseclasses; i++)
2824 tree base_binfo = TREE_VEC_ELT (binfos, i);
2825 tree basetype = BINFO_TYPE (base_binfo);
2827 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2828 base_fndecls);
2831 return base_fndecls;
2834 /* Mark the functions that have been hidden with their overriders.
2835 Since we start out with all functions already marked with a hider,
2836 no need to mark functions that are just hidden. */
2838 static void
2839 mark_overriders (fndecl, base_fndecls)
2840 tree fndecl, base_fndecls;
2842 while (base_fndecls)
2844 if (overrides (TREE_VALUE (base_fndecls), fndecl))
2845 TREE_PURPOSE (base_fndecls) = fndecl;
2847 base_fndecls = TREE_CHAIN (base_fndecls);
2851 /* If this declaration supersedes the declaration of
2852 a method declared virtual in the base class, then
2853 mark this field as being virtual as well. */
2855 static void
2856 check_for_override (decl, ctype)
2857 tree decl, ctype;
2859 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
2860 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2861 int virtualp = DECL_VIRTUAL_P (decl);
2863 for (i = 0; i < n_baselinks; i++)
2865 tree base_binfo = TREE_VEC_ELT (binfos, i);
2866 if (TYPE_VIRTUAL_P (BINFO_TYPE (base_binfo))
2867 || flag_all_virtual == 1)
2869 tree tmp = get_matching_virtual
2870 (base_binfo, decl,
2871 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
2872 if (tmp)
2874 /* If this function overrides some virtual in some base
2875 class, then the function itself is also necessarily
2876 virtual, even if the user didn't explicitly say so. */
2877 DECL_VIRTUAL_P (decl) = 1;
2879 /* The TMP we really want is the one from the deepest
2880 baseclass on this path, taking care not to
2881 duplicate if we have already found it (via another
2882 path to its virtual baseclass. */
2883 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
2885 cp_error_at ("method `%D' may not be declared static",
2886 decl);
2887 cp_error_at ("(since `%D' declared virtual in base class.)",
2888 tmp);
2889 break;
2891 virtualp = 1;
2893 #if 0 /* The signature of an overriding function is not changed. */
2895 /* The argument types may have changed... */
2896 tree type = TREE_TYPE (decl);
2897 tree argtypes = TYPE_ARG_TYPES (type);
2898 tree base_variant = TREE_TYPE (TREE_VALUE (argtypes));
2899 tree raises = TYPE_RAISES_EXCEPTIONS (type);
2901 argtypes = commonparms (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (tmp))),
2902 TREE_CHAIN (argtypes));
2903 /* But the return type has not. */
2904 type = build_cplus_method_type (base_variant, TREE_TYPE (type), argtypes);
2905 if (raises)
2906 type = build_exception_variant (type, raises);
2907 TREE_TYPE (decl) = type;
2909 #endif
2910 DECL_VINDEX (decl)
2911 = tree_cons (NULL_TREE, tmp, DECL_VINDEX (decl));
2912 break;
2916 if (virtualp)
2918 if (DECL_VINDEX (decl) == NULL_TREE)
2919 DECL_VINDEX (decl) = error_mark_node;
2920 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2924 /* Warn about hidden virtual functions that are not overridden in t.
2925 We know that constructors and destructors don't apply. */
2927 void
2928 warn_hidden (t)
2929 tree t;
2931 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2932 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2933 int i;
2935 /* We go through each separately named virtual function. */
2936 for (i = 2; i < n_methods; ++i)
2938 tree fndecl = TREE_VEC_ELT (method_vec, i);
2940 tree base_fndecls = NULL_TREE;
2941 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2942 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2944 if (DECL_VINDEX (fndecl) == NULL_TREE)
2945 continue;
2947 /* First we get a list of all possible functions that might be
2948 hidden from each base class. */
2949 for (i = 0; i < n_baseclasses; i++)
2951 tree base_binfo = TREE_VEC_ELT (binfos, i);
2952 tree basetype = BINFO_TYPE (base_binfo);
2954 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2955 base_fndecls);
2958 if (TREE_CHAIN (fndecl)
2959 && DECL_NAME (TREE_CHAIN (fndecl)) == DECL_NAME (fndecl))
2960 fndecl = TREE_CHAIN (fndecl);
2961 else
2962 fndecl = NULL_TREE;
2964 /* ...then mark up all the base functions with overriders, preferring
2965 overriders to hiders. */
2966 if (base_fndecls)
2967 while (fndecl)
2969 mark_overriders (fndecl, base_fndecls);
2971 if (TREE_CHAIN (fndecl)
2972 && DECL_NAME (TREE_CHAIN (fndecl)) == DECL_NAME (fndecl))
2973 fndecl = TREE_CHAIN (fndecl);
2974 else
2975 fndecl = NULL_TREE;
2978 /* Now give a warning for all base functions without overriders,
2979 as they are hidden. */
2980 while (base_fndecls)
2982 if (! overrides (TREE_VALUE (base_fndecls),
2983 TREE_PURPOSE (base_fndecls)))
2985 /* Here we know it is a hider, and no overrider exists. */
2986 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2987 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
2990 base_fndecls = TREE_CHAIN (base_fndecls);
2995 /* Check for things that are invalid. There are probably plenty of other
2996 things we should check for also. */
2998 static void
2999 finish_struct_anon (t)
3000 tree t;
3002 tree field;
3003 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3005 if (TREE_STATIC (field))
3006 continue;
3007 if (TREE_CODE (field) != FIELD_DECL)
3008 continue;
3010 if (DECL_NAME (field) == NULL_TREE
3011 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
3013 tree* uelt = &TYPE_FIELDS (TREE_TYPE (field));
3014 for (; *uelt; uelt = &TREE_CHAIN (*uelt))
3016 if (TREE_CODE (*uelt) != FIELD_DECL)
3017 continue;
3019 if (TREE_PRIVATE (*uelt))
3020 cp_pedwarn_at ("private member `%#D' in anonymous union",
3021 *uelt);
3022 else if (TREE_PROTECTED (*uelt))
3023 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3024 *uelt);
3026 TREE_PRIVATE (*uelt) = TREE_PRIVATE (field);
3027 TREE_PROTECTED (*uelt) = TREE_PROTECTED (field);
3033 extern int interface_only, interface_unknown;
3035 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
3036 (or C++ class declaration).
3038 For C++, we must handle the building of derived classes.
3039 Also, C++ allows static class members. The way that this is
3040 handled is to keep the field name where it is (as the DECL_NAME
3041 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
3042 of the field. layout_record and layout_union will know about this.
3044 More C++ hair: inline functions have text in their
3045 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
3046 meaningful tree structure. After the struct has been laid out, set
3047 things up so that this can happen.
3049 And still more: virtual functions. In the case of single inheritance,
3050 when a new virtual function is seen which redefines a virtual function
3051 from the base class, the new virtual function is placed into
3052 the virtual function table at exactly the same address that
3053 it had in the base class. When this is extended to multiple
3054 inheritance, the same thing happens, except that multiple virtual
3055 function tables must be maintained. The first virtual function
3056 table is treated in exactly the same way as in the case of single
3057 inheritance. Additional virtual function tables have different
3058 DELTAs, which tell how to adjust `this' to point to the right thing.
3060 LIST_OF_FIELDLISTS is just that. The elements of the list are
3061 TREE_LIST elements, whose TREE_PURPOSE field tells what access
3062 the list has, and the TREE_VALUE slot gives the actual fields.
3064 ATTRIBUTES is the set of decl attributes to be applied, if any.
3066 If flag_all_virtual == 1, then we lay all functions into
3067 the virtual function table, as though they were declared
3068 virtual. Constructors do not lay down in the virtual function table.
3070 If flag_all_virtual == 2, then we lay all functions into
3071 the virtual function table, such that virtual functions
3072 occupy a space by themselves, and then all functions
3073 of the class occupy a space by themselves. This is illustrated
3074 in the following diagram:
3076 class A; class B : A;
3078 Class A's vtbl: Class B's vtbl:
3079 --------------------------------------------------------------------
3080 | A's virtual functions| | B's virtual functions |
3081 | | | (may inherit some from A). |
3082 --------------------------------------------------------------------
3083 | All of A's functions | | All of A's functions |
3084 | (such as a->A::f). | | (such as b->A::f) |
3085 --------------------------------------------------------------------
3086 | B's new virtual functions |
3087 | (not defined in A.) |
3088 -------------------------------
3089 | All of B's functions |
3090 | (such as b->B::f) |
3091 -------------------------------
3093 this allows the program to make references to any function, virtual
3094 or otherwise in a type-consistent manner. */
3096 tree
3097 finish_struct_1 (t, warn_anon)
3098 tree t;
3099 int warn_anon;
3101 int old;
3102 tree name = TYPE_IDENTIFIER (t);
3103 enum tree_code code = TREE_CODE (t);
3104 tree fields = TYPE_FIELDS (t);
3105 tree fn_fields = TYPE_METHODS (t);
3106 tree x, last_x, method_vec;
3107 int all_virtual;
3108 int has_virtual;
3109 int max_has_virtual;
3110 tree pending_virtuals = NULL_TREE;
3111 tree pending_hard_virtuals = NULL_TREE;
3112 tree abstract_virtuals = NULL_TREE;
3113 tree vfield;
3114 tree vfields;
3115 int cant_have_default_ctor;
3116 int cant_have_const_ctor;
3117 int no_const_asn_ref;
3119 /* The index of the first base class which has virtual
3120 functions. Only applied to non-virtual baseclasses. */
3121 int first_vfn_base_index;
3123 int n_baseclasses;
3124 int any_default_members = 0;
3125 int const_sans_init = 0;
3126 int ref_sans_init = 0;
3127 int nonprivate_method = 0;
3128 tree t_binfo = TYPE_BINFO (t);
3129 tree access_decls = NULL_TREE;
3130 int aggregate = 1;
3131 int empty = 1;
3132 int has_pointers = 0;
3134 if (warn_anon && code != UNION_TYPE && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
3135 pedwarn ("anonymous class type not used to declare any objects");
3137 if (TYPE_SIZE (t))
3139 if (IS_AGGR_TYPE (t))
3140 cp_error ("redefinition of `%#T'", t);
3141 else
3142 my_friendly_abort (172);
3143 popclass (0);
3144 return t;
3147 GNU_xref_decl (current_function_decl, t);
3149 /* If this type was previously laid out as a forward reference,
3150 make sure we lay it out again. */
3152 TYPE_SIZE (t) = NULL_TREE;
3153 CLASSTYPE_GOT_SEMICOLON (t) = 0;
3155 #if 0
3156 /* This is in general too late to do this. I moved the main case up to
3157 left_curly, what else needs to move? */
3158 if (! IS_SIGNATURE (t))
3160 my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t) == interface_only, 999);
3161 my_friendly_assert (CLASSTYPE_INTERFACE_KNOWN (t) == ! interface_unknown, 999);
3163 #endif
3165 #if 0
3166 if (flag_rtti)
3167 build_t_desc (t, 0);
3168 #endif
3170 TYPE_BINFO (t) = NULL_TREE;
3172 old = suspend_momentary ();
3174 /* Install struct as DECL_FIELD_CONTEXT of each field decl.
3175 Also process specified field sizes.
3176 Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
3177 The specified size is found in the DECL_INITIAL.
3178 Store 0 there, except for ": 0" fields (so we can find them
3179 and delete them, below). */
3181 if (t_binfo && BINFO_BASETYPES (t_binfo))
3182 n_baseclasses = TREE_VEC_LENGTH (BINFO_BASETYPES (t_binfo));
3183 else
3184 n_baseclasses = 0;
3186 if (n_baseclasses > 0)
3188 struct base_info base_info;
3190 /* If using multiple inheritance, this may cause variants of our
3191 basetypes to be used (instead of their canonical forms). */
3192 tree vf = layout_basetypes (t, BINFO_BASETYPES (t_binfo));
3193 last_x = tree_last (vf);
3194 fields = chainon (vf, fields);
3196 first_vfn_base_index = finish_base_struct (t, &base_info, t_binfo);
3197 /* Remember where we got our vfield from. */
3198 CLASSTYPE_VFIELD_PARENT (t) = first_vfn_base_index;
3199 has_virtual = base_info.has_virtual;
3200 max_has_virtual = base_info.max_has_virtual;
3201 CLASSTYPE_N_SUPERCLASSES (t) += base_info.n_ancestors;
3202 vfield = base_info.vfield;
3203 vfields = base_info.vfields;
3204 CLASSTYPE_RTTI (t) = base_info.rtti;
3205 cant_have_default_ctor = base_info.cant_have_default_ctor;
3206 cant_have_const_ctor = base_info.cant_have_const_ctor;
3207 no_const_asn_ref = base_info.no_const_asn_ref;
3208 n_baseclasses = TREE_VEC_LENGTH (BINFO_BASETYPES (t_binfo));
3209 aggregate = 0;
3211 else
3213 first_vfn_base_index = -1;
3214 has_virtual = 0;
3215 max_has_virtual = has_virtual;
3216 vfield = NULL_TREE;
3217 vfields = NULL_TREE;
3218 CLASSTYPE_RTTI (t) = NULL_TREE;
3219 last_x = NULL_TREE;
3220 cant_have_default_ctor = 0;
3221 cant_have_const_ctor = 0;
3222 no_const_asn_ref = 0;
3225 #if 0
3226 /* Both of these should be done before now. */
3227 if (write_virtuals == 3 && CLASSTYPE_INTERFACE_KNOWN (t)
3228 && ! IS_SIGNATURE (t))
3230 my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t) == interface_only, 999);
3231 my_friendly_assert (CLASSTYPE_VTABLE_NEEDS_WRITING (t) == ! interface_only, 999);
3233 #endif
3235 /* The three of these are approximations which may later be
3236 modified. Needed at this point to make add_virtual_function
3237 and modify_vtable_entries work. */
3238 TREE_CHAIN (t_binfo) = TYPE_BINFO (t);
3239 TYPE_BINFO (t) = t_binfo;
3240 CLASSTYPE_VFIELDS (t) = vfields;
3241 CLASSTYPE_VFIELD (t) = vfield;
3243 if (IS_SIGNATURE (t))
3244 all_virtual = 0;
3245 else if (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (t))
3246 all_virtual = 1;
3247 else
3248 all_virtual = 0;
3250 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3252 GNU_xref_member (current_class_name, x);
3254 nonprivate_method |= ! TREE_PRIVATE (x);
3256 /* If this was an evil function, don't keep it in class. */
3257 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3258 continue;
3260 DECL_CLASS_CONTEXT (x) = t;
3262 /* Do both of these, even though they're in the same union;
3263 if the insn `r' member and the size `i' member are
3264 different sizes, as on the alpha, the larger of the two
3265 will end up with garbage in it. */
3266 DECL_SAVED_INSNS (x) = NULL_RTX;
3267 DECL_FIELD_SIZE (x) = 0;
3269 check_for_override (x, t);
3270 if (DECL_ABSTRACT_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3271 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3273 /* The name of the field is the original field name
3274 Save this in auxiliary field for later overloading. */
3275 if (DECL_VINDEX (x)
3276 || (all_virtual == 1 && ! DECL_CONSTRUCTOR_P (x)))
3278 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3279 &has_virtual, x, t);
3280 if (DECL_ABSTRACT_VIRTUAL_P (x))
3281 abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
3282 #if 0
3283 /* XXX Why did I comment this out? (jason) */
3284 else
3285 TREE_USED (x) = 1;
3286 #endif
3290 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
3292 GNU_xref_member (current_class_name, x);
3294 if (TREE_CODE (x) == FIELD_DECL)
3296 DECL_PACKED (x) |= TYPE_PACKED (t);
3297 empty = 0;
3300 /* Handle access declarations. */
3301 if (TREE_CODE (x) == USING_DECL)
3303 tree ctype = DECL_INITIAL (x);
3304 tree sname = DECL_NAME (x);
3305 tree access
3306 = TREE_PRIVATE (x) ? access_private_node
3307 : TREE_PROTECTED (x) ? access_protected_node
3308 : access_public_node;
3309 tree fdecl, binfo;
3311 if (last_x)
3312 TREE_CHAIN (last_x) = TREE_CHAIN (x);
3313 else
3314 fields = TREE_CHAIN (x);
3316 binfo = binfo_or_else (ctype, t);
3317 if (! binfo)
3318 continue;
3320 if (sname == constructor_name (ctype)
3321 || sname == constructor_name_full (ctype))
3322 cp_error_at ("using-declaration for constructor", x);
3324 fdecl = lookup_field (binfo, sname, 0, 0);
3325 if (! fdecl)
3326 fdecl = lookup_fnfields (binfo, sname, 0);
3328 if (fdecl)
3329 access_decls = scratch_tree_cons (access, fdecl, access_decls);
3330 else
3331 cp_error_at ("no members matching `%D' in `%#T'", x, ctype);
3332 continue;
3335 last_x = x;
3337 if (TREE_CODE (x) == TYPE_DECL)
3338 continue;
3340 /* If we've gotten this far, it's a data member, possibly static,
3341 or an enumerator. */
3343 DECL_FIELD_CONTEXT (x) = t;
3345 /* ``A local class cannot have static data members.'' ARM 9.4 */
3346 if (current_function_decl && TREE_STATIC (x))
3347 cp_error_at ("field `%D' in local class cannot be static", x);
3349 /* Perform error checking that did not get done in
3350 grokdeclarator. */
3351 if (TREE_CODE (TREE_TYPE (x)) == FUNCTION_TYPE)
3353 cp_error_at ("field `%D' invalidly declared function type",
3355 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3357 else if (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE)
3359 cp_error_at ("field `%D' invalidly declared method type", x);
3360 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3362 else if (TREE_CODE (TREE_TYPE (x)) == OFFSET_TYPE)
3364 cp_error_at ("field `%D' invalidly declared offset type", x);
3365 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3368 #if 0
3369 if (DECL_NAME (x) == constructor_name (t))
3370 cant_have_default_ctor = 1;
3371 #endif
3373 if (TREE_TYPE (x) == error_mark_node)
3374 continue;
3376 DECL_SAVED_INSNS (x) = NULL_RTX;
3377 DECL_FIELD_SIZE (x) = 0;
3379 /* When this goes into scope, it will be a non-local reference. */
3380 DECL_NONLOCAL (x) = 1;
3382 if (TREE_CODE (x) == CONST_DECL)
3383 continue;
3385 if (TREE_CODE (x) == VAR_DECL)
3387 if (TREE_CODE (t) == UNION_TYPE)
3388 /* Unions cannot have static members. */
3389 cp_error_at ("field `%D' declared static in union", x);
3391 continue;
3394 /* Now it can only be a FIELD_DECL. */
3396 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3397 aggregate = 0;
3399 /* If this is of reference type, check if it needs an init.
3400 Also do a little ANSI jig if necessary. */
3401 if (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE)
3403 if (DECL_INITIAL (x) == NULL_TREE)
3404 ref_sans_init = 1;
3406 /* ARM $12.6.2: [A member initializer list] (or, for an
3407 aggregate, initialization by a brace-enclosed list) is the
3408 only way to initialize nonstatic const and reference
3409 members. */
3410 cant_have_default_ctor = 1;
3411 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3413 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3415 if (DECL_NAME (x))
3416 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3417 else
3418 cp_warning_at ("non-static reference in class without a constructor", x);
3422 if (TREE_CODE (TREE_TYPE (x)) == POINTER_TYPE)
3423 has_pointers = 1;
3425 /* If any field is const, the structure type is pseudo-const. */
3426 if (TREE_READONLY (x))
3428 C_TYPE_FIELDS_READONLY (t) = 1;
3429 if (DECL_INITIAL (x) == NULL_TREE)
3430 const_sans_init = 1;
3432 /* ARM $12.6.2: [A member initializer list] (or, for an
3433 aggregate, initialization by a brace-enclosed list) is the
3434 only way to initialize nonstatic const and reference
3435 members. */
3436 cant_have_default_ctor = 1;
3437 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3439 if (! TYPE_HAS_CONSTRUCTOR (t) && !IS_SIGNATURE (t)
3440 && extra_warnings)
3442 if (DECL_NAME (x))
3443 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3444 else
3445 cp_warning_at ("non-static const member in class without a constructor", x);
3448 else
3450 /* A field that is pseudo-const makes the structure
3451 likewise. */
3452 tree t1 = TREE_TYPE (x);
3453 while (TREE_CODE (t1) == ARRAY_TYPE)
3454 t1 = TREE_TYPE (t1);
3455 if (IS_AGGR_TYPE (t1))
3457 if (C_TYPE_FIELDS_READONLY (t1))
3458 C_TYPE_FIELDS_READONLY (t) = 1;
3459 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1))
3460 const_sans_init = 1;
3464 /* We set DECL_BIT_FIELD tentatively in grokbitfield.
3465 If the type and width are valid, we'll keep it set.
3466 Otherwise, the flag is cleared. */
3467 if (DECL_BIT_FIELD (x))
3469 DECL_BIT_FIELD (x) = 0;
3470 /* Invalid bit-field size done by grokfield. */
3471 /* Detect invalid bit-field type. */
3472 if (DECL_INITIAL (x)
3473 && ! INTEGRAL_TYPE_P (TREE_TYPE (x)))
3475 cp_error_at ("bit-field `%#D' with non-integral type", x);
3476 DECL_INITIAL (x) = NULL;
3479 /* Detect and ignore out of range field width. */
3480 if (DECL_INITIAL (x))
3482 tree w = DECL_INITIAL (x);
3483 register int width;
3485 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3486 STRIP_NOPS (w);
3488 /* detect invalid field size. */
3489 if (TREE_CODE (w) == CONST_DECL)
3490 w = DECL_INITIAL (w);
3491 else if (TREE_READONLY_DECL_P (w))
3492 w = decl_constant_value (w);
3494 if (TREE_CODE (w) != INTEGER_CST)
3496 cp_error_at ("bit-field `%D' width not an integer constant",
3498 DECL_INITIAL (x) = NULL_TREE;
3500 else if (width = TREE_INT_CST_LOW (w),
3501 width < 0)
3503 DECL_INITIAL (x) = NULL;
3504 cp_error_at ("negative width in bit-field `%D'", x);
3506 else if (width == 0 && DECL_NAME (x) != 0)
3508 DECL_INITIAL (x) = NULL;
3509 cp_error_at ("zero width for bit-field `%D'", x);
3511 else if (width
3512 > TYPE_PRECISION (long_long_unsigned_type_node))
3514 /* The backend will dump if you try to use something
3515 too big; avoid that. */
3516 DECL_INITIAL (x) = NULL;
3517 sorry ("bit-fields larger than %d bits",
3518 TYPE_PRECISION (long_long_unsigned_type_node));
3519 cp_error_at (" in declaration of `%D'", x);
3521 else if (width > TYPE_PRECISION (TREE_TYPE (x))
3522 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
3524 cp_warning_at ("width of `%D' exceeds its type", x);
3526 else if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
3527 && ((min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
3528 TREE_UNSIGNED (TREE_TYPE (x))) > width)
3529 || (min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
3530 TREE_UNSIGNED (TREE_TYPE (x))) > width)))
3532 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3533 x, TREE_TYPE (x));
3536 if (DECL_INITIAL (x) == NULL_TREE)
3538 else if (width == 0)
3540 #ifdef EMPTY_FIELD_BOUNDARY
3541 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), EMPTY_FIELD_BOUNDARY);
3542 #endif
3543 #ifdef PCC_BITFIELD_TYPE_MATTERS
3544 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3545 TYPE_ALIGN (TREE_TYPE (x)));
3546 #endif
3548 else
3550 DECL_INITIAL (x) = NULL_TREE;
3551 DECL_FIELD_SIZE (x) = width;
3552 DECL_BIT_FIELD (x) = 1;
3555 else
3556 /* Non-bit-fields are aligned for their type. */
3557 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x)));
3559 else
3561 tree type = TREE_TYPE (x);
3563 while (TREE_CODE (type) == ARRAY_TYPE)
3564 type = TREE_TYPE (type);
3566 if (TYPE_LANG_SPECIFIC (type) && ! ANON_UNION_P (x)
3567 && ! TYPE_PTRMEMFUNC_P (type))
3569 /* Never let anything with uninheritable virtuals
3570 make it through without complaint. */
3571 if (CLASSTYPE_ABSTRACT_VIRTUALS (type))
3572 abstract_virtuals_error (x, type);
3574 /* Don't let signatures make it through either. */
3575 if (IS_SIGNATURE (type))
3576 signature_error (x, type);
3578 if (code == UNION_TYPE)
3580 char *fie = NULL;
3581 if (TYPE_NEEDS_CONSTRUCTING (type))
3582 fie = "constructor";
3583 else if (TYPE_NEEDS_DESTRUCTOR (type))
3584 fie = "destructor";
3585 else if (TYPE_HAS_REAL_ASSIGNMENT (type))
3586 fie = "assignment operator";
3587 if (fie)
3588 cp_error_at ("member `%#D' with %s not allowed in union", x,
3589 fie);
3591 else
3593 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3594 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3595 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3596 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3599 if (!TYPE_HAS_CONST_INIT_REF (type))
3600 cant_have_const_ctor = 1;
3602 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3603 no_const_asn_ref = 1;
3605 if (TYPE_HAS_CONSTRUCTOR (type)
3606 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3608 cant_have_default_ctor = 1;
3609 #if 0
3610 /* This is wrong for aggregates. */
3611 if (! TYPE_HAS_CONSTRUCTOR (t))
3613 if (DECL_NAME (x))
3614 cp_pedwarn_at ("member `%#D' with only non-default constructor", x);
3615 else
3616 cp_pedwarn_at ("member with only non-default constructor", x);
3617 cp_pedwarn_at ("in class without a constructor",
3620 #endif
3623 if (DECL_INITIAL (x) != NULL_TREE)
3625 /* `build_class_init_list' does not recognize
3626 non-FIELD_DECLs. */
3627 if (code == UNION_TYPE && any_default_members != 0)
3628 cp_error_at ("multiple fields in union `%T' initialized");
3629 any_default_members = 1;
3634 /* If this type has any constant members which did not come
3635 with their own initialization, mark that fact here. It is
3636 not an error here, since such types can be saved either by their
3637 constructors, or by fortuitous initialization. */
3638 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
3639 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
3640 CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
3642 /* Synthesize any needed methods. Note that methods will be synthesized
3643 for anonymous unions; grok_x_components undoes that. */
3645 if (! fn_fields)
3646 nonprivate_method = 1;
3648 if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t)
3649 && !IS_SIGNATURE (t))
3651 /* Here we must cons up a destructor on the fly. */
3652 tree dtor = cons_up_default_function (t, name, 0);
3653 check_for_override (dtor, t);
3655 /* If we couldn't make it work, then pretend we didn't need it. */
3656 if (dtor == void_type_node)
3657 TYPE_NEEDS_DESTRUCTOR (t) = 0;
3658 else
3660 /* Link dtor onto end of fn_fields. */
3662 TREE_CHAIN (dtor) = fn_fields;
3663 fn_fields = dtor;
3665 if (DECL_VINDEX (dtor))
3666 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3667 &has_virtual, dtor, t);
3668 nonprivate_method = 1;
3672 /* Effective C++ rule 11. */
3673 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3674 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3676 cp_warning ("`%#T' has pointer data members", t);
3678 if (! TYPE_HAS_INIT_REF (t))
3680 cp_warning (" but does not override `%T(const %T&)'", t, t);
3681 if (! TYPE_HAS_ASSIGN_REF (t))
3682 cp_warning (" or `operator=(const %T&)'", t);
3684 else if (! TYPE_HAS_ASSIGN_REF (t))
3685 cp_warning (" but does not override `operator=(const %T&)'", t);
3688 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3690 TYPE_HAS_COMPLEX_INIT_REF (t)
3691 |= (TYPE_HAS_INIT_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3692 || has_virtual || any_default_members);
3693 TYPE_NEEDS_CONSTRUCTING (t)
3694 |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3695 || has_virtual || any_default_members);
3696 if (! IS_SIGNATURE (t))
3697 CLASSTYPE_NON_AGGREGATE (t)
3698 = ! aggregate || has_virtual || TYPE_HAS_CONSTRUCTOR (t);
3700 /* ARM $12.1: A default constructor will be generated for a class X
3701 only if no constructor has been declared for class X. So we
3702 check TYPE_HAS_CONSTRUCTOR also, to make sure we don't generate
3703 one if they declared a constructor in this class. */
3704 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor
3705 && ! IS_SIGNATURE (t))
3707 tree default_fn = cons_up_default_function (t, name, 2);
3708 TREE_CHAIN (default_fn) = fn_fields;
3709 fn_fields = default_fn;
3712 /* Create default copy constructor, if needed. */
3713 if (! TYPE_HAS_INIT_REF (t) && ! IS_SIGNATURE (t))
3715 /* ARM 12.18: You get either X(X&) or X(const X&), but
3716 not both. --Chip */
3717 tree default_fn = cons_up_default_function (t, name,
3718 3 + cant_have_const_ctor);
3719 TREE_CHAIN (default_fn) = fn_fields;
3720 fn_fields = default_fn;
3723 TYPE_HAS_REAL_ASSIGNMENT (t) |= TYPE_HAS_ASSIGNMENT (t);
3724 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
3725 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
3726 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
3728 if (! TYPE_HAS_ASSIGN_REF (t) && ! IS_SIGNATURE (t))
3730 tree default_fn = cons_up_default_function (t, name,
3731 5 + no_const_asn_ref);
3732 TREE_CHAIN (default_fn) = fn_fields;
3733 fn_fields = default_fn;
3736 if (fn_fields)
3738 TYPE_METHODS (t) = fn_fields;
3739 method_vec = finish_struct_methods (t, fn_fields, nonprivate_method);
3741 if (TYPE_HAS_CONSTRUCTOR (t)
3742 && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
3743 && DECL_FRIENDLIST (TYPE_MAIN_DECL (t)) == NULL_TREE)
3745 int nonprivate_ctor = 0;
3746 tree ctor;
3748 for (ctor = TREE_VEC_ELT (method_vec, 0);
3749 ctor;
3750 ctor = DECL_CHAIN (ctor))
3751 if (! TREE_PRIVATE (ctor))
3753 nonprivate_ctor = 1;
3754 break;
3757 if (nonprivate_ctor == 0 && warn_ctor_dtor_privacy)
3758 cp_warning ("`%#T' only defines private constructors and has no friends",
3762 else
3764 method_vec = 0;
3766 /* Just in case these got accidentally
3767 filled in by syntax errors. */
3768 TYPE_HAS_CONSTRUCTOR (t) = 0;
3769 TYPE_HAS_DESTRUCTOR (t) = 0;
3773 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3775 for (access_decls = nreverse (access_decls); access_decls;
3776 access_decls = TREE_CHAIN (access_decls))
3778 tree fdecl = TREE_VALUE (access_decls);
3779 tree flist = NULL_TREE;
3780 tree name;
3781 tree access = TREE_PURPOSE (access_decls);
3782 int i = 2;
3783 tree tmp;
3785 if (TREE_CODE (fdecl) == TREE_LIST)
3787 flist = fdecl;
3788 fdecl = TREE_VALUE (flist);
3791 name = DECL_NAME (fdecl);
3793 for (; i < n_methods; i++)
3794 if (DECL_NAME (TREE_VEC_ELT (method_vec, i)) == name)
3796 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
3797 cp_error_at (" because of local method `%#D' with same name",
3798 TREE_VEC_ELT (method_vec, i));
3799 fdecl = NULL_TREE;
3800 break;
3803 if (! fdecl)
3804 continue;
3806 for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
3807 if (DECL_NAME (tmp) == name)
3809 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
3810 cp_error_at (" because of local field `%#D' with same name", tmp);
3811 fdecl = NULL_TREE;
3812 break;
3815 if (!fdecl)
3816 continue;
3818 /* Make type T see field decl FDECL with access ACCESS.*/
3819 if (flist)
3821 fdecl = TREE_VALUE (flist);
3822 while (fdecl)
3824 if (alter_access (t, fdecl, access) == 0)
3825 break;
3826 fdecl = DECL_CHAIN (fdecl);
3829 else
3830 alter_access (t, fdecl, access);
3835 if (vfield == NULL_TREE && has_virtual)
3837 /* We build this decl with ptr_type_node, and
3838 change the type when we know what it should be. */
3839 vfield = build_lang_field_decl (FIELD_DECL, get_vfield_name (t),
3840 ptr_type_node);
3841 /* If you change any of the below, take a look at all the
3842 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
3843 them too. */
3844 DECL_ASSEMBLER_NAME (vfield) = get_identifier (VFIELD_BASE);
3845 CLASSTYPE_VFIELD (t) = vfield;
3846 DECL_VIRTUAL_P (vfield) = 1;
3847 DECL_ARTIFICIAL (vfield) = 1;
3848 DECL_FIELD_CONTEXT (vfield) = t;
3849 DECL_CLASS_CONTEXT (vfield) = t;
3850 DECL_FCONTEXT (vfield) = t;
3851 DECL_SAVED_INSNS (vfield) = NULL_RTX;
3852 DECL_FIELD_SIZE (vfield) = 0;
3853 DECL_ALIGN (vfield) = TYPE_ALIGN (ptr_type_node);
3854 #if 0
3855 /* This is more efficient, but breaks binary compatibility, turn
3856 it on sometime when we don't care. If we turn it on, we also
3857 have to enable the code in dfs_init_vbase_pointers. */
3858 /* vfield is always first entry in structure. */
3859 TREE_CHAIN (vfield) = fields;
3860 fields = vfield;
3861 #else
3862 if (last_x)
3864 my_friendly_assert (TREE_CHAIN (last_x) == NULL_TREE, 175);
3865 TREE_CHAIN (last_x) = vfield;
3866 last_x = vfield;
3868 else
3869 fields = vfield;
3870 #endif
3871 empty = 0;
3872 vfields = chainon (vfields, CLASSTYPE_AS_LIST (t));
3875 /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
3876 And they have already done their work.
3878 C++: maybe we will support default field initialization some day... */
3880 /* Delete all zero-width bit-fields from the front of the fieldlist */
3881 while (fields && DECL_BIT_FIELD (fields)
3882 && DECL_INITIAL (fields))
3883 fields = TREE_CHAIN (fields);
3884 /* Delete all such fields from the rest of the fields. */
3885 for (x = fields; x;)
3887 if (TREE_CHAIN (x) && DECL_BIT_FIELD (TREE_CHAIN (x))
3888 && DECL_INITIAL (TREE_CHAIN (x)))
3889 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
3890 else
3891 x = TREE_CHAIN (x);
3893 /* Delete all duplicate fields from the fields */
3894 delete_duplicate_fields (fields);
3896 /* Catch function/field name conflict. We don't need to do this for a
3897 signature, since it can only contain the fields constructed in
3898 append_signature_fields. */
3899 if (! IS_SIGNATURE (t))
3901 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3902 for (x = fields; x; x = TREE_CHAIN (x))
3904 tree name = DECL_NAME (x);
3905 int i = 2;
3907 if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))
3908 continue;
3910 for (; i < n_methods; ++i)
3911 if (DECL_NAME (TREE_VEC_ELT (method_vec, i)) == name)
3913 cp_error_at ("data member `%#D' conflicts with", x);
3914 cp_error_at ("function member `%#D'",
3915 TREE_VEC_ELT (method_vec, i));
3916 break;
3921 /* Now we have the final fieldlist for the data fields. Record it,
3922 then lay out the structure or union (including the fields). */
3924 TYPE_FIELDS (t) = fields;
3926 /* Pass layout information about base classes to layout_type, if any. */
3927 if (n_baseclasses)
3929 tree pseudo_basetype = TREE_TYPE (base_layout_decl);
3931 TREE_CHAIN (base_layout_decl) = TYPE_FIELDS (t);
3932 TYPE_FIELDS (t) = base_layout_decl;
3934 TYPE_SIZE (pseudo_basetype) = CLASSTYPE_SIZE (t);
3935 TYPE_MODE (pseudo_basetype) = TYPE_MODE (t);
3936 TYPE_ALIGN (pseudo_basetype) = CLASSTYPE_ALIGN (t);
3937 DECL_ALIGN (base_layout_decl) = TYPE_ALIGN (pseudo_basetype);
3938 /* Don't re-use old size. */
3939 DECL_SIZE (base_layout_decl) = NULL_TREE;
3941 else if (empty)
3943 /* C++: do not let empty structures exist. */
3944 tree decl = build_lang_field_decl
3945 (FIELD_DECL, NULL_TREE, char_type_node);
3946 TREE_CHAIN (decl) = TYPE_FIELDS (t);
3947 TYPE_FIELDS (t) = decl;
3950 layout_type (t);
3952 finish_struct_anon (t);
3954 if (n_baseclasses || empty)
3955 TYPE_FIELDS (t) = TREE_CHAIN (TYPE_FIELDS (t));
3957 /* Set the TYPE_DECL for this type to contain the right
3958 value for DECL_OFFSET, so that we can use it as part
3959 of a COMPONENT_REF for multiple inheritance. */
3961 layout_decl (TYPE_MAIN_DECL (t), 0);
3963 /* Now fix up any virtual base class types that we left lying
3964 around. We must get these done before we try to lay out the
3965 virtual function table. */
3966 pending_hard_virtuals = nreverse (pending_hard_virtuals);
3968 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
3970 tree vbases;
3972 max_has_virtual = layout_vbasetypes (t, max_has_virtual);
3973 vbases = CLASSTYPE_VBASECLASSES (t);
3974 CLASSTYPE_N_VBASECLASSES (t) = list_length (vbases);
3977 /* Now fixup overrides of all functions in vtables from all
3978 direct or indirect virtual base classes. */
3979 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3980 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3982 for (i = 0; i < n_baseclasses; i++)
3984 tree base_binfo = TREE_VEC_ELT (binfos, i);
3985 tree basetype = BINFO_TYPE (base_binfo);
3986 tree vbases;
3988 vbases = CLASSTYPE_VBASECLASSES (basetype);
3989 while (vbases)
3991 merge_overrides (binfo_member (BINFO_TYPE (vbases),
3992 CLASSTYPE_VBASECLASSES (t)),
3993 vbases, 1, t);
3994 vbases = TREE_CHAIN (vbases);
4000 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
4001 might need to know it for setting up the offsets in the vtable
4002 (or in thunks) below. */
4003 if (vfield != NULL_TREE
4004 && DECL_FIELD_CONTEXT (vfield) != t)
4006 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4007 tree offset = BINFO_OFFSET (binfo);
4009 vfield = copy_node (vfield);
4010 copy_lang_decl (vfield);
4012 if (! integer_zerop (offset))
4013 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4014 DECL_FIELD_CONTEXT (vfield) = t;
4015 DECL_CLASS_CONTEXT (vfield) = t;
4016 DECL_FIELD_BITPOS (vfield)
4017 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4018 CLASSTYPE_VFIELD (t) = vfield;
4021 #ifdef NOTQUITE
4022 cp_warning ("Doing hard virtuals for %T...", t);
4023 #endif
4025 if (has_virtual > max_has_virtual)
4026 max_has_virtual = has_virtual;
4027 if (max_has_virtual > 0)
4028 TYPE_VIRTUAL_P (t) = 1;
4030 if (flag_rtti && TYPE_VIRTUAL_P (t) && !pending_hard_virtuals)
4031 modify_all_vtables (t, NULL_TREE, NULL_TREE);
4033 while (pending_hard_virtuals)
4035 modify_all_vtables (t,
4036 TREE_PURPOSE (pending_hard_virtuals),
4037 TREE_VALUE (pending_hard_virtuals));
4038 pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
4041 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
4043 tree vbases;
4044 /* Now fixup any virtual function entries from virtual bases
4045 that have different deltas. This has to come after we do the
4046 pending hard virtuals, as we might have a function that comes
4047 from multiple virtual base instances that is only overridden
4048 by a hard virtual above. */
4049 vbases = CLASSTYPE_VBASECLASSES (t);
4050 while (vbases)
4052 /* We might be able to shorten the amount of work we do by
4053 only doing this for vtables that come from virtual bases
4054 that have differing offsets, but don't want to miss any
4055 entries. */
4056 fixup_vtable_deltas (vbases, 1, t);
4057 vbases = TREE_CHAIN (vbases);
4061 /* Under our model of GC, every C++ class gets its own virtual
4062 function table, at least virtually. */
4063 if (pending_virtuals)
4065 pending_virtuals = nreverse (pending_virtuals);
4066 /* We must enter these virtuals into the table. */
4067 if (first_vfn_base_index < 0)
4069 /* The second slot is for the tdesc pointer when thunks are used. */
4070 if (flag_vtable_thunks)
4071 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4073 /* The first slot is for the rtti offset. */
4074 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4076 set_rtti_entry (pending_virtuals, size_zero_node, t);
4077 build_vtable (NULL_TREE, t);
4079 else
4081 /* Here we know enough to change the type of our virtual
4082 function table, but we will wait until later this function. */
4084 if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
4085 build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index), t);
4088 /* If this type has basetypes with constructors, then those
4089 constructors might clobber the virtual function table. But
4090 they don't if the derived class shares the exact vtable of the base
4091 class. */
4093 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4095 else if (first_vfn_base_index >= 0)
4097 tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index);
4098 /* This class contributes nothing new to the virtual function
4099 table. However, it may have declared functions which
4100 went into the virtual function table "inherited" from the
4101 base class. If so, we grab a copy of those updated functions,
4102 and pretend they are ours. */
4104 /* See if we should steal the virtual info from base class. */
4105 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
4106 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
4107 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
4108 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
4109 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
4110 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4113 if (max_has_virtual || first_vfn_base_index >= 0)
4115 CLASSTYPE_VSIZE (t) = has_virtual;
4116 if (first_vfn_base_index >= 0)
4118 if (pending_virtuals)
4119 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t),
4120 pending_virtuals);
4122 else if (has_virtual)
4124 TYPE_BINFO_VIRTUALS (t) = pending_virtuals;
4125 if (write_virtuals >= 0)
4126 DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1;
4130 /* Now lay out the virtual function table. */
4131 if (has_virtual)
4133 tree atype, itype;
4135 if (TREE_TYPE (vfield) == ptr_type_node)
4137 /* We must create a pointer to this table because
4138 the one inherited from base class does not exist.
4139 We will fill in the type when we know what it
4140 should really be. Use `size_int' so values are memoized
4141 in common cases. */
4142 itype = build_index_type (size_int (has_virtual));
4143 atype = build_array_type (vtable_entry_type, itype);
4144 layout_type (atype);
4145 TREE_TYPE (vfield) = build_pointer_type (atype);
4147 else
4149 atype = TREE_TYPE (TREE_TYPE (vfield));
4151 if (has_virtual != TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))))
4153 /* We must extend (or create) the boundaries on this array,
4154 because we picked up virtual functions from multiple
4155 base classes. */
4156 itype = build_index_type (size_int (has_virtual));
4157 atype = build_array_type (vtable_entry_type, itype);
4158 layout_type (atype);
4159 vfield = copy_node (vfield);
4160 TREE_TYPE (vfield) = build_pointer_type (atype);
4164 CLASSTYPE_VFIELD (t) = vfield;
4165 if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
4167 TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
4168 DECL_SIZE (TYPE_BINFO_VTABLE (t)) = 0;
4169 layout_decl (TYPE_BINFO_VTABLE (t), 0);
4170 /* At one time the vtable info was grabbed 2 words at a time. This
4171 fails on sparc unless you have 8-byte alignment. (tiemann) */
4172 DECL_ALIGN (TYPE_BINFO_VTABLE (t))
4173 = MAX (TYPE_ALIGN (double_type_node),
4174 DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
4177 else if (first_vfn_base_index >= 0)
4178 CLASSTYPE_VFIELD (t) = vfield;
4179 CLASSTYPE_VFIELDS (t) = vfields;
4181 finish_struct_bits (t, max_has_virtual);
4183 /* Complete the rtl for any static member objects of the type we're
4184 working on. */
4185 for (x = fields; x; x = TREE_CHAIN (x))
4187 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4188 && TREE_TYPE (x) == t)
4190 DECL_MODE (x) = TYPE_MODE (t);
4191 make_decl_rtl (x, NULL, 0);
4195 if (TYPE_HAS_CONSTRUCTOR (t))
4197 tree vfields = CLASSTYPE_VFIELDS (t);
4199 while (vfields)
4201 /* Mark the fact that constructor for T
4202 could affect anybody inheriting from T
4203 who wants to initialize vtables for VFIELDS's type. */
4204 if (VF_DERIVED_VALUE (vfields))
4205 TREE_ADDRESSABLE (vfields) = 1;
4206 vfields = TREE_CHAIN (vfields);
4208 if (any_default_members != 0)
4209 build_class_init_list (t);
4211 else if (TYPE_NEEDS_CONSTRUCTING (t))
4212 build_class_init_list (t);
4214 /* Write out inline function definitions. */
4215 do_inline_function_hair (t, CLASSTYPE_INLINE_FRIENDS (t));
4216 CLASSTYPE_INLINE_FRIENDS (t) = 0;
4218 if (CLASSTYPE_VSIZE (t) != 0)
4220 #if 0
4221 /* This is now done above. */
4222 if (DECL_FIELD_CONTEXT (vfield) != t)
4224 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4225 tree offset = BINFO_OFFSET (binfo);
4227 vfield = copy_node (vfield);
4228 copy_lang_decl (vfield);
4230 if (! integer_zerop (offset))
4231 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4232 DECL_FIELD_CONTEXT (vfield) = t;
4233 DECL_CLASS_CONTEXT (vfield) = t;
4234 DECL_FIELD_BITPOS (vfield)
4235 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4236 CLASSTYPE_VFIELD (t) = vfield;
4238 #endif
4240 /* In addition to this one, all the other vfields should be listed. */
4241 /* Before that can be done, we have to have FIELD_DECLs for them, and
4242 a place to find them. */
4243 TYPE_NONCOPIED_PARTS (t) = build_tree_list (default_conversion (TYPE_BINFO_VTABLE (t)), vfield);
4245 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
4246 && DECL_VINDEX (TREE_VEC_ELT (method_vec, 1)) == NULL_TREE)
4247 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
4251 /* Make the rtl for any new vtables we have created, and unmark
4252 the base types we marked. */
4253 finish_vtbls (TYPE_BINFO (t), 1, t);
4254 hack_incomplete_structures (t);
4256 #if 0
4257 if (TYPE_NAME (t) && TYPE_IDENTIFIER (t))
4258 undo_template_name_overload (TYPE_IDENTIFIER (t), 1);
4259 #endif
4261 resume_momentary (old);
4263 if (warn_overloaded_virtual)
4264 warn_hidden (t);
4266 #if 0
4267 /* This has to be done after we have sorted out what to do with
4268 the enclosing type. */
4269 if (write_symbols != DWARF_DEBUG)
4271 /* Be smarter about nested classes here. If a type is nested,
4272 only output it if we would output the enclosing type. */
4273 if (DECL_CONTEXT (TYPE_MAIN_DECL (t))
4274 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (TYPE_MAIN_DECL (t)))) == 't')
4275 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = TREE_ASM_WRITTEN (TYPE_MAIN_DECL (t));
4277 #endif
4279 if (write_symbols != DWARF_DEBUG && write_symbols != DWARF2_DEBUG)
4281 /* If the type has methods, we want to think about cutting down
4282 the amount of symbol table stuff we output. The value stored in
4283 the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
4284 For example, if a member function is seen and we decide to
4285 write out that member function, then we can change the value
4286 of the DECL_IGNORED_P slot, and the type will be output when
4287 that member function's debug info is written out.
4289 We can't do this with DWARF, which does not support name
4290 references between translation units. */
4291 if (CLASSTYPE_METHOD_VEC (t))
4293 extern tree pending_vtables;
4295 /* Don't output full info about any type
4296 which does not have its implementation defined here. */
4297 if (TYPE_VIRTUAL_P (t) && write_virtuals == 2)
4298 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t))
4299 = (value_member (TYPE_IDENTIFIER (t), pending_vtables) == 0);
4300 else if (CLASSTYPE_INTERFACE_ONLY (t))
4301 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4302 #if 0
4303 /* XXX do something about this. */
4304 else if (CLASSTYPE_INTERFACE_UNKNOWN (t))
4305 /* Only a first approximation! */
4306 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4307 #endif
4309 else if (CLASSTYPE_INTERFACE_ONLY (t))
4310 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4313 /* Finish debugging output for this type. */
4314 rest_of_type_compilation (t, toplevel_bindings_p ());
4316 return t;
4319 tree
4320 finish_struct (t, list_of_fieldlists, attributes, warn_anon)
4321 tree t, list_of_fieldlists, attributes;
4322 int warn_anon;
4324 tree fields = NULL_TREE;
4325 tree *tail = &TYPE_METHODS (t);
4326 tree name = TYPE_NAME (t);
4327 tree x, last_x = NULL_TREE;
4328 tree access;
4329 tree dummy = NULL_TREE;
4331 if (TREE_CODE (name) == TYPE_DECL)
4333 extern int lineno;
4335 DECL_SOURCE_FILE (name) = input_filename;
4336 /* For TYPE_DECL that are not typedefs (those marked with a line
4337 number of zero, we don't want to mark them as real typedefs.
4338 If this fails one needs to make sure real typedefs have a
4339 previous line number, even if it is wrong, that way the below
4340 will fill in the right line number. (mrs) */
4341 if (DECL_SOURCE_LINE (name))
4342 DECL_SOURCE_LINE (name) = lineno;
4343 CLASSTYPE_SOURCE_LINE (t) = lineno;
4344 name = DECL_NAME (name);
4347 /* Append the fields we need for constructing signature tables. */
4348 if (IS_SIGNATURE (t))
4349 append_signature_fields (list_of_fieldlists);
4351 /* Move our self-reference declaration to the end of the field list so
4352 any real field with the same name takes precedence. */
4353 if (list_of_fieldlists
4354 && TREE_VALUE (list_of_fieldlists)
4355 && DECL_ARTIFICIAL (TREE_VALUE (list_of_fieldlists)))
4357 dummy = TREE_VALUE (list_of_fieldlists);
4358 list_of_fieldlists = TREE_CHAIN (list_of_fieldlists);
4361 if (last_x && list_of_fieldlists)
4362 TREE_CHAIN (last_x) = TREE_VALUE (list_of_fieldlists);
4364 while (list_of_fieldlists)
4366 access = TREE_PURPOSE (list_of_fieldlists);
4368 /* For signatures, we made all methods `public' in the parser and
4369 reported an error if a access specifier was used. */
4370 if (access == access_default_node)
4372 if (CLASSTYPE_DECLARED_CLASS (t) == 0)
4373 access = access_public_node;
4374 else
4375 access = access_private_node;
4378 for (x = TREE_VALUE (list_of_fieldlists); x; x = TREE_CHAIN (x))
4380 TREE_PRIVATE (x) = access == access_private_node;
4381 TREE_PROTECTED (x) = access == access_protected_node;
4383 /* Check for inconsistent use of this name in the class body.
4384 Enums, types and static vars have already been checked. */
4385 if (TREE_CODE (x) != TYPE_DECL && TREE_CODE (x) != USING_DECL
4386 && TREE_CODE (x) != CONST_DECL && TREE_CODE (x) != VAR_DECL)
4388 tree name = DECL_NAME (x);
4389 tree icv;
4391 /* Don't get confused by access decls. */
4392 if (name && TREE_CODE (name) == IDENTIFIER_NODE)
4393 icv = IDENTIFIER_CLASS_VALUE (name);
4394 else
4395 icv = NULL_TREE;
4397 if (icv
4398 /* Don't complain about constructors. */
4399 && name != constructor_name (current_class_type)
4400 /* Or inherited names. */
4401 && id_in_current_class (name)
4402 /* Or shadowed tags. */
4403 && !(TREE_CODE (icv) == TYPE_DECL
4404 && DECL_CONTEXT (icv) == t))
4406 cp_error_at ("declaration of identifier `%D' as `%+#D'",
4407 name, x);
4408 cp_error_at ("conflicts with other use in class as `%#D'",
4409 icv);
4413 if (TREE_CODE (x) == FUNCTION_DECL
4414 || DECL_FUNCTION_TEMPLATE_P (x))
4416 DECL_CLASS_CONTEXT (x) = t;
4417 if (last_x)
4418 TREE_CHAIN (last_x) = TREE_CHAIN (x);
4419 /* Link x onto end of TYPE_METHODS. */
4420 *tail = x;
4421 tail = &TREE_CHAIN (x);
4422 continue;
4425 if (TREE_CODE (x) != TYPE_DECL)
4426 DECL_FIELD_CONTEXT (x) = t;
4428 if (! fields)
4429 fields = x;
4430 last_x = x;
4432 list_of_fieldlists = TREE_CHAIN (list_of_fieldlists);
4433 /* link the tail while we have it! */
4434 if (last_x)
4436 TREE_CHAIN (last_x) = NULL_TREE;
4438 if (list_of_fieldlists
4439 && TREE_VALUE (list_of_fieldlists)
4440 && TREE_CODE (TREE_VALUE (list_of_fieldlists)) != FUNCTION_DECL)
4441 TREE_CHAIN (last_x) = TREE_VALUE (list_of_fieldlists);
4445 /* Now add the tags, if any, to the list of TYPE_DECLs
4446 defined for this type. */
4447 if (CLASSTYPE_TAGS (t) || dummy)
4449 /* The list of tags was built up in pushtag in reverse order; we need
4450 to fix that so that enumerators will be processed in forward order
4451 in template instantiation. */
4452 CLASSTYPE_TAGS (t) = x = nreverse (CLASSTYPE_TAGS (t));
4453 while (x)
4455 tree tag = TYPE_MAIN_DECL (TREE_VALUE (x));
4457 TREE_NONLOCAL_FLAG (TREE_VALUE (x)) = 0;
4458 x = TREE_CHAIN (x);
4459 last_x = chainon (last_x, tag);
4461 if (dummy)
4462 last_x = chainon (last_x, dummy);
4463 if (fields == NULL_TREE)
4464 fields = last_x;
4465 CLASSTYPE_LOCAL_TYPEDECLS (t) = 1;
4468 *tail = NULL_TREE;
4469 TYPE_FIELDS (t) = fields;
4471 cplus_decl_attributes (t, attributes, NULL_TREE);
4473 if (processing_template_decl)
4475 tree d = getdecls ();
4476 for (; d; d = TREE_CHAIN (d))
4478 /* If this is the decl for the class or one of the template
4479 parms, we've seen all the injected decls. */
4480 if ((TREE_CODE (d) == TYPE_DECL
4481 && (TREE_TYPE (d) == t
4482 || TREE_CODE (TREE_TYPE (d)) == TEMPLATE_TYPE_PARM))
4483 || TREE_CODE (d) == CONST_DECL)
4484 break;
4485 /* Don't inject cache decls. */
4486 else if (IDENTIFIER_TEMPLATE (DECL_NAME (d)))
4487 continue;
4488 DECL_TEMPLATE_INJECT (CLASSTYPE_TI_TEMPLATE (t))
4489 = tree_cons (NULL_TREE, d,
4490 DECL_TEMPLATE_INJECT (CLASSTYPE_TI_TEMPLATE (t)));
4492 CLASSTYPE_METHOD_VEC (t)
4493 = finish_struct_methods (t, TYPE_METHODS (t), 1);
4494 TYPE_SIZE (t) = integer_zero_node;
4496 else
4497 t = finish_struct_1 (t, warn_anon);
4499 TYPE_BEING_DEFINED (t) = 0;
4501 if (current_class_type)
4502 popclass (0);
4503 else
4504 error ("trying to finish struct, but kicked out due to previous parse errors.");
4506 return t;
4509 /* Return non-zero if the effective type of INSTANCE is static.
4510 Used to determine whether the virtual function table is needed
4511 or not.
4513 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4514 of our knowledge of its type. */
4517 resolves_to_fixed_type_p (instance, nonnull)
4518 tree instance;
4519 int *nonnull;
4521 switch (TREE_CODE (instance))
4523 case INDIRECT_REF:
4524 /* Check that we are not going through a cast of some sort. */
4525 if (TREE_TYPE (instance)
4526 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
4527 instance = TREE_OPERAND (instance, 0);
4528 /* fall through... */
4529 case CALL_EXPR:
4530 /* This is a call to a constructor, hence it's never zero. */
4531 if (TREE_HAS_CONSTRUCTOR (instance))
4533 if (nonnull)
4534 *nonnull = 1;
4535 return 1;
4537 return 0;
4539 case SAVE_EXPR:
4540 /* This is a call to a constructor, hence it's never zero. */
4541 if (TREE_HAS_CONSTRUCTOR (instance))
4543 if (nonnull)
4544 *nonnull = 1;
4545 return 1;
4547 return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4549 case RTL_EXPR:
4550 /* This is a call to `new', hence it's never zero. */
4551 if (TREE_CALLS_NEW (instance))
4553 if (nonnull)
4554 *nonnull = 1;
4555 return 1;
4557 return 0;
4559 case PLUS_EXPR:
4560 case MINUS_EXPR:
4561 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
4562 /* Propagate nonnull. */
4563 resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4564 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
4565 return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4566 return 0;
4568 case NOP_EXPR:
4569 case CONVERT_EXPR:
4570 return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4572 case ADDR_EXPR:
4573 if (nonnull)
4574 *nonnull = 1;
4575 return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4577 case COMPONENT_REF:
4578 return resolves_to_fixed_type_p (TREE_OPERAND (instance, 1), nonnull);
4580 case VAR_DECL:
4581 case FIELD_DECL:
4582 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
4583 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
4585 if (nonnull)
4586 *nonnull = 1;
4587 return 1;
4589 /* fall through... */
4590 case TARGET_EXPR:
4591 case PARM_DECL:
4592 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
4594 if (nonnull)
4595 *nonnull = 1;
4596 return 1;
4598 else if (nonnull)
4600 if (instance == current_class_ptr
4601 && flag_this_is_variable <= 0)
4603 /* Some people still use `this = 0' inside destructors. */
4604 *nonnull = ! DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (current_function_decl));
4605 /* In a constructor, we know our type. */
4606 if (flag_this_is_variable < 0)
4607 return 1;
4609 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4610 /* Reference variables should be references to objects. */
4611 *nonnull = 1;
4613 return 0;
4615 default:
4616 return 0;
4620 void
4621 init_class_processing ()
4623 current_class_depth = 0;
4624 current_class_stacksize = 10;
4625 current_class_base = (tree *)xmalloc(current_class_stacksize * sizeof (tree));
4626 current_class_stack = current_class_base;
4628 current_lang_stacksize = 10;
4629 current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree));
4630 current_lang_stack = current_lang_base;
4632 access_default_node = build_int_2 (0, 0);
4633 access_public_node = build_int_2 (1, 0);
4634 access_protected_node = build_int_2 (2, 0);
4635 access_private_node = build_int_2 (3, 0);
4636 access_default_virtual_node = build_int_2 (4, 0);
4637 access_public_virtual_node = build_int_2 (5, 0);
4638 access_private_virtual_node = build_int_2 (6, 0);
4640 /* Keep these values lying around. */
4641 base_layout_decl = build_lang_field_decl (FIELD_DECL, NULL_TREE, error_mark_node);
4642 TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE);
4644 gcc_obstack_init (&class_obstack);
4647 /* Set current scope to NAME. CODE tells us if this is a
4648 STRUCT, UNION, or ENUM environment.
4650 NAME may end up being NULL_TREE if this is an anonymous or
4651 late-bound struct (as in "struct { ... } foo;") */
4653 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
4654 appropriate values, found by looking up the type definition of
4655 NAME (as a CODE).
4657 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
4658 which can be seen locally to the class. They are shadowed by
4659 any subsequent local declaration (including parameter names).
4661 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
4662 which have static meaning (i.e., static members, static
4663 member functions, enum declarations, etc).
4665 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
4666 which can be seen locally to the class (as in 1), but
4667 know that we are doing this for declaration purposes
4668 (i.e. friend foo::bar (int)).
4670 So that we may avoid calls to lookup_name, we cache the _TYPE
4671 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
4673 For multiple inheritance, we perform a two-pass depth-first search
4674 of the type lattice. The first pass performs a pre-order search,
4675 marking types after the type has had its fields installed in
4676 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
4677 unmarks the marked types. If a field or member function name
4678 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
4679 that name becomes `error_mark_node'. */
4681 void
4682 pushclass (type, modify)
4683 tree type;
4684 int modify;
4686 push_memoized_context (type, modify);
4688 current_class_depth++;
4689 *current_class_stack++ = current_class_name;
4690 *current_class_stack++ = current_class_type;
4691 if (current_class_stack >= current_class_base + current_class_stacksize)
4693 current_class_base
4694 = (tree *)xrealloc (current_class_base,
4695 sizeof (tree) * (current_class_stacksize + 10));
4696 current_class_stack = current_class_base + current_class_stacksize;
4697 current_class_stacksize += 10;
4700 current_class_name = TYPE_NAME (type);
4701 if (TREE_CODE (current_class_name) == TYPE_DECL)
4702 current_class_name = DECL_NAME (current_class_name);
4703 current_class_type = type;
4705 if (previous_class_type != NULL_TREE
4706 && (type != previous_class_type || TYPE_SIZE (previous_class_type) == NULL_TREE)
4707 && current_class_depth == 1)
4709 /* Forcibly remove any old class remnants. */
4710 popclass (-1);
4711 previous_class_type = NULL_TREE;
4714 pushlevel_class ();
4716 #if 0
4717 if (CLASSTYPE_TEMPLATE_INFO (type))
4718 overload_template_name (type);
4719 #endif
4721 if (modify)
4723 tree tags;
4724 tree this_fndecl = current_function_decl;
4726 if (current_function_decl
4727 && DECL_CONTEXT (current_function_decl)
4728 && TREE_CODE (DECL_CONTEXT (current_function_decl)) == FUNCTION_DECL)
4729 current_function_decl = DECL_CONTEXT (current_function_decl);
4730 else
4731 current_function_decl = NULL_TREE;
4733 if (type != previous_class_type || current_class_depth > 1)
4735 build_mi_matrix (type);
4736 push_class_decls (type);
4737 free_mi_matrix ();
4739 else
4741 tree item;
4743 /* Hooray, we successfully cached; let's just install the
4744 cached class_shadowed list, and walk through it to get the
4745 IDENTIFIER_TYPE_VALUEs correct. */
4746 set_class_shadows (previous_class_values);
4747 for (item = previous_class_values; item; item = TREE_CHAIN (item))
4749 tree id = TREE_PURPOSE (item);
4750 tree decl = IDENTIFIER_CLASS_VALUE (id);
4752 if (TREE_CODE (decl) == TYPE_DECL)
4753 set_identifier_type_value (id, TREE_TYPE (decl));
4755 unuse_fields (type);
4758 for (tags = CLASSTYPE_TAGS (type); tags; tags = TREE_CHAIN (tags))
4760 TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 1;
4761 if (! TREE_PURPOSE (tags))
4762 continue;
4763 pushtag (TREE_PURPOSE (tags), TREE_VALUE (tags), 0);
4766 current_function_decl = this_fndecl;
4770 /* Get out of the current class scope. If we were in a class scope
4771 previously, that is the one popped to. The flag MODIFY tells whether
4772 the current scope declarations needs to be modified as a result of
4773 popping to the previous scope. 0 is used for class definitions. */
4775 void
4776 popclass (modify)
4777 int modify;
4779 if (modify < 0)
4781 /* Back this old class out completely. */
4782 tree tags = CLASSTYPE_TAGS (previous_class_type);
4783 tree t;
4785 /* This code can be seen as a cache miss. When we've cached a
4786 class' scope's bindings and we can't use them, we need to reset
4787 them. This is it! */
4788 for (t = previous_class_values; t; t = TREE_CHAIN (t))
4789 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
4790 while (tags)
4792 TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
4793 tags = TREE_CHAIN (tags);
4795 goto ret;
4798 if (modify)
4800 /* Just remove from this class what didn't make
4801 it into IDENTIFIER_CLASS_VALUE. */
4802 tree tags = CLASSTYPE_TAGS (current_class_type);
4804 while (tags)
4806 TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
4807 tags = TREE_CHAIN (tags);
4811 /* Force clearing of IDENTIFIER_CLASS_VALUEs after a class definition,
4812 since not all class decls make it there currently. */
4813 poplevel_class (! modify);
4815 /* Since poplevel_class does the popping of class decls nowadays,
4816 this really only frees the obstack used for these decls.
4817 That's why it had to be moved down here. */
4818 if (modify)
4819 pop_class_decls ();
4821 current_class_depth--;
4822 current_class_type = *--current_class_stack;
4823 current_class_name = *--current_class_stack;
4825 pop_memoized_context (modify);
4827 ret:
4831 /* When entering a class scope, all enclosing class scopes' names with
4832 static meaning (static variables, static functions, types and enumerators)
4833 have to be visible. This recursive function calls pushclass for all
4834 enclosing class contexts until global or a local scope is reached.
4835 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
4836 formal of the same name. */
4838 void
4839 push_nested_class (type, modify)
4840 tree type;
4841 int modify;
4843 tree context;
4845 if (type == NULL_TREE || type == error_mark_node || ! IS_AGGR_TYPE (type)
4846 || TREE_CODE (type) == TEMPLATE_TYPE_PARM)
4847 return;
4849 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
4851 if (context && TREE_CODE (context) == RECORD_TYPE)
4852 push_nested_class (context, 2);
4853 pushclass (type, modify);
4856 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
4858 void
4859 pop_nested_class (modify)
4860 int modify;
4862 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
4864 popclass (modify);
4865 if (context && TREE_CODE (context) == RECORD_TYPE)
4866 pop_nested_class (modify);
4869 /* Set global variables CURRENT_LANG_NAME to appropriate value
4870 so that behavior of name-mangling machinery is correct. */
4872 void
4873 push_lang_context (name)
4874 tree name;
4876 *current_lang_stack++ = current_lang_name;
4877 if (current_lang_stack >= current_lang_base + current_lang_stacksize)
4879 current_lang_base
4880 = (tree *)xrealloc (current_lang_base,
4881 sizeof (tree) * (current_lang_stacksize + 10));
4882 current_lang_stack = current_lang_base + current_lang_stacksize;
4883 current_lang_stacksize += 10;
4886 if (name == lang_name_cplusplus)
4888 strict_prototype = strict_prototypes_lang_cplusplus;
4889 current_lang_name = name;
4891 else if (name == lang_name_c)
4893 strict_prototype = strict_prototypes_lang_c;
4894 current_lang_name = name;
4896 else
4897 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
4900 /* Get out of the current language scope. */
4902 void
4903 pop_lang_context ()
4905 current_lang_name = *--current_lang_stack;
4906 if (current_lang_name == lang_name_cplusplus)
4907 strict_prototype = strict_prototypes_lang_cplusplus;
4908 else if (current_lang_name == lang_name_c)
4909 strict_prototype = strict_prototypes_lang_c;
4912 /* Type instantiation routines. */
4914 /* This function will instantiate the type of the expression given in
4915 RHS to match the type of LHSTYPE. If errors exist, then return
4916 error_mark_node. If only complain is COMPLAIN is set. If we are
4917 not complaining, never modify rhs, as overload resolution wants to
4918 try many possible instantiations, in hopes that at least one will
4919 work.
4921 This function is used in build_modify_expr, convert_arguments,
4922 build_c_cast, and compute_conversion_costs. */
4924 tree
4925 instantiate_type (lhstype, rhs, complain)
4926 tree lhstype, rhs;
4927 int complain;
4929 tree explicit_targs = NULL_TREE;
4931 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
4933 if (complain)
4934 error ("not enough type information");
4935 return error_mark_node;
4938 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
4939 return rhs;
4941 rhs = copy_node (rhs);
4943 /* This should really only be used when attempting to distinguish
4944 what sort of a pointer to function we have. For now, any
4945 arithmetic operation which is not supported on pointers
4946 is rejected as an error. */
4948 switch (TREE_CODE (rhs))
4950 case TYPE_EXPR:
4951 case CONVERT_EXPR:
4952 case SAVE_EXPR:
4953 case CONSTRUCTOR:
4954 case BUFFER_REF:
4955 my_friendly_abort (177);
4956 return error_mark_node;
4958 case INDIRECT_REF:
4959 case ARRAY_REF:
4961 tree new_rhs;
4963 new_rhs = instantiate_type (build_pointer_type (lhstype),
4964 TREE_OPERAND (rhs, 0), complain);
4965 if (new_rhs == error_mark_node)
4966 return error_mark_node;
4968 TREE_TYPE (rhs) = lhstype;
4969 TREE_OPERAND (rhs, 0) = new_rhs;
4970 return rhs;
4973 case NOP_EXPR:
4974 rhs = copy_node (TREE_OPERAND (rhs, 0));
4975 TREE_TYPE (rhs) = unknown_type_node;
4976 return instantiate_type (lhstype, rhs, complain);
4978 case COMPONENT_REF:
4980 tree field = TREE_OPERAND (rhs, 1);
4981 if (TREE_CODE (field) == TREE_LIST)
4983 tree function = instantiate_type (lhstype, field, complain);
4984 if (function == error_mark_node)
4985 return error_mark_node;
4986 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 185);
4987 if (DECL_VINDEX (function))
4989 tree base = TREE_OPERAND (rhs, 0);
4990 tree base_ptr = build_unary_op (ADDR_EXPR, base, 0);
4991 if (base_ptr == error_mark_node)
4992 return error_mark_node;
4993 base_ptr = convert_pointer_to (DECL_CONTEXT (function), base_ptr);
4994 if (base_ptr == error_mark_node)
4995 return error_mark_node;
4996 return build_vfn_ref (&base_ptr, base, DECL_VINDEX (function));
4998 mark_used (function);
4999 return function;
5002 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 178);
5003 my_friendly_assert (!(TREE_CODE (TREE_TYPE (field)) == FUNCTION_TYPE
5004 || TREE_CODE (TREE_TYPE (field)) == METHOD_TYPE),
5005 179);
5007 TREE_TYPE (rhs) = lhstype;
5008 /* First look for an exact match */
5010 while (field && TREE_TYPE (field) != lhstype)
5011 field = DECL_CHAIN (field);
5012 if (field)
5014 TREE_OPERAND (rhs, 1) = field;
5015 mark_used (field);
5016 return rhs;
5019 /* No exact match found, look for a compatible function. */
5020 field = TREE_OPERAND (rhs, 1);
5021 while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
5022 field = DECL_CHAIN (field);
5023 if (field)
5025 TREE_OPERAND (rhs, 1) = field;
5026 field = DECL_CHAIN (field);
5027 while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
5028 field = DECL_CHAIN (field);
5029 if (field)
5031 if (complain)
5032 error ("ambiguous overload for COMPONENT_REF requested");
5033 return error_mark_node;
5036 else
5038 if (complain)
5039 error ("no appropriate overload exists for COMPONENT_REF");
5040 return error_mark_node;
5042 return rhs;
5045 case TEMPLATE_ID_EXPR:
5047 explicit_targs = TREE_OPERAND (rhs, 1);
5048 rhs = TREE_OPERAND (rhs, 0);
5050 /* fall through */
5052 case TREE_LIST:
5054 tree elem, baselink, name;
5055 int globals = overloaded_globals_p (rhs);
5057 /* First look for an exact match. Search either overloaded
5058 functions or member functions. May have to undo what
5059 `default_conversion' might do to lhstype. */
5061 if (TYPE_PTRMEMFUNC_P (lhstype))
5062 lhstype = TYPE_PTRMEMFUNC_FN_TYPE (lhstype);
5064 if (TREE_CODE (lhstype) == POINTER_TYPE)
5065 if (TREE_CODE (TREE_TYPE (lhstype)) == FUNCTION_TYPE
5066 || TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE)
5067 lhstype = TREE_TYPE (lhstype);
5068 else
5070 if (complain)
5071 error ("invalid type combination for overload");
5072 return error_mark_node;
5075 if (TREE_CODE (lhstype) != FUNCTION_TYPE && globals > 0)
5077 if (complain)
5078 cp_error ("cannot resolve overloaded function `%D' based on non-function type",
5079 TREE_PURPOSE (rhs));
5080 return error_mark_node;
5083 if (globals > 0)
5085 elem = get_first_fn (rhs);
5086 /* If there are explicit_targs, only a template function
5087 can match. */
5088 if (explicit_targs == NULL_TREE)
5089 while (elem)
5090 if (! comptypes (lhstype, TREE_TYPE (elem), 1))
5091 elem = DECL_CHAIN (elem);
5092 else
5094 mark_used (elem);
5095 return elem;
5098 /* No exact match found, look for a compatible template. */
5100 tree save_elem = 0;
5101 for (elem = get_first_fn (rhs); elem; elem = DECL_CHAIN (elem))
5102 if (TREE_CODE (elem) == TEMPLATE_DECL)
5104 int n = DECL_NTPARMS (elem);
5105 tree t = make_scratch_vec (n);
5106 int i, d = 0;
5107 i = type_unification
5108 (DECL_INNERMOST_TEMPLATE_PARMS (elem),
5109 &TREE_VEC_ELT (t, 0), TYPE_ARG_TYPES (TREE_TYPE (elem)),
5110 TYPE_ARG_TYPES (lhstype), explicit_targs, &d,
5111 1, 1);
5112 if (i == 0)
5114 if (save_elem)
5116 cp_error ("ambiguous template instantiation converting to `%#T'", lhstype);
5117 return error_mark_node;
5119 save_elem = instantiate_template (elem, t);
5120 /* Check the return type. */
5121 if (! comptypes (TREE_TYPE (lhstype),
5122 TREE_TYPE (TREE_TYPE (save_elem)), 1))
5123 save_elem = 0;
5126 if (save_elem)
5128 mark_used (save_elem);
5129 return save_elem;
5133 /* If there are explicit_targs, only a template function
5134 can match. */
5135 if (explicit_targs == NULL_TREE)
5137 /* No match found, look for a compatible function. */
5138 elem = get_first_fn (rhs);
5139 while (elem && comp_target_types (lhstype,
5140 TREE_TYPE (elem), 1) <= 0)
5141 elem = DECL_CHAIN (elem);
5142 if (elem)
5144 tree save_elem = elem;
5145 elem = DECL_CHAIN (elem);
5146 while (elem
5147 && comp_target_types (lhstype,
5148 TREE_TYPE (elem), 0) <= 0)
5149 elem = DECL_CHAIN (elem);
5150 if (elem)
5152 if (complain)
5154 cp_error
5155 ("cannot resolve overload to target type `%#T'",
5156 lhstype);
5157 cp_error_at (" ambiguity between `%#D'",
5158 save_elem);
5159 cp_error_at (" and `%#D', at least", elem);
5161 return error_mark_node;
5163 mark_used (save_elem);
5164 return save_elem;
5167 if (complain)
5169 cp_error ("cannot resolve overload to target type `%#T'",
5170 lhstype);
5171 cp_error
5172 (" because no suitable overload of function `%D' exists",
5173 TREE_PURPOSE (rhs));
5175 return error_mark_node;
5178 if (TREE_NONLOCAL_FLAG (rhs))
5180 /* Got to get it as a baselink. */
5181 rhs = lookup_fnfields (TYPE_BINFO (current_class_type),
5182 TREE_PURPOSE (rhs), 0);
5184 else
5186 my_friendly_assert (TREE_CHAIN (rhs) == NULL_TREE, 181);
5187 if (TREE_CODE (TREE_VALUE (rhs)) == TREE_LIST)
5188 rhs = TREE_VALUE (rhs);
5189 my_friendly_assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL,
5190 182);
5193 for (baselink = rhs; baselink;
5194 baselink = next_baselink (baselink))
5196 elem = TREE_VALUE (baselink);
5197 while (elem)
5198 if (comptypes (lhstype, TREE_TYPE (elem), 1))
5200 mark_used (elem);
5201 return elem;
5203 else
5204 elem = DECL_CHAIN (elem);
5207 /* No exact match found, look for a compatible method. */
5208 for (baselink = rhs; baselink;
5209 baselink = next_baselink (baselink))
5211 elem = TREE_VALUE (baselink);
5212 while (elem && comp_target_types (lhstype,
5213 TREE_TYPE (elem), 1) <= 0)
5214 elem = DECL_CHAIN (elem);
5215 if (elem)
5217 tree save_elem = elem;
5218 elem = DECL_CHAIN (elem);
5219 while (elem && comp_target_types (lhstype,
5220 TREE_TYPE (elem), 0) <= 0)
5221 elem = DECL_CHAIN (elem);
5222 if (elem)
5224 if (complain)
5225 error ("ambiguous overload for overloaded method requested");
5226 return error_mark_node;
5228 mark_used (save_elem);
5229 return save_elem;
5231 name = DECL_NAME (TREE_VALUE (rhs));
5232 #if 0
5233 if (TREE_CODE (lhstype) == FUNCTION_TYPE && globals < 0)
5235 /* Try to instantiate from non-member functions. */
5236 rhs = lookup_name_nonclass (name);
5237 if (rhs && TREE_CODE (rhs) == TREE_LIST)
5239 /* This code seems to be missing a `return'. */
5240 my_friendly_abort (4);
5241 instantiate_type (lhstype, rhs, complain);
5244 #endif
5246 if (complain)
5247 cp_error ("no compatible member functions named `%D'", name);
5248 return error_mark_node;
5251 case CALL_EXPR:
5252 /* This is too hard for now. */
5253 my_friendly_abort (183);
5254 return error_mark_node;
5256 case PLUS_EXPR:
5257 case MINUS_EXPR:
5258 case COMPOUND_EXPR:
5259 TREE_OPERAND (rhs, 0)
5260 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
5261 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5262 return error_mark_node;
5263 TREE_OPERAND (rhs, 1)
5264 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
5265 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5266 return error_mark_node;
5268 TREE_TYPE (rhs) = lhstype;
5269 return rhs;
5271 case MULT_EXPR:
5272 case TRUNC_DIV_EXPR:
5273 case FLOOR_DIV_EXPR:
5274 case CEIL_DIV_EXPR:
5275 case ROUND_DIV_EXPR:
5276 case RDIV_EXPR:
5277 case TRUNC_MOD_EXPR:
5278 case FLOOR_MOD_EXPR:
5279 case CEIL_MOD_EXPR:
5280 case ROUND_MOD_EXPR:
5281 case FIX_ROUND_EXPR:
5282 case FIX_FLOOR_EXPR:
5283 case FIX_CEIL_EXPR:
5284 case FIX_TRUNC_EXPR:
5285 case FLOAT_EXPR:
5286 case NEGATE_EXPR:
5287 case ABS_EXPR:
5288 case MAX_EXPR:
5289 case MIN_EXPR:
5290 case FFS_EXPR:
5292 case BIT_AND_EXPR:
5293 case BIT_IOR_EXPR:
5294 case BIT_XOR_EXPR:
5295 case LSHIFT_EXPR:
5296 case RSHIFT_EXPR:
5297 case LROTATE_EXPR:
5298 case RROTATE_EXPR:
5300 case PREINCREMENT_EXPR:
5301 case PREDECREMENT_EXPR:
5302 case POSTINCREMENT_EXPR:
5303 case POSTDECREMENT_EXPR:
5304 if (complain)
5305 error ("invalid operation on uninstantiated type");
5306 return error_mark_node;
5308 case TRUTH_AND_EXPR:
5309 case TRUTH_OR_EXPR:
5310 case TRUTH_XOR_EXPR:
5311 case LT_EXPR:
5312 case LE_EXPR:
5313 case GT_EXPR:
5314 case GE_EXPR:
5315 case EQ_EXPR:
5316 case NE_EXPR:
5317 case TRUTH_ANDIF_EXPR:
5318 case TRUTH_ORIF_EXPR:
5319 case TRUTH_NOT_EXPR:
5320 if (complain)
5321 error ("not enough type information");
5322 return error_mark_node;
5324 case COND_EXPR:
5325 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
5327 if (complain)
5328 error ("not enough type information");
5329 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;
5335 TREE_OPERAND (rhs, 2)
5336 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), complain);
5337 if (TREE_OPERAND (rhs, 2) == error_mark_node)
5338 return error_mark_node;
5340 TREE_TYPE (rhs) = lhstype;
5341 return rhs;
5343 case MODIFY_EXPR:
5344 TREE_OPERAND (rhs, 1)
5345 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
5346 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5347 return error_mark_node;
5349 TREE_TYPE (rhs) = lhstype;
5350 return rhs;
5352 case ADDR_EXPR:
5353 if (TYPE_PTRMEMFUNC_P (lhstype))
5354 lhstype = TYPE_PTRMEMFUNC_FN_TYPE (lhstype);
5355 else if (TREE_CODE (lhstype) != POINTER_TYPE)
5357 if (complain)
5358 error ("type for resolving address of overloaded function must be pointer type");
5359 return error_mark_node;
5362 tree fn = instantiate_type (TREE_TYPE (lhstype), TREE_OPERAND (rhs, 0), complain);
5363 if (fn == error_mark_node)
5364 return error_mark_node;
5365 mark_addressable (fn);
5366 TREE_TYPE (rhs) = lhstype;
5367 TREE_OPERAND (rhs, 0) = fn;
5368 TREE_CONSTANT (rhs) = staticp (fn);
5369 if (TREE_CODE (lhstype) == POINTER_TYPE
5370 && TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE)
5372 build_ptrmemfunc_type (lhstype);
5373 rhs = build_ptrmemfunc (lhstype, rhs, 0);
5376 return rhs;
5378 case ENTRY_VALUE_EXPR:
5379 my_friendly_abort (184);
5380 return error_mark_node;
5382 case ERROR_MARK:
5383 return error_mark_node;
5385 default:
5386 my_friendly_abort (185);
5387 return error_mark_node;
5391 /* Return the name of the virtual function pointer field
5392 (as an IDENTIFIER_NODE) for the given TYPE. Note that
5393 this may have to look back through base types to find the
5394 ultimate field name. (For single inheritance, these could
5395 all be the same name. Who knows for multiple inheritance). */
5397 static tree
5398 get_vfield_name (type)
5399 tree type;
5401 tree binfo = TYPE_BINFO (type);
5402 char *buf;
5404 while (BINFO_BASETYPES (binfo)
5405 && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
5406 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
5407 binfo = BINFO_BASETYPE (binfo, 0);
5409 type = BINFO_TYPE (binfo);
5410 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
5411 + TYPE_NAME_LENGTH (type) + 2);
5412 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
5413 return get_identifier (buf);
5416 void
5417 print_class_statistics ()
5419 #ifdef GATHER_STATISTICS
5420 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
5421 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
5422 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
5423 n_build_method_call, n_inner_fields_searched);
5424 if (n_vtables)
5426 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
5427 n_vtables, n_vtable_searches);
5428 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
5429 n_vtable_entries, n_vtable_elems);
5431 #endif
5434 /* Push an obstack which is sufficiently long-lived to hold such class
5435 decls that may be cached in the previous_class_values list. For now, let's
5436 use the permanent obstack, later we may create a dedicated obstack just
5437 for this purpose. The effect is undone by pop_obstacks. */
5439 void
5440 maybe_push_cache_obstack ()
5442 push_obstacks_nochange ();
5443 if (current_class_depth == 1)
5444 current_obstack = &permanent_obstack;
5447 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
5448 according to [class]:
5449 The class-name is also inserted
5450 into the scope of the class itself. For purposes of access checking,
5451 the inserted class name is treated as if it were a public member name. */
5453 tree
5454 build_self_reference ()
5456 tree name = constructor_name (current_class_type);
5457 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
5458 DECL_NONLOCAL (value) = 1;
5459 DECL_CONTEXT (value) = current_class_type;
5460 DECL_CLASS_CONTEXT (value) = current_class_type;
5461 CLASSTYPE_LOCAL_TYPEDECLS (current_class_type) = 1;
5462 DECL_ARTIFICIAL (value) = 1;
5464 pushdecl_class_level (value);
5465 return value;