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)
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. */
35 #define obstack_chunk_alloc xmalloc
36 #define obstack_chunk_free free
38 extern struct obstack permanent_obstack;
40 /* This is how we tell when two virtual member functions are really the
42 #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
44 extern void set_class_shadows PROTO ((tree));
46 /* The number of nested classes being processed. If we are not in the
47 scope of any class, this is zero. */
49 int current_class_depth;
51 /* In order to deal with nested classes, we keep a stack of classes.
52 The topmost entry is the innermost class, and is the entry at index
53 CURRENT_CLASS_DEPTH */
55 typedef struct class_stack_node {
56 /* The name of the class. */
59 /* The _TYPE node for the class. */
62 /* The access specifier pending for new declarations in the scope of
65 }* class_stack_node_t;
67 /* The stack itself. This is an dynamically resized array. The
68 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
69 static int current_class_stack_size;
70 static class_stack_node_t current_class_stack;
72 /* When we're processing a member function, current_class_ptr is the
73 PARM_DECL for the `this' pointer. The current_class_ref is an
74 expression for `*this'. */
75 tree current_class_ptr, current_class_ref;
77 /* The following two can be derived from the previous one */
78 tree current_class_name; /* IDENTIFIER_NODE: name of current class */
79 tree current_class_type; /* _TYPE: the type of the current class */
80 tree current_access_specifier;
81 tree previous_class_type; /* _TYPE: the previous type that was a class */
82 tree previous_class_values; /* TREE_LIST: copy of the class_shadowed list
83 when leaving an outermost class scope. */
87 static tree get_vfield_name PROTO((tree));
88 static void finish_struct_anon PROTO((tree));
89 static tree build_vbase_pointer PROTO((tree, tree));
90 static tree build_vtable_entry PROTO((tree, tree));
91 static tree get_vtable_name PROTO((tree));
92 static tree get_derived_offset PROTO((tree, tree));
93 static tree get_basefndecls PROTO((tree, tree));
94 static void set_rtti_entry PROTO((tree, tree, tree));
95 static tree build_vtable PROTO((tree, tree));
96 static void prepare_fresh_vtable PROTO((tree, tree));
97 static void fixup_vtable_deltas1 PROTO((tree, tree));
98 static void fixup_vtable_deltas PROTO((tree, int, tree));
99 static void finish_vtbls PROTO((tree, int, tree));
100 static void modify_vtable_entry PROTO((tree, tree, tree));
101 static tree get_vtable_entry_n PROTO((tree, unsigned HOST_WIDE_INT));
102 static void add_virtual_function PROTO((tree *, tree *, int *, tree, tree));
103 static tree delete_duplicate_fields_1 PROTO((tree, tree));
104 static void delete_duplicate_fields PROTO((tree));
105 static void finish_struct_bits PROTO((tree, int));
106 static int alter_access PROTO((tree, tree, tree, tree));
107 static void handle_using_decl PROTO((tree, tree, tree, tree));
108 static int overrides PROTO((tree, tree));
109 static int strictly_overrides PROTO((tree, tree));
110 static void merge_overrides PROTO((tree, tree, int, tree));
111 static void override_one_vtable PROTO((tree, tree, tree));
112 static void mark_overriders PROTO((tree, tree));
113 static void check_for_override PROTO((tree, tree));
114 static tree maybe_fixup_vptrs PROTO((tree, tree, tree));
115 static tree get_class_offset_1 PROTO((tree, tree, tree, tree, tree));
116 static tree get_class_offset PROTO((tree, tree, tree, tree));
117 static void modify_one_vtable PROTO((tree, tree, tree, tree));
118 static void modify_all_vtables PROTO((tree, tree, tree));
119 static void modify_all_direct_vtables PROTO((tree, int, tree, tree,
121 static void modify_all_indirect_vtables PROTO((tree, int, int, tree,
123 static void build_class_init_list PROTO((tree));
124 static int finish_base_struct PROTO((tree, struct base_info *));
125 static void finish_struct_methods PROTO((tree));
126 static void maybe_warn_about_overly_private_class PROTO ((tree));
127 static void check_member_decl_is_same_in_complete_scope PROTO((tree, tree));
128 static tree make_method_vec PROTO((int));
129 static void free_method_vec PROTO((tree));
130 static tree add_implicitly_declared_members PROTO((tree, int, int, int));
132 /* Way of stacking language names. */
133 tree *current_lang_base, *current_lang_stack;
134 int current_lang_stacksize;
136 /* Names of languages we recognize. */
137 tree lang_name_c, lang_name_cplusplus, lang_name_java;
138 tree current_lang_name;
140 /* When layout out an aggregate type, the size of the
141 basetypes (virtual and non-virtual) is passed to layout_record
143 static tree base_layout_decl;
145 /* Constants used for access control. */
146 tree access_default_node; /* 0 */
147 tree access_public_node; /* 1 */
148 tree access_protected_node; /* 2 */
149 tree access_private_node; /* 3 */
150 tree access_default_virtual_node; /* 4 */
151 tree access_public_virtual_node; /* 5 */
152 tree access_protected_virtual_node; /* 6 */
153 tree access_private_virtual_node; /* 7 */
155 /* Variables shared between class.c and call.c. */
157 #ifdef GATHER_STATISTICS
159 int n_vtable_entries = 0;
160 int n_vtable_searches = 0;
161 int n_vtable_elems = 0;
162 int n_convert_harshness = 0;
163 int n_compute_conversion_costs = 0;
164 int n_build_method_call = 0;
165 int n_inner_fields_searched = 0;
168 /* Virtual baseclass things. */
171 build_vbase_pointer (exp, type)
175 FORMAT_VBASE_NAME (name, type);
177 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
181 /* Is the type of the EXPR, the complete type of the object?
182 If we are going to be wrong, we must be conservative, and return 0. */
185 complete_type_p (expr)
188 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
191 switch (TREE_CODE (expr))
198 expr = TREE_OPERAND (expr, 0);
202 if (! TREE_HAS_CONSTRUCTOR (expr))
204 /* fall through... */
207 if (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
208 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr)))
209 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
211 /* fall through... */
214 if (IS_AGGR_TYPE (TREE_TYPE (expr))
215 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
217 /* fall through... */
228 /* Build multi-level access to EXPR using hierarchy path PATH.
229 CODE is PLUS_EXPR if we are going with the grain,
230 and MINUS_EXPR if we are not (in which case, we cannot traverse
231 virtual baseclass links).
233 TYPE is the type we want this path to have on exit.
235 NONNULL is non-zero if we know (for any reason) that EXPR is
236 not, in fact, zero. */
239 build_vbase_path (code, type, expr, path, nonnull)
241 tree type, expr, path;
244 register int changed = 0;
245 tree last = NULL_TREE, last_virtual = NULL_TREE;
247 tree null_expr = 0, nonnull_expr;
249 tree offset = integer_zero_node;
251 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
252 return build1 (NOP_EXPR, type, expr);
254 /* If -fthis-is-variable, we might have set nonnull incorrectly. We
255 don't care enough to get this right, so just clear it. */
256 if (flag_this_is_variable > 0)
259 /* We could do better if we had additional logic to convert back to the
260 unconverted type (the static type of the complete object), and then
261 convert back to the type we want. Until that is done, we only optimize
262 if the complete type is the same type as expr has. */
263 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
265 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
266 expr = save_expr (expr);
269 if (BINFO_INHERITANCE_CHAIN (path))
270 path = reverse_path (path);
272 basetype = BINFO_TYPE (path);
276 if (TREE_VIA_VIRTUAL (path))
278 last_virtual = BINFO_TYPE (path);
279 if (code == PLUS_EXPR)
281 changed = ! fixed_type_p;
287 /* We already check for ambiguous things in the caller, just
291 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
292 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
294 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
295 nonnull_expr = build_vbase_pointer (ind, last_virtual);
297 && TREE_CODE (type) == POINTER_TYPE
298 && null_expr == NULL_TREE)
300 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
301 expr = build (COND_EXPR, build_pointer_type (last_virtual),
302 build (EQ_EXPR, boolean_type_node, expr,
304 null_expr, nonnull_expr);
307 /* else we'll figure out the offset below. */
309 /* Happens in the case of parse errors. */
310 if (nonnull_expr == error_mark_node)
311 return error_mark_node;
315 cp_error ("cannot cast up from virtual baseclass `%T'",
317 return error_mark_node;
321 path = BINFO_INHERITANCE_CHAIN (path);
323 /* LAST is now the last basetype assoc on the path. */
325 /* A pointer to a virtual base member of a non-null object
326 is non-null. Therefore, we only need to test for zeroness once.
327 Make EXPR the canonical expression to deal with here. */
330 TREE_OPERAND (expr, 2) = nonnull_expr;
331 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
332 = TREE_TYPE (nonnull_expr);
337 /* If we go through any virtual base pointers, make sure that
338 casts to BASETYPE from the last virtual base class use
339 the right value for BASETYPE. */
342 tree intype = TREE_TYPE (TREE_TYPE (expr));
343 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
345 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
346 offset = BINFO_OFFSET (binfo);
353 offset = BINFO_OFFSET (binfo_member (last_virtual,
354 CLASSTYPE_VBASECLASSES (basetype)));
355 offset = size_binop (PLUS_EXPR, offset, BINFO_OFFSET (last));
358 offset = BINFO_OFFSET (last);
361 if (TREE_INT_CST_LOW (offset))
363 /* Bash types to make the backend happy. */
364 offset = cp_convert (type, offset);
366 /* This shouldn't be necessary. (mrs) */
367 expr = build1 (NOP_EXPR, type, expr);
370 /* If expr might be 0, we need to preserve that zeroness. */
374 TREE_TYPE (null_expr) = type;
376 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
377 if (TREE_SIDE_EFFECTS (expr))
378 expr = save_expr (expr);
380 return build (COND_EXPR, type,
381 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
383 build (code, type, expr, offset));
385 else return build (code, type, expr, offset);
388 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
389 be used multiple times in initialization of multiple inheritance. */
392 TREE_TYPE (expr) = type;
396 return build1 (NOP_EXPR, type, expr);
399 /* Virtual function things. */
401 /* Build an entry in the virtual function table.
402 DELTA is the offset for the `this' pointer.
403 PFN is an ADDR_EXPR containing a pointer to the virtual function.
404 Note that the index (DELTA2) in the virtual function table
408 build_vtable_entry (delta, pfn)
411 if (flag_vtable_thunks)
413 HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta);
414 if (idelta && ! DECL_ABSTRACT_VIRTUAL_P (TREE_OPERAND (pfn, 0)))
416 pfn = build1 (ADDR_EXPR, vtable_entry_type,
417 make_thunk (pfn, idelta));
418 TREE_READONLY (pfn) = 1;
419 TREE_CONSTANT (pfn) = 1;
421 #ifdef GATHER_STATISTICS
422 n_vtable_entries += 1;
428 extern int flag_huge_objects;
429 tree elems = expr_tree_cons (NULL_TREE, delta,
430 expr_tree_cons (NULL_TREE, integer_zero_node,
431 build_expr_list (NULL_TREE, pfn)));
432 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
434 /* DELTA used to be constructed by `size_int' and/or size_binop,
435 which caused overflow problems when it was negative. That should
438 if (! int_fits_type_p (delta, delta_type_node))
440 if (flag_huge_objects)
441 sorry ("object size exceeds built-in limit for virtual function table implementation");
443 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
446 TREE_CONSTANT (entry) = 1;
447 TREE_STATIC (entry) = 1;
448 TREE_READONLY (entry) = 1;
450 #ifdef GATHER_STATISTICS
451 n_vtable_entries += 1;
458 /* We want to give the assembler the vtable identifier as well as
459 the offset to the function pointer. So we generate
461 __asm__ __volatile__ (".vtable_entry %c0, %c1"
462 : : "s"(&class_vtable),
463 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
466 build_vtable_entry_ref (basetype, vtbl, idx)
467 tree basetype, vtbl, idx;
469 static char asm_stmt[] = ".vtable_entry %c0, %c1";
472 s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
473 s = build_tree_list (build_string (1, "s"), s);
475 i = build_array_ref (vtbl, idx);
476 if (!flag_vtable_thunks)
477 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
478 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
479 i2 = build_array_ref (vtbl, build_int_2(0,0));
480 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
481 i = build_binary_op (MINUS_EXPR, i, i2, 0);
482 i = build_tree_list (build_string (1, "i"), i);
484 expand_asm_operands (build_string (sizeof(asm_stmt)-1, asm_stmt),
485 NULL_TREE, chainon (s, i), NULL_TREE, 1, NULL, 0);
488 /* Given an object INSTANCE, return an expression which yields the
489 virtual function vtable element corresponding to INDEX. There are
490 many special cases for INSTANCE which we take care of here, mainly
491 to avoid creating extra tree nodes when we don't have to. */
494 build_vtbl_ref (instance, idx)
498 tree basetype = TREE_TYPE (instance);
500 if (TREE_CODE (basetype) == REFERENCE_TYPE)
501 basetype = TREE_TYPE (basetype);
503 if (instance == current_class_ref)
504 vtbl = build_vfield_ref (instance, basetype);
509 /* Try to figure out what a reference refers to, and
510 access its virtual function table directly. */
511 tree ref = NULL_TREE;
513 if (TREE_CODE (instance) == INDIRECT_REF
514 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
515 ref = TREE_OPERAND (instance, 0);
516 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
519 if (ref && TREE_CODE (ref) == VAR_DECL
520 && DECL_INITIAL (ref))
522 tree init = DECL_INITIAL (ref);
524 while (TREE_CODE (init) == NOP_EXPR
525 || TREE_CODE (init) == NON_LVALUE_EXPR)
526 init = TREE_OPERAND (init, 0);
527 if (TREE_CODE (init) == ADDR_EXPR)
529 init = TREE_OPERAND (init, 0);
530 if (IS_AGGR_TYPE (TREE_TYPE (init))
531 && (TREE_CODE (init) == PARM_DECL
532 || TREE_CODE (init) == VAR_DECL))
538 if (IS_AGGR_TYPE (TREE_TYPE (instance))
539 && (TREE_CODE (instance) == RESULT_DECL
540 || TREE_CODE (instance) == PARM_DECL
541 || TREE_CODE (instance) == VAR_DECL))
542 vtbl = TYPE_BINFO_VTABLE (basetype);
544 vtbl = build_vfield_ref (instance, basetype);
547 assemble_external (vtbl);
550 build_vtable_entry_ref (basetype, vtbl, idx);
552 aref = build_array_ref (vtbl, idx);
557 /* Given an object INSTANCE, return an expression which yields the
558 virtual function corresponding to INDEX. There are many special
559 cases for INSTANCE which we take care of here, mainly to avoid
560 creating extra tree nodes when we don't have to. */
563 build_vfn_ref (ptr_to_instptr, instance, idx)
564 tree *ptr_to_instptr, instance;
567 tree aref = build_vtbl_ref (instance, idx);
569 /* When using thunks, there is no extra delta, and we get the pfn
571 if (flag_vtable_thunks)
576 /* Save the intermediate result in a SAVE_EXPR so we don't have to
577 compute each component of the virtual function pointer twice. */
578 if (TREE_CODE (aref) == INDIRECT_REF)
579 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
582 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
584 cp_convert (ptrdiff_type_node,
585 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
588 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
591 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
592 for the given TYPE. */
595 get_vtable_name (type)
598 tree type_id = build_typename_overload (type);
599 char *buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
600 + IDENTIFIER_LENGTH (type_id) + 2);
601 char *ptr = IDENTIFIER_POINTER (type_id);
603 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
605 /* We don't take off the numbers; prepare_fresh_vtable uses the
606 DECL_ASSEMBLER_NAME for the type, which includes the number
607 in `3foo'. If we were to pull them off here, we'd end up with
608 something like `_vt.foo.3bar', instead of a uniform definition. */
609 while (ptr[i] >= '0' && ptr[i] <= '9')
612 sprintf (buf, VTABLE_NAME_FORMAT, ptr+i);
613 return get_identifier (buf);
616 /* Return the offset to the main vtable for a given base BINFO. */
619 get_vfield_offset (binfo)
623 = size_binop (FLOOR_DIV_EXPR,
624 DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))),
625 size_int (BITS_PER_UNIT));
626 tmp = convert (sizetype, tmp);
627 return size_binop (PLUS_EXPR, tmp, BINFO_OFFSET (binfo));
630 /* Get the offset to the start of the original binfo that we derived
631 this binfo from. If we find TYPE first, return the offset only
632 that far. The shortened search is useful because the this pointer
633 on method calling is expected to point to a DECL_CONTEXT (fndecl)
634 object, and not a baseclass of it. */
637 get_derived_offset (binfo, type)
640 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
643 while (BINFO_BASETYPES (binfo)
644 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
646 tree binfos = BINFO_BASETYPES (binfo);
647 if (BINFO_TYPE (binfo) == type)
649 binfo = TREE_VEC_ELT (binfos, i);
651 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
652 return size_binop (MINUS_EXPR, offset1, offset2);
655 /* Update the rtti info for this class. */
658 set_rtti_entry (virtuals, offset, type)
659 tree virtuals, offset, type;
664 vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, get_tinfo_fn (type));
666 vfn = build1 (NOP_EXPR, vfunc_ptr_type_node, size_zero_node);
667 TREE_CONSTANT (vfn) = 1;
669 if (! flag_vtable_thunks)
670 TREE_VALUE (virtuals) = build_vtable_entry (offset, vfn);
673 tree voff = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
674 TREE_CONSTANT (voff) = 1;
676 TREE_VALUE (virtuals) = build_vtable_entry (integer_zero_node, voff);
678 /* The second slot is for the tdesc pointer when thunks are used. */
679 TREE_VALUE (TREE_CHAIN (virtuals))
680 = build_vtable_entry (integer_zero_node, vfn);
684 /* Build a virtual function for type TYPE.
685 If BINFO is non-NULL, build the vtable starting with the initial
686 approximation that it is the same as the one which is the head of
687 the association list. */
690 build_vtable (binfo, type)
693 tree name = get_vtable_name (type);
700 virtuals = copy_list (BINFO_VIRTUALS (binfo));
701 decl = build_decl (VAR_DECL, name, TREE_TYPE (BINFO_VTABLE (binfo)));
703 /* Now do rtti stuff. */
704 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
705 offset = ssize_binop (MINUS_EXPR, integer_zero_node, offset);
706 set_rtti_entry (virtuals, offset, type);
710 virtuals = NULL_TREE;
711 decl = build_decl (VAR_DECL, name, void_type_node);
714 #ifdef GATHER_STATISTICS
716 n_vtable_elems += list_length (virtuals);
719 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
720 import_export_vtable (decl, type, 0);
722 decl = pushdecl_top_level (decl);
723 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
724 /* Initialize the association list for this type, based
725 on our first approximation. */
726 TYPE_BINFO_VTABLE (type) = decl;
727 TYPE_BINFO_VIRTUALS (type) = virtuals;
729 DECL_ARTIFICIAL (decl) = 1;
730 TREE_STATIC (decl) = 1;
731 #ifndef WRITABLE_VTABLES
732 /* Make them READONLY by default. (mrs) */
733 TREE_READONLY (decl) = 1;
735 /* At one time the vtable info was grabbed 2 words at a time. This
736 fails on sparc unless you have 8-byte alignment. (tiemann) */
737 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
740 DECL_VIRTUAL_P (decl) = 1;
741 DECL_CONTEXT (decl) = type;
743 binfo = TYPE_BINFO (type);
744 SET_BINFO_NEW_VTABLE_MARKED (binfo);
748 extern tree signed_size_zero_node;
750 /* Give TYPE a new virtual function table which is initialized
751 with a skeleton-copy of its original initialization. The only
752 entry that changes is the `delta' entry, so we can really
753 share a lot of structure.
755 FOR_TYPE is the derived type which caused this table to
758 BINFO is the type association which provided TYPE for FOR_TYPE.
760 The order in which vtables are built (by calling this function) for
761 an object must remain the same, otherwise a binary incompatibility
765 prepare_fresh_vtable (binfo, for_type)
766 tree binfo, for_type;
769 tree orig_decl = BINFO_VTABLE (binfo);
782 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
784 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
785 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
787 /* We know that the vtable that we are going to create doesn't exist
788 yet in the global namespace, and when we finish, it will be
789 pushed into the global namespace. In complex MI hierarchies, we
790 have to loop while the name we are thinking of adding is globally
791 defined, adding more name components to the vtable name as we
792 loop, until the name is unique. This is because in complex MI
793 cases, we might have the same base more than once. This means
794 that the order in which this function is called for vtables must
795 remain the same, otherwise binary compatibility can be
800 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
804 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
806 buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT) + strlen (buf1) + 1);
807 sprintf (buf, VTABLE_NAME_FORMAT, buf1);
808 name = get_identifier (buf);
810 /* If this name doesn't clash, then we can use it, otherwise
811 we add more to the name until it is unique. */
813 if (! IDENTIFIER_GLOBAL_VALUE (name))
816 /* Set values for next loop through, if the name isn't unique. */
818 path = BINFO_INHERITANCE_CHAIN (path);
820 /* We better not run out of stuff to make it unique. */
821 my_friendly_assert (path != NULL_TREE, 368);
823 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
825 if (for_type == basetype)
827 /* If we run out of basetypes in the path, we have already
828 found created a vtable with that name before, we now
829 resort to tacking on _%d to distinguish them. */
831 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
832 buf1 = (char *) alloca (i);
834 sprintf (buf1, "%s%c%s%c%d",
835 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
837 buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
838 + strlen (buf1) + 1);
839 sprintf (buf, VTABLE_NAME_FORMAT, buf1);
840 name = get_identifier (buf);
842 /* If this name doesn't clash, then we can use it,
843 otherwise we add something different to the name until
845 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
847 /* Hey, they really like MI don't they? Increase the 3
848 above to 6, and the 999 to 999999. :-) */
849 my_friendly_assert (j <= 999, 369);
854 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
855 new_buf2 = (char *) alloca (i);
856 sprintf (new_buf2, "%s%c%s",
857 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
861 new_decl = build_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
862 /* Remember which class this vtable is really for. */
863 DECL_CONTEXT (new_decl) = for_type;
865 DECL_ARTIFICIAL (new_decl) = 1;
866 TREE_STATIC (new_decl) = 1;
867 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
868 DECL_VIRTUAL_P (new_decl) = 1;
869 #ifndef WRITABLE_VTABLES
870 /* Make them READONLY by default. (mrs) */
871 TREE_READONLY (new_decl) = 1;
873 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
875 /* Make fresh virtual list, so we can smash it later. */
876 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
878 if (TREE_VIA_VIRTUAL (binfo))
880 tree binfo1 = binfo_member (BINFO_TYPE (binfo),
881 CLASSTYPE_VBASECLASSES (for_type));
883 /* XXX - This should never happen, if it does, the caller should
884 ensure that the binfo is from for_type's binfos, not from any
885 base type's. We can remove all this code after a while. */
887 warning ("internal inconsistency: binfo offset error for rtti");
889 offset = BINFO_OFFSET (binfo1);
892 offset = BINFO_OFFSET (binfo);
894 set_rtti_entry (BINFO_VIRTUALS (binfo),
895 ssize_binop (MINUS_EXPR, integer_zero_node, offset),
898 #ifdef GATHER_STATISTICS
900 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
903 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
904 import_export_vtable (new_decl, for_type, 0);
906 if (TREE_VIA_VIRTUAL (binfo))
907 my_friendly_assert (binfo == binfo_member (BINFO_TYPE (binfo),
908 CLASSTYPE_VBASECLASSES (current_class_type)),
910 SET_BINFO_NEW_VTABLE_MARKED (binfo);
914 /* Access the virtual function table entry that logically
915 contains BASE_FNDECL. VIRTUALS is the virtual function table's
916 initializer. We can run off the end, when dealing with virtual
917 destructors in MI situations, return NULL_TREE in that case. */
920 get_vtable_entry (virtuals, base_fndecl)
921 tree virtuals, base_fndecl;
923 unsigned HOST_WIDE_INT n = (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
924 ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
925 & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1))
926 : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
928 #ifdef GATHER_STATISTICS
929 n_vtable_searches += n;
932 while (n > 0 && virtuals)
935 virtuals = TREE_CHAIN (virtuals);
941 /* Put new entry ENTRY into virtual function table initializer
944 Also update DECL_VINDEX (FNDECL). */
947 modify_vtable_entry (old_entry_in_list, new_entry, fndecl)
948 tree old_entry_in_list, new_entry, fndecl;
950 tree base_fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list)), 0);
953 cp_warning ("replaced %D with %D", DECL_ASSEMBLER_NAME (base_fndecl),
954 DECL_ASSEMBLER_NAME (fndecl));
956 TREE_VALUE (old_entry_in_list) = new_entry;
958 /* Now assign virtual dispatch information, if unset. */
959 /* We can dispatch this, through any overridden base function. */
960 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
962 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
963 DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
967 /* Access the virtual function table entry N. VIRTUALS is the virtual
968 function table's initializer. */
971 get_vtable_entry_n (virtuals, n)
973 unsigned HOST_WIDE_INT n;
978 virtuals = TREE_CHAIN (virtuals);
983 /* Add a virtual function to all the appropriate vtables for the class
984 T. DECL_VINDEX(X) should be error_mark_node, if we want to
985 allocate a new slot in our table. If it is error_mark_node, we
986 know that no other function from another vtable is overridden by X.
987 HAS_VIRTUAL keeps track of how many virtuals there are in our main
988 vtable for the type, and we build upon the PENDING_VIRTUALS list
992 add_virtual_function (pv, phv, has_virtual, fndecl, t)
996 tree t; /* Structure type. */
998 tree pending_virtuals = *pv;
999 tree pending_hard_virtuals = *phv;
1001 /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
1002 convert to void *. Make such a conversion here. */
1003 tree vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
1004 TREE_CONSTANT (vfn) = 1;
1007 if (current_class_type == 0)
1008 cp_warning ("internal problem, current_class_type is zero when adding `%D', please report",
1010 if (current_class_type && t != current_class_type)
1011 cp_warning ("internal problem, current_class_type differs when adding `%D', please report",
1015 /* If the virtual function is a redefinition of a prior one,
1016 figure out in which base class the new definition goes,
1017 and if necessary, make a fresh virtual function table
1018 to hold that entry. */
1019 if (DECL_VINDEX (fndecl) == error_mark_node)
1023 /* We remember that this was the base sub-object for rtti. */
1024 CLASSTYPE_RTTI (t) = t;
1026 /* If we are using thunks, use two slots at the front, one
1027 for the offset pointer, one for the tdesc pointer. */
1028 if (*has_virtual == 0 && flag_vtable_thunks)
1033 /* Build a new INT_CST for this DECL_VINDEX. */
1035 static tree index_table[256];
1037 /* We skip a slot for the offset/tdesc entry. */
1038 int i = ++(*has_virtual);
1040 if (i >= 256 || index_table[i] == 0)
1042 idx = build_int_2 (i, 0);
1044 index_table[i] = idx;
1047 idx = index_table[i];
1049 /* Now assign virtual dispatch information. */
1050 DECL_VINDEX (fndecl) = idx;
1051 DECL_CONTEXT (fndecl) = t;
1053 entry = build_vtable_entry (integer_zero_node, vfn);
1054 pending_virtuals = tree_cons (DECL_VINDEX (fndecl), entry, pending_virtuals);
1056 /* Might already be INTEGER_CST if declared twice in class. We will
1057 give error later or we've already given it. */
1058 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1060 /* Need an entry in some other virtual function table.
1061 Deal with this after we have laid out our virtual base classes. */
1062 pending_hard_virtuals = temp_tree_cons (fndecl, vfn, pending_hard_virtuals);
1064 *pv = pending_virtuals;
1065 *phv = pending_hard_virtuals;
1068 /* Obstack on which to build the vector of class methods. */
1069 struct obstack class_obstack;
1070 extern struct obstack *current_obstack;
1072 /* These are method vectors that were too small for the number of
1073 methods in some class, and so were abandoned. */
1074 static tree free_method_vecs;
1076 /* Returns a method vector with enough room for N methods. N should
1077 be a power of two. */
1086 for (t = &free_method_vecs; *t; t = &(TREE_CHAIN (*t)))
1087 /* Note that we don't use >= n here because we don't want to
1088 allocate a very large vector where it isn't needed. */
1089 if (TREE_VEC_LENGTH (*t) == n)
1092 *t = TREE_CHAIN (new_vec);
1093 TREE_CHAIN (new_vec) = NULL_TREE;
1094 bzero ((PTR) &TREE_VEC_ELT (new_vec, 0), n * sizeof (tree));
1098 new_vec = make_tree_vec (n);
1102 /* Free the method vector VEC. */
1105 free_method_vec (vec)
1108 TREE_CHAIN (vec) = free_method_vecs;
1109 free_method_vecs = vec;
1112 /* Add method METHOD to class TYPE.
1114 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1115 the class type where the method should be added. */
1118 add_method (type, fields, method)
1119 tree type, *fields, method;
1121 push_obstacks (&permanent_obstack, &permanent_obstack);
1123 /* Setting the DECL_CONTEXT and DECL_CLASS_CONTEXT here is probably
1125 DECL_CONTEXT (method) = type;
1126 DECL_CLASS_CONTEXT (method) = type;
1128 if (fields && *fields)
1129 *fields = build_overload (method, *fields);
1135 if (!CLASSTYPE_METHOD_VEC (type))
1136 /* Make a new method vector. We start with 8 entries. We must
1137 allocate at least two (for constructors and destructors), and
1138 we're going to end up with an assignment operator at some
1141 We could use a TREE_LIST for now, and convert it to a
1142 TREE_VEC in finish_struct, but we would probably waste more
1143 memory making the links in the list than we would by
1144 over-allocating the size of the vector here. Furthermore,
1145 we would complicate all the code that expects this to be a
1146 vector. We keep a free list of vectors that we outgrew so
1147 that we don't really waste any memory. */
1148 CLASSTYPE_METHOD_VEC (type) = make_method_vec (8);
1150 method_vec = CLASSTYPE_METHOD_VEC (type);
1151 len = TREE_VEC_LENGTH (method_vec);
1153 if (DECL_NAME (method) == constructor_name (type))
1155 /* A new constructor or destructor. Constructors go in
1156 slot 0; destructors go in slot 1. */
1158 = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
1160 TREE_VEC_ELT (method_vec, slot)
1161 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1167 /* See if we already have an entry with this name. */
1168 for (i = 2; i < len; ++i)
1169 if (!TREE_VEC_ELT (method_vec, i)
1170 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)))
1171 == DECL_NAME (method)))
1176 /* We need a bigger method vector. */
1177 tree new_vec = make_method_vec (2 * len);
1178 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1179 (PTR) &TREE_VEC_ELT (new_vec, 0),
1180 len * sizeof (tree));
1181 free_method_vec (method_vec);
1183 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1185 else if (template_class_depth (type))
1186 /* TYPE is a template class. Don't issue any errors now;
1187 wait until instantiation time to complain. */
1193 /* Check to see if we've already got this method. */
1194 for (fns = TREE_VEC_ELT (method_vec, i);
1196 fns = OVL_NEXT (fns))
1198 tree fn = OVL_CURRENT (fns);
1200 if (TREE_CODE (fn) != TREE_CODE (method))
1203 if (TREE_CODE (method) != TEMPLATE_DECL)
1205 /* Since this is an ordinary function in a
1206 non-template class, it's mangled name can be
1207 used as a unique identifier. This technique
1208 is only an optimization; we would get the
1209 same results if we just used decls_match
1211 if (DECL_ASSEMBLER_NAME (fn)
1212 != DECL_ASSEMBLER_NAME (method))
1215 else if (!decls_match (fn, method))
1218 /* There has already been a declaration of this
1219 method or member template. */
1220 cp_error_at ("`%D' has already been declared in `%T'",
1223 /* We don't call duplicate_decls here to merege the
1224 declarations because that will confuse things if
1225 the methods have inline definitions In
1226 particular, we will crash while processing the
1232 if (DECL_CONV_FN_P (method))
1234 /* Type conversion operators have to come before
1235 ordinary methods; add_conversions depends on this to
1236 speed up looking for conversion operators. So, if
1237 necessary, we slide some of the vector elements up.
1238 In theory, this makes this algorithm O(N^2) but we
1239 don't expect many conversion operators. */
1240 for (i = 2; i < len; ++i)
1242 tree fn = TREE_VEC_ELT (method_vec, i);
1245 /* There are no more entries in the vector, so we
1246 can insert the new conversion operator here. */
1249 if (! DECL_CONV_FN_P (OVL_CURRENT (fn)))
1250 /* We can insert the new function right at the Ith
1255 if (!TREE_VEC_ELT (method_vec, i))
1256 /* There is nothing in the Ith slot, so we can avoid
1261 /* We know the last slot in the vector is empty
1262 because we know that at this point there's room for
1264 bcopy ((PTR) &TREE_VEC_ELT (method_vec, i),
1265 (PTR) &TREE_VEC_ELT (method_vec, i + 1),
1266 (len - i - 1) * sizeof (tree));
1267 TREE_VEC_ELT (method_vec, i) = NULL_TREE;
1271 /* Actually insert the new method. */
1272 TREE_VEC_ELT (method_vec, i)
1273 = build_overload (method, TREE_VEC_ELT (method_vec, i));
1276 if (TYPE_BINFO_BASETYPES (type) && CLASSTYPE_BASELINK_VEC (type))
1278 /* ??? May be better to know whether these can be extended? */
1279 tree baselink_vec = CLASSTYPE_BASELINK_VEC (type);
1281 TREE_VEC_LENGTH (baselink_vec) += 1;
1282 CLASSTYPE_BASELINK_VEC (type) = copy_node (baselink_vec);
1283 TREE_VEC_LENGTH (baselink_vec) -= 1;
1285 TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), len) = 0;
1291 /* Subroutines of finish_struct. */
1293 /* Look through the list of fields for this struct, deleting
1294 duplicates as we go. This must be recursive to handle
1297 FIELD is the field which may not appear anywhere in FIELDS.
1298 FIELD_PTR, if non-null, is the starting point at which
1299 chained deletions may take place.
1300 The value returned is the first acceptable entry found
1303 Note that anonymous fields which are not of UNION_TYPE are
1304 not duplicates, they are just anonymous fields. This happens
1305 when we have unnamed bitfields, for example. */
1308 delete_duplicate_fields_1 (field, fields)
1313 if (DECL_NAME (field) == 0)
1315 if (TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)
1318 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1319 fields = delete_duplicate_fields_1 (x, fields);
1324 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1326 if (DECL_NAME (x) == 0)
1328 if (TREE_CODE (TREE_TYPE (x)) != UNION_TYPE)
1330 TYPE_FIELDS (TREE_TYPE (x))
1331 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1332 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1335 fields = TREE_CHAIN (fields);
1337 TREE_CHAIN (prev) = TREE_CHAIN (x);
1342 if (DECL_NAME (field) == DECL_NAME (x))
1344 if (TREE_CODE (field) == CONST_DECL
1345 && TREE_CODE (x) == CONST_DECL)
1346 cp_error_at ("duplicate enum value `%D'", x);
1347 else if (TREE_CODE (field) == CONST_DECL
1348 || TREE_CODE (x) == CONST_DECL)
1349 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1351 else if (DECL_DECLARES_TYPE_P (field)
1352 && DECL_DECLARES_TYPE_P (x))
1354 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1356 cp_error_at ("duplicate nested type `%D'", x);
1358 else if (DECL_DECLARES_TYPE_P (field)
1359 || DECL_DECLARES_TYPE_P (x))
1361 /* Hide tag decls. */
1362 if ((TREE_CODE (field) == TYPE_DECL
1363 && DECL_ARTIFICIAL (field))
1364 || (TREE_CODE (x) == TYPE_DECL
1365 && DECL_ARTIFICIAL (x)))
1367 cp_error_at ("duplicate field `%D' (as type and non-type)",
1371 cp_error_at ("duplicate member `%D'", x);
1373 fields = TREE_CHAIN (fields);
1375 TREE_CHAIN (prev) = TREE_CHAIN (x);
1384 delete_duplicate_fields (fields)
1388 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1389 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1392 /* Change the access of FDECL to ACCESS in T. The access to FDECL is
1393 along the path given by BINFO. Return 1 if change was legit,
1394 otherwise return 0. */
1397 alter_access (t, binfo, fdecl, access)
1403 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1406 if (TREE_VALUE (elem) != access)
1408 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1409 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1411 error ("conflicting access specifications for field `%s', ignored",
1412 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1416 /* They're changing the access to the same thing they changed
1417 it to before. That's OK. */
1423 enforce_access (binfo, fdecl);
1425 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1431 /* Process the USING_DECL, which is a member of T. The METHOD_VEC, if
1432 non-NULL, is the methods of T. The FIELDS are the fields of T.
1433 Returns 1 if the USING_DECL was valid, 0 otherwise. */
1436 handle_using_decl (using_decl, t, method_vec, fields)
1442 tree ctype = DECL_INITIAL (using_decl);
1443 tree name = DECL_NAME (using_decl);
1445 = TREE_PRIVATE (using_decl) ? access_private_node
1446 : TREE_PROTECTED (using_decl) ? access_protected_node
1447 : access_public_node;
1449 tree flist = NULL_TREE;
1454 binfo = binfo_or_else (ctype, t);
1458 if (name == constructor_name (ctype)
1459 || name == constructor_name_full (ctype))
1460 cp_error_at ("using-declaration for constructor", using_decl);
1462 fdecl = lookup_member (binfo, name, 0, 0);
1466 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1470 /* Functions are represented as TREE_LIST, with the purpose
1471 being the type and the value the functions. Other members
1472 come as themselves. */
1473 if (TREE_CODE (fdecl) == TREE_LIST)
1474 /* Ignore base type this came from. */
1475 fdecl = TREE_VALUE (fdecl);
1477 if (TREE_CODE (fdecl) == OVERLOAD)
1479 /* We later iterate over all functions. */
1481 fdecl = OVL_FUNCTION (flist);
1484 name = DECL_NAME (fdecl);
1485 n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
1486 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); i++)
1487 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)))
1490 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1491 cp_error_at (" because of local method `%#D' with same name",
1492 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
1496 if (! DECL_LANG_SPECIFIC (fdecl))
1497 /* We don't currently handle DECL_ACCESS for TYPE_DECLs; just return. */
1500 for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
1501 if (DECL_NAME (tmp) == name)
1503 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1504 cp_error_at (" because of local field `%#D' with same name", tmp);
1508 /* Make type T see field decl FDECL with access ACCESS.*/
1513 if (alter_access (t, binfo, OVL_FUNCTION (flist),
1516 flist = OVL_CHAIN (flist);
1520 alter_access (t, binfo, fdecl, access);
1523 /* If FOR_TYPE needs to reinitialize virtual function table pointers
1524 for TYPE's sub-objects, add such reinitializations to BASE_INIT_LIST.
1525 Returns BASE_INIT_LIST appropriately modified. */
1528 maybe_fixup_vptrs (for_type, binfo, base_init_list)
1529 tree for_type, binfo, base_init_list;
1531 /* Now reinitialize any slots that don't fall under our virtual
1532 function table pointer. */
1533 tree vfields = CLASSTYPE_VFIELDS (BINFO_TYPE (binfo));
1536 tree basetype = VF_NORMAL_VALUE (vfields)
1537 ? TYPE_MAIN_VARIANT (VF_NORMAL_VALUE (vfields))
1538 : VF_BASETYPE_VALUE (vfields);
1540 tree base_binfo = get_binfo (basetype, for_type, 0);
1541 /* Punt until this is implemented. */
1542 if (1 /* BINFO_MODIFIED (base_binfo) */)
1544 tree base_offset = get_vfield_offset (base_binfo);
1545 if (! tree_int_cst_equal (base_offset, get_vfield_offset (TYPE_BINFO (for_type)))
1546 && ! tree_int_cst_equal (base_offset, get_vfield_offset (binfo)))
1547 base_init_list = tree_cons (error_mark_node, base_binfo,
1550 vfields = TREE_CHAIN (vfields);
1552 return base_init_list;
1555 /* If TYPE does not have a constructor, then the compiler must
1556 manually deal with all of the initialization this type requires.
1558 If a base initializer exists only to fill in the virtual function
1559 table pointer, then we mark that fact with the TREE_VIRTUAL bit.
1560 This way, we avoid multiple initializations of the same field by
1561 each virtual function table up the class hierarchy.
1563 Virtual base class pointers are not initialized here. They are
1564 initialized only at the "top level" of object creation. If we
1565 initialized them here, we would have to skip a lot of work. */
1568 build_class_init_list (type)
1571 tree base_init_list = NULL_TREE;
1572 tree member_init_list = NULL_TREE;
1574 /* Since we build member_init_list and base_init_list using
1575 tree_cons, backwards fields the all through work. */
1577 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
1578 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1580 for (x = TYPE_FIELDS (type); x; x = TREE_CHAIN (x))
1582 if (TREE_CODE (x) != FIELD_DECL)
1585 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (x))
1586 || DECL_INITIAL (x) != NULL_TREE)
1587 member_init_list = tree_cons (x, type, member_init_list);
1589 member_init_list = nreverse (member_init_list);
1591 /* We will end up doing this last. Need special marker
1592 to avoid infinite regress. */
1593 if (TYPE_VIRTUAL_P (type))
1595 base_init_list = build_tree_list (error_mark_node, TYPE_BINFO (type));
1596 if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (type) == 0)
1597 TREE_VALUE (base_init_list) = NULL_TREE;
1598 TREE_ADDRESSABLE (base_init_list) = 1;
1601 /* Each base class which needs to have initialization
1602 of some kind gets to make such requests known here. */
1603 for (i = n_baseclasses-1; i >= 0; i--)
1605 tree base_binfo = TREE_VEC_ELT (binfos, i);
1608 /* Don't initialize virtual baseclasses this way. */
1609 if (TREE_VIA_VIRTUAL (base_binfo))
1612 if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (base_binfo)))
1614 /* ...and the last shall come first... */
1615 base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
1616 base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
1620 if ((blist = CLASSTYPE_BASE_INIT_LIST (BINFO_TYPE (base_binfo))) == NULL_TREE)
1621 /* Nothing to initialize. */
1625 base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
1627 /* This is normally true for single inheritance.
1628 The win is we can shrink the chain of initializations
1629 to be done by only converting to the actual type
1630 we are interested in. */
1631 if (TREE_VALUE (blist)
1632 && TREE_CODE (TREE_VALUE (blist)) == TREE_VEC
1633 && tree_int_cst_equal (BINFO_OFFSET (base_binfo),
1634 BINFO_OFFSET (TREE_VALUE (blist))))
1638 /* Does it do more than just fill in a
1639 virtual function table pointer? */
1640 if (! TREE_ADDRESSABLE (blist))
1641 base_init_list = build_tree_list (blist, base_init_list);
1642 /* Can we get by just with the virtual function table
1643 pointer that it fills in? */
1644 else if (TREE_ADDRESSABLE (base_init_list)
1645 && TREE_VALUE (base_init_list) == 0)
1646 base_init_list = blist;
1647 /* Maybe, but it is not obvious as the previous case. */
1648 else if (! CLASSTYPE_NEEDS_VIRTUAL_REINIT (type))
1650 tree last = tree_last (base_init_list);
1651 while (TREE_VALUE (last)
1652 && TREE_CODE (TREE_VALUE (last)) == TREE_LIST)
1653 last = tree_last (TREE_VALUE (last));
1654 if (TREE_VALUE (last) == 0)
1655 base_init_list = build_tree_list (blist, base_init_list);
1659 base_init_list = blist;
1663 /* The function expand_aggr_init knows how to do the
1664 initialization of `basetype' without getting
1665 an explicit `blist'. */
1667 base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
1669 base_init_list = CLASSTYPE_BINFO_AS_LIST (BINFO_TYPE (base_binfo));
1675 if (member_init_list)
1676 CLASSTYPE_BASE_INIT_LIST (type) =
1677 build_tree_list (base_init_list, member_init_list);
1679 CLASSTYPE_BASE_INIT_LIST (type) = base_init_list;
1681 else if (member_init_list)
1682 CLASSTYPE_BASE_INIT_LIST (type) = member_init_list;
1688 int max_has_virtual;
1693 char cant_have_default_ctor;
1694 char cant_have_const_ctor;
1695 char no_const_asn_ref;
1698 /* Record information about type T derived from its base classes.
1699 Store most of that information in T itself, and place the
1700 remaining information in the struct BASE_INFO.
1702 Propagate basetype offsets throughout the lattice. Note that the
1703 lattice topped by T is really a pair: it's a DAG that gives the
1704 structure of the derivation hierarchy, and it's a list of the
1705 virtual baseclasses that appear anywhere in the DAG. When a vbase
1706 type appears in the DAG, it's offset is 0, and it's children start
1707 their offsets from that point. When a vbase type appears in the list,
1708 its offset is the offset it has in the hierarchy, and its children's
1709 offsets include that offset in theirs.
1711 Returns the index of the first base class to have virtual functions,
1712 or -1 if no such base class. */
1715 finish_base_struct (t, b)
1717 struct base_info *b;
1719 tree binfos = TYPE_BINFO_BASETYPES (t);
1720 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1721 int first_vfn_base_index = -1;
1722 bzero ((char *) b, sizeof (struct base_info));
1724 for (i = 0; i < n_baseclasses; i++)
1726 tree base_binfo = TREE_VEC_ELT (binfos, i);
1727 tree basetype = BINFO_TYPE (base_binfo);
1729 /* Effective C++ rule 14. We only need to check TYPE_VIRTUAL_P
1730 here because the case of virtual functions but non-virtual
1731 dtor is handled in finish_struct_1. */
1732 if (warn_ecpp && ! TYPE_VIRTUAL_P (basetype)
1733 && TYPE_HAS_DESTRUCTOR (basetype))
1734 cp_warning ("base class `%#T' has a non-virtual destructor", basetype);
1736 /* If the type of basetype is incomplete, then
1737 we already complained about that fact
1738 (and we should have fixed it up as well). */
1739 if (TYPE_SIZE (basetype) == 0)
1742 /* The base type is of incomplete type. It is
1743 probably best to pretend that it does not
1745 if (i == n_baseclasses-1)
1746 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1747 TREE_VEC_LENGTH (binfos) -= 1;
1749 for (j = i; j+1 < n_baseclasses; j++)
1750 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1753 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1754 b->cant_have_const_ctor = 1;
1756 if (TYPE_HAS_CONSTRUCTOR (basetype)
1757 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1759 b->cant_have_default_ctor = 1;
1760 if (! TYPE_HAS_CONSTRUCTOR (t))
1762 cp_pedwarn ("base `%T' with only non-default constructor",
1764 cp_pedwarn ("in class without a constructor");
1768 if (TYPE_HAS_ASSIGN_REF (basetype)
1769 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1770 b->no_const_asn_ref = 1;
1772 b->n_ancestors += CLASSTYPE_N_SUPERCLASSES (basetype);
1773 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1774 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1775 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1776 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1778 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1779 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1780 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1782 if (! TREE_VIA_VIRTUAL (base_binfo))
1783 CLASSTYPE_N_SUPERCLASSES (t) += 1;
1785 if (TYPE_VIRTUAL_P (basetype))
1787 /* Ensure that this is set from at least a virtual base
1789 if (b->rtti == NULL_TREE)
1790 b->rtti = CLASSTYPE_RTTI (basetype);
1792 /* Don't borrow virtuals from virtual baseclasses. */
1793 if (TREE_VIA_VIRTUAL (base_binfo))
1796 if (first_vfn_base_index < 0)
1799 first_vfn_base_index = i;
1801 /* Update these two, now that we know what vtable we are
1802 going to extend. This is so that we can add virtual
1803 functions, and override them properly. */
1804 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1805 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1806 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1807 b->vfield = CLASSTYPE_VFIELD (basetype);
1808 b->vfields = copy_list (CLASSTYPE_VFIELDS (basetype));
1809 vfields = b->vfields;
1812 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1813 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1815 tree value = VF_BASETYPE_VALUE (vfields);
1816 if (DECL_NAME (CLASSTYPE_VFIELD (value))
1817 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1818 VF_NORMAL_VALUE (b->vfields) = basetype;
1820 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1822 vfields = TREE_CHAIN (vfields);
1824 CLASSTYPE_VFIELD (t) = b->vfield;
1828 /* Only add unique vfields, and flatten them out as we go. */
1829 tree vfields = CLASSTYPE_VFIELDS (basetype);
1832 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1833 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1835 tree value = VF_BASETYPE_VALUE (vfields);
1836 b->vfields = tree_cons (base_binfo, value, b->vfields);
1837 if (DECL_NAME (CLASSTYPE_VFIELD (value))
1838 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1839 VF_NORMAL_VALUE (b->vfields) = basetype;
1841 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1843 vfields = TREE_CHAIN (vfields);
1846 if (b->has_virtual == 0)
1848 first_vfn_base_index = i;
1850 /* Update these two, now that we know what vtable we are
1851 going to extend. This is so that we can add virtual
1852 functions, and override them properly. */
1853 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1854 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1855 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1856 b->vfield = CLASSTYPE_VFIELD (basetype);
1857 CLASSTYPE_VFIELD (t) = b->vfield;
1858 /* When we install the first one, set the VF_NORMAL_VALUE
1859 to be the current class, as this it is the most derived
1860 class. Hopefully, this is not set to something else
1862 vfields = b->vfields;
1865 if (DECL_NAME (CLASSTYPE_VFIELD (t))
1866 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1868 VF_NORMAL_VALUE (vfields) = t;
1869 /* There should only be one of them! And it should
1870 always be found, if we get into here. (mrs) */
1873 vfields = TREE_CHAIN (vfields);
1882 /* Find the base class with the largest number of virtual functions. */
1883 for (vfields = b->vfields; vfields; vfields = TREE_CHAIN (vfields))
1885 if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)) > b->max_has_virtual)
1886 b->max_has_virtual = CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields));
1887 if (VF_DERIVED_VALUE (vfields)
1888 && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)) > b->max_has_virtual)
1889 b->max_has_virtual = CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields));
1894 /* If all virtual functions come only from virtual baseclasses. */
1897 /* Update the rtti base if we have a non-virtual base class version
1899 b->rtti = CLASSTYPE_RTTI (BINFO_TYPE (TREE_VEC_ELT (binfos, first_vfn_base_index)));
1901 return first_vfn_base_index;
1904 /* Set memoizing fields and bits of T (and its variants) for later use.
1905 MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables. */
1908 finish_struct_bits (t, max_has_virtual)
1910 int max_has_virtual;
1912 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1914 /* Fix up variants (if any). */
1915 tree variants = TYPE_NEXT_VARIANT (t);
1918 /* These fields are in the _TYPE part of the node, not in
1919 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1920 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1921 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1922 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1923 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
1925 TYPE_USES_COMPLEX_INHERITANCE (variants) = TYPE_USES_COMPLEX_INHERITANCE (t);
1926 TYPE_VIRTUAL_P (variants) = TYPE_VIRTUAL_P (t);
1927 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1928 /* Copy whatever these are holding today. */
1929 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1930 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1931 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1932 TYPE_SIZE (variants) = TYPE_SIZE (t);
1933 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1934 variants = TYPE_NEXT_VARIANT (variants);
1937 if (n_baseclasses && max_has_virtual)
1939 /* Done by `finish_struct' for classes without baseclasses. */
1940 int might_have_abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (t) != 0;
1941 tree binfos = TYPE_BINFO_BASETYPES (t);
1942 for (i = n_baseclasses-1; i >= 0; i--)
1944 might_have_abstract_virtuals
1945 |= (CLASSTYPE_ABSTRACT_VIRTUALS (BINFO_TYPE (TREE_VEC_ELT (binfos, i))) != 0);
1946 if (might_have_abstract_virtuals)
1949 if (might_have_abstract_virtuals)
1951 /* We use error_mark_node from override_one_vtable to signal
1952 an artificial abstract. */
1953 if (CLASSTYPE_ABSTRACT_VIRTUALS (t) == error_mark_node)
1954 CLASSTYPE_ABSTRACT_VIRTUALS (t) = NULL_TREE;
1955 CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
1961 /* Notice whether this class has type conversion functions defined. */
1962 tree binfo = TYPE_BINFO (t);
1963 tree binfos = BINFO_BASETYPES (binfo);
1966 for (i = n_baseclasses-1; i >= 0; i--)
1968 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1970 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1971 if (CLASSTYPE_MAX_DEPTH (basetype) >= CLASSTYPE_MAX_DEPTH (t))
1972 CLASSTYPE_MAX_DEPTH (t) = CLASSTYPE_MAX_DEPTH (basetype) + 1;
1976 /* If this type has a copy constructor, force its mode to be BLKmode, and
1977 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1978 be passed by invisible reference and prevent it from being returned in
1981 Also do this if the class has BLKmode but can still be returned in
1982 registers, since function_cannot_inline_p won't let us inline
1983 functions returning such a type. This affects the HP-PA. */
1984 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1985 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1986 && CLASSTYPE_NON_AGGREGATE (t)))
1989 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1990 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1992 TYPE_MODE (variants) = BLKmode;
1993 TREE_ADDRESSABLE (variants) = 1;
1998 /* Issue warnings about T having private constructors, but no friends,
2001 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2002 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2003 non-private static member functions. */
2006 maybe_warn_about_overly_private_class (t)
2009 int has_member_fn = 0;
2010 int has_nonprivate_method = 0;
2013 if (!warn_ctor_dtor_privacy
2014 /* If the class has friends, those entities might create and
2015 access instances, so we should not warn. */
2016 || (CLASSTYPE_FRIEND_CLASSES (t)
2017 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2018 /* We will have warned when the template was declared; there's
2019 no need to warn on every instantiation. */
2020 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2021 /* There's no reason to even consider warning about this
2025 /* We only issue one warning, if more than one applies, because
2026 otherwise, on code like:
2029 // Oops - forgot `public:'
2035 we warn several times about essentially the same problem. */
2037 /* Check to see if all (non-constructor, non-destructor) member
2038 functions are private. (Since there are no friends or
2039 non-private statics, we can't ever call any of the private member
2041 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
2042 /* We're not interested in compiler-generated methods; they don't
2043 provide any way to call private members. */
2044 if (!DECL_ARTIFICIAL (fn))
2046 if (!TREE_PRIVATE (fn))
2048 if (DECL_STATIC_FUNCTION_P (fn))
2049 /* A non-private static member function is just like a
2050 friend; it can create and invoke private member
2051 functions, and be accessed without a class
2055 has_nonprivate_method = 1;
2058 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2062 if (!has_nonprivate_method && has_member_fn)
2064 /* There are no non-private methods, and there's at least one
2065 private member function that isn't a constructor or
2066 destructor. (If all the private members are
2067 constructors/destructors we want to use the code below that
2068 issues error messages specifically referring to
2069 constructors/destructors.) */
2071 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2072 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
2073 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2074 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2076 has_nonprivate_method = 1;
2079 if (!has_nonprivate_method)
2081 cp_warning ("all member functions in class `%T' are private", t);
2086 /* Even if some of the member functions are non-private, the class
2087 won't be useful for much if all the constructors or destructors
2088 are private: such an object can never be created or destroyed. */
2089 if (TYPE_HAS_DESTRUCTOR (t))
2091 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2093 if (TREE_PRIVATE (dtor))
2095 cp_warning ("`%#T' only defines a private destructor and has no friends",
2101 if (TYPE_HAS_CONSTRUCTOR (t))
2103 int nonprivate_ctor = 0;
2105 /* If a non-template class does not define a copy
2106 constructor, one is defined for it, enabling it to avoid
2107 this warning. For a template class, this does not
2108 happen, and so we would normally get a warning on:
2110 template <class T> class C { private: C(); };
2112 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2113 complete non-template or fully instantiated classes have this
2115 if (!TYPE_HAS_INIT_REF (t))
2116 nonprivate_ctor = 1;
2118 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2122 tree ctor = OVL_CURRENT (fn);
2123 /* Ideally, we wouldn't count copy constructors (or, in
2124 fact, any constructor that takes an argument of the
2125 class type as a parameter) because such things cannot
2126 be used to construct an instance of the class unless
2127 you already have one. But, for now at least, we're
2129 if (! TREE_PRIVATE (ctor))
2131 nonprivate_ctor = 1;
2136 if (nonprivate_ctor == 0)
2138 cp_warning ("`%#T' only defines private constructors and has no friends",
2146 /* Warn about duplicate methods in fn_fields. Also compact method
2147 lists so that lookup can be made faster.
2149 Data Structure: List of method lists. The outer list is a
2150 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2151 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2152 links the entire list of methods for TYPE_METHODS. Friends are
2153 chained in the same way as member functions (? TREE_CHAIN or
2154 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2155 list. That allows them to be quickly deleted, and requires no
2158 If there are any constructors/destructors, they are moved to the
2159 front of the list. This makes pushclass more efficient.
2161 We also link each field which has shares a name with its baseclass
2162 to the head of the list of fields for that base class. This allows
2163 us to reduce search time in places like `build_method_call' to
2164 consider only reasonably likely functions. */
2167 finish_struct_methods (t)
2171 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2172 tree ctor_name = constructor_name (t);
2173 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2175 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2176 and the next few with type conversion operators (if any). */
2177 for (fn_fields = TYPE_METHODS (t); fn_fields;
2178 fn_fields = TREE_CHAIN (fn_fields))
2180 tree fn_name = DECL_NAME (fn_fields);
2182 /* Clear out this flag.
2184 @@ Doug may figure out how to break
2185 @@ this with nested classes and friends. */
2186 DECL_IN_AGGR_P (fn_fields) = 0;
2188 /* Note here that a copy ctor is private, so we don't dare generate
2189 a default copy constructor for a class that has a member
2190 of this type without making sure they have access to it. */
2191 if (fn_name == ctor_name)
2193 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2194 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2196 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2197 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2199 if (TREE_CHAIN (parmtypes) == NULL_TREE
2200 || TREE_CHAIN (parmtypes) == void_list_node
2201 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2203 if (TREE_PROTECTED (fn_fields))
2204 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2205 else if (TREE_PRIVATE (fn_fields))
2206 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2210 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2212 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2214 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2216 if (TREE_PROTECTED (fn_fields))
2217 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2218 else if (TREE_PRIVATE (fn_fields))
2219 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2224 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2225 /* We thought there was a destructor, but there wasn't. Some
2226 parse errors cause this anomalous situation. */
2227 TYPE_HAS_DESTRUCTOR (t) = 0;
2229 /* Issue warnings about private constructors and such. If there are
2230 no methods, then some public defaults are generated. */
2231 maybe_warn_about_overly_private_class (t);
2233 /* Now for each member function (except for constructors and
2234 destructors), compute where member functions of the same
2235 name reside in base classes. */
2236 if (n_baseclasses != 0
2238 && TREE_VEC_LENGTH (method_vec) > 2)
2240 int len = TREE_VEC_LENGTH (method_vec);
2241 tree baselink_vec = make_tree_vec (len);
2243 tree baselink_binfo = build_tree_list (NULL_TREE, TYPE_BINFO (t));
2245 for (i = 2; i < len && TREE_VEC_ELT (method_vec, i); i++)
2247 tree ovl = TREE_VEC_ELT (method_vec, i);
2249 TREE_VEC_ELT (baselink_vec, i)
2250 = get_baselinks (baselink_binfo, t,
2251 DECL_NAME (OVL_CURRENT (ovl)));
2252 if (TREE_VEC_ELT (baselink_vec, i) != 0)
2256 CLASSTYPE_BASELINK_VEC (t) = baselink_vec;
2258 obstack_free (current_obstack, baselink_vec);
2262 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2265 duplicate_tag_error (t)
2268 cp_error ("redefinition of `%#T'", t);
2269 cp_error_at ("previous definition here", t);
2271 /* Pretend we haven't defined this type. */
2273 /* All of the component_decl's were TREE_CHAINed together in the parser.
2274 finish_struct_methods walks these chains and assembles all methods with
2275 the same base name into DECL_CHAINs. Now we don't need the parser chains
2276 anymore, so we unravel them. */
2278 /* This used to be in finish_struct, but it turns out that the
2279 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2281 if (CLASSTYPE_METHOD_VEC (t))
2283 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2284 int i, len = TREE_VEC_LENGTH (method_vec);
2285 for (i = 0; i < len; i++)
2287 tree unchain = TREE_VEC_ELT (method_vec, i);
2288 while (unchain != NULL_TREE)
2290 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2291 unchain = OVL_NEXT (unchain);
2296 if (TYPE_LANG_SPECIFIC (t))
2298 tree as_list = CLASSTYPE_AS_LIST (t);
2299 tree binfo = TYPE_BINFO (t);
2300 tree binfo_as_list = CLASSTYPE_BINFO_AS_LIST (t);
2301 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2302 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2304 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2305 BINFO_BASETYPES(binfo) = NULL_TREE;
2307 CLASSTYPE_AS_LIST (t) = as_list;
2308 TYPE_BINFO (t) = binfo;
2309 CLASSTYPE_BINFO_AS_LIST (t) = binfo_as_list;
2310 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2311 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2312 TYPE_REDEFINED (t) = 1;
2314 TYPE_SIZE (t) = NULL_TREE;
2315 TYPE_MODE (t) = VOIDmode;
2316 TYPE_FIELDS (t) = NULL_TREE;
2317 TYPE_METHODS (t) = NULL_TREE;
2318 TYPE_VFIELD (t) = NULL_TREE;
2319 TYPE_CONTEXT (t) = NULL_TREE;
2322 /* finish up all new vtables. */
2325 finish_vtbls (binfo, do_self, t)
2330 tree binfos = BINFO_BASETYPES (binfo);
2331 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2333 /* Should we use something besides CLASSTYPE_VFIELDS? */
2334 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2336 if (BINFO_NEW_VTABLE_MARKED (binfo))
2340 decl = BINFO_VTABLE (binfo);
2341 context = DECL_CONTEXT (decl);
2342 DECL_CONTEXT (decl) = 0;
2343 if (DECL_INITIAL (decl) != BINFO_VIRTUALS (binfo))
2344 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE,
2345 BINFO_VIRTUALS (binfo));
2346 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0);
2347 DECL_CONTEXT (decl) = context;
2349 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2352 for (i = 0; i < n_baselinks; i++)
2354 tree base_binfo = TREE_VEC_ELT (binfos, i);
2355 int is_not_base_vtable
2356 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2357 if (TREE_VIA_VIRTUAL (base_binfo))
2359 base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2361 finish_vtbls (base_binfo, is_not_base_vtable, t);
2365 /* True if we should override the given BASE_FNDECL with the given
2369 overrides (fndecl, base_fndecl)
2370 tree fndecl, base_fndecl;
2372 /* Destructors have special names. */
2373 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2374 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2376 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2377 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2379 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2381 tree types, base_types;
2383 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2384 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2386 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2387 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2388 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2389 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2390 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2397 get_class_offset_1 (parent, binfo, context, t, fndecl)
2398 tree parent, binfo, context, t, fndecl;
2400 tree binfos = BINFO_BASETYPES (binfo);
2401 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2402 tree rval = NULL_TREE;
2404 if (binfo == parent)
2405 return error_mark_node;
2407 for (i = 0; i < n_baselinks; i++)
2409 tree base_binfo = TREE_VEC_ELT (binfos, i);
2412 if (TREE_VIA_VIRTUAL (base_binfo))
2413 base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2414 CLASSTYPE_VBASECLASSES (t));
2415 nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl);
2416 /* See if we have a new value */
2417 if (nrval && (nrval != error_mark_node || rval==0))
2419 /* Only compare if we have two offsets */
2420 if (rval && rval != error_mark_node
2421 && ! tree_int_cst_equal (nrval, rval))
2423 /* Only give error if the two offsets are different */
2424 error ("every virtual function must have a unique final overrider");
2425 cp_error (" found two (or more) `%T' class subobjects in `%T'", context, t);
2426 cp_error (" with virtual `%D' from virtual base class", fndecl);
2432 if (rval && BINFO_TYPE (binfo) == context)
2434 my_friendly_assert (rval == error_mark_node
2435 || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999);
2436 rval = BINFO_OFFSET (binfo);
2442 /* Get the offset to the CONTEXT subobject that is related to the
2446 get_class_offset (context, t, binfo, fndecl)
2447 tree context, t, binfo, fndecl;
2449 tree first_binfo = binfo;
2454 return integer_zero_node;
2456 if (BINFO_TYPE (binfo) == context)
2457 return BINFO_OFFSET (binfo);
2459 /* Check less derived binfos first. */
2460 while (BINFO_BASETYPES (binfo)
2461 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
2463 tree binfos = BINFO_BASETYPES (binfo);
2464 binfo = TREE_VEC_ELT (binfos, i);
2465 if (BINFO_TYPE (binfo) == context)
2466 return BINFO_OFFSET (binfo);
2469 /* Ok, not found in the less derived binfos, now check the more
2471 offset = get_class_offset_1 (first_binfo, TYPE_BINFO (t), context, t, fndecl);
2472 if (offset==0 || TREE_CODE (offset) != INTEGER_CST)
2473 my_friendly_abort (999); /* we have to find it. */
2477 /* Skip RTTI information at the front of the virtual list. */
2479 unsigned HOST_WIDE_INT
2480 skip_rtti_stuff (virtuals)
2488 /* We always reserve a slot for the offset/tdesc entry. */
2490 *virtuals = TREE_CHAIN (*virtuals);
2492 if (flag_vtable_thunks && *virtuals)
2494 /* The second slot is reserved for the tdesc pointer when thunks
2497 *virtuals = TREE_CHAIN (*virtuals);
2503 modify_one_vtable (binfo, t, fndecl, pfn)
2504 tree binfo, t, fndecl, pfn;
2506 tree virtuals = BINFO_VIRTUALS (binfo);
2507 unsigned HOST_WIDE_INT n;
2509 /* update rtti entry */
2512 if (binfo == TYPE_BINFO (t))
2514 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2515 build_vtable (TYPE_BINFO (DECL_CONTEXT (CLASSTYPE_VFIELD (t))), t);
2519 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2520 prepare_fresh_vtable (binfo, t);
2523 if (fndecl == NULL_TREE)
2526 n = skip_rtti_stuff (&virtuals);
2530 tree current_fndecl = TREE_VALUE (virtuals);
2531 current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl);
2532 current_fndecl = TREE_OPERAND (current_fndecl, 0);
2533 if (current_fndecl && overrides (fndecl, current_fndecl))
2535 tree base_offset, offset;
2536 tree context = DECL_CLASS_CONTEXT (fndecl);
2537 tree vfield = CLASSTYPE_VFIELD (t);
2540 offset = get_class_offset (context, t, binfo, fndecl);
2542 /* Find the right offset for the this pointer based on the
2543 base class we just found. We have to take into
2544 consideration the virtual base class pointers that we
2545 stick in before the virtual function table pointer.
2547 Also, we want just the delta between the most base class
2548 that we derived this vfield from and us. */
2549 base_offset = size_binop (PLUS_EXPR,
2550 get_derived_offset (binfo, DECL_CONTEXT (current_fndecl)),
2551 BINFO_OFFSET (binfo));
2552 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2554 if (binfo == TYPE_BINFO (t))
2556 /* In this case, it is *type*'s vtable we are modifying.
2557 We start with the approximation that it's vtable is that
2558 of the immediate base class. */
2559 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2560 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2564 /* This is our very own copy of `basetype' to play with.
2565 Later, we will fill in all the virtual functions
2566 that override the virtual functions in these base classes
2567 which are not defined by the current type. */
2568 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2569 prepare_fresh_vtable (binfo, t);
2573 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
2575 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2576 build_vtable_entry (this_offset, pfn),
2580 virtuals = TREE_CHAIN (virtuals);
2584 /* These are the ones that are not through virtual base classes. */
2587 modify_all_direct_vtables (binfo, do_self, t, fndecl, pfn)
2590 tree t, fndecl, pfn;
2592 tree binfos = BINFO_BASETYPES (binfo);
2593 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2595 /* Should we use something besides CLASSTYPE_VFIELDS? */
2596 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2598 modify_one_vtable (binfo, t, fndecl, pfn);
2601 for (i = 0; i < n_baselinks; i++)
2603 tree base_binfo = TREE_VEC_ELT (binfos, i);
2604 int is_not_base_vtable
2605 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2606 if (! TREE_VIA_VIRTUAL (base_binfo))
2607 modify_all_direct_vtables (base_binfo, is_not_base_vtable, t, fndecl, pfn);
2611 /* Fixup all the delta entries in this one vtable that need updating. */
2614 fixup_vtable_deltas1 (binfo, t)
2617 tree virtuals = BINFO_VIRTUALS (binfo);
2618 unsigned HOST_WIDE_INT n;
2620 n = skip_rtti_stuff (&virtuals);
2624 tree fndecl = TREE_VALUE (virtuals);
2625 tree pfn = FNADDR_FROM_VTABLE_ENTRY (fndecl);
2626 tree delta = DELTA_FROM_VTABLE_ENTRY (fndecl);
2627 fndecl = TREE_OPERAND (pfn, 0);
2630 tree base_offset, offset;
2631 tree context = DECL_CLASS_CONTEXT (fndecl);
2632 tree vfield = CLASSTYPE_VFIELD (t);
2635 offset = get_class_offset (context, t, binfo, fndecl);
2637 /* Find the right offset for the this pointer based on the
2638 base class we just found. We have to take into
2639 consideration the virtual base class pointers that we
2640 stick in before the virtual function table pointer.
2642 Also, we want just the delta between the most base class
2643 that we derived this vfield from and us. */
2644 base_offset = size_binop (PLUS_EXPR,
2645 get_derived_offset (binfo,
2646 DECL_CONTEXT (fndecl)),
2647 BINFO_OFFSET (binfo));
2648 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2650 if (! tree_int_cst_equal (this_offset, delta))
2652 /* Make sure we can modify the derived association with immunity. */
2653 if (binfo == TYPE_BINFO (t))
2655 /* In this case, it is *type*'s vtable we are modifying.
2656 We start with the approximation that it's vtable is that
2657 of the immediate base class. */
2658 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2659 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2663 /* This is our very own copy of `basetype' to play with.
2664 Later, we will fill in all the virtual functions
2665 that override the virtual functions in these base classes
2666 which are not defined by the current type. */
2667 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2668 prepare_fresh_vtable (binfo, t);
2671 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2672 build_vtable_entry (this_offset, pfn),
2677 virtuals = TREE_CHAIN (virtuals);
2681 /* Fixup all the delta entries in all the direct vtables that need updating.
2682 This happens when we have non-overridden virtual functions from a
2683 virtual base class, that are at a different offset, in the new
2684 hierarchy, because the layout of the virtual bases has changed. */
2687 fixup_vtable_deltas (binfo, init_self, t)
2692 tree binfos = BINFO_BASETYPES (binfo);
2693 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2695 for (i = 0; i < n_baselinks; i++)
2697 tree base_binfo = TREE_VEC_ELT (binfos, i);
2698 int is_not_base_vtable
2699 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2700 if (! TREE_VIA_VIRTUAL (base_binfo))
2701 fixup_vtable_deltas (base_binfo, is_not_base_vtable, t);
2703 /* Should we use something besides CLASSTYPE_VFIELDS? */
2704 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2706 fixup_vtable_deltas1 (binfo, t);
2710 /* These are the ones that are through virtual base classes. */
2713 modify_all_indirect_vtables (binfo, do_self, via_virtual, t, fndecl, pfn)
2715 int do_self, via_virtual;
2716 tree t, fndecl, pfn;
2718 tree binfos = BINFO_BASETYPES (binfo);
2719 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2721 /* Should we use something besides CLASSTYPE_VFIELDS? */
2722 if (do_self && via_virtual && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2724 modify_one_vtable (binfo, t, fndecl, pfn);
2727 for (i = 0; i < n_baselinks; i++)
2729 tree base_binfo = TREE_VEC_ELT (binfos, i);
2730 int is_not_base_vtable
2731 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2732 if (TREE_VIA_VIRTUAL (base_binfo))
2735 base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2737 modify_all_indirect_vtables (base_binfo, is_not_base_vtable, via_virtual, t, fndecl, pfn);
2742 modify_all_vtables (t, fndecl, vfn)
2743 tree t, fndecl, vfn;
2745 /* Do these first, so that we will make use of any non-virtual class's
2746 vtable, over a virtual classes vtable. */
2747 modify_all_direct_vtables (TYPE_BINFO (t), 1, t, fndecl, vfn);
2748 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
2749 modify_all_indirect_vtables (TYPE_BINFO (t), 1, 0, t, fndecl, vfn);
2752 /* Here, we already know that they match in every respect.
2753 All we have to check is where they had their declarations. */
2756 strictly_overrides (fndecl1, fndecl2)
2757 tree fndecl1, fndecl2;
2759 int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
2760 DECL_CLASS_CONTEXT (fndecl1),
2762 if (distance == -2 || distance > 0)
2767 /* Merge overrides for one vtable.
2768 If we want to merge in same function, we are fine.
2770 if one has a DECL_CLASS_CONTEXT that is a parent of the
2771 other, than choose the more derived one
2773 potentially ill-formed (see 10.3 [class.virtual])
2774 we have to check later to see if there was an
2775 override in this class. If there was ok, if not
2776 then it is ill-formed. (mrs)
2778 We take special care to reuse a vtable, if we can. */
2781 override_one_vtable (binfo, old, t)
2784 tree virtuals = BINFO_VIRTUALS (binfo);
2785 tree old_virtuals = BINFO_VIRTUALS (old);
2786 enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
2788 /* If we have already committed to modifying it, then don't try and
2789 reuse another vtable. */
2790 if (BINFO_NEW_VTABLE_MARKED (binfo))
2793 skip_rtti_stuff (&virtuals);
2794 skip_rtti_stuff (&old_virtuals);
2798 tree fndecl = TREE_VALUE (virtuals);
2799 tree old_fndecl = TREE_VALUE (old_virtuals);
2800 fndecl = FNADDR_FROM_VTABLE_ENTRY (fndecl);
2801 old_fndecl = FNADDR_FROM_VTABLE_ENTRY (old_fndecl);
2802 fndecl = TREE_OPERAND (fndecl, 0);
2803 old_fndecl = TREE_OPERAND (old_fndecl, 0);
2804 /* First check to see if they are the same. */
2805 if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
2807 /* No need to do anything. */
2809 else if (strictly_overrides (fndecl, old_fndecl))
2811 if (choose == UNDECIDED)
2813 else if (choose == REUSE_OLD)
2816 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2818 prepare_fresh_vtable (binfo, t);
2819 override_one_vtable (binfo, old, t);
2824 else if (strictly_overrides (old_fndecl, fndecl))
2826 if (choose == UNDECIDED)
2828 else if (choose == REUSE_NEW)
2831 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2833 prepare_fresh_vtable (binfo, t);
2834 override_one_vtable (binfo, old, t);
2837 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2839 else if (choose == NEITHER)
2841 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2847 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2849 prepare_fresh_vtable (binfo, t);
2850 override_one_vtable (binfo, old, t);
2854 /* This MUST be overridden, or the class is ill-formed. */
2855 /* For now, we just make it abstract. */
2856 tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
2859 fndecl = copy_node (fndecl);
2860 copy_lang_decl (fndecl);
2861 DECL_ABSTRACT_VIRTUAL_P (fndecl) = 1;
2862 DECL_NEEDS_FINAL_OVERRIDER_P (fndecl) = 1;
2863 /* Make sure we search for it later. */
2864 if (! CLASSTYPE_ABSTRACT_VIRTUALS (t))
2865 CLASSTYPE_ABSTRACT_VIRTUALS (t) = error_mark_node;
2867 vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
2868 TREE_CONSTANT (vfn) = 1;
2870 /* We can use integer_zero_node, as we will core dump
2871 if this is used anyway. */
2872 TREE_VALUE (virtuals) = build_vtable_entry (integer_zero_node, vfn);
2875 virtuals = TREE_CHAIN (virtuals);
2876 old_virtuals = TREE_CHAIN (old_virtuals);
2879 /* Let's reuse the old vtable. */
2880 if (choose == REUSE_OLD)
2882 BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
2883 BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
2887 /* Merge in overrides for virtual bases.
2888 BINFO is the hierarchy we want to modify, and OLD has the potential
2892 merge_overrides (binfo, old, do_self, t)
2897 tree binfos = BINFO_BASETYPES (binfo);
2898 tree old_binfos = BINFO_BASETYPES (old);
2899 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2901 /* Should we use something besides CLASSTYPE_VFIELDS? */
2902 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2904 override_one_vtable (binfo, old, t);
2907 for (i = 0; i < n_baselinks; i++)
2909 tree base_binfo = TREE_VEC_ELT (binfos, i);
2910 tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
2911 int is_not_base_vtable
2912 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2913 if (! TREE_VIA_VIRTUAL (base_binfo))
2914 merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
2918 /* Get the base virtual function declarations in T that are either
2919 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2920 the overrider/hider. */
2923 get_basefndecls (fndecl, t)
2926 tree methods = TYPE_METHODS (t);
2927 tree base_fndecls = NULL_TREE;
2928 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2929 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2933 if (TREE_CODE (methods) == FUNCTION_DECL
2934 && DECL_VINDEX (methods) != NULL_TREE
2935 && DECL_NAME (fndecl) == DECL_NAME (methods))
2936 base_fndecls = temp_tree_cons (fndecl, methods, base_fndecls);
2938 methods = TREE_CHAIN (methods);
2942 return base_fndecls;
2944 for (i = 0; i < n_baseclasses; i++)
2946 tree base_binfo = TREE_VEC_ELT (binfos, i);
2947 tree basetype = BINFO_TYPE (base_binfo);
2949 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2953 return base_fndecls;
2956 /* Mark the functions that have been hidden with their overriders.
2957 Since we start out with all functions already marked with a hider,
2958 no need to mark functions that are just hidden. */
2961 mark_overriders (fndecl, base_fndecls)
2962 tree fndecl, base_fndecls;
2964 while (base_fndecls)
2966 if (overrides (TREE_VALUE (base_fndecls), fndecl))
2967 TREE_PURPOSE (base_fndecls) = fndecl;
2969 base_fndecls = TREE_CHAIN (base_fndecls);
2973 /* If this declaration supersedes the declaration of
2974 a method declared virtual in the base class, then
2975 mark this field as being virtual as well. */
2978 check_for_override (decl, ctype)
2981 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
2982 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2983 int virtualp = DECL_VIRTUAL_P (decl);
2985 for (i = 0; i < n_baselinks; i++)
2987 tree base_binfo = TREE_VEC_ELT (binfos, i);
2988 if (TYPE_VIRTUAL_P (BINFO_TYPE (base_binfo)))
2990 tree tmp = get_matching_virtual
2992 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
2995 /* If this function overrides some virtual in some base
2996 class, then the function itself is also necessarily
2997 virtual, even if the user didn't explicitly say so. */
2998 DECL_VIRTUAL_P (decl) = 1;
3000 /* The TMP we really want is the one from the deepest
3001 baseclass on this path, taking care not to
3002 duplicate if we have already found it (via another
3003 path to its virtual baseclass. */
3004 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
3006 cp_error_at ("method `%D' may not be declared static",
3008 cp_error_at ("(since `%D' declared virtual in base class.)",
3014 #if 0 /* The signature of an overriding function is not changed. */
3016 /* The argument types may have changed... */
3017 tree type = TREE_TYPE (decl);
3018 tree argtypes = TYPE_ARG_TYPES (type);
3019 tree base_variant = TREE_TYPE (TREE_VALUE (argtypes));
3020 tree raises = TYPE_RAISES_EXCEPTIONS (type);
3022 argtypes = commonparms (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (tmp))),
3023 TREE_CHAIN (argtypes));
3024 /* But the return type has not. */
3025 type = build_cplus_method_type (base_variant, TREE_TYPE (type), argtypes);
3027 type = build_exception_variant (type, raises);
3028 TREE_TYPE (decl) = type;
3032 = tree_cons (NULL_TREE, tmp, DECL_VINDEX (decl));
3039 if (DECL_VINDEX (decl) == NULL_TREE)
3040 DECL_VINDEX (decl) = error_mark_node;
3041 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3045 /* Warn about hidden virtual functions that are not overridden in t.
3046 We know that constructors and destructors don't apply. */
3052 tree method_vec = CLASSTYPE_METHOD_VEC (t);
3053 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3056 /* We go through each separately named virtual function. */
3057 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
3059 tree fns = TREE_VEC_ELT (method_vec, i);
3062 tree base_fndecls = NULL_TREE;
3063 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3064 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3066 fndecl = OVL_CURRENT (fns);
3067 if (DECL_VINDEX (fndecl) == NULL_TREE)
3070 /* First we get a list of all possible functions that might be
3071 hidden from each base class. */
3072 for (i = 0; i < n_baseclasses; i++)
3074 tree base_binfo = TREE_VEC_ELT (binfos, i);
3075 tree basetype = BINFO_TYPE (base_binfo);
3077 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3081 fns = OVL_NEXT (fns);
3083 fndecl = OVL_CURRENT (fns);
3087 /* ...then mark up all the base functions with overriders, preferring
3088 overriders to hiders. */
3092 mark_overriders (fndecl, base_fndecls);
3094 fns = OVL_NEXT (fns);
3096 fndecl = OVL_CURRENT (fns);
3101 /* Now give a warning for all base functions without overriders,
3102 as they are hidden. */
3103 while (base_fndecls)
3105 if (! overrides (TREE_VALUE (base_fndecls),
3106 TREE_PURPOSE (base_fndecls)))
3108 /* Here we know it is a hider, and no overrider exists. */
3109 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
3110 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
3113 base_fndecls = TREE_CHAIN (base_fndecls);
3118 /* Check for things that are invalid. There are probably plenty of other
3119 things we should check for also. */
3122 finish_struct_anon (t)
3126 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3128 if (TREE_STATIC (field))
3130 if (TREE_CODE (field) != FIELD_DECL)
3133 if (DECL_NAME (field) == NULL_TREE
3134 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
3136 tree* uelt = &TYPE_FIELDS (TREE_TYPE (field));
3137 for (; *uelt; uelt = &TREE_CHAIN (*uelt))
3139 if (TREE_CODE (*uelt) != FIELD_DECL)
3142 if (TREE_PRIVATE (*uelt))
3143 cp_pedwarn_at ("private member `%#D' in anonymous union",
3145 else if (TREE_PROTECTED (*uelt))
3146 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3149 TREE_PRIVATE (*uelt) = TREE_PRIVATE (field);
3150 TREE_PROTECTED (*uelt) = TREE_PROTECTED (field);
3156 extern int interface_only, interface_unknown;
3158 /* Create default constructors, assignment operators, and so forth for
3159 the type indicated by T, if they are needed.
3160 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3161 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3162 cannot have a default constructor, copy constructor taking a const
3163 reference argument, or an assignment operator, respectively. If a
3164 virtual destructor is created, its DECL is returned; otherwise the
3165 return value is NULL_TREE. */
3168 add_implicitly_declared_members (t, cant_have_default_ctor,
3169 cant_have_const_cctor,
3170 cant_have_assignment)
3172 int cant_have_default_ctor;
3173 int cant_have_const_cctor;
3174 int cant_have_assignment;
3177 tree implicit_fns = NULL_TREE;
3178 tree name = TYPE_IDENTIFIER (t);
3179 tree virtual_dtor = NULL_TREE;
3183 if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t)
3184 && !IS_SIGNATURE (t))
3186 default_fn = cons_up_default_function (t, name, 0);
3187 check_for_override (default_fn, t);
3189 /* If we couldn't make it work, then pretend we didn't need it. */
3190 if (default_fn == void_type_node)
3191 TYPE_NEEDS_DESTRUCTOR (t) = 0;
3194 TREE_CHAIN (default_fn) = implicit_fns;
3195 implicit_fns = default_fn;
3197 if (DECL_VINDEX (default_fn))
3198 virtual_dtor = default_fn;
3201 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3203 /* Default constructor. */
3204 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor
3205 && ! IS_SIGNATURE (t))
3207 default_fn = cons_up_default_function (t, name, 2);
3208 TREE_CHAIN (default_fn) = implicit_fns;
3209 implicit_fns = default_fn;
3212 /* Copy constructor. */
3213 if (! TYPE_HAS_INIT_REF (t) && ! IS_SIGNATURE (t) && ! TYPE_FOR_JAVA (t))
3215 /* ARM 12.18: You get either X(X&) or X(const X&), but
3217 default_fn = cons_up_default_function (t, name,
3218 3 + cant_have_const_cctor);
3219 TREE_CHAIN (default_fn) = implicit_fns;
3220 implicit_fns = default_fn;
3223 /* Assignment operator. */
3224 if (! TYPE_HAS_ASSIGN_REF (t) && ! IS_SIGNATURE (t) && ! TYPE_FOR_JAVA (t))
3226 default_fn = cons_up_default_function (t, name,
3227 5 + cant_have_assignment);
3228 TREE_CHAIN (default_fn) = implicit_fns;
3229 implicit_fns = default_fn;
3232 /* Now, hook all of the new functions on to TYPE_METHODS,
3233 and add them to the CLASSTYPE_METHOD_VEC. */
3234 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3235 add_method (t, 0, *f);
3236 *f = TYPE_METHODS (t);
3237 TYPE_METHODS (t) = implicit_fns;
3239 return virtual_dtor;
3242 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
3243 (or C++ class declaration).
3245 For C++, we must handle the building of derived classes.
3246 Also, C++ allows static class members. The way that this is
3247 handled is to keep the field name where it is (as the DECL_NAME
3248 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
3249 of the field. layout_record and layout_union will know about this.
3251 More C++ hair: inline functions have text in their
3252 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
3253 meaningful tree structure. After the struct has been laid out, set
3254 things up so that this can happen.
3256 And still more: virtual functions. In the case of single inheritance,
3257 when a new virtual function is seen which redefines a virtual function
3258 from the base class, the new virtual function is placed into
3259 the virtual function table at exactly the same address that
3260 it had in the base class. When this is extended to multiple
3261 inheritance, the same thing happens, except that multiple virtual
3262 function tables must be maintained. The first virtual function
3263 table is treated in exactly the same way as in the case of single
3264 inheritance. Additional virtual function tables have different
3265 DELTAs, which tell how to adjust `this' to point to the right thing.
3267 ATTRIBUTES is the set of decl attributes to be applied, if any. */
3270 finish_struct_1 (t, warn_anon)
3275 enum tree_code code = TREE_CODE (t);
3276 tree fields = TYPE_FIELDS (t);
3277 tree x, last_x, method_vec;
3279 int max_has_virtual;
3280 tree pending_virtuals = NULL_TREE;
3281 tree pending_hard_virtuals = NULL_TREE;
3282 tree abstract_virtuals = NULL_TREE;
3286 int cant_have_default_ctor;
3287 int cant_have_const_ctor;
3288 int no_const_asn_ref;
3290 /* The index of the first base class which has virtual
3291 functions. Only applied to non-virtual baseclasses. */
3292 int first_vfn_base_index;
3295 int any_default_members = 0;
3296 int const_sans_init = 0;
3297 int ref_sans_init = 0;
3298 tree access_decls = NULL_TREE;
3301 int has_pointers = 0;
3303 if (warn_anon && code != UNION_TYPE && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
3304 pedwarn ("anonymous class type not used to declare any objects");
3308 if (IS_AGGR_TYPE (t))
3309 cp_error ("redefinition of `%#T'", t);
3311 my_friendly_abort (172);
3316 GNU_xref_decl (current_function_decl, t);
3318 /* If this type was previously laid out as a forward reference,
3319 make sure we lay it out again. */
3321 TYPE_SIZE (t) = NULL_TREE;
3322 CLASSTYPE_GOT_SEMICOLON (t) = 0;
3325 /* This is in general too late to do this. I moved the main case up to
3326 left_curly, what else needs to move? */
3327 if (! IS_SIGNATURE (t))
3329 my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t) == interface_only, 999);
3330 my_friendly_assert (CLASSTYPE_INTERFACE_KNOWN (t) == ! interface_unknown, 999);
3334 old = suspend_momentary ();
3336 /* Install struct as DECL_FIELD_CONTEXT of each field decl.
3337 Also process specified field sizes.
3338 Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
3339 The specified size is found in the DECL_INITIAL.
3340 Store 0 there, except for ": 0" fields (so we can find them
3341 and delete them, below). */
3343 if (TYPE_BINFO_BASETYPES (t))
3344 n_baseclasses = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t));
3348 if (n_baseclasses > 0)
3350 struct base_info base_info;
3352 first_vfn_base_index = finish_base_struct (t, &base_info);
3353 /* Remember where we got our vfield from. */
3354 CLASSTYPE_VFIELD_PARENT (t) = first_vfn_base_index;
3355 has_virtual = base_info.has_virtual;
3356 max_has_virtual = base_info.max_has_virtual;
3357 CLASSTYPE_N_SUPERCLASSES (t) += base_info.n_ancestors;
3358 vfield = base_info.vfield;
3359 vfields = base_info.vfields;
3360 CLASSTYPE_RTTI (t) = base_info.rtti;
3361 cant_have_default_ctor = base_info.cant_have_default_ctor;
3362 cant_have_const_ctor = base_info.cant_have_const_ctor;
3363 no_const_asn_ref = base_info.no_const_asn_ref;
3368 first_vfn_base_index = -1;
3370 max_has_virtual = has_virtual;
3372 vfields = NULL_TREE;
3373 CLASSTYPE_RTTI (t) = NULL_TREE;
3374 cant_have_default_ctor = 0;
3375 cant_have_const_ctor = 0;
3376 no_const_asn_ref = 0;
3380 /* Both of these should be done before now. */
3381 if (write_virtuals == 3 && CLASSTYPE_INTERFACE_KNOWN (t)
3382 && ! IS_SIGNATURE (t))
3384 my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t) == interface_only, 999);
3385 my_friendly_assert (CLASSTYPE_VTABLE_NEEDS_WRITING (t) == ! interface_only, 999);
3389 /* The three of these are approximations which may later be
3390 modified. Needed at this point to make add_virtual_function
3391 and modify_vtable_entries work. */
3392 CLASSTYPE_VFIELDS (t) = vfields;
3393 CLASSTYPE_VFIELD (t) = vfield;
3395 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3397 GNU_xref_member (current_class_name, x);
3399 /* If this was an evil function, don't keep it in class. */
3400 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3403 /* Do both of these, even though they're in the same union;
3404 if the insn `r' member and the size `i' member are
3405 different sizes, as on the alpha, the larger of the two
3406 will end up with garbage in it. */
3407 DECL_SAVED_INSNS (x) = NULL_RTX;
3408 DECL_FIELD_SIZE (x) = 0;
3410 check_for_override (x, t);
3411 if (DECL_ABSTRACT_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3412 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3414 /* The name of the field is the original field name
3415 Save this in auxiliary field for later overloading. */
3416 if (DECL_VINDEX (x))
3418 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3419 &has_virtual, x, t);
3420 if (DECL_ABSTRACT_VIRTUAL_P (x))
3421 abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
3423 /* XXX Why did I comment this out? (jason) */
3431 fields = chainon (build_vbase_pointer_fields (t), fields);
3434 for (x = fields; x; x = TREE_CHAIN (x))
3436 GNU_xref_member (current_class_name, x);
3438 if (TREE_CODE (x) == FIELD_DECL)
3440 DECL_PACKED (x) |= TYPE_PACKED (t);
3444 if (TREE_CODE (x) == USING_DECL)
3446 /* Save access declarations for later. */
3448 TREE_CHAIN (last_x) = TREE_CHAIN (x);
3450 fields = TREE_CHAIN (x);
3452 access_decls = scratch_tree_cons (NULL_TREE, x, access_decls);
3458 if (TREE_CODE (x) == TYPE_DECL
3459 || TREE_CODE (x) == TEMPLATE_DECL)
3462 /* If we've gotten this far, it's a data member, possibly static,
3463 or an enumerator. */
3465 DECL_FIELD_CONTEXT (x) = t;
3467 /* ``A local class cannot have static data members.'' ARM 9.4 */
3468 if (current_function_decl && TREE_STATIC (x))
3469 cp_error_at ("field `%D' in local class cannot be static", x);
3471 /* Perform error checking that did not get done in
3473 if (TREE_CODE (TREE_TYPE (x)) == FUNCTION_TYPE)
3475 cp_error_at ("field `%D' invalidly declared function type",
3477 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3479 else if (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE)
3481 cp_error_at ("field `%D' invalidly declared method type", x);
3482 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3484 else if (TREE_CODE (TREE_TYPE (x)) == OFFSET_TYPE)
3486 cp_error_at ("field `%D' invalidly declared offset type", x);
3487 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3491 if (DECL_NAME (x) == constructor_name (t))
3492 cant_have_default_ctor = 1;
3495 if (TREE_TYPE (x) == error_mark_node)
3498 DECL_SAVED_INSNS (x) = NULL_RTX;
3499 DECL_FIELD_SIZE (x) = 0;
3501 /* When this goes into scope, it will be a non-local reference. */
3502 DECL_NONLOCAL (x) = 1;
3504 if (TREE_CODE (x) == CONST_DECL)
3507 if (TREE_CODE (x) == VAR_DECL)
3509 if (TREE_CODE (t) == UNION_TYPE)
3510 /* Unions cannot have static members. */
3511 cp_error_at ("field `%D' declared static in union", x);
3516 /* Now it can only be a FIELD_DECL. */
3518 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3521 /* If this is of reference type, check if it needs an init.
3522 Also do a little ANSI jig if necessary. */
3523 if (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE)
3525 if (DECL_INITIAL (x) == NULL_TREE)
3528 /* ARM $12.6.2: [A member initializer list] (or, for an
3529 aggregate, initialization by a brace-enclosed list) is the
3530 only way to initialize nonstatic const and reference
3532 cant_have_default_ctor = 1;
3533 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3535 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3538 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3540 cp_warning_at ("non-static reference in class without a constructor", x);
3544 if (TREE_CODE (TREE_TYPE (x)) == POINTER_TYPE)
3547 /* If any field is const, the structure type is pseudo-const. */
3548 if (TREE_READONLY (x))
3550 C_TYPE_FIELDS_READONLY (t) = 1;
3551 if (DECL_INITIAL (x) == NULL_TREE)
3552 const_sans_init = 1;
3554 /* ARM $12.6.2: [A member initializer list] (or, for an
3555 aggregate, initialization by a brace-enclosed list) is the
3556 only way to initialize nonstatic const and reference
3558 cant_have_default_ctor = 1;
3559 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3561 if (! TYPE_HAS_CONSTRUCTOR (t) && !IS_SIGNATURE (t)
3565 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3567 cp_warning_at ("non-static const member in class without a constructor", x);
3572 /* A field that is pseudo-const makes the structure
3574 tree t1 = TREE_TYPE (x);
3575 while (TREE_CODE (t1) == ARRAY_TYPE)
3576 t1 = TREE_TYPE (t1);
3577 if (IS_AGGR_TYPE (t1))
3579 if (C_TYPE_FIELDS_READONLY (t1))
3580 C_TYPE_FIELDS_READONLY (t) = 1;
3581 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1))
3582 const_sans_init = 1;
3586 /* We set DECL_C_BIT_FIELD in grokbitfield.
3587 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3588 if (DECL_C_BIT_FIELD (x))
3590 /* Invalid bit-field size done by grokfield. */
3591 /* Detect invalid bit-field type. */
3592 if (DECL_INITIAL (x)
3593 && ! INTEGRAL_TYPE_P (TREE_TYPE (x)))
3595 cp_error_at ("bit-field `%#D' with non-integral type", x);
3596 DECL_INITIAL (x) = NULL;
3599 /* Detect and ignore out of range field width. */
3600 if (DECL_INITIAL (x))
3602 tree w = DECL_INITIAL (x);
3603 register int width = 0;
3605 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3608 /* detect invalid field size. */
3609 if (TREE_CODE (w) == CONST_DECL)
3610 w = DECL_INITIAL (w);
3611 else if (TREE_READONLY_DECL_P (w))
3612 w = decl_constant_value (w);
3614 if (TREE_CODE (w) != INTEGER_CST)
3616 cp_error_at ("bit-field `%D' width not an integer constant",
3618 DECL_INITIAL (x) = NULL_TREE;
3620 else if (width = TREE_INT_CST_LOW (w),
3623 DECL_INITIAL (x) = NULL;
3624 cp_error_at ("negative width in bit-field `%D'", x);
3626 else if (width == 0 && DECL_NAME (x) != 0)
3628 DECL_INITIAL (x) = NULL;
3629 cp_error_at ("zero width for bit-field `%D'", x);
3632 > TYPE_PRECISION (long_long_unsigned_type_node))
3634 /* The backend will dump if you try to use something
3635 too big; avoid that. */
3636 DECL_INITIAL (x) = NULL;
3637 sorry ("bit-fields larger than %d bits",
3638 TYPE_PRECISION (long_long_unsigned_type_node));
3639 cp_error_at (" in declaration of `%D'", x);
3641 else if (width > TYPE_PRECISION (TREE_TYPE (x))
3642 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE
3643 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE)
3645 cp_warning_at ("width of `%D' exceeds its type", x);
3647 else if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
3648 && ((min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
3649 TREE_UNSIGNED (TREE_TYPE (x))) > width)
3650 || (min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
3651 TREE_UNSIGNED (TREE_TYPE (x))) > width)))
3653 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3657 if (DECL_INITIAL (x) == NULL_TREE)
3659 else if (width == 0)
3661 #ifdef EMPTY_FIELD_BOUNDARY
3662 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), EMPTY_FIELD_BOUNDARY);
3664 #ifdef PCC_BITFIELD_TYPE_MATTERS
3665 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3666 TYPE_ALIGN (TREE_TYPE (x)));
3671 DECL_INITIAL (x) = NULL_TREE;
3672 DECL_FIELD_SIZE (x) = width;
3673 DECL_BIT_FIELD (x) = 1;
3677 /* Non-bit-fields are aligned for their type. */
3678 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x)));
3682 tree type = TREE_TYPE (x);
3684 while (TREE_CODE (type) == ARRAY_TYPE)
3685 type = TREE_TYPE (type);
3687 if (TYPE_LANG_SPECIFIC (type) && ! ANON_UNION_P (x)
3688 && ! TYPE_PTRMEMFUNC_P (type))
3690 /* Never let anything with uninheritable virtuals
3691 make it through without complaint. */
3692 if (CLASSTYPE_ABSTRACT_VIRTUALS (type))
3693 abstract_virtuals_error (x, type);
3695 /* Don't let signatures make it through either. */
3696 if (IS_SIGNATURE (type))
3697 signature_error (x, type);
3699 if (code == UNION_TYPE)
3702 if (TYPE_NEEDS_CONSTRUCTING (type))
3703 fie = "constructor";
3704 else if (TYPE_NEEDS_DESTRUCTOR (type))
3706 else if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3707 fie = "copy assignment operator";
3709 cp_error_at ("member `%#D' with %s not allowed in union", x,
3714 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3715 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3716 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3717 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3720 if (!TYPE_HAS_CONST_INIT_REF (type))
3721 cant_have_const_ctor = 1;
3723 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3724 no_const_asn_ref = 1;
3726 if (TYPE_HAS_CONSTRUCTOR (type)
3727 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3729 cant_have_default_ctor = 1;
3731 /* This is wrong for aggregates. */
3732 if (! TYPE_HAS_CONSTRUCTOR (t))
3735 cp_pedwarn_at ("member `%#D' with only non-default constructor", x);
3737 cp_pedwarn_at ("member with only non-default constructor", x);
3738 cp_pedwarn_at ("in class without a constructor",
3744 if (DECL_INITIAL (x) != NULL_TREE)
3746 /* `build_class_init_list' does not recognize
3748 if (code == UNION_TYPE && any_default_members != 0)
3749 cp_error_at ("multiple fields in union `%T' initialized");
3750 any_default_members = 1;
3755 /* If this type has any constant members which did not come
3756 with their own initialization, mark that fact here. It is
3757 not an error here, since such types can be saved either by their
3758 constructors, or by fortuitous initialization. */
3759 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
3760 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
3761 CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
3763 /* Effective C++ rule 11. */
3764 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3765 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3767 cp_warning ("`%#T' has pointer data members", t);
3769 if (! TYPE_HAS_INIT_REF (t))
3771 cp_warning (" but does not override `%T(const %T&)'", t, t);
3772 if (! TYPE_HAS_ASSIGN_REF (t))
3773 cp_warning (" or `operator=(const %T&)'", t);
3775 else if (! TYPE_HAS_ASSIGN_REF (t))
3776 cp_warning (" but does not override `operator=(const %T&)'", t);
3779 /* Do some bookkeeping that will guide the generation of implicitly
3780 declared member functions. */
3781 TYPE_HAS_COMPLEX_INIT_REF (t)
3782 |= (TYPE_HAS_INIT_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3783 || has_virtual || any_default_members);
3784 TYPE_NEEDS_CONSTRUCTING (t)
3785 |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3786 || has_virtual || any_default_members);
3787 if (! IS_SIGNATURE (t))
3788 CLASSTYPE_NON_AGGREGATE (t)
3789 = ! aggregate || has_virtual || TYPE_HAS_CONSTRUCTOR (t);
3790 TYPE_HAS_REAL_ASSIGNMENT (t) |= TYPE_HAS_ASSIGNMENT (t);
3791 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
3792 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
3793 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
3795 /* Synthesize any needed methods. Note that methods will be synthesized
3796 for anonymous unions; grok_x_components undoes that. */
3798 = add_implicitly_declared_members (t, cant_have_default_ctor,
3799 cant_have_const_ctor,
3802 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3803 &has_virtual, virtual_dtor, t);
3805 if (TYPE_METHODS (t))
3807 finish_struct_methods (t);
3808 method_vec = CLASSTYPE_METHOD_VEC (t);
3814 /* Just in case these got accidentally
3815 filled in by syntax errors. */
3816 TYPE_HAS_CONSTRUCTOR (t) = 0;
3817 TYPE_HAS_DESTRUCTOR (t) = 0;
3820 for (access_decls = nreverse (access_decls); access_decls;
3821 access_decls = TREE_CHAIN (access_decls))
3822 handle_using_decl (TREE_VALUE (access_decls), t, method_vec, fields);
3824 if (vfield == NULL_TREE && has_virtual)
3826 /* We build this decl with vtbl_ptr_type_node, which is a
3827 `vtable_entry_type*'. It might seem more precise to use
3828 `vtable_entry_type (*)[N]' where N is the number of firtual
3829 functions. However, that would require the vtable pointer in
3830 base classes to have a different type than the vtable pointer
3831 in derived classes. We could make that happen, but that
3832 still wouldn't solve all the problems. In particular, the
3833 type-based alias analysis code would decide that assignments
3834 to the base class vtable pointer can't alias assignments to
3835 the derived class vtable pointer, since they have different
3836 types. Thus, in an derived class destructor, where the base
3837 class constructor was inlined, we could generate bad code for
3838 setting up the vtable pointer.
3840 Therefore, we use one type for all vtable pointers. We still
3841 use a type-correct type; it's just doesn't indicate the array
3842 bounds. That's better than using `void*' or some such; it's
3843 cleaner, and it let's the alias analysis code know that these
3844 stores cannot alias stores to void*! */
3845 vfield = build_lang_field_decl (FIELD_DECL, get_vfield_name (t),
3846 vtbl_ptr_type_node);
3847 /* If you change any of the below, take a look at all the
3848 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
3850 DECL_ASSEMBLER_NAME (vfield) = get_identifier (VFIELD_BASE);
3851 CLASSTYPE_VFIELD (t) = vfield;
3852 DECL_VIRTUAL_P (vfield) = 1;
3853 DECL_ARTIFICIAL (vfield) = 1;
3854 DECL_FIELD_CONTEXT (vfield) = t;
3855 DECL_CLASS_CONTEXT (vfield) = t;
3856 DECL_FCONTEXT (vfield) = t;
3857 DECL_SAVED_INSNS (vfield) = NULL_RTX;
3858 DECL_FIELD_SIZE (vfield) = 0;
3859 DECL_ALIGN (vfield) = TYPE_ALIGN (ptr_type_node);
3861 /* This is more efficient, but breaks binary compatibility, turn
3862 it on sometime when we don't care. If we turn it on, we also
3863 have to enable the code in dfs_init_vbase_pointers. */
3864 /* vfield is always first entry in structure. */
3865 TREE_CHAIN (vfield) = fields;
3870 my_friendly_assert (TREE_CHAIN (last_x) == NULL_TREE, 175);
3871 TREE_CHAIN (last_x) = vfield;
3878 vfields = chainon (vfields, CLASSTYPE_AS_LIST (t));
3881 /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
3882 And they have already done their work.
3884 C++: maybe we will support default field initialization some day... */
3886 /* Delete all zero-width bit-fields from the front of the fieldlist */
3887 while (fields && DECL_C_BIT_FIELD (fields)
3888 && DECL_INITIAL (fields))
3889 fields = TREE_CHAIN (fields);
3890 /* Delete all such fields from the rest of the fields. */
3891 for (x = fields; x;)
3893 if (TREE_CHAIN (x) && DECL_C_BIT_FIELD (TREE_CHAIN (x))
3894 && DECL_INITIAL (TREE_CHAIN (x)))
3895 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
3899 /* Delete all duplicate fields from the fields */
3900 delete_duplicate_fields (fields);
3902 /* Catch function/field name conflict. We don't need to do this for a
3903 signature, since it can only contain the fields constructed in
3904 append_signature_fields. */
3905 if (! IS_SIGNATURE (t))
3907 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3908 for (x = fields; x; x = TREE_CHAIN (x))
3910 tree name = DECL_NAME (x);
3913 if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))
3916 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
3917 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)))
3920 cp_error_at ("data member `%#D' conflicts with", x);
3921 cp_error_at ("function member `%#D'",
3922 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
3928 /* Now we have the final fieldlist for the data fields. Record it,
3929 then lay out the structure or union (including the fields). */
3931 TYPE_FIELDS (t) = fields;
3935 last_x = build_base_fields (t);
3937 /* If all our bases are empty, we can be empty too. */
3938 for (x = last_x; empty && x; x = TREE_CHAIN (x))
3939 if (DECL_SIZE (x) != integer_zero_node)
3944 /* C++: do not let empty structures exist. */
3945 tree decl = build_lang_field_decl
3946 (FIELD_DECL, NULL_TREE, char_type_node);
3947 TREE_CHAIN (decl) = fields;
3948 TYPE_FIELDS (t) = decl;
3951 TYPE_FIELDS (t) = chainon (last_x, TYPE_FIELDS (t));
3955 /* Remember the size and alignment of the class before adding
3956 the virtual bases. */
3957 if (empty && flag_new_abi)
3958 CLASSTYPE_SIZE (t) = integer_zero_node;
3959 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
3960 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
3961 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
3963 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
3964 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
3966 finish_struct_anon (t);
3968 /* Set the TYPE_DECL for this type to contain the right
3969 value for DECL_OFFSET, so that we can use it as part
3970 of a COMPONENT_REF for multiple inheritance. */
3972 layout_decl (TYPE_MAIN_DECL (t), 0);
3974 /* Now fix up any virtual base class types that we left lying
3975 around. We must get these done before we try to lay out the
3976 virtual function table. */
3977 pending_hard_virtuals = nreverse (pending_hard_virtuals);
3980 /* layout_basetypes will remove the base subobject fields. */
3981 max_has_virtual = layout_basetypes (t, max_has_virtual);
3983 TYPE_FIELDS (t) = fields;
3985 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
3989 vbases = CLASSTYPE_VBASECLASSES (t);
3990 CLASSTYPE_N_VBASECLASSES (t) = list_length (vbases);
3993 /* Now fixup overrides of all functions in vtables from all
3994 direct or indirect virtual base classes. */
3995 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3996 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3998 for (i = 0; i < n_baseclasses; i++)
4000 tree base_binfo = TREE_VEC_ELT (binfos, i);
4001 tree basetype = BINFO_TYPE (base_binfo);
4004 vbases = CLASSTYPE_VBASECLASSES (basetype);
4007 merge_overrides (binfo_member (BINFO_TYPE (vbases),
4008 CLASSTYPE_VBASECLASSES (t)),
4010 vbases = TREE_CHAIN (vbases);
4016 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
4017 might need to know it for setting up the offsets in the vtable
4018 (or in thunks) below. */
4019 if (vfield != NULL_TREE
4020 && DECL_FIELD_CONTEXT (vfield) != t)
4022 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4023 tree offset = BINFO_OFFSET (binfo);
4025 vfield = copy_node (vfield);
4026 copy_lang_decl (vfield);
4028 if (! integer_zerop (offset))
4029 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4030 DECL_FIELD_CONTEXT (vfield) = t;
4031 DECL_CLASS_CONTEXT (vfield) = t;
4032 DECL_FIELD_BITPOS (vfield)
4033 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4034 CLASSTYPE_VFIELD (t) = vfield;
4038 cp_warning ("Doing hard virtuals for %T...", t);
4041 if (has_virtual > max_has_virtual)
4042 max_has_virtual = has_virtual;
4043 if (max_has_virtual > 0)
4044 TYPE_VIRTUAL_P (t) = 1;
4046 if (flag_rtti && TYPE_VIRTUAL_P (t) && !pending_hard_virtuals)
4047 modify_all_vtables (t, NULL_TREE, NULL_TREE);
4049 while (pending_hard_virtuals)
4051 modify_all_vtables (t,
4052 TREE_PURPOSE (pending_hard_virtuals),
4053 TREE_VALUE (pending_hard_virtuals));
4054 pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
4057 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
4060 /* Now fixup any virtual function entries from virtual bases
4061 that have different deltas. This has to come after we do the
4062 pending hard virtuals, as we might have a function that comes
4063 from multiple virtual base instances that is only overridden
4064 by a hard virtual above. */
4065 vbases = CLASSTYPE_VBASECLASSES (t);
4068 /* We might be able to shorten the amount of work we do by
4069 only doing this for vtables that come from virtual bases
4070 that have differing offsets, but don't want to miss any
4072 fixup_vtable_deltas (vbases, 1, t);
4073 vbases = TREE_CHAIN (vbases);
4077 /* Under our model of GC, every C++ class gets its own virtual
4078 function table, at least virtually. */
4079 if (pending_virtuals)
4081 pending_virtuals = nreverse (pending_virtuals);
4082 /* We must enter these virtuals into the table. */
4083 if (first_vfn_base_index < 0)
4085 /* The second slot is for the tdesc pointer when thunks are used. */
4086 if (flag_vtable_thunks)
4087 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4089 /* The first slot is for the rtti offset. */
4090 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4092 set_rtti_entry (pending_virtuals,
4093 convert (ssizetype, integer_zero_node), t);
4094 build_vtable (NULL_TREE, t);
4098 /* Here we know enough to change the type of our virtual
4099 function table, but we will wait until later this function. */
4101 if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
4102 build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index), t);
4105 /* If this type has basetypes with constructors, then those
4106 constructors might clobber the virtual function table. But
4107 they don't if the derived class shares the exact vtable of the base
4110 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4112 else if (first_vfn_base_index >= 0)
4114 tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index);
4115 /* This class contributes nothing new to the virtual function
4116 table. However, it may have declared functions which
4117 went into the virtual function table "inherited" from the
4118 base class. If so, we grab a copy of those updated functions,
4119 and pretend they are ours. */
4121 /* See if we should steal the virtual info from base class. */
4122 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
4123 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
4124 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
4125 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
4126 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
4127 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4130 if (max_has_virtual || first_vfn_base_index >= 0)
4132 CLASSTYPE_VSIZE (t) = has_virtual;
4133 if (first_vfn_base_index >= 0)
4135 if (pending_virtuals)
4136 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t),
4139 else if (has_virtual)
4141 TYPE_BINFO_VIRTUALS (t) = pending_virtuals;
4142 DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1;
4146 /* Now lay out the virtual function table. */
4149 /* Use size_int so values are memoized in common cases. */
4150 tree itype = build_index_type (size_int (has_virtual));
4151 tree atype = build_cplus_array_type (vtable_entry_type, itype);
4153 layout_type (atype);
4155 CLASSTYPE_VFIELD (t) = vfield;
4157 /* We may have to grow the vtable. */
4158 if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
4160 TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
4161 DECL_SIZE (TYPE_BINFO_VTABLE (t)) = 0;
4162 layout_decl (TYPE_BINFO_VTABLE (t), 0);
4163 /* At one time the vtable info was grabbed 2 words at a time. This
4164 fails on sparc unless you have 8-byte alignment. (tiemann) */
4165 DECL_ALIGN (TYPE_BINFO_VTABLE (t))
4166 = MAX (TYPE_ALIGN (double_type_node),
4167 DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
4170 else if (first_vfn_base_index >= 0)
4171 CLASSTYPE_VFIELD (t) = vfield;
4172 CLASSTYPE_VFIELDS (t) = vfields;
4174 finish_struct_bits (t, max_has_virtual);
4176 /* Complete the rtl for any static member objects of the type we're
4178 for (x = fields; x; x = TREE_CHAIN (x))
4180 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4181 && TREE_TYPE (x) == t)
4183 DECL_MODE (x) = TYPE_MODE (t);
4184 make_decl_rtl (x, NULL, 0);
4188 if (TYPE_HAS_CONSTRUCTOR (t))
4190 tree vfields = CLASSTYPE_VFIELDS (t);
4194 /* Mark the fact that constructor for T
4195 could affect anybody inheriting from T
4196 who wants to initialize vtables for VFIELDS's type. */
4197 if (VF_DERIVED_VALUE (vfields))
4198 TREE_ADDRESSABLE (vfields) = 1;
4199 vfields = TREE_CHAIN (vfields);
4201 if (any_default_members != 0)
4202 build_class_init_list (t);
4204 else if (TYPE_NEEDS_CONSTRUCTING (t))
4205 build_class_init_list (t);
4207 /* Write out inline function definitions. */
4208 do_inline_function_hair (t, CLASSTYPE_INLINE_FRIENDS (t));
4209 CLASSTYPE_INLINE_FRIENDS (t) = 0;
4211 if (CLASSTYPE_VSIZE (t) != 0)
4214 /* This is now done above. */
4215 if (DECL_FIELD_CONTEXT (vfield) != t)
4217 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4218 tree offset = BINFO_OFFSET (binfo);
4220 vfield = copy_node (vfield);
4221 copy_lang_decl (vfield);
4223 if (! integer_zerop (offset))
4224 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4225 DECL_FIELD_CONTEXT (vfield) = t;
4226 DECL_CLASS_CONTEXT (vfield) = t;
4227 DECL_FIELD_BITPOS (vfield)
4228 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4229 CLASSTYPE_VFIELD (t) = vfield;
4233 /* In addition to this one, all the other vfields should be listed. */
4234 /* Before that can be done, we have to have FIELD_DECLs for them, and
4235 a place to find them. */
4236 TYPE_NONCOPIED_PARTS (t) = build_tree_list (default_conversion (TYPE_BINFO_VTABLE (t)), vfield);
4238 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
4239 && DECL_VINDEX (TREE_VEC_ELT (method_vec, 1)) == NULL_TREE)
4240 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
4244 /* Make the rtl for any new vtables we have created, and unmark
4245 the base types we marked. */
4246 finish_vtbls (TYPE_BINFO (t), 1, t);
4247 hack_incomplete_structures (t);
4250 if (TYPE_NAME (t) && TYPE_IDENTIFIER (t))
4251 undo_template_name_overload (TYPE_IDENTIFIER (t), 1);
4254 resume_momentary (old);
4256 if (warn_overloaded_virtual)
4260 /* This has to be done after we have sorted out what to do with
4261 the enclosing type. */
4262 if (write_symbols != DWARF_DEBUG)
4264 /* Be smarter about nested classes here. If a type is nested,
4265 only output it if we would output the enclosing type. */
4266 if (DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (t)))
4267 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = TREE_ASM_WRITTEN (TYPE_MAIN_DECL (t));
4271 if (write_symbols != DWARF_DEBUG && write_symbols != DWARF2_DEBUG)
4273 /* If the type has methods, we want to think about cutting down
4274 the amount of symbol table stuff we output. The value stored in
4275 the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
4276 For example, if a member function is seen and we decide to
4277 write out that member function, then we can change the value
4278 of the DECL_IGNORED_P slot, and the type will be output when
4279 that member function's debug info is written out.
4281 We can't do this with DWARF, which does not support name
4282 references between translation units. */
4283 if (CLASSTYPE_METHOD_VEC (t))
4285 /* Don't output full info about any type
4286 which does not have its implementation defined here. */
4287 if (CLASSTYPE_INTERFACE_ONLY (t))
4288 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4290 /* XXX do something about this. */
4291 else if (CLASSTYPE_INTERFACE_UNKNOWN (t))
4292 /* Only a first approximation! */
4293 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4296 else if (CLASSTYPE_INTERFACE_ONLY (t))
4297 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4300 /* Finish debugging output for this type. */
4301 rest_of_type_compilation (t, toplevel_bindings_p ());
4306 /* In [basic.scope.class] we have:
4308 A name N used in a class S shall refer to the same declaration in
4309 its context and when re-evaluated in the completed scope of S.
4311 This function checks this condition for X, which is a member of
4315 check_member_decl_is_same_in_complete_scope (t, x)
4319 /* A name N used in a class S shall refer to the same declaration in
4320 its context and when re-evaluated in the completed scope of S.
4322 Enums, types and static vars have already been checked. */
4323 if (TREE_CODE (x) != USING_DECL
4324 && TREE_CODE (x) != TYPE_DECL && !DECL_CLASS_TEMPLATE_P (x)
4325 && TREE_CODE (x) != CONST_DECL && TREE_CODE (x) != VAR_DECL)
4327 tree name = DECL_NAME (x);
4330 /* Don't get confused by access decls. */
4331 if (name && TREE_CODE (name) == IDENTIFIER_NODE)
4332 icv = IDENTIFIER_CLASS_VALUE (name);
4337 /* Don't complain about constructors. */
4338 && name != constructor_name (current_class_type)
4339 /* Or inherited names. */
4340 && id_in_current_class (name)
4341 /* Or shadowed tags. */
4342 && !(TREE_CODE (icv) == TYPE_DECL && DECL_CONTEXT (icv) == t))
4344 cp_pedwarn_at ("declaration of identifier `%D' as `%+#D'",
4346 cp_pedwarn_at ("conflicts with other use in class as `%#D'",
4352 /* When T was built up, the member declarations were added in reverse
4353 order. Rearrange them to declaration order. */
4356 unreverse_member_declarations (t)
4363 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
4364 reverse order. Put them in declaration order now. */
4365 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
4366 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
4368 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
4369 reverse order, so we can't just use nreverse. */
4371 for (x = TYPE_FIELDS (t);
4372 x && TREE_CODE (x) != TYPE_DECL;
4375 next = TREE_CHAIN (x);
4376 TREE_CHAIN (x) = prev;
4381 TREE_CHAIN (TYPE_FIELDS (t)) = x;
4383 TYPE_FIELDS (t) = prev;
4388 finish_struct (t, attributes, warn_anon)
4392 tree name = TYPE_NAME (t);
4395 if (TREE_CODE (name) == TYPE_DECL)
4399 DECL_SOURCE_FILE (name) = input_filename;
4400 /* For TYPE_DECL that are not typedefs (those marked with a line
4401 number of zero, we don't want to mark them as real typedefs.
4402 If this fails one needs to make sure real typedefs have a
4403 previous line number, even if it is wrong, that way the below
4404 will fill in the right line number. (mrs) */
4405 if (DECL_SOURCE_LINE (name))
4406 DECL_SOURCE_LINE (name) = lineno;
4407 CLASSTYPE_SOURCE_LINE (t) = lineno;
4408 name = DECL_NAME (name);
4411 /* Append the fields we need for constructing signature tables. */
4412 if (IS_SIGNATURE (t))
4413 append_signature_fields (t);
4415 /* Now that we've got all the field declarations, reverse everything
4417 unreverse_member_declarations (t);
4419 if (flag_optional_diags)
4421 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
4422 check_member_decl_is_same_in_complete_scope (t, x);
4423 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
4424 check_member_decl_is_same_in_complete_scope (t, x);
4427 /* Mark all the tags in the class as class-local. */
4428 for (x = CLASSTYPE_TAGS (t); x; x = TREE_CHAIN (x))
4429 TREE_NONLOCAL_FLAG (TREE_VALUE (x)) = 0;
4431 cplus_decl_attributes (t, attributes, NULL_TREE);
4433 if (processing_template_decl)
4435 tree d = getdecls ();
4436 for (; d; d = TREE_CHAIN (d))
4438 /* If this is the decl for the class or one of the template
4439 parms, we've seen all the injected decls. */
4440 if ((TREE_CODE (d) == TYPE_DECL
4441 && (TREE_TYPE (d) == t
4442 || TREE_CODE (TREE_TYPE (d)) == TEMPLATE_TYPE_PARM
4443 || TREE_CODE (TREE_TYPE (d)) == TEMPLATE_TEMPLATE_PARM))
4444 || TREE_CODE (d) == CONST_DECL)
4446 /* Don't inject cache decls. */
4447 else if (IDENTIFIER_TEMPLATE (DECL_NAME (d)))
4449 DECL_TEMPLATE_INJECT (CLASSTYPE_TI_TEMPLATE (t))
4450 = tree_cons (NULL_TREE, d,
4451 DECL_TEMPLATE_INJECT (CLASSTYPE_TI_TEMPLATE (t)));
4453 finish_struct_methods (t);
4454 TYPE_SIZE (t) = integer_zero_node;
4457 t = finish_struct_1 (t, warn_anon);
4459 TYPE_BEING_DEFINED (t) = 0;
4460 if (current_class_type)
4463 error ("trying to finish struct, but kicked out due to previous parse errors.");
4468 /* Return the dynamic type of INSTANCE, if known.
4469 Used to determine whether the virtual function table is needed
4472 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4473 of our knowledge of its type. */
4476 fixed_type_or_null (instance, nonnull)
4480 switch (TREE_CODE (instance))
4483 /* Check that we are not going through a cast of some sort. */
4484 if (TREE_TYPE (instance)
4485 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
4486 instance = TREE_OPERAND (instance, 0);
4487 /* fall through... */
4489 /* This is a call to a constructor, hence it's never zero. */
4490 if (TREE_HAS_CONSTRUCTOR (instance))
4494 return TREE_TYPE (instance);
4499 /* This is a call to a constructor, hence it's never zero. */
4500 if (TREE_HAS_CONSTRUCTOR (instance))
4504 return TREE_TYPE (instance);
4506 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4513 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
4514 /* Propagate nonnull. */
4515 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4516 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
4517 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4522 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4527 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4530 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
4534 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
4535 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
4539 return TREE_TYPE (TREE_TYPE (instance));
4541 /* fall through... */
4544 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
4548 return TREE_TYPE (instance);
4552 if (instance == current_class_ptr
4553 && flag_this_is_variable <= 0)
4555 /* Normally, 'this' must be non-null. */
4556 if (flag_this_is_variable == 0)
4559 /* <0 means we're in a constructor and we know our type. */
4560 if (flag_this_is_variable < 0)
4561 return TREE_TYPE (TREE_TYPE (instance));
4563 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4564 /* Reference variables should be references to objects. */
4574 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
4575 to the static type. We also handle the case where INSTANCE is really
4578 Used to determine whether the virtual function table is needed
4581 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4582 of our knowledge of its type. */
4585 resolves_to_fixed_type_p (instance, nonnull)
4589 tree t = TREE_TYPE (instance);
4590 tree fixed = fixed_type_or_null (instance, nonnull);
4591 if (fixed == NULL_TREE)
4593 if (POINTER_TYPE_P (t))
4595 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
4600 init_class_processing ()
4602 current_class_depth = 0;
4603 current_class_stack_size = 10;
4605 = (class_stack_node_t) xmalloc (current_class_stack_size
4606 * sizeof (struct class_stack_node));
4608 current_lang_stacksize = 10;
4609 current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree));
4610 current_lang_stack = current_lang_base;
4612 access_default_node = build_int_2 (0, 0);
4613 access_public_node = build_int_2 (1, 0);
4614 access_protected_node = build_int_2 (2, 0);
4615 access_private_node = build_int_2 (3, 0);
4616 access_default_virtual_node = build_int_2 (4, 0);
4617 access_public_virtual_node = build_int_2 (5, 0);
4618 access_protected_virtual_node = build_int_2 (6, 0);
4619 access_private_virtual_node = build_int_2 (7, 0);
4621 /* Keep these values lying around. */
4622 base_layout_decl = build_lang_field_decl (FIELD_DECL, NULL_TREE, error_mark_node);
4623 TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE);
4625 gcc_obstack_init (&class_obstack);
4628 /* Set current scope to NAME. CODE tells us if this is a
4629 STRUCT, UNION, or ENUM environment.
4631 NAME may end up being NULL_TREE if this is an anonymous or
4632 late-bound struct (as in "struct { ... } foo;") */
4634 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
4635 appropriate values, found by looking up the type definition of
4638 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
4639 which can be seen locally to the class. They are shadowed by
4640 any subsequent local declaration (including parameter names).
4642 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
4643 which have static meaning (i.e., static members, static
4644 member functions, enum declarations, etc).
4646 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
4647 which can be seen locally to the class (as in 1), but
4648 know that we are doing this for declaration purposes
4649 (i.e. friend foo::bar (int)).
4651 So that we may avoid calls to lookup_name, we cache the _TYPE
4652 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
4654 For multiple inheritance, we perform a two-pass depth-first search
4655 of the type lattice. The first pass performs a pre-order search,
4656 marking types after the type has had its fields installed in
4657 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
4658 unmarks the marked types. If a field or member function name
4659 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
4660 that name becomes `error_mark_node'. */
4663 pushclass (type, modify)
4667 type = TYPE_MAIN_VARIANT (type);
4669 /* Make sure there is enough room for the new entry on the stack. */
4670 if (current_class_depth + 1 >= current_class_stack_size)
4672 current_class_stack_size *= 2;
4674 = (class_stack_node_t) xrealloc (current_class_stack,
4675 current_class_stack_size
4676 * sizeof (struct class_stack_node));
4679 /* Insert a new entry on the class stack. */
4680 current_class_stack[current_class_depth].name = current_class_name;
4681 current_class_stack[current_class_depth].type = current_class_type;
4682 current_class_stack[current_class_depth].access = current_access_specifier;
4683 current_class_depth++;
4685 /* Now set up the new type. */
4686 current_class_name = TYPE_NAME (type);
4687 if (TREE_CODE (current_class_name) == TYPE_DECL)
4688 current_class_name = DECL_NAME (current_class_name);
4689 current_class_type = type;
4691 /* By default, things in classes are private, while things in
4692 structures or unions are public. */
4693 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
4694 ? access_private_node
4695 : access_public_node);
4697 if (previous_class_type != NULL_TREE
4698 && (type != previous_class_type || TYPE_SIZE (previous_class_type) == NULL_TREE)
4699 && current_class_depth == 1)
4701 /* Forcibly remove any old class remnants. */
4703 previous_class_type = NULL_TREE;
4709 if (CLASSTYPE_TEMPLATE_INFO (type))
4710 overload_template_name (type);
4716 tree this_fndecl = current_function_decl;
4718 if (current_function_decl
4719 && DECL_CONTEXT (current_function_decl)
4720 && TREE_CODE (DECL_CONTEXT (current_function_decl)) == FUNCTION_DECL)
4721 current_function_decl = DECL_CONTEXT (current_function_decl);
4723 current_function_decl = NULL_TREE;
4725 if (type != previous_class_type || current_class_depth > 1)
4728 build_mi_matrix (type);
4729 push_class_decls (type);
4732 push_class_decls (type);
4739 /* Hooray, we successfully cached; let's just install the
4740 cached class_shadowed list, and walk through it to get the
4741 IDENTIFIER_TYPE_VALUEs correct. */
4742 set_class_shadows (previous_class_values);
4743 for (item = previous_class_values; item; item = TREE_CHAIN (item))
4745 tree id = TREE_PURPOSE (item);
4746 tree decl = IDENTIFIER_CLASS_VALUE (id);
4748 if (TREE_CODE (decl) == TYPE_DECL)
4749 set_identifier_type_value (id, TREE_TYPE (decl));
4751 unuse_fields (type);
4754 for (tags = CLASSTYPE_TAGS (type); tags; tags = TREE_CHAIN (tags))
4756 tree tag_type = TREE_VALUE (tags);
4758 TREE_NONLOCAL_FLAG (tag_type) = 1;
4759 if (! TREE_PURPOSE (tags))
4761 if (! (IS_AGGR_TYPE_CODE (TREE_CODE (tag_type))
4762 && CLASSTYPE_IS_TEMPLATE (tag_type)))
4763 pushtag (TREE_PURPOSE (tags), tag_type, 0);
4765 pushdecl_class_level (CLASSTYPE_TI_TEMPLATE (tag_type));
4768 current_function_decl = this_fndecl;
4772 /* Get out of the current class scope. If we were in a class scope
4773 previously, that is the one popped to. The flag MODIFY tells whether
4774 the current scope declarations needs to be modified as a result of
4775 popping to the previous scope. 0 is used for class definitions. */
4783 /* Back this old class out completely. */
4784 tree tags = CLASSTYPE_TAGS (previous_class_type);
4787 /* This code can be seen as a cache miss. When we've cached a
4788 class' scope's bindings and we can't use them, we need to reset
4789 them. This is it! */
4790 for (t = previous_class_values; t; t = TREE_CHAIN (t))
4791 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
4794 TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
4795 tags = TREE_CHAIN (tags);
4802 /* Just remove from this class what didn't make
4803 it into IDENTIFIER_CLASS_VALUE. */
4804 tree tags = CLASSTYPE_TAGS (current_class_type);
4808 TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
4809 tags = TREE_CHAIN (tags);
4813 /* Force clearing of IDENTIFIER_CLASS_VALUEs after a class definition,
4814 since not all class decls make it there currently. */
4815 poplevel_class (! modify);
4817 /* Since poplevel_class does the popping of class decls nowadays,
4818 this really only frees the obstack used for these decls.
4819 That's why it had to be moved down here. */
4823 current_class_depth--;
4824 current_class_name = current_class_stack[current_class_depth].name;
4825 current_class_type = current_class_stack[current_class_depth].type;
4826 current_access_specifier = current_class_stack[current_class_depth].access;
4832 /* Returns 1 if current_class_type is either T or a nested type of T. */
4835 currently_open_class (t)
4839 if (t == current_class_type)
4841 for (i = 0; i < current_class_depth; ++i)
4842 if (current_class_stack [i].type == t)
4847 /* When entering a class scope, all enclosing class scopes' names with
4848 static meaning (static variables, static functions, types and enumerators)
4849 have to be visible. This recursive function calls pushclass for all
4850 enclosing class contexts until global or a local scope is reached.
4851 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
4852 formal of the same name. */
4855 push_nested_class (type, modify)
4861 my_friendly_assert (!type || TREE_CODE (type) != NAMESPACE_DECL, 980711);
4863 if (type == NULL_TREE || type == error_mark_node || ! IS_AGGR_TYPE (type)
4864 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
4865 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
4868 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
4870 if (context && TREE_CODE (context) == RECORD_TYPE)
4871 push_nested_class (context, 2);
4872 pushclass (type, modify);
4875 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
4878 pop_nested_class (modify)
4881 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
4884 if (context && TREE_CODE (context) == RECORD_TYPE)
4885 pop_nested_class (modify);
4888 /* Set global variables CURRENT_LANG_NAME to appropriate value
4889 so that behavior of name-mangling machinery is correct. */
4892 push_lang_context (name)
4895 *current_lang_stack++ = current_lang_name;
4896 if (current_lang_stack >= current_lang_base + current_lang_stacksize)
4899 = (tree *)xrealloc (current_lang_base,
4900 sizeof (tree) * (current_lang_stacksize + 10));
4901 current_lang_stack = current_lang_base + current_lang_stacksize;
4902 current_lang_stacksize += 10;
4905 if (name == lang_name_cplusplus || name == lang_name_java)
4907 strict_prototype = strict_prototypes_lang_cplusplus;
4908 current_lang_name = name;
4910 else if (name == lang_name_c)
4912 strict_prototype = strict_prototypes_lang_c;
4913 current_lang_name = name;
4916 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
4919 /* Get out of the current language scope. */
4924 current_lang_name = *--current_lang_stack;
4925 if (current_lang_name == lang_name_cplusplus
4926 || current_lang_name == lang_name_java)
4927 strict_prototype = strict_prototypes_lang_cplusplus;
4928 else if (current_lang_name == lang_name_c)
4929 strict_prototype = strict_prototypes_lang_c;
4932 /* Type instantiation routines. */
4935 validate_lhs (lhstype, complain)
4939 if (TYPE_PTRMEMFUNC_P (lhstype))
4940 lhstype = TYPE_PTRMEMFUNC_FN_TYPE (lhstype);
4942 if (TREE_CODE (lhstype) == POINTER_TYPE)
4944 if (TREE_CODE (TREE_TYPE (lhstype)) == FUNCTION_TYPE
4945 || TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE)
4946 lhstype = TREE_TYPE (lhstype);
4950 error ("invalid type combination for overload");
4951 return error_mark_node;
4957 /* This function will instantiate the type of the expression given in
4958 RHS to match the type of LHSTYPE. If errors exist, then return
4959 error_mark_node. If only complain is COMPLAIN is set. If we are
4960 not complaining, never modify rhs, as overload resolution wants to
4961 try many possible instantiations, in hopes that at least one will
4964 For non-recursive calls, LHSTYPE should be a function, pointer to
4965 function, or a pointer to member function. */
4968 instantiate_type (lhstype, rhs, complain)
4972 tree explicit_targs = NULL_TREE;
4973 int template_only = 0;
4975 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
4978 error ("not enough type information");
4979 return error_mark_node;
4982 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
4984 if (same_type_p (lhstype, TREE_TYPE (rhs)))
4987 cp_error ("argument of type `%T' does not match `%T'",
4988 TREE_TYPE (rhs), lhstype);
4989 return error_mark_node;
4992 /* We don't overwrite rhs if it is an overloaded function.
4993 Copying it would destroy the tree link. */
4994 if (TREE_CODE (rhs) != OVERLOAD)
4995 rhs = copy_node (rhs);
4997 /* This should really only be used when attempting to distinguish
4998 what sort of a pointer to function we have. For now, any
4999 arithmetic operation which is not supported on pointers
5000 is rejected as an error. */
5002 switch (TREE_CODE (rhs))
5009 my_friendly_abort (177);
5010 return error_mark_node;
5017 new_rhs = instantiate_type (build_pointer_type (lhstype),
5018 TREE_OPERAND (rhs, 0), complain);
5019 if (new_rhs == error_mark_node)
5020 return error_mark_node;
5022 TREE_TYPE (rhs) = lhstype;
5023 TREE_OPERAND (rhs, 0) = new_rhs;
5028 rhs = copy_node (TREE_OPERAND (rhs, 0));
5029 TREE_TYPE (rhs) = unknown_type_node;
5030 return instantiate_type (lhstype, rhs, complain);
5034 tree field = TREE_OPERAND (rhs, 1);
5035 if (TREE_CODE (field) == TREE_LIST)
5037 tree function = instantiate_type (lhstype, field, complain);
5038 if (function == error_mark_node)
5039 return error_mark_node;
5040 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 185);
5042 if (! DECL_STATIC_FUNCTION_P (function))
5044 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
5045 if (TYPE_MAIN_VARIANT (t) == current_class_type)
5046 t = constructor_name (t);
5048 cp_error ("object-dependent reference to `%D' can only be used in a call",
5050 cp_error (" to form a pointer to member function, say `&%T::%D'",
5051 t, DECL_NAME (function));
5054 mark_used (function);
5058 /* I could not trigger this code. MvL */
5059 my_friendly_abort (980326);
5064 /* This can happen if we are forming a pointer-to-member for a
5066 rhs = TREE_OPERAND (rhs, 1);
5067 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
5071 case TEMPLATE_ID_EXPR:
5073 explicit_targs = TREE_OPERAND (rhs, 1);
5075 rhs = TREE_OPERAND (rhs, 0);
5078 my_friendly_assert (TREE_CODE (rhs) == OVERLOAD, 980401);
5084 /* Check that the LHSTYPE and the RHS are reasonable. */
5085 lhstype = validate_lhs (lhstype, complain);
5086 if (lhstype == error_mark_node)
5089 if (TREE_CODE (lhstype) != FUNCTION_TYPE
5090 && TREE_CODE (lhstype) != METHOD_TYPE)
5093 cp_error("cannot resolve overloaded function `%D' "
5094 "based on non-function type `%T'",
5095 DECL_NAME (OVL_FUNCTION (rhs)), lhstype);
5096 return error_mark_node;
5099 /* Look for an exact match, by searching through the
5100 overloaded functions. */
5102 /* If we're processing a template-id, only a template
5103 function can match, so we don't look through the
5104 overloaded functions. */
5106 else for (elems = rhs; elems; elems = OVL_CHAIN (elems))
5108 elem = OVL_FUNCTION (elems);
5109 if (TREE_CODE (elem) == FUNCTION_DECL
5110 && same_type_p (lhstype, TREE_TYPE (elem)))
5117 /* No overloaded function was an exact match. See if we can
5118 instantiate some template to match. */
5122 if (TREE_CODE (elems) == TREE_LIST)
5123 elems = TREE_VALUE (rhs);
5124 for (; elems; elems = OVL_NEXT (elems))
5125 if (TREE_CODE (elem = OVL_CURRENT (elems)) == TEMPLATE_DECL)
5127 int n = DECL_NTPARMS (elem);
5128 tree t = make_scratch_vec (n);
5130 i = type_unification
5131 (DECL_INNERMOST_TEMPLATE_PARMS (elem), t,
5132 TYPE_ARG_TYPES (TREE_TYPE (elem)),
5133 TYPE_ARG_TYPES (lhstype), explicit_targs, DEDUCE_EXACT, 1);
5138 cp_error ("ambiguous template instantiation converting to `%#T'", lhstype);
5139 return error_mark_node;
5141 save_elem = instantiate_template (elem, t);
5142 /* Check the return type. */
5143 if (!same_type_p (TREE_TYPE (lhstype),
5144 TREE_TYPE (TREE_TYPE (save_elem))))
5150 mark_used (save_elem);
5155 /* There's no exact match, and no templates can be
5156 instantiated to match. The last thing we try is to see if
5157 some ordinary overloaded function is close enough. If
5158 we're only looking for template functions, we don't do
5162 for (elems = rhs; elems; elems = OVL_NEXT (elems))
5164 elem = OVL_CURRENT (elems);
5165 if (TREE_CODE (elem) == FUNCTION_DECL
5166 && comp_target_types (lhstype, TREE_TYPE (elem), 1) > 0)
5171 tree save_elem = elem;
5172 for (elems = OVL_CHAIN (elems); elems;
5173 elems = OVL_CHAIN (elems))
5175 elem = OVL_FUNCTION (elems);
5176 if (TREE_CODE (elem) == FUNCTION_DECL
5177 && comp_target_types (lhstype, TREE_TYPE (elem), 0) >0)
5185 ("cannot resolve overload to target type `%#T'",
5187 cp_error_at (" ambiguity between `%#D'", save_elem);
5188 cp_error_at (" and `%#D', at least", elem);
5190 return error_mark_node;
5192 mark_used (save_elem);
5197 /* We failed to find a match. */
5200 cp_error ("cannot resolve overload to target type `%#T'", lhstype);
5202 (" because no suitable overload of function `%D' exists",
5203 DECL_NAME (OVL_FUNCTION (rhs)));
5205 return error_mark_node;
5210 tree elem, baselink, name = NULL_TREE;
5212 if (TREE_PURPOSE (rhs) == error_mark_node)
5214 /* Make sure we don't drop the non-local flag, as the old code
5215 would rely on it. */
5216 int nl = TREE_NONLOCAL_FLAG (rhs);
5217 /* We don't need the type of this node. */
5218 rhs = TREE_VALUE (rhs);
5219 my_friendly_assert (TREE_NONLOCAL_FLAG (rhs) == nl, 980331);
5222 /* Now we should have a baselink. */
5223 my_friendly_assert (TREE_CODE (TREE_PURPOSE (rhs)) == TREE_VEC,
5225 /* First look for an exact match. Search member functions.
5226 May have to undo what `default_conversion' might do to
5229 lhstype = validate_lhs (lhstype, complain);
5230 if (lhstype == error_mark_node)
5233 my_friendly_assert (TREE_CHAIN (rhs) == NULL_TREE, 181);
5234 my_friendly_assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL
5235 || TREE_CODE (TREE_VALUE (rhs)) == OVERLOAD,
5238 for (baselink = rhs; baselink;
5239 baselink = next_baselink (baselink))
5241 elem = TREE_VALUE (baselink);
5243 if (same_type_p (lhstype, TREE_TYPE (OVL_CURRENT (elem))))
5245 mark_used (OVL_CURRENT (elem));
5246 return OVL_CURRENT (elem);
5249 elem = OVL_NEXT (elem);
5253 while (TREE_CODE (name) == TREE_LIST)
5254 name = TREE_VALUE (name);
5255 name = DECL_NAME (OVL_CURRENT (name));
5258 cp_error ("no compatible member functions named `%D'", name);
5259 return error_mark_node;
5263 /* This is too hard for now. */
5264 my_friendly_abort (183);
5265 return error_mark_node;
5270 TREE_OPERAND (rhs, 0)
5271 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
5272 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5273 return error_mark_node;
5274 TREE_OPERAND (rhs, 1)
5275 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
5276 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5277 return error_mark_node;
5279 TREE_TYPE (rhs) = lhstype;
5283 case TRUNC_DIV_EXPR:
5284 case FLOOR_DIV_EXPR:
5286 case ROUND_DIV_EXPR:
5288 case TRUNC_MOD_EXPR:
5289 case FLOOR_MOD_EXPR:
5291 case ROUND_MOD_EXPR:
5292 case FIX_ROUND_EXPR:
5293 case FIX_FLOOR_EXPR:
5295 case FIX_TRUNC_EXPR:
5311 case PREINCREMENT_EXPR:
5312 case PREDECREMENT_EXPR:
5313 case POSTINCREMENT_EXPR:
5314 case POSTDECREMENT_EXPR:
5316 error ("invalid operation on uninstantiated type");
5317 return error_mark_node;
5319 case TRUTH_AND_EXPR:
5321 case TRUTH_XOR_EXPR:
5328 case TRUTH_ANDIF_EXPR:
5329 case TRUTH_ORIF_EXPR:
5330 case TRUTH_NOT_EXPR:
5332 error ("not enough type information");
5333 return error_mark_node;
5336 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
5339 error ("not enough type information");
5340 return error_mark_node;
5342 TREE_OPERAND (rhs, 1)
5343 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
5344 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5345 return error_mark_node;
5346 TREE_OPERAND (rhs, 2)
5347 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), complain);
5348 if (TREE_OPERAND (rhs, 2) == error_mark_node)
5349 return error_mark_node;
5351 TREE_TYPE (rhs) = lhstype;
5355 TREE_OPERAND (rhs, 1)
5356 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
5357 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5358 return error_mark_node;
5360 TREE_TYPE (rhs) = lhstype;
5364 if (TYPE_PTRMEMFUNC_P (lhstype))
5365 lhstype = TYPE_PTRMEMFUNC_FN_TYPE (lhstype);
5366 else if (TREE_CODE (lhstype) != POINTER_TYPE)
5369 error ("type for resolving address of overloaded function must be pointer type");
5370 return error_mark_node;
5373 tree fn = instantiate_type (TREE_TYPE (lhstype), TREE_OPERAND (rhs, 0), complain);
5374 if (fn == error_mark_node)
5375 return error_mark_node;
5376 mark_addressable (fn);
5377 TREE_TYPE (rhs) = lhstype;
5378 TREE_OPERAND (rhs, 0) = fn;
5379 TREE_CONSTANT (rhs) = staticp (fn);
5380 if (TREE_CODE (lhstype) == POINTER_TYPE
5381 && TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE)
5383 build_ptrmemfunc_type (lhstype);
5384 rhs = build_ptrmemfunc (lhstype, rhs, 0);
5389 case ENTRY_VALUE_EXPR:
5390 my_friendly_abort (184);
5391 return error_mark_node;
5394 return error_mark_node;
5397 my_friendly_abort (185);
5398 return error_mark_node;
5402 /* Return the name of the virtual function pointer field
5403 (as an IDENTIFIER_NODE) for the given TYPE. Note that
5404 this may have to look back through base types to find the
5405 ultimate field name. (For single inheritance, these could
5406 all be the same name. Who knows for multiple inheritance). */
5409 get_vfield_name (type)
5412 tree binfo = TYPE_BINFO (type);
5415 while (BINFO_BASETYPES (binfo)
5416 && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
5417 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
5418 binfo = BINFO_BASETYPE (binfo, 0);
5420 type = BINFO_TYPE (binfo);
5421 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
5422 + TYPE_NAME_LENGTH (type) + 2);
5423 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
5424 return get_identifier (buf);
5428 print_class_statistics ()
5430 #ifdef GATHER_STATISTICS
5431 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
5432 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
5433 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
5434 n_build_method_call, n_inner_fields_searched);
5437 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
5438 n_vtables, n_vtable_searches);
5439 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
5440 n_vtable_entries, n_vtable_elems);
5445 /* Push an obstack which is sufficiently long-lived to hold such class
5446 decls that may be cached in the previous_class_values list. For now, let's
5447 use the permanent obstack, later we may create a dedicated obstack just
5448 for this purpose. The effect is undone by pop_obstacks. */
5451 maybe_push_cache_obstack ()
5453 push_obstacks_nochange ();
5454 if (current_class_depth == 1)
5455 current_obstack = &permanent_obstack;
5458 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
5459 according to [class]:
5460 The class-name is also inserted
5461 into the scope of the class itself. For purposes of access checking,
5462 the inserted class name is treated as if it were a public member name. */
5465 build_self_reference ()
5467 tree name = constructor_name (current_class_type);
5468 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
5469 DECL_NONLOCAL (value) = 1;
5470 DECL_CONTEXT (value) = current_class_type;
5471 DECL_CLASS_CONTEXT (value) = current_class_type;
5472 DECL_ARTIFICIAL (value) = 1;
5474 pushdecl_class_level (value);
5478 /* Returns 1 if TYPE contains only padding bytes. */
5481 is_empty_class (type)
5486 if (type == error_mark_node)
5489 if (! IS_AGGR_TYPE (type))
5493 return CLASSTYPE_SIZE (type) == integer_zero_node;
5495 if (TYPE_BINFO_BASETYPES (type))
5497 t = TYPE_FIELDS (type);
5498 while (t && TREE_CODE (t) != FIELD_DECL)
5500 return (t == NULL_TREE);
5503 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
5504 a *_TYPE node. NODE can also be a local class. */
5507 get_enclosing_class (type)
5512 while (node && TREE_CODE (node) != NAMESPACE_DECL)
5514 switch (TREE_CODE_CLASS (TREE_CODE (node)))
5517 node = DECL_CONTEXT (node);
5523 node = TYPE_CONTEXT (node);
5527 my_friendly_abort (0);
5533 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
5536 is_base_of_enclosing_class (base, type)
5541 if (get_binfo (base, type, 0))
5544 type = get_enclosing_class (type);