1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* High-level class interface. */
35 #define obstack_chunk_alloc xmalloc
36 #define obstack_chunk_free free
38 extern struct obstack permanent_obstack
;
40 /* This is how we tell when two virtual member functions are really the
42 #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
44 extern void set_class_shadows
PROTO ((tree
));
46 /* The number of nested classes being processed. If we are not in the
47 scope of any class, this is zero. */
49 int current_class_depth
;
51 /* In order to deal with nested classes, we keep a stack of classes.
52 The topmost entry is the innermost class, and is the entry at index
53 CURRENT_CLASS_DEPTH */
55 typedef struct class_stack_node
{
56 /* The name of the class. */
59 /* The _TYPE node for the class. */
62 /* The access specifier pending for new declarations in the scope of
65 }* class_stack_node_t
;
67 /* The stack itself. This is an dynamically resized array. The
68 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
69 static int current_class_stack_size
;
70 static class_stack_node_t current_class_stack
;
72 /* When we're processing a member function, current_class_ptr is the
73 PARM_DECL for the `this' pointer. The current_class_ref is an
74 expression for `*this'. */
75 tree current_class_ptr
, current_class_ref
;
77 /* The following two can be derived from the previous one */
78 tree current_class_name
; /* IDENTIFIER_NODE: name of current class */
79 tree current_class_type
; /* _TYPE: the type of the current class */
80 tree current_access_specifier
;
81 tree previous_class_type
; /* _TYPE: the previous type that was a class */
82 tree previous_class_values
; /* TREE_LIST: copy of the class_shadowed list
83 when leaving an outermost class scope. */
85 /* The obstack on which the cached class declarations are kept. */
86 static struct obstack class_cache_obstack
;
87 /* The first object allocated on that obstack. We can use
88 obstack_free with tis value to free the entire obstack. */
89 static char *class_cache_firstobj
;
93 static tree get_vfield_name
PROTO((tree
));
94 static void finish_struct_anon
PROTO((tree
));
95 static tree build_vbase_pointer
PROTO((tree
, tree
));
96 static tree build_vtable_entry
PROTO((tree
, tree
));
97 static tree get_vtable_name
PROTO((tree
));
98 static tree get_derived_offset
PROTO((tree
, tree
));
99 static tree get_basefndecls
PROTO((tree
, tree
));
100 static void set_rtti_entry
PROTO((tree
, tree
, tree
));
101 static tree build_vtable
PROTO((tree
, tree
));
102 static void prepare_fresh_vtable
PROTO((tree
, tree
));
103 static void fixup_vtable_deltas1
PROTO((tree
, tree
));
104 static void fixup_vtable_deltas
PROTO((tree
, int, tree
));
105 static void finish_vtbls
PROTO((tree
, int, tree
));
106 static void modify_vtable_entry
PROTO((tree
, tree
, tree
));
107 static tree get_vtable_entry_n
PROTO((tree
, unsigned HOST_WIDE_INT
));
108 static void add_virtual_function
PROTO((tree
*, tree
*, int *, tree
, tree
));
109 static tree delete_duplicate_fields_1
PROTO((tree
, tree
));
110 static void delete_duplicate_fields
PROTO((tree
));
111 static void finish_struct_bits
PROTO((tree
, int));
112 static int alter_access
PROTO((tree
, tree
, tree
, tree
));
113 static void handle_using_decl
PROTO((tree
, tree
, tree
, tree
));
114 static int overrides
PROTO((tree
, tree
));
115 static int strictly_overrides
PROTO((tree
, tree
));
116 static void merge_overrides
PROTO((tree
, tree
, int, tree
));
117 static void override_one_vtable
PROTO((tree
, tree
, tree
));
118 static void mark_overriders
PROTO((tree
, tree
));
119 static void check_for_override
PROTO((tree
, tree
));
120 static tree maybe_fixup_vptrs
PROTO((tree
, tree
, tree
));
121 static tree get_class_offset_1
PROTO((tree
, tree
, tree
, tree
, tree
));
122 static tree get_class_offset
PROTO((tree
, tree
, tree
, tree
));
123 static void modify_one_vtable
PROTO((tree
, tree
, tree
, tree
));
124 static void modify_all_vtables
PROTO((tree
, tree
, tree
));
125 static void modify_all_direct_vtables
PROTO((tree
, int, tree
, tree
,
127 static void modify_all_indirect_vtables
PROTO((tree
, int, int, tree
,
129 static void build_class_init_list
PROTO((tree
));
130 static int finish_base_struct
PROTO((tree
, struct base_info
*));
131 static void finish_struct_methods
PROTO((tree
));
132 static void maybe_warn_about_overly_private_class
PROTO ((tree
));
133 static void check_member_decl_is_same_in_complete_scope
PROTO((tree
, tree
));
134 static tree make_method_vec
PROTO((int));
135 static void free_method_vec
PROTO((tree
));
136 static tree add_implicitly_declared_members
PROTO((tree
, int, int, int));
138 /* Way of stacking language names. */
139 tree
*current_lang_base
, *current_lang_stack
;
140 int current_lang_stacksize
;
142 /* Names of languages we recognize. */
143 tree lang_name_c
, lang_name_cplusplus
, lang_name_java
;
144 tree current_lang_name
;
146 /* When layout out an aggregate type, the size of the
147 basetypes (virtual and non-virtual) is passed to layout_record
149 static tree base_layout_decl
;
151 /* Constants used for access control. */
152 tree access_default_node
; /* 0 */
153 tree access_public_node
; /* 1 */
154 tree access_protected_node
; /* 2 */
155 tree access_private_node
; /* 3 */
156 tree access_default_virtual_node
; /* 4 */
157 tree access_public_virtual_node
; /* 5 */
158 tree access_protected_virtual_node
; /* 6 */
159 tree access_private_virtual_node
; /* 7 */
161 /* Variables shared between class.c and call.c. */
163 #ifdef GATHER_STATISTICS
165 int n_vtable_entries
= 0;
166 int n_vtable_searches
= 0;
167 int n_vtable_elems
= 0;
168 int n_convert_harshness
= 0;
169 int n_compute_conversion_costs
= 0;
170 int n_build_method_call
= 0;
171 int n_inner_fields_searched
= 0;
174 /* Virtual baseclass things. */
177 build_vbase_pointer (exp
, type
)
181 FORMAT_VBASE_NAME (name
, type
);
183 return build_component_ref (exp
, get_identifier (name
), NULL_TREE
, 0);
187 /* Is the type of the EXPR, the complete type of the object?
188 If we are going to be wrong, we must be conservative, and return 0. */
191 complete_type_p (expr
)
194 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (expr
));
197 switch (TREE_CODE (expr
))
204 expr
= TREE_OPERAND (expr
, 0);
208 if (! TREE_HAS_CONSTRUCTOR (expr
))
210 /* fall through... */
213 if (TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
214 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr
)))
215 && TYPE_MAIN_VARIANT (TREE_TYPE (expr
)) == type
)
217 /* fall through... */
220 if (IS_AGGR_TYPE (TREE_TYPE (expr
))
221 && TYPE_MAIN_VARIANT (TREE_TYPE (expr
)) == type
)
223 /* fall through... */
234 /* Build multi-level access to EXPR using hierarchy path PATH.
235 CODE is PLUS_EXPR if we are going with the grain,
236 and MINUS_EXPR if we are not (in which case, we cannot traverse
237 virtual baseclass links).
239 TYPE is the type we want this path to have on exit.
241 NONNULL is non-zero if we know (for any reason) that EXPR is
242 not, in fact, zero. */
245 build_vbase_path (code
, type
, expr
, path
, nonnull
)
247 tree type
, expr
, path
;
250 register int changed
= 0;
251 tree last
= NULL_TREE
, last_virtual
= NULL_TREE
;
253 tree null_expr
= 0, nonnull_expr
;
255 tree offset
= integer_zero_node
;
257 if (BINFO_INHERITANCE_CHAIN (path
) == NULL_TREE
)
258 return build1 (NOP_EXPR
, type
, expr
);
260 /* If -fthis-is-variable, we might have set nonnull incorrectly. We
261 don't care enough to get this right, so just clear it. */
262 if (flag_this_is_variable
> 0)
265 /* We could do better if we had additional logic to convert back to the
266 unconverted type (the static type of the complete object), and then
267 convert back to the type we want. Until that is done, we only optimize
268 if the complete type is the same type as expr has. */
269 fixed_type_p
= resolves_to_fixed_type_p (expr
, &nonnull
);
271 if (!fixed_type_p
&& TREE_SIDE_EFFECTS (expr
))
272 expr
= save_expr (expr
);
275 if (BINFO_INHERITANCE_CHAIN (path
))
276 path
= reverse_path (path
);
278 basetype
= BINFO_TYPE (path
);
282 if (TREE_VIA_VIRTUAL (path
))
284 last_virtual
= BINFO_TYPE (path
);
285 if (code
== PLUS_EXPR
)
287 changed
= ! fixed_type_p
;
293 /* We already check for ambiguous things in the caller, just
297 tree binfo
= get_binfo (last
, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr
))), 0);
298 nonnull_expr
= convert_pointer_to_real (binfo
, nonnull_expr
);
300 ind
= build_indirect_ref (nonnull_expr
, NULL_PTR
);
301 nonnull_expr
= build_vbase_pointer (ind
, last_virtual
);
303 && TREE_CODE (type
) == POINTER_TYPE
304 && null_expr
== NULL_TREE
)
306 null_expr
= build1 (NOP_EXPR
, build_pointer_type (last_virtual
), integer_zero_node
);
307 expr
= build (COND_EXPR
, build_pointer_type (last_virtual
),
308 build (EQ_EXPR
, boolean_type_node
, expr
,
310 null_expr
, nonnull_expr
);
313 /* else we'll figure out the offset below. */
315 /* Happens in the case of parse errors. */
316 if (nonnull_expr
== error_mark_node
)
317 return error_mark_node
;
321 cp_error ("cannot cast up from virtual baseclass `%T'",
323 return error_mark_node
;
327 path
= BINFO_INHERITANCE_CHAIN (path
);
329 /* LAST is now the last basetype assoc on the path. */
331 /* A pointer to a virtual base member of a non-null object
332 is non-null. Therefore, we only need to test for zeroness once.
333 Make EXPR the canonical expression to deal with here. */
336 TREE_OPERAND (expr
, 2) = nonnull_expr
;
337 TREE_TYPE (expr
) = TREE_TYPE (TREE_OPERAND (expr
, 1))
338 = TREE_TYPE (nonnull_expr
);
343 /* If we go through any virtual base pointers, make sure that
344 casts to BASETYPE from the last virtual base class use
345 the right value for BASETYPE. */
348 tree intype
= TREE_TYPE (TREE_TYPE (expr
));
349 if (TYPE_MAIN_VARIANT (intype
) != BINFO_TYPE (last
))
351 tree binfo
= get_binfo (last
, TYPE_MAIN_VARIANT (intype
), 0);
352 offset
= BINFO_OFFSET (binfo
);
359 offset
= BINFO_OFFSET (binfo_member (last_virtual
,
360 CLASSTYPE_VBASECLASSES (basetype
)));
361 offset
= size_binop (PLUS_EXPR
, offset
, BINFO_OFFSET (last
));
364 offset
= BINFO_OFFSET (last
);
367 if (TREE_INT_CST_LOW (offset
))
369 /* Bash types to make the backend happy. */
370 offset
= cp_convert (type
, offset
);
372 /* This shouldn't be necessary. (mrs) */
373 expr
= build1 (NOP_EXPR
, type
, expr
);
376 /* If expr might be 0, we need to preserve that zeroness. */
380 TREE_TYPE (null_expr
) = type
;
382 null_expr
= build1 (NOP_EXPR
, type
, integer_zero_node
);
383 if (TREE_SIDE_EFFECTS (expr
))
384 expr
= save_expr (expr
);
386 return build (COND_EXPR
, type
,
387 build (EQ_EXPR
, boolean_type_node
, expr
, integer_zero_node
),
389 build (code
, type
, expr
, offset
));
391 else return build (code
, type
, expr
, offset
);
394 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
395 be used multiple times in initialization of multiple inheritance. */
398 TREE_TYPE (expr
) = type
;
402 return build1 (NOP_EXPR
, type
, expr
);
405 /* Virtual function things. */
407 /* Build an entry in the virtual function table.
408 DELTA is the offset for the `this' pointer.
409 PFN is an ADDR_EXPR containing a pointer to the virtual function.
410 Note that the index (DELTA2) in the virtual function table
414 build_vtable_entry (delta
, pfn
)
417 if (flag_vtable_thunks
)
419 HOST_WIDE_INT idelta
= TREE_INT_CST_LOW (delta
);
420 if (idelta
&& ! DECL_ABSTRACT_VIRTUAL_P (TREE_OPERAND (pfn
, 0)))
422 pfn
= build1 (ADDR_EXPR
, vtable_entry_type
,
423 make_thunk (pfn
, idelta
));
424 TREE_READONLY (pfn
) = 1;
425 TREE_CONSTANT (pfn
) = 1;
427 #ifdef GATHER_STATISTICS
428 n_vtable_entries
+= 1;
434 extern int flag_huge_objects
;
435 tree elems
= expr_tree_cons (NULL_TREE
, delta
,
436 expr_tree_cons (NULL_TREE
, integer_zero_node
,
437 build_expr_list (NULL_TREE
, pfn
)));
438 tree entry
= build (CONSTRUCTOR
, vtable_entry_type
, NULL_TREE
, elems
);
440 /* DELTA used to be constructed by `size_int' and/or size_binop,
441 which caused overflow problems when it was negative. That should
444 if (! int_fits_type_p (delta
, delta_type_node
))
446 if (flag_huge_objects
)
447 sorry ("object size exceeds built-in limit for virtual function table implementation");
449 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
452 TREE_CONSTANT (entry
) = 1;
453 TREE_STATIC (entry
) = 1;
454 TREE_READONLY (entry
) = 1;
456 #ifdef GATHER_STATISTICS
457 n_vtable_entries
+= 1;
464 /* We want to give the assembler the vtable identifier as well as
465 the offset to the function pointer. So we generate
467 __asm__ __volatile__ (".vtable_entry %c0, %c1"
468 : : "s"(&class_vtable),
469 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
472 build_vtable_entry_ref (basetype
, vtbl
, idx
)
473 tree basetype
, vtbl
, idx
;
475 static char asm_stmt
[] = ".vtable_entry %c0, %c1";
478 s
= build_unary_op (ADDR_EXPR
, TYPE_BINFO_VTABLE (basetype
), 0);
479 s
= build_tree_list (build_string (1, "s"), s
);
481 i
= build_array_ref (vtbl
, idx
);
482 if (!flag_vtable_thunks
)
483 i
= build_component_ref (i
, pfn_identifier
, vtable_entry_type
, 0);
484 i
= build_c_cast (ptrdiff_type_node
, build_unary_op (ADDR_EXPR
, i
, 0));
485 i2
= build_array_ref (vtbl
, build_int_2(0,0));
486 i2
= build_c_cast (ptrdiff_type_node
, build_unary_op (ADDR_EXPR
, i2
, 0));
487 i
= build_binary_op (MINUS_EXPR
, i
, i2
, 0);
488 i
= build_tree_list (build_string (1, "i"), i
);
490 expand_asm_operands (build_string (sizeof(asm_stmt
)-1, asm_stmt
),
491 NULL_TREE
, chainon (s
, i
), NULL_TREE
, 1, NULL
, 0);
494 /* Given an object INSTANCE, return an expression which yields the
495 virtual function vtable element corresponding to INDEX. There are
496 many special cases for INSTANCE which we take care of here, mainly
497 to avoid creating extra tree nodes when we don't have to. */
500 build_vtbl_ref (instance
, idx
)
504 tree basetype
= TREE_TYPE (instance
);
506 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
507 basetype
= TREE_TYPE (basetype
);
509 if (instance
== current_class_ref
)
510 vtbl
= build_vfield_ref (instance
, basetype
);
515 /* Try to figure out what a reference refers to, and
516 access its virtual function table directly. */
517 tree ref
= NULL_TREE
;
519 if (TREE_CODE (instance
) == INDIRECT_REF
520 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance
, 0))) == REFERENCE_TYPE
)
521 ref
= TREE_OPERAND (instance
, 0);
522 else if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
525 if (ref
&& TREE_CODE (ref
) == VAR_DECL
526 && DECL_INITIAL (ref
))
528 tree init
= DECL_INITIAL (ref
);
530 while (TREE_CODE (init
) == NOP_EXPR
531 || TREE_CODE (init
) == NON_LVALUE_EXPR
)
532 init
= TREE_OPERAND (init
, 0);
533 if (TREE_CODE (init
) == ADDR_EXPR
)
535 init
= TREE_OPERAND (init
, 0);
536 if (IS_AGGR_TYPE (TREE_TYPE (init
))
537 && (TREE_CODE (init
) == PARM_DECL
538 || TREE_CODE (init
) == VAR_DECL
))
544 if (IS_AGGR_TYPE (TREE_TYPE (instance
))
545 && (TREE_CODE (instance
) == RESULT_DECL
546 || TREE_CODE (instance
) == PARM_DECL
547 || TREE_CODE (instance
) == VAR_DECL
))
548 vtbl
= TYPE_BINFO_VTABLE (basetype
);
550 vtbl
= build_vfield_ref (instance
, basetype
);
553 assemble_external (vtbl
);
556 build_vtable_entry_ref (basetype
, vtbl
, idx
);
558 aref
= build_array_ref (vtbl
, idx
);
563 /* Given an object INSTANCE, return an expression which yields the
564 virtual function corresponding to INDEX. There are many special
565 cases for INSTANCE which we take care of here, mainly to avoid
566 creating extra tree nodes when we don't have to. */
569 build_vfn_ref (ptr_to_instptr
, instance
, idx
)
570 tree
*ptr_to_instptr
, instance
;
573 tree aref
= build_vtbl_ref (instance
, idx
);
575 /* When using thunks, there is no extra delta, and we get the pfn
577 if (flag_vtable_thunks
)
582 /* Save the intermediate result in a SAVE_EXPR so we don't have to
583 compute each component of the virtual function pointer twice. */
584 if (TREE_CODE (aref
) == INDIRECT_REF
)
585 TREE_OPERAND (aref
, 0) = save_expr (TREE_OPERAND (aref
, 0));
588 = build (PLUS_EXPR
, TREE_TYPE (*ptr_to_instptr
),
590 cp_convert (ptrdiff_type_node
,
591 build_component_ref (aref
, delta_identifier
, NULL_TREE
, 0)));
594 return build_component_ref (aref
, pfn_identifier
, NULL_TREE
, 0);
597 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
598 for the given TYPE. */
601 get_vtable_name (type
)
604 tree type_id
= build_typename_overload (type
);
605 char *buf
= (char *) alloca (strlen (VTABLE_NAME_FORMAT
)
606 + IDENTIFIER_LENGTH (type_id
) + 2);
607 char *ptr
= IDENTIFIER_POINTER (type_id
);
609 for (i
= 0; ptr
[i
] == OPERATOR_TYPENAME_FORMAT
[i
]; i
++) ;
611 /* We don't take off the numbers; prepare_fresh_vtable uses the
612 DECL_ASSEMBLER_NAME for the type, which includes the number
613 in `3foo'. If we were to pull them off here, we'd end up with
614 something like `_vt.foo.3bar', instead of a uniform definition. */
615 while (ptr
[i
] >= '0' && ptr
[i
] <= '9')
618 sprintf (buf
, VTABLE_NAME_FORMAT
, ptr
+i
);
619 return get_identifier (buf
);
622 /* Return the offset to the main vtable for a given base BINFO. */
625 get_vfield_offset (binfo
)
629 = size_binop (FLOOR_DIV_EXPR
,
630 DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo
))),
631 size_int (BITS_PER_UNIT
));
632 tmp
= convert (sizetype
, tmp
);
633 return size_binop (PLUS_EXPR
, tmp
, BINFO_OFFSET (binfo
));
636 /* Get the offset to the start of the original binfo that we derived
637 this binfo from. If we find TYPE first, return the offset only
638 that far. The shortened search is useful because the this pointer
639 on method calling is expected to point to a DECL_CONTEXT (fndecl)
640 object, and not a baseclass of it. */
643 get_derived_offset (binfo
, type
)
646 tree offset1
= get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo
)));
649 while (BINFO_BASETYPES (binfo
)
650 && (i
=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
))) != -1)
652 tree binfos
= BINFO_BASETYPES (binfo
);
653 if (BINFO_TYPE (binfo
) == type
)
655 binfo
= TREE_VEC_ELT (binfos
, i
);
657 offset2
= get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo
)));
658 return size_binop (MINUS_EXPR
, offset1
, offset2
);
661 /* Update the rtti info for this class. */
664 set_rtti_entry (virtuals
, offset
, type
)
665 tree virtuals
, offset
, type
;
670 vfn
= build1 (ADDR_EXPR
, vfunc_ptr_type_node
, get_tinfo_fn (type
));
672 vfn
= build1 (NOP_EXPR
, vfunc_ptr_type_node
, size_zero_node
);
673 TREE_CONSTANT (vfn
) = 1;
675 if (! flag_vtable_thunks
)
676 TREE_VALUE (virtuals
) = build_vtable_entry (offset
, vfn
);
679 tree voff
= build1 (NOP_EXPR
, vfunc_ptr_type_node
, offset
);
680 TREE_CONSTANT (voff
) = 1;
682 TREE_VALUE (virtuals
) = build_vtable_entry (integer_zero_node
, voff
);
684 /* The second slot is for the tdesc pointer when thunks are used. */
685 TREE_VALUE (TREE_CHAIN (virtuals
))
686 = build_vtable_entry (integer_zero_node
, vfn
);
690 /* Build a virtual function for type TYPE.
691 If BINFO is non-NULL, build the vtable starting with the initial
692 approximation that it is the same as the one which is the head of
693 the association list. */
696 build_vtable (binfo
, type
)
699 tree name
= get_vtable_name (type
);
706 virtuals
= copy_list (BINFO_VIRTUALS (binfo
));
707 decl
= build_decl (VAR_DECL
, name
, TREE_TYPE (BINFO_VTABLE (binfo
)));
709 /* Now do rtti stuff. */
710 offset
= get_derived_offset (TYPE_BINFO (type
), NULL_TREE
);
711 offset
= ssize_binop (MINUS_EXPR
, integer_zero_node
, offset
);
712 set_rtti_entry (virtuals
, offset
, type
);
716 virtuals
= NULL_TREE
;
717 decl
= build_decl (VAR_DECL
, name
, void_type_node
);
720 #ifdef GATHER_STATISTICS
722 n_vtable_elems
+= list_length (virtuals
);
725 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
726 import_export_vtable (decl
, type
, 0);
728 decl
= pushdecl_top_level (decl
);
729 SET_IDENTIFIER_GLOBAL_VALUE (name
, decl
);
730 /* Initialize the association list for this type, based
731 on our first approximation. */
732 TYPE_BINFO_VTABLE (type
) = decl
;
733 TYPE_BINFO_VIRTUALS (type
) = virtuals
;
735 DECL_ARTIFICIAL (decl
) = 1;
736 TREE_STATIC (decl
) = 1;
737 #ifndef WRITABLE_VTABLES
738 /* Make them READONLY by default. (mrs) */
739 TREE_READONLY (decl
) = 1;
741 /* At one time the vtable info was grabbed 2 words at a time. This
742 fails on sparc unless you have 8-byte alignment. (tiemann) */
743 DECL_ALIGN (decl
) = MAX (TYPE_ALIGN (double_type_node
),
746 DECL_VIRTUAL_P (decl
) = 1;
747 DECL_CONTEXT (decl
) = type
;
749 binfo
= TYPE_BINFO (type
);
750 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
754 extern tree signed_size_zero_node
;
756 /* Give TYPE a new virtual function table which is initialized
757 with a skeleton-copy of its original initialization. The only
758 entry that changes is the `delta' entry, so we can really
759 share a lot of structure.
761 FOR_TYPE is the derived type which caused this table to
764 BINFO is the type association which provided TYPE for FOR_TYPE.
766 The order in which vtables are built (by calling this function) for
767 an object must remain the same, otherwise a binary incompatibility
771 prepare_fresh_vtable (binfo
, for_type
)
772 tree binfo
, for_type
;
775 tree orig_decl
= BINFO_VTABLE (binfo
);
788 basetype
= TYPE_MAIN_VARIANT (BINFO_TYPE (binfo
));
790 buf2
= TYPE_ASSEMBLER_NAME_STRING (basetype
);
791 i
= TYPE_ASSEMBLER_NAME_LENGTH (basetype
) + 1;
793 /* We know that the vtable that we are going to create doesn't exist
794 yet in the global namespace, and when we finish, it will be
795 pushed into the global namespace. In complex MI hierarchies, we
796 have to loop while the name we are thinking of adding is globally
797 defined, adding more name components to the vtable name as we
798 loop, until the name is unique. This is because in complex MI
799 cases, we might have the same base more than once. This means
800 that the order in which this function is called for vtables must
801 remain the same, otherwise binary compatibility can be
806 char *buf1
= (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type
)
810 sprintf (buf1
, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type
), joiner
,
812 buf
= (char *) alloca (strlen (VTABLE_NAME_FORMAT
) + strlen (buf1
) + 1);
813 sprintf (buf
, VTABLE_NAME_FORMAT
, buf1
);
814 name
= get_identifier (buf
);
816 /* If this name doesn't clash, then we can use it, otherwise
817 we add more to the name until it is unique. */
819 if (! IDENTIFIER_GLOBAL_VALUE (name
))
822 /* Set values for next loop through, if the name isn't unique. */
824 path
= BINFO_INHERITANCE_CHAIN (path
);
826 /* We better not run out of stuff to make it unique. */
827 my_friendly_assert (path
!= NULL_TREE
, 368);
829 basetype
= TYPE_MAIN_VARIANT (BINFO_TYPE (path
));
831 if (for_type
== basetype
)
833 /* If we run out of basetypes in the path, we have already
834 found created a vtable with that name before, we now
835 resort to tacking on _%d to distinguish them. */
837 i
= TYPE_ASSEMBLER_NAME_LENGTH (basetype
) + 1 + i
+ 1 + 3;
838 buf1
= (char *) alloca (i
);
840 sprintf (buf1
, "%s%c%s%c%d",
841 TYPE_ASSEMBLER_NAME_STRING (basetype
), joiner
,
843 buf
= (char *) alloca (strlen (VTABLE_NAME_FORMAT
)
844 + strlen (buf1
) + 1);
845 sprintf (buf
, VTABLE_NAME_FORMAT
, buf1
);
846 name
= get_identifier (buf
);
848 /* If this name doesn't clash, then we can use it,
849 otherwise we add something different to the name until
851 } while (++j
<= 999 && IDENTIFIER_GLOBAL_VALUE (name
));
853 /* Hey, they really like MI don't they? Increase the 3
854 above to 6, and the 999 to 999999. :-) */
855 my_friendly_assert (j
<= 999, 369);
860 i
= TYPE_ASSEMBLER_NAME_LENGTH (basetype
) + 1 + i
;
861 new_buf2
= (char *) alloca (i
);
862 sprintf (new_buf2
, "%s%c%s",
863 TYPE_ASSEMBLER_NAME_STRING (basetype
), joiner
, buf2
);
867 new_decl
= build_decl (VAR_DECL
, name
, TREE_TYPE (orig_decl
));
868 /* Remember which class this vtable is really for. */
869 DECL_CONTEXT (new_decl
) = for_type
;
871 DECL_ARTIFICIAL (new_decl
) = 1;
872 TREE_STATIC (new_decl
) = 1;
873 BINFO_VTABLE (binfo
) = pushdecl_top_level (new_decl
);
874 DECL_VIRTUAL_P (new_decl
) = 1;
875 #ifndef WRITABLE_VTABLES
876 /* Make them READONLY by default. (mrs) */
877 TREE_READONLY (new_decl
) = 1;
879 DECL_ALIGN (new_decl
) = DECL_ALIGN (orig_decl
);
881 /* Make fresh virtual list, so we can smash it later. */
882 BINFO_VIRTUALS (binfo
) = copy_list (BINFO_VIRTUALS (binfo
));
884 if (TREE_VIA_VIRTUAL (binfo
))
886 tree binfo1
= binfo_member (BINFO_TYPE (binfo
),
887 CLASSTYPE_VBASECLASSES (for_type
));
889 /* XXX - This should never happen, if it does, the caller should
890 ensure that the binfo is from for_type's binfos, not from any
891 base type's. We can remove all this code after a while. */
893 warning ("internal inconsistency: binfo offset error for rtti");
895 offset
= BINFO_OFFSET (binfo1
);
898 offset
= BINFO_OFFSET (binfo
);
900 set_rtti_entry (BINFO_VIRTUALS (binfo
),
901 ssize_binop (MINUS_EXPR
, integer_zero_node
, offset
),
904 #ifdef GATHER_STATISTICS
906 n_vtable_elems
+= list_length (BINFO_VIRTUALS (binfo
));
909 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
910 import_export_vtable (new_decl
, for_type
, 0);
912 if (TREE_VIA_VIRTUAL (binfo
))
913 my_friendly_assert (binfo
== binfo_member (BINFO_TYPE (binfo
),
914 CLASSTYPE_VBASECLASSES (current_class_type
)),
916 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
920 /* Access the virtual function table entry that logically
921 contains BASE_FNDECL. VIRTUALS is the virtual function table's
922 initializer. We can run off the end, when dealing with virtual
923 destructors in MI situations, return NULL_TREE in that case. */
926 get_vtable_entry (virtuals
, base_fndecl
)
927 tree virtuals
, base_fndecl
;
929 unsigned HOST_WIDE_INT n
= (HOST_BITS_PER_WIDE_INT
>= BITS_PER_WORD
930 ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl
))
931 & (((unsigned HOST_WIDE_INT
)1<<(BITS_PER_WORD
-1))-1))
932 : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl
)));
934 #ifdef GATHER_STATISTICS
935 n_vtable_searches
+= n
;
938 while (n
> 0 && virtuals
)
941 virtuals
= TREE_CHAIN (virtuals
);
947 /* Put new entry ENTRY into virtual function table initializer
950 Also update DECL_VINDEX (FNDECL). */
953 modify_vtable_entry (old_entry_in_list
, new_entry
, fndecl
)
954 tree old_entry_in_list
, new_entry
, fndecl
;
956 tree base_fndecl
= TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list
)), 0);
959 cp_warning ("replaced %D with %D", DECL_ASSEMBLER_NAME (base_fndecl
),
960 DECL_ASSEMBLER_NAME (fndecl
));
962 TREE_VALUE (old_entry_in_list
) = new_entry
;
964 /* Now assign virtual dispatch information, if unset. */
965 /* We can dispatch this, through any overridden base function. */
966 if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
968 DECL_VINDEX (fndecl
) = DECL_VINDEX (base_fndecl
);
969 DECL_CONTEXT (fndecl
) = DECL_CONTEXT (base_fndecl
);
973 /* Access the virtual function table entry N. VIRTUALS is the virtual
974 function table's initializer. */
977 get_vtable_entry_n (virtuals
, n
)
979 unsigned HOST_WIDE_INT n
;
984 virtuals
= TREE_CHAIN (virtuals
);
989 /* Add a virtual function to all the appropriate vtables for the class
990 T. DECL_VINDEX(X) should be error_mark_node, if we want to
991 allocate a new slot in our table. If it is error_mark_node, we
992 know that no other function from another vtable is overridden by X.
993 HAS_VIRTUAL keeps track of how many virtuals there are in our main
994 vtable for the type, and we build upon the PENDING_VIRTUALS list
998 add_virtual_function (pv
, phv
, has_virtual
, fndecl
, t
)
1002 tree t
; /* Structure type. */
1004 tree pending_virtuals
= *pv
;
1005 tree pending_hard_virtuals
= *phv
;
1007 /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
1008 convert to void *. Make such a conversion here. */
1009 tree vfn
= build1 (ADDR_EXPR
, vfunc_ptr_type_node
, fndecl
);
1010 TREE_CONSTANT (vfn
) = 1;
1013 if (current_class_type
== 0)
1014 cp_warning ("internal problem, current_class_type is zero when adding `%D', please report",
1016 if (current_class_type
&& t
!= current_class_type
)
1017 cp_warning ("internal problem, current_class_type differs when adding `%D', please report",
1021 /* If the virtual function is a redefinition of a prior one,
1022 figure out in which base class the new definition goes,
1023 and if necessary, make a fresh virtual function table
1024 to hold that entry. */
1025 if (DECL_VINDEX (fndecl
) == error_mark_node
)
1029 /* We remember that this was the base sub-object for rtti. */
1030 CLASSTYPE_RTTI (t
) = t
;
1032 /* If we are using thunks, use two slots at the front, one
1033 for the offset pointer, one for the tdesc pointer. */
1034 if (*has_virtual
== 0 && flag_vtable_thunks
)
1039 /* Build a new INT_CST for this DECL_VINDEX. */
1041 static tree index_table
[256];
1043 /* We skip a slot for the offset/tdesc entry. */
1044 int i
= ++(*has_virtual
);
1046 if (i
>= 256 || index_table
[i
] == 0)
1048 idx
= build_int_2 (i
, 0);
1050 index_table
[i
] = idx
;
1053 idx
= index_table
[i
];
1055 /* Now assign virtual dispatch information. */
1056 DECL_VINDEX (fndecl
) = idx
;
1057 DECL_CONTEXT (fndecl
) = t
;
1059 entry
= build_vtable_entry (integer_zero_node
, vfn
);
1060 pending_virtuals
= tree_cons (DECL_VINDEX (fndecl
), entry
, pending_virtuals
);
1062 /* Might already be INTEGER_CST if declared twice in class. We will
1063 give error later or we've already given it. */
1064 else if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
1066 /* Need an entry in some other virtual function table.
1067 Deal with this after we have laid out our virtual base classes. */
1068 pending_hard_virtuals
= temp_tree_cons (fndecl
, vfn
, pending_hard_virtuals
);
1070 *pv
= pending_virtuals
;
1071 *phv
= pending_hard_virtuals
;
1074 /* Obstack on which to build the vector of class methods. */
1075 struct obstack class_obstack
;
1076 extern struct obstack
*current_obstack
;
1078 /* These are method vectors that were too small for the number of
1079 methods in some class, and so were abandoned. */
1080 static tree free_method_vecs
;
1082 /* Returns a method vector with enough room for N methods. N should
1083 be a power of two. */
1092 for (t
= &free_method_vecs
; *t
; t
= &(TREE_CHAIN (*t
)))
1093 /* Note that we don't use >= n here because we don't want to
1094 allocate a very large vector where it isn't needed. */
1095 if (TREE_VEC_LENGTH (*t
) == n
)
1098 *t
= TREE_CHAIN (new_vec
);
1099 TREE_CHAIN (new_vec
) = NULL_TREE
;
1100 bzero ((PTR
) &TREE_VEC_ELT (new_vec
, 0), n
* sizeof (tree
));
1104 new_vec
= make_tree_vec (n
);
1108 /* Free the method vector VEC. */
1111 free_method_vec (vec
)
1114 TREE_CHAIN (vec
) = free_method_vecs
;
1115 free_method_vecs
= vec
;
1118 /* Add method METHOD to class TYPE.
1120 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1121 the class type where the method should be added. */
1124 add_method (type
, fields
, method
)
1125 tree type
, *fields
, method
;
1127 push_obstacks (&permanent_obstack
, &permanent_obstack
);
1129 /* Setting the DECL_CONTEXT and DECL_CLASS_CONTEXT here is probably
1131 DECL_CONTEXT (method
) = type
;
1132 DECL_CLASS_CONTEXT (method
) = type
;
1134 if (fields
&& *fields
)
1135 *fields
= build_overload (method
, *fields
);
1141 if (!CLASSTYPE_METHOD_VEC (type
))
1142 /* Make a new method vector. We start with 8 entries. We must
1143 allocate at least two (for constructors and destructors), and
1144 we're going to end up with an assignment operator at some
1147 We could use a TREE_LIST for now, and convert it to a
1148 TREE_VEC in finish_struct, but we would probably waste more
1149 memory making the links in the list than we would by
1150 over-allocating the size of the vector here. Furthermore,
1151 we would complicate all the code that expects this to be a
1152 vector. We keep a free list of vectors that we outgrew so
1153 that we don't really waste any memory. */
1154 CLASSTYPE_METHOD_VEC (type
) = make_method_vec (8);
1156 method_vec
= CLASSTYPE_METHOD_VEC (type
);
1157 len
= TREE_VEC_LENGTH (method_vec
);
1159 if (DECL_NAME (method
) == constructor_name (type
))
1161 /* A new constructor or destructor. Constructors go in
1162 slot 0; destructors go in slot 1. */
1164 = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method
)) ? 1 : 0;
1166 TREE_VEC_ELT (method_vec
, slot
)
1167 = build_overload (method
, TREE_VEC_ELT (method_vec
, slot
));
1173 /* See if we already have an entry with this name. */
1174 for (i
= 2; i
< len
; ++i
)
1175 if (!TREE_VEC_ELT (method_vec
, i
)
1176 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec
, i
)))
1177 == DECL_NAME (method
)))
1182 /* We need a bigger method vector. */
1183 tree new_vec
= make_method_vec (2 * len
);
1184 bcopy ((PTR
) &TREE_VEC_ELT (method_vec
, 0),
1185 (PTR
) &TREE_VEC_ELT (new_vec
, 0),
1186 len
* sizeof (tree
));
1187 free_method_vec (method_vec
);
1189 method_vec
= CLASSTYPE_METHOD_VEC (type
) = new_vec
;
1191 else if (template_class_depth (type
))
1192 /* TYPE is a template class. Don't issue any errors now;
1193 wait until instantiation time to complain. */
1199 /* Check to see if we've already got this method. */
1200 for (fns
= TREE_VEC_ELT (method_vec
, i
);
1202 fns
= OVL_NEXT (fns
))
1204 tree fn
= OVL_CURRENT (fns
);
1206 if (TREE_CODE (fn
) != TREE_CODE (method
))
1209 if (TREE_CODE (method
) != TEMPLATE_DECL
)
1211 /* Since this is an ordinary function in a
1212 non-template class, it's mangled name can be
1213 used as a unique identifier. This technique
1214 is only an optimization; we would get the
1215 same results if we just used decls_match
1217 if (DECL_ASSEMBLER_NAME (fn
)
1218 != DECL_ASSEMBLER_NAME (method
))
1221 else if (!decls_match (fn
, method
))
1224 /* There has already been a declaration of this
1225 method or member template. */
1226 cp_error_at ("`%D' has already been declared in `%T'",
1229 /* We don't call duplicate_decls here to merege the
1230 declarations because that will confuse things if
1231 the methods have inline definitions In
1232 particular, we will crash while processing the
1238 if (DECL_CONV_FN_P (method
))
1240 /* Type conversion operators have to come before
1241 ordinary methods; add_conversions depends on this to
1242 speed up looking for conversion operators. So, if
1243 necessary, we slide some of the vector elements up.
1244 In theory, this makes this algorithm O(N^2) but we
1245 don't expect many conversion operators. */
1246 for (i
= 2; i
< len
; ++i
)
1248 tree fn
= TREE_VEC_ELT (method_vec
, i
);
1251 /* There are no more entries in the vector, so we
1252 can insert the new conversion operator here. */
1255 if (! DECL_CONV_FN_P (OVL_CURRENT (fn
)))
1256 /* We can insert the new function right at the Ith
1261 if (!TREE_VEC_ELT (method_vec
, i
))
1262 /* There is nothing in the Ith slot, so we can avoid
1267 /* We know the last slot in the vector is empty
1268 because we know that at this point there's room for
1270 bcopy ((PTR
) &TREE_VEC_ELT (method_vec
, i
),
1271 (PTR
) &TREE_VEC_ELT (method_vec
, i
+ 1),
1272 (len
- i
- 1) * sizeof (tree
));
1273 TREE_VEC_ELT (method_vec
, i
) = NULL_TREE
;
1277 /* Actually insert the new method. */
1278 TREE_VEC_ELT (method_vec
, i
)
1279 = build_overload (method
, TREE_VEC_ELT (method_vec
, i
));
1282 if (TYPE_BINFO_BASETYPES (type
) && CLASSTYPE_BASELINK_VEC (type
))
1284 /* ??? May be better to know whether these can be extended? */
1285 tree baselink_vec
= CLASSTYPE_BASELINK_VEC (type
);
1287 TREE_VEC_LENGTH (baselink_vec
) += 1;
1288 CLASSTYPE_BASELINK_VEC (type
) = copy_node (baselink_vec
);
1289 TREE_VEC_LENGTH (baselink_vec
) -= 1;
1291 TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type
), len
) = 0;
1297 /* Subroutines of finish_struct. */
1299 /* Look through the list of fields for this struct, deleting
1300 duplicates as we go. This must be recursive to handle
1303 FIELD is the field which may not appear anywhere in FIELDS.
1304 FIELD_PTR, if non-null, is the starting point at which
1305 chained deletions may take place.
1306 The value returned is the first acceptable entry found
1309 Note that anonymous fields which are not of UNION_TYPE are
1310 not duplicates, they are just anonymous fields. This happens
1311 when we have unnamed bitfields, for example. */
1314 delete_duplicate_fields_1 (field
, fields
)
1319 if (DECL_NAME (field
) == 0)
1321 if (TREE_CODE (TREE_TYPE (field
)) != UNION_TYPE
)
1324 for (x
= TYPE_FIELDS (TREE_TYPE (field
)); x
; x
= TREE_CHAIN (x
))
1325 fields
= delete_duplicate_fields_1 (x
, fields
);
1330 for (x
= fields
; x
; prev
= x
, x
= TREE_CHAIN (x
))
1332 if (DECL_NAME (x
) == 0)
1334 if (TREE_CODE (TREE_TYPE (x
)) != UNION_TYPE
)
1336 TYPE_FIELDS (TREE_TYPE (x
))
1337 = delete_duplicate_fields_1 (field
, TYPE_FIELDS (TREE_TYPE (x
)));
1338 if (TYPE_FIELDS (TREE_TYPE (x
)) == 0)
1341 fields
= TREE_CHAIN (fields
);
1343 TREE_CHAIN (prev
) = TREE_CHAIN (x
);
1348 if (DECL_NAME (field
) == DECL_NAME (x
))
1350 if (TREE_CODE (field
) == CONST_DECL
1351 && TREE_CODE (x
) == CONST_DECL
)
1352 cp_error_at ("duplicate enum value `%D'", x
);
1353 else if (TREE_CODE (field
) == CONST_DECL
1354 || TREE_CODE (x
) == CONST_DECL
)
1355 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1357 else if (DECL_DECLARES_TYPE_P (field
)
1358 && DECL_DECLARES_TYPE_P (x
))
1360 if (same_type_p (TREE_TYPE (field
), TREE_TYPE (x
)))
1362 cp_error_at ("duplicate nested type `%D'", x
);
1364 else if (DECL_DECLARES_TYPE_P (field
)
1365 || DECL_DECLARES_TYPE_P (x
))
1367 /* Hide tag decls. */
1368 if ((TREE_CODE (field
) == TYPE_DECL
1369 && DECL_ARTIFICIAL (field
))
1370 || (TREE_CODE (x
) == TYPE_DECL
1371 && DECL_ARTIFICIAL (x
)))
1373 cp_error_at ("duplicate field `%D' (as type and non-type)",
1377 cp_error_at ("duplicate member `%D'", x
);
1379 fields
= TREE_CHAIN (fields
);
1381 TREE_CHAIN (prev
) = TREE_CHAIN (x
);
1390 delete_duplicate_fields (fields
)
1394 for (x
= fields
; x
&& TREE_CHAIN (x
); x
= TREE_CHAIN (x
))
1395 TREE_CHAIN (x
) = delete_duplicate_fields_1 (x
, TREE_CHAIN (x
));
1398 /* Change the access of FDECL to ACCESS in T. The access to FDECL is
1399 along the path given by BINFO. Return 1 if change was legit,
1400 otherwise return 0. */
1403 alter_access (t
, binfo
, fdecl
, access
)
1409 tree elem
= purpose_member (t
, DECL_ACCESS (fdecl
));
1412 if (TREE_VALUE (elem
) != access
)
1414 if (TREE_CODE (TREE_TYPE (fdecl
)) == FUNCTION_DECL
)
1415 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl
));
1417 error ("conflicting access specifications for field `%s', ignored",
1418 IDENTIFIER_POINTER (DECL_NAME (fdecl
)));
1422 /* They're changing the access to the same thing they changed
1423 it to before. That's OK. */
1429 enforce_access (binfo
, fdecl
);
1431 DECL_ACCESS (fdecl
) = tree_cons (t
, access
, DECL_ACCESS (fdecl
));
1437 /* Process the USING_DECL, which is a member of T. The METHOD_VEC, if
1438 non-NULL, is the methods of T. The FIELDS are the fields of T.
1439 Returns 1 if the USING_DECL was valid, 0 otherwise. */
1442 handle_using_decl (using_decl
, t
, method_vec
, fields
)
1448 tree ctype
= DECL_INITIAL (using_decl
);
1449 tree name
= DECL_NAME (using_decl
);
1451 = TREE_PRIVATE (using_decl
) ? access_private_node
1452 : TREE_PROTECTED (using_decl
) ? access_protected_node
1453 : access_public_node
;
1455 tree flist
= NULL_TREE
;
1460 binfo
= binfo_or_else (ctype
, t
);
1464 if (name
== constructor_name (ctype
)
1465 || name
== constructor_name_full (ctype
))
1466 cp_error_at ("using-declaration for constructor", using_decl
);
1468 fdecl
= lookup_member (binfo
, name
, 0, 0);
1472 cp_error_at ("no members matching `%D' in `%#T'", using_decl
, ctype
);
1476 /* Functions are represented as TREE_LIST, with the purpose
1477 being the type and the value the functions. Other members
1478 come as themselves. */
1479 if (TREE_CODE (fdecl
) == TREE_LIST
)
1480 /* Ignore base type this came from. */
1481 fdecl
= TREE_VALUE (fdecl
);
1483 if (TREE_CODE (fdecl
) == OVERLOAD
)
1485 /* We later iterate over all functions. */
1487 fdecl
= OVL_FUNCTION (flist
);
1490 name
= DECL_NAME (fdecl
);
1491 n_methods
= method_vec
? TREE_VEC_LENGTH (method_vec
) : 0;
1492 for (i
= 2; i
< n_methods
&& TREE_VEC_ELT (method_vec
, i
); i
++)
1493 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec
, i
)))
1496 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl
, t
);
1497 cp_error_at (" because of local method `%#D' with same name",
1498 OVL_CURRENT (TREE_VEC_ELT (method_vec
, i
)));
1502 if (! DECL_LANG_SPECIFIC (fdecl
))
1503 /* We don't currently handle DECL_ACCESS for TYPE_DECLs; just return. */
1506 for (tmp
= fields
; tmp
; tmp
= TREE_CHAIN (tmp
))
1507 if (DECL_NAME (tmp
) == name
)
1509 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl
, t
);
1510 cp_error_at (" because of local field `%#D' with same name", tmp
);
1514 /* Make type T see field decl FDECL with access ACCESS.*/
1519 if (alter_access (t
, binfo
, OVL_FUNCTION (flist
),
1522 flist
= OVL_CHAIN (flist
);
1526 alter_access (t
, binfo
, fdecl
, access
);
1529 /* If FOR_TYPE needs to reinitialize virtual function table pointers
1530 for TYPE's sub-objects, add such reinitializations to BASE_INIT_LIST.
1531 Returns BASE_INIT_LIST appropriately modified. */
1534 maybe_fixup_vptrs (for_type
, binfo
, base_init_list
)
1535 tree for_type
, binfo
, base_init_list
;
1537 /* Now reinitialize any slots that don't fall under our virtual
1538 function table pointer. */
1539 tree vfields
= CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
));
1542 tree basetype
= VF_NORMAL_VALUE (vfields
)
1543 ? TYPE_MAIN_VARIANT (VF_NORMAL_VALUE (vfields
))
1544 : VF_BASETYPE_VALUE (vfields
);
1546 tree base_binfo
= get_binfo (basetype
, for_type
, 0);
1547 /* Punt until this is implemented. */
1548 if (1 /* BINFO_MODIFIED (base_binfo) */)
1550 tree base_offset
= get_vfield_offset (base_binfo
);
1551 if (! tree_int_cst_equal (base_offset
, get_vfield_offset (TYPE_BINFO (for_type
)))
1552 && ! tree_int_cst_equal (base_offset
, get_vfield_offset (binfo
)))
1553 base_init_list
= tree_cons (error_mark_node
, base_binfo
,
1556 vfields
= TREE_CHAIN (vfields
);
1558 return base_init_list
;
1561 /* If TYPE does not have a constructor, then the compiler must
1562 manually deal with all of the initialization this type requires.
1564 If a base initializer exists only to fill in the virtual function
1565 table pointer, then we mark that fact with the TREE_VIRTUAL bit.
1566 This way, we avoid multiple initializations of the same field by
1567 each virtual function table up the class hierarchy.
1569 Virtual base class pointers are not initialized here. They are
1570 initialized only at the "top level" of object creation. If we
1571 initialized them here, we would have to skip a lot of work. */
1574 build_class_init_list (type
)
1577 tree base_init_list
= NULL_TREE
;
1578 tree member_init_list
= NULL_TREE
;
1580 /* Since we build member_init_list and base_init_list using
1581 tree_cons, backwards fields the all through work. */
1583 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (type
));
1584 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
1586 for (x
= TYPE_FIELDS (type
); x
; x
= TREE_CHAIN (x
))
1588 if (TREE_CODE (x
) != FIELD_DECL
)
1591 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (x
))
1592 || DECL_INITIAL (x
) != NULL_TREE
)
1593 member_init_list
= tree_cons (x
, type
, member_init_list
);
1595 member_init_list
= nreverse (member_init_list
);
1597 /* We will end up doing this last. Need special marker
1598 to avoid infinite regress. */
1599 if (TYPE_VIRTUAL_P (type
))
1601 base_init_list
= build_tree_list (error_mark_node
, TYPE_BINFO (type
));
1602 if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (type
) == 0)
1603 TREE_VALUE (base_init_list
) = NULL_TREE
;
1604 TREE_ADDRESSABLE (base_init_list
) = 1;
1607 /* Each base class which needs to have initialization
1608 of some kind gets to make such requests known here. */
1609 for (i
= n_baseclasses
-1; i
>= 0; i
--)
1611 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1614 /* Don't initialize virtual baseclasses this way. */
1615 if (TREE_VIA_VIRTUAL (base_binfo
))
1618 if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (base_binfo
)))
1620 /* ...and the last shall come first... */
1621 base_init_list
= maybe_fixup_vptrs (type
, base_binfo
, base_init_list
);
1622 base_init_list
= tree_cons (NULL_TREE
, base_binfo
, base_init_list
);
1626 if ((blist
= CLASSTYPE_BASE_INIT_LIST (BINFO_TYPE (base_binfo
))) == NULL_TREE
)
1627 /* Nothing to initialize. */
1631 base_init_list
= maybe_fixup_vptrs (type
, base_binfo
, base_init_list
);
1633 /* This is normally true for single inheritance.
1634 The win is we can shrink the chain of initializations
1635 to be done by only converting to the actual type
1636 we are interested in. */
1637 if (TREE_VALUE (blist
)
1638 && TREE_CODE (TREE_VALUE (blist
)) == TREE_VEC
1639 && tree_int_cst_equal (BINFO_OFFSET (base_binfo
),
1640 BINFO_OFFSET (TREE_VALUE (blist
))))
1644 /* Does it do more than just fill in a
1645 virtual function table pointer? */
1646 if (! TREE_ADDRESSABLE (blist
))
1647 base_init_list
= build_tree_list (blist
, base_init_list
);
1648 /* Can we get by just with the virtual function table
1649 pointer that it fills in? */
1650 else if (TREE_ADDRESSABLE (base_init_list
)
1651 && TREE_VALUE (base_init_list
) == 0)
1652 base_init_list
= blist
;
1653 /* Maybe, but it is not obvious as the previous case. */
1654 else if (! CLASSTYPE_NEEDS_VIRTUAL_REINIT (type
))
1656 tree last
= tree_last (base_init_list
);
1657 while (TREE_VALUE (last
)
1658 && TREE_CODE (TREE_VALUE (last
)) == TREE_LIST
)
1659 last
= tree_last (TREE_VALUE (last
));
1660 if (TREE_VALUE (last
) == 0)
1661 base_init_list
= build_tree_list (blist
, base_init_list
);
1665 base_init_list
= blist
;
1669 /* The function expand_aggr_init knows how to do the
1670 initialization of `basetype' without getting
1671 an explicit `blist'. */
1673 base_init_list
= tree_cons (NULL_TREE
, base_binfo
, base_init_list
);
1675 base_init_list
= CLASSTYPE_BINFO_AS_LIST (BINFO_TYPE (base_binfo
));
1681 if (member_init_list
)
1682 CLASSTYPE_BASE_INIT_LIST (type
) =
1683 build_tree_list (base_init_list
, member_init_list
);
1685 CLASSTYPE_BASE_INIT_LIST (type
) = base_init_list
;
1687 else if (member_init_list
)
1688 CLASSTYPE_BASE_INIT_LIST (type
) = member_init_list
;
1694 int max_has_virtual
;
1699 char cant_have_default_ctor
;
1700 char cant_have_const_ctor
;
1701 char no_const_asn_ref
;
1704 /* Record information about type T derived from its base classes.
1705 Store most of that information in T itself, and place the
1706 remaining information in the struct BASE_INFO.
1708 Propagate basetype offsets throughout the lattice. Note that the
1709 lattice topped by T is really a pair: it's a DAG that gives the
1710 structure of the derivation hierarchy, and it's a list of the
1711 virtual baseclasses that appear anywhere in the DAG. When a vbase
1712 type appears in the DAG, it's offset is 0, and it's children start
1713 their offsets from that point. When a vbase type appears in the list,
1714 its offset is the offset it has in the hierarchy, and its children's
1715 offsets include that offset in theirs.
1717 Returns the index of the first base class to have virtual functions,
1718 or -1 if no such base class. */
1721 finish_base_struct (t
, b
)
1723 struct base_info
*b
;
1725 tree binfos
= TYPE_BINFO_BASETYPES (t
);
1726 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
1727 int first_vfn_base_index
= -1;
1728 bzero ((char *) b
, sizeof (struct base_info
));
1730 for (i
= 0; i
< n_baseclasses
; i
++)
1732 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1733 tree basetype
= BINFO_TYPE (base_binfo
);
1735 /* Effective C++ rule 14. We only need to check TYPE_VIRTUAL_P
1736 here because the case of virtual functions but non-virtual
1737 dtor is handled in finish_struct_1. */
1738 if (warn_ecpp
&& ! TYPE_VIRTUAL_P (basetype
)
1739 && TYPE_HAS_DESTRUCTOR (basetype
))
1740 cp_warning ("base class `%#T' has a non-virtual destructor", basetype
);
1742 /* If the type of basetype is incomplete, then
1743 we already complained about that fact
1744 (and we should have fixed it up as well). */
1745 if (TYPE_SIZE (basetype
) == 0)
1748 /* The base type is of incomplete type. It is
1749 probably best to pretend that it does not
1751 if (i
== n_baseclasses
-1)
1752 TREE_VEC_ELT (binfos
, i
) = NULL_TREE
;
1753 TREE_VEC_LENGTH (binfos
) -= 1;
1755 for (j
= i
; j
+1 < n_baseclasses
; j
++)
1756 TREE_VEC_ELT (binfos
, j
) = TREE_VEC_ELT (binfos
, j
+1);
1759 if (! TYPE_HAS_CONST_INIT_REF (basetype
))
1760 b
->cant_have_const_ctor
= 1;
1762 if (TYPE_HAS_CONSTRUCTOR (basetype
)
1763 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
))
1765 b
->cant_have_default_ctor
= 1;
1766 if (! TYPE_HAS_CONSTRUCTOR (t
))
1768 cp_pedwarn ("base `%T' with only non-default constructor",
1770 cp_pedwarn ("in class without a constructor");
1774 if (TYPE_HAS_ASSIGN_REF (basetype
)
1775 && !TYPE_HAS_CONST_ASSIGN_REF (basetype
))
1776 b
->no_const_asn_ref
= 1;
1778 b
->n_ancestors
+= CLASSTYPE_N_SUPERCLASSES (basetype
);
1779 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (basetype
);
1780 TYPE_NEEDS_DESTRUCTOR (t
) |= TYPE_NEEDS_DESTRUCTOR (basetype
);
1781 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype
);
1782 TYPE_HAS_COMPLEX_INIT_REF (t
) |= TYPE_HAS_COMPLEX_INIT_REF (basetype
);
1784 TYPE_OVERLOADS_CALL_EXPR (t
) |= TYPE_OVERLOADS_CALL_EXPR (basetype
);
1785 TYPE_OVERLOADS_ARRAY_REF (t
) |= TYPE_OVERLOADS_ARRAY_REF (basetype
);
1786 TYPE_OVERLOADS_ARROW (t
) |= TYPE_OVERLOADS_ARROW (basetype
);
1788 if (! TREE_VIA_VIRTUAL (base_binfo
))
1789 CLASSTYPE_N_SUPERCLASSES (t
) += 1;
1791 if (TYPE_VIRTUAL_P (basetype
))
1793 /* Ensure that this is set from at least a virtual base
1795 if (b
->rtti
== NULL_TREE
)
1796 b
->rtti
= CLASSTYPE_RTTI (basetype
);
1798 /* Don't borrow virtuals from virtual baseclasses. */
1799 if (TREE_VIA_VIRTUAL (base_binfo
))
1802 if (first_vfn_base_index
< 0)
1805 first_vfn_base_index
= i
;
1807 /* Update these two, now that we know what vtable we are
1808 going to extend. This is so that we can add virtual
1809 functions, and override them properly. */
1810 TYPE_BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (basetype
);
1811 TYPE_BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (basetype
);
1812 b
->has_virtual
= CLASSTYPE_VSIZE (basetype
);
1813 b
->vfield
= CLASSTYPE_VFIELD (basetype
);
1814 b
->vfields
= copy_list (CLASSTYPE_VFIELDS (basetype
));
1815 vfields
= b
->vfields
;
1818 if (VF_BINFO_VALUE (vfields
) == NULL_TREE
1819 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields
)))
1821 tree value
= VF_BASETYPE_VALUE (vfields
);
1822 if (DECL_NAME (CLASSTYPE_VFIELD (value
))
1823 == DECL_NAME (CLASSTYPE_VFIELD (basetype
)))
1824 VF_NORMAL_VALUE (b
->vfields
) = basetype
;
1826 VF_NORMAL_VALUE (b
->vfields
) = VF_NORMAL_VALUE (vfields
);
1828 vfields
= TREE_CHAIN (vfields
);
1830 CLASSTYPE_VFIELD (t
) = b
->vfield
;
1834 /* Only add unique vfields, and flatten them out as we go. */
1835 tree vfields
= CLASSTYPE_VFIELDS (basetype
);
1838 if (VF_BINFO_VALUE (vfields
) == NULL_TREE
1839 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields
)))
1841 tree value
= VF_BASETYPE_VALUE (vfields
);
1842 b
->vfields
= tree_cons (base_binfo
, value
, b
->vfields
);
1843 if (DECL_NAME (CLASSTYPE_VFIELD (value
))
1844 == DECL_NAME (CLASSTYPE_VFIELD (basetype
)))
1845 VF_NORMAL_VALUE (b
->vfields
) = basetype
;
1847 VF_NORMAL_VALUE (b
->vfields
) = VF_NORMAL_VALUE (vfields
);
1849 vfields
= TREE_CHAIN (vfields
);
1852 if (b
->has_virtual
== 0)
1854 first_vfn_base_index
= i
;
1856 /* Update these two, now that we know what vtable we are
1857 going to extend. This is so that we can add virtual
1858 functions, and override them properly. */
1859 TYPE_BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (basetype
);
1860 TYPE_BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (basetype
);
1861 b
->has_virtual
= CLASSTYPE_VSIZE (basetype
);
1862 b
->vfield
= CLASSTYPE_VFIELD (basetype
);
1863 CLASSTYPE_VFIELD (t
) = b
->vfield
;
1864 /* When we install the first one, set the VF_NORMAL_VALUE
1865 to be the current class, as this it is the most derived
1866 class. Hopefully, this is not set to something else
1868 vfields
= b
->vfields
;
1871 if (DECL_NAME (CLASSTYPE_VFIELD (t
))
1872 == DECL_NAME (CLASSTYPE_VFIELD (basetype
)))
1874 VF_NORMAL_VALUE (vfields
) = t
;
1875 /* There should only be one of them! And it should
1876 always be found, if we get into here. (mrs) */
1879 vfields
= TREE_CHAIN (vfields
);
1888 /* Find the base class with the largest number of virtual functions. */
1889 for (vfields
= b
->vfields
; vfields
; vfields
= TREE_CHAIN (vfields
))
1891 if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields
)) > b
->max_has_virtual
)
1892 b
->max_has_virtual
= CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields
));
1893 if (VF_DERIVED_VALUE (vfields
)
1894 && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields
)) > b
->max_has_virtual
)
1895 b
->max_has_virtual
= CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields
));
1900 /* If all virtual functions come only from virtual baseclasses. */
1903 /* Update the rtti base if we have a non-virtual base class version
1905 b
->rtti
= CLASSTYPE_RTTI (BINFO_TYPE (TREE_VEC_ELT (binfos
, first_vfn_base_index
)));
1907 return first_vfn_base_index
;
1910 /* Set memoizing fields and bits of T (and its variants) for later use.
1911 MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables. */
1914 finish_struct_bits (t
, max_has_virtual
)
1916 int max_has_virtual
;
1918 int i
, n_baseclasses
= CLASSTYPE_N_BASECLASSES (t
);
1920 /* Fix up variants (if any). */
1921 tree variants
= TYPE_NEXT_VARIANT (t
);
1924 /* These fields are in the _TYPE part of the node, not in
1925 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1926 TYPE_HAS_CONSTRUCTOR (variants
) = TYPE_HAS_CONSTRUCTOR (t
);
1927 TYPE_HAS_DESTRUCTOR (variants
) = TYPE_HAS_DESTRUCTOR (t
);
1928 TYPE_NEEDS_CONSTRUCTING (variants
) = TYPE_NEEDS_CONSTRUCTING (t
);
1929 TYPE_NEEDS_DESTRUCTOR (variants
) = TYPE_NEEDS_DESTRUCTOR (t
);
1931 TYPE_USES_COMPLEX_INHERITANCE (variants
) = TYPE_USES_COMPLEX_INHERITANCE (t
);
1932 TYPE_VIRTUAL_P (variants
) = TYPE_VIRTUAL_P (t
);
1933 TYPE_USES_VIRTUAL_BASECLASSES (variants
) = TYPE_USES_VIRTUAL_BASECLASSES (t
);
1934 /* Copy whatever these are holding today. */
1935 TYPE_MIN_VALUE (variants
) = TYPE_MIN_VALUE (t
);
1936 TYPE_MAX_VALUE (variants
) = TYPE_MAX_VALUE (t
);
1937 TYPE_FIELDS (variants
) = TYPE_FIELDS (t
);
1938 TYPE_SIZE (variants
) = TYPE_SIZE (t
);
1939 TYPE_SIZE_UNIT (variants
) = TYPE_SIZE_UNIT (t
);
1940 variants
= TYPE_NEXT_VARIANT (variants
);
1943 if (n_baseclasses
&& max_has_virtual
)
1945 /* Done by `finish_struct' for classes without baseclasses. */
1946 int might_have_abstract_virtuals
= CLASSTYPE_ABSTRACT_VIRTUALS (t
) != 0;
1947 tree binfos
= TYPE_BINFO_BASETYPES (t
);
1948 for (i
= n_baseclasses
-1; i
>= 0; i
--)
1950 might_have_abstract_virtuals
1951 |= (CLASSTYPE_ABSTRACT_VIRTUALS (BINFO_TYPE (TREE_VEC_ELT (binfos
, i
))) != 0);
1952 if (might_have_abstract_virtuals
)
1955 if (might_have_abstract_virtuals
)
1957 /* We use error_mark_node from override_one_vtable to signal
1958 an artificial abstract. */
1959 if (CLASSTYPE_ABSTRACT_VIRTUALS (t
) == error_mark_node
)
1960 CLASSTYPE_ABSTRACT_VIRTUALS (t
) = NULL_TREE
;
1961 CLASSTYPE_ABSTRACT_VIRTUALS (t
) = get_abstract_virtuals (t
);
1967 /* Notice whether this class has type conversion functions defined. */
1968 tree binfo
= TYPE_BINFO (t
);
1969 tree binfos
= BINFO_BASETYPES (binfo
);
1972 for (i
= n_baseclasses
-1; i
>= 0; i
--)
1974 basetype
= BINFO_TYPE (TREE_VEC_ELT (binfos
, i
));
1976 TYPE_HAS_CONVERSION (t
) |= TYPE_HAS_CONVERSION (basetype
);
1977 if (CLASSTYPE_MAX_DEPTH (basetype
) >= CLASSTYPE_MAX_DEPTH (t
))
1978 CLASSTYPE_MAX_DEPTH (t
) = CLASSTYPE_MAX_DEPTH (basetype
) + 1;
1982 /* If this type has a copy constructor, force its mode to be BLKmode, and
1983 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1984 be passed by invisible reference and prevent it from being returned in
1987 Also do this if the class has BLKmode but can still be returned in
1988 registers, since function_cannot_inline_p won't let us inline
1989 functions returning such a type. This affects the HP-PA. */
1990 if (! TYPE_HAS_TRIVIAL_INIT_REF (t
)
1991 || (TYPE_MODE (t
) == BLKmode
&& ! aggregate_value_p (t
)
1992 && CLASSTYPE_NON_AGGREGATE (t
)))
1995 DECL_MODE (TYPE_MAIN_DECL (t
)) = BLKmode
;
1996 for (variants
= t
; variants
; variants
= TYPE_NEXT_VARIANT (variants
))
1998 TYPE_MODE (variants
) = BLKmode
;
1999 TREE_ADDRESSABLE (variants
) = 1;
2004 /* Issue warnings about T having private constructors, but no friends,
2007 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2008 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2009 non-private static member functions. */
2012 maybe_warn_about_overly_private_class (t
)
2015 int has_member_fn
= 0;
2016 int has_nonprivate_method
= 0;
2019 if (!warn_ctor_dtor_privacy
2020 /* If the class has friends, those entities might create and
2021 access instances, so we should not warn. */
2022 || (CLASSTYPE_FRIEND_CLASSES (t
)
2023 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
2024 /* We will have warned when the template was declared; there's
2025 no need to warn on every instantiation. */
2026 || CLASSTYPE_TEMPLATE_INSTANTIATION (t
))
2027 /* There's no reason to even consider warning about this
2031 /* We only issue one warning, if more than one applies, because
2032 otherwise, on code like:
2035 // Oops - forgot `public:'
2041 we warn several times about essentially the same problem. */
2043 /* Check to see if all (non-constructor, non-destructor) member
2044 functions are private. (Since there are no friends or
2045 non-private statics, we can't ever call any of the private member
2047 for (fn
= TYPE_METHODS (t
); fn
; fn
= TREE_CHAIN (fn
))
2048 /* We're not interested in compiler-generated methods; they don't
2049 provide any way to call private members. */
2050 if (!DECL_ARTIFICIAL (fn
))
2052 if (!TREE_PRIVATE (fn
))
2054 if (DECL_STATIC_FUNCTION_P (fn
))
2055 /* A non-private static member function is just like a
2056 friend; it can create and invoke private member
2057 functions, and be accessed without a class
2061 has_nonprivate_method
= 1;
2064 else if (!DECL_CONSTRUCTOR_P (fn
) && !DECL_DESTRUCTOR_P (fn
))
2068 if (!has_nonprivate_method
&& has_member_fn
)
2070 /* There are no non-private methods, and there's at least one
2071 private member function that isn't a constructor or
2072 destructor. (If all the private members are
2073 constructors/destructors we want to use the code below that
2074 issues error messages specifically referring to
2075 constructors/destructors.) */
2077 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
2078 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (t
); i
++)
2079 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos
, i
))
2080 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos
, i
)))
2082 has_nonprivate_method
= 1;
2085 if (!has_nonprivate_method
)
2087 cp_warning ("all member functions in class `%T' are private", t
);
2092 /* Even if some of the member functions are non-private, the class
2093 won't be useful for much if all the constructors or destructors
2094 are private: such an object can never be created or destroyed. */
2095 if (TYPE_HAS_DESTRUCTOR (t
))
2097 tree dtor
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 1);
2099 if (TREE_PRIVATE (dtor
))
2101 cp_warning ("`%#T' only defines a private destructor and has no friends",
2107 if (TYPE_HAS_CONSTRUCTOR (t
))
2109 int nonprivate_ctor
= 0;
2111 /* If a non-template class does not define a copy
2112 constructor, one is defined for it, enabling it to avoid
2113 this warning. For a template class, this does not
2114 happen, and so we would normally get a warning on:
2116 template <class T> class C { private: C(); };
2118 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2119 complete non-template or fully instantiated classes have this
2121 if (!TYPE_HAS_INIT_REF (t
))
2122 nonprivate_ctor
= 1;
2124 for (fn
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 0);
2128 tree ctor
= OVL_CURRENT (fn
);
2129 /* Ideally, we wouldn't count copy constructors (or, in
2130 fact, any constructor that takes an argument of the
2131 class type as a parameter) because such things cannot
2132 be used to construct an instance of the class unless
2133 you already have one. But, for now at least, we're
2135 if (! TREE_PRIVATE (ctor
))
2137 nonprivate_ctor
= 1;
2142 if (nonprivate_ctor
== 0)
2144 cp_warning ("`%#T' only defines private constructors and has no friends",
2152 /* Warn about duplicate methods in fn_fields. Also compact method
2153 lists so that lookup can be made faster.
2155 Data Structure: List of method lists. The outer list is a
2156 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2157 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2158 links the entire list of methods for TYPE_METHODS. Friends are
2159 chained in the same way as member functions (? TREE_CHAIN or
2160 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2161 list. That allows them to be quickly deleted, and requires no
2164 If there are any constructors/destructors, they are moved to the
2165 front of the list. This makes pushclass more efficient.
2167 We also link each field which has shares a name with its baseclass
2168 to the head of the list of fields for that base class. This allows
2169 us to reduce search time in places like `build_method_call' to
2170 consider only reasonably likely functions. */
2173 finish_struct_methods (t
)
2177 tree method_vec
= CLASSTYPE_METHOD_VEC (t
);
2178 tree ctor_name
= constructor_name (t
);
2179 int i
, n_baseclasses
= CLASSTYPE_N_BASECLASSES (t
);
2181 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2182 and the next few with type conversion operators (if any). */
2183 for (fn_fields
= TYPE_METHODS (t
); fn_fields
;
2184 fn_fields
= TREE_CHAIN (fn_fields
))
2186 tree fn_name
= DECL_NAME (fn_fields
);
2188 /* Clear out this flag.
2190 @@ Doug may figure out how to break
2191 @@ this with nested classes and friends. */
2192 DECL_IN_AGGR_P (fn_fields
) = 0;
2194 /* Note here that a copy ctor is private, so we don't dare generate
2195 a default copy constructor for a class that has a member
2196 of this type without making sure they have access to it. */
2197 if (fn_name
== ctor_name
)
2199 tree parmtypes
= FUNCTION_ARG_CHAIN (fn_fields
);
2200 tree parmtype
= parmtypes
? TREE_VALUE (parmtypes
) : void_type_node
;
2202 if (TREE_CODE (parmtype
) == REFERENCE_TYPE
2203 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype
)) == t
)
2205 if (TREE_CHAIN (parmtypes
) == NULL_TREE
2206 || TREE_CHAIN (parmtypes
) == void_list_node
2207 || TREE_PURPOSE (TREE_CHAIN (parmtypes
)))
2209 if (TREE_PROTECTED (fn_fields
))
2210 TYPE_HAS_NONPUBLIC_CTOR (t
) = 1;
2211 else if (TREE_PRIVATE (fn_fields
))
2212 TYPE_HAS_NONPUBLIC_CTOR (t
) = 2;
2216 else if (fn_name
== ansi_opname
[(int) MODIFY_EXPR
])
2218 tree parmtype
= TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields
));
2220 if (copy_assignment_arg_p (parmtype
, DECL_VIRTUAL_P (fn_fields
)))
2222 if (TREE_PROTECTED (fn_fields
))
2223 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t
) = 1;
2224 else if (TREE_PRIVATE (fn_fields
))
2225 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t
) = 2;
2230 if (TYPE_HAS_DESTRUCTOR (t
) && !TREE_VEC_ELT (method_vec
, 1))
2231 /* We thought there was a destructor, but there wasn't. Some
2232 parse errors cause this anomalous situation. */
2233 TYPE_HAS_DESTRUCTOR (t
) = 0;
2235 /* Issue warnings about private constructors and such. If there are
2236 no methods, then some public defaults are generated. */
2237 maybe_warn_about_overly_private_class (t
);
2239 /* Now for each member function (except for constructors and
2240 destructors), compute where member functions of the same
2241 name reside in base classes. */
2242 if (n_baseclasses
!= 0
2244 && TREE_VEC_LENGTH (method_vec
) > 2)
2246 int len
= TREE_VEC_LENGTH (method_vec
);
2247 tree baselink_vec
= make_tree_vec (len
);
2249 tree baselink_binfo
= build_tree_list (NULL_TREE
, TYPE_BINFO (t
));
2251 for (i
= 2; i
< len
&& TREE_VEC_ELT (method_vec
, i
); i
++)
2253 tree ovl
= TREE_VEC_ELT (method_vec
, i
);
2255 TREE_VEC_ELT (baselink_vec
, i
)
2256 = get_baselinks (baselink_binfo
, t
,
2257 DECL_NAME (OVL_CURRENT (ovl
)));
2258 if (TREE_VEC_ELT (baselink_vec
, i
) != 0)
2262 CLASSTYPE_BASELINK_VEC (t
) = baselink_vec
;
2264 obstack_free (current_obstack
, baselink_vec
);
2268 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2271 duplicate_tag_error (t
)
2274 cp_error ("redefinition of `%#T'", t
);
2275 cp_error_at ("previous definition here", t
);
2277 /* Pretend we haven't defined this type. */
2279 /* All of the component_decl's were TREE_CHAINed together in the parser.
2280 finish_struct_methods walks these chains and assembles all methods with
2281 the same base name into DECL_CHAINs. Now we don't need the parser chains
2282 anymore, so we unravel them. */
2284 /* This used to be in finish_struct, but it turns out that the
2285 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2287 if (CLASSTYPE_METHOD_VEC (t
))
2289 tree method_vec
= CLASSTYPE_METHOD_VEC (t
);
2290 int i
, len
= TREE_VEC_LENGTH (method_vec
);
2291 for (i
= 0; i
< len
; i
++)
2293 tree unchain
= TREE_VEC_ELT (method_vec
, i
);
2294 while (unchain
!= NULL_TREE
)
2296 TREE_CHAIN (OVL_CURRENT (unchain
)) = NULL_TREE
;
2297 unchain
= OVL_NEXT (unchain
);
2302 if (TYPE_LANG_SPECIFIC (t
))
2304 tree as_list
= CLASSTYPE_AS_LIST (t
);
2305 tree binfo
= TYPE_BINFO (t
);
2306 tree binfo_as_list
= CLASSTYPE_BINFO_AS_LIST (t
);
2307 int interface_only
= CLASSTYPE_INTERFACE_ONLY (t
);
2308 int interface_unknown
= CLASSTYPE_INTERFACE_UNKNOWN (t
);
2310 bzero ((char *) TYPE_LANG_SPECIFIC (t
), sizeof (struct lang_type
));
2311 BINFO_BASETYPES(binfo
) = NULL_TREE
;
2313 CLASSTYPE_AS_LIST (t
) = as_list
;
2314 TYPE_BINFO (t
) = binfo
;
2315 CLASSTYPE_BINFO_AS_LIST (t
) = binfo_as_list
;
2316 CLASSTYPE_INTERFACE_ONLY (t
) = interface_only
;
2317 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t
, interface_unknown
);
2318 TYPE_REDEFINED (t
) = 1;
2320 TYPE_SIZE (t
) = NULL_TREE
;
2321 TYPE_MODE (t
) = VOIDmode
;
2322 TYPE_FIELDS (t
) = NULL_TREE
;
2323 TYPE_METHODS (t
) = NULL_TREE
;
2324 TYPE_VFIELD (t
) = NULL_TREE
;
2325 TYPE_CONTEXT (t
) = NULL_TREE
;
2328 /* finish up all new vtables. */
2331 finish_vtbls (binfo
, do_self
, t
)
2336 tree binfos
= BINFO_BASETYPES (binfo
);
2337 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2339 /* Should we use something besides CLASSTYPE_VFIELDS? */
2340 if (do_self
&& CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
2342 if (BINFO_NEW_VTABLE_MARKED (binfo
))
2346 decl
= BINFO_VTABLE (binfo
);
2347 context
= DECL_CONTEXT (decl
);
2348 DECL_CONTEXT (decl
) = 0;
2349 if (DECL_INITIAL (decl
) != BINFO_VIRTUALS (binfo
))
2350 DECL_INITIAL (decl
) = build_nt (CONSTRUCTOR
, NULL_TREE
,
2351 BINFO_VIRTUALS (binfo
));
2352 cp_finish_decl (decl
, DECL_INITIAL (decl
), NULL_TREE
, 0, 0);
2353 DECL_CONTEXT (decl
) = context
;
2355 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo
);
2358 for (i
= 0; i
< n_baselinks
; i
++)
2360 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2361 int is_not_base_vtable
2362 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
2363 if (TREE_VIA_VIRTUAL (base_binfo
))
2365 base_binfo
= binfo_member (BINFO_TYPE (base_binfo
), CLASSTYPE_VBASECLASSES (t
));
2367 finish_vtbls (base_binfo
, is_not_base_vtable
, t
);
2371 /* True if we should override the given BASE_FNDECL with the given
2375 overrides (fndecl
, base_fndecl
)
2376 tree fndecl
, base_fndecl
;
2378 /* Destructors have special names. */
2379 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl
))
2380 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl
)))
2382 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl
))
2383 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl
)))
2385 if (DECL_NAME (fndecl
) == DECL_NAME (base_fndecl
))
2387 tree types
, base_types
;
2389 retypes
= TREE_TYPE (TREE_TYPE (fndecl
));
2390 base_retypes
= TREE_TYPE (TREE_TYPE (base_fndecl
));
2392 types
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
2393 base_types
= TYPE_ARG_TYPES (TREE_TYPE (base_fndecl
));
2394 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types
)))
2395 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types
))))
2396 && compparms (TREE_CHAIN (base_types
), TREE_CHAIN (types
)))
2403 get_class_offset_1 (parent
, binfo
, context
, t
, fndecl
)
2404 tree parent
, binfo
, context
, t
, fndecl
;
2406 tree binfos
= BINFO_BASETYPES (binfo
);
2407 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2408 tree rval
= NULL_TREE
;
2410 if (binfo
== parent
)
2411 return error_mark_node
;
2413 for (i
= 0; i
< n_baselinks
; i
++)
2415 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2418 if (TREE_VIA_VIRTUAL (base_binfo
))
2419 base_binfo
= binfo_member (BINFO_TYPE (base_binfo
),
2420 CLASSTYPE_VBASECLASSES (t
));
2421 nrval
= get_class_offset_1 (parent
, base_binfo
, context
, t
, fndecl
);
2422 /* See if we have a new value */
2423 if (nrval
&& (nrval
!= error_mark_node
|| rval
==0))
2425 /* Only compare if we have two offsets */
2426 if (rval
&& rval
!= error_mark_node
2427 && ! tree_int_cst_equal (nrval
, rval
))
2429 /* Only give error if the two offsets are different */
2430 error ("every virtual function must have a unique final overrider");
2431 cp_error (" found two (or more) `%T' class subobjects in `%T'", context
, t
);
2432 cp_error (" with virtual `%D' from virtual base class", fndecl
);
2438 if (rval
&& BINFO_TYPE (binfo
) == context
)
2440 my_friendly_assert (rval
== error_mark_node
2441 || tree_int_cst_equal (rval
, BINFO_OFFSET (binfo
)), 999);
2442 rval
= BINFO_OFFSET (binfo
);
2448 /* Get the offset to the CONTEXT subobject that is related to the
2452 get_class_offset (context
, t
, binfo
, fndecl
)
2453 tree context
, t
, binfo
, fndecl
;
2455 tree first_binfo
= binfo
;
2460 return integer_zero_node
;
2462 if (BINFO_TYPE (binfo
) == context
)
2463 return BINFO_OFFSET (binfo
);
2465 /* Check less derived binfos first. */
2466 while (BINFO_BASETYPES (binfo
)
2467 && (i
=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
))) != -1)
2469 tree binfos
= BINFO_BASETYPES (binfo
);
2470 binfo
= TREE_VEC_ELT (binfos
, i
);
2471 if (BINFO_TYPE (binfo
) == context
)
2472 return BINFO_OFFSET (binfo
);
2475 /* Ok, not found in the less derived binfos, now check the more
2477 offset
= get_class_offset_1 (first_binfo
, TYPE_BINFO (t
), context
, t
, fndecl
);
2478 if (offset
==0 || TREE_CODE (offset
) != INTEGER_CST
)
2479 my_friendly_abort (999); /* we have to find it. */
2483 /* Skip RTTI information at the front of the virtual list. */
2485 unsigned HOST_WIDE_INT
2486 skip_rtti_stuff (virtuals
)
2494 /* We always reserve a slot for the offset/tdesc entry. */
2496 *virtuals
= TREE_CHAIN (*virtuals
);
2498 if (flag_vtable_thunks
&& *virtuals
)
2500 /* The second slot is reserved for the tdesc pointer when thunks
2503 *virtuals
= TREE_CHAIN (*virtuals
);
2509 modify_one_vtable (binfo
, t
, fndecl
, pfn
)
2510 tree binfo
, t
, fndecl
, pfn
;
2512 tree virtuals
= BINFO_VIRTUALS (binfo
);
2513 unsigned HOST_WIDE_INT n
;
2515 /* update rtti entry */
2518 if (binfo
== TYPE_BINFO (t
))
2520 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2521 build_vtable (TYPE_BINFO (DECL_CONTEXT (CLASSTYPE_VFIELD (t
))), t
);
2525 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2526 prepare_fresh_vtable (binfo
, t
);
2529 if (fndecl
== NULL_TREE
)
2532 n
= skip_rtti_stuff (&virtuals
);
2536 tree current_fndecl
= TREE_VALUE (virtuals
);
2537 current_fndecl
= FNADDR_FROM_VTABLE_ENTRY (current_fndecl
);
2538 current_fndecl
= TREE_OPERAND (current_fndecl
, 0);
2539 if (current_fndecl
&& overrides (fndecl
, current_fndecl
))
2541 tree base_offset
, offset
;
2542 tree context
= DECL_CLASS_CONTEXT (fndecl
);
2543 tree vfield
= CLASSTYPE_VFIELD (t
);
2546 offset
= get_class_offset (context
, t
, binfo
, fndecl
);
2548 /* Find the right offset for the this pointer based on the
2549 base class we just found. We have to take into
2550 consideration the virtual base class pointers that we
2551 stick in before the virtual function table pointer.
2553 Also, we want just the delta between the most base class
2554 that we derived this vfield from and us. */
2555 base_offset
= size_binop (PLUS_EXPR
,
2556 get_derived_offset (binfo
, DECL_CONTEXT (current_fndecl
)),
2557 BINFO_OFFSET (binfo
));
2558 this_offset
= ssize_binop (MINUS_EXPR
, offset
, base_offset
);
2560 if (binfo
== TYPE_BINFO (t
))
2562 /* In this case, it is *type*'s vtable we are modifying.
2563 We start with the approximation that it's vtable is that
2564 of the immediate base class. */
2565 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2566 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield
)), t
);
2570 /* This is our very own copy of `basetype' to play with.
2571 Later, we will fill in all the virtual functions
2572 that override the virtual functions in these base classes
2573 which are not defined by the current type. */
2574 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2575 prepare_fresh_vtable (binfo
, t
);
2579 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo
)));
2581 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo
), n
),
2582 build_vtable_entry (this_offset
, pfn
),
2586 virtuals
= TREE_CHAIN (virtuals
);
2590 /* These are the ones that are not through virtual base classes. */
2593 modify_all_direct_vtables (binfo
, do_self
, t
, fndecl
, pfn
)
2596 tree t
, fndecl
, pfn
;
2598 tree binfos
= BINFO_BASETYPES (binfo
);
2599 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2601 /* Should we use something besides CLASSTYPE_VFIELDS? */
2602 if (do_self
&& CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
2604 modify_one_vtable (binfo
, t
, fndecl
, pfn
);
2607 for (i
= 0; i
< n_baselinks
; i
++)
2609 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2610 int is_not_base_vtable
2611 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
2612 if (! TREE_VIA_VIRTUAL (base_binfo
))
2613 modify_all_direct_vtables (base_binfo
, is_not_base_vtable
, t
, fndecl
, pfn
);
2617 /* Fixup all the delta entries in this one vtable that need updating. */
2620 fixup_vtable_deltas1 (binfo
, t
)
2623 tree virtuals
= BINFO_VIRTUALS (binfo
);
2624 unsigned HOST_WIDE_INT n
;
2626 n
= skip_rtti_stuff (&virtuals
);
2630 tree fndecl
= TREE_VALUE (virtuals
);
2631 tree pfn
= FNADDR_FROM_VTABLE_ENTRY (fndecl
);
2632 tree delta
= DELTA_FROM_VTABLE_ENTRY (fndecl
);
2633 fndecl
= TREE_OPERAND (pfn
, 0);
2636 tree base_offset
, offset
;
2637 tree context
= DECL_CLASS_CONTEXT (fndecl
);
2638 tree vfield
= CLASSTYPE_VFIELD (t
);
2641 offset
= get_class_offset (context
, t
, binfo
, fndecl
);
2643 /* Find the right offset for the this pointer based on the
2644 base class we just found. We have to take into
2645 consideration the virtual base class pointers that we
2646 stick in before the virtual function table pointer.
2648 Also, we want just the delta between the most base class
2649 that we derived this vfield from and us. */
2650 base_offset
= size_binop (PLUS_EXPR
,
2651 get_derived_offset (binfo
,
2652 DECL_CONTEXT (fndecl
)),
2653 BINFO_OFFSET (binfo
));
2654 this_offset
= ssize_binop (MINUS_EXPR
, offset
, base_offset
);
2656 if (! tree_int_cst_equal (this_offset
, delta
))
2658 /* Make sure we can modify the derived association with immunity. */
2659 if (binfo
== TYPE_BINFO (t
))
2661 /* In this case, it is *type*'s vtable we are modifying.
2662 We start with the approximation that it's vtable is that
2663 of the immediate base class. */
2664 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2665 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield
)), t
);
2669 /* This is our very own copy of `basetype' to play with.
2670 Later, we will fill in all the virtual functions
2671 that override the virtual functions in these base classes
2672 which are not defined by the current type. */
2673 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2674 prepare_fresh_vtable (binfo
, t
);
2677 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo
), n
),
2678 build_vtable_entry (this_offset
, pfn
),
2683 virtuals
= TREE_CHAIN (virtuals
);
2687 /* Fixup all the delta entries in all the direct vtables that need updating.
2688 This happens when we have non-overridden virtual functions from a
2689 virtual base class, that are at a different offset, in the new
2690 hierarchy, because the layout of the virtual bases has changed. */
2693 fixup_vtable_deltas (binfo
, init_self
, t
)
2698 tree binfos
= BINFO_BASETYPES (binfo
);
2699 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2701 for (i
= 0; i
< n_baselinks
; i
++)
2703 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2704 int is_not_base_vtable
2705 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
2706 if (! TREE_VIA_VIRTUAL (base_binfo
))
2707 fixup_vtable_deltas (base_binfo
, is_not_base_vtable
, t
);
2709 /* Should we use something besides CLASSTYPE_VFIELDS? */
2710 if (init_self
&& CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
2712 fixup_vtable_deltas1 (binfo
, t
);
2716 /* These are the ones that are through virtual base classes. */
2719 modify_all_indirect_vtables (binfo
, do_self
, via_virtual
, t
, fndecl
, pfn
)
2721 int do_self
, via_virtual
;
2722 tree t
, fndecl
, pfn
;
2724 tree binfos
= BINFO_BASETYPES (binfo
);
2725 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2727 /* Should we use something besides CLASSTYPE_VFIELDS? */
2728 if (do_self
&& via_virtual
&& CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
2730 modify_one_vtable (binfo
, t
, fndecl
, pfn
);
2733 for (i
= 0; i
< n_baselinks
; i
++)
2735 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2736 int is_not_base_vtable
2737 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
2738 if (TREE_VIA_VIRTUAL (base_binfo
))
2741 base_binfo
= binfo_member (BINFO_TYPE (base_binfo
), CLASSTYPE_VBASECLASSES (t
));
2743 modify_all_indirect_vtables (base_binfo
, is_not_base_vtable
, via_virtual
, t
, fndecl
, pfn
);
2748 modify_all_vtables (t
, fndecl
, vfn
)
2749 tree t
, fndecl
, vfn
;
2751 /* Do these first, so that we will make use of any non-virtual class's
2752 vtable, over a virtual classes vtable. */
2753 modify_all_direct_vtables (TYPE_BINFO (t
), 1, t
, fndecl
, vfn
);
2754 if (TYPE_USES_VIRTUAL_BASECLASSES (t
))
2755 modify_all_indirect_vtables (TYPE_BINFO (t
), 1, 0, t
, fndecl
, vfn
);
2758 /* Here, we already know that they match in every respect.
2759 All we have to check is where they had their declarations. */
2762 strictly_overrides (fndecl1
, fndecl2
)
2763 tree fndecl1
, fndecl2
;
2765 int distance
= get_base_distance (DECL_CLASS_CONTEXT (fndecl2
),
2766 DECL_CLASS_CONTEXT (fndecl1
),
2768 if (distance
== -2 || distance
> 0)
2773 /* Merge overrides for one vtable.
2774 If we want to merge in same function, we are fine.
2776 if one has a DECL_CLASS_CONTEXT that is a parent of the
2777 other, than choose the more derived one
2779 potentially ill-formed (see 10.3 [class.virtual])
2780 we have to check later to see if there was an
2781 override in this class. If there was ok, if not
2782 then it is ill-formed. (mrs)
2784 We take special care to reuse a vtable, if we can. */
2787 override_one_vtable (binfo
, old
, t
)
2790 tree virtuals
= BINFO_VIRTUALS (binfo
);
2791 tree old_virtuals
= BINFO_VIRTUALS (old
);
2792 enum { REUSE_NEW
, REUSE_OLD
, UNDECIDED
, NEITHER
} choose
= UNDECIDED
;
2794 /* If we have already committed to modifying it, then don't try and
2795 reuse another vtable. */
2796 if (BINFO_NEW_VTABLE_MARKED (binfo
))
2799 skip_rtti_stuff (&virtuals
);
2800 skip_rtti_stuff (&old_virtuals
);
2804 tree fndecl
= TREE_VALUE (virtuals
);
2805 tree old_fndecl
= TREE_VALUE (old_virtuals
);
2806 fndecl
= FNADDR_FROM_VTABLE_ENTRY (fndecl
);
2807 old_fndecl
= FNADDR_FROM_VTABLE_ENTRY (old_fndecl
);
2808 fndecl
= TREE_OPERAND (fndecl
, 0);
2809 old_fndecl
= TREE_OPERAND (old_fndecl
, 0);
2810 /* First check to see if they are the same. */
2811 if (DECL_ASSEMBLER_NAME (fndecl
) == DECL_ASSEMBLER_NAME (old_fndecl
))
2813 /* No need to do anything. */
2815 else if (strictly_overrides (fndecl
, old_fndecl
))
2817 if (choose
== UNDECIDED
)
2819 else if (choose
== REUSE_OLD
)
2822 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2824 prepare_fresh_vtable (binfo
, t
);
2825 override_one_vtable (binfo
, old
, t
);
2830 else if (strictly_overrides (old_fndecl
, fndecl
))
2832 if (choose
== UNDECIDED
)
2834 else if (choose
== REUSE_NEW
)
2837 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2839 prepare_fresh_vtable (binfo
, t
);
2840 override_one_vtable (binfo
, old
, t
);
2843 TREE_VALUE (virtuals
) = TREE_VALUE (old_virtuals
);
2845 else if (choose
== NEITHER
)
2847 TREE_VALUE (virtuals
) = TREE_VALUE (old_virtuals
);
2853 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2855 prepare_fresh_vtable (binfo
, t
);
2856 override_one_vtable (binfo
, old
, t
);
2860 /* This MUST be overridden, or the class is ill-formed. */
2861 tree fndecl
= TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals
)), 0);
2864 fndecl
= copy_node (fndecl
);
2865 copy_lang_decl (fndecl
);
2866 DECL_NEEDS_FINAL_OVERRIDER_P (fndecl
) = 1;
2867 /* Make sure we search for it later. */
2868 if (! CLASSTYPE_ABSTRACT_VIRTUALS (t
))
2869 CLASSTYPE_ABSTRACT_VIRTUALS (t
) = error_mark_node
;
2871 vfn
= build1 (ADDR_EXPR
, vfunc_ptr_type_node
, fndecl
);
2872 TREE_CONSTANT (vfn
) = 1;
2874 /* We can use integer_zero_node, as we will core dump
2875 if this is used anyway. */
2876 TREE_VALUE (virtuals
) = build_vtable_entry (integer_zero_node
, vfn
);
2879 virtuals
= TREE_CHAIN (virtuals
);
2880 old_virtuals
= TREE_CHAIN (old_virtuals
);
2883 /* Let's reuse the old vtable. */
2884 if (choose
== REUSE_OLD
)
2886 BINFO_VTABLE (binfo
) = BINFO_VTABLE (old
);
2887 BINFO_VIRTUALS (binfo
) = BINFO_VIRTUALS (old
);
2891 /* Merge in overrides for virtual bases.
2892 BINFO is the hierarchy we want to modify, and OLD has the potential
2896 merge_overrides (binfo
, old
, do_self
, t
)
2901 tree binfos
= BINFO_BASETYPES (binfo
);
2902 tree old_binfos
= BINFO_BASETYPES (old
);
2903 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2905 /* Should we use something besides CLASSTYPE_VFIELDS? */
2906 if (do_self
&& CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
2908 override_one_vtable (binfo
, old
, t
);
2911 for (i
= 0; i
< n_baselinks
; i
++)
2913 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2914 tree old_base_binfo
= TREE_VEC_ELT (old_binfos
, i
);
2915 int is_not_base_vtable
2916 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
2917 if (! TREE_VIA_VIRTUAL (base_binfo
))
2918 merge_overrides (base_binfo
, old_base_binfo
, is_not_base_vtable
, t
);
2922 /* Get the base virtual function declarations in T that are either
2923 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2924 the overrider/hider. */
2927 get_basefndecls (fndecl
, t
)
2930 tree methods
= TYPE_METHODS (t
);
2931 tree base_fndecls
= NULL_TREE
;
2932 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
2933 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2937 if (TREE_CODE (methods
) == FUNCTION_DECL
2938 && DECL_VINDEX (methods
) != NULL_TREE
2939 && DECL_NAME (fndecl
) == DECL_NAME (methods
))
2940 base_fndecls
= temp_tree_cons (fndecl
, methods
, base_fndecls
);
2942 methods
= TREE_CHAIN (methods
);
2946 return base_fndecls
;
2948 for (i
= 0; i
< n_baseclasses
; i
++)
2950 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2951 tree basetype
= BINFO_TYPE (base_binfo
);
2953 base_fndecls
= chainon (get_basefndecls (fndecl
, basetype
),
2957 return base_fndecls
;
2960 /* Mark the functions that have been hidden with their overriders.
2961 Since we start out with all functions already marked with a hider,
2962 no need to mark functions that are just hidden. */
2965 mark_overriders (fndecl
, base_fndecls
)
2966 tree fndecl
, base_fndecls
;
2968 while (base_fndecls
)
2970 if (overrides (TREE_VALUE (base_fndecls
), fndecl
))
2971 TREE_PURPOSE (base_fndecls
) = fndecl
;
2973 base_fndecls
= TREE_CHAIN (base_fndecls
);
2977 /* If this declaration supersedes the declaration of
2978 a method declared virtual in the base class, then
2979 mark this field as being virtual as well. */
2982 check_for_override (decl
, ctype
)
2985 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (ctype
));
2986 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2987 int virtualp
= DECL_VIRTUAL_P (decl
);
2989 for (i
= 0; i
< n_baselinks
; i
++)
2991 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2992 if (TYPE_VIRTUAL_P (BINFO_TYPE (base_binfo
)))
2994 tree tmp
= get_matching_virtual
2996 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl
)));
2999 /* If this function overrides some virtual in some base
3000 class, then the function itself is also necessarily
3001 virtual, even if the user didn't explicitly say so. */
3002 DECL_VIRTUAL_P (decl
) = 1;
3004 /* The TMP we really want is the one from the deepest
3005 baseclass on this path, taking care not to
3006 duplicate if we have already found it (via another
3007 path to its virtual baseclass. */
3008 if (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
)
3010 cp_error_at ("method `%D' may not be declared static",
3012 cp_error_at ("(since `%D' declared virtual in base class.)",
3018 #if 0 /* The signature of an overriding function is not changed. */
3020 /* The argument types may have changed... */
3021 tree type
= TREE_TYPE (decl
);
3022 tree argtypes
= TYPE_ARG_TYPES (type
);
3023 tree base_variant
= TREE_TYPE (TREE_VALUE (argtypes
));
3024 tree raises
= TYPE_RAISES_EXCEPTIONS (type
);
3026 argtypes
= commonparms (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (tmp
))),
3027 TREE_CHAIN (argtypes
));
3028 /* But the return type has not. */
3029 type
= build_cplus_method_type (base_variant
, TREE_TYPE (type
), argtypes
);
3031 type
= build_exception_variant (type
, raises
);
3032 TREE_TYPE (decl
) = type
;
3036 = tree_cons (NULL_TREE
, tmp
, DECL_VINDEX (decl
));
3043 if (DECL_VINDEX (decl
) == NULL_TREE
)
3044 DECL_VINDEX (decl
) = error_mark_node
;
3045 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
)) = 1;
3049 /* Warn about hidden virtual functions that are not overridden in t.
3050 We know that constructors and destructors don't apply. */
3056 tree method_vec
= CLASSTYPE_METHOD_VEC (t
);
3057 int n_methods
= method_vec
? TREE_VEC_LENGTH (method_vec
) : 0;
3060 /* We go through each separately named virtual function. */
3061 for (i
= 2; i
< n_methods
&& TREE_VEC_ELT (method_vec
, i
); ++i
)
3063 tree fns
= TREE_VEC_ELT (method_vec
, i
);
3066 tree base_fndecls
= NULL_TREE
;
3067 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
3068 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
3070 fndecl
= OVL_CURRENT (fns
);
3071 if (DECL_VINDEX (fndecl
) == NULL_TREE
)
3074 /* First we get a list of all possible functions that might be
3075 hidden from each base class. */
3076 for (i
= 0; i
< n_baseclasses
; i
++)
3078 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
3079 tree basetype
= BINFO_TYPE (base_binfo
);
3081 base_fndecls
= chainon (get_basefndecls (fndecl
, basetype
),
3085 fns
= OVL_NEXT (fns
);
3087 fndecl
= OVL_CURRENT (fns
);
3091 /* ...then mark up all the base functions with overriders, preferring
3092 overriders to hiders. */
3096 mark_overriders (fndecl
, base_fndecls
);
3098 fns
= OVL_NEXT (fns
);
3100 fndecl
= OVL_CURRENT (fns
);
3105 /* Now give a warning for all base functions without overriders,
3106 as they are hidden. */
3107 while (base_fndecls
)
3109 if (! overrides (TREE_VALUE (base_fndecls
),
3110 TREE_PURPOSE (base_fndecls
)))
3112 /* Here we know it is a hider, and no overrider exists. */
3113 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls
));
3114 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls
));
3117 base_fndecls
= TREE_CHAIN (base_fndecls
);
3122 /* Check for things that are invalid. There are probably plenty of other
3123 things we should check for also. */
3126 finish_struct_anon (t
)
3130 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
3132 if (TREE_STATIC (field
))
3134 if (TREE_CODE (field
) != FIELD_DECL
)
3137 if (DECL_NAME (field
) == NULL_TREE
3138 && TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
3140 tree
* uelt
= &TYPE_FIELDS (TREE_TYPE (field
));
3141 for (; *uelt
; uelt
= &TREE_CHAIN (*uelt
))
3143 if (DECL_ARTIFICIAL (*uelt
))
3146 if (DECL_NAME (*uelt
) == constructor_name (t
))
3147 cp_pedwarn_at ("ANSI C++ forbids member `%D' with same name as enclosing class",
3150 if (TREE_CODE (*uelt
) != FIELD_DECL
)
3152 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3157 if (TREE_PRIVATE (*uelt
))
3158 cp_pedwarn_at ("private member `%#D' in anonymous union",
3160 else if (TREE_PROTECTED (*uelt
))
3161 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3164 TREE_PRIVATE (*uelt
) = TREE_PRIVATE (field
);
3165 TREE_PROTECTED (*uelt
) = TREE_PROTECTED (field
);
3171 extern int interface_only
, interface_unknown
;
3173 /* Create default constructors, assignment operators, and so forth for
3174 the type indicated by T, if they are needed.
3175 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3176 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3177 cannot have a default constructor, copy constructor taking a const
3178 reference argument, or an assignment operator, respectively. If a
3179 virtual destructor is created, its DECL is returned; otherwise the
3180 return value is NULL_TREE. */
3183 add_implicitly_declared_members (t
, cant_have_default_ctor
,
3184 cant_have_const_cctor
,
3185 cant_have_assignment
)
3187 int cant_have_default_ctor
;
3188 int cant_have_const_cctor
;
3189 int cant_have_assignment
;
3192 tree implicit_fns
= NULL_TREE
;
3193 tree name
= TYPE_IDENTIFIER (t
);
3194 tree virtual_dtor
= NULL_TREE
;
3198 if (TYPE_NEEDS_DESTRUCTOR (t
) && !TYPE_HAS_DESTRUCTOR (t
)
3199 && !IS_SIGNATURE (t
))
3201 default_fn
= cons_up_default_function (t
, name
, 0);
3202 check_for_override (default_fn
, t
);
3204 /* If we couldn't make it work, then pretend we didn't need it. */
3205 if (default_fn
== void_type_node
)
3206 TYPE_NEEDS_DESTRUCTOR (t
) = 0;
3209 TREE_CHAIN (default_fn
) = implicit_fns
;
3210 implicit_fns
= default_fn
;
3212 if (DECL_VINDEX (default_fn
))
3213 virtual_dtor
= default_fn
;
3216 TYPE_NEEDS_DESTRUCTOR (t
) |= TYPE_HAS_DESTRUCTOR (t
);
3218 /* Default constructor. */
3219 if (! TYPE_HAS_CONSTRUCTOR (t
) && ! cant_have_default_ctor
3220 && ! IS_SIGNATURE (t
))
3222 default_fn
= cons_up_default_function (t
, name
, 2);
3223 TREE_CHAIN (default_fn
) = implicit_fns
;
3224 implicit_fns
= default_fn
;
3227 /* Copy constructor. */
3228 if (! TYPE_HAS_INIT_REF (t
) && ! IS_SIGNATURE (t
) && ! TYPE_FOR_JAVA (t
))
3230 /* ARM 12.18: You get either X(X&) or X(const X&), but
3232 default_fn
= cons_up_default_function (t
, name
,
3233 3 + cant_have_const_cctor
);
3234 TREE_CHAIN (default_fn
) = implicit_fns
;
3235 implicit_fns
= default_fn
;
3238 /* Assignment operator. */
3239 if (! TYPE_HAS_ASSIGN_REF (t
) && ! IS_SIGNATURE (t
) && ! TYPE_FOR_JAVA (t
))
3241 default_fn
= cons_up_default_function (t
, name
,
3242 5 + cant_have_assignment
);
3243 TREE_CHAIN (default_fn
) = implicit_fns
;
3244 implicit_fns
= default_fn
;
3247 /* Now, hook all of the new functions on to TYPE_METHODS,
3248 and add them to the CLASSTYPE_METHOD_VEC. */
3249 for (f
= &implicit_fns
; *f
; f
= &TREE_CHAIN (*f
))
3250 add_method (t
, 0, *f
);
3251 *f
= TYPE_METHODS (t
);
3252 TYPE_METHODS (t
) = implicit_fns
;
3254 return virtual_dtor
;
3257 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
3258 (or C++ class declaration).
3260 For C++, we must handle the building of derived classes.
3261 Also, C++ allows static class members. The way that this is
3262 handled is to keep the field name where it is (as the DECL_NAME
3263 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
3264 of the field. layout_record and layout_union will know about this.
3266 More C++ hair: inline functions have text in their
3267 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
3268 meaningful tree structure. After the struct has been laid out, set
3269 things up so that this can happen.
3271 And still more: virtual functions. In the case of single inheritance,
3272 when a new virtual function is seen which redefines a virtual function
3273 from the base class, the new virtual function is placed into
3274 the virtual function table at exactly the same address that
3275 it had in the base class. When this is extended to multiple
3276 inheritance, the same thing happens, except that multiple virtual
3277 function tables must be maintained. The first virtual function
3278 table is treated in exactly the same way as in the case of single
3279 inheritance. Additional virtual function tables have different
3280 DELTAs, which tell how to adjust `this' to point to the right thing.
3282 ATTRIBUTES is the set of decl attributes to be applied, if any. */
3285 finish_struct_1 (t
, warn_anon
)
3290 enum tree_code code
= TREE_CODE (t
);
3291 tree fields
= TYPE_FIELDS (t
);
3292 tree x
, last_x
, method_vec
;
3294 int max_has_virtual
;
3295 tree pending_virtuals
= NULL_TREE
;
3296 tree pending_hard_virtuals
= NULL_TREE
;
3297 tree abstract_virtuals
= NULL_TREE
;
3301 int cant_have_default_ctor
;
3302 int cant_have_const_ctor
;
3303 int no_const_asn_ref
;
3305 /* The index of the first base class which has virtual
3306 functions. Only applied to non-virtual baseclasses. */
3307 int first_vfn_base_index
;
3310 int any_default_members
= 0;
3311 int const_sans_init
= 0;
3312 int ref_sans_init
= 0;
3313 tree access_decls
= NULL_TREE
;
3316 int has_pointers
= 0;
3318 if (warn_anon
&& code
!= UNION_TYPE
&& ANON_AGGRNAME_P (TYPE_IDENTIFIER (t
)))
3319 pedwarn ("anonymous class type not used to declare any objects");
3323 if (IS_AGGR_TYPE (t
))
3324 cp_error ("redefinition of `%#T'", t
);
3326 my_friendly_abort (172);
3331 GNU_xref_decl (current_function_decl
, t
);
3333 /* If this type was previously laid out as a forward reference,
3334 make sure we lay it out again. */
3336 TYPE_SIZE (t
) = NULL_TREE
;
3337 CLASSTYPE_GOT_SEMICOLON (t
) = 0;
3340 /* This is in general too late to do this. I moved the main case up to
3341 left_curly, what else needs to move? */
3342 if (! IS_SIGNATURE (t
))
3344 my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t
) == interface_only
, 999);
3345 my_friendly_assert (CLASSTYPE_INTERFACE_KNOWN (t
) == ! interface_unknown
, 999);
3349 old
= suspend_momentary ();
3351 /* Install struct as DECL_FIELD_CONTEXT of each field decl.
3352 Also process specified field sizes.
3353 Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
3354 The specified size is found in the DECL_INITIAL.
3355 Store 0 there, except for ": 0" fields (so we can find them
3356 and delete them, below). */
3358 if (TYPE_BINFO_BASETYPES (t
))
3359 n_baseclasses
= TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t
));
3363 if (n_baseclasses
> 0)
3365 struct base_info base_info
;
3367 first_vfn_base_index
= finish_base_struct (t
, &base_info
);
3368 /* Remember where we got our vfield from. */
3369 CLASSTYPE_VFIELD_PARENT (t
) = first_vfn_base_index
;
3370 has_virtual
= base_info
.has_virtual
;
3371 max_has_virtual
= base_info
.max_has_virtual
;
3372 CLASSTYPE_N_SUPERCLASSES (t
) += base_info
.n_ancestors
;
3373 vfield
= base_info
.vfield
;
3374 vfields
= base_info
.vfields
;
3375 CLASSTYPE_RTTI (t
) = base_info
.rtti
;
3376 cant_have_default_ctor
= base_info
.cant_have_default_ctor
;
3377 cant_have_const_ctor
= base_info
.cant_have_const_ctor
;
3378 no_const_asn_ref
= base_info
.no_const_asn_ref
;
3383 first_vfn_base_index
= -1;
3385 max_has_virtual
= has_virtual
;
3387 vfields
= NULL_TREE
;
3388 CLASSTYPE_RTTI (t
) = NULL_TREE
;
3389 cant_have_default_ctor
= 0;
3390 cant_have_const_ctor
= 0;
3391 no_const_asn_ref
= 0;
3395 /* Both of these should be done before now. */
3396 if (write_virtuals
== 3 && CLASSTYPE_INTERFACE_KNOWN (t
)
3397 && ! IS_SIGNATURE (t
))
3399 my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t
) == interface_only
, 999);
3400 my_friendly_assert (CLASSTYPE_VTABLE_NEEDS_WRITING (t
) == ! interface_only
, 999);
3404 /* The three of these are approximations which may later be
3405 modified. Needed at this point to make add_virtual_function
3406 and modify_vtable_entries work. */
3407 CLASSTYPE_VFIELDS (t
) = vfields
;
3408 CLASSTYPE_VFIELD (t
) = vfield
;
3410 for (x
= TYPE_METHODS (t
); x
; x
= TREE_CHAIN (x
))
3412 GNU_xref_member (current_class_name
, x
);
3414 /* If this was an evil function, don't keep it in class. */
3415 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x
)))
3418 /* Do both of these, even though they're in the same union;
3419 if the insn `r' member and the size `i' member are
3420 different sizes, as on the alpha, the larger of the two
3421 will end up with garbage in it. */
3422 DECL_SAVED_INSNS (x
) = NULL_RTX
;
3423 DECL_FIELD_SIZE (x
) = 0;
3425 check_for_override (x
, t
);
3426 if (DECL_ABSTRACT_VIRTUAL_P (x
) && ! DECL_VINDEX (x
))
3427 cp_error_at ("initializer specified for non-virtual method `%D'", x
);
3429 /* The name of the field is the original field name
3430 Save this in auxiliary field for later overloading. */
3431 if (DECL_VINDEX (x
))
3433 add_virtual_function (&pending_virtuals
, &pending_hard_virtuals
,
3434 &has_virtual
, x
, t
);
3435 if (DECL_ABSTRACT_VIRTUAL_P (x
))
3436 abstract_virtuals
= tree_cons (NULL_TREE
, x
, abstract_virtuals
);
3438 /* XXX Why did I comment this out? (jason) */
3446 fields
= chainon (build_vbase_pointer_fields (t
), fields
);
3449 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
3451 GNU_xref_member (current_class_name
, x
);
3453 if (TREE_CODE (x
) == FIELD_DECL
)
3455 DECL_PACKED (x
) |= TYPE_PACKED (t
);
3459 if (TREE_CODE (x
) == USING_DECL
)
3461 /* Save access declarations for later. */
3463 TREE_CHAIN (last_x
) = TREE_CHAIN (x
);
3465 fields
= TREE_CHAIN (x
);
3467 access_decls
= scratch_tree_cons (NULL_TREE
, x
, access_decls
);
3473 if (TREE_CODE (x
) == TYPE_DECL
3474 || TREE_CODE (x
) == TEMPLATE_DECL
)
3477 /* If we've gotten this far, it's a data member, possibly static,
3478 or an enumerator. */
3480 DECL_FIELD_CONTEXT (x
) = t
;
3482 /* ``A local class cannot have static data members.'' ARM 9.4 */
3483 if (current_function_decl
&& TREE_STATIC (x
))
3484 cp_error_at ("field `%D' in local class cannot be static", x
);
3486 /* Perform error checking that did not get done in
3488 if (TREE_CODE (TREE_TYPE (x
)) == FUNCTION_TYPE
)
3490 cp_error_at ("field `%D' invalidly declared function type",
3492 TREE_TYPE (x
) = build_pointer_type (TREE_TYPE (x
));
3494 else if (TREE_CODE (TREE_TYPE (x
)) == METHOD_TYPE
)
3496 cp_error_at ("field `%D' invalidly declared method type", x
);
3497 TREE_TYPE (x
) = build_pointer_type (TREE_TYPE (x
));
3499 else if (TREE_CODE (TREE_TYPE (x
)) == OFFSET_TYPE
)
3501 cp_error_at ("field `%D' invalidly declared offset type", x
);
3502 TREE_TYPE (x
) = build_pointer_type (TREE_TYPE (x
));
3506 if (DECL_NAME (x
) == constructor_name (t
))
3507 cant_have_default_ctor
= 1;
3510 if (TREE_TYPE (x
) == error_mark_node
)
3513 DECL_SAVED_INSNS (x
) = NULL_RTX
;
3514 DECL_FIELD_SIZE (x
) = 0;
3516 /* When this goes into scope, it will be a non-local reference. */
3517 DECL_NONLOCAL (x
) = 1;
3519 if (TREE_CODE (x
) == CONST_DECL
)
3522 if (TREE_CODE (x
) == VAR_DECL
)
3524 if (TREE_CODE (t
) == UNION_TYPE
)
3525 /* Unions cannot have static members. */
3526 cp_error_at ("field `%D' declared static in union", x
);
3531 /* Now it can only be a FIELD_DECL. */
3533 if (TREE_PRIVATE (x
) || TREE_PROTECTED (x
))
3536 /* If this is of reference type, check if it needs an init.
3537 Also do a little ANSI jig if necessary. */
3538 if (TREE_CODE (TREE_TYPE (x
)) == REFERENCE_TYPE
)
3540 if (DECL_INITIAL (x
) == NULL_TREE
)
3543 /* ARM $12.6.2: [A member initializer list] (or, for an
3544 aggregate, initialization by a brace-enclosed list) is the
3545 only way to initialize nonstatic const and reference
3547 cant_have_default_ctor
= 1;
3548 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) = 1;
3550 if (! TYPE_HAS_CONSTRUCTOR (t
) && extra_warnings
)
3553 cp_warning_at ("non-static reference `%#D' in class without a constructor", x
);
3555 cp_warning_at ("non-static reference in class without a constructor", x
);
3559 if (TREE_CODE (TREE_TYPE (x
)) == POINTER_TYPE
)
3562 /* If any field is const, the structure type is pseudo-const. */
3563 if (TREE_READONLY (x
))
3565 C_TYPE_FIELDS_READONLY (t
) = 1;
3566 if (DECL_INITIAL (x
) == NULL_TREE
)
3567 const_sans_init
= 1;
3569 /* ARM $12.6.2: [A member initializer list] (or, for an
3570 aggregate, initialization by a brace-enclosed list) is the
3571 only way to initialize nonstatic const and reference
3573 cant_have_default_ctor
= 1;
3574 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) = 1;
3576 if (! TYPE_HAS_CONSTRUCTOR (t
) && !IS_SIGNATURE (t
)
3580 cp_warning_at ("non-static const member `%#D' in class without a constructor", x
);
3582 cp_warning_at ("non-static const member in class without a constructor", x
);
3587 /* A field that is pseudo-const makes the structure
3589 tree t1
= TREE_TYPE (x
);
3590 while (TREE_CODE (t1
) == ARRAY_TYPE
)
3591 t1
= TREE_TYPE (t1
);
3592 if (IS_AGGR_TYPE (t1
))
3594 if (C_TYPE_FIELDS_READONLY (t1
))
3595 C_TYPE_FIELDS_READONLY (t
) = 1;
3596 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1
))
3597 const_sans_init
= 1;
3601 /* We set DECL_C_BIT_FIELD in grokbitfield.
3602 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3603 if (DECL_C_BIT_FIELD (x
))
3605 /* Invalid bit-field size done by grokfield. */
3606 /* Detect invalid bit-field type. */
3607 if (DECL_INITIAL (x
)
3608 && ! INTEGRAL_TYPE_P (TREE_TYPE (x
)))
3610 cp_error_at ("bit-field `%#D' with non-integral type", x
);
3611 DECL_INITIAL (x
) = NULL
;
3614 /* Detect and ignore out of range field width. */
3615 if (DECL_INITIAL (x
))
3617 tree w
= DECL_INITIAL (x
);
3618 register int width
= 0;
3620 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3623 /* detect invalid field size. */
3624 if (TREE_CODE (w
) == CONST_DECL
)
3625 w
= DECL_INITIAL (w
);
3626 else if (TREE_READONLY_DECL_P (w
))
3627 w
= decl_constant_value (w
);
3629 if (TREE_CODE (w
) != INTEGER_CST
)
3631 cp_error_at ("bit-field `%D' width not an integer constant",
3633 DECL_INITIAL (x
) = NULL_TREE
;
3635 else if (width
= TREE_INT_CST_LOW (w
),
3638 DECL_INITIAL (x
) = NULL
;
3639 cp_error_at ("negative width in bit-field `%D'", x
);
3641 else if (width
== 0 && DECL_NAME (x
) != 0)
3643 DECL_INITIAL (x
) = NULL
;
3644 cp_error_at ("zero width for bit-field `%D'", x
);
3647 > TYPE_PRECISION (long_long_unsigned_type_node
))
3649 /* The backend will dump if you try to use something
3650 too big; avoid that. */
3651 DECL_INITIAL (x
) = NULL
;
3652 sorry ("bit-fields larger than %d bits",
3653 TYPE_PRECISION (long_long_unsigned_type_node
));
3654 cp_error_at (" in declaration of `%D'", x
);
3656 else if (width
> TYPE_PRECISION (TREE_TYPE (x
))
3657 && TREE_CODE (TREE_TYPE (x
)) != ENUMERAL_TYPE
3658 && TREE_CODE (TREE_TYPE (x
)) != BOOLEAN_TYPE
)
3660 cp_warning_at ("width of `%D' exceeds its type", x
);
3662 else if (TREE_CODE (TREE_TYPE (x
)) == ENUMERAL_TYPE
3663 && ((min_precision (TYPE_MIN_VALUE (TREE_TYPE (x
)),
3664 TREE_UNSIGNED (TREE_TYPE (x
))) > width
)
3665 || (min_precision (TYPE_MAX_VALUE (TREE_TYPE (x
)),
3666 TREE_UNSIGNED (TREE_TYPE (x
))) > width
)))
3668 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3672 if (DECL_INITIAL (x
) == NULL_TREE
)
3674 else if (width
== 0)
3676 #ifdef EMPTY_FIELD_BOUNDARY
3677 DECL_ALIGN (x
) = MAX (DECL_ALIGN (x
), EMPTY_FIELD_BOUNDARY
);
3679 #ifdef PCC_BITFIELD_TYPE_MATTERS
3680 DECL_ALIGN (x
) = MAX (DECL_ALIGN (x
),
3681 TYPE_ALIGN (TREE_TYPE (x
)));
3686 DECL_INITIAL (x
) = NULL_TREE
;
3687 DECL_FIELD_SIZE (x
) = width
;
3688 DECL_BIT_FIELD (x
) = 1;
3692 /* Non-bit-fields are aligned for their type. */
3693 DECL_ALIGN (x
) = MAX (DECL_ALIGN (x
), TYPE_ALIGN (TREE_TYPE (x
)));
3697 tree type
= TREE_TYPE (x
);
3699 while (TREE_CODE (type
) == ARRAY_TYPE
)
3700 type
= TREE_TYPE (type
);
3702 if (TYPE_LANG_SPECIFIC (type
) && ! ANON_UNION_P (x
)
3703 && ! TYPE_PTRMEMFUNC_P (type
))
3705 /* Never let anything with uninheritable virtuals
3706 make it through without complaint. */
3707 if (CLASSTYPE_ABSTRACT_VIRTUALS (type
))
3708 abstract_virtuals_error (x
, type
);
3710 /* Don't let signatures make it through either. */
3711 if (IS_SIGNATURE (type
))
3712 signature_error (x
, type
);
3714 if (code
== UNION_TYPE
)
3717 if (TYPE_NEEDS_CONSTRUCTING (type
))
3718 fie
= "constructor";
3719 else if (TYPE_NEEDS_DESTRUCTOR (type
))
3721 else if (TYPE_HAS_COMPLEX_ASSIGN_REF (type
))
3722 fie
= "copy assignment operator";
3724 cp_error_at ("member `%#D' with %s not allowed in union", x
,
3729 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (type
);
3730 TYPE_NEEDS_DESTRUCTOR (t
) |= TYPE_NEEDS_DESTRUCTOR (type
);
3731 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type
);
3732 TYPE_HAS_COMPLEX_INIT_REF (t
) |= TYPE_HAS_COMPLEX_INIT_REF (type
);
3735 if (!TYPE_HAS_CONST_INIT_REF (type
))
3736 cant_have_const_ctor
= 1;
3738 if (!TYPE_HAS_CONST_ASSIGN_REF (type
))
3739 no_const_asn_ref
= 1;
3741 if (TYPE_HAS_CONSTRUCTOR (type
)
3742 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
3744 cant_have_default_ctor
= 1;
3746 /* This is wrong for aggregates. */
3747 if (! TYPE_HAS_CONSTRUCTOR (t
))
3750 cp_pedwarn_at ("member `%#D' with only non-default constructor", x
);
3752 cp_pedwarn_at ("member with only non-default constructor", x
);
3753 cp_pedwarn_at ("in class without a constructor",
3759 if (DECL_INITIAL (x
) != NULL_TREE
)
3761 /* `build_class_init_list' does not recognize
3763 if (code
== UNION_TYPE
&& any_default_members
!= 0)
3764 cp_error_at ("multiple fields in union `%T' initialized");
3765 any_default_members
= 1;
3770 /* If this type has any constant members which did not come
3771 with their own initialization, mark that fact here. It is
3772 not an error here, since such types can be saved either by their
3773 constructors, or by fortuitous initialization. */
3774 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
) = const_sans_init
;
3775 CLASSTYPE_REF_FIELDS_NEED_INIT (t
) = ref_sans_init
;
3776 CLASSTYPE_ABSTRACT_VIRTUALS (t
) = abstract_virtuals
;
3778 /* Effective C++ rule 11. */
3779 if (has_pointers
&& warn_ecpp
&& TYPE_HAS_CONSTRUCTOR (t
)
3780 && ! (TYPE_HAS_INIT_REF (t
) && TYPE_HAS_ASSIGN_REF (t
)))
3782 cp_warning ("`%#T' has pointer data members", t
);
3784 if (! TYPE_HAS_INIT_REF (t
))
3786 cp_warning (" but does not override `%T(const %T&)'", t
, t
);
3787 if (! TYPE_HAS_ASSIGN_REF (t
))
3788 cp_warning (" or `operator=(const %T&)'", t
);
3790 else if (! TYPE_HAS_ASSIGN_REF (t
))
3791 cp_warning (" but does not override `operator=(const %T&)'", t
);
3794 /* Do some bookkeeping that will guide the generation of implicitly
3795 declared member functions. */
3796 TYPE_HAS_COMPLEX_INIT_REF (t
)
3797 |= (TYPE_HAS_INIT_REF (t
) || TYPE_USES_VIRTUAL_BASECLASSES (t
)
3798 || has_virtual
|| any_default_members
);
3799 TYPE_NEEDS_CONSTRUCTING (t
)
3800 |= (TYPE_HAS_CONSTRUCTOR (t
) || TYPE_USES_VIRTUAL_BASECLASSES (t
)
3801 || has_virtual
|| any_default_members
);
3802 if (! IS_SIGNATURE (t
))
3803 CLASSTYPE_NON_AGGREGATE (t
)
3804 = ! aggregate
|| has_virtual
|| TYPE_HAS_CONSTRUCTOR (t
);
3805 TYPE_HAS_REAL_ASSIGNMENT (t
) |= TYPE_HAS_ASSIGNMENT (t
);
3806 TYPE_HAS_REAL_ASSIGN_REF (t
) |= TYPE_HAS_ASSIGN_REF (t
);
3807 TYPE_HAS_COMPLEX_ASSIGN_REF (t
)
3808 |= TYPE_HAS_ASSIGN_REF (t
) || TYPE_USES_VIRTUAL_BASECLASSES (t
);
3810 /* Synthesize any needed methods. Note that methods will be synthesized
3811 for anonymous unions; grok_x_components undoes that. */
3813 = add_implicitly_declared_members (t
, cant_have_default_ctor
,
3814 cant_have_const_ctor
,
3817 add_virtual_function (&pending_virtuals
, &pending_hard_virtuals
,
3818 &has_virtual
, virtual_dtor
, t
);
3820 if (TYPE_METHODS (t
))
3822 finish_struct_methods (t
);
3823 method_vec
= CLASSTYPE_METHOD_VEC (t
);
3829 /* Just in case these got accidentally
3830 filled in by syntax errors. */
3831 TYPE_HAS_CONSTRUCTOR (t
) = 0;
3832 TYPE_HAS_DESTRUCTOR (t
) = 0;
3835 for (access_decls
= nreverse (access_decls
); access_decls
;
3836 access_decls
= TREE_CHAIN (access_decls
))
3837 handle_using_decl (TREE_VALUE (access_decls
), t
, method_vec
, fields
);
3839 if (vfield
== NULL_TREE
&& has_virtual
)
3841 /* We build this decl with vtbl_ptr_type_node, which is a
3842 `vtable_entry_type*'. It might seem more precise to use
3843 `vtable_entry_type (*)[N]' where N is the number of firtual
3844 functions. However, that would require the vtable pointer in
3845 base classes to have a different type than the vtable pointer
3846 in derived classes. We could make that happen, but that
3847 still wouldn't solve all the problems. In particular, the
3848 type-based alias analysis code would decide that assignments
3849 to the base class vtable pointer can't alias assignments to
3850 the derived class vtable pointer, since they have different
3851 types. Thus, in an derived class destructor, where the base
3852 class constructor was inlined, we could generate bad code for
3853 setting up the vtable pointer.
3855 Therefore, we use one type for all vtable pointers. We still
3856 use a type-correct type; it's just doesn't indicate the array
3857 bounds. That's better than using `void*' or some such; it's
3858 cleaner, and it let's the alias analysis code know that these
3859 stores cannot alias stores to void*! */
3860 vfield
= build_lang_field_decl (FIELD_DECL
, get_vfield_name (t
),
3861 vtbl_ptr_type_node
);
3862 /* If you change any of the below, take a look at all the
3863 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
3865 DECL_ASSEMBLER_NAME (vfield
) = get_identifier (VFIELD_BASE
);
3866 CLASSTYPE_VFIELD (t
) = vfield
;
3867 DECL_VIRTUAL_P (vfield
) = 1;
3868 DECL_ARTIFICIAL (vfield
) = 1;
3869 DECL_FIELD_CONTEXT (vfield
) = t
;
3870 DECL_CLASS_CONTEXT (vfield
) = t
;
3871 DECL_FCONTEXT (vfield
) = t
;
3872 DECL_SAVED_INSNS (vfield
) = NULL_RTX
;
3873 DECL_FIELD_SIZE (vfield
) = 0;
3874 DECL_ALIGN (vfield
) = TYPE_ALIGN (ptr_type_node
);
3876 /* This is more efficient, but breaks binary compatibility, turn
3877 it on sometime when we don't care. If we turn it on, we also
3878 have to enable the code in dfs_init_vbase_pointers. */
3879 /* vfield is always first entry in structure. */
3880 TREE_CHAIN (vfield
) = fields
;
3885 my_friendly_assert (TREE_CHAIN (last_x
) == NULL_TREE
, 175);
3886 TREE_CHAIN (last_x
) = vfield
;
3893 vfields
= chainon (vfields
, CLASSTYPE_AS_LIST (t
));
3896 /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
3898 C++: maybe we will support default field initialization some day... */
3900 /* Delete all duplicate fields from the fields */
3901 delete_duplicate_fields (fields
);
3903 /* Catch function/field name conflict. We don't need to do this for a
3904 signature, since it can only contain the fields constructed in
3905 append_signature_fields. */
3906 if (! IS_SIGNATURE (t
))
3908 int n_methods
= method_vec
? TREE_VEC_LENGTH (method_vec
) : 0;
3909 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
3911 tree name
= DECL_NAME (x
);
3914 if (TREE_CODE (x
) == TYPE_DECL
&& DECL_ARTIFICIAL (x
))
3917 for (i
= 2; i
< n_methods
&& TREE_VEC_ELT (method_vec
, i
); ++i
)
3918 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec
, i
)))
3921 cp_error_at ("data member `%#D' conflicts with", x
);
3922 cp_error_at ("function member `%#D'",
3923 OVL_CURRENT (TREE_VEC_ELT (method_vec
, i
)));
3929 /* Now we have the nearly final fieldlist for the data fields. Record it,
3930 then lay out the structure or union (including the fields). */
3932 TYPE_FIELDS (t
) = fields
;
3936 last_x
= build_base_fields (t
);
3938 /* If all our bases are empty, we can be empty too. */
3939 for (x
= last_x
; empty
&& x
; x
= TREE_CHAIN (x
))
3940 if (DECL_SIZE (x
) != integer_zero_node
)
3945 /* C++: do not let empty structures exist. */
3946 tree decl
= build_lang_field_decl
3947 (FIELD_DECL
, NULL_TREE
, char_type_node
);
3948 TREE_CHAIN (decl
) = fields
;
3949 TYPE_FIELDS (t
) = decl
;
3952 TYPE_FIELDS (t
) = chainon (last_x
, TYPE_FIELDS (t
));
3956 /* Remember the size and alignment of the class before adding
3957 the virtual bases. */
3958 if (empty
&& flag_new_abi
)
3959 CLASSTYPE_SIZE (t
) = integer_zero_node
;
3960 else if (flag_new_abi
&& TYPE_HAS_COMPLEX_INIT_REF (t
)
3961 && TYPE_HAS_COMPLEX_ASSIGN_REF (t
))
3962 CLASSTYPE_SIZE (t
) = TYPE_BINFO_SIZE (t
);
3964 CLASSTYPE_SIZE (t
) = TYPE_SIZE (t
);
3965 CLASSTYPE_ALIGN (t
) = TYPE_ALIGN (t
);
3967 finish_struct_anon (t
);
3969 /* Set the TYPE_DECL for this type to contain the right
3970 value for DECL_OFFSET, so that we can use it as part
3971 of a COMPONENT_REF for multiple inheritance. */
3973 layout_decl (TYPE_MAIN_DECL (t
), 0);
3975 /* Now fix up any virtual base class types that we left lying
3976 around. We must get these done before we try to lay out the
3977 virtual function table. */
3978 pending_hard_virtuals
= nreverse (pending_hard_virtuals
);
3981 /* layout_basetypes will remove the base subobject fields. */
3982 max_has_virtual
= layout_basetypes (t
, max_has_virtual
);
3984 TYPE_FIELDS (t
) = fields
;
3986 my_friendly_assert (TYPE_FIELDS (t
) == fields
, 981117);
3988 /* Delete all zero-width bit-fields from the front of the fieldlist */
3989 while (fields
&& DECL_C_BIT_FIELD (fields
)
3990 && DECL_INITIAL (fields
))
3991 fields
= TREE_CHAIN (fields
);
3992 /* Delete all such fields from the rest of the fields. */
3993 for (x
= fields
; x
;)
3995 if (TREE_CHAIN (x
) && DECL_C_BIT_FIELD (TREE_CHAIN (x
))
3996 && DECL_INITIAL (TREE_CHAIN (x
)))
3997 TREE_CHAIN (x
) = TREE_CHAIN (TREE_CHAIN (x
));
4001 TYPE_FIELDS (t
) = fields
;
4003 if (TYPE_USES_VIRTUAL_BASECLASSES (t
))
4007 vbases
= CLASSTYPE_VBASECLASSES (t
);
4008 CLASSTYPE_N_VBASECLASSES (t
) = list_length (vbases
);
4011 /* Now fixup overrides of all functions in vtables from all
4012 direct or indirect virtual base classes. */
4013 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
4014 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
4016 for (i
= 0; i
< n_baseclasses
; i
++)
4018 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
4019 tree basetype
= BINFO_TYPE (base_binfo
);
4022 vbases
= CLASSTYPE_VBASECLASSES (basetype
);
4025 merge_overrides (binfo_member (BINFO_TYPE (vbases
),
4026 CLASSTYPE_VBASECLASSES (t
)),
4028 vbases
= TREE_CHAIN (vbases
);
4034 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
4035 might need to know it for setting up the offsets in the vtable
4036 (or in thunks) below. */
4037 if (vfield
!= NULL_TREE
4038 && DECL_FIELD_CONTEXT (vfield
) != t
)
4040 tree binfo
= get_binfo (DECL_FIELD_CONTEXT (vfield
), t
, 0);
4041 tree offset
= BINFO_OFFSET (binfo
);
4043 vfield
= copy_node (vfield
);
4044 copy_lang_decl (vfield
);
4046 if (! integer_zerop (offset
))
4047 offset
= size_binop (MULT_EXPR
, offset
, size_int (BITS_PER_UNIT
));
4048 DECL_FIELD_CONTEXT (vfield
) = t
;
4049 DECL_CLASS_CONTEXT (vfield
) = t
;
4050 DECL_FIELD_BITPOS (vfield
)
4051 = size_binop (PLUS_EXPR
, offset
, DECL_FIELD_BITPOS (vfield
));
4052 CLASSTYPE_VFIELD (t
) = vfield
;
4056 cp_warning ("Doing hard virtuals for %T...", t
);
4059 if (has_virtual
> max_has_virtual
)
4060 max_has_virtual
= has_virtual
;
4061 if (max_has_virtual
> 0)
4062 TYPE_VIRTUAL_P (t
) = 1;
4064 if (flag_rtti
&& TYPE_VIRTUAL_P (t
) && !pending_hard_virtuals
)
4065 modify_all_vtables (t
, NULL_TREE
, NULL_TREE
);
4067 while (pending_hard_virtuals
)
4069 modify_all_vtables (t
,
4070 TREE_PURPOSE (pending_hard_virtuals
),
4071 TREE_VALUE (pending_hard_virtuals
));
4072 pending_hard_virtuals
= TREE_CHAIN (pending_hard_virtuals
);
4075 if (TYPE_USES_VIRTUAL_BASECLASSES (t
))
4078 /* Now fixup any virtual function entries from virtual bases
4079 that have different deltas. This has to come after we do the
4080 pending hard virtuals, as we might have a function that comes
4081 from multiple virtual base instances that is only overridden
4082 by a hard virtual above. */
4083 vbases
= CLASSTYPE_VBASECLASSES (t
);
4086 /* We might be able to shorten the amount of work we do by
4087 only doing this for vtables that come from virtual bases
4088 that have differing offsets, but don't want to miss any
4090 fixup_vtable_deltas (vbases
, 1, t
);
4091 vbases
= TREE_CHAIN (vbases
);
4095 /* Under our model of GC, every C++ class gets its own virtual
4096 function table, at least virtually. */
4097 if (pending_virtuals
)
4099 pending_virtuals
= nreverse (pending_virtuals
);
4100 /* We must enter these virtuals into the table. */
4101 if (first_vfn_base_index
< 0)
4103 /* The second slot is for the tdesc pointer when thunks are used. */
4104 if (flag_vtable_thunks
)
4105 pending_virtuals
= tree_cons (NULL_TREE
, NULL_TREE
, pending_virtuals
);
4107 /* The first slot is for the rtti offset. */
4108 pending_virtuals
= tree_cons (NULL_TREE
, NULL_TREE
, pending_virtuals
);
4110 set_rtti_entry (pending_virtuals
,
4111 convert (ssizetype
, integer_zero_node
), t
);
4112 build_vtable (NULL_TREE
, t
);
4116 /* Here we know enough to change the type of our virtual
4117 function table, but we will wait until later this function. */
4119 if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t
)))
4120 build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t
), first_vfn_base_index
), t
);
4123 /* If this type has basetypes with constructors, then those
4124 constructors might clobber the virtual function table. But
4125 they don't if the derived class shares the exact vtable of the base
4128 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t
) = 1;
4130 else if (first_vfn_base_index
>= 0)
4132 tree binfo
= TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t
), first_vfn_base_index
);
4133 /* This class contributes nothing new to the virtual function
4134 table. However, it may have declared functions which
4135 went into the virtual function table "inherited" from the
4136 base class. If so, we grab a copy of those updated functions,
4137 and pretend they are ours. */
4139 /* See if we should steal the virtual info from base class. */
4140 if (TYPE_BINFO_VTABLE (t
) == NULL_TREE
)
4141 TYPE_BINFO_VTABLE (t
) = BINFO_VTABLE (binfo
);
4142 if (TYPE_BINFO_VIRTUALS (t
) == NULL_TREE
)
4143 TYPE_BINFO_VIRTUALS (t
) = BINFO_VIRTUALS (binfo
);
4144 if (TYPE_BINFO_VTABLE (t
) != BINFO_VTABLE (binfo
))
4145 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t
) = 1;
4148 if (max_has_virtual
|| first_vfn_base_index
>= 0)
4150 CLASSTYPE_VSIZE (t
) = has_virtual
;
4151 if (first_vfn_base_index
>= 0)
4153 if (pending_virtuals
)
4154 TYPE_BINFO_VIRTUALS (t
) = chainon (TYPE_BINFO_VIRTUALS (t
),
4157 else if (has_virtual
)
4159 TYPE_BINFO_VIRTUALS (t
) = pending_virtuals
;
4160 DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t
)) = 1;
4164 /* Now lay out the virtual function table. */
4167 /* Use size_int so values are memoized in common cases. */
4168 tree itype
= build_index_type (size_int (has_virtual
));
4169 tree atype
= build_cplus_array_type (vtable_entry_type
, itype
);
4171 layout_type (atype
);
4173 CLASSTYPE_VFIELD (t
) = vfield
;
4175 /* We may have to grow the vtable. */
4176 if (TREE_TYPE (TYPE_BINFO_VTABLE (t
)) != atype
)
4178 TREE_TYPE (TYPE_BINFO_VTABLE (t
)) = atype
;
4179 DECL_SIZE (TYPE_BINFO_VTABLE (t
)) = 0;
4180 layout_decl (TYPE_BINFO_VTABLE (t
), 0);
4181 /* At one time the vtable info was grabbed 2 words at a time. This
4182 fails on sparc unless you have 8-byte alignment. (tiemann) */
4183 DECL_ALIGN (TYPE_BINFO_VTABLE (t
))
4184 = MAX (TYPE_ALIGN (double_type_node
),
4185 DECL_ALIGN (TYPE_BINFO_VTABLE (t
)));
4188 else if (first_vfn_base_index
>= 0)
4189 CLASSTYPE_VFIELD (t
) = vfield
;
4190 CLASSTYPE_VFIELDS (t
) = vfields
;
4192 finish_struct_bits (t
, max_has_virtual
);
4194 /* Complete the rtl for any static member objects of the type we're
4196 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
4198 if (TREE_CODE (x
) == VAR_DECL
&& TREE_STATIC (x
)
4199 && TREE_TYPE (x
) == t
)
4201 DECL_MODE (x
) = TYPE_MODE (t
);
4202 make_decl_rtl (x
, NULL
, 0);
4206 if (TYPE_HAS_CONSTRUCTOR (t
))
4208 tree vfields
= CLASSTYPE_VFIELDS (t
);
4212 /* Mark the fact that constructor for T
4213 could affect anybody inheriting from T
4214 who wants to initialize vtables for VFIELDS's type. */
4215 if (VF_DERIVED_VALUE (vfields
))
4216 TREE_ADDRESSABLE (vfields
) = 1;
4217 vfields
= TREE_CHAIN (vfields
);
4219 if (any_default_members
!= 0)
4220 build_class_init_list (t
);
4222 else if (TYPE_NEEDS_CONSTRUCTING (t
))
4223 build_class_init_list (t
);
4225 /* Write out inline function definitions. */
4226 do_inline_function_hair (t
, CLASSTYPE_INLINE_FRIENDS (t
));
4227 CLASSTYPE_INLINE_FRIENDS (t
) = 0;
4229 if (CLASSTYPE_VSIZE (t
) != 0)
4232 /* This is now done above. */
4233 if (DECL_FIELD_CONTEXT (vfield
) != t
)
4235 tree binfo
= get_binfo (DECL_FIELD_CONTEXT (vfield
), t
, 0);
4236 tree offset
= BINFO_OFFSET (binfo
);
4238 vfield
= copy_node (vfield
);
4239 copy_lang_decl (vfield
);
4241 if (! integer_zerop (offset
))
4242 offset
= size_binop (MULT_EXPR
, offset
, size_int (BITS_PER_UNIT
));
4243 DECL_FIELD_CONTEXT (vfield
) = t
;
4244 DECL_CLASS_CONTEXT (vfield
) = t
;
4245 DECL_FIELD_BITPOS (vfield
)
4246 = size_binop (PLUS_EXPR
, offset
, DECL_FIELD_BITPOS (vfield
));
4247 CLASSTYPE_VFIELD (t
) = vfield
;
4251 /* In addition to this one, all the other vfields should be listed. */
4252 /* Before that can be done, we have to have FIELD_DECLs for them, and
4253 a place to find them. */
4254 TYPE_NONCOPIED_PARTS (t
) = build_tree_list (default_conversion (TYPE_BINFO_VTABLE (t
)), vfield
);
4256 if (warn_nonvdtor
&& TYPE_HAS_DESTRUCTOR (t
)
4257 && DECL_VINDEX (TREE_VEC_ELT (method_vec
, 1)) == NULL_TREE
)
4258 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
4262 /* Make the rtl for any new vtables we have created, and unmark
4263 the base types we marked. */
4264 finish_vtbls (TYPE_BINFO (t
), 1, t
);
4265 hack_incomplete_structures (t
);
4268 if (TYPE_NAME (t
) && TYPE_IDENTIFIER (t
))
4269 undo_template_name_overload (TYPE_IDENTIFIER (t
), 1);
4272 resume_momentary (old
);
4274 if (warn_overloaded_virtual
)
4278 /* This has to be done after we have sorted out what to do with
4279 the enclosing type. */
4280 if (write_symbols
!= DWARF_DEBUG
)
4282 /* Be smarter about nested classes here. If a type is nested,
4283 only output it if we would output the enclosing type. */
4284 if (DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (t
)))
4285 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = TREE_ASM_WRITTEN (TYPE_MAIN_DECL (t
));
4289 if (write_symbols
!= DWARF_DEBUG
&& write_symbols
!= DWARF2_DEBUG
)
4291 /* If the type has methods, we want to think about cutting down
4292 the amount of symbol table stuff we output. The value stored in
4293 the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
4294 For example, if a member function is seen and we decide to
4295 write out that member function, then we can change the value
4296 of the DECL_IGNORED_P slot, and the type will be output when
4297 that member function's debug info is written out.
4299 We can't do this with DWARF, which does not support name
4300 references between translation units. */
4301 if (CLASSTYPE_METHOD_VEC (t
))
4303 /* Don't output full info about any type
4304 which does not have its implementation defined here. */
4305 if (CLASSTYPE_INTERFACE_ONLY (t
))
4306 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 1;
4308 /* XXX do something about this. */
4309 else if (CLASSTYPE_INTERFACE_UNKNOWN (t
))
4310 /* Only a first approximation! */
4311 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 1;
4314 else if (CLASSTYPE_INTERFACE_ONLY (t
))
4315 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 1;
4318 /* Finish debugging output for this type. */
4319 rest_of_type_compilation (t
, toplevel_bindings_p ());
4324 /* In [basic.scope.class] we have:
4326 A name N used in a class S shall refer to the same declaration in
4327 its context and when re-evaluated in the completed scope of S.
4329 This function checks this condition for X, which is a member of
4333 check_member_decl_is_same_in_complete_scope (t
, x
)
4337 /* A name N used in a class S shall refer to the same declaration in
4338 its context and when re-evaluated in the completed scope of S.
4340 Enums, types and static vars have already been checked. */
4341 if (TREE_CODE (x
) != USING_DECL
4342 && TREE_CODE (x
) != TYPE_DECL
&& !DECL_CLASS_TEMPLATE_P (x
)
4343 && TREE_CODE (x
) != CONST_DECL
&& TREE_CODE (x
) != VAR_DECL
)
4345 tree name
= DECL_NAME (x
);
4348 /* Don't get confused by access decls. */
4349 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
)
4350 icv
= IDENTIFIER_CLASS_VALUE (name
);
4355 /* Don't complain about constructors. */
4356 && name
!= constructor_name (current_class_type
)
4357 /* Or inherited names. */
4358 && id_in_current_class (name
)
4359 /* Or shadowed tags. */
4360 && !(TREE_CODE (icv
) == TYPE_DECL
&& DECL_CONTEXT (icv
) == t
))
4362 cp_pedwarn_at ("declaration of identifier `%D' as `%+#D'",
4364 cp_pedwarn_at ("conflicts with other use in class as `%#D'",
4370 /* When T was built up, the member declarations were added in reverse
4371 order. Rearrange them to declaration order. */
4374 unreverse_member_declarations (t
)
4381 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
4382 reverse order. Put them in declaration order now. */
4383 TYPE_METHODS (t
) = nreverse (TYPE_METHODS (t
));
4384 CLASSTYPE_TAGS (t
) = nreverse (CLASSTYPE_TAGS (t
));
4386 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
4387 reverse order, so we can't just use nreverse. */
4389 for (x
= TYPE_FIELDS (t
);
4390 x
&& TREE_CODE (x
) != TYPE_DECL
;
4393 next
= TREE_CHAIN (x
);
4394 TREE_CHAIN (x
) = prev
;
4399 TREE_CHAIN (TYPE_FIELDS (t
)) = x
;
4401 TYPE_FIELDS (t
) = prev
;
4406 finish_struct (t
, attributes
, warn_anon
)
4410 tree name
= TYPE_NAME (t
);
4413 if (TREE_CODE (name
) == TYPE_DECL
)
4417 DECL_SOURCE_FILE (name
) = input_filename
;
4418 /* For TYPE_DECL that are not typedefs (those marked with a line
4419 number of zero, we don't want to mark them as real typedefs.
4420 If this fails one needs to make sure real typedefs have a
4421 previous line number, even if it is wrong, that way the below
4422 will fill in the right line number. (mrs) */
4423 if (DECL_SOURCE_LINE (name
))
4424 DECL_SOURCE_LINE (name
) = lineno
;
4425 name
= DECL_NAME (name
);
4428 /* Append the fields we need for constructing signature tables. */
4429 if (IS_SIGNATURE (t
))
4430 append_signature_fields (t
);
4432 /* Now that we've got all the field declarations, reverse everything
4434 unreverse_member_declarations (t
);
4436 if (flag_optional_diags
)
4438 for (x
= TYPE_METHODS (t
); x
; x
= TREE_CHAIN (x
))
4439 check_member_decl_is_same_in_complete_scope (t
, x
);
4440 for (x
= TYPE_FIELDS (t
); x
; x
= TREE_CHAIN (x
))
4441 check_member_decl_is_same_in_complete_scope (t
, x
);
4444 /* Mark all the tags in the class as class-local. */
4445 for (x
= CLASSTYPE_TAGS (t
); x
; x
= TREE_CHAIN (x
))
4446 TREE_NONLOCAL_FLAG (TREE_VALUE (x
)) = 0;
4448 cplus_decl_attributes (t
, attributes
, NULL_TREE
);
4450 if (processing_template_decl
)
4452 tree d
= getdecls ();
4453 for (; d
; d
= TREE_CHAIN (d
))
4455 /* If this is the decl for the class or one of the template
4456 parms, we've seen all the injected decls. */
4457 if ((TREE_CODE (d
) == TYPE_DECL
4458 && (TREE_TYPE (d
) == t
4459 || TREE_CODE (TREE_TYPE (d
)) == TEMPLATE_TYPE_PARM
4460 || TREE_CODE (TREE_TYPE (d
)) == TEMPLATE_TEMPLATE_PARM
))
4461 || TREE_CODE (d
) == CONST_DECL
)
4463 /* Don't inject cache decls. */
4464 else if (IDENTIFIER_TEMPLATE (DECL_NAME (d
)))
4466 DECL_TEMPLATE_INJECT (CLASSTYPE_TI_TEMPLATE (t
))
4467 = tree_cons (NULL_TREE
, d
,
4468 DECL_TEMPLATE_INJECT (CLASSTYPE_TI_TEMPLATE (t
)));
4470 finish_struct_methods (t
);
4471 TYPE_SIZE (t
) = integer_zero_node
;
4474 t
= finish_struct_1 (t
, warn_anon
);
4476 TYPE_BEING_DEFINED (t
) = 0;
4477 if (current_class_type
)
4480 error ("trying to finish struct, but kicked out due to previous parse errors.");
4485 /* Return the dynamic type of INSTANCE, if known.
4486 Used to determine whether the virtual function table is needed
4489 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4490 of our knowledge of its type. */
4493 fixed_type_or_null (instance
, nonnull
)
4497 switch (TREE_CODE (instance
))
4500 /* Check that we are not going through a cast of some sort. */
4501 if (TREE_TYPE (instance
)
4502 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance
, 0))))
4503 instance
= TREE_OPERAND (instance
, 0);
4504 /* fall through... */
4506 /* This is a call to a constructor, hence it's never zero. */
4507 if (TREE_HAS_CONSTRUCTOR (instance
))
4511 return TREE_TYPE (instance
);
4516 /* This is a call to a constructor, hence it's never zero. */
4517 if (TREE_HAS_CONSTRUCTOR (instance
))
4521 return TREE_TYPE (instance
);
4523 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
4530 if (TREE_CODE (TREE_OPERAND (instance
, 1)) == INTEGER_CST
)
4531 /* Propagate nonnull. */
4532 fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
4533 if (TREE_CODE (TREE_OPERAND (instance
, 0)) == ADDR_EXPR
)
4534 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
4539 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
4544 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
4547 return fixed_type_or_null (TREE_OPERAND (instance
, 1), nonnull
);
4551 if (TREE_CODE (TREE_TYPE (instance
)) == ARRAY_TYPE
4552 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance
))))
4556 return TREE_TYPE (TREE_TYPE (instance
));
4558 /* fall through... */
4561 if (IS_AGGR_TYPE (TREE_TYPE (instance
)))
4565 return TREE_TYPE (instance
);
4569 if (instance
== current_class_ptr
4570 && flag_this_is_variable
<= 0)
4572 /* Normally, 'this' must be non-null. */
4573 if (flag_this_is_variable
== 0)
4576 /* <0 means we're in a constructor and we know our type. */
4577 if (flag_this_is_variable
< 0)
4578 return TREE_TYPE (TREE_TYPE (instance
));
4580 else if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
4581 /* Reference variables should be references to objects. */
4591 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
4592 to the static type. We also handle the case where INSTANCE is really
4595 Used to determine whether the virtual function table is needed
4598 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4599 of our knowledge of its type. */
4602 resolves_to_fixed_type_p (instance
, nonnull
)
4606 tree t
= TREE_TYPE (instance
);
4607 tree fixed
= fixed_type_or_null (instance
, nonnull
);
4608 if (fixed
== NULL_TREE
)
4610 if (POINTER_TYPE_P (t
))
4612 return same_type_p (TYPE_MAIN_VARIANT (t
), TYPE_MAIN_VARIANT (fixed
));
4617 init_class_processing ()
4619 current_class_depth
= 0;
4620 current_class_stack_size
= 10;
4622 = (class_stack_node_t
) xmalloc (current_class_stack_size
4623 * sizeof (struct class_stack_node
));
4625 current_lang_stacksize
= 10;
4626 current_lang_base
= (tree
*)xmalloc(current_lang_stacksize
* sizeof (tree
));
4627 current_lang_stack
= current_lang_base
;
4629 access_default_node
= build_int_2 (0, 0);
4630 access_public_node
= build_int_2 (1, 0);
4631 access_protected_node
= build_int_2 (2, 0);
4632 access_private_node
= build_int_2 (3, 0);
4633 access_default_virtual_node
= build_int_2 (4, 0);
4634 access_public_virtual_node
= build_int_2 (5, 0);
4635 access_protected_virtual_node
= build_int_2 (6, 0);
4636 access_private_virtual_node
= build_int_2 (7, 0);
4638 /* Keep these values lying around. */
4639 base_layout_decl
= build_lang_field_decl (FIELD_DECL
, NULL_TREE
, error_mark_node
);
4640 TREE_TYPE (base_layout_decl
) = make_node (RECORD_TYPE
);
4642 gcc_obstack_init (&class_obstack
);
4645 /* Set current scope to NAME. CODE tells us if this is a
4646 STRUCT, UNION, or ENUM environment.
4648 NAME may end up being NULL_TREE if this is an anonymous or
4649 late-bound struct (as in "struct { ... } foo;") */
4651 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
4652 appropriate values, found by looking up the type definition of
4655 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
4656 which can be seen locally to the class. They are shadowed by
4657 any subsequent local declaration (including parameter names).
4659 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
4660 which have static meaning (i.e., static members, static
4661 member functions, enum declarations, etc).
4663 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
4664 which can be seen locally to the class (as in 1), but
4665 know that we are doing this for declaration purposes
4666 (i.e. friend foo::bar (int)).
4668 So that we may avoid calls to lookup_name, we cache the _TYPE
4669 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
4671 For multiple inheritance, we perform a two-pass depth-first search
4672 of the type lattice. The first pass performs a pre-order search,
4673 marking types after the type has had its fields installed in
4674 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
4675 unmarks the marked types. If a field or member function name
4676 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
4677 that name becomes `error_mark_node'. */
4680 pushclass (type
, modify
)
4684 type
= TYPE_MAIN_VARIANT (type
);
4686 /* Make sure there is enough room for the new entry on the stack. */
4687 if (current_class_depth
+ 1 >= current_class_stack_size
)
4689 current_class_stack_size
*= 2;
4691 = (class_stack_node_t
) xrealloc (current_class_stack
,
4692 current_class_stack_size
4693 * sizeof (struct class_stack_node
));
4696 /* Insert a new entry on the class stack. */
4697 current_class_stack
[current_class_depth
].name
= current_class_name
;
4698 current_class_stack
[current_class_depth
].type
= current_class_type
;
4699 current_class_stack
[current_class_depth
].access
= current_access_specifier
;
4700 current_class_depth
++;
4702 /* Now set up the new type. */
4703 current_class_name
= TYPE_NAME (type
);
4704 if (TREE_CODE (current_class_name
) == TYPE_DECL
)
4705 current_class_name
= DECL_NAME (current_class_name
);
4706 current_class_type
= type
;
4708 /* By default, things in classes are private, while things in
4709 structures or unions are public. */
4710 current_access_specifier
= (CLASSTYPE_DECLARED_CLASS (type
)
4711 ? access_private_node
4712 : access_public_node
);
4714 if (previous_class_type
!= NULL_TREE
4715 && (type
!= previous_class_type
|| TYPE_SIZE (previous_class_type
) == NULL_TREE
)
4716 && current_class_depth
== 1)
4718 /* Forcibly remove any old class remnants. */
4720 previous_class_type
= NULL_TREE
;
4722 /* Now, free the obstack on which we cached all the values. */
4723 obstack_free (&class_cache_obstack
, class_cache_firstobj
);
4724 class_cache_firstobj
4725 = (char*) obstack_finish (&class_cache_obstack
);
4731 if (CLASSTYPE_TEMPLATE_INFO (type
))
4732 overload_template_name (type
);
4738 tree this_fndecl
= current_function_decl
;
4740 if (current_function_decl
4741 && DECL_CONTEXT (current_function_decl
)
4742 && TREE_CODE (DECL_CONTEXT (current_function_decl
)) == FUNCTION_DECL
)
4743 current_function_decl
= DECL_CONTEXT (current_function_decl
);
4745 current_function_decl
= NULL_TREE
;
4747 if (type
!= previous_class_type
|| current_class_depth
> 1)
4750 build_mi_matrix (type
);
4751 push_class_decls (type
);
4754 push_class_decls (type
);
4761 /* Hooray, we successfully cached; let's just install the
4762 cached class_shadowed list, and walk through it to get the
4763 IDENTIFIER_TYPE_VALUEs correct. */
4764 set_class_shadows (previous_class_values
);
4765 for (item
= previous_class_values
; item
; item
= TREE_CHAIN (item
))
4767 tree id
= TREE_PURPOSE (item
);
4768 tree decl
= IDENTIFIER_CLASS_VALUE (id
);
4770 if (TREE_CODE (decl
) == TYPE_DECL
)
4771 set_identifier_type_value (id
, TREE_TYPE (decl
));
4773 unuse_fields (type
);
4776 for (tags
= CLASSTYPE_TAGS (type
); tags
; tags
= TREE_CHAIN (tags
))
4778 tree tag_type
= TREE_VALUE (tags
);
4780 TREE_NONLOCAL_FLAG (tag_type
) = 1;
4781 if (! TREE_PURPOSE (tags
))
4783 if (! (IS_AGGR_TYPE_CODE (TREE_CODE (tag_type
))
4784 && CLASSTYPE_IS_TEMPLATE (tag_type
)))
4785 pushtag (TREE_PURPOSE (tags
), tag_type
, 0);
4787 pushdecl_class_level (CLASSTYPE_TI_TEMPLATE (tag_type
));
4790 current_function_decl
= this_fndecl
;
4794 /* Get out of the current class scope. If we were in a class scope
4795 previously, that is the one popped to. The flag MODIFY tells whether
4796 the current scope declarations needs to be modified as a result of
4797 popping to the previous scope. 0 is used for class definitions. */
4805 /* Back this old class out completely. */
4806 tree tags
= CLASSTYPE_TAGS (previous_class_type
);
4809 /* This code can be seen as a cache miss. When we've cached a
4810 class' scope's bindings and we can't use them, we need to reset
4811 them. This is it! */
4812 for (t
= previous_class_values
; t
; t
= TREE_CHAIN (t
))
4813 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t
)) = NULL_TREE
;
4816 TREE_NONLOCAL_FLAG (TREE_VALUE (tags
)) = 0;
4817 tags
= TREE_CHAIN (tags
);
4825 /* Just remove from this class what didn't make
4826 it into IDENTIFIER_CLASS_VALUE. */
4827 tree tags
= CLASSTYPE_TAGS (current_class_type
);
4831 TREE_NONLOCAL_FLAG (TREE_VALUE (tags
)) = 0;
4832 tags
= TREE_CHAIN (tags
);
4836 /* Force clearing of IDENTIFIER_CLASS_VALUEs after a class definition,
4837 since not all class decls make it there currently. */
4838 poplevel_class (! modify
);
4840 /* Since poplevel_class does the popping of class decls nowadays,
4841 this really only frees the obstack used for these decls.
4842 That's why it had to be moved down here. */
4846 current_class_depth
--;
4847 current_class_name
= current_class_stack
[current_class_depth
].name
;
4848 current_class_type
= current_class_stack
[current_class_depth
].type
;
4849 current_access_specifier
= current_class_stack
[current_class_depth
].access
;
4852 /* Returns 1 if current_class_type is either T or a nested type of T. */
4855 currently_open_class (t
)
4859 if (t
== current_class_type
)
4861 for (i
= 0; i
< current_class_depth
; ++i
)
4862 if (current_class_stack
[i
].type
== t
)
4867 /* When entering a class scope, all enclosing class scopes' names with
4868 static meaning (static variables, static functions, types and enumerators)
4869 have to be visible. This recursive function calls pushclass for all
4870 enclosing class contexts until global or a local scope is reached.
4871 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
4872 formal of the same name. */
4875 push_nested_class (type
, modify
)
4881 my_friendly_assert (!type
|| TREE_CODE (type
) != NAMESPACE_DECL
, 980711);
4883 if (type
== NULL_TREE
|| type
== error_mark_node
|| ! IS_AGGR_TYPE (type
)
4884 || TREE_CODE (type
) == TEMPLATE_TYPE_PARM
4885 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
4888 context
= DECL_CONTEXT (TYPE_MAIN_DECL (type
));
4890 if (context
&& TREE_CODE (context
) == RECORD_TYPE
)
4891 push_nested_class (context
, 2);
4892 pushclass (type
, modify
);
4895 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
4898 pop_nested_class (modify
)
4901 tree context
= DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type
));
4904 if (context
&& TREE_CODE (context
) == RECORD_TYPE
)
4905 pop_nested_class (modify
);
4908 /* Set global variables CURRENT_LANG_NAME to appropriate value
4909 so that behavior of name-mangling machinery is correct. */
4912 push_lang_context (name
)
4915 *current_lang_stack
++ = current_lang_name
;
4916 if (current_lang_stack
>= current_lang_base
+ current_lang_stacksize
)
4919 = (tree
*)xrealloc (current_lang_base
,
4920 sizeof (tree
) * (current_lang_stacksize
+ 10));
4921 current_lang_stack
= current_lang_base
+ current_lang_stacksize
;
4922 current_lang_stacksize
+= 10;
4925 if (name
== lang_name_cplusplus
|| name
== lang_name_java
)
4927 strict_prototype
= strict_prototypes_lang_cplusplus
;
4928 current_lang_name
= name
;
4930 else if (name
== lang_name_c
)
4932 strict_prototype
= strict_prototypes_lang_c
;
4933 current_lang_name
= name
;
4936 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name
));
4939 /* Get out of the current language scope. */
4944 current_lang_name
= *--current_lang_stack
;
4945 if (current_lang_name
== lang_name_cplusplus
4946 || current_lang_name
== lang_name_java
)
4947 strict_prototype
= strict_prototypes_lang_cplusplus
;
4948 else if (current_lang_name
== lang_name_c
)
4949 strict_prototype
= strict_prototypes_lang_c
;
4952 /* Type instantiation routines. */
4955 validate_lhs (lhstype
, complain
)
4959 if (TYPE_PTRMEMFUNC_P (lhstype
))
4960 lhstype
= TYPE_PTRMEMFUNC_FN_TYPE (lhstype
);
4962 if (TREE_CODE (lhstype
) == POINTER_TYPE
)
4964 if (TREE_CODE (TREE_TYPE (lhstype
)) == FUNCTION_TYPE
4965 || TREE_CODE (TREE_TYPE (lhstype
)) == METHOD_TYPE
)
4966 lhstype
= TREE_TYPE (lhstype
);
4970 error ("invalid type combination for overload");
4971 return error_mark_node
;
4977 /* This function will instantiate the type of the expression given in
4978 RHS to match the type of LHSTYPE. If errors exist, then return
4979 error_mark_node. If only complain is COMPLAIN is set. If we are
4980 not complaining, never modify rhs, as overload resolution wants to
4981 try many possible instantiations, in hopes that at least one will
4984 For non-recursive calls, LHSTYPE should be a function, pointer to
4985 function, or a pointer to member function. */
4988 instantiate_type (lhstype
, rhs
, complain
)
4992 tree explicit_targs
= NULL_TREE
;
4993 int template_only
= 0;
4995 if (TREE_CODE (lhstype
) == UNKNOWN_TYPE
)
4998 error ("not enough type information");
4999 return error_mark_node
;
5002 if (TREE_TYPE (rhs
) != NULL_TREE
&& ! (type_unknown_p (rhs
)))
5004 if (same_type_p (lhstype
, TREE_TYPE (rhs
)))
5007 cp_error ("argument of type `%T' does not match `%T'",
5008 TREE_TYPE (rhs
), lhstype
);
5009 return error_mark_node
;
5012 /* We don't overwrite rhs if it is an overloaded function.
5013 Copying it would destroy the tree link. */
5014 if (TREE_CODE (rhs
) != OVERLOAD
)
5015 rhs
= copy_node (rhs
);
5017 /* This should really only be used when attempting to distinguish
5018 what sort of a pointer to function we have. For now, any
5019 arithmetic operation which is not supported on pointers
5020 is rejected as an error. */
5022 switch (TREE_CODE (rhs
))
5029 my_friendly_abort (177);
5030 return error_mark_node
;
5037 new_rhs
= instantiate_type (build_pointer_type (lhstype
),
5038 TREE_OPERAND (rhs
, 0), complain
);
5039 if (new_rhs
== error_mark_node
)
5040 return error_mark_node
;
5042 TREE_TYPE (rhs
) = lhstype
;
5043 TREE_OPERAND (rhs
, 0) = new_rhs
;
5048 rhs
= copy_node (TREE_OPERAND (rhs
, 0));
5049 TREE_TYPE (rhs
) = unknown_type_node
;
5050 return instantiate_type (lhstype
, rhs
, complain
);
5054 tree field
= TREE_OPERAND (rhs
, 1);
5055 if (TREE_CODE (field
) == TREE_LIST
)
5057 tree function
= instantiate_type (lhstype
, field
, complain
);
5058 if (function
== error_mark_node
)
5059 return error_mark_node
;
5060 my_friendly_assert (TREE_CODE (function
) == FUNCTION_DECL
, 185);
5062 if (! DECL_STATIC_FUNCTION_P (function
))
5064 tree t
= TREE_TYPE (TREE_OPERAND (rhs
, 0));
5065 if (TYPE_MAIN_VARIANT (t
) == current_class_type
)
5066 t
= constructor_name (t
);
5068 cp_error ("object-dependent reference to `%D' can only be used in a call",
5070 cp_error (" to form a pointer to member function, say `&%T::%D'",
5071 t
, DECL_NAME (function
));
5074 mark_used (function
);
5078 /* I could not trigger this code. MvL */
5079 my_friendly_abort (980326);
5084 /* This can happen if we are forming a pointer-to-member for a
5086 rhs
= TREE_OPERAND (rhs
, 1);
5087 my_friendly_assert (TREE_CODE (rhs
) == TEMPLATE_ID_EXPR
, 0);
5091 case TEMPLATE_ID_EXPR
:
5093 explicit_targs
= TREE_OPERAND (rhs
, 1);
5095 rhs
= TREE_OPERAND (rhs
, 0);
5098 my_friendly_assert (TREE_CODE (rhs
) == OVERLOAD
, 980401);
5104 /* Check that the LHSTYPE and the RHS are reasonable. */
5105 lhstype
= validate_lhs (lhstype
, complain
);
5106 if (lhstype
== error_mark_node
)
5109 if (TREE_CODE (lhstype
) != FUNCTION_TYPE
5110 && TREE_CODE (lhstype
) != METHOD_TYPE
)
5113 cp_error("cannot resolve overloaded function `%D' "
5114 "based on non-function type `%T'",
5115 DECL_NAME (OVL_FUNCTION (rhs
)), lhstype
);
5116 return error_mark_node
;
5119 /* Look for an exact match, by searching through the
5120 overloaded functions. */
5122 /* If we're processing a template-id, only a template
5123 function can match, so we don't look through the
5124 overloaded functions. */
5126 else for (elems
= rhs
; elems
; elems
= OVL_CHAIN (elems
))
5128 elem
= OVL_FUNCTION (elems
);
5129 if (TREE_CODE (elem
) == FUNCTION_DECL
5130 && same_type_p (lhstype
, TREE_TYPE (elem
)))
5137 /* No overloaded function was an exact match. See if we can
5138 instantiate some template to match. */
5142 if (TREE_CODE (elems
) == TREE_LIST
)
5143 elems
= TREE_VALUE (rhs
);
5144 for (; elems
; elems
= OVL_NEXT (elems
))
5145 if (TREE_CODE (elem
= OVL_CURRENT (elems
)) == TEMPLATE_DECL
)
5147 int n
= DECL_NTPARMS (elem
);
5148 tree t
= make_scratch_vec (n
);
5150 i
= type_unification
5151 (DECL_INNERMOST_TEMPLATE_PARMS (elem
), t
,
5152 TYPE_ARG_TYPES (TREE_TYPE (elem
)),
5153 TYPE_ARG_TYPES (lhstype
), explicit_targs
, DEDUCE_EXACT
, 1);
5158 cp_error ("ambiguous template instantiation converting to `%#T'", lhstype
);
5159 return error_mark_node
;
5161 save_elem
= instantiate_template (elem
, t
);
5162 /* Check the return type. */
5163 if (!same_type_p (TREE_TYPE (lhstype
),
5164 TREE_TYPE (TREE_TYPE (save_elem
))))
5170 mark_used (save_elem
);
5175 /* There's no exact match, and no templates can be
5176 instantiated to match. The last thing we try is to see if
5177 some ordinary overloaded function is close enough. If
5178 we're only looking for template functions, we don't do
5182 for (elems
= rhs
; elems
; elems
= OVL_NEXT (elems
))
5184 elem
= OVL_CURRENT (elems
);
5185 if (TREE_CODE (elem
) == FUNCTION_DECL
5186 && comp_target_types (lhstype
, TREE_TYPE (elem
), 1) > 0)
5191 tree save_elem
= elem
;
5192 for (elems
= OVL_CHAIN (elems
); elems
;
5193 elems
= OVL_CHAIN (elems
))
5195 elem
= OVL_FUNCTION (elems
);
5196 if (TREE_CODE (elem
) == FUNCTION_DECL
5197 && comp_target_types (lhstype
, TREE_TYPE (elem
), 0) >0)
5205 ("cannot resolve overload to target type `%#T'",
5207 cp_error_at (" ambiguity between `%#D'", save_elem
);
5208 cp_error_at (" and `%#D', at least", elem
);
5210 return error_mark_node
;
5212 mark_used (save_elem
);
5217 /* We failed to find a match. */
5220 cp_error ("cannot resolve overload to target type `%#T'", lhstype
);
5222 (" because no suitable overload of function `%D' exists",
5223 DECL_NAME (OVL_FUNCTION (rhs
)));
5225 return error_mark_node
;
5230 if (TREE_PURPOSE (rhs
) == error_mark_node
)
5232 /* Make sure we don't drop the non-local flag, as the old code
5233 would rely on it. */
5234 int nl
= TREE_NONLOCAL_FLAG (rhs
);
5235 /* We don't need the type of this node. */
5236 rhs
= TREE_VALUE (rhs
);
5237 my_friendly_assert (TREE_NONLOCAL_FLAG (rhs
) == nl
, 980331);
5240 /* Now we should have a baselink. */
5241 my_friendly_assert (TREE_CODE (TREE_PURPOSE (rhs
)) == TREE_VEC
,
5243 my_friendly_assert (TREE_CHAIN (rhs
) == NULL_TREE
, 181);
5244 my_friendly_assert (TREE_CODE (TREE_VALUE (rhs
)) == FUNCTION_DECL
5245 || TREE_CODE (TREE_VALUE (rhs
)) == OVERLOAD
,
5248 return instantiate_type (lhstype
, TREE_VALUE (rhs
), complain
);
5252 /* This is too hard for now. */
5253 my_friendly_abort (183);
5254 return error_mark_node
;
5259 TREE_OPERAND (rhs
, 0)
5260 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), complain
);
5261 if (TREE_OPERAND (rhs
, 0) == error_mark_node
)
5262 return error_mark_node
;
5263 TREE_OPERAND (rhs
, 1)
5264 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), complain
);
5265 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
5266 return error_mark_node
;
5268 TREE_TYPE (rhs
) = lhstype
;
5272 case TRUNC_DIV_EXPR
:
5273 case FLOOR_DIV_EXPR
:
5275 case ROUND_DIV_EXPR
:
5277 case TRUNC_MOD_EXPR
:
5278 case FLOOR_MOD_EXPR
:
5280 case ROUND_MOD_EXPR
:
5281 case FIX_ROUND_EXPR
:
5282 case FIX_FLOOR_EXPR
:
5284 case FIX_TRUNC_EXPR
:
5300 case PREINCREMENT_EXPR
:
5301 case PREDECREMENT_EXPR
:
5302 case POSTINCREMENT_EXPR
:
5303 case POSTDECREMENT_EXPR
:
5305 error ("invalid operation on uninstantiated type");
5306 return error_mark_node
;
5308 case TRUTH_AND_EXPR
:
5310 case TRUTH_XOR_EXPR
:
5317 case TRUTH_ANDIF_EXPR
:
5318 case TRUTH_ORIF_EXPR
:
5319 case TRUTH_NOT_EXPR
:
5321 error ("not enough type information");
5322 return error_mark_node
;
5325 if (type_unknown_p (TREE_OPERAND (rhs
, 0)))
5328 error ("not enough type information");
5329 return error_mark_node
;
5331 TREE_OPERAND (rhs
, 1)
5332 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), complain
);
5333 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
5334 return error_mark_node
;
5335 TREE_OPERAND (rhs
, 2)
5336 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 2), complain
);
5337 if (TREE_OPERAND (rhs
, 2) == error_mark_node
)
5338 return error_mark_node
;
5340 TREE_TYPE (rhs
) = lhstype
;
5344 TREE_OPERAND (rhs
, 1)
5345 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), complain
);
5346 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
5347 return error_mark_node
;
5349 TREE_TYPE (rhs
) = lhstype
;
5353 if (TYPE_PTRMEMFUNC_P (lhstype
))
5354 lhstype
= TYPE_PTRMEMFUNC_FN_TYPE (lhstype
);
5355 else if (TREE_CODE (lhstype
) != POINTER_TYPE
)
5358 error ("type for resolving address of overloaded function must be pointer type");
5359 return error_mark_node
;
5362 tree fn
= instantiate_type (TREE_TYPE (lhstype
), TREE_OPERAND (rhs
, 0), complain
);
5363 if (fn
== error_mark_node
)
5364 return error_mark_node
;
5365 mark_addressable (fn
);
5366 TREE_TYPE (rhs
) = lhstype
;
5367 TREE_OPERAND (rhs
, 0) = fn
;
5368 TREE_CONSTANT (rhs
) = staticp (fn
);
5369 if (TREE_CODE (lhstype
) == POINTER_TYPE
5370 && TREE_CODE (TREE_TYPE (lhstype
)) == METHOD_TYPE
)
5372 build_ptrmemfunc_type (lhstype
);
5373 rhs
= build_ptrmemfunc (lhstype
, rhs
, 0);
5378 case ENTRY_VALUE_EXPR
:
5379 my_friendly_abort (184);
5380 return error_mark_node
;
5383 return error_mark_node
;
5386 my_friendly_abort (185);
5387 return error_mark_node
;
5391 /* Return the name of the virtual function pointer field
5392 (as an IDENTIFIER_NODE) for the given TYPE. Note that
5393 this may have to look back through base types to find the
5394 ultimate field name. (For single inheritance, these could
5395 all be the same name. Who knows for multiple inheritance). */
5398 get_vfield_name (type
)
5401 tree binfo
= TYPE_BINFO (type
);
5404 while (BINFO_BASETYPES (binfo
)
5405 && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo
, 0)))
5406 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo
, 0)))
5407 binfo
= BINFO_BASETYPE (binfo
, 0);
5409 type
= BINFO_TYPE (binfo
);
5410 buf
= (char *) alloca (sizeof (VFIELD_NAME_FORMAT
)
5411 + TYPE_NAME_LENGTH (type
) + 2);
5412 sprintf (buf
, VFIELD_NAME_FORMAT
, TYPE_NAME_STRING (type
));
5413 return get_identifier (buf
);
5417 print_class_statistics ()
5419 #ifdef GATHER_STATISTICS
5420 fprintf (stderr
, "convert_harshness = %d\n", n_convert_harshness
);
5421 fprintf (stderr
, "compute_conversion_costs = %d\n", n_compute_conversion_costs
);
5422 fprintf (stderr
, "build_method_call = %d (inner = %d)\n",
5423 n_build_method_call
, n_inner_fields_searched
);
5426 fprintf (stderr
, "vtables = %d; vtable searches = %d\n",
5427 n_vtables
, n_vtable_searches
);
5428 fprintf (stderr
, "vtable entries = %d; vtable elems = %d\n",
5429 n_vtable_entries
, n_vtable_elems
);
5434 /* Push an obstack which is sufficiently long-lived to hold such class
5435 decls that may be cached in the previous_class_values list. The
5436 effect is undone by pop_obstacks. */
5439 maybe_push_cache_obstack ()
5441 static int cache_obstack_initialized
;
5443 if (!cache_obstack_initialized
)
5445 gcc_obstack_init (&class_cache_obstack
);
5446 class_cache_firstobj
5447 = (char*) obstack_finish (&class_cache_obstack
);
5448 cache_obstack_initialized
= 1;
5451 push_obstacks_nochange ();
5452 current_obstack
= &class_cache_obstack
;
5455 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
5456 according to [class]:
5457 The class-name is also inserted
5458 into the scope of the class itself. For purposes of access checking,
5459 the inserted class name is treated as if it were a public member name. */
5462 build_self_reference ()
5464 tree name
= constructor_name (current_class_type
);
5465 tree value
= build_lang_decl (TYPE_DECL
, name
, current_class_type
);
5466 DECL_NONLOCAL (value
) = 1;
5467 DECL_CONTEXT (value
) = current_class_type
;
5468 DECL_CLASS_CONTEXT (value
) = current_class_type
;
5469 DECL_ARTIFICIAL (value
) = 1;
5471 pushdecl_class_level (value
);
5475 /* Returns 1 if TYPE contains only padding bytes. */
5478 is_empty_class (type
)
5483 if (type
== error_mark_node
)
5486 if (! IS_AGGR_TYPE (type
))
5490 return CLASSTYPE_SIZE (type
) == integer_zero_node
;
5492 if (TYPE_BINFO_BASETYPES (type
))
5494 t
= TYPE_FIELDS (type
);
5495 while (t
&& TREE_CODE (t
) != FIELD_DECL
)
5497 return (t
== NULL_TREE
);
5500 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
5501 a *_TYPE node. NODE can also be a local class. */
5504 get_enclosing_class (type
)
5509 while (node
&& TREE_CODE (node
) != NAMESPACE_DECL
)
5511 switch (TREE_CODE_CLASS (TREE_CODE (node
)))
5514 node
= DECL_CONTEXT (node
);
5520 node
= TYPE_CONTEXT (node
);
5524 my_friendly_abort (0);
5530 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
5533 is_base_of_enclosing_class (base
, type
)
5538 if (get_binfo (base
, type
, 0))
5541 type
= get_enclosing_class (type
);