1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 92-97, 1998, 1999 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. */
33 #include "splay-tree.h"
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
39 /* This is how we tell when two virtual member functions are really the
41 #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
43 extern void set_class_shadows PROTO ((tree));
45 /* The number of nested classes being processed. If we are not in the
46 scope of any class, this is zero. */
48 int current_class_depth;
50 /* In order to deal with nested classes, we keep a stack of classes.
51 The topmost entry is the innermost class, and is the entry at index
52 CURRENT_CLASS_DEPTH */
54 typedef struct class_stack_node {
55 /* The name of the class. */
58 /* The _TYPE node for the class. */
61 /* The access specifier pending for new declarations in the scope of
65 /* If were defining TYPE, the names used in this class. */
66 splay_tree names_used;
67 }* class_stack_node_t;
69 /* The stack itself. This is an dynamically resized array. The
70 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
71 static int current_class_stack_size;
72 static class_stack_node_t current_class_stack;
74 /* When we're processing a member function, current_class_ptr is the
75 PARM_DECL for the `this' pointer. The current_class_ref is an
76 expression for `*this'. */
77 tree current_class_ptr, current_class_ref;
79 /* The following two can be derived from the previous one */
80 tree current_class_name; /* IDENTIFIER_NODE: name of current class */
81 tree current_class_type; /* _TYPE: the type of the current class */
82 tree current_access_specifier;
83 tree previous_class_type; /* _TYPE: the previous type that was a class */
84 tree previous_class_values; /* TREE_LIST: copy of the class_shadowed list
85 when leaving an outermost class scope. */
87 /* The obstack on which the cached class declarations are kept. */
88 static struct obstack class_cache_obstack;
89 /* The first object allocated on that obstack. We can use
90 obstack_free with tis value to free the entire obstack. */
91 char *class_cache_firstobj;
95 static tree get_vfield_name PROTO((tree));
96 static void finish_struct_anon PROTO((tree));
97 static tree build_vbase_pointer PROTO((tree, tree));
98 static tree build_vtable_entry PROTO((tree, tree));
99 static tree get_vtable_name PROTO((tree));
100 static tree get_derived_offset PROTO((tree, tree));
101 static tree get_basefndecls PROTO((tree, tree));
102 static void set_rtti_entry PROTO((tree, tree, tree));
103 static tree build_vtable PROTO((tree, tree));
104 static void prepare_fresh_vtable PROTO((tree, tree));
105 static tree prepare_ctor_vtable PROTO((tree, tree, tree));
106 static void fixup_vtable_deltas1 PROTO((tree, tree));
107 static void fixup_vtable_deltas PROTO((tree, int, tree));
108 static tree finish_one_ctor_vtable PROTO((tree, tree, tree, tree));
109 static tree prepend_ctor_vfields_for_vbase PROTO((tree, tree, tree, tree, int, tree));
110 static tree finish_ctor_vtables_for_vbases PROTO((tree, tree, tree));
111 static tree finish_ctor_vtables_1 PROTO((tree, tree));
112 static tree prepend_vbase_vfields PROTO((tree, int, tree));
113 static void finish_ctor_vtables PROTO((tree));
114 static void finish_vtbls PROTO((tree, int, tree));
115 static void modify_vtable_entry PROTO((tree, tree, tree));
116 static tree get_vtable_entry_n PROTO((tree, unsigned HOST_WIDE_INT));
117 static void add_virtual_function PROTO((tree *, tree *, int *, tree, tree));
118 static tree delete_duplicate_fields_1 PROTO((tree, tree));
119 static void delete_duplicate_fields PROTO((tree));
120 static void finish_struct_bits PROTO((tree, int));
121 static int alter_access PROTO((tree, tree, tree, tree));
122 static void handle_using_decl PROTO((tree, tree, tree, tree));
123 static int overrides PROTO((tree, tree));
124 static int strictly_overrides PROTO((tree, tree));
125 static void merge_overrides PROTO((tree, tree, int, tree));
126 static void override_one_vtable PROTO((tree, tree, tree));
127 static void mark_overriders PROTO((tree, tree));
128 static void check_for_override PROTO((tree, tree));
129 static tree get_class_offset_1 PROTO((tree, tree, tree, tree, tree));
130 static tree get_class_offset PROTO((tree, tree, tree, tree));
131 static void modify_one_vtable PROTO((tree, tree, tree, tree));
132 static void modify_all_vtables PROTO((tree, tree, tree));
133 static void modify_all_direct_vtables PROTO((tree, int, tree, tree,
135 static void modify_all_indirect_vtables PROTO((tree, int, int, tree,
137 static int finish_base_struct PROTO((tree, struct base_info *));
138 static void finish_struct_methods PROTO((tree));
139 static void maybe_warn_about_overly_private_class PROTO ((tree));
140 static tree make_method_vec PROTO((int));
141 static void free_method_vec PROTO((tree));
142 static tree add_implicitly_declared_members PROTO((tree, int, int, int));
143 static tree fixed_type_or_null PROTO((tree, int *));
144 static tree resolve_address_of_overloaded_function PROTO((tree, tree, int,
146 static void build_vtable_entry_ref PROTO((tree, tree, tree));
148 /* Way of stacking language names. */
149 tree *current_lang_base, *current_lang_stack;
150 int current_lang_stacksize;
152 /* Names of languages we recognize. */
153 tree lang_name_c, lang_name_cplusplus, lang_name_java;
154 tree current_lang_name;
156 /* When layout out an aggregate type, the size of the
157 basetypes (virtual and non-virtual) is passed to layout_record
159 static tree base_layout_decl;
161 /* Constants used for access control. */
162 tree access_default_node; /* 0 */
163 tree access_public_node; /* 1 */
164 tree access_protected_node; /* 2 */
165 tree access_private_node; /* 3 */
166 tree access_default_virtual_node; /* 4 */
167 tree access_public_virtual_node; /* 5 */
168 tree access_protected_virtual_node; /* 6 */
169 tree access_private_virtual_node; /* 7 */
171 /* Variables shared between class.c and call.c. */
173 #ifdef GATHER_STATISTICS
175 int n_vtable_entries = 0;
176 int n_vtable_searches = 0;
177 int n_vtable_elems = 0;
178 int n_convert_harshness = 0;
179 int n_compute_conversion_costs = 0;
180 int n_build_method_call = 0;
181 int n_inner_fields_searched = 0;
184 /* Virtual baseclass things. */
187 build_vbase_pointer (exp, type)
191 FORMAT_VBASE_NAME (name, type);
193 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
197 /* Is the type of the EXPR, the complete type of the object?
198 If we are going to be wrong, we must be conservative, and return 0. */
201 complete_type_p (expr)
204 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
207 switch (TREE_CODE (expr))
214 expr = TREE_OPERAND (expr, 0);
218 if (! TREE_HAS_CONSTRUCTOR (expr))
220 /* fall through... */
223 if (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
224 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr)))
225 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
227 /* fall through... */
230 if (IS_AGGR_TYPE (TREE_TYPE (expr))
231 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
233 /* fall through... */
244 /* Build multi-level access to EXPR using hierarchy path PATH.
245 CODE is PLUS_EXPR if we are going with the grain,
246 and MINUS_EXPR if we are not (in which case, we cannot traverse
247 virtual baseclass links).
249 TYPE is the type we want this path to have on exit.
251 NONNULL is non-zero if we know (for any reason) that EXPR is
252 not, in fact, zero. */
255 build_vbase_path (code, type, expr, path, nonnull)
257 tree type, expr, path;
260 register int changed = 0;
261 tree last = NULL_TREE, last_virtual = NULL_TREE;
263 tree null_expr = 0, nonnull_expr;
265 tree offset = integer_zero_node;
267 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
268 return build1 (NOP_EXPR, type, expr);
270 /* If -fthis-is-variable, we might have set nonnull incorrectly. We
271 don't care enough to get this right, so just clear it. */
272 if (flag_this_is_variable > 0)
275 /* We could do better if we had additional logic to convert back to the
276 unconverted type (the static type of the complete object), and then
277 convert back to the type we want. Until that is done, we only optimize
278 if the complete type is the same type as expr has. */
279 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
281 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
282 expr = save_expr (expr);
285 if (BINFO_INHERITANCE_CHAIN (path))
286 path = reverse_path (path);
288 basetype = BINFO_TYPE (path);
292 if (TREE_VIA_VIRTUAL (path))
294 last_virtual = BINFO_TYPE (path);
295 if (code == PLUS_EXPR)
297 changed = ! fixed_type_p;
303 /* We already check for ambiguous things in the caller, just
307 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
308 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
310 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
311 nonnull_expr = build_vbase_pointer (ind, last_virtual);
313 && TREE_CODE (type) == POINTER_TYPE
314 && null_expr == NULL_TREE)
316 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
317 expr = build (COND_EXPR, build_pointer_type (last_virtual),
318 build (EQ_EXPR, boolean_type_node, expr,
320 null_expr, nonnull_expr);
323 /* else we'll figure out the offset below. */
325 /* Happens in the case of parse errors. */
326 if (nonnull_expr == error_mark_node)
327 return error_mark_node;
331 cp_error ("cannot cast up from virtual baseclass `%T'",
333 return error_mark_node;
337 path = BINFO_INHERITANCE_CHAIN (path);
339 /* LAST is now the last basetype assoc on the path. */
341 /* A pointer to a virtual base member of a non-null object
342 is non-null. Therefore, we only need to test for zeroness once.
343 Make EXPR the canonical expression to deal with here. */
346 TREE_OPERAND (expr, 2) = nonnull_expr;
347 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
348 = TREE_TYPE (nonnull_expr);
353 /* If we go through any virtual base pointers, make sure that
354 casts to BASETYPE from the last virtual base class use
355 the right value for BASETYPE. */
358 tree intype = TREE_TYPE (TREE_TYPE (expr));
359 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
361 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
362 offset = BINFO_OFFSET (binfo);
369 offset = BINFO_OFFSET (binfo_member (last_virtual,
370 CLASSTYPE_VBASECLASSES (basetype)));
371 offset = size_binop (PLUS_EXPR, offset, BINFO_OFFSET (last));
374 offset = BINFO_OFFSET (last);
377 if (TREE_INT_CST_LOW (offset))
379 /* Bash types to make the backend happy. */
380 offset = cp_convert (type, offset);
382 /* This shouldn't be necessary. (mrs) */
383 expr = build1 (NOP_EXPR, type, expr);
386 /* If expr might be 0, we need to preserve that zeroness. */
390 TREE_TYPE (null_expr) = type;
392 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
393 if (TREE_SIDE_EFFECTS (expr))
394 expr = save_expr (expr);
396 return build (COND_EXPR, type,
397 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
399 build (code, type, expr, offset));
401 else return build (code, type, expr, offset);
404 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
405 be used multiple times in initialization of multiple inheritance. */
408 TREE_TYPE (expr) = type;
412 return build1 (NOP_EXPR, type, expr);
415 /* Virtual function things. */
417 /* Build an entry in the virtual function table.
418 DELTA is the offset for the `this' pointer.
419 PFN is an ADDR_EXPR containing a pointer to the virtual function.
420 Note that the index (DELTA2) in the virtual function table
424 build_vtable_entry (delta, pfn)
427 if (flag_vtable_thunks)
429 HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta);
430 if (idelta && ! DECL_ABSTRACT_VIRTUAL_P (TREE_OPERAND (pfn, 0)))
432 pfn = build1 (ADDR_EXPR, vtable_entry_type,
433 make_thunk (pfn, idelta));
434 TREE_READONLY (pfn) = 1;
435 TREE_CONSTANT (pfn) = 1;
437 #ifdef GATHER_STATISTICS
438 n_vtable_entries += 1;
444 extern int flag_huge_objects;
445 tree elems = expr_tree_cons (NULL_TREE, delta,
446 expr_tree_cons (NULL_TREE, integer_zero_node,
447 build_expr_list (NULL_TREE, pfn)));
448 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
450 /* DELTA used to be constructed by `size_int' and/or size_binop,
451 which caused overflow problems when it was negative. That should
454 if (! int_fits_type_p (delta, delta_type_node))
456 if (flag_huge_objects)
457 sorry ("object size exceeds built-in limit for virtual function table implementation");
459 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
462 TREE_CONSTANT (entry) = 1;
463 TREE_STATIC (entry) = 1;
464 TREE_READONLY (entry) = 1;
466 #ifdef GATHER_STATISTICS
467 n_vtable_entries += 1;
474 /* We want to give the assembler the vtable identifier as well as
475 the offset to the function pointer. So we generate
477 __asm__ __volatile__ (".vtable_entry %c0, %c1"
478 : : "s"(&class_vtable),
479 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
482 build_vtable_entry_ref (basetype, vtbl, idx)
483 tree basetype, vtbl, idx;
485 static char asm_stmt[] = ".vtable_entry %c0, %c1";
488 s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
489 s = build_tree_list (build_string (1, "s"), s);
491 i = build_array_ref (vtbl, idx);
492 if (!flag_vtable_thunks)
493 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
494 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
495 i2 = build_array_ref (vtbl, build_int_2(0,0));
496 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
497 i = build_binary_op (MINUS_EXPR, i, i2);
498 i = build_tree_list (build_string (1, "i"), i);
500 expand_asm_operands (build_string (sizeof(asm_stmt)-1, asm_stmt),
501 NULL_TREE, chainon (s, i), NULL_TREE, 1, NULL, 0);
504 /* Given an object INSTANCE, return an expression which yields the
505 virtual function vtable element corresponding to INDEX. There are
506 many special cases for INSTANCE which we take care of here, mainly
507 to avoid creating extra tree nodes when we don't have to. */
510 build_vtbl_ref (instance, idx)
514 tree basetype = TREE_TYPE (instance);
516 if (TREE_CODE (basetype) == REFERENCE_TYPE)
517 basetype = TREE_TYPE (basetype);
519 if (instance == current_class_ref)
520 vtbl = build_vfield_ref (instance, basetype);
525 /* Try to figure out what a reference refers to, and
526 access its virtual function table directly. */
527 tree ref = NULL_TREE;
529 if (TREE_CODE (instance) == INDIRECT_REF
530 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
531 ref = TREE_OPERAND (instance, 0);
532 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
535 if (ref && TREE_CODE (ref) == VAR_DECL
536 && DECL_INITIAL (ref))
538 tree init = DECL_INITIAL (ref);
540 while (TREE_CODE (init) == NOP_EXPR
541 || TREE_CODE (init) == NON_LVALUE_EXPR)
542 init = TREE_OPERAND (init, 0);
543 if (TREE_CODE (init) == ADDR_EXPR)
545 init = TREE_OPERAND (init, 0);
546 if (IS_AGGR_TYPE (TREE_TYPE (init))
547 && (TREE_CODE (init) == PARM_DECL
548 || TREE_CODE (init) == VAR_DECL))
554 if (IS_AGGR_TYPE (TREE_TYPE (instance))
555 && (TREE_CODE (instance) == RESULT_DECL
556 || TREE_CODE (instance) == PARM_DECL
557 || TREE_CODE (instance) == VAR_DECL))
558 vtbl = TYPE_BINFO_VTABLE (basetype);
560 vtbl = build_vfield_ref (instance, basetype);
563 assemble_external (vtbl);
566 build_vtable_entry_ref (basetype, vtbl, idx);
568 aref = build_array_ref (vtbl, idx);
573 /* Given an object INSTANCE, return an expression which yields the
574 virtual function corresponding to INDEX. There are many special
575 cases for INSTANCE which we take care of here, mainly to avoid
576 creating extra tree nodes when we don't have to. */
579 build_vfn_ref (ptr_to_instptr, instance, idx)
580 tree *ptr_to_instptr, instance;
583 tree aref = build_vtbl_ref (instance, idx);
585 /* When using thunks, there is no extra delta, and we get the pfn
587 if (flag_vtable_thunks)
592 /* Save the intermediate result in a SAVE_EXPR so we don't have to
593 compute each component of the virtual function pointer twice. */
594 if (TREE_CODE (aref) == INDIRECT_REF)
595 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
598 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
600 cp_convert (ptrdiff_type_node,
601 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
604 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
607 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
608 for the given TYPE. */
611 get_vtable_name (type)
614 tree type_id = build_typename_overload (type);
615 char *buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
616 + IDENTIFIER_LENGTH (type_id) + 2);
617 const char *ptr = IDENTIFIER_POINTER (type_id);
619 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
621 /* We don't take off the numbers; prepare_fresh_vtable uses the
622 DECL_ASSEMBLER_NAME for the type, which includes the number
623 in `3foo'. If we were to pull them off here, we'd end up with
624 something like `_vt.foo.3bar', instead of a uniform definition. */
625 while (ptr[i] >= '0' && ptr[i] <= '9')
628 sprintf (buf, VTABLE_NAME_FORMAT, ptr+i);
629 return get_identifier (buf);
632 /* Return the offset to the main vtable for a given base BINFO. */
635 get_vfield_offset (binfo)
639 = size_binop (FLOOR_DIV_EXPR,
640 DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))),
641 size_int (BITS_PER_UNIT));
642 tmp = convert (sizetype, tmp);
643 return size_binop (PLUS_EXPR, tmp, BINFO_OFFSET (binfo));
646 /* Get the offset to the start of the original binfo that we derived
647 this binfo from. If we find TYPE first, return the offset only
648 that far. The shortened search is useful because the this pointer
649 on method calling is expected to point to a DECL_CONTEXT (fndecl)
650 object, and not a baseclass of it. */
653 get_derived_offset (binfo, type)
656 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
659 while (BINFO_BASETYPES (binfo)
660 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
662 tree binfos = BINFO_BASETYPES (binfo);
663 if (BINFO_TYPE (binfo) == type)
665 binfo = TREE_VEC_ELT (binfos, i);
667 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
668 return size_binop (MINUS_EXPR, offset1, offset2);
671 /* Update the rtti info for this class. */
674 set_rtti_entry (virtuals, offset, type)
675 tree virtuals, offset, type;
679 if (CLASSTYPE_COM_INTERFACE (type))
683 vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, get_tinfo_fn_unused (type));
685 vfn = build1 (NOP_EXPR, vfunc_ptr_type_node, size_zero_node);
686 TREE_CONSTANT (vfn) = 1;
688 if (! flag_vtable_thunks)
689 TREE_VALUE (virtuals) = build_vtable_entry (offset, vfn);
692 tree voff = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
693 TREE_CONSTANT (voff) = 1;
695 TREE_VALUE (virtuals) = build_vtable_entry (integer_zero_node, voff);
697 /* The second slot is for the tdesc pointer when thunks are used. */
698 TREE_VALUE (TREE_CHAIN (virtuals))
699 = build_vtable_entry (integer_zero_node, vfn);
703 /* Build a virtual function for type TYPE.
704 If BINFO is non-NULL, build the vtable starting with the initial
705 approximation that it is the same as the one which is the head of
706 the association list. */
709 build_vtable (binfo, type)
712 tree name = get_vtable_name (type);
719 virtuals = copy_list (BINFO_VIRTUALS (binfo));
720 decl = build_lang_decl (VAR_DECL, name, TREE_TYPE (BINFO_VTABLE (binfo)));
722 /* Now do rtti stuff. */
723 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
724 offset = ssize_binop (MINUS_EXPR, integer_zero_node, offset);
725 set_rtti_entry (virtuals, offset, type);
729 virtuals = NULL_TREE;
730 decl = build_lang_decl (VAR_DECL, name, void_type_node);
733 #ifdef GATHER_STATISTICS
735 n_vtable_elems += list_length (virtuals);
738 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
739 import_export_vtable (decl, type, 0);
741 decl = pushdecl_top_level (decl);
742 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
743 /* Initialize the association list for this type, based
744 on our first approximation. */
745 TYPE_BINFO_VTABLE (type) = decl;
746 TYPE_BINFO_VIRTUALS (type) = virtuals;
748 DECL_ARTIFICIAL (decl) = 1;
749 TREE_STATIC (decl) = 1;
750 #ifndef WRITABLE_VTABLES
751 /* Make them READONLY by default. (mrs) */
752 TREE_READONLY (decl) = 1;
754 /* At one time the vtable info was grabbed 2 words at a time. This
755 fails on sparc unless you have 8-byte alignment. (tiemann) */
756 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
759 DECL_VIRTUAL_P (decl) = 1;
760 DECL_CONTEXT (decl) = type;
762 binfo = TYPE_BINFO (type);
763 SET_BINFO_NEW_VTABLE_MARKED (binfo);
767 extern tree signed_size_zero_node;
769 /* Give TYPE a new virtual function table which is initialized
770 with a skeleton-copy of its original initialization. The only
771 entry that changes is the `delta' entry, so we can really
772 share a lot of structure.
774 FOR_TYPE is the derived type which caused this table to
777 BINFO is the type association which provided TYPE for FOR_TYPE.
779 The order in which vtables are built (by calling this function) for
780 an object must remain the same, otherwise a binary incompatibility
784 prepare_fresh_vtable (binfo, for_type)
785 tree binfo, for_type;
788 tree orig_decl = BINFO_VTABLE (binfo);
801 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
803 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
804 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
806 /* We know that the vtable that we are going to create doesn't exist
807 yet in the global namespace, and when we finish, it will be
808 pushed into the global namespace. In complex MI hierarchies, we
809 have to loop while the name we are thinking of adding is globally
810 defined, adding more name components to the vtable name as we
811 loop, until the name is unique. This is because in complex MI
812 cases, we might have the same base more than once. This means
813 that the order in which this function is called for vtables must
814 remain the same, otherwise binary compatibility can be
819 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
823 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
825 buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT) + strlen (buf1) + 1);
826 sprintf (buf, VTABLE_NAME_FORMAT, buf1);
827 name = get_identifier (buf);
829 /* If this name doesn't clash, then we can use it, otherwise
830 we add more to the name until it is unique. */
832 if (! IDENTIFIER_GLOBAL_VALUE (name))
835 /* Set values for next loop through, if the name isn't unique. */
837 path = BINFO_INHERITANCE_CHAIN (path);
839 /* We better not run out of stuff to make it unique. */
840 my_friendly_assert (path != NULL_TREE, 368);
842 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
844 if (for_type == basetype)
846 /* If we run out of basetypes in the path, we have already
847 found created a vtable with that name before, we now
848 resort to tacking on _%d to distinguish them. */
850 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
851 buf1 = (char *) alloca (i);
853 sprintf (buf1, "%s%c%s%c%d",
854 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
856 buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
857 + strlen (buf1) + 1);
858 sprintf (buf, VTABLE_NAME_FORMAT, buf1);
859 name = get_identifier (buf);
861 /* If this name doesn't clash, then we can use it,
862 otherwise we add something different to the name until
864 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
866 /* Hey, they really like MI don't they? Increase the 3
867 above to 6, and the 999 to 999999. :-) */
868 my_friendly_assert (j <= 999, 369);
873 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
874 new_buf2 = (char *) alloca (i);
875 sprintf (new_buf2, "%s%c%s",
876 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
880 new_decl = build_lang_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
881 /* Remember which class this vtable is really for. */
882 DECL_CONTEXT (new_decl) = for_type;
884 DECL_ARTIFICIAL (new_decl) = 1;
885 TREE_STATIC (new_decl) = 1;
886 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
887 DECL_VIRTUAL_P (new_decl) = 1;
888 #ifndef WRITABLE_VTABLES
889 /* Make them READONLY by default. (mrs) */
890 TREE_READONLY (new_decl) = 1;
892 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
894 /* Make fresh virtual list, so we can smash it later. */
895 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
897 if (TREE_VIA_VIRTUAL (binfo))
899 tree binfo1 = binfo_member (BINFO_TYPE (binfo),
900 CLASSTYPE_VBASECLASSES (for_type));
902 /* XXX - This should never happen, if it does, the caller should
903 ensure that the binfo is from for_type's binfos, not from any
904 base type's. We can remove all this code after a while. */
906 warning ("internal inconsistency: binfo offset error for rtti");
908 offset = BINFO_OFFSET (binfo1);
911 offset = BINFO_OFFSET (binfo);
913 set_rtti_entry (BINFO_VIRTUALS (binfo),
914 ssize_binop (MINUS_EXPR, integer_zero_node, offset),
917 #ifdef GATHER_STATISTICS
919 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
922 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
923 import_export_vtable (new_decl, for_type, 0);
925 if (TREE_VIA_VIRTUAL (binfo))
926 my_friendly_assert (binfo == binfo_member (BINFO_TYPE (binfo),
927 CLASSTYPE_VBASECLASSES (current_class_type)),
929 SET_BINFO_NEW_VTABLE_MARKED (binfo);
932 /* Return a new vtable for use in initialization of the BASE subobject
933 of COMPLETE_TYPE. The vtable there goes into the vfield of the
934 VBASEBASE virtual subobject. */
937 prepare_ctor_vtable (complete_type, base, vbasebase)
938 tree complete_type, base, vbasebase;
940 tree orig_decl = BINFO_VTABLE (vbasebase);
941 tree name = get_vlist_vtable_id (base, vbasebase);
944 new_decl = build_lang_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
945 /* Remember which class this vtable is really for. */
946 DECL_CONTEXT (new_decl) = complete_type;
948 DECL_ARTIFICIAL (new_decl) = 1;
949 TREE_STATIC (new_decl) = 1;
950 new_decl = pushdecl_top_level (new_decl);
951 DECL_VIRTUAL_P (new_decl) = 1;
952 #ifndef WRITABLE_VTABLES
953 /* Make them READONLY by default. (mrs) */
954 TREE_READONLY (new_decl) = 1;
956 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
958 #ifdef GATHER_STATISTICS
960 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
963 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
964 import_export_vtable (new_decl, complete_type, 0);
970 /* Access the virtual function table entry that logically
971 contains BASE_FNDECL. VIRTUALS is the virtual function table's
972 initializer. We can run off the end, when dealing with virtual
973 destructors in MI situations, return NULL_TREE in that case. */
976 get_vtable_entry (virtuals, base_fndecl)
977 tree virtuals, base_fndecl;
979 unsigned HOST_WIDE_INT n = (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
980 ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
981 & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1))
982 : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
984 #ifdef GATHER_STATISTICS
985 n_vtable_searches += n;
988 while (n > 0 && virtuals)
991 virtuals = TREE_CHAIN (virtuals);
997 /* Put new entry ENTRY into virtual function table initializer
1000 Also update DECL_VINDEX (FNDECL). */
1003 modify_vtable_entry (old_entry_in_list, new_entry, fndecl)
1004 tree old_entry_in_list, new_entry, fndecl;
1006 tree base_fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list)), 0);
1009 cp_warning ("replaced %D with %D", DECL_ASSEMBLER_NAME (base_fndecl),
1010 DECL_ASSEMBLER_NAME (fndecl));
1012 TREE_VALUE (old_entry_in_list) = new_entry;
1014 /* Now assign virtual dispatch information, if unset. */
1015 /* We can dispatch this, through any overridden base function. */
1016 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1018 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
1019 DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
1023 /* Access the virtual function table entry N. VIRTUALS is the virtual
1024 function table's initializer. */
1027 get_vtable_entry_n (virtuals, n)
1029 unsigned HOST_WIDE_INT n;
1034 virtuals = TREE_CHAIN (virtuals);
1039 /* Add a virtual function to all the appropriate vtables for the class
1040 T. DECL_VINDEX(X) should be error_mark_node, if we want to
1041 allocate a new slot in our table. If it is error_mark_node, we
1042 know that no other function from another vtable is overridden by X.
1043 HAS_VIRTUAL keeps track of how many virtuals there are in our main
1044 vtable for the type, and we build upon the PENDING_VIRTUALS list
1048 add_virtual_function (pv, phv, has_virtual, fndecl, t)
1052 tree t; /* Structure type. */
1054 tree pending_virtuals = *pv;
1055 tree pending_hard_virtuals = *phv;
1057 /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
1058 convert to void *. Make such a conversion here. */
1059 tree vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
1060 TREE_CONSTANT (vfn) = 1;
1063 if (current_class_type == 0)
1064 cp_warning ("internal problem, current_class_type is zero when adding `%D', please report",
1066 if (current_class_type && t != current_class_type)
1067 cp_warning ("internal problem, current_class_type differs when adding `%D', please report",
1071 /* If the virtual function is a redefinition of a prior one,
1072 figure out in which base class the new definition goes,
1073 and if necessary, make a fresh virtual function table
1074 to hold that entry. */
1075 if (DECL_VINDEX (fndecl) == error_mark_node)
1079 /* We remember that this was the base sub-object for rtti. */
1080 CLASSTYPE_RTTI (t) = t;
1082 /* If we are using thunks, use two slots at the front, one
1083 for the offset pointer, one for the tdesc pointer.
1084 For ARM-style vtables, use the same slot for both. */
1085 if (*has_virtual == 0 && ! CLASSTYPE_COM_INTERFACE (t))
1087 if (flag_vtable_thunks)
1093 /* Build a new INT_CST for this DECL_VINDEX. */
1095 static tree index_table[256];
1097 /* We skip a slot for the offset/tdesc entry. */
1098 int i = (*has_virtual)++;
1100 if (i >= 256 || index_table[i] == 0)
1102 idx = build_int_2 (i, 0);
1104 index_table[i] = idx;
1107 idx = index_table[i];
1109 /* Now assign virtual dispatch information. */
1110 DECL_VINDEX (fndecl) = idx;
1111 DECL_CONTEXT (fndecl) = t;
1113 entry = build_vtable_entry (integer_zero_node, vfn);
1114 pending_virtuals = tree_cons (DECL_VINDEX (fndecl), entry, pending_virtuals);
1116 /* Might already be INTEGER_CST if declared twice in class. We will
1117 give error later or we've already given it. */
1118 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1120 /* Need an entry in some other virtual function table.
1121 Deal with this after we have laid out our virtual base classes. */
1122 pending_hard_virtuals = temp_tree_cons (fndecl, vfn, pending_hard_virtuals);
1124 *pv = pending_virtuals;
1125 *phv = pending_hard_virtuals;
1128 /* Obstack on which to build the vector of class methods. */
1129 struct obstack class_obstack;
1130 extern struct obstack *current_obstack;
1132 /* These are method vectors that were too small for the number of
1133 methods in some class, and so were abandoned. */
1134 static tree free_method_vecs;
1136 /* Returns a method vector with enough room for N methods. N should
1137 be a power of two. */
1146 for (t = &free_method_vecs; *t; t = &(TREE_CHAIN (*t)))
1147 /* Note that we don't use >= n here because we don't want to
1148 allocate a very large vector where it isn't needed. */
1149 if (TREE_VEC_LENGTH (*t) == n)
1152 *t = TREE_CHAIN (new_vec);
1153 TREE_CHAIN (new_vec) = NULL_TREE;
1154 bzero ((PTR) &TREE_VEC_ELT (new_vec, 0), n * sizeof (tree));
1158 new_vec = make_tree_vec (n);
1162 /* Free the method vector VEC. */
1165 free_method_vec (vec)
1168 TREE_CHAIN (vec) = free_method_vecs;
1169 free_method_vecs = vec;
1172 /* Add method METHOD to class TYPE.
1174 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1175 the class type where the method should be added. */
1178 add_method (type, fields, method)
1179 tree type, *fields, method;
1181 push_obstacks_nochange ();
1182 end_temporary_allocation ();
1184 /* Setting the DECL_CONTEXT and DECL_CLASS_CONTEXT here is probably
1186 DECL_CONTEXT (method) = type;
1187 DECL_CLASS_CONTEXT (method) = type;
1189 if (fields && *fields)
1190 *fields = build_overload (method, *fields);
1197 if (!CLASSTYPE_METHOD_VEC (type))
1198 /* Make a new method vector. We start with 8 entries. We must
1199 allocate at least two (for constructors and destructors), and
1200 we're going to end up with an assignment operator at some
1203 We could use a TREE_LIST for now, and convert it to a
1204 TREE_VEC in finish_struct, but we would probably waste more
1205 memory making the links in the list than we would by
1206 over-allocating the size of the vector here. Furthermore,
1207 we would complicate all the code that expects this to be a
1208 vector. We keep a free list of vectors that we outgrew so
1209 that we don't really waste any memory. */
1210 CLASSTYPE_METHOD_VEC (type) = make_method_vec (8);
1212 method_vec = CLASSTYPE_METHOD_VEC (type);
1213 len = TREE_VEC_LENGTH (method_vec);
1215 if (DECL_NAME (method) == constructor_name (type))
1216 /* A new constructor or destructor. Constructors go in
1217 slot 0; destructors go in slot 1. */
1218 slot = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
1221 /* See if we already have an entry with this name. */
1222 for (slot = 2; slot < len; ++slot)
1223 if (!TREE_VEC_ELT (method_vec, slot)
1224 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1226 == DECL_NAME (method)))
1231 /* We need a bigger method vector. */
1232 tree new_vec = make_method_vec (2 * len);
1233 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1234 (PTR) &TREE_VEC_ELT (new_vec, 0),
1235 len * sizeof (tree));
1236 free_method_vec (method_vec);
1238 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1241 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1243 /* Type conversion operators have to come before
1244 ordinary methods; add_conversions depends on this to
1245 speed up looking for conversion operators. So, if
1246 necessary, we slide some of the vector elements up.
1247 In theory, this makes this algorithm O(N^2) but we
1248 don't expect many conversion operators. */
1249 for (slot = 2; slot < len; ++slot)
1251 tree fn = TREE_VEC_ELT (method_vec, slot);
1254 /* There are no more entries in the vector, so we
1255 can insert the new conversion operator here. */
1258 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1259 /* We can insert the new function right at the
1264 if (!TREE_VEC_ELT (method_vec, slot))
1265 /* There is nothing in the Ith slot, so we can avoid
1270 /* We know the last slot in the vector is empty
1271 because we know that at this point there's room
1272 for a new function. */
1273 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1274 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1275 (len - slot - 1) * sizeof (tree));
1276 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1281 if (template_class_depth (type))
1282 /* TYPE is a template class. Don't issue any errors now; wait
1283 until instantiation time to complain. */
1289 /* Check to see if we've already got this method. */
1290 for (fns = TREE_VEC_ELT (method_vec, slot);
1292 fns = OVL_NEXT (fns))
1294 tree fn = OVL_CURRENT (fns);
1296 if (TREE_CODE (fn) != TREE_CODE (method))
1299 if (TREE_CODE (method) != TEMPLATE_DECL)
1301 /* [over.load] Member function declarations with the
1302 same name and the same parameter types cannot be
1303 overloaded if any of them is a static member
1304 function declaration. */
1305 if (DECL_STATIC_FUNCTION_P (fn)
1306 != DECL_STATIC_FUNCTION_P (method))
1308 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1309 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1311 if (! DECL_STATIC_FUNCTION_P (fn))
1312 parms1 = TREE_CHAIN (parms1);
1314 parms2 = TREE_CHAIN (parms2);
1316 if (compparms (parms1, parms2))
1317 cp_error ("`%#D' and `%#D' cannot be overloaded",
1321 /* Since this is an ordinary function in a
1322 non-template class, it's mangled name can be used
1323 as a unique identifier. This technique is only
1324 an optimization; we would get the same results if
1325 we just used decls_match here. */
1326 if (DECL_ASSEMBLER_NAME (fn)
1327 != DECL_ASSEMBLER_NAME (method))
1330 else if (!decls_match (fn, method))
1333 /* There has already been a declaration of this method
1334 or member template. */
1335 cp_error_at ("`%D' has already been declared in `%T'",
1338 /* We don't call duplicate_decls here to merge the
1339 declarations because that will confuse things if the
1340 methods have inline definitions. In particular, we
1341 will crash while processing the definitions. */
1346 /* Actually insert the new method. */
1347 TREE_VEC_ELT (method_vec, slot)
1348 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1350 /* Add the new binding. */
1351 if (!DECL_CONSTRUCTOR_P (method)
1352 && !DECL_DESTRUCTOR_P (method))
1353 push_class_level_binding (DECL_NAME (method),
1354 TREE_VEC_ELT (method_vec, slot));
1359 /* Subroutines of finish_struct. */
1361 /* Look through the list of fields for this struct, deleting
1362 duplicates as we go. This must be recursive to handle
1365 FIELD is the field which may not appear anywhere in FIELDS.
1366 FIELD_PTR, if non-null, is the starting point at which
1367 chained deletions may take place.
1368 The value returned is the first acceptable entry found
1371 Note that anonymous fields which are not of UNION_TYPE are
1372 not duplicates, they are just anonymous fields. This happens
1373 when we have unnamed bitfields, for example. */
1376 delete_duplicate_fields_1 (field, fields)
1381 if (DECL_NAME (field) == 0)
1383 if (TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)
1386 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1387 fields = delete_duplicate_fields_1 (x, fields);
1392 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1394 if (DECL_NAME (x) == 0)
1396 if (TREE_CODE (TREE_TYPE (x)) != UNION_TYPE)
1398 TYPE_FIELDS (TREE_TYPE (x))
1399 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1400 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1403 fields = TREE_CHAIN (fields);
1405 TREE_CHAIN (prev) = TREE_CHAIN (x);
1410 if (DECL_NAME (field) == DECL_NAME (x))
1412 if (TREE_CODE (field) == CONST_DECL
1413 && TREE_CODE (x) == CONST_DECL)
1414 cp_error_at ("duplicate enum value `%D'", x);
1415 else if (TREE_CODE (field) == CONST_DECL
1416 || TREE_CODE (x) == CONST_DECL)
1417 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1419 else if (DECL_DECLARES_TYPE_P (field)
1420 && DECL_DECLARES_TYPE_P (x))
1422 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1424 cp_error_at ("duplicate nested type `%D'", x);
1426 else if (DECL_DECLARES_TYPE_P (field)
1427 || DECL_DECLARES_TYPE_P (x))
1429 /* Hide tag decls. */
1430 if ((TREE_CODE (field) == TYPE_DECL
1431 && DECL_ARTIFICIAL (field))
1432 || (TREE_CODE (x) == TYPE_DECL
1433 && DECL_ARTIFICIAL (x)))
1435 cp_error_at ("duplicate field `%D' (as type and non-type)",
1439 cp_error_at ("duplicate member `%D'", x);
1441 fields = TREE_CHAIN (fields);
1443 TREE_CHAIN (prev) = TREE_CHAIN (x);
1452 delete_duplicate_fields (fields)
1456 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1457 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1460 /* Change the access of FDECL to ACCESS in T. The access to FDECL is
1461 along the path given by BINFO. Return 1 if change was legit,
1462 otherwise return 0. */
1465 alter_access (t, binfo, fdecl, access)
1471 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1474 if (TREE_VALUE (elem) != access)
1476 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1477 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1479 error ("conflicting access specifications for field `%s', ignored",
1480 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1484 /* They're changing the access to the same thing they changed
1485 it to before. That's OK. */
1491 enforce_access (binfo, fdecl);
1492 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1498 /* Process the USING_DECL, which is a member of T. The METHOD_VEC, if
1499 non-NULL, is the methods of T. The FIELDS are the fields of T. */
1502 handle_using_decl (using_decl, t, method_vec, fields)
1508 tree ctype = DECL_INITIAL (using_decl);
1509 tree name = DECL_NAME (using_decl);
1511 = TREE_PRIVATE (using_decl) ? access_private_node
1512 : TREE_PROTECTED (using_decl) ? access_protected_node
1513 : access_public_node;
1515 tree flist = NULL_TREE;
1520 binfo = binfo_or_else (ctype, t);
1524 if (name == constructor_name (ctype)
1525 || name == constructor_name_full (ctype))
1527 cp_error_at ("using-declaration for constructor", using_decl);
1531 fdecl = lookup_member (binfo, name, 0, 0);
1535 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1539 /* Functions are represented as TREE_LIST, with the purpose
1540 being the type and the value the functions. Other members
1541 come as themselves. */
1542 if (TREE_CODE (fdecl) == TREE_LIST)
1543 /* Ignore base type this came from. */
1544 fdecl = TREE_VALUE (fdecl);
1546 if (TREE_CODE (fdecl) == OVERLOAD)
1548 /* We later iterate over all functions. */
1550 fdecl = OVL_FUNCTION (flist);
1553 name = DECL_NAME (fdecl);
1554 n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
1555 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); i++)
1556 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)))
1559 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1560 cp_error_at (" because of local method `%#D' with same name",
1561 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
1565 if (! DECL_LANG_SPECIFIC (fdecl))
1566 /* We don't currently handle DECL_ACCESS for TYPE_DECLs; just return. */
1569 for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
1570 if (DECL_NAME (tmp) == name)
1572 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1573 cp_error_at (" because of local field `%#D' with same name", tmp);
1577 /* Make type T see field decl FDECL with access ACCESS.*/
1582 if (alter_access (t, binfo, OVL_FUNCTION (flist),
1585 flist = OVL_CHAIN (flist);
1589 alter_access (t, binfo, fdecl, access);
1595 int max_has_virtual;
1599 char cant_have_default_ctor;
1600 char cant_have_const_ctor;
1601 char no_const_asn_ref;
1604 /* Record information about type T derived from its base classes.
1605 Store most of that information in T itself, and place the
1606 remaining information in the struct BASE_INFO.
1608 Propagate basetype offsets throughout the lattice. Note that the
1609 lattice topped by T is really a pair: it's a DAG that gives the
1610 structure of the derivation hierarchy, and it's a list of the
1611 virtual baseclasses that appear anywhere in the DAG. When a vbase
1612 type appears in the DAG, it's offset is 0, and it's children start
1613 their offsets from that point. When a vbase type appears in the list,
1614 its offset is the offset it has in the hierarchy, and its children's
1615 offsets include that offset in theirs.
1617 Returns the index of the first base class to have virtual functions,
1618 or -1 if no such base class. */
1621 finish_base_struct (t, b)
1623 struct base_info *b;
1625 tree binfos = TYPE_BINFO_BASETYPES (t);
1626 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1627 int first_vfn_base_index = -1;
1628 bzero ((char *) b, sizeof (struct base_info));
1630 for (i = 0; i < n_baseclasses; i++)
1632 tree base_binfo = TREE_VEC_ELT (binfos, i);
1633 tree basetype = BINFO_TYPE (base_binfo);
1635 /* Effective C++ rule 14. We only need to check TYPE_VIRTUAL_P
1636 here because the case of virtual functions but non-virtual
1637 dtor is handled in finish_struct_1. */
1638 if (warn_ecpp && ! TYPE_VIRTUAL_P (basetype)
1639 && TYPE_HAS_DESTRUCTOR (basetype))
1640 cp_warning ("base class `%#T' has a non-virtual destructor", basetype);
1642 /* If the type of basetype is incomplete, then
1643 we already complained about that fact
1644 (and we should have fixed it up as well). */
1645 if (TYPE_SIZE (basetype) == 0)
1648 /* The base type is of incomplete type. It is
1649 probably best to pretend that it does not
1651 if (i == n_baseclasses-1)
1652 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1653 TREE_VEC_LENGTH (binfos) -= 1;
1655 for (j = i; j+1 < n_baseclasses; j++)
1656 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1659 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1660 b->cant_have_const_ctor = 1;
1662 if (TYPE_HAS_CONSTRUCTOR (basetype)
1663 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1665 b->cant_have_default_ctor = 1;
1666 if (! TYPE_HAS_CONSTRUCTOR (t))
1668 cp_pedwarn ("base `%T' with only non-default constructor",
1670 cp_pedwarn ("in class without a constructor");
1674 if (TYPE_HAS_ASSIGN_REF (basetype)
1675 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1676 b->no_const_asn_ref = 1;
1678 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1679 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1680 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1681 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1683 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1684 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1685 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1687 if (CLASSTYPE_COM_INTERFACE (basetype))
1689 CLASSTYPE_COM_INTERFACE (t) = 1;
1692 ("COM interface type `%T' must be the leftmost base class",
1695 else if (CLASSTYPE_COM_INTERFACE (t))
1697 cp_error ("COM interface type `%T' with non-COM base class `%T'",
1699 CLASSTYPE_COM_INTERFACE (t) = 0;
1702 if (TYPE_VIRTUAL_P (basetype))
1704 /* Ensure that this is set from at least a virtual base
1706 if (b->rtti == NULL_TREE)
1707 b->rtti = CLASSTYPE_RTTI (basetype);
1709 /* Don't borrow virtuals from virtual baseclasses. */
1710 if (TREE_VIA_VIRTUAL (base_binfo))
1713 if (first_vfn_base_index < 0)
1716 first_vfn_base_index = i;
1718 /* Update these two, now that we know what vtable we are
1719 going to extend. This is so that we can add virtual
1720 functions, and override them properly. */
1721 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1722 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1723 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1724 b->vfield = CLASSTYPE_VFIELD (basetype);
1725 b->vfields = copy_list (CLASSTYPE_VFIELDS (basetype));
1726 vfields = b->vfields;
1729 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1730 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1732 tree value = VF_BASETYPE_VALUE (vfields);
1733 if (DECL_NAME (CLASSTYPE_VFIELD (value))
1734 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1735 VF_NORMAL_VALUE (b->vfields) = basetype;
1737 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1739 vfields = TREE_CHAIN (vfields);
1741 CLASSTYPE_VFIELD (t) = b->vfield;
1745 /* Only add unique vfields, and flatten them out as we go. */
1746 tree vfields = CLASSTYPE_VFIELDS (basetype);
1749 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1750 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1752 tree value = VF_BASETYPE_VALUE (vfields);
1753 b->vfields = tree_cons (base_binfo, value, b->vfields);
1754 if (DECL_NAME (CLASSTYPE_VFIELD (value))
1755 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1756 VF_NORMAL_VALUE (b->vfields) = basetype;
1758 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1760 vfields = TREE_CHAIN (vfields);
1763 if (b->has_virtual == 0)
1765 first_vfn_base_index = i;
1767 /* Update these two, now that we know what vtable we are
1768 going to extend. This is so that we can add virtual
1769 functions, and override them properly. */
1770 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1771 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1772 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1773 b->vfield = CLASSTYPE_VFIELD (basetype);
1774 CLASSTYPE_VFIELD (t) = b->vfield;
1775 /* When we install the first one, set the VF_NORMAL_VALUE
1776 to be the current class, as this it is the most derived
1777 class. Hopefully, this is not set to something else
1779 vfields = b->vfields;
1782 if (DECL_NAME (CLASSTYPE_VFIELD (t))
1783 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1785 VF_NORMAL_VALUE (vfields) = t;
1786 /* There should only be one of them! And it should
1787 always be found, if we get into here. (mrs) */
1790 vfields = TREE_CHAIN (vfields);
1799 /* Find the base class with the largest number of virtual functions. */
1800 for (vfields = b->vfields; vfields; vfields = TREE_CHAIN (vfields))
1802 if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)) > b->max_has_virtual)
1803 b->max_has_virtual = CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields));
1804 if (VF_DERIVED_VALUE (vfields)
1805 && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)) > b->max_has_virtual)
1806 b->max_has_virtual = CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields));
1811 /* If all virtual functions come only from virtual baseclasses. */
1814 /* Update the rtti base if we have a non-virtual base class version
1816 b->rtti = CLASSTYPE_RTTI (BINFO_TYPE (TREE_VEC_ELT (binfos, first_vfn_base_index)));
1818 return first_vfn_base_index;
1821 /* Set memoizing fields and bits of T (and its variants) for later use.
1822 MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables. */
1825 finish_struct_bits (t, max_has_virtual)
1827 int max_has_virtual;
1829 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1831 /* Fix up variants (if any). */
1832 tree variants = TYPE_NEXT_VARIANT (t);
1835 /* These fields are in the _TYPE part of the node, not in
1836 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1837 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1838 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1839 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1840 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
1842 TYPE_USES_COMPLEX_INHERITANCE (variants) = TYPE_USES_COMPLEX_INHERITANCE (t);
1843 TYPE_VIRTUAL_P (variants) = TYPE_VIRTUAL_P (t);
1844 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1845 TYPE_USES_PVBASES (variants) = TYPE_USES_PVBASES (t);
1846 /* Copy whatever these are holding today. */
1847 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1848 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1849 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1850 TYPE_SIZE (variants) = TYPE_SIZE (t);
1851 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1852 variants = TYPE_NEXT_VARIANT (variants);
1855 if (n_baseclasses && max_has_virtual)
1857 /* For a class w/o baseclasses, `finish_struct' has set
1858 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by definition). Similarly
1859 for a class who's base classes do not have vtables. When neither
1860 of these is true, we might have removed abstract virtuals (by
1861 providing a definition), added some (by declaring new ones), or
1862 redeclared ones from a base class. We need to recalculate what's
1863 really an abstract virtual at this point (by looking in the
1865 CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
1870 /* Notice whether this class has type conversion functions defined. */
1871 tree binfo = TYPE_BINFO (t);
1872 tree binfos = BINFO_BASETYPES (binfo);
1875 for (i = n_baseclasses-1; i >= 0; i--)
1877 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1879 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1883 /* If this type has a copy constructor, force its mode to be BLKmode, and
1884 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1885 be passed by invisible reference and prevent it from being returned in
1888 Also do this if the class has BLKmode but can still be returned in
1889 registers, since function_cannot_inline_p won't let us inline
1890 functions returning such a type. This affects the HP-PA. */
1891 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1892 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1893 && CLASSTYPE_NON_AGGREGATE (t)))
1896 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1897 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1899 TYPE_MODE (variants) = BLKmode;
1900 TREE_ADDRESSABLE (variants) = 1;
1905 /* Issue warnings about T having private constructors, but no friends,
1908 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1909 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1910 non-private static member functions. */
1913 maybe_warn_about_overly_private_class (t)
1916 int has_member_fn = 0;
1917 int has_nonprivate_method = 0;
1920 if (!warn_ctor_dtor_privacy
1921 /* If the class has friends, those entities might create and
1922 access instances, so we should not warn. */
1923 || (CLASSTYPE_FRIEND_CLASSES (t)
1924 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1925 /* We will have warned when the template was declared; there's
1926 no need to warn on every instantiation. */
1927 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1928 /* There's no reason to even consider warning about this
1932 /* We only issue one warning, if more than one applies, because
1933 otherwise, on code like:
1936 // Oops - forgot `public:'
1942 we warn several times about essentially the same problem. */
1944 /* Check to see if all (non-constructor, non-destructor) member
1945 functions are private. (Since there are no friends or
1946 non-private statics, we can't ever call any of the private member
1948 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1949 /* We're not interested in compiler-generated methods; they don't
1950 provide any way to call private members. */
1951 if (!DECL_ARTIFICIAL (fn))
1953 if (!TREE_PRIVATE (fn))
1955 if (DECL_STATIC_FUNCTION_P (fn))
1956 /* A non-private static member function is just like a
1957 friend; it can create and invoke private member
1958 functions, and be accessed without a class
1962 has_nonprivate_method = 1;
1965 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1969 if (!has_nonprivate_method && has_member_fn)
1971 /* There are no non-private methods, and there's at least one
1972 private member function that isn't a constructor or
1973 destructor. (If all the private members are
1974 constructors/destructors we want to use the code below that
1975 issues error messages specifically referring to
1976 constructors/destructors.) */
1978 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1979 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1980 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1981 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1983 has_nonprivate_method = 1;
1986 if (!has_nonprivate_method)
1988 cp_warning ("all member functions in class `%T' are private", t);
1993 /* Even if some of the member functions are non-private, the class
1994 won't be useful for much if all the constructors or destructors
1995 are private: such an object can never be created or destroyed. */
1996 if (TYPE_HAS_DESTRUCTOR (t))
1998 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2000 if (TREE_PRIVATE (dtor))
2002 cp_warning ("`%#T' only defines a private destructor and has no friends",
2008 if (TYPE_HAS_CONSTRUCTOR (t))
2010 int nonprivate_ctor = 0;
2012 /* If a non-template class does not define a copy
2013 constructor, one is defined for it, enabling it to avoid
2014 this warning. For a template class, this does not
2015 happen, and so we would normally get a warning on:
2017 template <class T> class C { private: C(); };
2019 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2020 complete non-template or fully instantiated classes have this
2022 if (!TYPE_HAS_INIT_REF (t))
2023 nonprivate_ctor = 1;
2025 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2029 tree ctor = OVL_CURRENT (fn);
2030 /* Ideally, we wouldn't count copy constructors (or, in
2031 fact, any constructor that takes an argument of the
2032 class type as a parameter) because such things cannot
2033 be used to construct an instance of the class unless
2034 you already have one. But, for now at least, we're
2036 if (! TREE_PRIVATE (ctor))
2038 nonprivate_ctor = 1;
2043 if (nonprivate_ctor == 0)
2045 cp_warning ("`%#T' only defines private constructors and has no friends",
2053 /* Warn about duplicate methods in fn_fields. Also compact method
2054 lists so that lookup can be made faster.
2056 Data Structure: List of method lists. The outer list is a
2057 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2058 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2059 links the entire list of methods for TYPE_METHODS. Friends are
2060 chained in the same way as member functions (? TREE_CHAIN or
2061 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2062 list. That allows them to be quickly deleted, and requires no
2065 If there are any constructors/destructors, they are moved to the
2066 front of the list. This makes pushclass more efficient.
2068 We also link each field which has shares a name with its baseclass
2069 to the head of the list of fields for that base class. This allows
2070 us to reduce search time in places like `build_method_call' to
2071 consider only reasonably likely functions. */
2074 finish_struct_methods (t)
2078 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2079 tree ctor_name = constructor_name (t);
2081 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2082 and the next few with type conversion operators (if any). */
2083 for (fn_fields = TYPE_METHODS (t); fn_fields;
2084 fn_fields = TREE_CHAIN (fn_fields))
2086 tree fn_name = DECL_NAME (fn_fields);
2088 /* Clear out this flag.
2090 @@ Doug may figure out how to break
2091 @@ this with nested classes and friends. */
2092 DECL_IN_AGGR_P (fn_fields) = 0;
2094 /* Note here that a copy ctor is private, so we don't dare generate
2095 a default copy constructor for a class that has a member
2096 of this type without making sure they have access to it. */
2097 if (fn_name == ctor_name)
2099 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2100 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2102 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2103 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2105 if (TREE_CHAIN (parmtypes) == NULL_TREE
2106 || TREE_CHAIN (parmtypes) == void_list_node
2107 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2109 if (TREE_PROTECTED (fn_fields))
2110 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2111 else if (TREE_PRIVATE (fn_fields))
2112 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2116 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2118 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2120 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2122 if (TREE_PROTECTED (fn_fields))
2123 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2124 else if (TREE_PRIVATE (fn_fields))
2125 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2130 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2131 /* We thought there was a destructor, but there wasn't. Some
2132 parse errors cause this anomalous situation. */
2133 TYPE_HAS_DESTRUCTOR (t) = 0;
2135 /* Issue warnings about private constructors and such. If there are
2136 no methods, then some public defaults are generated. */
2137 maybe_warn_about_overly_private_class (t);
2140 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2143 duplicate_tag_error (t)
2146 cp_error ("redefinition of `%#T'", t);
2147 cp_error_at ("previous definition here", t);
2149 /* Pretend we haven't defined this type. */
2151 /* All of the component_decl's were TREE_CHAINed together in the parser.
2152 finish_struct_methods walks these chains and assembles all methods with
2153 the same base name into DECL_CHAINs. Now we don't need the parser chains
2154 anymore, so we unravel them. */
2156 /* This used to be in finish_struct, but it turns out that the
2157 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2159 if (CLASSTYPE_METHOD_VEC (t))
2161 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2162 int i, len = TREE_VEC_LENGTH (method_vec);
2163 for (i = 0; i < len; i++)
2165 tree unchain = TREE_VEC_ELT (method_vec, i);
2166 while (unchain != NULL_TREE)
2168 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2169 unchain = OVL_NEXT (unchain);
2174 if (TYPE_LANG_SPECIFIC (t))
2176 tree binfo = TYPE_BINFO (t);
2177 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2178 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2180 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2181 BINFO_BASETYPES(binfo) = NULL_TREE;
2183 TYPE_BINFO (t) = binfo;
2184 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2185 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2186 TYPE_REDEFINED (t) = 1;
2188 TYPE_SIZE (t) = NULL_TREE;
2189 TYPE_MODE (t) = VOIDmode;
2190 TYPE_FIELDS (t) = NULL_TREE;
2191 TYPE_METHODS (t) = NULL_TREE;
2192 TYPE_VFIELD (t) = NULL_TREE;
2193 TYPE_CONTEXT (t) = NULL_TREE;
2196 /* finish up all new vtables. */
2199 finish_vtbls (binfo, do_self, t)
2204 tree binfos = BINFO_BASETYPES (binfo);
2205 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2207 /* Should we use something besides CLASSTYPE_VFIELDS? */
2208 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2210 if (BINFO_NEW_VTABLE_MARKED (binfo))
2214 decl = BINFO_VTABLE (binfo);
2215 context = DECL_CONTEXT (decl);
2216 DECL_CONTEXT (decl) = 0;
2217 if (DECL_INITIAL (decl) != BINFO_VIRTUALS (binfo))
2218 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE,
2219 BINFO_VIRTUALS (binfo));
2220 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0);
2221 DECL_CONTEXT (decl) = context;
2223 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2226 for (i = 0; i < n_baselinks; i++)
2228 tree base_binfo = TREE_VEC_ELT (binfos, i);
2229 int is_not_base_vtable
2230 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2231 if (TREE_VIA_VIRTUAL (base_binfo))
2233 base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2235 finish_vtbls (base_binfo, is_not_base_vtable, t);
2239 /* True if we should override the given BASE_FNDECL with the given
2243 overrides (fndecl, base_fndecl)
2244 tree fndecl, base_fndecl;
2246 /* Destructors have special names. */
2247 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2248 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2250 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2251 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2253 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2255 tree types, base_types;
2257 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2258 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2260 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2261 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2262 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2263 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2264 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2271 get_class_offset_1 (parent, binfo, context, t, fndecl)
2272 tree parent, binfo, context, t, fndecl;
2274 tree binfos = BINFO_BASETYPES (binfo);
2275 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2276 tree rval = NULL_TREE;
2278 if (binfo == parent)
2279 return error_mark_node;
2281 for (i = 0; i < n_baselinks; i++)
2283 tree base_binfo = TREE_VEC_ELT (binfos, i);
2286 if (TREE_VIA_VIRTUAL (base_binfo))
2287 base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2288 CLASSTYPE_VBASECLASSES (t));
2289 nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl);
2290 /* See if we have a new value */
2291 if (nrval && (nrval != error_mark_node || rval==0))
2293 /* Only compare if we have two offsets */
2294 if (rval && rval != error_mark_node
2295 && ! tree_int_cst_equal (nrval, rval))
2297 /* Only give error if the two offsets are different */
2298 error ("every virtual function must have a unique final overrider");
2299 cp_error (" found two (or more) `%T' class subobjects in `%T'", context, t);
2300 cp_error (" with virtual `%D' from virtual base class", fndecl);
2306 if (rval && BINFO_TYPE (binfo) == context)
2308 my_friendly_assert (rval == error_mark_node
2309 || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999);
2310 rval = BINFO_OFFSET (binfo);
2316 /* Get the offset to the CONTEXT subobject that is related to the
2320 get_class_offset (context, t, binfo, fndecl)
2321 tree context, t, binfo, fndecl;
2323 tree first_binfo = binfo;
2328 return integer_zero_node;
2330 if (BINFO_TYPE (binfo) == context)
2331 return BINFO_OFFSET (binfo);
2333 /* Check less derived binfos first. */
2334 while (BINFO_BASETYPES (binfo)
2335 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
2337 tree binfos = BINFO_BASETYPES (binfo);
2338 binfo = TREE_VEC_ELT (binfos, i);
2339 if (BINFO_TYPE (binfo) == context)
2340 return BINFO_OFFSET (binfo);
2343 /* Ok, not found in the less derived binfos, now check the more
2345 offset = get_class_offset_1 (first_binfo, TYPE_BINFO (t), context, t, fndecl);
2346 if (offset==0 || TREE_CODE (offset) != INTEGER_CST)
2347 my_friendly_abort (999); /* we have to find it. */
2351 /* Skip RTTI information at the front of the virtual list. */
2353 unsigned HOST_WIDE_INT
2354 skip_rtti_stuff (virtuals, t)
2359 if (CLASSTYPE_COM_INTERFACE (t))
2365 /* We always reserve a slot for the offset/tdesc entry. */
2367 *virtuals = TREE_CHAIN (*virtuals);
2369 if (flag_vtable_thunks && *virtuals)
2371 /* The second slot is reserved for the tdesc pointer when thunks
2374 *virtuals = TREE_CHAIN (*virtuals);
2380 modify_one_vtable (binfo, t, fndecl, pfn)
2381 tree binfo, t, fndecl, pfn;
2383 tree virtuals = BINFO_VIRTUALS (binfo);
2384 unsigned HOST_WIDE_INT n;
2386 /* update rtti entry */
2389 if (binfo == TYPE_BINFO (t))
2391 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2392 build_vtable (TYPE_BINFO (DECL_CONTEXT (CLASSTYPE_VFIELD (t))), t);
2396 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2397 prepare_fresh_vtable (binfo, t);
2400 if (fndecl == NULL_TREE)
2403 n = skip_rtti_stuff (&virtuals, t);
2407 tree current_fndecl = TREE_VALUE (virtuals);
2408 current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl);
2409 current_fndecl = TREE_OPERAND (current_fndecl, 0);
2410 if (current_fndecl && overrides (fndecl, current_fndecl))
2412 tree base_offset, offset;
2413 tree context = DECL_CLASS_CONTEXT (fndecl);
2414 tree vfield = CLASSTYPE_VFIELD (t);
2417 offset = get_class_offset (context, t, binfo, fndecl);
2419 /* Find the right offset for the this pointer based on the
2420 base class we just found. We have to take into
2421 consideration the virtual base class pointers that we
2422 stick in before the virtual function table pointer.
2424 Also, we want just the delta between the most base class
2425 that we derived this vfield from and us. */
2426 base_offset = size_binop (PLUS_EXPR,
2427 get_derived_offset (binfo, DECL_CONTEXT (current_fndecl)),
2428 BINFO_OFFSET (binfo));
2429 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2431 if (binfo == TYPE_BINFO (t))
2433 /* In this case, it is *type*'s vtable we are modifying.
2434 We start with the approximation that it's vtable is that
2435 of the immediate base class. */
2436 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2437 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2441 /* This is our very own copy of `basetype' to play with.
2442 Later, we will fill in all the virtual functions
2443 that override the virtual functions in these base classes
2444 which are not defined by the current type. */
2445 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2446 prepare_fresh_vtable (binfo, t);
2450 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
2452 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2453 build_vtable_entry (this_offset, pfn),
2457 virtuals = TREE_CHAIN (virtuals);
2461 /* These are the ones that are not through virtual base classes. */
2464 modify_all_direct_vtables (binfo, do_self, t, fndecl, pfn)
2467 tree t, fndecl, pfn;
2469 tree binfos = BINFO_BASETYPES (binfo);
2470 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2472 /* Should we use something besides CLASSTYPE_VFIELDS? */
2473 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2475 modify_one_vtable (binfo, t, fndecl, pfn);
2478 for (i = 0; i < n_baselinks; i++)
2480 tree base_binfo = TREE_VEC_ELT (binfos, i);
2481 int is_not_base_vtable
2482 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2483 if (! TREE_VIA_VIRTUAL (base_binfo))
2484 modify_all_direct_vtables (base_binfo, is_not_base_vtable, t, fndecl, pfn);
2488 /* Fixup all the delta entries in this one vtable that need updating. */
2491 fixup_vtable_deltas1 (binfo, t)
2494 tree virtuals = BINFO_VIRTUALS (binfo);
2495 unsigned HOST_WIDE_INT n;
2497 n = skip_rtti_stuff (&virtuals, t);
2501 tree fndecl = TREE_VALUE (virtuals);
2502 tree pfn = FNADDR_FROM_VTABLE_ENTRY (fndecl);
2503 tree delta = DELTA_FROM_VTABLE_ENTRY (fndecl);
2504 fndecl = TREE_OPERAND (pfn, 0);
2507 tree base_offset, offset;
2508 tree context = DECL_CLASS_CONTEXT (fndecl);
2509 tree vfield = CLASSTYPE_VFIELD (t);
2512 offset = get_class_offset (context, t, binfo, fndecl);
2514 /* Find the right offset for the this pointer based on the
2515 base class we just found. We have to take into
2516 consideration the virtual base class pointers that we
2517 stick in before the virtual function table pointer.
2519 Also, we want just the delta between the most base class
2520 that we derived this vfield from and us. */
2521 base_offset = size_binop (PLUS_EXPR,
2522 get_derived_offset (binfo,
2523 DECL_CONTEXT (fndecl)),
2524 BINFO_OFFSET (binfo));
2525 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2527 if (! tree_int_cst_equal (this_offset, delta))
2529 /* Make sure we can modify the derived association with immunity. */
2530 if (binfo == TYPE_BINFO (t))
2532 /* In this case, it is *type*'s vtable we are modifying.
2533 We start with the approximation that it's vtable is that
2534 of the immediate base class. */
2535 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2536 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2540 /* This is our very own copy of `basetype' to play with.
2541 Later, we will fill in all the virtual functions
2542 that override the virtual functions in these base classes
2543 which are not defined by the current type. */
2544 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2545 prepare_fresh_vtable (binfo, t);
2548 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2549 build_vtable_entry (this_offset, pfn),
2554 virtuals = TREE_CHAIN (virtuals);
2558 /* Fixup all the delta entries in all the direct vtables that need updating.
2559 This happens when we have non-overridden virtual functions from a
2560 virtual base class, that are at a different offset, in the new
2561 hierarchy, because the layout of the virtual bases has changed. */
2564 fixup_vtable_deltas (binfo, init_self, t)
2569 tree binfos = BINFO_BASETYPES (binfo);
2570 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2572 for (i = 0; i < n_baselinks; i++)
2574 tree base_binfo = TREE_VEC_ELT (binfos, i);
2575 int is_not_base_vtable
2576 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2577 if (! TREE_VIA_VIRTUAL (base_binfo))
2578 fixup_vtable_deltas (base_binfo, is_not_base_vtable, t);
2580 /* Should we use something besides CLASSTYPE_VFIELDS? */
2581 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2583 fixup_vtable_deltas1 (binfo, t);
2587 /* These are the ones that are through virtual base classes. */
2590 modify_all_indirect_vtables (binfo, do_self, via_virtual, t, fndecl, pfn)
2592 int do_self, via_virtual;
2593 tree t, fndecl, pfn;
2595 tree binfos = BINFO_BASETYPES (binfo);
2596 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2598 /* Should we use something besides CLASSTYPE_VFIELDS? */
2599 if (do_self && via_virtual && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2601 modify_one_vtable (binfo, t, fndecl, pfn);
2604 for (i = 0; i < n_baselinks; i++)
2606 tree base_binfo = TREE_VEC_ELT (binfos, i);
2607 int is_not_base_vtable
2608 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2609 if (TREE_VIA_VIRTUAL (base_binfo))
2612 base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2614 modify_all_indirect_vtables (base_binfo, is_not_base_vtable, via_virtual, t, fndecl, pfn);
2619 modify_all_vtables (t, fndecl, vfn)
2620 tree t, fndecl, vfn;
2622 /* Do these first, so that we will make use of any non-virtual class's
2623 vtable, over a virtual classes vtable. */
2624 modify_all_direct_vtables (TYPE_BINFO (t), 1, t, fndecl, vfn);
2625 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
2626 modify_all_indirect_vtables (TYPE_BINFO (t), 1, 0, t, fndecl, vfn);
2629 /* Here, we already know that they match in every respect.
2630 All we have to check is where they had their declarations. */
2633 strictly_overrides (fndecl1, fndecl2)
2634 tree fndecl1, fndecl2;
2636 int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
2637 DECL_CLASS_CONTEXT (fndecl1),
2639 if (distance == -2 || distance > 0)
2644 /* Merge overrides for one vtable.
2645 If we want to merge in same function, we are fine.
2647 if one has a DECL_CLASS_CONTEXT that is a parent of the
2648 other, than choose the more derived one
2650 potentially ill-formed (see 10.3 [class.virtual])
2651 we have to check later to see if there was an
2652 override in this class. If there was ok, if not
2653 then it is ill-formed. (mrs)
2655 We take special care to reuse a vtable, if we can. */
2658 override_one_vtable (binfo, old, t)
2661 tree virtuals = BINFO_VIRTUALS (binfo);
2662 tree old_virtuals = BINFO_VIRTUALS (old);
2663 enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
2665 /* If we have already committed to modifying it, then don't try and
2666 reuse another vtable. */
2667 if (BINFO_NEW_VTABLE_MARKED (binfo))
2670 skip_rtti_stuff (&virtuals, t);
2671 skip_rtti_stuff (&old_virtuals, t);
2675 tree fndecl = TREE_VALUE (virtuals);
2676 tree old_fndecl = TREE_VALUE (old_virtuals);
2677 fndecl = FNADDR_FROM_VTABLE_ENTRY (fndecl);
2678 old_fndecl = FNADDR_FROM_VTABLE_ENTRY (old_fndecl);
2679 fndecl = TREE_OPERAND (fndecl, 0);
2680 old_fndecl = TREE_OPERAND (old_fndecl, 0);
2681 /* First check to see if they are the same. */
2682 if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
2684 /* No need to do anything. */
2686 else if (strictly_overrides (fndecl, old_fndecl))
2688 if (choose == UNDECIDED)
2690 else if (choose == REUSE_OLD)
2693 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2695 prepare_fresh_vtable (binfo, t);
2696 override_one_vtable (binfo, old, t);
2701 else if (strictly_overrides (old_fndecl, fndecl))
2703 if (choose == UNDECIDED)
2705 else if (choose == REUSE_NEW)
2708 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2710 prepare_fresh_vtable (binfo, t);
2711 override_one_vtable (binfo, old, t);
2714 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2716 else if (choose == NEITHER)
2718 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2724 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2726 prepare_fresh_vtable (binfo, t);
2727 override_one_vtable (binfo, old, t);
2731 /* This MUST be overridden, or the class is ill-formed. */
2732 tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
2735 fndecl = copy_node (fndecl);
2736 copy_lang_decl (fndecl);
2737 DECL_NEEDS_FINAL_OVERRIDER_P (fndecl) = 1;
2738 /* Make sure we search for it later. */
2739 if (! CLASSTYPE_ABSTRACT_VIRTUALS (t))
2740 CLASSTYPE_ABSTRACT_VIRTUALS (t) = error_mark_node;
2742 vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
2743 TREE_CONSTANT (vfn) = 1;
2745 /* We can use integer_zero_node, as we will core dump
2746 if this is used anyway. */
2747 TREE_VALUE (virtuals) = build_vtable_entry (integer_zero_node, vfn);
2750 virtuals = TREE_CHAIN (virtuals);
2751 old_virtuals = TREE_CHAIN (old_virtuals);
2754 /* Let's reuse the old vtable. */
2755 if (choose == REUSE_OLD)
2757 BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
2758 BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
2762 /* Merge in overrides for virtual bases.
2763 BINFO is the hierarchy we want to modify, and OLD has the potential
2767 merge_overrides (binfo, old, do_self, t)
2772 tree binfos = BINFO_BASETYPES (binfo);
2773 tree old_binfos = BINFO_BASETYPES (old);
2774 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2776 /* Should we use something besides CLASSTYPE_VFIELDS? */
2777 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2779 override_one_vtable (binfo, old, t);
2782 for (i = 0; i < n_baselinks; i++)
2784 tree base_binfo = TREE_VEC_ELT (binfos, i);
2785 tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
2786 int is_not_base_vtable
2787 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2788 if (! TREE_VIA_VIRTUAL (base_binfo))
2789 merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
2793 /* Get the base virtual function declarations in T that are either
2794 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2795 the overrider/hider. */
2798 get_basefndecls (fndecl, t)
2801 tree methods = TYPE_METHODS (t);
2802 tree base_fndecls = NULL_TREE;
2803 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2804 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2808 if (TREE_CODE (methods) == FUNCTION_DECL
2809 && DECL_VINDEX (methods) != NULL_TREE
2810 && DECL_NAME (fndecl) == DECL_NAME (methods))
2811 base_fndecls = temp_tree_cons (fndecl, methods, base_fndecls);
2813 methods = TREE_CHAIN (methods);
2817 return base_fndecls;
2819 for (i = 0; i < n_baseclasses; i++)
2821 tree base_binfo = TREE_VEC_ELT (binfos, i);
2822 tree basetype = BINFO_TYPE (base_binfo);
2824 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2828 return base_fndecls;
2831 /* Mark the functions that have been hidden with their overriders.
2832 Since we start out with all functions already marked with a hider,
2833 no need to mark functions that are just hidden.
2835 Subroutine of warn_hidden. */
2838 mark_overriders (fndecl, base_fndecls)
2839 tree fndecl, base_fndecls;
2841 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2843 if (overrides (fndecl, TREE_VALUE (base_fndecls)))
2844 TREE_PURPOSE (base_fndecls) = fndecl;
2848 /* If this declaration supersedes the declaration of
2849 a method declared virtual in the base class, then
2850 mark this field as being virtual as well. */
2853 check_for_override (decl, ctype)
2856 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
2857 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2858 int virtualp = DECL_VIRTUAL_P (decl);
2859 int found_overriden_fn = 0;
2861 for (i = 0; i < n_baselinks; i++)
2863 tree base_binfo = TREE_VEC_ELT (binfos, i);
2864 if (TYPE_VIRTUAL_P (BINFO_TYPE (base_binfo)))
2866 tree tmp = get_matching_virtual
2868 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
2870 if (tmp && !found_overriden_fn)
2872 /* If this function overrides some virtual in some base
2873 class, then the function itself is also necessarily
2874 virtual, even if the user didn't explicitly say so. */
2875 DECL_VIRTUAL_P (decl) = 1;
2877 /* The TMP we really want is the one from the deepest
2878 baseclass on this path, taking care not to
2879 duplicate if we have already found it (via another
2880 path to its virtual baseclass. */
2881 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
2883 cp_error_at ("method `%D' may not be declared static",
2885 cp_error_at ("(since `%D' declared virtual in base class.)",
2892 = tree_cons (NULL_TREE, tmp, DECL_VINDEX (decl));
2894 /* We now know that DECL overrides something,
2895 which is all that is important. But, we must
2896 continue to iterate through all the base-classes
2897 in order to allow get_matching_virtual to check for
2898 various illegal overrides. */
2899 found_overriden_fn = 1;
2905 if (DECL_VINDEX (decl) == NULL_TREE)
2906 DECL_VINDEX (decl) = error_mark_node;
2907 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2911 /* Warn about hidden virtual functions that are not overridden in t.
2912 We know that constructors and destructors don't apply. */
2918 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2919 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2922 /* We go through each separately named virtual function. */
2923 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2925 tree fns = TREE_VEC_ELT (method_vec, i);
2928 tree base_fndecls = NULL_TREE;
2929 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2930 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2932 /* First see if we have any virtual functions in this batch. */
2933 for (; fns; fns = OVL_NEXT (fns))
2935 fndecl = OVL_CURRENT (fns);
2936 if (DECL_VINDEX (fndecl))
2940 if (fns == NULL_TREE)
2943 /* First we get a list of all possible functions that might be
2944 hidden from each base class. */
2945 for (i = 0; i < n_baseclasses; i++)
2947 tree base_binfo = TREE_VEC_ELT (binfos, i);
2948 tree basetype = BINFO_TYPE (base_binfo);
2950 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2954 fns = OVL_NEXT (fns);
2956 /* ...then mark up all the base functions with overriders, preferring
2957 overriders to hiders. */
2959 for (; fns; fns = OVL_NEXT (fns))
2961 fndecl = OVL_CURRENT (fns);
2962 if (DECL_VINDEX (fndecl))
2963 mark_overriders (fndecl, base_fndecls);
2966 /* Now give a warning for all base functions without overriders,
2967 as they are hidden. */
2968 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2970 if (! overrides (TREE_PURPOSE (base_fndecls),
2971 TREE_VALUE (base_fndecls)))
2973 /* Here we know it is a hider, and no overrider exists. */
2974 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2975 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
2981 /* Generate one vtable for use in constructors or destructors of BASE
2982 subobjects of COMPLETE_TYPE objects. The vtable belongs to the
2983 vfield of the VBASEVASE subobject of the VBASE virtual base of
2984 COMPLETE_TYPE (and BASE). */
2987 finish_one_ctor_vtable (complete_type, base, vbase, vbasebase)
2988 tree complete_type, base, vbase, vbasebase;
2994 tree newvbase = binfo_member (BINFO_TYPE (vbase),
2995 CLASSTYPE_VBASECLASSES (complete_type));
2997 newtable = prepare_ctor_vtable (complete_type, base, vbasebase);
2998 newvirtuals = copy_list (BINFO_VIRTUALS (vbasebase));
3000 virtuals = newvirtuals;
3001 /* Change the offset entry. First, delta between base an vbase. */
3002 offset = ssize_binop (MINUS_EXPR, BINFO_OFFSET (newvbase),
3003 BINFO_OFFSET (base));
3004 /* Add delta between vbase and vbasebase. */
3005 offset = ssize_binop (PLUS_EXPR, offset, BINFO_OFFSET (vbasebase));
3006 offset = ssize_binop (MINUS_EXPR, offset, BINFO_OFFSET (vbase));
3007 /* Finally, negate. */
3008 offset = ssize_binop (MINUS_EXPR, integer_zero_node, offset);
3009 offset = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
3010 TREE_CONSTANT (offset) = 1;
3011 TREE_VALUE (virtuals) = build_vtable_entry (integer_zero_node, offset);
3012 virtuals = TREE_CHAIN (virtuals);
3014 /* Skip the typeinfo function. */
3015 virtuals = TREE_CHAIN (virtuals);
3017 /* Iterate over all methods of this virtual base. */
3018 for (; virtuals; virtuals = TREE_CHAIN (virtuals))
3020 tree fndecl = TREE_VALUE (virtuals);
3021 tree pfn = FNADDR_FROM_VTABLE_ENTRY (fndecl);
3022 fndecl = TREE_OPERAND (pfn, 0);
3025 tree delta, newdelta, binfo_context;
3026 tree context = DECL_CLASS_CONTEXT (fndecl);
3028 /* If this method is implemented in a base of the vbase, the
3029 thunk we have is correct. */
3030 if (DERIVED_FROM_P (context, vbase))
3033 binfo_context = binfo_value (context, base);
3034 if (TREE_VIA_VIRTUAL (binfo_context))
3035 binfo_context = binfo_member
3036 (context, CLASSTYPE_VBASECLASSES (complete_type));
3037 /* This is the delta from a complete C to a B subobject, or
3038 more generally to the base subobject that implements the
3039 virtual function for B. BASE already has the offset to
3040 the complete type. */
3041 delta = BINFO_OFFSET (binfo_context);
3042 /* This is the delta from the A to the complete C. */
3043 newdelta = BINFO_OFFSET (newvbase);
3044 /* This is the delta from the A to the B subobject. */
3045 newdelta = size_binop (MINUS_EXPR, newdelta, delta);
3046 newdelta = ssize_binop (MINUS_EXPR, integer_zero_node,
3049 modify_vtable_entry (virtuals,
3050 build_vtable_entry (newdelta, pfn),
3054 DECL_INITIAL (newtable) = build_nt (CONSTRUCTOR, NULL_TREE,
3056 DECL_CONTEXT (newtable) = NULL_TREE;
3057 cp_finish_decl (newtable, DECL_INITIAL (newtable), NULL_TREE, 0, 0);
3058 DECL_CONTEXT (newtable) = complete_type;
3062 /* Add all vtables into LIST for the VBASEBASE subobject and its bases
3063 of VBASE virtual BASE of COMPLETE_TYPE for use in BASE
3064 constructors. DO_SELF indicates whether this is the VBASEBASE that
3065 has 'primary' vfield. Return the new LIST. */
3068 prepend_ctor_vfields_for_vbase (complete_type, base, vbase, vbasebase,
3070 tree complete_type, base, vbase, vbasebase;
3076 tree bases = BINFO_BASETYPES (vbasebase);
3077 int vfp = CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (vbasebase));
3079 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (vbasebase)))
3081 vtbl = finish_one_ctor_vtable (complete_type, base, vbase, vbasebase);
3082 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, vtbl);
3083 TREE_READONLY (vtbl) = 1;
3084 TREE_CONSTANT (vtbl) = 1;
3085 list = tree_cons (NULL_TREE, vtbl, list);
3091 for (i = 0; i < TREE_VEC_LENGTH (bases); i++)
3093 tree vbasebase = TREE_VEC_ELT (bases, i);
3094 if (TREE_VIA_VIRTUAL (vbasebase))
3096 list = prepend_ctor_vfields_for_vbase
3097 (complete_type, base, vbase, vbasebase, (i != vfp), list);
3103 /* Iterate over all virtual bases of the BASE subobject of
3104 COMPLETE_TYPE. This list is given in VBASES. Return the list of
3105 vtables generated in the process. */
3108 finish_ctor_vtables_for_vbases (vbases, base, complete_type)
3109 tree vbases, base, complete_type;
3111 tree result = NULL_TREE;
3113 for (; vbases; vbases = TREE_CHAIN (vbases))
3114 result = prepend_ctor_vfields_for_vbase
3115 (complete_type, base, vbases, vbases, 1, result);
3119 /* Generate special vtables for virtual bases for use inside base
3120 class ctors and dtors. Inside this function, we assume the
3122 class A{virtual void foo();};
3123 class B:virtual A{int member1;}
3124 class C:B{int member2;}
3126 BINFO is a base subject (e.g. B) of COMPLETE_TYPE. Returns the list
3127 of virtual tables. */
3130 finish_ctor_vtables_1 (binfo, complete_type)
3136 tree result = NULL_TREE;
3138 binfos = BINFO_BASETYPES (binfo);
3142 /* Iterate over all bases (i.e. B). */
3143 for (i = 0; i < TREE_VEC_LENGTH (binfos); i++)
3145 tree base = TREE_VEC_ELT (binfos, i);
3146 tree vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (base));
3148 /* This base class does not have virtual bases. */
3150 if (TREE_VIA_VIRTUAL (base))
3151 /* A virtual base class is initialized on in the most-derived
3154 if (!TYPE_USES_PVBASES (BINFO_TYPE (base)))
3155 /* Class has no polymorphic vbases. */
3157 /* Prepend vtable list for base class. */
3158 result = chainon (finish_ctor_vtables_1 (base, complete_type),
3160 /* Prepend our own vtable list. */
3162 (finish_ctor_vtables_for_vbases (vbases, base, complete_type),
3168 /* Add the vtables of a virtual base BINFO in front of LIST, returning
3169 the new list. DO_SELF indicates whether we have to return the
3170 vtable of a vfield borrowed in a derived class. */
3173 prepend_vbase_vfields (binfo, do_self, list)
3180 tree bases = BINFO_BASETYPES (binfo);
3181 int vfp = CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
3183 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
3185 vtbl = BINFO_VTABLE (binfo);
3186 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, vtbl);
3187 TREE_READONLY (vtbl) = 1;
3188 TREE_CONSTANT (vtbl) = 1;
3189 list = tree_cons (NULL_TREE, vtbl, list);
3195 for (i = 0; i < TREE_VEC_LENGTH (bases); i++)
3197 tree base = TREE_VEC_ELT (bases, i);
3198 if (TREE_VIA_VIRTUAL (base))
3200 list = prepend_vbase_vfields (base, (i != vfp), list);
3206 /* Wrapper around finish_ctor_vtables_1. Compute the vtable list for
3210 finish_ctor_vtables (t)
3213 tree veclist = NULL_TREE;
3219 /* This is only good for vtable thunks. */
3220 my_friendly_assert (flag_vtable_thunks, 990307);
3222 /* Start with the list of most-derived vtables. */
3224 for (vbase = CLASSTYPE_VBASECLASSES (t); vbase;
3225 vbase = TREE_CHAIN (vbase))
3226 veclist = prepend_vbase_vfields (vbase, 1, veclist);
3228 /* Compute the list of vtables for the bases. */
3229 veclist = chainon (veclist, finish_ctor_vtables_1 (TYPE_BINFO (t), t));
3231 /* Finally, we initialize the virtual bases first. */
3232 for (vbase = CLASSTYPE_VBASECLASSES (t); vbase;
3233 vbase = TREE_CHAIN (vbase))
3235 tree vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (vbase));
3238 veclist = chainon (veclist,
3239 finish_ctor_vtables_for_vbases (vbases, vbase, t));
3240 veclist = chainon (veclist,
3241 finish_ctor_vtables_1 (vbase, t));
3244 veclist = nreverse (veclist);
3246 /* Generate the name for the vtable list. */
3247 name = alloca (strlen (VLIST_NAME_FORMAT)
3248 + TYPE_ASSEMBLER_NAME_LENGTH (t) + 2);
3249 sprintf (name, VLIST_NAME_FORMAT, TYPE_ASSEMBLER_NAME_STRING (t));
3251 /* Build the type of the list. */
3252 len = list_length (veclist) - 1;
3254 /* If this class has virtual bases without virtual methods, make a
3255 single zero-entry in the array. This avoids zero-sized objects. */
3257 type = build_cplus_array_type (vtbl_ptr_type_node,
3258 build_index_type (size_int (len)));
3261 /* Produce a new decl holding the list. */
3262 decl = build_lang_decl (VAR_DECL, get_identifier (name), type);
3263 TREE_STATIC (decl) = 1;
3264 TREE_READONLY (decl) = 1;
3265 decl = pushdecl_top_level (decl);
3266 import_export_vtable (decl, t, 0);
3267 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, veclist);
3269 DECL_ARTIFICIAL (decl) = 1;
3270 /* This tells finish_file et.al. that this is related to virtual
3271 tables. There is currently no way to distinguish between vtables
3272 and vlists, other than the name of the decl. */
3273 DECL_VIRTUAL_P (decl) = 1;
3275 /* Output the array. */
3276 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0);
3278 /* Set the class context after finishing, so that finish thinks this
3279 is an unrelated global, and then finish_vtable_vardecl knows what
3280 class this is related to. */
3281 DECL_CONTEXT (decl) = t;
3284 /* Check for things that are invalid. There are probably plenty of other
3285 things we should check for also. */
3288 finish_struct_anon (t)
3292 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3294 if (TREE_STATIC (field))
3296 if (TREE_CODE (field) != FIELD_DECL)
3299 if (DECL_NAME (field) == NULL_TREE
3300 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
3302 tree* uelt = &TYPE_FIELDS (TREE_TYPE (field));
3303 for (; *uelt; uelt = &TREE_CHAIN (*uelt))
3305 if (DECL_ARTIFICIAL (*uelt))
3308 if (DECL_NAME (*uelt) == constructor_name (t))
3309 cp_pedwarn_at ("ANSI C++ forbids member `%D' with same name as enclosing class",
3312 if (TREE_CODE (*uelt) != FIELD_DECL)
3314 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3319 if (TREE_PRIVATE (*uelt))
3320 cp_pedwarn_at ("private member `%#D' in anonymous union",
3322 else if (TREE_PROTECTED (*uelt))
3323 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3326 TREE_PRIVATE (*uelt) = TREE_PRIVATE (field);
3327 TREE_PROTECTED (*uelt) = TREE_PROTECTED (field);
3333 extern int interface_only, interface_unknown;
3335 /* Create default constructors, assignment operators, and so forth for
3336 the type indicated by T, if they are needed.
3337 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3338 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3339 cannot have a default constructor, copy constructor taking a const
3340 reference argument, or an assignment operator, respectively. If a
3341 virtual destructor is created, its DECL is returned; otherwise the
3342 return value is NULL_TREE. */
3345 add_implicitly_declared_members (t, cant_have_default_ctor,
3346 cant_have_const_cctor,
3347 cant_have_assignment)
3349 int cant_have_default_ctor;
3350 int cant_have_const_cctor;
3351 int cant_have_assignment;
3354 tree implicit_fns = NULL_TREE;
3355 tree name = TYPE_IDENTIFIER (t);
3356 tree virtual_dtor = NULL_TREE;
3360 if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t)
3361 && !IS_SIGNATURE (t))
3363 default_fn = cons_up_default_function (t, name, 0);
3364 check_for_override (default_fn, t);
3366 /* If we couldn't make it work, then pretend we didn't need it. */
3367 if (default_fn == void_type_node)
3368 TYPE_NEEDS_DESTRUCTOR (t) = 0;
3371 TREE_CHAIN (default_fn) = implicit_fns;
3372 implicit_fns = default_fn;
3374 if (DECL_VINDEX (default_fn))
3375 virtual_dtor = default_fn;
3378 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3380 /* Default constructor. */
3381 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor
3382 && ! IS_SIGNATURE (t))
3384 default_fn = cons_up_default_function (t, name, 2);
3385 TREE_CHAIN (default_fn) = implicit_fns;
3386 implicit_fns = default_fn;
3389 /* Copy constructor. */
3390 if (! TYPE_HAS_INIT_REF (t) && ! IS_SIGNATURE (t) && ! TYPE_FOR_JAVA (t))
3392 /* ARM 12.18: You get either X(X&) or X(const X&), but
3394 default_fn = cons_up_default_function (t, name,
3395 3 + cant_have_const_cctor);
3396 TREE_CHAIN (default_fn) = implicit_fns;
3397 implicit_fns = default_fn;
3400 /* Assignment operator. */
3401 if (! TYPE_HAS_ASSIGN_REF (t) && ! IS_SIGNATURE (t) && ! TYPE_FOR_JAVA (t))
3403 default_fn = cons_up_default_function (t, name,
3404 5 + cant_have_assignment);
3405 TREE_CHAIN (default_fn) = implicit_fns;
3406 implicit_fns = default_fn;
3409 /* Now, hook all of the new functions on to TYPE_METHODS,
3410 and add them to the CLASSTYPE_METHOD_VEC. */
3411 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3412 add_method (t, 0, *f);
3413 *f = TYPE_METHODS (t);
3414 TYPE_METHODS (t) = implicit_fns;
3416 return virtual_dtor;
3419 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
3420 (or C++ class declaration).
3422 For C++, we must handle the building of derived classes.
3423 Also, C++ allows static class members. The way that this is
3424 handled is to keep the field name where it is (as the DECL_NAME
3425 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
3426 of the field. layout_record and layout_union will know about this.
3428 More C++ hair: inline functions have text in their
3429 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
3430 meaningful tree structure. After the struct has been laid out, set
3431 things up so that this can happen.
3433 And still more: virtual functions. In the case of single inheritance,
3434 when a new virtual function is seen which redefines a virtual function
3435 from the base class, the new virtual function is placed into
3436 the virtual function table at exactly the same address that
3437 it had in the base class. When this is extended to multiple
3438 inheritance, the same thing happens, except that multiple virtual
3439 function tables must be maintained. The first virtual function
3440 table is treated in exactly the same way as in the case of single
3441 inheritance. Additional virtual function tables have different
3442 DELTAs, which tell how to adjust `this' to point to the right thing.
3444 ATTRIBUTES is the set of decl attributes to be applied, if any. */
3447 finish_struct_1 (t, warn_anon)
3452 enum tree_code code = TREE_CODE (t);
3453 tree fields = TYPE_FIELDS (t);
3454 tree x, last_x, method_vec;
3456 int max_has_virtual;
3457 tree pending_virtuals = NULL_TREE;
3458 tree pending_hard_virtuals = NULL_TREE;
3459 tree abstract_virtuals = NULL_TREE;
3463 int cant_have_default_ctor;
3464 int cant_have_const_ctor;
3465 int no_const_asn_ref;
3466 int has_mutable = 0;
3468 /* The index of the first base class which has virtual
3469 functions. Only applied to non-virtual baseclasses. */
3470 int first_vfn_base_index;
3473 int any_default_members = 0;
3474 int const_sans_init = 0;
3475 int ref_sans_init = 0;
3476 tree access_decls = NULL_TREE;
3479 int has_pointers = 0;
3480 tree inline_friends;
3482 if (warn_anon && code != UNION_TYPE && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
3483 pedwarn ("anonymous class type not used to declare any objects");
3487 if (IS_AGGR_TYPE (t))
3488 cp_error ("redefinition of `%#T'", t);
3490 my_friendly_abort (172);
3495 GNU_xref_decl (current_function_decl, t);
3497 /* If this type was previously laid out as a forward reference,
3498 make sure we lay it out again. */
3500 TYPE_SIZE (t) = NULL_TREE;
3501 CLASSTYPE_GOT_SEMICOLON (t) = 0;
3504 /* This is in general too late to do this. I moved the main case up to
3505 left_curly, what else needs to move? */
3506 if (! IS_SIGNATURE (t))
3508 my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t) == interface_only, 999);
3509 my_friendly_assert (CLASSTYPE_INTERFACE_KNOWN (t) == ! interface_unknown, 999);
3513 old = suspend_momentary ();
3515 /* Install struct as DECL_FIELD_CONTEXT of each field decl.
3516 Also process specified field sizes.
3517 Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
3518 The specified size is found in the DECL_INITIAL.
3519 Store 0 there, except for ": 0" fields (so we can find them
3520 and delete them, below). */
3522 if (TYPE_BINFO_BASETYPES (t))
3523 n_baseclasses = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t));
3527 if (n_baseclasses > 0)
3529 struct base_info base_info;
3531 first_vfn_base_index = finish_base_struct (t, &base_info);
3532 /* Remember where we got our vfield from. */
3533 CLASSTYPE_VFIELD_PARENT (t) = first_vfn_base_index;
3534 has_virtual = base_info.has_virtual;
3535 max_has_virtual = base_info.max_has_virtual;
3536 vfield = base_info.vfield;
3537 vfields = base_info.vfields;
3538 CLASSTYPE_RTTI (t) = base_info.rtti;
3539 cant_have_default_ctor = base_info.cant_have_default_ctor;
3540 cant_have_const_ctor = base_info.cant_have_const_ctor;
3541 no_const_asn_ref = base_info.no_const_asn_ref;
3546 first_vfn_base_index = -1;
3548 max_has_virtual = has_virtual;
3550 vfields = NULL_TREE;
3551 CLASSTYPE_RTTI (t) = NULL_TREE;
3552 cant_have_default_ctor = 0;
3553 cant_have_const_ctor = 0;
3554 no_const_asn_ref = 0;
3558 /* Both of these should be done before now. */
3559 if (write_virtuals == 3 && CLASSTYPE_INTERFACE_KNOWN (t)
3560 && ! IS_SIGNATURE (t))
3562 my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t) == interface_only, 999);
3563 my_friendly_assert (CLASSTYPE_VTABLE_NEEDS_WRITING (t) == ! interface_only, 999);
3567 /* The three of these are approximations which may later be
3568 modified. Needed at this point to make add_virtual_function
3569 and modify_vtable_entries work. */
3570 CLASSTYPE_VFIELDS (t) = vfields;
3571 CLASSTYPE_VFIELD (t) = vfield;
3573 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3575 GNU_xref_member (current_class_name, x);
3577 /* If this was an evil function, don't keep it in class. */
3578 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3581 /* Do both of these, even though they're in the same union;
3582 if the insn `r' member and the size `i' member are
3583 different sizes, as on the alpha, the larger of the two
3584 will end up with garbage in it. */
3585 DECL_SAVED_INSNS (x) = NULL_RTX;
3586 DECL_FIELD_SIZE (x) = 0;
3588 check_for_override (x, t);
3589 if (DECL_ABSTRACT_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3590 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3592 /* The name of the field is the original field name
3593 Save this in auxiliary field for later overloading. */
3594 if (DECL_VINDEX (x))
3596 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3597 &has_virtual, x, t);
3598 if (DECL_ABSTRACT_VIRTUAL_P (x))
3599 abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
3601 /* XXX Why did I comment this out? (jason) */
3609 fields = chainon (build_vbase_pointer_fields (t), fields);
3612 for (x = fields; x; x = TREE_CHAIN (x))
3614 GNU_xref_member (current_class_name, x);
3616 if (TREE_CODE (x) == FIELD_DECL)
3618 DECL_PACKED (x) |= TYPE_PACKED (t);
3620 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3621 /* A zero-width bitfield doesn't do the trick. */;
3626 if (TREE_CODE (x) == USING_DECL)
3628 /* Save access declarations for later. */
3630 TREE_CHAIN (last_x) = TREE_CHAIN (x);
3632 fields = TREE_CHAIN (x);
3634 access_decls = scratch_tree_cons (NULL_TREE, x, access_decls);
3640 if (TREE_CODE (x) == TYPE_DECL
3641 || TREE_CODE (x) == TEMPLATE_DECL)
3644 /* If we've gotten this far, it's a data member, possibly static,
3645 or an enumerator. */
3647 DECL_FIELD_CONTEXT (x) = t;
3649 /* ``A local class cannot have static data members.'' ARM 9.4 */
3650 if (current_function_decl && TREE_STATIC (x))
3651 cp_error_at ("field `%D' in local class cannot be static", x);
3653 /* Perform error checking that did not get done in
3655 if (TREE_CODE (TREE_TYPE (x)) == FUNCTION_TYPE)
3657 cp_error_at ("field `%D' invalidly declared function type",
3659 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3661 else if (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE)
3663 cp_error_at ("field `%D' invalidly declared method type", x);
3664 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3666 else if (TREE_CODE (TREE_TYPE (x)) == OFFSET_TYPE)
3668 cp_error_at ("field `%D' invalidly declared offset type", x);
3669 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3673 if (DECL_NAME (x) == constructor_name (t))
3674 cant_have_default_ctor = 1;
3677 if (TREE_TYPE (x) == error_mark_node)
3680 DECL_SAVED_INSNS (x) = NULL_RTX;
3681 DECL_FIELD_SIZE (x) = 0;
3683 /* When this goes into scope, it will be a non-local reference. */
3684 DECL_NONLOCAL (x) = 1;
3686 if (TREE_CODE (x) == CONST_DECL)
3689 if (TREE_CODE (x) == VAR_DECL)
3691 if (TREE_CODE (t) == UNION_TYPE)
3692 /* Unions cannot have static members. */
3693 cp_error_at ("field `%D' declared static in union", x);
3698 /* Now it can only be a FIELD_DECL. */
3700 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3703 /* If this is of reference type, check if it needs an init.
3704 Also do a little ANSI jig if necessary. */
3705 if (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE)
3707 if (DECL_INITIAL (x) == NULL_TREE)
3710 /* ARM $12.6.2: [A member initializer list] (or, for an
3711 aggregate, initialization by a brace-enclosed list) is the
3712 only way to initialize nonstatic const and reference
3714 cant_have_default_ctor = 1;
3715 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3717 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3720 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3722 cp_warning_at ("non-static reference in class without a constructor", x);
3726 if (TREE_CODE (TREE_TYPE (x)) == POINTER_TYPE)
3729 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (TREE_TYPE (x)))
3732 /* If any field is const, the structure type is pseudo-const. */
3733 if (CP_TYPE_CONST_P (TREE_TYPE (x)))
3735 C_TYPE_FIELDS_READONLY (t) = 1;
3736 if (DECL_INITIAL (x) == NULL_TREE)
3737 const_sans_init = 1;
3739 /* ARM $12.6.2: [A member initializer list] (or, for an
3740 aggregate, initialization by a brace-enclosed list) is the
3741 only way to initialize nonstatic const and reference
3743 cant_have_default_ctor = 1;
3744 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3746 if (! TYPE_HAS_CONSTRUCTOR (t) && !IS_SIGNATURE (t)
3750 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3752 cp_warning_at ("non-static const member in class without a constructor", x);
3757 /* A field that is pseudo-const makes the structure
3759 tree t1 = TREE_TYPE (x);
3760 while (TREE_CODE (t1) == ARRAY_TYPE)
3761 t1 = TREE_TYPE (t1);
3762 if (IS_AGGR_TYPE (t1))
3764 if (C_TYPE_FIELDS_READONLY (t1))
3765 C_TYPE_FIELDS_READONLY (t) = 1;
3766 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1))
3767 const_sans_init = 1;
3771 /* We set DECL_C_BIT_FIELD in grokbitfield.
3772 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3773 if (DECL_C_BIT_FIELD (x))
3775 /* Invalid bit-field size done by grokfield. */
3776 /* Detect invalid bit-field type. */
3777 if (DECL_INITIAL (x)
3778 && ! INTEGRAL_TYPE_P (TREE_TYPE (x)))
3780 cp_error_at ("bit-field `%#D' with non-integral type", x);
3781 DECL_INITIAL (x) = NULL;
3784 /* Detect and ignore out of range field width. */
3785 if (DECL_INITIAL (x))
3787 tree w = DECL_INITIAL (x);
3788 register int width = 0;
3790 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3793 /* detect invalid field size. */
3794 if (TREE_CODE (w) == CONST_DECL)
3795 w = DECL_INITIAL (w);
3796 else if (TREE_READONLY_DECL_P (w))
3797 w = decl_constant_value (w);
3799 if (TREE_CODE (w) != INTEGER_CST)
3801 cp_error_at ("bit-field `%D' width not an integer constant",
3803 DECL_INITIAL (x) = NULL_TREE;
3805 else if (width = TREE_INT_CST_LOW (w),
3808 DECL_INITIAL (x) = NULL;
3809 cp_error_at ("negative width in bit-field `%D'", x);
3811 else if (width == 0 && DECL_NAME (x) != 0)
3813 DECL_INITIAL (x) = NULL;
3814 cp_error_at ("zero width for bit-field `%D'", x);
3817 > TYPE_PRECISION (long_long_unsigned_type_node))
3819 /* The backend will dump if you try to use something
3820 too big; avoid that. */
3821 DECL_INITIAL (x) = NULL;
3822 sorry ("bit-fields larger than %d bits",
3823 TYPE_PRECISION (long_long_unsigned_type_node));
3824 cp_error_at (" in declaration of `%D'", x);
3826 else if (width > TYPE_PRECISION (TREE_TYPE (x))
3827 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE
3828 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE)
3830 cp_warning_at ("width of `%D' exceeds its type", x);
3832 else if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
3833 && ((min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
3834 TREE_UNSIGNED (TREE_TYPE (x))) > width)
3835 || (min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
3836 TREE_UNSIGNED (TREE_TYPE (x))) > width)))
3838 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3842 if (DECL_INITIAL (x))
3844 DECL_INITIAL (x) = NULL_TREE;
3845 DECL_FIELD_SIZE (x) = width;
3846 DECL_BIT_FIELD (x) = 1;
3850 #ifdef EMPTY_FIELD_BOUNDARY
3851 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3852 EMPTY_FIELD_BOUNDARY);
3854 #ifdef PCC_BITFIELD_TYPE_MATTERS
3855 if (PCC_BITFIELD_TYPE_MATTERS)
3856 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3857 TYPE_ALIGN (TREE_TYPE (x)));
3863 /* Non-bit-fields are aligned for their type. */
3864 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x)));
3868 tree type = TREE_TYPE (x);
3870 while (TREE_CODE (type) == ARRAY_TYPE)
3871 type = TREE_TYPE (type);
3873 if (TYPE_LANG_SPECIFIC (type) && ! ANON_UNION_P (x)
3874 && ! TYPE_PTRMEMFUNC_P (type))
3876 /* Never let anything with uninheritable virtuals
3877 make it through without complaint. */
3878 if (CLASSTYPE_ABSTRACT_VIRTUALS (type))
3879 abstract_virtuals_error (x, type);
3881 /* Don't let signatures make it through either. */
3882 if (IS_SIGNATURE (type))
3883 signature_error (x, type);
3885 if (code == UNION_TYPE)
3887 const char *fie = NULL;
3888 if (TYPE_NEEDS_CONSTRUCTING (type))
3889 fie = "constructor";
3890 else if (TYPE_NEEDS_DESTRUCTOR (type))
3892 else if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3893 fie = "copy assignment operator";
3895 cp_error_at ("member `%#D' with %s not allowed in union", x,
3900 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3901 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3902 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3903 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3906 if (!TYPE_HAS_CONST_INIT_REF (type))
3907 cant_have_const_ctor = 1;
3909 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3910 no_const_asn_ref = 1;
3912 if (TYPE_HAS_CONSTRUCTOR (type)
3913 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3915 cant_have_default_ctor = 1;
3917 /* This is wrong for aggregates. */
3918 if (! TYPE_HAS_CONSTRUCTOR (t))
3921 cp_pedwarn_at ("member `%#D' with only non-default constructor", x);
3923 cp_pedwarn_at ("member with only non-default constructor", x);
3924 cp_pedwarn_at ("in class without a constructor",
3930 if (DECL_INITIAL (x) != NULL_TREE)
3932 /* `build_class_init_list' does not recognize
3934 if (code == UNION_TYPE && any_default_members != 0)
3935 cp_error_at ("multiple fields in union `%T' initialized");
3936 any_default_members = 1;
3941 /* If this type has any constant members which did not come
3942 with their own initialization, mark that fact here. It is
3943 not an error here, since such types can be saved either by their
3944 constructors, or by fortuitous initialization. */
3945 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
3946 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
3947 CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
3948 CLASSTYPE_HAS_MUTABLE (t) = has_mutable;
3950 /* Effective C++ rule 11. */
3951 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3952 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3954 cp_warning ("`%#T' has pointer data members", t);
3956 if (! TYPE_HAS_INIT_REF (t))
3958 cp_warning (" but does not override `%T(const %T&)'", t, t);
3959 if (! TYPE_HAS_ASSIGN_REF (t))
3960 cp_warning (" or `operator=(const %T&)'", t);
3962 else if (! TYPE_HAS_ASSIGN_REF (t))
3963 cp_warning (" but does not override `operator=(const %T&)'", t);
3966 /* Do some bookkeeping that will guide the generation of implicitly
3967 declared member functions. */
3968 TYPE_HAS_COMPLEX_INIT_REF (t)
3969 |= (TYPE_HAS_INIT_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3970 || has_virtual || any_default_members);
3971 TYPE_NEEDS_CONSTRUCTING (t)
3972 |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3973 || has_virtual || any_default_members);
3974 if (! IS_SIGNATURE (t))
3975 CLASSTYPE_NON_AGGREGATE (t)
3976 = ! aggregate || has_virtual || TYPE_HAS_CONSTRUCTOR (t);
3977 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
3978 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
3979 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
3981 /* Synthesize any needed methods. Note that methods will be synthesized
3982 for anonymous unions; grok_x_components undoes that. */
3984 = add_implicitly_declared_members (t, cant_have_default_ctor,
3985 cant_have_const_ctor,
3988 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3989 &has_virtual, virtual_dtor, t);
3991 if (TYPE_METHODS (t))
3993 finish_struct_methods (t);
3994 method_vec = CLASSTYPE_METHOD_VEC (t);
4000 /* Just in case these got accidentally
4001 filled in by syntax errors. */
4002 TYPE_HAS_CONSTRUCTOR (t) = 0;
4003 TYPE_HAS_DESTRUCTOR (t) = 0;
4006 for (access_decls = nreverse (access_decls); access_decls;
4007 access_decls = TREE_CHAIN (access_decls))
4008 handle_using_decl (TREE_VALUE (access_decls), t, method_vec, fields);
4010 if (vfield == NULL_TREE && has_virtual)
4012 /* We build this decl with vtbl_ptr_type_node, which is a
4013 `vtable_entry_type*'. It might seem more precise to use
4014 `vtable_entry_type (*)[N]' where N is the number of firtual
4015 functions. However, that would require the vtable pointer in
4016 base classes to have a different type than the vtable pointer
4017 in derived classes. We could make that happen, but that
4018 still wouldn't solve all the problems. In particular, the
4019 type-based alias analysis code would decide that assignments
4020 to the base class vtable pointer can't alias assignments to
4021 the derived class vtable pointer, since they have different
4022 types. Thus, in an derived class destructor, where the base
4023 class constructor was inlined, we could generate bad code for
4024 setting up the vtable pointer.
4026 Therefore, we use one type for all vtable pointers. We still
4027 use a type-correct type; it's just doesn't indicate the array
4028 bounds. That's better than using `void*' or some such; it's
4029 cleaner, and it let's the alias analysis code know that these
4030 stores cannot alias stores to void*! */
4031 vfield = build_lang_field_decl (FIELD_DECL, get_vfield_name (t),
4032 vtbl_ptr_type_node);
4033 /* If you change any of the below, take a look at all the
4034 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
4036 DECL_ASSEMBLER_NAME (vfield) = get_identifier (VFIELD_BASE);
4037 CLASSTYPE_VFIELD (t) = vfield;
4038 DECL_VIRTUAL_P (vfield) = 1;
4039 DECL_ARTIFICIAL (vfield) = 1;
4040 DECL_FIELD_CONTEXT (vfield) = t;
4041 DECL_CLASS_CONTEXT (vfield) = t;
4042 DECL_FCONTEXT (vfield) = t;
4043 DECL_SAVED_INSNS (vfield) = NULL_RTX;
4044 DECL_FIELD_SIZE (vfield) = 0;
4045 DECL_ALIGN (vfield) = TYPE_ALIGN (ptr_type_node);
4047 /* This is more efficient, but breaks binary compatibility, turn
4048 it on sometime when we don't care. If we turn it on, we also
4049 have to enable the code in dfs_init_vbase_pointers. */
4050 /* vfield is always first entry in structure. */
4051 TREE_CHAIN (vfield) = fields;
4056 my_friendly_assert (TREE_CHAIN (last_x) == NULL_TREE, 175);
4057 TREE_CHAIN (last_x) = vfield;
4064 vfields = chainon (vfields, build_tree_list (NULL_TREE, t));
4067 /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
4069 C++: maybe we will support default field initialization some day... */
4071 /* Delete all duplicate fields from the fields */
4072 delete_duplicate_fields (fields);
4074 /* Now we have the nearly final fieldlist for the data fields. Record it,
4075 then lay out the structure or union (including the fields). */
4077 TYPE_FIELDS (t) = fields;
4081 last_x = build_base_fields (t);
4083 /* If all our bases are empty, we can be empty too. */
4084 for (x = last_x; empty && x; x = TREE_CHAIN (x))
4085 if (DECL_SIZE (x) != integer_zero_node)
4089 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4090 we have to save this before we start modifying
4091 TYPE_NONCOPIED_PARTS. */
4092 inline_friends = CLASSTYPE_INLINE_FRIENDS (t);
4093 CLASSTYPE_INLINE_FRIENDS (t) = NULL_TREE;
4097 /* C++: do not let empty structures exist. */
4098 tree decl = build_lang_field_decl
4099 (FIELD_DECL, NULL_TREE, char_type_node);
4100 TREE_CHAIN (decl) = fields;
4101 TYPE_FIELDS (t) = decl;
4102 TYPE_NONCOPIED_PARTS (t)
4103 = tree_cons (NULL_TREE, decl, TYPE_NONCOPIED_PARTS (t));
4104 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
4108 TYPE_FIELDS (t) = chainon (last_x, TYPE_FIELDS (t));
4112 /* Remember the size and alignment of the class before adding
4113 the virtual bases. */
4114 if (empty && flag_new_abi)
4115 CLASSTYPE_SIZE (t) = integer_zero_node;
4116 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
4117 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
4118 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
4120 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
4121 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
4123 finish_struct_anon (t);
4125 /* Set the TYPE_DECL for this type to contain the right
4126 value for DECL_OFFSET, so that we can use it as part
4127 of a COMPONENT_REF for multiple inheritance. */
4129 layout_decl (TYPE_MAIN_DECL (t), 0);
4131 /* Now fix up any virtual base class types that we left lying
4132 around. We must get these done before we try to lay out the
4133 virtual function table. */
4134 pending_hard_virtuals = nreverse (pending_hard_virtuals);
4137 /* layout_basetypes will remove the base subobject fields. */
4138 max_has_virtual = layout_basetypes (t, max_has_virtual);
4140 TYPE_FIELDS (t) = fields;
4142 my_friendly_assert (TYPE_FIELDS (t) == fields, 981117);
4144 /* Delete all zero-width bit-fields from the front of the fieldlist */
4145 while (fields && DECL_C_BIT_FIELD (fields)
4146 && DECL_INITIAL (fields))
4147 fields = TREE_CHAIN (fields);
4148 /* Delete all such fields from the rest of the fields. */
4149 for (x = fields; x;)
4151 if (TREE_CHAIN (x) && DECL_C_BIT_FIELD (TREE_CHAIN (x))
4152 && DECL_INITIAL (TREE_CHAIN (x)))
4153 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
4157 TYPE_FIELDS (t) = fields;
4159 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
4163 vbases = CLASSTYPE_VBASECLASSES (t);
4166 /* Now fixup overrides of all functions in vtables from all
4167 direct or indirect virtual base classes. */
4168 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
4169 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
4171 for (i = 0; i < n_baseclasses; i++)
4173 tree base_binfo = TREE_VEC_ELT (binfos, i);
4174 tree basetype = BINFO_TYPE (base_binfo);
4177 vbases = CLASSTYPE_VBASECLASSES (basetype);
4180 merge_overrides (binfo_member (BINFO_TYPE (vbases),
4181 CLASSTYPE_VBASECLASSES (t)),
4183 vbases = TREE_CHAIN (vbases);
4189 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
4190 might need to know it for setting up the offsets in the vtable
4191 (or in thunks) below. */
4192 if (vfield != NULL_TREE
4193 && DECL_FIELD_CONTEXT (vfield) != t)
4195 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4196 tree offset = BINFO_OFFSET (binfo);
4198 vfield = copy_node (vfield);
4199 copy_lang_decl (vfield);
4201 if (! integer_zerop (offset))
4202 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4203 DECL_FIELD_CONTEXT (vfield) = t;
4204 DECL_CLASS_CONTEXT (vfield) = t;
4205 DECL_FIELD_BITPOS (vfield)
4206 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4207 CLASSTYPE_VFIELD (t) = vfield;
4211 cp_warning ("Doing hard virtuals for %T...", t);
4214 if (has_virtual > max_has_virtual)
4215 max_has_virtual = has_virtual;
4216 if (max_has_virtual > 0)
4217 TYPE_VIRTUAL_P (t) = 1;
4219 if (flag_rtti && TYPE_VIRTUAL_P (t) && !pending_hard_virtuals)
4220 modify_all_vtables (t, NULL_TREE, NULL_TREE);
4222 while (pending_hard_virtuals)
4224 modify_all_vtables (t,
4225 TREE_PURPOSE (pending_hard_virtuals),
4226 TREE_VALUE (pending_hard_virtuals));
4227 pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
4230 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
4233 /* Now fixup any virtual function entries from virtual bases
4234 that have different deltas. This has to come after we do the
4235 pending hard virtuals, as we might have a function that comes
4236 from multiple virtual base instances that is only overridden
4237 by a hard virtual above. */
4238 vbases = CLASSTYPE_VBASECLASSES (t);
4241 /* We might be able to shorten the amount of work we do by
4242 only doing this for vtables that come from virtual bases
4243 that have differing offsets, but don't want to miss any
4245 fixup_vtable_deltas (vbases, 1, t);
4246 vbases = TREE_CHAIN (vbases);
4250 /* Under our model of GC, every C++ class gets its own virtual
4251 function table, at least virtually. */
4252 if (pending_virtuals)
4254 pending_virtuals = nreverse (pending_virtuals);
4255 /* We must enter these virtuals into the table. */
4256 if (first_vfn_base_index < 0)
4258 if (! CLASSTYPE_COM_INTERFACE (t))
4260 /* The second slot is for the tdesc pointer when thunks are used. */
4261 if (flag_vtable_thunks)
4262 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4264 /* The first slot is for the rtti offset. */
4265 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4267 set_rtti_entry (pending_virtuals,
4268 convert (ssizetype, integer_zero_node), t);
4270 build_vtable (NULL_TREE, t);
4274 /* Here we know enough to change the type of our virtual
4275 function table, but we will wait until later this function. */
4277 if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
4278 build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index), t);
4281 /* If this type has basetypes with constructors, then those
4282 constructors might clobber the virtual function table. But
4283 they don't if the derived class shares the exact vtable of the base
4286 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4288 else if (first_vfn_base_index >= 0)
4290 tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index);
4291 /* This class contributes nothing new to the virtual function
4292 table. However, it may have declared functions which
4293 went into the virtual function table "inherited" from the
4294 base class. If so, we grab a copy of those updated functions,
4295 and pretend they are ours. */
4297 /* See if we should steal the virtual info from base class. */
4298 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
4299 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
4300 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
4301 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
4302 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
4303 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4306 if (max_has_virtual || first_vfn_base_index >= 0)
4308 CLASSTYPE_VSIZE (t) = has_virtual;
4309 if (first_vfn_base_index >= 0)
4311 if (pending_virtuals)
4312 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t),
4315 else if (has_virtual)
4317 TYPE_BINFO_VIRTUALS (t) = pending_virtuals;
4318 DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1;
4322 /* Now lay out the virtual function table. */
4325 /* Use size_int so values are memoized in common cases. */
4326 tree itype = build_index_type (size_int (has_virtual));
4327 tree atype = build_cplus_array_type (vtable_entry_type, itype);
4329 layout_type (atype);
4331 CLASSTYPE_VFIELD (t) = vfield;
4333 /* We may have to grow the vtable. */
4334 if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
4336 TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
4337 DECL_SIZE (TYPE_BINFO_VTABLE (t)) = 0;
4338 layout_decl (TYPE_BINFO_VTABLE (t), 0);
4339 /* At one time the vtable info was grabbed 2 words at a time. This
4340 fails on sparc unless you have 8-byte alignment. (tiemann) */
4341 DECL_ALIGN (TYPE_BINFO_VTABLE (t))
4342 = MAX (TYPE_ALIGN (double_type_node),
4343 DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
4346 else if (first_vfn_base_index >= 0)
4347 CLASSTYPE_VFIELD (t) = vfield;
4348 CLASSTYPE_VFIELDS (t) = vfields;
4350 finish_struct_bits (t, max_has_virtual);
4352 /* Complete the rtl for any static member objects of the type we're
4354 for (x = fields; x; x = TREE_CHAIN (x))
4356 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4357 && TREE_TYPE (x) == t)
4359 DECL_MODE (x) = TYPE_MODE (t);
4360 make_decl_rtl (x, NULL, 0);
4364 if (TYPE_HAS_CONSTRUCTOR (t))
4366 tree vfields = CLASSTYPE_VFIELDS (t);
4370 /* Mark the fact that constructor for T
4371 could affect anybody inheriting from T
4372 who wants to initialize vtables for VFIELDS's type. */
4373 if (VF_DERIVED_VALUE (vfields))
4374 TREE_ADDRESSABLE (vfields) = 1;
4375 vfields = TREE_CHAIN (vfields);
4379 /* Write out inline function definitions. */
4380 do_inline_function_hair (t, inline_friends);
4382 if (CLASSTYPE_VSIZE (t) != 0)
4385 /* This is now done above. */
4386 if (DECL_FIELD_CONTEXT (vfield) != t)
4388 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4389 tree offset = BINFO_OFFSET (binfo);
4391 vfield = copy_node (vfield);
4392 copy_lang_decl (vfield);
4394 if (! integer_zerop (offset))
4395 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4396 DECL_FIELD_CONTEXT (vfield) = t;
4397 DECL_CLASS_CONTEXT (vfield) = t;
4398 DECL_FIELD_BITPOS (vfield)
4399 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4400 CLASSTYPE_VFIELD (t) = vfield;
4404 /* In addition to this one, all the other vfields should be listed. */
4405 /* Before that can be done, we have to have FIELD_DECLs for them, and
4406 a place to find them. */
4407 TYPE_NONCOPIED_PARTS (t)
4408 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
4409 vfield, TYPE_NONCOPIED_PARTS (t));
4411 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
4412 && DECL_VINDEX (TREE_VEC_ELT (method_vec, 1)) == NULL_TREE)
4413 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
4417 /* Make the rtl for any new vtables we have created, and unmark
4418 the base types we marked. */
4419 finish_vtbls (TYPE_BINFO (t), 1, t);
4420 /* If we use thunks, and have virtual bases, we might need to emit
4421 additional vtables. */
4422 if (flag_vtable_thunks && TYPE_USES_PVBASES (t))
4423 finish_ctor_vtables (t);
4424 hack_incomplete_structures (t);
4427 if (TYPE_NAME (t) && TYPE_IDENTIFIER (t))
4428 undo_template_name_overload (TYPE_IDENTIFIER (t), 1);
4431 resume_momentary (old);
4433 if (warn_overloaded_virtual)
4437 /* This has to be done after we have sorted out what to do with
4438 the enclosing type. */
4439 if (write_symbols != DWARF_DEBUG)
4441 /* Be smarter about nested classes here. If a type is nested,
4442 only output it if we would output the enclosing type. */
4443 if (DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (t)))
4444 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = TREE_ASM_WRITTEN (TYPE_MAIN_DECL (t));
4448 if (write_symbols != DWARF_DEBUG && write_symbols != DWARF2_DEBUG)
4450 /* If the type has methods, we want to think about cutting down
4451 the amount of symbol table stuff we output. The value stored in
4452 the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
4453 For example, if a member function is seen and we decide to
4454 write out that member function, then we can change the value
4455 of the DECL_IGNORED_P slot, and the type will be output when
4456 that member function's debug info is written out.
4458 We can't do this with DWARF, which does not support name
4459 references between translation units. */
4460 if (CLASSTYPE_METHOD_VEC (t))
4462 /* Don't output full info about any type
4463 which does not have its implementation defined here. */
4464 if (CLASSTYPE_INTERFACE_ONLY (t))
4465 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4467 /* XXX do something about this. */
4468 else if (CLASSTYPE_INTERFACE_UNKNOWN (t))
4469 /* Only a first approximation! */
4470 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4473 else if (CLASSTYPE_INTERFACE_ONLY (t))
4474 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4477 /* Finish debugging output for this type. */
4478 rest_of_type_compilation (t, toplevel_bindings_p ());
4483 /* When T was built up, the member declarations were added in reverse
4484 order. Rearrange them to declaration order. */
4487 unreverse_member_declarations (t)
4494 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
4495 reverse order. Put them in declaration order now. */
4496 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
4497 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
4499 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
4500 reverse order, so we can't just use nreverse. */
4502 for (x = TYPE_FIELDS (t);
4503 x && TREE_CODE (x) != TYPE_DECL;
4506 next = TREE_CHAIN (x);
4507 TREE_CHAIN (x) = prev;
4512 TREE_CHAIN (TYPE_FIELDS (t)) = x;
4514 TYPE_FIELDS (t) = prev;
4519 finish_struct (t, attributes, warn_anon)
4523 tree name = TYPE_NAME (t);
4525 if (TREE_CODE (name) == TYPE_DECL)
4529 DECL_SOURCE_FILE (name) = input_filename;
4530 /* For TYPE_DECL that are not typedefs (those marked with a line
4531 number of zero, we don't want to mark them as real typedefs.
4532 If this fails one needs to make sure real typedefs have a
4533 previous line number, even if it is wrong, that way the below
4534 will fill in the right line number. (mrs) */
4535 if (DECL_SOURCE_LINE (name))
4536 DECL_SOURCE_LINE (name) = lineno;
4537 name = DECL_NAME (name);
4540 /* Append the fields we need for constructing signature tables. */
4541 if (IS_SIGNATURE (t))
4542 append_signature_fields (t);
4544 /* Now that we've got all the field declarations, reverse everything
4546 unreverse_member_declarations (t);
4548 cplus_decl_attributes (t, attributes, NULL_TREE);
4550 if (processing_template_decl)
4552 tree d = getdecls ();
4553 for (; d; d = TREE_CHAIN (d))
4555 /* If this is the decl for the class or one of the template
4556 parms, we've seen all the injected decls. */
4557 if ((TREE_CODE (d) == TYPE_DECL
4558 && (TREE_TYPE (d) == t
4559 || TREE_CODE (TREE_TYPE (d)) == TEMPLATE_TYPE_PARM
4560 || TREE_CODE (TREE_TYPE (d)) == TEMPLATE_TEMPLATE_PARM))
4561 || TREE_CODE (d) == CONST_DECL)
4563 /* Don't inject cache decls. */
4564 else if (IDENTIFIER_TEMPLATE (DECL_NAME (d)))
4566 DECL_TEMPLATE_INJECT (CLASSTYPE_TI_TEMPLATE (t))
4567 = tree_cons (NULL_TREE, d,
4568 DECL_TEMPLATE_INJECT (CLASSTYPE_TI_TEMPLATE (t)));
4570 finish_struct_methods (t);
4571 TYPE_SIZE (t) = integer_zero_node;
4574 finish_struct_1 (t, warn_anon);
4576 TYPE_BEING_DEFINED (t) = 0;
4578 if (current_class_type)
4581 error ("trying to finish struct, but kicked out due to previous parse errors.");
4586 /* Return the dynamic type of INSTANCE, if known.
4587 Used to determine whether the virtual function table is needed
4590 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4591 of our knowledge of its type. */
4594 fixed_type_or_null (instance, nonnull)
4598 switch (TREE_CODE (instance))
4601 /* Check that we are not going through a cast of some sort. */
4602 if (TREE_TYPE (instance)
4603 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
4604 instance = TREE_OPERAND (instance, 0);
4605 /* fall through... */
4607 /* This is a call to a constructor, hence it's never zero. */
4608 if (TREE_HAS_CONSTRUCTOR (instance))
4612 return TREE_TYPE (instance);
4617 /* This is a call to a constructor, hence it's never zero. */
4618 if (TREE_HAS_CONSTRUCTOR (instance))
4622 return TREE_TYPE (instance);
4624 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4631 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
4632 /* Propagate nonnull. */
4633 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4634 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
4635 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4640 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4645 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4648 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
4652 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
4653 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
4657 return TREE_TYPE (TREE_TYPE (instance));
4659 /* fall through... */
4662 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
4666 return TREE_TYPE (instance);
4670 if (instance == current_class_ptr
4671 && flag_this_is_variable <= 0)
4673 /* Normally, 'this' must be non-null. */
4674 if (flag_this_is_variable == 0)
4677 /* <0 means we're in a constructor and we know our type. */
4678 if (flag_this_is_variable < 0)
4679 return TREE_TYPE (TREE_TYPE (instance));
4681 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4682 /* Reference variables should be references to objects. */
4692 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
4693 to the static type. We also handle the case where INSTANCE is really
4696 Used to determine whether the virtual function table is needed
4699 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4700 of our knowledge of its type. */
4703 resolves_to_fixed_type_p (instance, nonnull)
4707 tree t = TREE_TYPE (instance);
4708 tree fixed = fixed_type_or_null (instance, nonnull);
4709 if (fixed == NULL_TREE)
4711 if (POINTER_TYPE_P (t))
4713 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
4718 init_class_processing ()
4720 current_class_depth = 0;
4721 current_class_stack_size = 10;
4723 = (class_stack_node_t) xmalloc (current_class_stack_size
4724 * sizeof (struct class_stack_node));
4726 current_lang_stacksize = 10;
4727 current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree));
4728 current_lang_stack = current_lang_base;
4730 access_default_node = build_int_2 (0, 0);
4731 access_public_node = build_int_2 (1, 0);
4732 access_protected_node = build_int_2 (2, 0);
4733 access_private_node = build_int_2 (3, 0);
4734 access_default_virtual_node = build_int_2 (4, 0);
4735 access_public_virtual_node = build_int_2 (5, 0);
4736 access_protected_virtual_node = build_int_2 (6, 0);
4737 access_private_virtual_node = build_int_2 (7, 0);
4739 /* Keep these values lying around. */
4740 base_layout_decl = build_lang_field_decl (FIELD_DECL, NULL_TREE, error_mark_node);
4741 TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE);
4743 gcc_obstack_init (&class_obstack);
4746 /* Set current scope to NAME. CODE tells us if this is a
4747 STRUCT, UNION, or ENUM environment.
4749 NAME may end up being NULL_TREE if this is an anonymous or
4750 late-bound struct (as in "struct { ... } foo;") */
4752 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
4753 appropriate values, found by looking up the type definition of
4756 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
4757 which can be seen locally to the class. They are shadowed by
4758 any subsequent local declaration (including parameter names).
4760 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
4761 which have static meaning (i.e., static members, static
4762 member functions, enum declarations, etc).
4764 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
4765 which can be seen locally to the class (as in 1), but
4766 know that we are doing this for declaration purposes
4767 (i.e. friend foo::bar (int)).
4769 So that we may avoid calls to lookup_name, we cache the _TYPE
4770 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
4772 For multiple inheritance, we perform a two-pass depth-first search
4773 of the type lattice. The first pass performs a pre-order search,
4774 marking types after the type has had its fields installed in
4775 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
4776 unmarks the marked types. If a field or member function name
4777 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
4778 that name becomes `error_mark_node'. */
4781 pushclass (type, modify)
4785 type = TYPE_MAIN_VARIANT (type);
4787 /* Make sure there is enough room for the new entry on the stack. */
4788 if (current_class_depth + 1 >= current_class_stack_size)
4790 current_class_stack_size *= 2;
4792 = (class_stack_node_t) xrealloc (current_class_stack,
4793 current_class_stack_size
4794 * sizeof (struct class_stack_node));
4797 /* Insert a new entry on the class stack. */
4798 current_class_stack[current_class_depth].name = current_class_name;
4799 current_class_stack[current_class_depth].type = current_class_type;
4800 current_class_stack[current_class_depth].access = current_access_specifier;
4801 current_class_stack[current_class_depth].names_used = 0;
4802 current_class_depth++;
4804 /* Now set up the new type. */
4805 current_class_name = TYPE_NAME (type);
4806 if (TREE_CODE (current_class_name) == TYPE_DECL)
4807 current_class_name = DECL_NAME (current_class_name);
4808 current_class_type = type;
4810 /* By default, things in classes are private, while things in
4811 structures or unions are public. */
4812 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
4813 ? access_private_node
4814 : access_public_node);
4816 if (previous_class_type != NULL_TREE
4817 && (type != previous_class_type
4818 || TYPE_SIZE (previous_class_type) == NULL_TREE)
4819 && current_class_depth == 1)
4821 /* Forcibly remove any old class remnants. */
4822 invalidate_class_lookup_cache ();
4824 /* Now, free the obstack on which we cached all the values. */
4825 if (class_cache_firstobj)
4826 obstack_free (&class_cache_obstack, class_cache_firstobj);
4827 class_cache_firstobj
4828 = (char*) obstack_finish (&class_cache_obstack);
4831 /* If we're about to enter a nested class, clear
4832 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
4833 if (modify && current_class_depth > 1)
4834 clear_identifier_class_values ();
4839 if (CLASSTYPE_TEMPLATE_INFO (type))
4840 overload_template_name (type);
4845 if (type != previous_class_type || current_class_depth > 1)
4846 push_class_decls (type);
4851 /* We are re-entering the same class we just left, so we
4852 don't have to search the whole inheritance matrix to find
4853 all the decls to bind again. Instead, we install the
4854 cached class_shadowed list, and walk through it binding
4855 names and setting up IDENTIFIER_TYPE_VALUEs. */
4856 set_class_shadows (previous_class_values);
4857 for (item = previous_class_values; item; item = TREE_CHAIN (item))
4859 tree id = TREE_PURPOSE (item);
4860 tree decl = TREE_TYPE (item);
4862 push_class_binding (id, decl);
4863 if (TREE_CODE (decl) == TYPE_DECL)
4864 set_identifier_type_value (id, TREE_TYPE (decl));
4866 unuse_fields (type);
4869 storetags (CLASSTYPE_TAGS (type));
4873 /* When we exit a toplevel class scope, we save the
4874 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
4875 reenter the class. Here, we've entered some other class, so we
4876 must invalidate our cache. */
4879 invalidate_class_lookup_cache ()
4883 /* This code can be seen as a cache miss. When we've cached a
4884 class' scope's bindings and we can't use them, we need to reset
4885 them. This is it! */
4886 for (t = previous_class_values; t; t = TREE_CHAIN (t))
4887 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
4889 previous_class_type = NULL_TREE;
4892 /* Get out of the current class scope. If we were in a class scope
4893 previously, that is the one popped to. */
4899 /* Since poplevel_class does the popping of class decls nowadays,
4900 this really only frees the obstack used for these decls. */
4903 current_class_depth--;
4904 current_class_name = current_class_stack[current_class_depth].name;
4905 current_class_type = current_class_stack[current_class_depth].type;
4906 current_access_specifier = current_class_stack[current_class_depth].access;
4907 if (current_class_stack[current_class_depth].names_used)
4908 splay_tree_delete (current_class_stack[current_class_depth].names_used);
4911 /* Returns 1 if current_class_type is either T or a nested type of T. */
4914 currently_open_class (t)
4918 if (t == current_class_type)
4920 for (i = 0; i < current_class_depth; ++i)
4921 if (current_class_stack [i].type == t)
4926 /* When entering a class scope, all enclosing class scopes' names with
4927 static meaning (static variables, static functions, types and enumerators)
4928 have to be visible. This recursive function calls pushclass for all
4929 enclosing class contexts until global or a local scope is reached.
4930 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
4931 formal of the same name. */
4934 push_nested_class (type, modify)
4940 /* A namespace might be passed in error cases, like A::B:C. */
4941 if (type == NULL_TREE || type == error_mark_node || ! IS_AGGR_TYPE (type)
4942 || TREE_CODE (type) == NAMESPACE_DECL
4943 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
4944 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
4947 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
4949 if (context && CLASS_TYPE_P (context))
4950 push_nested_class (context, 2);
4951 pushclass (type, modify);
4954 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
4959 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
4962 if (context && CLASS_TYPE_P (context))
4963 pop_nested_class ();
4966 /* Set global variables CURRENT_LANG_NAME to appropriate value
4967 so that behavior of name-mangling machinery is correct. */
4970 push_lang_context (name)
4973 *current_lang_stack++ = current_lang_name;
4974 if (current_lang_stack >= current_lang_base + current_lang_stacksize)
4977 = (tree *)xrealloc (current_lang_base,
4978 sizeof (tree) * (current_lang_stacksize + 10));
4979 current_lang_stack = current_lang_base + current_lang_stacksize;
4980 current_lang_stacksize += 10;
4983 if (name == lang_name_cplusplus)
4985 strict_prototype = strict_prototypes_lang_cplusplus;
4986 current_lang_name = name;
4988 else if (name == lang_name_java)
4990 strict_prototype = strict_prototypes_lang_cplusplus;
4991 current_lang_name = name;
4992 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
4993 (See record_builtin_java_type in decl.c.) However, that causes
4994 incorrect debug entries if these types are actually used.
4995 So we re-enable debug output after extern "Java". */
4996 DECL_IGNORED_P (java_byte_type_node) = 0;
4997 DECL_IGNORED_P (java_short_type_node) = 0;
4998 DECL_IGNORED_P (java_int_type_node) = 0;
4999 DECL_IGNORED_P (java_long_type_node) = 0;
5000 DECL_IGNORED_P (java_float_type_node) = 0;
5001 DECL_IGNORED_P (java_double_type_node) = 0;
5002 DECL_IGNORED_P (java_char_type_node) = 0;
5003 DECL_IGNORED_P (java_boolean_type_node) = 0;
5005 else if (name == lang_name_c)
5007 strict_prototype = strict_prototypes_lang_c;
5008 current_lang_name = name;
5011 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5014 /* Get out of the current language scope. */
5019 current_lang_name = *--current_lang_stack;
5020 if (current_lang_name == lang_name_cplusplus
5021 || current_lang_name == lang_name_java)
5022 strict_prototype = strict_prototypes_lang_cplusplus;
5023 else if (current_lang_name == lang_name_c)
5024 strict_prototype = strict_prototypes_lang_c;
5027 /* Type instantiation routines. */
5029 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5030 matches the TARGET_TYPE. If there is no satisfactory match, return
5031 error_mark_node, and issue an error message if COMPLAIN is
5032 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
5033 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5034 template arguments. */
5037 resolve_address_of_overloaded_function (target_type,
5046 tree explicit_targs;
5048 /* Here's what the standard says:
5052 If the name is a function template, template argument deduction
5053 is done, and if the argument deduction succeeds, the deduced
5054 arguments are used to generate a single template function, which
5055 is added to the set of overloaded functions considered.
5057 Non-member functions and static member functions match targets of
5058 type "pointer-to-function" or "reference-to-function." Nonstatic
5059 member functions match targets of type "pointer-to-member
5060 function;" the function type of the pointer to member is used to
5061 select the member function from the set of overloaded member
5062 functions. If a nonstatic member function is selected, the
5063 reference to the overloaded function name is required to have the
5064 form of a pointer to member as described in 5.3.1.
5066 If more than one function is selected, any template functions in
5067 the set are eliminated if the set also contains a non-template
5068 function, and any given template function is eliminated if the
5069 set contains a second template function that is more specialized
5070 than the first according to the partial ordering rules 14.5.5.2.
5071 After such eliminations, if any, there shall remain exactly one
5072 selected function. */
5075 int is_reference = 0;
5076 /* We store the matches in a TREE_LIST rooted here. The functions
5077 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5078 interoperability with most_specialized_instantiation. */
5079 tree matches = NULL_TREE;
5082 /* By the time we get here, we should be seeing only real
5083 pointer-to-member types, not the internal POINTER_TYPE to
5084 METHOD_TYPE representation. */
5085 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5086 && (TREE_CODE (TREE_TYPE (target_type))
5087 == METHOD_TYPE)), 0);
5089 /* Check that the TARGET_TYPE is reasonable. */
5090 if (TYPE_PTRFN_P (target_type))
5093 else if (TYPE_PTRMEMFUNC_P (target_type))
5094 /* This is OK, too. */
5096 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5098 /* This is OK, too. This comes from a conversion to reference
5100 target_type = build_reference_type (target_type);
5106 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
5107 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5108 return error_mark_node;
5111 /* If we can find a non-template function that matches, we can just
5112 use it. There's no point in generating template instantiations
5113 if we're just going to throw them out anyhow. But, of course, we
5114 can only do this when we don't *need* a template function. */
5119 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5121 tree fn = OVL_FUNCTION (fns);
5124 if (TREE_CODE (fn) == TEMPLATE_DECL)
5125 /* We're not looking for templates just yet. */
5128 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5130 /* We're looking for a non-static member, and this isn't
5131 one, or vice versa. */
5134 /* See if there's a match. */
5135 fntype = TREE_TYPE (fn);
5137 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5138 else if (!is_reference)
5139 fntype = build_pointer_type (fntype);
5141 if (can_convert_arg (target_type, fntype, fn))
5142 matches = scratch_tree_cons (fn, NULL_TREE, matches);
5146 /* Now, if we've already got a match (or matches), there's no need
5147 to proceed to the template functions. But, if we don't have a
5148 match we need to look at them, too. */
5151 tree target_fn_type;
5152 tree target_arg_types;
5157 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5159 target_fn_type = TREE_TYPE (target_type);
5160 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5162 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5164 tree fn = OVL_FUNCTION (fns);
5166 tree instantiation_type;
5169 if (TREE_CODE (fn) != TEMPLATE_DECL)
5170 /* We're only looking for templates. */
5173 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5175 /* We're not looking for a non-static member, and this is
5176 one, or vice versa. */
5179 /* Try to do argument deduction. */
5180 targs = make_scratch_vec (DECL_NTPARMS (fn));
5181 if (fn_type_unification (fn, explicit_targs, targs,
5182 target_arg_types, NULL_TREE,
5184 /* Argument deduction failed. */
5187 /* Instantiate the template. */
5188 instantiation = instantiate_template (fn, targs);
5189 if (instantiation == error_mark_node)
5190 /* Instantiation failed. */
5193 /* See if there's a match. */
5194 instantiation_type = TREE_TYPE (instantiation);
5196 instantiation_type =
5197 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5198 else if (!is_reference)
5199 instantiation_type = build_pointer_type (instantiation_type);
5200 if (can_convert_arg (target_type, instantiation_type, instantiation))
5201 matches = scratch_tree_cons (instantiation, fn, matches);
5204 /* Now, remove all but the most specialized of the matches. */
5207 tree match = most_specialized_instantiation (matches,
5210 if (match != error_mark_node)
5211 matches = scratch_tree_cons (match, NULL_TREE, NULL_TREE);
5215 /* Now we should have exactly one function in MATCHES. */
5216 if (matches == NULL_TREE)
5218 /* There were *no* matches. */
5221 cp_error ("no matches converting function `%D' to type `%#T'",
5222 DECL_NAME (OVL_FUNCTION (overload)),
5225 /* print_candidates expects a chain with the functions in
5226 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5227 so why be clever?). */
5228 for (; overload; overload = OVL_NEXT (overload))
5229 matches = scratch_tree_cons (NULL_TREE, OVL_CURRENT (overload),
5232 print_candidates (matches);
5234 return error_mark_node;
5236 else if (TREE_CHAIN (matches))
5238 /* There were too many matches. */
5244 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
5245 DECL_NAME (OVL_FUNCTION (overload)),
5248 /* Since print_candidates expects the functions in the
5249 TREE_VALUE slot, we flip them here. */
5250 for (match = matches; match; match = TREE_CHAIN (match))
5251 TREE_VALUE (match) = TREE_PURPOSE (match);
5253 print_candidates (matches);
5256 return error_mark_node;
5259 /* Good, exactly one match. Now, convert it to the correct type. */
5260 fn = TREE_PURPOSE (matches);
5264 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
5265 return build_unary_op (ADDR_EXPR, fn, 0);
5268 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
5269 will mark the function as addressed, but here we must do it
5271 mark_addressable (fn);
5277 /* This function will instantiate the type of the expression given in
5278 RHS to match the type of LHSTYPE. If errors exist, then return
5279 error_mark_node. We only complain is COMPLAIN is set. If we are
5280 not complaining, never modify rhs, as overload resolution wants to
5281 try many possible instantiations, in hopes that at least one will
5284 FLAGS is a bitmask, as we see at the top of the function.
5286 For non-recursive calls, LHSTYPE should be a function, pointer to
5287 function, or a pointer to member function. */
5290 instantiate_type (lhstype, rhs, flags)
5294 int complain = (flags & 1);
5295 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
5297 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
5300 error ("not enough type information");
5301 return error_mark_node;
5304 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
5306 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
5309 cp_error ("argument of type `%T' does not match `%T'",
5310 TREE_TYPE (rhs), lhstype);
5311 return error_mark_node;
5314 /* We don't overwrite rhs if it is an overloaded function.
5315 Copying it would destroy the tree link. */
5316 if (TREE_CODE (rhs) != OVERLOAD)
5317 rhs = copy_node (rhs);
5319 /* This should really only be used when attempting to distinguish
5320 what sort of a pointer to function we have. For now, any
5321 arithmetic operation which is not supported on pointers
5322 is rejected as an error. */
5324 switch (TREE_CODE (rhs))
5331 my_friendly_abort (177);
5332 return error_mark_node;
5339 new_rhs = instantiate_type (build_pointer_type (lhstype),
5340 TREE_OPERAND (rhs, 0), flags);
5341 if (new_rhs == error_mark_node)
5342 return error_mark_node;
5344 TREE_TYPE (rhs) = lhstype;
5345 TREE_OPERAND (rhs, 0) = new_rhs;
5350 rhs = copy_node (TREE_OPERAND (rhs, 0));
5351 TREE_TYPE (rhs) = unknown_type_node;
5352 return instantiate_type (lhstype, rhs, flags);
5356 tree field = TREE_OPERAND (rhs, 1);
5359 r = instantiate_type (lhstype, field, flags);
5361 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype))
5365 tree t = TYPE_PTRMEMFUNC_OBJECT_TYPE (lhstype);
5367 if (TREE_CODE (field) == OVERLOAD)
5368 field = OVL_FUNCTION (field);
5369 if (TREE_CODE (field) == FUNCTION_DECL)
5371 cp_pedwarn ("object-dependent reference `%E' can only be used in a call",
5373 cp_pedwarn (" to form a pointer to member function, say `&%T::%E'",
5374 t, DECL_NAME (field));
5377 cp_pedwarn ("object-dependent reference can only be used in a call");
5386 rhs = TREE_OPERAND (rhs, 1);
5387 if (BASELINK_P (rhs))
5388 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5390 /* This can happen if we are forming a pointer-to-member for a
5392 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
5396 case TEMPLATE_ID_EXPR:
5398 resolve_address_of_overloaded_function (lhstype,
5399 TREE_OPERAND (rhs, 0),
5401 /*template_only=*/1,
5402 TREE_OPERAND (rhs, 1));
5406 resolve_address_of_overloaded_function (lhstype,
5409 /*template_only=*/0,
5410 /*explicit_targs=*/NULL_TREE);
5413 /* Now we should have a baselink. */
5414 my_friendly_assert (BASELINK_P (rhs), 990412);
5416 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5419 /* This is too hard for now. */
5420 my_friendly_abort (183);
5421 return error_mark_node;
5426 TREE_OPERAND (rhs, 0)
5427 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5428 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5429 return error_mark_node;
5430 TREE_OPERAND (rhs, 1)
5431 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5432 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5433 return error_mark_node;
5435 TREE_TYPE (rhs) = lhstype;
5439 case TRUNC_DIV_EXPR:
5440 case FLOOR_DIV_EXPR:
5442 case ROUND_DIV_EXPR:
5444 case TRUNC_MOD_EXPR:
5445 case FLOOR_MOD_EXPR:
5447 case ROUND_MOD_EXPR:
5448 case FIX_ROUND_EXPR:
5449 case FIX_FLOOR_EXPR:
5451 case FIX_TRUNC_EXPR:
5467 case PREINCREMENT_EXPR:
5468 case PREDECREMENT_EXPR:
5469 case POSTINCREMENT_EXPR:
5470 case POSTDECREMENT_EXPR:
5472 error ("invalid operation on uninstantiated type");
5473 return error_mark_node;
5475 case TRUTH_AND_EXPR:
5477 case TRUTH_XOR_EXPR:
5484 case TRUTH_ANDIF_EXPR:
5485 case TRUTH_ORIF_EXPR:
5486 case TRUTH_NOT_EXPR:
5488 error ("not enough type information");
5489 return error_mark_node;
5492 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
5495 error ("not enough type information");
5496 return error_mark_node;
5498 TREE_OPERAND (rhs, 1)
5499 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5500 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5501 return error_mark_node;
5502 TREE_OPERAND (rhs, 2)
5503 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
5504 if (TREE_OPERAND (rhs, 2) == error_mark_node)
5505 return error_mark_node;
5507 TREE_TYPE (rhs) = lhstype;
5511 TREE_OPERAND (rhs, 1)
5512 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5513 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5514 return error_mark_node;
5516 TREE_TYPE (rhs) = lhstype;
5520 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5522 case ENTRY_VALUE_EXPR:
5523 my_friendly_abort (184);
5524 return error_mark_node;
5527 return error_mark_node;
5530 my_friendly_abort (185);
5531 return error_mark_node;
5535 /* Return the name of the virtual function pointer field
5536 (as an IDENTIFIER_NODE) for the given TYPE. Note that
5537 this may have to look back through base types to find the
5538 ultimate field name. (For single inheritance, these could
5539 all be the same name. Who knows for multiple inheritance). */
5542 get_vfield_name (type)
5545 tree binfo = TYPE_BINFO (type);
5548 while (BINFO_BASETYPES (binfo)
5549 && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
5550 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
5551 binfo = BINFO_BASETYPE (binfo, 0);
5553 type = BINFO_TYPE (binfo);
5554 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
5555 + TYPE_NAME_LENGTH (type) + 2);
5556 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
5557 return get_identifier (buf);
5561 print_class_statistics ()
5563 #ifdef GATHER_STATISTICS
5564 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
5565 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
5566 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
5567 n_build_method_call, n_inner_fields_searched);
5570 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
5571 n_vtables, n_vtable_searches);
5572 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
5573 n_vtable_entries, n_vtable_elems);
5578 /* Push an obstack which is sufficiently long-lived to hold such class
5579 decls that may be cached in the previous_class_values list. The
5580 effect is undone by pop_obstacks. */
5583 push_cache_obstack ()
5585 static int cache_obstack_initialized;
5587 if (!cache_obstack_initialized)
5589 gcc_obstack_init (&class_cache_obstack);
5590 class_cache_firstobj
5591 = (char*) obstack_finish (&class_cache_obstack);
5592 cache_obstack_initialized = 1;
5595 push_obstacks_nochange ();
5596 current_obstack = &class_cache_obstack;
5599 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
5600 according to [class]:
5601 The class-name is also inserted
5602 into the scope of the class itself. For purposes of access checking,
5603 the inserted class name is treated as if it were a public member name. */
5606 build_self_reference ()
5608 tree name = constructor_name (current_class_type);
5609 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
5612 DECL_NONLOCAL (value) = 1;
5613 DECL_CONTEXT (value) = current_class_type;
5614 DECL_CLASS_CONTEXT (value) = current_class_type;
5615 DECL_ARTIFICIAL (value) = 1;
5617 saved_cas = current_access_specifier;
5618 current_access_specifier = access_public_node;
5619 finish_member_declaration (value);
5620 current_access_specifier = saved_cas;
5623 /* Returns 1 if TYPE contains only padding bytes. */
5626 is_empty_class (type)
5631 if (type == error_mark_node)
5634 if (! IS_AGGR_TYPE (type))
5638 return CLASSTYPE_SIZE (type) == integer_zero_node;
5640 if (TYPE_BINFO_BASETYPES (type))
5642 t = TYPE_FIELDS (type);
5643 while (t && TREE_CODE (t) != FIELD_DECL)
5645 return (t == NULL_TREE);
5648 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
5649 a *_TYPE node. NODE can also be a local class. */
5652 get_enclosing_class (type)
5657 while (node && TREE_CODE (node) != NAMESPACE_DECL)
5659 switch (TREE_CODE_CLASS (TREE_CODE (node)))
5662 node = DECL_CONTEXT (node);
5668 node = TYPE_CONTEXT (node);
5672 my_friendly_abort (0);
5678 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
5681 is_base_of_enclosing_class (base, type)
5686 if (get_binfo (base, type, 0))
5689 type = get_enclosing_class (type);
5694 /* Note that NAME was looked up while the current class was being
5695 defined and that the result of that lookup was DECL. */
5698 maybe_note_name_used_in_class (name, decl)
5702 splay_tree names_used;
5704 /* If we're not defining a class, there's nothing to do. */
5705 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
5708 /* If there's already a binding for this NAME, then we don't have
5709 anything to worry about. */
5710 if (IDENTIFIER_CLASS_VALUE (name))
5713 if (!current_class_stack[current_class_depth - 1].names_used)
5714 current_class_stack[current_class_depth - 1].names_used
5715 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
5716 names_used = current_class_stack[current_class_depth - 1].names_used;
5718 splay_tree_insert (names_used,
5719 (splay_tree_key) name,
5720 (splay_tree_value) decl);
5723 /* Note that NAME was declared (as DECL) in the current class. Check
5724 to see that the declaration is legal. */
5727 note_name_declared_in_class (name, decl)
5731 splay_tree names_used;
5734 /* Look to see if we ever used this name. */
5736 = current_class_stack[current_class_depth - 1].names_used;
5740 n = splay_tree_lookup (names_used, (splay_tree_key) name);
5743 /* [basic.scope.class]
5745 A name N used in a class S shall refer to the same declaration
5746 in its context and when re-evaluated in the completed scope of
5748 cp_error ("declaration of `%#D'", decl);
5749 cp_error_at ("changes meaning of `%s' from `%+#D'",
5750 IDENTIFIER_POINTER (DECL_NAME (decl)),