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 /* The following two can be derived from the previous one */
75 tree current_class_name; /* IDENTIFIER_NODE: name of current class */
76 tree current_class_type; /* _TYPE: the type of the current class */
77 tree current_access_specifier;
78 tree previous_class_type; /* _TYPE: the previous type that was a class */
79 tree previous_class_values; /* TREE_LIST: copy of the class_shadowed list
80 when leaving an outermost class scope. */
82 /* The obstack on which the cached class declarations are kept. */
83 static struct obstack class_cache_obstack;
84 /* The first object allocated on that obstack. We can use
85 obstack_free with tis value to free the entire obstack. */
86 char *class_cache_firstobj;
90 static tree get_vfield_name PROTO((tree));
91 static void finish_struct_anon PROTO((tree));
92 static tree build_vbase_pointer PROTO((tree, tree));
93 static tree build_vtable_entry PROTO((tree, tree));
94 static tree get_vtable_name PROTO((tree));
95 static tree get_derived_offset PROTO((tree, tree));
96 static tree get_basefndecls PROTO((tree, tree));
97 static void set_rtti_entry PROTO((tree, tree, tree));
98 static tree build_vtable PROTO((tree, tree));
99 static void prepare_fresh_vtable PROTO((tree, tree));
100 static void fixup_vtable_deltas1 PROTO((tree, tree));
101 static void fixup_vtable_deltas PROTO((tree, int, tree));
102 static void finish_vtbls PROTO((tree, int, tree));
103 static void modify_vtable_entry PROTO((tree, tree, tree));
104 static tree get_vtable_entry_n PROTO((tree, unsigned HOST_WIDE_INT));
105 static void add_virtual_function PROTO((tree *, tree *, int *, tree, tree));
106 static tree delete_duplicate_fields_1 PROTO((tree, tree));
107 static void delete_duplicate_fields PROTO((tree));
108 static void finish_struct_bits PROTO((tree, int));
109 static int alter_access PROTO((tree, tree, tree, tree));
110 static void handle_using_decl PROTO((tree, tree, tree, tree));
111 static int overrides PROTO((tree, tree));
112 static int strictly_overrides PROTO((tree, tree));
113 static void merge_overrides PROTO((tree, tree, int, tree));
114 static void override_one_vtable PROTO((tree, tree, tree));
115 static void mark_overriders PROTO((tree, tree));
116 static void check_for_override PROTO((tree, tree));
117 static tree get_class_offset_1 PROTO((tree, tree, tree, tree, tree));
118 static tree get_class_offset PROTO((tree, tree, tree, tree));
119 static void modify_one_vtable PROTO((tree, tree, tree));
120 static void modify_all_vtables PROTO((tree, tree));
121 static void modify_all_direct_vtables PROTO((tree, int, tree, tree));
122 static void modify_all_indirect_vtables PROTO((tree, int, int, tree, tree));
123 static int finish_base_struct PROTO((tree, struct base_info *));
124 static void finish_struct_methods PROTO((tree));
125 static void maybe_warn_about_overly_private_class PROTO ((tree));
126 static int field_decl_cmp PROTO ((const tree *, const tree *));
127 static int method_name_cmp PROTO ((const tree *, const tree *));
128 static tree make_method_vec PROTO((int));
129 static void free_method_vec PROTO((tree));
130 static tree add_implicitly_declared_members PROTO((tree, int, int, int));
131 static tree fixed_type_or_null PROTO((tree, int *));
132 static tree resolve_address_of_overloaded_function PROTO((tree, tree, int,
134 static void build_vtable_entry_ref PROTO((tree, tree, tree));
135 static tree build_vtable_entry_for_fn PROTO((tree, tree));
136 static tree build_vtbl_initializer PROTO((tree));
137 static int count_fields PROTO((tree));
138 static int add_fields_to_vec PROTO((tree, tree, int));
140 /* Way of stacking language names. */
141 tree *current_lang_base, *current_lang_stack;
142 int current_lang_stacksize;
144 /* Names of languages we recognize. */
145 tree lang_name_c, lang_name_cplusplus, lang_name_java;
146 tree current_lang_name;
148 /* When layout out an aggregate type, the size of the
149 basetypes (virtual and non-virtual) is passed to layout_record
151 static tree base_layout_decl;
153 /* Constants used for access control. */
154 tree access_default_node; /* 0 */
155 tree access_public_node; /* 1 */
156 tree access_protected_node; /* 2 */
157 tree access_private_node; /* 3 */
158 tree access_default_virtual_node; /* 4 */
159 tree access_public_virtual_node; /* 5 */
160 tree access_protected_virtual_node; /* 6 */
161 tree access_private_virtual_node; /* 7 */
163 /* Variables shared between class.c and call.c. */
165 #ifdef GATHER_STATISTICS
167 int n_vtable_entries = 0;
168 int n_vtable_searches = 0;
169 int n_vtable_elems = 0;
170 int n_convert_harshness = 0;
171 int n_compute_conversion_costs = 0;
172 int n_build_method_call = 0;
173 int n_inner_fields_searched = 0;
176 /* Virtual baseclass things. */
179 build_vbase_pointer (exp, type)
183 FORMAT_VBASE_NAME (name, type);
185 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
189 /* Is the type of the EXPR, the complete type of the object?
190 If we are going to be wrong, we must be conservative, and return 0. */
193 complete_type_p (expr)
196 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
199 switch (TREE_CODE (expr))
206 expr = TREE_OPERAND (expr, 0);
210 if (! TREE_HAS_CONSTRUCTOR (expr))
212 /* fall through... */
215 if (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
216 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr)))
217 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
219 /* fall through... */
222 if (IS_AGGR_TYPE (TREE_TYPE (expr))
223 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
225 /* fall through... */
236 /* Build multi-level access to EXPR using hierarchy path PATH.
237 CODE is PLUS_EXPR if we are going with the grain,
238 and MINUS_EXPR if we are not (in which case, we cannot traverse
239 virtual baseclass links).
241 TYPE is the type we want this path to have on exit.
243 NONNULL is non-zero if we know (for any reason) that EXPR is
244 not, in fact, zero. */
247 build_vbase_path (code, type, expr, path, nonnull)
249 tree type, expr, path;
252 register int changed = 0;
253 tree last = NULL_TREE, last_virtual = NULL_TREE;
255 tree null_expr = 0, nonnull_expr;
257 tree offset = integer_zero_node;
259 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
260 return build1 (NOP_EXPR, type, expr);
262 /* If -fthis-is-variable, we might have set nonnull incorrectly. We
263 don't care enough to get this right, so just clear it. */
264 if (flag_this_is_variable > 0)
267 /* We could do better if we had additional logic to convert back to the
268 unconverted type (the static type of the complete object), and then
269 convert back to the type we want. Until that is done, we only optimize
270 if the complete type is the same type as expr has. */
271 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
273 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
274 expr = save_expr (expr);
277 if (BINFO_INHERITANCE_CHAIN (path))
278 path = reverse_path (path);
280 basetype = BINFO_TYPE (path);
284 if (TREE_VIA_VIRTUAL (path))
286 last_virtual = BINFO_TYPE (path);
287 if (code == PLUS_EXPR)
289 changed = ! fixed_type_p;
295 /* We already check for ambiguous things in the caller, just
299 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
300 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
302 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
303 nonnull_expr = build_vbase_pointer (ind, last_virtual);
305 && TREE_CODE (type) == POINTER_TYPE
306 && null_expr == NULL_TREE)
308 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
309 expr = build (COND_EXPR, build_pointer_type (last_virtual),
310 build (EQ_EXPR, boolean_type_node, expr,
312 null_expr, nonnull_expr);
315 /* else we'll figure out the offset below. */
317 /* Happens in the case of parse errors. */
318 if (nonnull_expr == error_mark_node)
319 return error_mark_node;
323 cp_error ("cannot cast up from virtual baseclass `%T'",
325 return error_mark_node;
329 path = BINFO_INHERITANCE_CHAIN (path);
331 /* LAST is now the last basetype assoc on the path. */
333 /* A pointer to a virtual base member of a non-null object
334 is non-null. Therefore, we only need to test for zeroness once.
335 Make EXPR the canonical expression to deal with here. */
338 TREE_OPERAND (expr, 2) = nonnull_expr;
339 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
340 = TREE_TYPE (nonnull_expr);
345 /* If we go through any virtual base pointers, make sure that
346 casts to BASETYPE from the last virtual base class use
347 the right value for BASETYPE. */
350 tree intype = TREE_TYPE (TREE_TYPE (expr));
351 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
353 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
354 offset = BINFO_OFFSET (binfo);
361 offset = BINFO_OFFSET (binfo_member (last_virtual,
362 CLASSTYPE_VBASECLASSES (basetype)));
363 offset = size_binop (PLUS_EXPR, offset, BINFO_OFFSET (last));
366 offset = BINFO_OFFSET (last);
369 if (TREE_INT_CST_LOW (offset))
371 /* Bash types to make the backend happy. */
372 offset = cp_convert (type, offset);
374 /* This shouldn't be necessary. (mrs) */
375 expr = build1 (NOP_EXPR, type, expr);
378 /* If expr might be 0, we need to preserve that zeroness. */
382 TREE_TYPE (null_expr) = type;
384 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
385 if (TREE_SIDE_EFFECTS (expr))
386 expr = save_expr (expr);
388 return build (COND_EXPR, type,
389 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
391 build (code, type, expr, offset));
393 else return build (code, type, expr, offset);
396 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
397 be used multiple times in initialization of multiple inheritance. */
400 TREE_TYPE (expr) = type;
404 return build1 (NOP_EXPR, type, expr);
407 /* Virtual function things. */
409 /* Build an entry in the virtual function table.
410 DELTA is the offset for the `this' pointer.
411 PFN is an ADDR_EXPR containing a pointer to the virtual function.
412 Note that the index (DELTA2) in the virtual function table
416 build_vtable_entry (delta, pfn)
419 if (flag_vtable_thunks)
421 HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta);
422 if (idelta && ! DECL_ABSTRACT_VIRTUAL_P (TREE_OPERAND (pfn, 0)))
424 pfn = build1 (ADDR_EXPR, vtable_entry_type,
425 make_thunk (pfn, idelta));
426 TREE_READONLY (pfn) = 1;
427 TREE_CONSTANT (pfn) = 1;
429 #ifdef GATHER_STATISTICS
430 n_vtable_entries += 1;
436 extern int flag_huge_objects;
437 tree elems = expr_tree_cons (NULL_TREE, delta,
438 expr_tree_cons (NULL_TREE, integer_zero_node,
439 build_expr_list (NULL_TREE, pfn)));
440 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
442 /* DELTA used to be constructed by `size_int' and/or size_binop,
443 which caused overflow problems when it was negative. That should
446 if (! int_fits_type_p (delta, delta_type_node))
448 if (flag_huge_objects)
449 sorry ("object size exceeds built-in limit for virtual function table implementation");
451 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
454 TREE_CONSTANT (entry) = 1;
455 TREE_STATIC (entry) = 1;
456 TREE_READONLY (entry) = 1;
458 #ifdef GATHER_STATISTICS
459 n_vtable_entries += 1;
466 /* Build a vtable entry for FNDECL. DELTA is the amount by which we
467 must adjust the this pointer when calling F. */
470 build_vtable_entry_for_fn (delta, fndecl)
476 /* Take the address of the function, considering it to be of an
477 appropriate generic type. */
478 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
479 /* The address of a function can't change. */
480 TREE_CONSTANT (pfn) = 1;
481 /* Now build the vtable entry itself. */
482 return build_vtable_entry (delta, pfn);
485 /* We want to give the assembler the vtable identifier as well as
486 the offset to the function pointer. So we generate
488 __asm__ __volatile__ (".vtable_entry %c0, %c1"
489 : : "s"(&class_vtable),
490 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
493 build_vtable_entry_ref (basetype, vtbl, idx)
494 tree basetype, vtbl, idx;
496 static char asm_stmt[] = ".vtable_entry %c0, %c1";
499 s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
500 s = build_tree_list (build_string (1, "s"), s);
502 i = build_array_ref (vtbl, idx);
503 if (!flag_vtable_thunks)
504 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
505 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
506 i2 = build_array_ref (vtbl, build_int_2(0,0));
507 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
508 i = build_binary_op (MINUS_EXPR, i, i2);
509 i = build_tree_list (build_string (1, "i"), i);
511 expand_asm_operands (build_string (sizeof(asm_stmt)-1, asm_stmt),
512 NULL_TREE, chainon (s, i), NULL_TREE, 1, NULL, 0);
515 /* Given an object INSTANCE, return an expression which yields the
516 virtual function vtable element corresponding to INDEX. There are
517 many special cases for INSTANCE which we take care of here, mainly
518 to avoid creating extra tree nodes when we don't have to. */
521 build_vtbl_ref (instance, idx)
525 tree basetype = TREE_TYPE (instance);
527 if (TREE_CODE (basetype) == REFERENCE_TYPE)
528 basetype = TREE_TYPE (basetype);
530 if (instance == current_class_ref)
531 vtbl = build_vfield_ref (instance, basetype);
536 /* Try to figure out what a reference refers to, and
537 access its virtual function table directly. */
538 tree ref = NULL_TREE;
540 if (TREE_CODE (instance) == INDIRECT_REF
541 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
542 ref = TREE_OPERAND (instance, 0);
543 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
546 if (ref && TREE_CODE (ref) == VAR_DECL
547 && DECL_INITIAL (ref))
549 tree init = DECL_INITIAL (ref);
551 while (TREE_CODE (init) == NOP_EXPR
552 || TREE_CODE (init) == NON_LVALUE_EXPR)
553 init = TREE_OPERAND (init, 0);
554 if (TREE_CODE (init) == ADDR_EXPR)
556 init = TREE_OPERAND (init, 0);
557 if (IS_AGGR_TYPE (TREE_TYPE (init))
558 && (TREE_CODE (init) == PARM_DECL
559 || TREE_CODE (init) == VAR_DECL))
565 if (IS_AGGR_TYPE (TREE_TYPE (instance))
566 && (TREE_CODE (instance) == RESULT_DECL
567 || TREE_CODE (instance) == PARM_DECL
568 || TREE_CODE (instance) == VAR_DECL))
569 vtbl = TYPE_BINFO_VTABLE (basetype);
571 vtbl = build_vfield_ref (instance, basetype);
574 assemble_external (vtbl);
577 build_vtable_entry_ref (basetype, vtbl, idx);
579 aref = build_array_ref (vtbl, idx);
584 /* Given an object INSTANCE, return an expression which yields the
585 virtual function corresponding to INDEX. There are many special
586 cases for INSTANCE which we take care of here, mainly to avoid
587 creating extra tree nodes when we don't have to. */
590 build_vfn_ref (ptr_to_instptr, instance, idx)
591 tree *ptr_to_instptr, instance;
594 tree aref = build_vtbl_ref (instance, idx);
596 /* When using thunks, there is no extra delta, and we get the pfn
598 if (flag_vtable_thunks)
603 /* Save the intermediate result in a SAVE_EXPR so we don't have to
604 compute each component of the virtual function pointer twice. */
605 if (TREE_CODE (aref) == INDIRECT_REF)
606 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
609 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
611 cp_convert (ptrdiff_type_node,
612 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
615 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
618 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
619 for the given TYPE. */
622 get_vtable_name (type)
625 tree type_id = build_typename_overload (type);
626 char *buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
627 + IDENTIFIER_LENGTH (type_id) + 2);
628 const char *ptr = IDENTIFIER_POINTER (type_id);
630 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
632 /* We don't take off the numbers; prepare_fresh_vtable uses the
633 DECL_ASSEMBLER_NAME for the type, which includes the number
634 in `3foo'. If we were to pull them off here, we'd end up with
635 something like `_vt.foo.3bar', instead of a uniform definition. */
636 while (ptr[i] >= '0' && ptr[i] <= '9')
639 sprintf (buf, VTABLE_NAME_FORMAT, ptr+i);
640 return get_identifier (buf);
643 /* Return the offset to the main vtable for a given base BINFO. */
646 get_vfield_offset (binfo)
650 = size_binop (FLOOR_DIV_EXPR,
651 DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))),
652 size_int (BITS_PER_UNIT));
653 tmp = convert (sizetype, tmp);
654 return size_binop (PLUS_EXPR, tmp, BINFO_OFFSET (binfo));
657 /* Get the offset to the start of the original binfo that we derived
658 this binfo from. If we find TYPE first, return the offset only
659 that far. The shortened search is useful because the this pointer
660 on method calling is expected to point to a DECL_CONTEXT (fndecl)
661 object, and not a baseclass of it. */
664 get_derived_offset (binfo, type)
667 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
670 while (BINFO_BASETYPES (binfo)
671 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
673 tree binfos = BINFO_BASETYPES (binfo);
674 if (BINFO_TYPE (binfo) == type)
676 binfo = TREE_VEC_ELT (binfos, i);
678 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
679 return size_binop (MINUS_EXPR, offset1, offset2);
682 /* Update the rtti info for this class. */
685 set_rtti_entry (virtuals, offset, type)
686 tree virtuals, offset, type;
690 if (CLASSTYPE_COM_INTERFACE (type))
694 fn = get_tinfo_fn (type);
696 /* If someone tries to get RTTI information for a type compiled
697 without RTTI, they're out of luck. By calling __pure_virtual
698 in this case, we give a small clue as to what went wrong. We
699 could consider having a __no_typeinfo function as well, for a
700 more specific hint. */
703 if (flag_vtable_thunks)
705 /* The first slot holds the offset. */
706 TREE_PURPOSE (virtuals) = offset;
708 /* The next node holds the function. */
709 virtuals = TREE_CHAIN (virtuals);
710 offset = integer_zero_node;
713 /* This slot holds the function to call. */
714 TREE_PURPOSE (virtuals) = offset;
715 TREE_VALUE (virtuals) = fn;
718 /* Build a virtual function for type TYPE.
719 If BINFO is non-NULL, build the vtable starting with the initial
720 approximation that it is the same as the one which is the head of
721 the association list. */
724 build_vtable (binfo, type)
727 tree name = get_vtable_name (type);
734 virtuals = copy_list (BINFO_VIRTUALS (binfo));
735 decl = build_lang_decl (VAR_DECL, name,
736 TREE_TYPE (BINFO_VTABLE (binfo)));
738 /* Now do rtti stuff. */
739 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
740 offset = ssize_binop (MINUS_EXPR, integer_zero_node, offset);
741 set_rtti_entry (virtuals, offset, type);
745 virtuals = NULL_TREE;
746 decl = build_lang_decl (VAR_DECL, name, void_type_node);
749 #ifdef GATHER_STATISTICS
751 n_vtable_elems += list_length (virtuals);
754 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
755 import_export_vtable (decl, type, 0);
757 decl = pushdecl_top_level (decl);
758 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
759 /* Initialize the association list for this type, based
760 on our first approximation. */
761 TYPE_BINFO_VTABLE (type) = decl;
762 TYPE_BINFO_VIRTUALS (type) = virtuals;
764 DECL_ARTIFICIAL (decl) = 1;
765 TREE_STATIC (decl) = 1;
766 #ifndef WRITABLE_VTABLES
767 /* Make them READONLY by default. (mrs) */
768 TREE_READONLY (decl) = 1;
770 /* At one time the vtable info was grabbed 2 words at a time. This
771 fails on sparc unless you have 8-byte alignment. (tiemann) */
772 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
775 DECL_VIRTUAL_P (decl) = 1;
776 DECL_CONTEXT (decl) = type;
778 binfo = TYPE_BINFO (type);
779 SET_BINFO_NEW_VTABLE_MARKED (binfo);
783 extern tree signed_size_zero_node;
785 /* Give TYPE a new virtual function table which is initialized
786 with a skeleton-copy of its original initialization. The only
787 entry that changes is the `delta' entry, so we can really
788 share a lot of structure.
790 FOR_TYPE is the derived type which caused this table to
793 BINFO is the type association which provided TYPE for FOR_TYPE.
795 The order in which vtables are built (by calling this function) for
796 an object must remain the same, otherwise a binary incompatibility
800 prepare_fresh_vtable (binfo, for_type)
801 tree binfo, for_type;
804 tree orig_decl = BINFO_VTABLE (binfo);
817 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
819 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
820 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
822 /* We know that the vtable that we are going to create doesn't exist
823 yet in the global namespace, and when we finish, it will be
824 pushed into the global namespace. In complex MI hierarchies, we
825 have to loop while the name we are thinking of adding is globally
826 defined, adding more name components to the vtable name as we
827 loop, until the name is unique. This is because in complex MI
828 cases, we might have the same base more than once. This means
829 that the order in which this function is called for vtables must
830 remain the same, otherwise binary compatibility can be
835 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
839 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
841 buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT) + strlen (buf1) + 1);
842 sprintf (buf, VTABLE_NAME_FORMAT, buf1);
843 name = get_identifier (buf);
845 /* If this name doesn't clash, then we can use it, otherwise
846 we add more to the name until it is unique. */
848 if (! IDENTIFIER_GLOBAL_VALUE (name))
851 /* Set values for next loop through, if the name isn't unique. */
853 path = BINFO_INHERITANCE_CHAIN (path);
855 /* We better not run out of stuff to make it unique. */
856 my_friendly_assert (path != NULL_TREE, 368);
858 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
860 if (for_type == basetype)
862 /* If we run out of basetypes in the path, we have already
863 found created a vtable with that name before, we now
864 resort to tacking on _%d to distinguish them. */
866 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
867 buf1 = (char *) alloca (i);
869 sprintf (buf1, "%s%c%s%c%d",
870 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
872 buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
873 + strlen (buf1) + 1);
874 sprintf (buf, VTABLE_NAME_FORMAT, buf1);
875 name = get_identifier (buf);
877 /* If this name doesn't clash, then we can use it,
878 otherwise we add something different to the name until
880 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
882 /* Hey, they really like MI don't they? Increase the 3
883 above to 6, and the 999 to 999999. :-) */
884 my_friendly_assert (j <= 999, 369);
889 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
890 new_buf2 = (char *) alloca (i);
891 sprintf (new_buf2, "%s%c%s",
892 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
896 new_decl = build_lang_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
897 /* Remember which class this vtable is really for. */
898 DECL_CONTEXT (new_decl) = for_type;
900 DECL_ARTIFICIAL (new_decl) = 1;
901 TREE_STATIC (new_decl) = 1;
902 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
903 DECL_VIRTUAL_P (new_decl) = 1;
904 #ifndef WRITABLE_VTABLES
905 /* Make them READONLY by default. (mrs) */
906 TREE_READONLY (new_decl) = 1;
908 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
910 /* Make fresh virtual list, so we can smash it later. */
911 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
913 if (TREE_VIA_VIRTUAL (binfo))
915 tree binfo1 = binfo_member (BINFO_TYPE (binfo),
916 CLASSTYPE_VBASECLASSES (for_type));
918 /* XXX - This should never happen, if it does, the caller should
919 ensure that the binfo is from for_type's binfos, not from any
920 base type's. We can remove all this code after a while. */
922 warning ("internal inconsistency: binfo offset error for rtti");
924 offset = BINFO_OFFSET (binfo1);
927 offset = BINFO_OFFSET (binfo);
929 set_rtti_entry (BINFO_VIRTUALS (binfo),
930 ssize_binop (MINUS_EXPR, integer_zero_node, offset),
933 #ifdef GATHER_STATISTICS
935 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
938 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
939 import_export_vtable (new_decl, for_type, 0);
941 if (TREE_VIA_VIRTUAL (binfo))
942 my_friendly_assert (binfo == binfo_member (BINFO_TYPE (binfo),
943 CLASSTYPE_VBASECLASSES (current_class_type)),
945 SET_BINFO_NEW_VTABLE_MARKED (binfo);
949 /* Access the virtual function table entry that logically
950 contains BASE_FNDECL. VIRTUALS is the virtual function table's
951 initializer. We can run off the end, when dealing with virtual
952 destructors in MI situations, return NULL_TREE in that case. */
955 get_vtable_entry (virtuals, base_fndecl)
956 tree virtuals, base_fndecl;
958 unsigned HOST_WIDE_INT n = (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
959 ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
960 & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1))
961 : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
963 #ifdef GATHER_STATISTICS
964 n_vtable_searches += n;
967 while (n > 0 && virtuals)
970 virtuals = TREE_CHAIN (virtuals);
976 /* Change the offset for the FNDECL entry to NEW_OFFSET. Also update
977 DECL_VINDEX (FNDECL). */
980 modify_vtable_entry (old_entry_in_list, new_offset, fndecl)
981 tree old_entry_in_list, new_offset, fndecl;
983 tree base_fndecl = TREE_VALUE (old_entry_in_list);
985 /* Update the entry. */
986 TREE_PURPOSE (old_entry_in_list) = new_offset;
987 TREE_VALUE (old_entry_in_list) = fndecl;
989 /* Now assign virtual dispatch information, if unset. We can
990 dispatch this, through any overridden base function. */
991 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
993 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
994 DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
998 /* Access the virtual function table entry N. VIRTUALS is the virtual
999 function table's initializer. */
1002 get_vtable_entry_n (virtuals, n)
1004 unsigned HOST_WIDE_INT n;
1009 virtuals = TREE_CHAIN (virtuals);
1014 /* Add a virtual function to all the appropriate vtables for the class
1015 T. DECL_VINDEX(X) should be error_mark_node, if we want to
1016 allocate a new slot in our table. If it is error_mark_node, we
1017 know that no other function from another vtable is overridden by X.
1018 HAS_VIRTUAL keeps track of how many virtuals there are in our main
1019 vtable for the type, and we build upon the PENDING_VIRTUALS list
1023 add_virtual_function (pv, phv, has_virtual, fndecl, t)
1027 tree t; /* Structure type. */
1029 tree pending_virtuals = *pv;
1030 tree pending_hard_virtuals = *phv;
1033 if (current_class_type == 0)
1034 cp_warning ("internal problem, current_class_type is zero when adding `%D', please report",
1036 if (current_class_type && t != current_class_type)
1037 cp_warning ("internal problem, current_class_type differs when adding `%D', please report",
1041 /* If the virtual function is a redefinition of a prior one,
1042 figure out in which base class the new definition goes,
1043 and if necessary, make a fresh virtual function table
1044 to hold that entry. */
1045 if (DECL_VINDEX (fndecl) == error_mark_node)
1047 /* We remember that this was the base sub-object for rtti. */
1048 CLASSTYPE_RTTI (t) = t;
1050 /* If we are using thunks, use two slots at the front, one
1051 for the offset pointer, one for the tdesc pointer.
1052 For ARM-style vtables, use the same slot for both. */
1053 if (*has_virtual == 0 && ! CLASSTYPE_COM_INTERFACE (t))
1055 if (flag_vtable_thunks)
1061 /* Build a new INT_CST for this DECL_VINDEX. */
1063 static tree index_table[256];
1065 /* We skip a slot for the offset/tdesc entry. */
1066 int i = (*has_virtual)++;
1068 if (i >= 256 || index_table[i] == 0)
1070 idx = build_int_2 (i, 0);
1072 index_table[i] = idx;
1075 idx = index_table[i];
1077 /* Now assign virtual dispatch information. */
1078 DECL_VINDEX (fndecl) = idx;
1079 DECL_CONTEXT (fndecl) = t;
1081 /* Save the state we've computed on the PENDING_VIRTUALS list. */
1082 pending_virtuals = tree_cons (integer_zero_node,
1086 /* Might already be INTEGER_CST if declared twice in class. We will
1087 give error later or we've already given it. */
1088 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1090 /* Need an entry in some other virtual function table.
1091 Deal with this after we have laid out our virtual base classes. */
1092 pending_hard_virtuals = temp_tree_cons (NULL_TREE,
1094 pending_hard_virtuals);
1096 *pv = pending_virtuals;
1097 *phv = pending_hard_virtuals;
1100 /* Obstack on which to build the vector of class methods. */
1101 struct obstack class_obstack;
1102 extern struct obstack *current_obstack;
1104 /* These are method vectors that were too small for the number of
1105 methods in some class, and so were abandoned. */
1106 static tree free_method_vecs;
1108 /* Returns a method vector with enough room for N methods. N should
1109 be a power of two. */
1118 for (t = &free_method_vecs; *t; t = &(TREE_CHAIN (*t)))
1119 /* Note that we don't use >= n here because we don't want to
1120 allocate a very large vector where it isn't needed. */
1121 if (TREE_VEC_LENGTH (*t) == n)
1124 *t = TREE_CHAIN (new_vec);
1125 TREE_CHAIN (new_vec) = NULL_TREE;
1126 bzero ((PTR) &TREE_VEC_ELT (new_vec, 0), n * sizeof (tree));
1130 new_vec = make_tree_vec (n);
1134 /* Free the method vector VEC. */
1137 free_method_vec (vec)
1140 TREE_CHAIN (vec) = free_method_vecs;
1141 free_method_vecs = vec;
1144 /* Add method METHOD to class TYPE.
1146 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1147 the class type where the method should be added. */
1150 add_method (type, fields, method)
1151 tree type, *fields, method;
1153 push_permanent_obstack ();
1155 /* Setting the DECL_CONTEXT and DECL_CLASS_CONTEXT here is probably
1157 DECL_CONTEXT (method) = type;
1158 DECL_CLASS_CONTEXT (method) = type;
1160 if (fields && *fields)
1161 *fields = build_overload (method, *fields);
1168 if (!CLASSTYPE_METHOD_VEC (type))
1169 /* Make a new method vector. We start with 8 entries. We must
1170 allocate at least two (for constructors and destructors), and
1171 we're going to end up with an assignment operator at some
1174 We could use a TREE_LIST for now, and convert it to a
1175 TREE_VEC in finish_struct, but we would probably waste more
1176 memory making the links in the list than we would by
1177 over-allocating the size of the vector here. Furthermore,
1178 we would complicate all the code that expects this to be a
1179 vector. We keep a free list of vectors that we outgrew so
1180 that we don't really waste any memory. */
1181 CLASSTYPE_METHOD_VEC (type) = make_method_vec (8);
1183 method_vec = CLASSTYPE_METHOD_VEC (type);
1184 len = TREE_VEC_LENGTH (method_vec);
1186 if (DECL_NAME (method) == constructor_name (type))
1187 /* A new constructor or destructor. Constructors go in
1188 slot 0; destructors go in slot 1. */
1189 slot = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
1192 /* See if we already have an entry with this name. */
1193 for (slot = 2; slot < len; ++slot)
1194 if (!TREE_VEC_ELT (method_vec, slot)
1195 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1197 == DECL_NAME (method)))
1202 /* We need a bigger method vector. */
1203 tree new_vec = make_method_vec (2 * len);
1204 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1205 (PTR) &TREE_VEC_ELT (new_vec, 0),
1206 len * sizeof (tree));
1207 free_method_vec (method_vec);
1209 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1212 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1214 /* Type conversion operators have to come before
1215 ordinary methods; add_conversions depends on this to
1216 speed up looking for conversion operators. So, if
1217 necessary, we slide some of the vector elements up.
1218 In theory, this makes this algorithm O(N^2) but we
1219 don't expect many conversion operators. */
1220 for (slot = 2; slot < len; ++slot)
1222 tree fn = TREE_VEC_ELT (method_vec, slot);
1225 /* There are no more entries in the vector, so we
1226 can insert the new conversion operator here. */
1229 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1230 /* We can insert the new function right at the
1235 if (!TREE_VEC_ELT (method_vec, slot))
1236 /* There is nothing in the Ith slot, so we can avoid
1241 /* We know the last slot in the vector is empty
1242 because we know that at this point there's room
1243 for a new function. */
1244 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1245 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1246 (len - slot - 1) * sizeof (tree));
1247 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1252 if (template_class_depth (type))
1253 /* TYPE is a template class. Don't issue any errors now; wait
1254 until instantiation time to complain. */
1260 /* Check to see if we've already got this method. */
1261 for (fns = TREE_VEC_ELT (method_vec, slot);
1263 fns = OVL_NEXT (fns))
1265 tree fn = OVL_CURRENT (fns);
1267 if (TREE_CODE (fn) != TREE_CODE (method))
1270 if (TREE_CODE (method) != TEMPLATE_DECL)
1272 /* [over.load] Member function declarations with the
1273 same name and the same parameter types cannot be
1274 overloaded if any of them is a static member
1275 function declaration. */
1276 if (DECL_STATIC_FUNCTION_P (fn)
1277 != DECL_STATIC_FUNCTION_P (method))
1279 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1280 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1282 if (! DECL_STATIC_FUNCTION_P (fn))
1283 parms1 = TREE_CHAIN (parms1);
1285 parms2 = TREE_CHAIN (parms2);
1287 if (compparms (parms1, parms2))
1288 cp_error ("`%#D' and `%#D' cannot be overloaded",
1292 /* Since this is an ordinary function in a
1293 non-template class, it's mangled name can be used
1294 as a unique identifier. This technique is only
1295 an optimization; we would get the same results if
1296 we just used decls_match here. */
1297 if (DECL_ASSEMBLER_NAME (fn)
1298 != DECL_ASSEMBLER_NAME (method))
1301 else if (!decls_match (fn, method))
1304 /* There has already been a declaration of this method
1305 or member template. */
1306 cp_error_at ("`%D' has already been declared in `%T'",
1309 /* We don't call duplicate_decls here to merge the
1310 declarations because that will confuse things if the
1311 methods have inline definitions. In particular, we
1312 will crash while processing the definitions. */
1317 /* Actually insert the new method. */
1318 TREE_VEC_ELT (method_vec, slot)
1319 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1321 /* Add the new binding. */
1322 if (!DECL_CONSTRUCTOR_P (method)
1323 && !DECL_DESTRUCTOR_P (method))
1324 push_class_level_binding (DECL_NAME (method),
1325 TREE_VEC_ELT (method_vec, slot));
1330 /* Subroutines of finish_struct. */
1332 /* Look through the list of fields for this struct, deleting
1333 duplicates as we go. This must be recursive to handle
1336 FIELD is the field which may not appear anywhere in FIELDS.
1337 FIELD_PTR, if non-null, is the starting point at which
1338 chained deletions may take place.
1339 The value returned is the first acceptable entry found
1342 Note that anonymous fields which are not of UNION_TYPE are
1343 not duplicates, they are just anonymous fields. This happens
1344 when we have unnamed bitfields, for example. */
1347 delete_duplicate_fields_1 (field, fields)
1352 if (DECL_NAME (field) == 0)
1354 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1357 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1358 fields = delete_duplicate_fields_1 (x, fields);
1363 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1365 if (DECL_NAME (x) == 0)
1367 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1369 TYPE_FIELDS (TREE_TYPE (x))
1370 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1371 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1374 fields = TREE_CHAIN (fields);
1376 TREE_CHAIN (prev) = TREE_CHAIN (x);
1381 if (DECL_NAME (field) == DECL_NAME (x))
1383 if (TREE_CODE (field) == CONST_DECL
1384 && TREE_CODE (x) == CONST_DECL)
1385 cp_error_at ("duplicate enum value `%D'", x);
1386 else if (TREE_CODE (field) == CONST_DECL
1387 || TREE_CODE (x) == CONST_DECL)
1388 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1390 else if (DECL_DECLARES_TYPE_P (field)
1391 && DECL_DECLARES_TYPE_P (x))
1393 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1395 cp_error_at ("duplicate nested type `%D'", x);
1397 else if (DECL_DECLARES_TYPE_P (field)
1398 || DECL_DECLARES_TYPE_P (x))
1400 /* Hide tag decls. */
1401 if ((TREE_CODE (field) == TYPE_DECL
1402 && DECL_ARTIFICIAL (field))
1403 || (TREE_CODE (x) == TYPE_DECL
1404 && DECL_ARTIFICIAL (x)))
1406 cp_error_at ("duplicate field `%D' (as type and non-type)",
1410 cp_error_at ("duplicate member `%D'", x);
1412 fields = TREE_CHAIN (fields);
1414 TREE_CHAIN (prev) = TREE_CHAIN (x);
1423 delete_duplicate_fields (fields)
1427 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1428 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1431 /* Change the access of FDECL to ACCESS in T. The access to FDECL is
1432 along the path given by BINFO. Return 1 if change was legit,
1433 otherwise return 0. */
1436 alter_access (t, binfo, fdecl, access)
1442 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1445 if (TREE_VALUE (elem) != access)
1447 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1448 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1450 error ("conflicting access specifications for field `%s', ignored",
1451 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1455 /* They're changing the access to the same thing they changed
1456 it to before. That's OK. */
1462 enforce_access (binfo, fdecl);
1463 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1469 /* Process the USING_DECL, which is a member of T. The METHOD_VEC, if
1470 non-NULL, is the methods of T. The FIELDS are the fields of T. */
1473 handle_using_decl (using_decl, t, method_vec, fields)
1479 tree ctype = DECL_INITIAL (using_decl);
1480 tree name = DECL_NAME (using_decl);
1482 = TREE_PRIVATE (using_decl) ? access_private_node
1483 : TREE_PROTECTED (using_decl) ? access_protected_node
1484 : access_public_node;
1486 tree flist = NULL_TREE;
1491 binfo = binfo_or_else (ctype, t);
1495 if (name == constructor_name (ctype)
1496 || name == constructor_name_full (ctype))
1498 cp_error_at ("using-declaration for constructor", using_decl);
1502 fdecl = lookup_member (binfo, name, 0, 0);
1506 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1510 /* Functions are represented as TREE_LIST, with the purpose
1511 being the type and the value the functions. Other members
1512 come as themselves. */
1513 if (TREE_CODE (fdecl) == TREE_LIST)
1514 /* Ignore base type this came from. */
1515 fdecl = TREE_VALUE (fdecl);
1517 if (TREE_CODE (fdecl) == OVERLOAD)
1519 /* We later iterate over all functions. */
1521 fdecl = OVL_FUNCTION (flist);
1524 name = DECL_NAME (fdecl);
1525 n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
1526 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); i++)
1527 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)))
1530 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1531 cp_error_at (" because of local method `%#D' with same name",
1532 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
1536 if (! DECL_LANG_SPECIFIC (fdecl))
1537 /* We don't currently handle DECL_ACCESS for TYPE_DECLs; just return. */
1540 for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
1541 if (DECL_NAME (tmp) == name)
1543 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1544 cp_error_at (" because of local field `%#D' with same name", tmp);
1548 /* Make type T see field decl FDECL with access ACCESS.*/
1553 if (alter_access (t, binfo, OVL_FUNCTION (flist),
1556 flist = OVL_CHAIN (flist);
1560 alter_access (t, binfo, fdecl, access);
1566 int max_has_virtual;
1570 char cant_have_default_ctor;
1571 char cant_have_const_ctor;
1572 char no_const_asn_ref;
1575 /* Record information about type T derived from its base classes.
1576 Store most of that information in T itself, and place the
1577 remaining information in the struct BASE_INFO.
1579 Propagate basetype offsets throughout the lattice. Note that the
1580 lattice topped by T is really a pair: it's a DAG that gives the
1581 structure of the derivation hierarchy, and it's a list of the
1582 virtual baseclasses that appear anywhere in the DAG. When a vbase
1583 type appears in the DAG, it's offset is 0, and it's children start
1584 their offsets from that point. When a vbase type appears in the list,
1585 its offset is the offset it has in the hierarchy, and its children's
1586 offsets include that offset in theirs.
1588 Returns the index of the first base class to have virtual functions,
1589 or -1 if no such base class. */
1592 finish_base_struct (t, b)
1594 struct base_info *b;
1596 tree binfos = TYPE_BINFO_BASETYPES (t);
1597 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1598 int first_vfn_base_index = -1;
1599 bzero ((char *) b, sizeof (struct base_info));
1601 for (i = 0; i < n_baseclasses; i++)
1603 tree base_binfo = TREE_VEC_ELT (binfos, i);
1604 tree basetype = BINFO_TYPE (base_binfo);
1606 /* Effective C++ rule 14. We only need to check TYPE_VIRTUAL_P
1607 here because the case of virtual functions but non-virtual
1608 dtor is handled in finish_struct_1. */
1609 if (warn_ecpp && ! TYPE_VIRTUAL_P (basetype)
1610 && TYPE_HAS_DESTRUCTOR (basetype))
1611 cp_warning ("base class `%#T' has a non-virtual destructor", basetype);
1613 /* If the type of basetype is incomplete, then
1614 we already complained about that fact
1615 (and we should have fixed it up as well). */
1616 if (TYPE_SIZE (basetype) == 0)
1619 /* The base type is of incomplete type. It is
1620 probably best to pretend that it does not
1622 if (i == n_baseclasses-1)
1623 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1624 TREE_VEC_LENGTH (binfos) -= 1;
1626 for (j = i; j+1 < n_baseclasses; j++)
1627 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1630 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1631 b->cant_have_const_ctor = 1;
1633 if (TYPE_HAS_CONSTRUCTOR (basetype)
1634 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1636 b->cant_have_default_ctor = 1;
1637 if (! TYPE_HAS_CONSTRUCTOR (t))
1639 cp_pedwarn ("base `%T' with only non-default constructor",
1641 cp_pedwarn ("in class without a constructor");
1645 if (TYPE_HAS_ASSIGN_REF (basetype)
1646 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1647 b->no_const_asn_ref = 1;
1649 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1650 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1651 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1652 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1654 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1655 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1656 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1658 if (CLASSTYPE_COM_INTERFACE (basetype))
1660 CLASSTYPE_COM_INTERFACE (t) = 1;
1663 ("COM interface type `%T' must be the leftmost base class",
1666 else if (CLASSTYPE_COM_INTERFACE (t) && i == 0)
1668 cp_error ("COM interface type `%T' with non-COM base class `%T'",
1670 CLASSTYPE_COM_INTERFACE (t) = 0;
1673 if (TYPE_VIRTUAL_P (basetype))
1675 /* Ensure that this is set from at least a virtual base
1677 if (b->rtti == NULL_TREE)
1678 b->rtti = CLASSTYPE_RTTI (basetype);
1680 /* Don't borrow virtuals from virtual baseclasses. */
1681 if (TREE_VIA_VIRTUAL (base_binfo))
1684 if (first_vfn_base_index < 0)
1687 first_vfn_base_index = i;
1689 /* Update these two, now that we know what vtable we are
1690 going to extend. This is so that we can add virtual
1691 functions, and override them properly. */
1692 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1693 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1694 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1695 b->vfield = CLASSTYPE_VFIELD (basetype);
1696 b->vfields = copy_list (CLASSTYPE_VFIELDS (basetype));
1697 vfields = b->vfields;
1700 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1701 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1703 tree value = VF_BASETYPE_VALUE (vfields);
1704 if (DECL_NAME (CLASSTYPE_VFIELD (value))
1705 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1706 VF_NORMAL_VALUE (b->vfields) = basetype;
1708 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1710 vfields = TREE_CHAIN (vfields);
1712 CLASSTYPE_VFIELD (t) = b->vfield;
1716 /* Only add unique vfields, and flatten them out as we go. */
1717 tree vfields = CLASSTYPE_VFIELDS (basetype);
1720 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1721 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1723 tree value = VF_BASETYPE_VALUE (vfields);
1724 b->vfields = tree_cons (base_binfo, value, b->vfields);
1725 if (DECL_NAME (CLASSTYPE_VFIELD (value))
1726 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1727 VF_NORMAL_VALUE (b->vfields) = basetype;
1729 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1731 vfields = TREE_CHAIN (vfields);
1734 if (b->has_virtual == 0)
1736 first_vfn_base_index = i;
1738 /* Update these two, now that we know what vtable we are
1739 going to extend. This is so that we can add virtual
1740 functions, and override them properly. */
1741 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1742 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1743 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1744 b->vfield = CLASSTYPE_VFIELD (basetype);
1745 CLASSTYPE_VFIELD (t) = b->vfield;
1746 /* When we install the first one, set the VF_NORMAL_VALUE
1747 to be the current class, as this it is the most derived
1748 class. Hopefully, this is not set to something else
1750 vfields = b->vfields;
1753 if (DECL_NAME (CLASSTYPE_VFIELD (t))
1754 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1756 VF_NORMAL_VALUE (vfields) = t;
1757 /* There should only be one of them! And it should
1758 always be found, if we get into here. (mrs) */
1761 vfields = TREE_CHAIN (vfields);
1770 /* Find the base class with the largest number of virtual functions. */
1771 for (vfields = b->vfields; vfields; vfields = TREE_CHAIN (vfields))
1773 if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)) > b->max_has_virtual)
1774 b->max_has_virtual = CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields));
1775 if (VF_DERIVED_VALUE (vfields)
1776 && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)) > b->max_has_virtual)
1777 b->max_has_virtual = CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields));
1782 /* If all virtual functions come only from virtual baseclasses. */
1785 /* Update the rtti base if we have a non-virtual base class version
1787 b->rtti = CLASSTYPE_RTTI (BINFO_TYPE (TREE_VEC_ELT (binfos, first_vfn_base_index)));
1789 return first_vfn_base_index;
1792 /* Set memoizing fields and bits of T (and its variants) for later use.
1793 MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables. */
1796 finish_struct_bits (t, max_has_virtual)
1798 int max_has_virtual;
1800 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1802 /* Fix up variants (if any). */
1803 tree variants = TYPE_NEXT_VARIANT (t);
1806 /* These fields are in the _TYPE part of the node, not in
1807 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1808 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1809 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1810 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1811 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
1813 TYPE_USES_COMPLEX_INHERITANCE (variants) = TYPE_USES_COMPLEX_INHERITANCE (t);
1814 TYPE_VIRTUAL_P (variants) = TYPE_VIRTUAL_P (t);
1815 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1816 /* Copy whatever these are holding today. */
1817 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1818 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1819 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1820 TYPE_SIZE (variants) = TYPE_SIZE (t);
1821 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1822 variants = TYPE_NEXT_VARIANT (variants);
1825 if (n_baseclasses && max_has_virtual)
1827 /* For a class w/o baseclasses, `finish_struct' has set
1828 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by definition). Similarly
1829 for a class who's base classes do not have vtables. When neither
1830 of these is true, we might have removed abstract virtuals (by
1831 providing a definition), added some (by declaring new ones), or
1832 redeclared ones from a base class. We need to recalculate what's
1833 really an abstract virtual at this point (by looking in the
1835 CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
1840 /* Notice whether this class has type conversion functions defined. */
1841 tree binfo = TYPE_BINFO (t);
1842 tree binfos = BINFO_BASETYPES (binfo);
1845 for (i = n_baseclasses-1; i >= 0; i--)
1847 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1849 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1853 /* If this type has a copy constructor, force its mode to be BLKmode, and
1854 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1855 be passed by invisible reference and prevent it from being returned in
1858 Also do this if the class has BLKmode but can still be returned in
1859 registers, since function_cannot_inline_p won't let us inline
1860 functions returning such a type. This affects the HP-PA. */
1861 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1862 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1863 && CLASSTYPE_NON_AGGREGATE (t)))
1866 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1867 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1869 TYPE_MODE (variants) = BLKmode;
1870 TREE_ADDRESSABLE (variants) = 1;
1875 /* Issue warnings about T having private constructors, but no friends,
1878 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1879 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1880 non-private static member functions. */
1883 maybe_warn_about_overly_private_class (t)
1886 int has_member_fn = 0;
1887 int has_nonprivate_method = 0;
1890 if (!warn_ctor_dtor_privacy
1891 /* If the class has friends, those entities might create and
1892 access instances, so we should not warn. */
1893 || (CLASSTYPE_FRIEND_CLASSES (t)
1894 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1895 /* We will have warned when the template was declared; there's
1896 no need to warn on every instantiation. */
1897 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1898 /* There's no reason to even consider warning about this
1902 /* We only issue one warning, if more than one applies, because
1903 otherwise, on code like:
1906 // Oops - forgot `public:'
1912 we warn several times about essentially the same problem. */
1914 /* Check to see if all (non-constructor, non-destructor) member
1915 functions are private. (Since there are no friends or
1916 non-private statics, we can't ever call any of the private member
1918 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1919 /* We're not interested in compiler-generated methods; they don't
1920 provide any way to call private members. */
1921 if (!DECL_ARTIFICIAL (fn))
1923 if (!TREE_PRIVATE (fn))
1925 if (DECL_STATIC_FUNCTION_P (fn))
1926 /* A non-private static member function is just like a
1927 friend; it can create and invoke private member
1928 functions, and be accessed without a class
1932 has_nonprivate_method = 1;
1935 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1939 if (!has_nonprivate_method && has_member_fn)
1941 /* There are no non-private methods, and there's at least one
1942 private member function that isn't a constructor or
1943 destructor. (If all the private members are
1944 constructors/destructors we want to use the code below that
1945 issues error messages specifically referring to
1946 constructors/destructors.) */
1948 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1949 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1950 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1951 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1953 has_nonprivate_method = 1;
1956 if (!has_nonprivate_method)
1958 cp_warning ("all member functions in class `%T' are private", t);
1963 /* Even if some of the member functions are non-private, the class
1964 won't be useful for much if all the constructors or destructors
1965 are private: such an object can never be created or destroyed. */
1966 if (TYPE_HAS_DESTRUCTOR (t))
1968 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
1970 if (TREE_PRIVATE (dtor))
1972 cp_warning ("`%#T' only defines a private destructor and has no friends",
1978 if (TYPE_HAS_CONSTRUCTOR (t))
1980 int nonprivate_ctor = 0;
1982 /* If a non-template class does not define a copy
1983 constructor, one is defined for it, enabling it to avoid
1984 this warning. For a template class, this does not
1985 happen, and so we would normally get a warning on:
1987 template <class T> class C { private: C(); };
1989 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1990 complete non-template or fully instantiated classes have this
1992 if (!TYPE_HAS_INIT_REF (t))
1993 nonprivate_ctor = 1;
1995 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
1999 tree ctor = OVL_CURRENT (fn);
2000 /* Ideally, we wouldn't count copy constructors (or, in
2001 fact, any constructor that takes an argument of the
2002 class type as a parameter) because such things cannot
2003 be used to construct an instance of the class unless
2004 you already have one. But, for now at least, we're
2006 if (! TREE_PRIVATE (ctor))
2008 nonprivate_ctor = 1;
2013 if (nonprivate_ctor == 0)
2015 cp_warning ("`%#T' only defines private constructors and has no friends",
2022 /* Function to help qsort sort FIELD_DECLs by name order. */
2025 field_decl_cmp (x, y)
2028 if (DECL_NAME (*x) == DECL_NAME (*y))
2030 if (DECL_NAME (*x) == NULL_TREE)
2032 if (DECL_NAME (*y) == NULL_TREE)
2034 if (DECL_NAME (*x) < DECL_NAME (*y))
2039 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2042 method_name_cmp (m1, m2)
2043 const tree *m1, *m2;
2045 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2047 if (*m1 == NULL_TREE)
2049 if (*m2 == NULL_TREE)
2051 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2056 /* Warn about duplicate methods in fn_fields. Also compact method
2057 lists so that lookup can be made faster.
2059 Data Structure: List of method lists. The outer list is a
2060 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2061 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2062 links the entire list of methods for TYPE_METHODS. Friends are
2063 chained in the same way as member functions (? TREE_CHAIN or
2064 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2065 list. That allows them to be quickly deleted, and requires no
2068 If there are any constructors/destructors, they are moved to the
2069 front of the list. This makes pushclass more efficient.
2071 @@ The above comment is obsolete. It mostly describes what add_method
2072 @@ and add_implicitly_declared_members do.
2074 Sort methods that are not special (i.e., constructors, destructors, and
2075 type conversion operators) so that we can find them faster in search. */
2078 finish_struct_methods (t)
2082 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2083 tree ctor_name = constructor_name (t);
2084 int slot, len = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2086 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2087 and the next few with type conversion operators (if any). */
2088 for (fn_fields = TYPE_METHODS (t); fn_fields;
2089 fn_fields = TREE_CHAIN (fn_fields))
2091 tree fn_name = DECL_NAME (fn_fields);
2093 /* Clear out this flag.
2095 @@ Doug may figure out how to break
2096 @@ this with nested classes and friends. */
2097 DECL_IN_AGGR_P (fn_fields) = 0;
2099 /* Note here that a copy ctor is private, so we don't dare generate
2100 a default copy constructor for a class that has a member
2101 of this type without making sure they have access to it. */
2102 if (fn_name == ctor_name)
2104 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2105 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2107 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2108 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2110 if (TREE_CHAIN (parmtypes) == NULL_TREE
2111 || TREE_CHAIN (parmtypes) == void_list_node
2112 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2114 if (TREE_PROTECTED (fn_fields))
2115 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2116 else if (TREE_PRIVATE (fn_fields))
2117 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2121 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2123 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2125 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2127 if (TREE_PROTECTED (fn_fields))
2128 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2129 else if (TREE_PRIVATE (fn_fields))
2130 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2135 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2136 /* We thought there was a destructor, but there wasn't. Some
2137 parse errors cause this anomalous situation. */
2138 TYPE_HAS_DESTRUCTOR (t) = 0;
2140 /* Issue warnings about private constructors and such. If there are
2141 no methods, then some public defaults are generated. */
2142 maybe_warn_about_overly_private_class (t);
2144 if (method_vec == NULL_TREE)
2147 /* Now sort the methods. */
2148 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2150 TREE_VEC_LENGTH (method_vec) = len;
2152 /* The type conversion ops have to live at the front of the vec, so we
2154 for (slot = 2; slot < len; ++slot)
2156 tree fn = TREE_VEC_ELT (method_vec, slot);
2158 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2162 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2163 (int (*)(const void *, const void *))method_name_cmp);
2166 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2169 duplicate_tag_error (t)
2172 cp_error ("redefinition of `%#T'", t);
2173 cp_error_at ("previous definition here", t);
2175 /* Pretend we haven't defined this type. */
2177 /* All of the component_decl's were TREE_CHAINed together in the parser.
2178 finish_struct_methods walks these chains and assembles all methods with
2179 the same base name into DECL_CHAINs. Now we don't need the parser chains
2180 anymore, so we unravel them. */
2182 /* This used to be in finish_struct, but it turns out that the
2183 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2185 if (CLASSTYPE_METHOD_VEC (t))
2187 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2188 int i, len = TREE_VEC_LENGTH (method_vec);
2189 for (i = 0; i < len; i++)
2191 tree unchain = TREE_VEC_ELT (method_vec, i);
2192 while (unchain != NULL_TREE)
2194 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2195 unchain = OVL_NEXT (unchain);
2200 if (TYPE_LANG_SPECIFIC (t))
2202 tree binfo = TYPE_BINFO (t);
2203 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2204 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2205 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2206 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2208 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2209 BINFO_BASETYPES(binfo) = NULL_TREE;
2211 TYPE_BINFO (t) = binfo;
2212 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2213 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2214 TYPE_REDEFINED (t) = 1;
2215 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2216 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2218 TYPE_SIZE (t) = NULL_TREE;
2219 TYPE_MODE (t) = VOIDmode;
2220 TYPE_FIELDS (t) = NULL_TREE;
2221 TYPE_METHODS (t) = NULL_TREE;
2222 TYPE_VFIELD (t) = NULL_TREE;
2223 TYPE_CONTEXT (t) = NULL_TREE;
2224 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2227 /* Construct the initializer for BINFOs virtual function table. */
2230 build_vtbl_initializer (binfo)
2233 tree v = BINFO_VIRTUALS (binfo);
2234 tree inits = NULL_TREE;
2236 /* Process the RTTI stuff at the head of the list. If we're not
2237 using vtable thunks, then the RTTI entry is just an ordinary
2238 function, and we can process it just like the other virtual
2239 function entries. */
2240 if (!CLASSTYPE_COM_INTERFACE (BINFO_TYPE (binfo))
2241 && flag_vtable_thunks)
2246 /* The first entry is an offset. */
2247 offset = TREE_PURPOSE (v);
2248 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST,
2251 /* Convert the offset to look like a function pointer, so that
2252 we can put it in the vtable. */
2253 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
2254 TREE_CONSTANT (init) = 1;
2255 init = build_vtable_entry (integer_zero_node, init);
2256 inits = tree_cons (NULL_TREE, init, inits);
2258 /* Even in this case, the second entry (the tdesc pointer) is
2259 just an ordinary function. */
2263 /* Go through all the ordinary virtual functions, building up
2271 /* Pull the offset for `this', and the function to call, out of
2273 delta = TREE_PURPOSE (v);
2274 fn = TREE_VALUE (v);
2275 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
2276 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
2278 /* You can't call an abstract virtual function; it's abstract.
2279 So, we replace these functions with __pure_virtual. */
2280 if (DECL_ABSTRACT_VIRTUAL_P (fn))
2283 /* Package up that information for the vtable. */
2284 init = build_vtable_entry_for_fn (delta, fn);
2285 /* And add it to the chain of initializers. */
2286 inits = tree_cons (NULL_TREE, init, inits);
2292 /* The initializers were built up in reverse order; straighten them
2294 inits = nreverse (inits);
2295 /* Package all the initializers up as an array initializer. */
2296 return build_nt (CONSTRUCTOR, NULL_TREE, inits);
2299 /* finish up all new vtables. */
2302 finish_vtbls (binfo, do_self, t)
2307 tree binfos = BINFO_BASETYPES (binfo);
2308 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2310 /* Should we use something besides CLASSTYPE_VFIELDS? */
2311 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2313 if (BINFO_NEW_VTABLE_MARKED (binfo))
2317 decl = BINFO_VTABLE (binfo);
2318 context = DECL_CONTEXT (decl);
2319 DECL_CONTEXT (decl) = 0;
2320 DECL_INITIAL (decl) = build_vtbl_initializer (binfo);
2321 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0);
2322 DECL_CONTEXT (decl) = context;
2324 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2327 for (i = 0; i < n_baselinks; i++)
2329 tree base_binfo = TREE_VEC_ELT (binfos, i);
2330 int is_not_base_vtable
2331 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2332 if (TREE_VIA_VIRTUAL (base_binfo))
2333 base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2334 CLASSTYPE_VBASECLASSES (t));
2335 finish_vtbls (base_binfo, is_not_base_vtable, t);
2339 /* True if we should override the given BASE_FNDECL with the given
2343 overrides (fndecl, base_fndecl)
2344 tree fndecl, base_fndecl;
2346 /* Destructors have special names. */
2347 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2348 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2350 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2351 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2353 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2355 tree types, base_types;
2357 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2358 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2360 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2361 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2362 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2363 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2364 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2371 get_class_offset_1 (parent, binfo, context, t, fndecl)
2372 tree parent, binfo, context, t, fndecl;
2374 tree binfos = BINFO_BASETYPES (binfo);
2375 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2376 tree rval = NULL_TREE;
2378 if (binfo == parent)
2379 return error_mark_node;
2381 for (i = 0; i < n_baselinks; i++)
2383 tree base_binfo = TREE_VEC_ELT (binfos, i);
2386 if (TREE_VIA_VIRTUAL (base_binfo))
2387 base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2388 CLASSTYPE_VBASECLASSES (t));
2389 nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl);
2390 /* See if we have a new value */
2391 if (nrval && (nrval != error_mark_node || rval==0))
2393 /* Only compare if we have two offsets */
2394 if (rval && rval != error_mark_node
2395 && ! tree_int_cst_equal (nrval, rval))
2397 /* Only give error if the two offsets are different */
2398 error ("every virtual function must have a unique final overrider");
2399 cp_error (" found two (or more) `%T' class subobjects in `%T'", context, t);
2400 cp_error (" with virtual `%D' from virtual base class", fndecl);
2406 if (rval && BINFO_TYPE (binfo) == context)
2408 my_friendly_assert (rval == error_mark_node
2409 || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999);
2410 rval = BINFO_OFFSET (binfo);
2416 /* Get the offset to the CONTEXT subobject that is related to the
2420 get_class_offset (context, t, binfo, fndecl)
2421 tree context, t, binfo, fndecl;
2423 tree first_binfo = binfo;
2428 return integer_zero_node;
2430 if (BINFO_TYPE (binfo) == context)
2431 return BINFO_OFFSET (binfo);
2433 /* Check less derived binfos first. */
2434 while (BINFO_BASETYPES (binfo)
2435 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
2437 tree binfos = BINFO_BASETYPES (binfo);
2438 binfo = TREE_VEC_ELT (binfos, i);
2439 if (BINFO_TYPE (binfo) == context)
2440 return BINFO_OFFSET (binfo);
2443 /* Ok, not found in the less derived binfos, now check the more
2445 offset = get_class_offset_1 (first_binfo, TYPE_BINFO (t), context, t, fndecl);
2446 if (offset==0 || TREE_CODE (offset) != INTEGER_CST)
2447 my_friendly_abort (999); /* we have to find it. */
2451 /* Skip RTTI information at the front of the virtual list. */
2453 unsigned HOST_WIDE_INT
2454 skip_rtti_stuff (virtuals, t)
2459 if (CLASSTYPE_COM_INTERFACE (t))
2465 /* We always reserve a slot for the offset/tdesc entry. */
2467 *virtuals = TREE_CHAIN (*virtuals);
2469 if (flag_vtable_thunks && *virtuals)
2471 /* The second slot is reserved for the tdesc pointer when thunks
2474 *virtuals = TREE_CHAIN (*virtuals);
2480 modify_one_vtable (binfo, t, fndecl)
2481 tree binfo, t, fndecl;
2483 tree virtuals = BINFO_VIRTUALS (binfo);
2484 unsigned HOST_WIDE_INT n;
2486 /* update rtti entry */
2489 if (binfo == TYPE_BINFO (t))
2491 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2492 build_vtable (TYPE_BINFO (DECL_CONTEXT (CLASSTYPE_VFIELD (t))), t);
2496 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2497 prepare_fresh_vtable (binfo, t);
2500 if (fndecl == NULL_TREE)
2503 n = skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo));
2507 tree current_fndecl = TREE_VALUE (virtuals);
2509 /* We should never have an instance of __pure_virtual on the
2510 BINFO_VIRTUALS list. If we do, then we will never notice
2511 that the function that should have been there instead has
2513 my_friendly_assert (current_fndecl != abort_fndecl,
2516 if (current_fndecl && overrides (fndecl, current_fndecl))
2518 tree base_offset, offset;
2519 tree context = DECL_CLASS_CONTEXT (fndecl);
2520 tree vfield = CLASSTYPE_VFIELD (t);
2523 offset = get_class_offset (context, t, binfo, fndecl);
2525 /* Find the right offset for the this pointer based on the
2526 base class we just found. We have to take into
2527 consideration the virtual base class pointers that we
2528 stick in before the virtual function table pointer.
2530 Also, we want just the delta between the most base class
2531 that we derived this vfield from and us. */
2532 base_offset = size_binop (PLUS_EXPR,
2533 get_derived_offset (binfo, DECL_CONTEXT (current_fndecl)),
2534 BINFO_OFFSET (binfo));
2535 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2537 if (binfo == TYPE_BINFO (t))
2539 /* In this case, it is *type*'s vtable we are modifying.
2540 We start with the approximation that it's vtable is that
2541 of the immediate base class. */
2542 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2543 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2547 /* This is our very own copy of `basetype' to play with.
2548 Later, we will fill in all the virtual functions
2549 that override the virtual functions in these base classes
2550 which are not defined by the current type. */
2551 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2552 prepare_fresh_vtable (binfo, t);
2556 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
2558 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2563 virtuals = TREE_CHAIN (virtuals);
2567 /* These are the ones that are not through virtual base classes. */
2570 modify_all_direct_vtables (binfo, do_self, t, fndecl)
2575 tree binfos = BINFO_BASETYPES (binfo);
2576 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2578 /* Should we use something besides CLASSTYPE_VFIELDS? */
2579 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2580 modify_one_vtable (binfo, t, fndecl);
2582 for (i = 0; i < n_baselinks; i++)
2584 tree base_binfo = TREE_VEC_ELT (binfos, i);
2585 int is_not_base_vtable
2586 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2587 if (! TREE_VIA_VIRTUAL (base_binfo))
2588 modify_all_direct_vtables (base_binfo, is_not_base_vtable, t, fndecl);
2592 /* Fixup all the delta entries in this one vtable that need updating. */
2595 fixup_vtable_deltas1 (binfo, t)
2598 tree virtuals = BINFO_VIRTUALS (binfo);
2599 unsigned HOST_WIDE_INT n;
2601 n = skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo));
2605 tree fndecl = TREE_VALUE (virtuals);
2606 tree delta = TREE_PURPOSE (virtuals);
2610 tree base_offset, offset;
2611 tree context = DECL_CLASS_CONTEXT (fndecl);
2612 tree vfield = CLASSTYPE_VFIELD (t);
2615 offset = get_class_offset (context, t, binfo, fndecl);
2617 /* Find the right offset for the this pointer based on the
2618 base class we just found. We have to take into
2619 consideration the virtual base class pointers that we
2620 stick in before the virtual function table pointer.
2622 Also, we want just the delta between the most base class
2623 that we derived this vfield from and us. */
2624 base_offset = size_binop (PLUS_EXPR,
2625 get_derived_offset (binfo,
2626 DECL_CONTEXT (fndecl)),
2627 BINFO_OFFSET (binfo));
2628 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2630 if (! tree_int_cst_equal (this_offset, delta))
2632 /* Make sure we can modify the derived association with immunity. */
2633 if (binfo == TYPE_BINFO (t))
2635 /* In this case, it is *type*'s vtable we are modifying.
2636 We start with the approximation that it's vtable is that
2637 of the immediate base class. */
2638 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2639 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2643 /* This is our very own copy of `basetype' to play with.
2644 Later, we will fill in all the virtual functions
2645 that override the virtual functions in these base classes
2646 which are not defined by the current type. */
2647 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2648 prepare_fresh_vtable (binfo, t);
2651 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2657 virtuals = TREE_CHAIN (virtuals);
2661 /* Fixup all the delta entries in all the direct vtables that need updating.
2662 This happens when we have non-overridden virtual functions from a
2663 virtual base class, that are at a different offset, in the new
2664 hierarchy, because the layout of the virtual bases has changed. */
2667 fixup_vtable_deltas (binfo, init_self, t)
2672 tree binfos = BINFO_BASETYPES (binfo);
2673 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2675 for (i = 0; i < n_baselinks; i++)
2677 tree base_binfo = TREE_VEC_ELT (binfos, i);
2678 int is_not_base_vtable
2679 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2680 if (! TREE_VIA_VIRTUAL (base_binfo))
2681 fixup_vtable_deltas (base_binfo, is_not_base_vtable, t);
2683 /* Should we use something besides CLASSTYPE_VFIELDS? */
2684 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2685 fixup_vtable_deltas1 (binfo, t);
2688 /* These are the ones that are through virtual base classes. */
2691 modify_all_indirect_vtables (binfo, do_self, via_virtual, t, fndecl)
2693 int do_self, via_virtual;
2696 tree binfos = BINFO_BASETYPES (binfo);
2697 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2699 /* Should we use something besides CLASSTYPE_VFIELDS? */
2700 if (do_self && via_virtual && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2701 modify_one_vtable (binfo, t, fndecl);
2703 for (i = 0; i < n_baselinks; i++)
2705 tree base_binfo = TREE_VEC_ELT (binfos, i);
2706 int is_not_base_vtable
2707 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2708 if (TREE_VIA_VIRTUAL (base_binfo))
2711 base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2713 modify_all_indirect_vtables (base_binfo, is_not_base_vtable, via_virtual, t, fndecl);
2718 modify_all_vtables (t, fndecl)
2722 /* Do these first, so that we will make use of any non-virtual class's
2723 vtable, over a virtual classes vtable. */
2724 modify_all_direct_vtables (TYPE_BINFO (t), 1, t, fndecl);
2725 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
2726 modify_all_indirect_vtables (TYPE_BINFO (t), 1, 0, t, fndecl);
2729 /* Here, we already know that they match in every respect.
2730 All we have to check is where they had their declarations. */
2733 strictly_overrides (fndecl1, fndecl2)
2734 tree fndecl1, fndecl2;
2736 int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
2737 DECL_CLASS_CONTEXT (fndecl1),
2739 if (distance == -2 || distance > 0)
2744 /* Merge overrides for one vtable.
2745 If we want to merge in same function, we are fine.
2747 if one has a DECL_CLASS_CONTEXT that is a parent of the
2748 other, than choose the more derived one
2750 potentially ill-formed (see 10.3 [class.virtual])
2751 we have to check later to see if there was an
2752 override in this class. If there was ok, if not
2753 then it is ill-formed. (mrs)
2755 We take special care to reuse a vtable, if we can. */
2758 override_one_vtable (binfo, old, t)
2761 tree virtuals = BINFO_VIRTUALS (binfo);
2762 tree old_virtuals = BINFO_VIRTUALS (old);
2763 enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
2765 /* If we have already committed to modifying it, then don't try and
2766 reuse another vtable. */
2767 if (BINFO_NEW_VTABLE_MARKED (binfo))
2770 skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo));
2771 skip_rtti_stuff (&old_virtuals, BINFO_TYPE (binfo));
2775 tree fndecl = TREE_VALUE (virtuals);
2776 tree old_fndecl = TREE_VALUE (old_virtuals);
2778 /* First check to see if they are the same. */
2779 if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
2781 /* No need to do anything. */
2783 else if (strictly_overrides (fndecl, old_fndecl))
2785 if (choose == UNDECIDED)
2787 else if (choose == REUSE_OLD)
2790 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2792 prepare_fresh_vtable (binfo, t);
2793 override_one_vtable (binfo, old, t);
2798 else if (strictly_overrides (old_fndecl, fndecl))
2800 if (choose == UNDECIDED)
2802 else if (choose == REUSE_NEW)
2805 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2807 prepare_fresh_vtable (binfo, t);
2808 override_one_vtable (binfo, old, t);
2811 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2813 else if (choose == NEITHER)
2815 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2821 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2823 prepare_fresh_vtable (binfo, t);
2824 override_one_vtable (binfo, old, t);
2828 /* This MUST be overridden, or the class is ill-formed. */
2829 tree fndecl = TREE_VALUE (virtuals);
2831 fndecl = copy_node (fndecl);
2832 copy_lang_decl (fndecl);
2833 DECL_NEEDS_FINAL_OVERRIDER_P (fndecl) = 1;
2834 /* Make sure we search for it later. */
2835 if (! CLASSTYPE_ABSTRACT_VIRTUALS (t))
2836 CLASSTYPE_ABSTRACT_VIRTUALS (t) = error_mark_node;
2838 /* We can use integer_zero_node, as we will core dump
2839 if this is used anyway. */
2840 TREE_PURPOSE (virtuals) = integer_zero_node;
2841 TREE_VALUE (virtuals) = fndecl;
2844 virtuals = TREE_CHAIN (virtuals);
2845 old_virtuals = TREE_CHAIN (old_virtuals);
2848 /* Let's reuse the old vtable. */
2849 if (choose == REUSE_OLD)
2851 BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
2852 BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
2856 /* Merge in overrides for virtual bases.
2857 BINFO is the hierarchy we want to modify, and OLD has the potential
2861 merge_overrides (binfo, old, do_self, t)
2866 tree binfos = BINFO_BASETYPES (binfo);
2867 tree old_binfos = BINFO_BASETYPES (old);
2868 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2870 /* Should we use something besides CLASSTYPE_VFIELDS? */
2871 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2873 override_one_vtable (binfo, old, t);
2876 for (i = 0; i < n_baselinks; i++)
2878 tree base_binfo = TREE_VEC_ELT (binfos, i);
2879 tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
2880 int is_not_base_vtable
2881 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2882 if (! TREE_VIA_VIRTUAL (base_binfo))
2883 merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
2887 /* Get the base virtual function declarations in T that are either
2888 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2889 the overrider/hider. */
2892 get_basefndecls (fndecl, t)
2895 tree methods = TYPE_METHODS (t);
2896 tree base_fndecls = NULL_TREE;
2897 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2898 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2902 if (TREE_CODE (methods) == FUNCTION_DECL
2903 && DECL_VINDEX (methods) != NULL_TREE
2904 && DECL_NAME (fndecl) == DECL_NAME (methods))
2905 base_fndecls = temp_tree_cons (fndecl, methods, base_fndecls);
2907 methods = TREE_CHAIN (methods);
2911 return base_fndecls;
2913 for (i = 0; i < n_baseclasses; i++)
2915 tree base_binfo = TREE_VEC_ELT (binfos, i);
2916 tree basetype = BINFO_TYPE (base_binfo);
2918 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2922 return base_fndecls;
2925 /* Mark the functions that have been hidden with their overriders.
2926 Since we start out with all functions already marked with a hider,
2927 no need to mark functions that are just hidden.
2929 Subroutine of warn_hidden. */
2932 mark_overriders (fndecl, base_fndecls)
2933 tree fndecl, base_fndecls;
2935 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2937 if (overrides (fndecl, TREE_VALUE (base_fndecls)))
2938 TREE_PURPOSE (base_fndecls) = fndecl;
2942 /* If this declaration supersedes the declaration of
2943 a method declared virtual in the base class, then
2944 mark this field as being virtual as well. */
2947 check_for_override (decl, ctype)
2950 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
2951 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2952 int virtualp = DECL_VIRTUAL_P (decl);
2953 int found_overriden_fn = 0;
2955 for (i = 0; i < n_baselinks; i++)
2957 tree base_binfo = TREE_VEC_ELT (binfos, i);
2958 if (TYPE_VIRTUAL_P (BINFO_TYPE (base_binfo)))
2960 tree tmp = get_matching_virtual
2962 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
2964 if (tmp && !found_overriden_fn)
2966 /* If this function overrides some virtual in some base
2967 class, then the function itself is also necessarily
2968 virtual, even if the user didn't explicitly say so. */
2969 DECL_VIRTUAL_P (decl) = 1;
2971 /* The TMP we really want is the one from the deepest
2972 baseclass on this path, taking care not to
2973 duplicate if we have already found it (via another
2974 path to its virtual baseclass. */
2975 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
2977 cp_error_at ("`static %#D' cannot be declared", decl);
2978 cp_error_at (" since `virtual %#D' declared in base class",
2985 = tree_cons (NULL_TREE, tmp, DECL_VINDEX (decl));
2987 /* We now know that DECL overrides something,
2988 which is all that is important. But, we must
2989 continue to iterate through all the base-classes
2990 in order to allow get_matching_virtual to check for
2991 various illegal overrides. */
2992 found_overriden_fn = 1;
2998 if (DECL_VINDEX (decl) == NULL_TREE)
2999 DECL_VINDEX (decl) = error_mark_node;
3000 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3004 /* Warn about hidden virtual functions that are not overridden in t.
3005 We know that constructors and destructors don't apply. */
3011 tree method_vec = CLASSTYPE_METHOD_VEC (t);
3012 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3015 /* We go through each separately named virtual function. */
3016 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
3018 tree fns = TREE_VEC_ELT (method_vec, i);
3021 tree base_fndecls = NULL_TREE;
3022 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3023 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3025 /* First see if we have any virtual functions in this batch. */
3026 for (; fns; fns = OVL_NEXT (fns))
3028 fndecl = OVL_CURRENT (fns);
3029 if (DECL_VINDEX (fndecl))
3033 if (fns == NULL_TREE)
3036 /* First we get a list of all possible functions that might be
3037 hidden from each base class. */
3038 for (i = 0; i < n_baseclasses; i++)
3040 tree base_binfo = TREE_VEC_ELT (binfos, i);
3041 tree basetype = BINFO_TYPE (base_binfo);
3043 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3047 fns = OVL_NEXT (fns);
3049 /* ...then mark up all the base functions with overriders, preferring
3050 overriders to hiders. */
3052 for (; fns; fns = OVL_NEXT (fns))
3054 fndecl = OVL_CURRENT (fns);
3055 if (DECL_VINDEX (fndecl))
3056 mark_overriders (fndecl, base_fndecls);
3059 /* Now give a warning for all base functions without overriders,
3060 as they are hidden. */
3061 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3063 if (! overrides (TREE_PURPOSE (base_fndecls),
3064 TREE_VALUE (base_fndecls)))
3066 /* Here we know it is a hider, and no overrider exists. */
3067 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
3068 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
3074 /* Check for things that are invalid. There are probably plenty of other
3075 things we should check for also. */
3078 finish_struct_anon (t)
3083 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3085 if (TREE_STATIC (field))
3087 if (TREE_CODE (field) != FIELD_DECL)
3090 if (DECL_NAME (field) == NULL_TREE
3091 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3093 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3094 for (; elt; elt = TREE_CHAIN (elt))
3096 if (DECL_ARTIFICIAL (elt))
3099 if (DECL_NAME (elt) == constructor_name (t))
3100 cp_pedwarn_at ("ANSI C++ forbids member `%D' with same name as enclosing class",
3103 if (TREE_CODE (elt) != FIELD_DECL)
3105 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3110 if (TREE_PRIVATE (elt))
3111 cp_pedwarn_at ("private member `%#D' in anonymous union",
3113 else if (TREE_PROTECTED (elt))
3114 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3117 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3118 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3124 extern int interface_only, interface_unknown;
3126 /* Create default constructors, assignment operators, and so forth for
3127 the type indicated by T, if they are needed.
3128 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3129 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3130 cannot have a default constructor, copy constructor taking a const
3131 reference argument, or an assignment operator, respectively. If a
3132 virtual destructor is created, its DECL is returned; otherwise the
3133 return value is NULL_TREE. */
3136 add_implicitly_declared_members (t, cant_have_default_ctor,
3137 cant_have_const_cctor,
3138 cant_have_assignment)
3140 int cant_have_default_ctor;
3141 int cant_have_const_cctor;
3142 int cant_have_assignment;
3145 tree implicit_fns = NULL_TREE;
3146 tree name = TYPE_IDENTIFIER (t);
3147 tree virtual_dtor = NULL_TREE;
3151 if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
3153 default_fn = cons_up_default_function (t, name, 0);
3154 check_for_override (default_fn, t);
3156 /* If we couldn't make it work, then pretend we didn't need it. */
3157 if (default_fn == void_type_node)
3158 TYPE_NEEDS_DESTRUCTOR (t) = 0;
3161 TREE_CHAIN (default_fn) = implicit_fns;
3162 implicit_fns = default_fn;
3164 if (DECL_VINDEX (default_fn))
3165 virtual_dtor = default_fn;
3168 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3170 /* Default constructor. */
3171 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3173 default_fn = cons_up_default_function (t, name, 2);
3174 TREE_CHAIN (default_fn) = implicit_fns;
3175 implicit_fns = default_fn;
3178 /* Copy constructor. */
3179 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3181 /* ARM 12.18: You get either X(X&) or X(const X&), but
3183 default_fn = cons_up_default_function (t, name,
3184 3 + cant_have_const_cctor);
3185 TREE_CHAIN (default_fn) = implicit_fns;
3186 implicit_fns = default_fn;
3189 /* Assignment operator. */
3190 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3192 default_fn = cons_up_default_function (t, name,
3193 5 + cant_have_assignment);
3194 TREE_CHAIN (default_fn) = implicit_fns;
3195 implicit_fns = default_fn;
3198 /* Now, hook all of the new functions on to TYPE_METHODS,
3199 and add them to the CLASSTYPE_METHOD_VEC. */
3200 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3201 add_method (t, 0, *f);
3202 *f = TYPE_METHODS (t);
3203 TYPE_METHODS (t) = implicit_fns;
3205 return virtual_dtor;
3208 /* Subroutine of finish_struct_1. Recursively count the number of fields
3209 in TYPE, including anonymous union members. */
3212 count_fields (fields)
3217 for (x = fields; x; x = TREE_CHAIN (x))
3219 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3220 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3227 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3228 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3231 add_fields_to_vec (fields, field_vec, idx)
3232 tree fields, field_vec;
3236 for (x = fields; x; x = TREE_CHAIN (x))
3238 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3239 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3241 TREE_VEC_ELT (field_vec, idx++) = x;
3246 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
3247 (or C++ class declaration).
3249 For C++, we must handle the building of derived classes.
3250 Also, C++ allows static class members. The way that this is
3251 handled is to keep the field name where it is (as the DECL_NAME
3252 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
3253 of the field. layout_record and layout_union will know about this.
3255 More C++ hair: inline functions have text in their
3256 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
3257 meaningful tree structure. After the struct has been laid out, set
3258 things up so that this can happen.
3260 And still more: virtual functions. In the case of single inheritance,
3261 when a new virtual function is seen which redefines a virtual function
3262 from the base class, the new virtual function is placed into
3263 the virtual function table at exactly the same address that
3264 it had in the base class. When this is extended to multiple
3265 inheritance, the same thing happens, except that multiple virtual
3266 function tables must be maintained. The first virtual function
3267 table is treated in exactly the same way as in the case of single
3268 inheritance. Additional virtual function tables have different
3269 DELTAs, which tell how to adjust `this' to point to the right thing.
3271 ATTRIBUTES is the set of decl attributes to be applied, if any. */
3278 enum tree_code code = TREE_CODE (t);
3279 tree fields = TYPE_FIELDS (t);
3280 tree x, last_x, method_vec;
3282 int max_has_virtual;
3283 tree pending_virtuals = NULL_TREE;
3284 tree pending_hard_virtuals = NULL_TREE;
3285 tree abstract_virtuals = NULL_TREE;
3289 int cant_have_default_ctor;
3290 int cant_have_const_ctor;
3291 int no_const_asn_ref;
3292 int has_mutable = 0;
3294 int non_pod_class = 0;
3296 /* The index of the first base class which has virtual
3297 functions. Only applied to non-virtual baseclasses. */
3298 int first_vfn_base_index;
3301 int any_default_members = 0;
3302 int const_sans_init = 0;
3303 int ref_sans_init = 0;
3304 tree access_decls = NULL_TREE;
3307 int has_pointers = 0;
3308 tree inline_friends;
3312 if (IS_AGGR_TYPE (t))
3313 cp_error ("redefinition of `%#T'", t);
3315 my_friendly_abort (172);
3320 GNU_xref_decl (current_function_decl, t);
3322 /* If this type was previously laid out as a forward reference,
3323 make sure we lay it out again. */
3325 TYPE_SIZE (t) = NULL_TREE;
3326 CLASSTYPE_GOT_SEMICOLON (t) = 0;
3328 old = suspend_momentary ();
3330 /* Install struct as DECL_FIELD_CONTEXT of each field decl.
3331 Also process specified field sizes.
3332 Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
3333 The specified size is found in the DECL_INITIAL.
3334 Store 0 there, except for ": 0" fields (so we can find them
3335 and delete them, below). */
3337 if (TYPE_BINFO_BASETYPES (t))
3338 n_baseclasses = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t));
3342 if (n_baseclasses > 0)
3344 struct base_info base_info;
3346 first_vfn_base_index = finish_base_struct (t, &base_info);
3347 /* Remember where we got our vfield from. */
3348 CLASSTYPE_VFIELD_PARENT (t) = first_vfn_base_index;
3349 has_virtual = base_info.has_virtual;
3350 max_has_virtual = base_info.max_has_virtual;
3351 vfield = base_info.vfield;
3352 vfields = base_info.vfields;
3353 CLASSTYPE_RTTI (t) = base_info.rtti;
3354 cant_have_default_ctor = base_info.cant_have_default_ctor;
3355 cant_have_const_ctor = base_info.cant_have_const_ctor;
3356 no_const_asn_ref = base_info.no_const_asn_ref;
3361 first_vfn_base_index = -1;
3363 max_has_virtual = has_virtual;
3365 vfields = NULL_TREE;
3366 CLASSTYPE_RTTI (t) = NULL_TREE;
3367 cant_have_default_ctor = 0;
3368 cant_have_const_ctor = 0;
3369 no_const_asn_ref = 0;
3372 /* The three of these are approximations which may later be
3373 modified. Needed at this point to make add_virtual_function
3374 and modify_vtable_entries work. */
3375 CLASSTYPE_VFIELDS (t) = vfields;
3376 CLASSTYPE_VFIELD (t) = vfield;
3378 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3380 GNU_xref_member (current_class_name, x);
3382 /* If this was an evil function, don't keep it in class. */
3383 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3386 /* Do both of these, even though they're in the same union;
3387 if the insn `r' member and the size `i' member are
3388 different sizes, as on the alpha, the larger of the two
3389 will end up with garbage in it. */
3390 DECL_SAVED_INSNS (x) = 0;
3391 DECL_FIELD_SIZE (x) = 0;
3393 check_for_override (x, t);
3394 if (DECL_ABSTRACT_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3395 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3397 /* The name of the field is the original field name
3398 Save this in auxiliary field for later overloading. */
3399 if (DECL_VINDEX (x))
3401 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3402 &has_virtual, x, t);
3403 if (DECL_ABSTRACT_VIRTUAL_P (x))
3404 abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
3406 /* XXX Why did I comment this out? (jason) */
3414 fields = chainon (build_vbase_pointer_fields (t), fields);
3417 for (x = fields; x; x = TREE_CHAIN (x))
3419 tree type = TREE_TYPE (x);
3420 GNU_xref_member (current_class_name, x);
3422 if (TREE_CODE (x) == FIELD_DECL)
3424 DECL_PACKED (x) |= TYPE_PACKED (t);
3426 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3427 /* A zero-width bitfield doesn't do the trick. */;
3432 if (TREE_CODE (x) == USING_DECL)
3434 /* Save access declarations for later. */
3436 TREE_CHAIN (last_x) = TREE_CHAIN (x);
3438 fields = TREE_CHAIN (x);
3440 access_decls = scratch_tree_cons (NULL_TREE, x, access_decls);
3446 if (TREE_CODE (x) == TYPE_DECL
3447 || TREE_CODE (x) == TEMPLATE_DECL)
3450 /* If we've gotten this far, it's a data member, possibly static,
3451 or an enumerator. */
3453 DECL_FIELD_CONTEXT (x) = t;
3455 /* ``A local class cannot have static data members.'' ARM 9.4 */
3456 if (current_function_decl && TREE_STATIC (x))
3457 cp_error_at ("field `%D' in local class cannot be static", x);
3459 /* Perform error checking that did not get done in
3461 if (TREE_CODE (type) == FUNCTION_TYPE)
3463 cp_error_at ("field `%D' invalidly declared function type",
3465 type = build_pointer_type (type);
3466 TREE_TYPE (x) = type;
3468 else if (TREE_CODE (type) == METHOD_TYPE)
3470 cp_error_at ("field `%D' invalidly declared method type", x);
3471 type = build_pointer_type (type);
3472 TREE_TYPE (x) = type;
3474 else if (TREE_CODE (type) == OFFSET_TYPE)
3476 cp_error_at ("field `%D' invalidly declared offset type", x);
3477 type = build_pointer_type (type);
3478 TREE_TYPE (x) = type;
3482 if (DECL_NAME (x) == constructor_name (t))
3483 cant_have_default_ctor = 1;
3486 if (type == error_mark_node)
3489 DECL_SAVED_INSNS (x) = 0;
3490 DECL_FIELD_SIZE (x) = 0;
3492 /* When this goes into scope, it will be a non-local reference. */
3493 DECL_NONLOCAL (x) = 1;
3495 if (TREE_CODE (x) == CONST_DECL)
3498 if (TREE_CODE (x) == VAR_DECL)
3500 if (TREE_CODE (t) == UNION_TYPE)
3501 /* Unions cannot have static members. */
3502 cp_error_at ("field `%D' declared static in union", x);
3507 /* Now it can only be a FIELD_DECL. */
3509 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3512 /* If this is of reference type, check if it needs an init.
3513 Also do a little ANSI jig if necessary. */
3514 if (TREE_CODE (type) == REFERENCE_TYPE)
3518 if (DECL_INITIAL (x) == NULL_TREE)
3521 /* ARM $12.6.2: [A member initializer list] (or, for an
3522 aggregate, initialization by a brace-enclosed list) is the
3523 only way to initialize nonstatic const and reference
3525 cant_have_default_ctor = 1;
3526 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3528 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3531 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3533 cp_warning_at ("non-static reference in class without a constructor", x);
3537 while (TREE_CODE (type) == ARRAY_TYPE)
3538 type = TREE_TYPE (type);
3540 if (TREE_CODE (type) == POINTER_TYPE)
3543 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3546 if (! pod_type_p (type) || TYPE_PTRMEM_P (type)
3547 || TYPE_PTRMEMFUNC_P (type))
3550 /* If any field is const, the structure type is pseudo-const. */
3551 if (CP_TYPE_CONST_P (type))
3553 C_TYPE_FIELDS_READONLY (t) = 1;
3554 if (DECL_INITIAL (x) == NULL_TREE)
3555 const_sans_init = 1;
3557 /* ARM $12.6.2: [A member initializer list] (or, for an
3558 aggregate, initialization by a brace-enclosed list) is the
3559 only way to initialize nonstatic const and reference
3561 cant_have_default_ctor = 1;
3562 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3564 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3567 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3569 cp_warning_at ("non-static const member in class without a constructor", x);
3574 /* A field that is pseudo-const makes the structure
3576 if (IS_AGGR_TYPE (type))
3578 if (C_TYPE_FIELDS_READONLY (type))
3579 C_TYPE_FIELDS_READONLY (t) = 1;
3580 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3581 const_sans_init = 1;
3585 /* We set DECL_C_BIT_FIELD in grokbitfield.
3586 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3587 if (DECL_C_BIT_FIELD (x))
3589 /* Invalid bit-field size done by grokfield. */
3590 /* Detect invalid bit-field type. Simply checking if TYPE is
3591 integral is insufficient, as that is the array core of the
3592 field type. If TREE_TYPE (x) is integral, then TYPE must be
3594 if (DECL_INITIAL (x)
3595 && ! INTEGRAL_TYPE_P (TREE_TYPE (x)))
3597 cp_error_at ("bit-field `%#D' with non-integral type", x);
3598 DECL_INITIAL (x) = NULL;
3601 /* Detect and ignore out of range field width. */
3602 if (DECL_INITIAL (x))
3604 tree w = DECL_INITIAL (x);
3605 register int width = 0;
3607 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3610 /* detect invalid field size. */
3611 if (TREE_CODE (w) == CONST_DECL)
3612 w = DECL_INITIAL (w);
3613 else if (TREE_READONLY_DECL_P (w))
3614 w = decl_constant_value (w);
3616 if (TREE_CODE (w) != INTEGER_CST)
3618 cp_error_at ("bit-field `%D' width not an integer constant",
3620 DECL_INITIAL (x) = NULL_TREE;
3622 else if (width = TREE_INT_CST_LOW (w),
3625 DECL_INITIAL (x) = NULL;
3626 cp_error_at ("negative width in bit-field `%D'", x);
3628 else if (width == 0 && DECL_NAME (x) != 0)
3630 DECL_INITIAL (x) = NULL;
3631 cp_error_at ("zero width for bit-field `%D'", x);
3634 > TYPE_PRECISION (long_long_unsigned_type_node))
3636 /* The backend will dump if you try to use something
3637 too big; avoid that. */
3638 DECL_INITIAL (x) = NULL;
3639 sorry ("bit-fields larger than %d bits",
3640 TYPE_PRECISION (long_long_unsigned_type_node));
3641 cp_error_at (" in declaration of `%D'", x);
3643 else if (width > TYPE_PRECISION (type)
3644 && TREE_CODE (type) != ENUMERAL_TYPE
3645 && TREE_CODE (type) != BOOLEAN_TYPE)
3647 cp_warning_at ("width of `%D' exceeds its type", x);
3649 else if (TREE_CODE (type) == ENUMERAL_TYPE
3650 && ((min_precision (TYPE_MIN_VALUE (type),
3651 TREE_UNSIGNED (type)) > width)
3652 || (min_precision (TYPE_MAX_VALUE (type),
3653 TREE_UNSIGNED (type)) > width)))
3655 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3659 if (DECL_INITIAL (x))
3661 DECL_INITIAL (x) = NULL_TREE;
3662 DECL_FIELD_SIZE (x) = width;
3663 DECL_BIT_FIELD (x) = 1;
3667 #ifdef EMPTY_FIELD_BOUNDARY
3668 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3669 EMPTY_FIELD_BOUNDARY);
3671 #ifdef PCC_BITFIELD_TYPE_MATTERS
3672 if (PCC_BITFIELD_TYPE_MATTERS)
3673 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3680 /* Non-bit-fields are aligned for their type. */
3681 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (type));
3685 if (CLASS_TYPE_P (type) && ! ANON_AGGR_TYPE_P (type))
3687 /* Never let anything with uninheritable virtuals
3688 make it through without complaint. */
3689 abstract_virtuals_error (x, type);
3691 if (code == UNION_TYPE)
3693 const char *fie = NULL;
3694 if (TYPE_NEEDS_CONSTRUCTING (type))
3695 fie = "constructor";
3696 else if (TYPE_NEEDS_DESTRUCTOR (type))
3698 else if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3699 fie = "copy assignment operator";
3701 cp_error_at ("member `%#D' with %s not allowed in union", x,
3706 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3707 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3708 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3709 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3712 if (!TYPE_HAS_CONST_INIT_REF (type))
3713 cant_have_const_ctor = 1;
3715 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3716 no_const_asn_ref = 1;
3718 if (TYPE_HAS_CONSTRUCTOR (type)
3719 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3721 cant_have_default_ctor = 1;
3723 /* This is wrong for aggregates. */
3724 if (! TYPE_HAS_CONSTRUCTOR (t))
3727 cp_pedwarn_at ("member `%#D' with only non-default constructor", x);
3729 cp_pedwarn_at ("member with only non-default constructor", x);
3730 cp_pedwarn_at ("in class without a constructor",
3736 if (DECL_INITIAL (x) != NULL_TREE)
3738 /* `build_class_init_list' does not recognize
3740 if (code == UNION_TYPE && any_default_members != 0)
3741 cp_error_at ("multiple fields in union `%T' initialized");
3742 any_default_members = 1;
3747 /* If this type has any constant members which did not come
3748 with their own initialization, mark that fact here. It is
3749 not an error here, since such types can be saved either by their
3750 constructors, or by fortuitous initialization. */
3751 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
3752 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
3753 CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
3754 CLASSTYPE_HAS_MUTABLE (t) = has_mutable;
3756 /* Effective C++ rule 11. */
3757 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3758 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3760 cp_warning ("`%#T' has pointer data members", t);
3762 if (! TYPE_HAS_INIT_REF (t))
3764 cp_warning (" but does not override `%T(const %T&)'", t, t);
3765 if (! TYPE_HAS_ASSIGN_REF (t))
3766 cp_warning (" or `operator=(const %T&)'", t);
3768 else if (! TYPE_HAS_ASSIGN_REF (t))
3769 cp_warning (" but does not override `operator=(const %T&)'", t);
3772 /* Do some bookkeeping that will guide the generation of implicitly
3773 declared member functions. */
3774 TYPE_HAS_COMPLEX_INIT_REF (t)
3775 |= (TYPE_HAS_INIT_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3776 || has_virtual || any_default_members);
3777 TYPE_NEEDS_CONSTRUCTING (t)
3778 |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3779 || has_virtual || any_default_members);
3780 CLASSTYPE_NON_AGGREGATE (t)
3781 = ! aggregate || has_virtual || TYPE_HAS_CONSTRUCTOR (t);
3782 CLASSTYPE_NON_POD_P (t)
3783 = non_pod_class || CLASSTYPE_NON_AGGREGATE (t)
3784 || TYPE_HAS_DESTRUCTOR (t) || TYPE_HAS_ASSIGN_REF (t);
3785 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
3786 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
3787 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
3789 /* Synthesize any needed methods. Note that methods will be synthesized
3790 for anonymous unions; grok_x_components undoes that. */
3792 = add_implicitly_declared_members (t, cant_have_default_ctor,
3793 cant_have_const_ctor,
3796 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3797 &has_virtual, virtual_dtor, t);
3799 if (TYPE_METHODS (t))
3801 finish_struct_methods (t);
3802 method_vec = CLASSTYPE_METHOD_VEC (t);
3808 /* Just in case these got accidentally
3809 filled in by syntax errors. */
3810 TYPE_HAS_CONSTRUCTOR (t) = 0;
3811 TYPE_HAS_DESTRUCTOR (t) = 0;
3814 for (access_decls = nreverse (access_decls); access_decls;
3815 access_decls = TREE_CHAIN (access_decls))
3816 handle_using_decl (TREE_VALUE (access_decls), t, method_vec, fields);
3818 if (vfield == NULL_TREE && has_virtual)
3820 /* We build this decl with vtbl_ptr_type_node, which is a
3821 `vtable_entry_type*'. It might seem more precise to use
3822 `vtable_entry_type (*)[N]' where N is the number of firtual
3823 functions. However, that would require the vtable pointer in
3824 base classes to have a different type than the vtable pointer
3825 in derived classes. We could make that happen, but that
3826 still wouldn't solve all the problems. In particular, the
3827 type-based alias analysis code would decide that assignments
3828 to the base class vtable pointer can't alias assignments to
3829 the derived class vtable pointer, since they have different
3830 types. Thus, in an derived class destructor, where the base
3831 class constructor was inlined, we could generate bad code for
3832 setting up the vtable pointer.
3834 Therefore, we use one type for all vtable pointers. We still
3835 use a type-correct type; it's just doesn't indicate the array
3836 bounds. That's better than using `void*' or some such; it's
3837 cleaner, and it let's the alias analysis code know that these
3838 stores cannot alias stores to void*! */
3839 vfield = build_lang_decl (FIELD_DECL, get_vfield_name (t),
3840 vtbl_ptr_type_node);
3841 /* If you change any of the below, take a look at all the
3842 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
3844 DECL_ASSEMBLER_NAME (vfield) = get_identifier (VFIELD_BASE);
3845 CLASSTYPE_VFIELD (t) = vfield;
3846 DECL_VIRTUAL_P (vfield) = 1;
3847 DECL_ARTIFICIAL (vfield) = 1;
3848 DECL_FIELD_CONTEXT (vfield) = t;
3849 DECL_CLASS_CONTEXT (vfield) = t;
3850 DECL_FCONTEXT (vfield) = t;
3851 DECL_SAVED_INSNS (vfield) = 0;
3852 DECL_FIELD_SIZE (vfield) = 0;
3853 DECL_ALIGN (vfield) = TYPE_ALIGN (ptr_type_node);
3855 /* This is more efficient, but breaks binary compatibility, turn
3856 it on sometime when we don't care. If we turn it on, we also
3857 have to enable the code in dfs_init_vbase_pointers. */
3858 /* vfield is always first entry in structure. */
3859 TREE_CHAIN (vfield) = fields;
3864 my_friendly_assert (TREE_CHAIN (last_x) == NULL_TREE, 175);
3865 TREE_CHAIN (last_x) = vfield;
3872 vfields = chainon (vfields, build_tree_list (NULL_TREE, t));
3875 /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
3877 C++: maybe we will support default field initialization some day... */
3879 /* Delete all duplicate fields from the fields */
3880 delete_duplicate_fields (fields);
3882 /* Now we have the nearly final fieldlist for the data fields. Record it,
3883 then lay out the structure or union (including the fields). */
3885 TYPE_FIELDS (t) = fields;
3889 last_x = build_base_fields (t);
3891 /* If all our bases are empty, we can be empty too. */
3892 for (x = last_x; empty && x; x = TREE_CHAIN (x))
3893 if (DECL_SIZE (x) != integer_zero_node)
3897 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
3898 we have to save this before we start modifying
3899 TYPE_NONCOPIED_PARTS. */
3900 inline_friends = CLASSTYPE_INLINE_FRIENDS (t);
3901 CLASSTYPE_INLINE_FRIENDS (t) = NULL_TREE;
3905 /* C++: do not let empty structures exist. */
3906 tree decl = build_lang_decl
3907 (FIELD_DECL, NULL_TREE, char_type_node);
3908 TREE_CHAIN (decl) = fields;
3909 TYPE_FIELDS (t) = decl;
3910 TYPE_NONCOPIED_PARTS (t)
3911 = tree_cons (NULL_TREE, decl, TYPE_NONCOPIED_PARTS (t));
3912 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
3916 TYPE_FIELDS (t) = chainon (last_x, TYPE_FIELDS (t));
3920 /* Remember the size and alignment of the class before adding
3921 the virtual bases. */
3922 if (empty && flag_new_abi)
3923 CLASSTYPE_SIZE (t) = integer_zero_node;
3924 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
3925 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
3926 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
3928 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
3929 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
3931 finish_struct_anon (t);
3933 /* Set the TYPE_DECL for this type to contain the right
3934 value for DECL_OFFSET, so that we can use it as part
3935 of a COMPONENT_REF for multiple inheritance. */
3937 layout_decl (TYPE_MAIN_DECL (t), 0);
3939 /* Now fix up any virtual base class types that we left lying
3940 around. We must get these done before we try to lay out the
3941 virtual function table. */
3942 pending_hard_virtuals = nreverse (pending_hard_virtuals);
3945 /* layout_basetypes will remove the base subobject fields. */
3946 max_has_virtual = layout_basetypes (t, max_has_virtual);
3948 TYPE_FIELDS (t) = fields;
3950 my_friendly_assert (TYPE_FIELDS (t) == fields, 981117);
3952 /* Delete all zero-width bit-fields from the front of the fieldlist */
3953 while (fields && DECL_C_BIT_FIELD (fields)
3954 && DECL_INITIAL (fields))
3955 fields = TREE_CHAIN (fields);
3956 /* Delete all such fields from the rest of the fields. */
3957 for (x = fields; x;)
3959 if (TREE_CHAIN (x) && DECL_C_BIT_FIELD (TREE_CHAIN (x))
3960 && DECL_INITIAL (TREE_CHAIN (x)))
3961 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
3965 TYPE_FIELDS (t) = fields;
3967 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
3971 vbases = CLASSTYPE_VBASECLASSES (t);
3974 /* Now fixup overrides of all functions in vtables from all
3975 direct or indirect virtual base classes. */
3976 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3977 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3979 for (i = 0; i < n_baseclasses; i++)
3981 tree base_binfo = TREE_VEC_ELT (binfos, i);
3982 tree basetype = BINFO_TYPE (base_binfo);
3985 vbases = CLASSTYPE_VBASECLASSES (basetype);
3988 merge_overrides (binfo_member (BINFO_TYPE (vbases),
3989 CLASSTYPE_VBASECLASSES (t)),
3991 vbases = TREE_CHAIN (vbases);
3997 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
3998 might need to know it for setting up the offsets in the vtable
3999 (or in thunks) below. */
4000 if (vfield != NULL_TREE
4001 && DECL_FIELD_CONTEXT (vfield) != t)
4003 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4004 tree offset = BINFO_OFFSET (binfo);
4006 vfield = copy_node (vfield);
4007 copy_lang_decl (vfield);
4009 if (! integer_zerop (offset))
4010 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4011 DECL_FIELD_CONTEXT (vfield) = t;
4012 DECL_CLASS_CONTEXT (vfield) = t;
4013 DECL_FIELD_BITPOS (vfield)
4014 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4015 CLASSTYPE_VFIELD (t) = vfield;
4019 cp_warning ("Doing hard virtuals for %T...", t);
4022 if (has_virtual > max_has_virtual)
4023 max_has_virtual = has_virtual;
4024 if (max_has_virtual > 0)
4025 TYPE_VIRTUAL_P (t) = 1;
4027 if (flag_rtti && TYPE_VIRTUAL_P (t) && !pending_hard_virtuals)
4028 modify_all_vtables (t, NULL_TREE);
4030 while (pending_hard_virtuals)
4032 modify_all_vtables (t,
4033 TREE_VALUE (pending_hard_virtuals));
4034 pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
4037 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
4040 /* Now fixup any virtual function entries from virtual bases
4041 that have different deltas. This has to come after we do the
4042 pending hard virtuals, as we might have a function that comes
4043 from multiple virtual base instances that is only overridden
4044 by a hard virtual above. */
4045 vbases = CLASSTYPE_VBASECLASSES (t);
4048 /* We might be able to shorten the amount of work we do by
4049 only doing this for vtables that come from virtual bases
4050 that have differing offsets, but don't want to miss any
4052 fixup_vtable_deltas (vbases, 1, t);
4053 vbases = TREE_CHAIN (vbases);
4057 /* Under our model of GC, every C++ class gets its own virtual
4058 function table, at least virtually. */
4059 if (pending_virtuals)
4061 pending_virtuals = nreverse (pending_virtuals);
4062 /* We must enter these virtuals into the table. */
4063 if (first_vfn_base_index < 0)
4065 if (! CLASSTYPE_COM_INTERFACE (t))
4067 /* The second slot is for the tdesc pointer when thunks are used. */
4068 if (flag_vtable_thunks)
4069 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4071 /* The first slot is for the rtti offset. */
4072 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4074 set_rtti_entry (pending_virtuals,
4075 convert (ssizetype, integer_zero_node), t);
4077 build_vtable (NULL_TREE, t);
4081 /* Here we know enough to change the type of our virtual
4082 function table, but we will wait until later this function. */
4084 if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
4085 build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index), t);
4088 /* If this type has basetypes with constructors, then those
4089 constructors might clobber the virtual function table. But
4090 they don't if the derived class shares the exact vtable of the base
4093 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4095 else if (first_vfn_base_index >= 0)
4097 tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index);
4098 /* This class contributes nothing new to the virtual function
4099 table. However, it may have declared functions which
4100 went into the virtual function table "inherited" from the
4101 base class. If so, we grab a copy of those updated functions,
4102 and pretend they are ours. */
4104 /* See if we should steal the virtual info from base class. */
4105 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
4106 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
4107 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
4108 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
4109 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
4110 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4113 if (max_has_virtual || first_vfn_base_index >= 0)
4115 CLASSTYPE_VSIZE (t) = has_virtual;
4116 if (first_vfn_base_index >= 0)
4118 if (pending_virtuals)
4119 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t),
4122 else if (has_virtual)
4124 TYPE_BINFO_VIRTUALS (t) = pending_virtuals;
4125 DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1;
4129 /* Now lay out the virtual function table. */
4132 /* Use size_int so values are memoized in common cases. */
4133 tree itype = build_index_type (size_int (has_virtual));
4134 tree atype = build_cplus_array_type (vtable_entry_type, itype);
4136 layout_type (atype);
4138 CLASSTYPE_VFIELD (t) = vfield;
4140 /* We may have to grow the vtable. */
4141 if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
4143 TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
4144 DECL_SIZE (TYPE_BINFO_VTABLE (t)) = 0;
4145 layout_decl (TYPE_BINFO_VTABLE (t), 0);
4146 /* At one time the vtable info was grabbed 2 words at a time. This
4147 fails on sparc unless you have 8-byte alignment. (tiemann) */
4148 DECL_ALIGN (TYPE_BINFO_VTABLE (t))
4149 = MAX (TYPE_ALIGN (double_type_node),
4150 DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
4153 else if (first_vfn_base_index >= 0)
4154 CLASSTYPE_VFIELD (t) = vfield;
4155 CLASSTYPE_VFIELDS (t) = vfields;
4157 finish_struct_bits (t, max_has_virtual);
4159 /* Complete the rtl for any static member objects of the type we're
4161 for (x = fields; x; x = TREE_CHAIN (x))
4163 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4164 && TREE_TYPE (x) == t)
4166 DECL_MODE (x) = TYPE_MODE (t);
4167 make_decl_rtl (x, NULL, 0);
4171 /* Done with FIELDS...now decide whether to sort these for
4172 faster lookups later. Don't worry about optimizing
4173 for structs only declared in inline functions...they're
4174 not going to be referenced anywhere else.
4176 The C front-end only does this when n_fields > 15. We use
4177 a smaller number because most searches fail (succeeding
4178 ultimately as the search bores through the inheritance
4179 hierarchy), and we want this failure to occur quickly. */
4181 n_fields = count_fields (fields);
4182 if (n_fields > 7 && !allocation_temporary_p ())
4184 tree field_vec = make_tree_vec (n_fields);
4185 add_fields_to_vec (fields, field_vec, 0);
4186 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
4187 (int (*)(const void *, const void *))field_decl_cmp);
4188 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
4189 retrofit_lang_decl (TYPE_MAIN_DECL (t));
4190 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
4193 if (TYPE_HAS_CONSTRUCTOR (t))
4195 tree vfields = CLASSTYPE_VFIELDS (t);
4199 /* Mark the fact that constructor for T
4200 could affect anybody inheriting from T
4201 who wants to initialize vtables for VFIELDS's type. */
4202 if (VF_DERIVED_VALUE (vfields))
4203 TREE_ADDRESSABLE (vfields) = 1;
4204 vfields = TREE_CHAIN (vfields);
4208 /* Write out inline function definitions. */
4209 do_inline_function_hair (t, inline_friends);
4211 if (CLASSTYPE_VSIZE (t) != 0)
4213 /* In addition to this one, all the other vfields should be listed. */
4214 /* Before that can be done, we have to have FIELD_DECLs for them, and
4215 a place to find them. */
4216 TYPE_NONCOPIED_PARTS (t)
4217 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
4218 vfield, TYPE_NONCOPIED_PARTS (t));
4220 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
4221 && DECL_VINDEX (TREE_VEC_ELT (method_vec, 1)) == NULL_TREE)
4222 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
4226 /* Make the rtl for any new vtables we have created, and unmark
4227 the base types we marked. */
4228 finish_vtbls (TYPE_BINFO (t), 1, t);
4229 hack_incomplete_structures (t);
4232 if (TYPE_NAME (t) && TYPE_IDENTIFIER (t))
4233 undo_template_name_overload (TYPE_IDENTIFIER (t), 1);
4236 resume_momentary (old);
4238 if (warn_overloaded_virtual)
4242 /* This has to be done after we have sorted out what to do with
4243 the enclosing type. */
4244 if (write_symbols != DWARF_DEBUG)
4246 /* Be smarter about nested classes here. If a type is nested,
4247 only output it if we would output the enclosing type. */
4248 if (DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (t)))
4249 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = TREE_ASM_WRITTEN (TYPE_MAIN_DECL (t));
4253 if (write_symbols != DWARF_DEBUG && write_symbols != DWARF2_DEBUG)
4255 /* If the type has methods, we want to think about cutting down
4256 the amount of symbol table stuff we output. The value stored in
4257 the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
4258 For example, if a member function is seen and we decide to
4259 write out that member function, then we can change the value
4260 of the DECL_IGNORED_P slot, and the type will be output when
4261 that member function's debug info is written out.
4263 We can't do this with DWARF, which does not support name
4264 references between translation units. */
4265 if (CLASSTYPE_METHOD_VEC (t))
4267 /* Don't output full info about any type
4268 which does not have its implementation defined here. */
4269 if (CLASSTYPE_INTERFACE_ONLY (t))
4270 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4272 /* XXX do something about this. */
4273 else if (CLASSTYPE_INTERFACE_UNKNOWN (t))
4274 /* Only a first approximation! */
4275 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4278 else if (CLASSTYPE_INTERFACE_ONLY (t))
4279 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4282 /* Finish debugging output for this type. */
4283 rest_of_type_compilation (t, toplevel_bindings_p ());
4288 /* When T was built up, the member declarations were added in reverse
4289 order. Rearrange them to declaration order. */
4292 unreverse_member_declarations (t)
4299 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
4300 reverse order. Put them in declaration order now. */
4301 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
4302 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
4304 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
4305 reverse order, so we can't just use nreverse. */
4307 for (x = TYPE_FIELDS (t);
4308 x && TREE_CODE (x) != TYPE_DECL;
4311 next = TREE_CHAIN (x);
4312 TREE_CHAIN (x) = prev;
4317 TREE_CHAIN (TYPE_FIELDS (t)) = x;
4319 TYPE_FIELDS (t) = prev;
4324 finish_struct (t, attributes)
4327 /* Now that we've got all the field declarations, reverse everything
4329 unreverse_member_declarations (t);
4331 cplus_decl_attributes (t, attributes, NULL_TREE);
4333 if (processing_template_decl)
4335 finish_struct_methods (t);
4336 TYPE_SIZE (t) = integer_zero_node;
4339 finish_struct_1 (t);
4341 TYPE_BEING_DEFINED (t) = 0;
4343 if (current_class_type)
4346 error ("trying to finish struct, but kicked out due to previous parse errors.");
4351 /* Return the dynamic type of INSTANCE, if known.
4352 Used to determine whether the virtual function table is needed
4355 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4356 of our knowledge of its type. */
4359 fixed_type_or_null (instance, nonnull)
4363 switch (TREE_CODE (instance))
4366 /* Check that we are not going through a cast of some sort. */
4367 if (TREE_TYPE (instance)
4368 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
4369 instance = TREE_OPERAND (instance, 0);
4370 /* fall through... */
4372 /* This is a call to a constructor, hence it's never zero. */
4373 if (TREE_HAS_CONSTRUCTOR (instance))
4377 return TREE_TYPE (instance);
4382 /* This is a call to a constructor, hence it's never zero. */
4383 if (TREE_HAS_CONSTRUCTOR (instance))
4387 return TREE_TYPE (instance);
4389 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4396 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
4397 /* Propagate nonnull. */
4398 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4399 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
4400 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4405 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4410 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4413 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
4417 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
4418 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
4422 return TREE_TYPE (TREE_TYPE (instance));
4424 /* fall through... */
4427 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
4431 return TREE_TYPE (instance);
4435 if (instance == current_class_ptr
4436 && flag_this_is_variable <= 0)
4438 /* Normally, 'this' must be non-null. */
4439 if (flag_this_is_variable == 0)
4442 /* <0 means we're in a constructor and we know our type. */
4443 if (flag_this_is_variable < 0)
4444 return TREE_TYPE (TREE_TYPE (instance));
4446 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4447 /* Reference variables should be references to objects. */
4457 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
4458 to the static type. We also handle the case where INSTANCE is really
4461 Used to determine whether the virtual function table is needed
4464 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4465 of our knowledge of its type. */
4468 resolves_to_fixed_type_p (instance, nonnull)
4472 tree t = TREE_TYPE (instance);
4473 tree fixed = fixed_type_or_null (instance, nonnull);
4474 if (fixed == NULL_TREE)
4476 if (POINTER_TYPE_P (t))
4478 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
4483 init_class_processing ()
4485 current_class_depth = 0;
4486 current_class_stack_size = 10;
4488 = (class_stack_node_t) xmalloc (current_class_stack_size
4489 * sizeof (struct class_stack_node));
4491 current_lang_stacksize = 10;
4492 current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree));
4493 current_lang_stack = current_lang_base;
4495 access_default_node = build_int_2 (0, 0);
4496 access_public_node = build_int_2 (1, 0);
4497 access_protected_node = build_int_2 (2, 0);
4498 access_private_node = build_int_2 (3, 0);
4499 access_default_virtual_node = build_int_2 (4, 0);
4500 access_public_virtual_node = build_int_2 (5, 0);
4501 access_protected_virtual_node = build_int_2 (6, 0);
4502 access_private_virtual_node = build_int_2 (7, 0);
4504 /* Keep these values lying around. */
4505 base_layout_decl = build_lang_decl (FIELD_DECL, NULL_TREE, error_mark_node);
4506 TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE);
4508 gcc_obstack_init (&class_obstack);
4511 /* Set current scope to NAME. CODE tells us if this is a
4512 STRUCT, UNION, or ENUM environment.
4514 NAME may end up being NULL_TREE if this is an anonymous or
4515 late-bound struct (as in "struct { ... } foo;") */
4517 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
4518 appropriate values, found by looking up the type definition of
4521 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
4522 which can be seen locally to the class. They are shadowed by
4523 any subsequent local declaration (including parameter names).
4525 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
4526 which have static meaning (i.e., static members, static
4527 member functions, enum declarations, etc).
4529 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
4530 which can be seen locally to the class (as in 1), but
4531 know that we are doing this for declaration purposes
4532 (i.e. friend foo::bar (int)).
4534 So that we may avoid calls to lookup_name, we cache the _TYPE
4535 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
4537 For multiple inheritance, we perform a two-pass depth-first search
4538 of the type lattice. The first pass performs a pre-order search,
4539 marking types after the type has had its fields installed in
4540 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
4541 unmarks the marked types. If a field or member function name
4542 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
4543 that name becomes `error_mark_node'. */
4546 pushclass (type, modify)
4550 type = TYPE_MAIN_VARIANT (type);
4552 /* Make sure there is enough room for the new entry on the stack. */
4553 if (current_class_depth + 1 >= current_class_stack_size)
4555 current_class_stack_size *= 2;
4557 = (class_stack_node_t) xrealloc (current_class_stack,
4558 current_class_stack_size
4559 * sizeof (struct class_stack_node));
4562 /* Insert a new entry on the class stack. */
4563 current_class_stack[current_class_depth].name = current_class_name;
4564 current_class_stack[current_class_depth].type = current_class_type;
4565 current_class_stack[current_class_depth].access = current_access_specifier;
4566 current_class_stack[current_class_depth].names_used = 0;
4567 current_class_depth++;
4569 /* Now set up the new type. */
4570 current_class_name = TYPE_NAME (type);
4571 if (TREE_CODE (current_class_name) == TYPE_DECL)
4572 current_class_name = DECL_NAME (current_class_name);
4573 current_class_type = type;
4575 /* By default, things in classes are private, while things in
4576 structures or unions are public. */
4577 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
4578 ? access_private_node
4579 : access_public_node);
4581 if (previous_class_type != NULL_TREE
4582 && (type != previous_class_type
4583 || TYPE_SIZE (previous_class_type) == NULL_TREE)
4584 && current_class_depth == 1)
4586 /* Forcibly remove any old class remnants. */
4587 invalidate_class_lookup_cache ();
4589 /* Now, free the obstack on which we cached all the values. */
4590 if (class_cache_firstobj)
4591 obstack_free (&class_cache_obstack, class_cache_firstobj);
4592 class_cache_firstobj
4593 = (char*) obstack_finish (&class_cache_obstack);
4596 /* If we're about to enter a nested class, clear
4597 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
4598 if (modify && current_class_depth > 1)
4599 clear_identifier_class_values ();
4604 if (CLASSTYPE_TEMPLATE_INFO (type))
4605 overload_template_name (type);
4610 if (type != previous_class_type || current_class_depth > 1)
4611 push_class_decls (type);
4616 /* We are re-entering the same class we just left, so we
4617 don't have to search the whole inheritance matrix to find
4618 all the decls to bind again. Instead, we install the
4619 cached class_shadowed list, and walk through it binding
4620 names and setting up IDENTIFIER_TYPE_VALUEs. */
4621 set_class_shadows (previous_class_values);
4622 for (item = previous_class_values; item; item = TREE_CHAIN (item))
4624 tree id = TREE_PURPOSE (item);
4625 tree decl = TREE_TYPE (item);
4627 push_class_binding (id, decl);
4628 if (TREE_CODE (decl) == TYPE_DECL)
4629 set_identifier_type_value (id, TREE_TYPE (decl));
4631 unuse_fields (type);
4634 storetags (CLASSTYPE_TAGS (type));
4638 /* When we exit a toplevel class scope, we save the
4639 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
4640 reenter the class. Here, we've entered some other class, so we
4641 must invalidate our cache. */
4644 invalidate_class_lookup_cache ()
4648 /* This code can be seen as a cache miss. When we've cached a
4649 class' scope's bindings and we can't use them, we need to reset
4650 them. This is it! */
4651 for (t = previous_class_values; t; t = TREE_CHAIN (t))
4652 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
4654 previous_class_type = NULL_TREE;
4657 /* Get out of the current class scope. If we were in a class scope
4658 previously, that is the one popped to. */
4664 /* Since poplevel_class does the popping of class decls nowadays,
4665 this really only frees the obstack used for these decls. */
4668 current_class_depth--;
4669 current_class_name = current_class_stack[current_class_depth].name;
4670 current_class_type = current_class_stack[current_class_depth].type;
4671 current_access_specifier = current_class_stack[current_class_depth].access;
4672 if (current_class_stack[current_class_depth].names_used)
4673 splay_tree_delete (current_class_stack[current_class_depth].names_used);
4676 /* Returns 1 if current_class_type is either T or a nested type of T. */
4679 currently_open_class (t)
4683 if (t == current_class_type)
4685 for (i = 0; i < current_class_depth; ++i)
4686 if (current_class_stack [i].type == t)
4691 /* When entering a class scope, all enclosing class scopes' names with
4692 static meaning (static variables, static functions, types and enumerators)
4693 have to be visible. This recursive function calls pushclass for all
4694 enclosing class contexts until global or a local scope is reached.
4695 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
4696 formal of the same name. */
4699 push_nested_class (type, modify)
4705 /* A namespace might be passed in error cases, like A::B:C. */
4706 if (type == NULL_TREE || type == error_mark_node || ! IS_AGGR_TYPE (type)
4707 || TREE_CODE (type) == NAMESPACE_DECL
4708 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
4709 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
4712 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
4714 if (context && CLASS_TYPE_P (context))
4715 push_nested_class (context, 2);
4716 pushclass (type, modify);
4719 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
4724 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
4727 if (context && CLASS_TYPE_P (context))
4728 pop_nested_class ();
4731 /* Set global variables CURRENT_LANG_NAME to appropriate value
4732 so that behavior of name-mangling machinery is correct. */
4735 push_lang_context (name)
4738 *current_lang_stack++ = current_lang_name;
4739 if (current_lang_stack >= current_lang_base + current_lang_stacksize)
4742 = (tree *)xrealloc (current_lang_base,
4743 sizeof (tree) * (current_lang_stacksize + 10));
4744 current_lang_stack = current_lang_base + current_lang_stacksize;
4745 current_lang_stacksize += 10;
4748 if (name == lang_name_cplusplus)
4750 strict_prototype = strict_prototypes_lang_cplusplus;
4751 current_lang_name = name;
4753 else if (name == lang_name_java)
4755 strict_prototype = strict_prototypes_lang_cplusplus;
4756 current_lang_name = name;
4757 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
4758 (See record_builtin_java_type in decl.c.) However, that causes
4759 incorrect debug entries if these types are actually used.
4760 So we re-enable debug output after extern "Java". */
4761 DECL_IGNORED_P (java_byte_type_node) = 0;
4762 DECL_IGNORED_P (java_short_type_node) = 0;
4763 DECL_IGNORED_P (java_int_type_node) = 0;
4764 DECL_IGNORED_P (java_long_type_node) = 0;
4765 DECL_IGNORED_P (java_float_type_node) = 0;
4766 DECL_IGNORED_P (java_double_type_node) = 0;
4767 DECL_IGNORED_P (java_char_type_node) = 0;
4768 DECL_IGNORED_P (java_boolean_type_node) = 0;
4770 else if (name == lang_name_c)
4772 strict_prototype = strict_prototypes_lang_c;
4773 current_lang_name = name;
4776 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
4779 /* Get out of the current language scope. */
4784 current_lang_name = *--current_lang_stack;
4785 if (current_lang_name == lang_name_cplusplus
4786 || current_lang_name == lang_name_java)
4787 strict_prototype = strict_prototypes_lang_cplusplus;
4788 else if (current_lang_name == lang_name_c)
4789 strict_prototype = strict_prototypes_lang_c;
4792 /* Type instantiation routines. */
4794 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
4795 matches the TARGET_TYPE. If there is no satisfactory match, return
4796 error_mark_node, and issue an error message if COMPLAIN is
4797 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
4798 was a template-id, and EXPLICIT_TARGS are the explicitly provided
4799 template arguments. */
4802 resolve_address_of_overloaded_function (target_type,
4811 tree explicit_targs;
4813 /* Here's what the standard says:
4817 If the name is a function template, template argument deduction
4818 is done, and if the argument deduction succeeds, the deduced
4819 arguments are used to generate a single template function, which
4820 is added to the set of overloaded functions considered.
4822 Non-member functions and static member functions match targets of
4823 type "pointer-to-function" or "reference-to-function." Nonstatic
4824 member functions match targets of type "pointer-to-member
4825 function;" the function type of the pointer to member is used to
4826 select the member function from the set of overloaded member
4827 functions. If a nonstatic member function is selected, the
4828 reference to the overloaded function name is required to have the
4829 form of a pointer to member as described in 5.3.1.
4831 If more than one function is selected, any template functions in
4832 the set are eliminated if the set also contains a non-template
4833 function, and any given template function is eliminated if the
4834 set contains a second template function that is more specialized
4835 than the first according to the partial ordering rules 14.5.5.2.
4836 After such eliminations, if any, there shall remain exactly one
4837 selected function. */
4840 int is_reference = 0;
4841 /* We store the matches in a TREE_LIST rooted here. The functions
4842 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
4843 interoperability with most_specialized_instantiation. */
4844 tree matches = NULL_TREE;
4847 /* By the time we get here, we should be seeing only real
4848 pointer-to-member types, not the internal POINTER_TYPE to
4849 METHOD_TYPE representation. */
4850 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
4851 && (TREE_CODE (TREE_TYPE (target_type))
4852 == METHOD_TYPE)), 0);
4854 /* Check that the TARGET_TYPE is reasonable. */
4855 if (TYPE_PTRFN_P (target_type))
4858 else if (TYPE_PTRMEMFUNC_P (target_type))
4859 /* This is OK, too. */
4861 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
4863 /* This is OK, too. This comes from a conversion to reference
4865 target_type = build_reference_type (target_type);
4871 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
4872 DECL_NAME (OVL_FUNCTION (overload)), target_type);
4873 return error_mark_node;
4876 /* If we can find a non-template function that matches, we can just
4877 use it. There's no point in generating template instantiations
4878 if we're just going to throw them out anyhow. But, of course, we
4879 can only do this when we don't *need* a template function. */
4884 for (fns = overload; fns; fns = OVL_CHAIN (fns))
4886 tree fn = OVL_FUNCTION (fns);
4889 if (TREE_CODE (fn) == TEMPLATE_DECL)
4890 /* We're not looking for templates just yet. */
4893 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4895 /* We're looking for a non-static member, and this isn't
4896 one, or vice versa. */
4899 /* See if there's a match. */
4900 fntype = TREE_TYPE (fn);
4902 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
4903 else if (!is_reference)
4904 fntype = build_pointer_type (fntype);
4906 if (can_convert_arg (target_type, fntype, fn))
4907 matches = scratch_tree_cons (fn, NULL_TREE, matches);
4911 /* Now, if we've already got a match (or matches), there's no need
4912 to proceed to the template functions. But, if we don't have a
4913 match we need to look at them, too. */
4916 tree target_fn_type;
4917 tree target_arg_types;
4922 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
4924 target_fn_type = TREE_TYPE (target_type);
4925 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
4927 for (fns = overload; fns; fns = OVL_CHAIN (fns))
4929 tree fn = OVL_FUNCTION (fns);
4931 tree instantiation_type;
4934 if (TREE_CODE (fn) != TEMPLATE_DECL)
4935 /* We're only looking for templates. */
4938 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4940 /* We're not looking for a non-static member, and this is
4941 one, or vice versa. */
4944 /* Try to do argument deduction. */
4945 targs = make_scratch_vec (DECL_NTPARMS (fn));
4946 if (fn_type_unification (fn, explicit_targs, targs,
4947 target_arg_types, NULL_TREE,
4949 /* Argument deduction failed. */
4952 /* Instantiate the template. */
4953 instantiation = instantiate_template (fn, targs);
4954 if (instantiation == error_mark_node)
4955 /* Instantiation failed. */
4958 /* See if there's a match. */
4959 instantiation_type = TREE_TYPE (instantiation);
4961 instantiation_type =
4962 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
4963 else if (!is_reference)
4964 instantiation_type = build_pointer_type (instantiation_type);
4965 if (can_convert_arg (target_type, instantiation_type, instantiation))
4966 matches = scratch_tree_cons (instantiation, fn, matches);
4969 /* Now, remove all but the most specialized of the matches. */
4972 tree match = most_specialized_instantiation (matches,
4975 if (match != error_mark_node)
4976 matches = scratch_tree_cons (match, NULL_TREE, NULL_TREE);
4980 /* Now we should have exactly one function in MATCHES. */
4981 if (matches == NULL_TREE)
4983 /* There were *no* matches. */
4986 cp_error ("no matches converting function `%D' to type `%#T'",
4987 DECL_NAME (OVL_FUNCTION (overload)),
4990 /* print_candidates expects a chain with the functions in
4991 TREE_VALUE slots, so we cons one up here (we're losing anyway,
4992 so why be clever?). */
4993 for (; overload; overload = OVL_NEXT (overload))
4994 matches = scratch_tree_cons (NULL_TREE, OVL_CURRENT (overload),
4997 print_candidates (matches);
4999 return error_mark_node;
5001 else if (TREE_CHAIN (matches))
5003 /* There were too many matches. */
5009 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
5010 DECL_NAME (OVL_FUNCTION (overload)),
5013 /* Since print_candidates expects the functions in the
5014 TREE_VALUE slot, we flip them here. */
5015 for (match = matches; match; match = TREE_CHAIN (match))
5016 TREE_VALUE (match) = TREE_PURPOSE (match);
5018 print_candidates (matches);
5021 return error_mark_node;
5024 /* Good, exactly one match. Now, convert it to the correct type. */
5025 fn = TREE_PURPOSE (matches);
5029 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
5030 return build_unary_op (ADDR_EXPR, fn, 0);
5033 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
5034 will mark the function as addressed, but here we must do it
5036 mark_addressable (fn);
5042 /* This function will instantiate the type of the expression given in
5043 RHS to match the type of LHSTYPE. If errors exist, then return
5044 error_mark_node. We only complain is COMPLAIN is set. If we are
5045 not complaining, never modify rhs, as overload resolution wants to
5046 try many possible instantiations, in hopes that at least one will
5049 FLAGS is a bitmask, as we see at the top of the function.
5051 For non-recursive calls, LHSTYPE should be a function, pointer to
5052 function, or a pointer to member function. */
5055 instantiate_type (lhstype, rhs, flags)
5059 int complain = (flags & 1);
5060 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
5062 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
5065 error ("not enough type information");
5066 return error_mark_node;
5069 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
5071 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
5074 cp_error ("argument of type `%T' does not match `%T'",
5075 TREE_TYPE (rhs), lhstype);
5076 return error_mark_node;
5079 /* We don't overwrite rhs if it is an overloaded function.
5080 Copying it would destroy the tree link. */
5081 if (TREE_CODE (rhs) != OVERLOAD)
5082 rhs = copy_node (rhs);
5084 /* This should really only be used when attempting to distinguish
5085 what sort of a pointer to function we have. For now, any
5086 arithmetic operation which is not supported on pointers
5087 is rejected as an error. */
5089 switch (TREE_CODE (rhs))
5096 my_friendly_abort (177);
5097 return error_mark_node;
5104 new_rhs = instantiate_type (build_pointer_type (lhstype),
5105 TREE_OPERAND (rhs, 0), flags);
5106 if (new_rhs == error_mark_node)
5107 return error_mark_node;
5109 TREE_TYPE (rhs) = lhstype;
5110 TREE_OPERAND (rhs, 0) = new_rhs;
5115 rhs = copy_node (TREE_OPERAND (rhs, 0));
5116 TREE_TYPE (rhs) = unknown_type_node;
5117 return instantiate_type (lhstype, rhs, flags);
5121 tree r = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5123 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype)
5124 && complain && !flag_ms_extensions)
5126 /* Note: we check this after the recursive call to avoid
5127 complaining about cases where overload resolution fails. */
5129 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
5130 tree fn = PTRMEM_CST_MEMBER (r);
5132 my_friendly_assert (TREE_CODE (r) == PTRMEM_CST, 990811);
5135 ("object-dependent reference to `%E' can only be used in a call",
5138 (" to form a pointer to member function, say `&%T::%E'",
5146 rhs = TREE_OPERAND (rhs, 1);
5147 if (BASELINK_P (rhs))
5148 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5150 /* This can happen if we are forming a pointer-to-member for a
5152 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
5156 case TEMPLATE_ID_EXPR:
5158 resolve_address_of_overloaded_function (lhstype,
5159 TREE_OPERAND (rhs, 0),
5161 /*template_only=*/1,
5162 TREE_OPERAND (rhs, 1));
5166 resolve_address_of_overloaded_function (lhstype,
5169 /*template_only=*/0,
5170 /*explicit_targs=*/NULL_TREE);
5173 /* Now we should have a baselink. */
5174 my_friendly_assert (BASELINK_P (rhs), 990412);
5176 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5179 /* This is too hard for now. */
5180 my_friendly_abort (183);
5181 return error_mark_node;
5186 TREE_OPERAND (rhs, 0)
5187 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5188 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5189 return error_mark_node;
5190 TREE_OPERAND (rhs, 1)
5191 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5192 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5193 return error_mark_node;
5195 TREE_TYPE (rhs) = lhstype;
5199 case TRUNC_DIV_EXPR:
5200 case FLOOR_DIV_EXPR:
5202 case ROUND_DIV_EXPR:
5204 case TRUNC_MOD_EXPR:
5205 case FLOOR_MOD_EXPR:
5207 case ROUND_MOD_EXPR:
5208 case FIX_ROUND_EXPR:
5209 case FIX_FLOOR_EXPR:
5211 case FIX_TRUNC_EXPR:
5227 case PREINCREMENT_EXPR:
5228 case PREDECREMENT_EXPR:
5229 case POSTINCREMENT_EXPR:
5230 case POSTDECREMENT_EXPR:
5232 error ("invalid operation on uninstantiated type");
5233 return error_mark_node;
5235 case TRUTH_AND_EXPR:
5237 case TRUTH_XOR_EXPR:
5244 case TRUTH_ANDIF_EXPR:
5245 case TRUTH_ORIF_EXPR:
5246 case TRUTH_NOT_EXPR:
5248 error ("not enough type information");
5249 return error_mark_node;
5252 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
5255 error ("not enough type information");
5256 return error_mark_node;
5258 TREE_OPERAND (rhs, 1)
5259 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5260 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5261 return error_mark_node;
5262 TREE_OPERAND (rhs, 2)
5263 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
5264 if (TREE_OPERAND (rhs, 2) == error_mark_node)
5265 return error_mark_node;
5267 TREE_TYPE (rhs) = lhstype;
5271 TREE_OPERAND (rhs, 1)
5272 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5273 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5274 return error_mark_node;
5276 TREE_TYPE (rhs) = lhstype;
5280 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5282 case ENTRY_VALUE_EXPR:
5283 my_friendly_abort (184);
5284 return error_mark_node;
5287 return error_mark_node;
5290 my_friendly_abort (185);
5291 return error_mark_node;
5295 /* Return the name of the virtual function pointer field
5296 (as an IDENTIFIER_NODE) for the given TYPE. Note that
5297 this may have to look back through base types to find the
5298 ultimate field name. (For single inheritance, these could
5299 all be the same name. Who knows for multiple inheritance). */
5302 get_vfield_name (type)
5305 tree binfo = TYPE_BINFO (type);
5308 while (BINFO_BASETYPES (binfo)
5309 && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
5310 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
5311 binfo = BINFO_BASETYPE (binfo, 0);
5313 type = BINFO_TYPE (binfo);
5314 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
5315 + TYPE_NAME_LENGTH (type) + 2);
5316 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
5317 return get_identifier (buf);
5321 print_class_statistics ()
5323 #ifdef GATHER_STATISTICS
5324 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
5325 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
5326 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
5327 n_build_method_call, n_inner_fields_searched);
5330 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
5331 n_vtables, n_vtable_searches);
5332 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
5333 n_vtable_entries, n_vtable_elems);
5338 /* Push an obstack which is sufficiently long-lived to hold such class
5339 decls that may be cached in the previous_class_values list. The
5340 effect is undone by pop_obstacks. */
5343 push_cache_obstack ()
5345 static int cache_obstack_initialized;
5347 if (!cache_obstack_initialized)
5349 gcc_obstack_init (&class_cache_obstack);
5350 class_cache_firstobj
5351 = (char*) obstack_finish (&class_cache_obstack);
5352 cache_obstack_initialized = 1;
5355 push_obstacks_nochange ();
5356 current_obstack = &class_cache_obstack;
5359 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
5360 according to [class]:
5361 The class-name is also inserted
5362 into the scope of the class itself. For purposes of access checking,
5363 the inserted class name is treated as if it were a public member name. */
5366 build_self_reference ()
5368 tree name = constructor_name (current_class_type);
5369 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
5372 DECL_NONLOCAL (value) = 1;
5373 DECL_CONTEXT (value) = current_class_type;
5374 DECL_CLASS_CONTEXT (value) = current_class_type;
5375 DECL_ARTIFICIAL (value) = 1;
5377 if (processing_template_decl)
5378 value = push_template_decl (value);
5380 saved_cas = current_access_specifier;
5381 current_access_specifier = access_public_node;
5382 finish_member_declaration (value);
5383 current_access_specifier = saved_cas;
5386 /* Returns 1 if TYPE contains only padding bytes. */
5389 is_empty_class (type)
5394 if (type == error_mark_node)
5397 if (! IS_AGGR_TYPE (type))
5401 return CLASSTYPE_SIZE (type) == integer_zero_node;
5403 if (TYPE_BINFO_BASETYPES (type))
5405 t = TYPE_FIELDS (type);
5406 while (t && TREE_CODE (t) != FIELD_DECL)
5408 return (t == NULL_TREE);
5411 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
5412 a *_TYPE node. NODE can also be a local class. */
5415 get_enclosing_class (type)
5420 while (node && TREE_CODE (node) != NAMESPACE_DECL)
5422 switch (TREE_CODE_CLASS (TREE_CODE (node)))
5425 node = DECL_CONTEXT (node);
5431 node = TYPE_CONTEXT (node);
5435 my_friendly_abort (0);
5441 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
5444 is_base_of_enclosing_class (base, type)
5449 if (get_binfo (base, type, 0))
5452 type = get_enclosing_class (type);
5457 /* Note that NAME was looked up while the current class was being
5458 defined and that the result of that lookup was DECL. */
5461 maybe_note_name_used_in_class (name, decl)
5465 splay_tree names_used;
5467 /* If we're not defining a class, there's nothing to do. */
5468 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
5471 /* If there's already a binding for this NAME, then we don't have
5472 anything to worry about. */
5473 if (IDENTIFIER_CLASS_VALUE (name))
5476 if (!current_class_stack[current_class_depth - 1].names_used)
5477 current_class_stack[current_class_depth - 1].names_used
5478 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
5479 names_used = current_class_stack[current_class_depth - 1].names_used;
5481 splay_tree_insert (names_used,
5482 (splay_tree_key) name,
5483 (splay_tree_value) decl);
5486 /* Note that NAME was declared (as DECL) in the current class. Check
5487 to see that the declaration is legal. */
5490 note_name_declared_in_class (name, decl)
5494 splay_tree names_used;
5497 /* Look to see if we ever used this name. */
5499 = current_class_stack[current_class_depth - 1].names_used;
5503 n = splay_tree_lookup (names_used, (splay_tree_key) name);
5506 /* [basic.scope.class]
5508 A name N used in a class S shall refer to the same declaration
5509 in its context and when re-evaluated in the completed scope of
5511 cp_error ("declaration of `%#D'", decl);
5512 cp_error_at ("changes meaning of `%s' from `%+#D'",
5513 IDENTIFIER_POINTER (DECL_NAME (decl)),