1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 92-97, 1998, 1999 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* High-level class interface. */
33 #include "splay-tree.h"
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
39 /* This is how we tell when two virtual member functions are really the
41 #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
43 extern void set_class_shadows
PROTO ((tree
));
45 /* The number of nested classes being processed. If we are not in the
46 scope of any class, this is zero. */
48 int current_class_depth
;
50 /* In order to deal with nested classes, we keep a stack of classes.
51 The topmost entry is the innermost class, and is the entry at index
52 CURRENT_CLASS_DEPTH */
54 typedef struct class_stack_node
{
55 /* The name of the class. */
58 /* The _TYPE node for the class. */
61 /* The access specifier pending for new declarations in the scope of
65 /* If were defining TYPE, the names used in this class. */
66 splay_tree names_used
;
67 }* class_stack_node_t
;
69 /* The stack itself. This is an dynamically resized array. The
70 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
71 static int current_class_stack_size
;
72 static class_stack_node_t current_class_stack
;
74 /* When we're processing a member function, current_class_ptr is the
75 PARM_DECL for the `this' pointer. The current_class_ref is an
76 expression for `*this'. */
77 tree current_class_ptr
, current_class_ref
;
79 /* The following two can be derived from the previous one */
80 tree current_class_name
; /* IDENTIFIER_NODE: name of current class */
81 tree current_class_type
; /* _TYPE: the type of the current class */
82 tree current_access_specifier
;
83 tree previous_class_type
; /* _TYPE: the previous type that was a class */
84 tree previous_class_values
; /* TREE_LIST: copy of the class_shadowed list
85 when leaving an outermost class scope. */
87 /* The obstack on which the cached class declarations are kept. */
88 static struct obstack class_cache_obstack
;
89 /* The first object allocated on that obstack. We can use
90 obstack_free with tis value to free the entire obstack. */
91 char *class_cache_firstobj
;
95 static tree get_vfield_name
PROTO((tree
));
96 static void finish_struct_anon
PROTO((tree
));
97 static tree build_vbase_pointer
PROTO((tree
, tree
));
98 static tree build_vtable_entry
PROTO((tree
, tree
));
99 static tree get_vtable_name
PROTO((tree
));
100 static tree get_derived_offset
PROTO((tree
, tree
));
101 static tree get_basefndecls
PROTO((tree
, tree
));
102 static void set_rtti_entry
PROTO((tree
, tree
, tree
));
103 static tree build_vtable
PROTO((tree
, tree
));
104 static void prepare_fresh_vtable
PROTO((tree
, tree
));
105 static void fixup_vtable_deltas1
PROTO((tree
, tree
));
106 static void fixup_vtable_deltas
PROTO((tree
, int, tree
));
107 static void finish_vtbls
PROTO((tree
, int, tree
));
108 static void modify_vtable_entry
PROTO((tree
, tree
, tree
));
109 static tree get_vtable_entry_n
PROTO((tree
, unsigned HOST_WIDE_INT
));
110 static void add_virtual_function
PROTO((tree
*, tree
*, int *, tree
, tree
));
111 static tree delete_duplicate_fields_1
PROTO((tree
, tree
));
112 static void delete_duplicate_fields
PROTO((tree
));
113 static void finish_struct_bits
PROTO((tree
, int));
114 static int alter_access
PROTO((tree
, tree
, tree
, tree
));
115 static void handle_using_decl
PROTO((tree
, tree
, tree
, tree
));
116 static int overrides
PROTO((tree
, tree
));
117 static int strictly_overrides
PROTO((tree
, tree
));
118 static void merge_overrides
PROTO((tree
, tree
, int, tree
));
119 static void override_one_vtable
PROTO((tree
, tree
, tree
));
120 static void mark_overriders
PROTO((tree
, tree
));
121 static void check_for_override
PROTO((tree
, tree
));
122 static tree get_class_offset_1
PROTO((tree
, tree
, tree
, tree
, tree
));
123 static tree get_class_offset
PROTO((tree
, tree
, tree
, tree
));
124 static void modify_one_vtable
PROTO((tree
, tree
, tree
, tree
));
125 static void modify_all_vtables
PROTO((tree
, tree
, tree
));
126 static void modify_all_direct_vtables
PROTO((tree
, int, tree
, tree
,
128 static void modify_all_indirect_vtables
PROTO((tree
, int, int, 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 int field_decl_cmp
PROTO ((const tree
*, const tree
*));
134 static int method_name_cmp
PROTO ((const tree
*, const tree
*));
135 static tree make_method_vec
PROTO((int));
136 static void free_method_vec
PROTO((tree
));
137 static tree add_implicitly_declared_members
PROTO((tree
, int, int, int));
138 static tree fixed_type_or_null
PROTO((tree
, int *));
139 static tree resolve_address_of_overloaded_function
PROTO((tree
, tree
, int,
141 static void build_vtable_entry_ref
PROTO((tree
, tree
, tree
));
143 /* Way of stacking language names. */
144 tree
*current_lang_base
, *current_lang_stack
;
145 int current_lang_stacksize
;
147 /* Names of languages we recognize. */
148 tree lang_name_c
, lang_name_cplusplus
, lang_name_java
;
149 tree current_lang_name
;
151 /* When layout out an aggregate type, the size of the
152 basetypes (virtual and non-virtual) is passed to layout_record
154 static tree base_layout_decl
;
156 /* Constants used for access control. */
157 tree access_default_node
; /* 0 */
158 tree access_public_node
; /* 1 */
159 tree access_protected_node
; /* 2 */
160 tree access_private_node
; /* 3 */
161 tree access_default_virtual_node
; /* 4 */
162 tree access_public_virtual_node
; /* 5 */
163 tree access_protected_virtual_node
; /* 6 */
164 tree access_private_virtual_node
; /* 7 */
166 /* Variables shared between class.c and call.c. */
168 #ifdef GATHER_STATISTICS
170 int n_vtable_entries
= 0;
171 int n_vtable_searches
= 0;
172 int n_vtable_elems
= 0;
173 int n_convert_harshness
= 0;
174 int n_compute_conversion_costs
= 0;
175 int n_build_method_call
= 0;
176 int n_inner_fields_searched
= 0;
179 /* Virtual baseclass things. */
182 build_vbase_pointer (exp
, type
)
186 FORMAT_VBASE_NAME (name
, type
);
188 return build_component_ref (exp
, get_identifier (name
), NULL_TREE
, 0);
192 /* Is the type of the EXPR, the complete type of the object?
193 If we are going to be wrong, we must be conservative, and return 0. */
196 complete_type_p (expr
)
199 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (expr
));
202 switch (TREE_CODE (expr
))
209 expr
= TREE_OPERAND (expr
, 0);
213 if (! TREE_HAS_CONSTRUCTOR (expr
))
215 /* fall through... */
218 if (TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
219 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr
)))
220 && TYPE_MAIN_VARIANT (TREE_TYPE (expr
)) == type
)
222 /* fall through... */
225 if (IS_AGGR_TYPE (TREE_TYPE (expr
))
226 && TYPE_MAIN_VARIANT (TREE_TYPE (expr
)) == type
)
228 /* fall through... */
239 /* Build multi-level access to EXPR using hierarchy path PATH.
240 CODE is PLUS_EXPR if we are going with the grain,
241 and MINUS_EXPR if we are not (in which case, we cannot traverse
242 virtual baseclass links).
244 TYPE is the type we want this path to have on exit.
246 NONNULL is non-zero if we know (for any reason) that EXPR is
247 not, in fact, zero. */
250 build_vbase_path (code
, type
, expr
, path
, nonnull
)
252 tree type
, expr
, path
;
255 register int changed
= 0;
256 tree last
= NULL_TREE
, last_virtual
= NULL_TREE
;
258 tree null_expr
= 0, nonnull_expr
;
260 tree offset
= integer_zero_node
;
262 if (BINFO_INHERITANCE_CHAIN (path
) == NULL_TREE
)
263 return build1 (NOP_EXPR
, type
, expr
);
265 /* If -fthis-is-variable, we might have set nonnull incorrectly. We
266 don't care enough to get this right, so just clear it. */
267 if (flag_this_is_variable
> 0)
270 /* We could do better if we had additional logic to convert back to the
271 unconverted type (the static type of the complete object), and then
272 convert back to the type we want. Until that is done, we only optimize
273 if the complete type is the same type as expr has. */
274 fixed_type_p
= resolves_to_fixed_type_p (expr
, &nonnull
);
276 if (!fixed_type_p
&& TREE_SIDE_EFFECTS (expr
))
277 expr
= save_expr (expr
);
280 if (BINFO_INHERITANCE_CHAIN (path
))
281 path
= reverse_path (path
);
283 basetype
= BINFO_TYPE (path
);
287 if (TREE_VIA_VIRTUAL (path
))
289 last_virtual
= BINFO_TYPE (path
);
290 if (code
== PLUS_EXPR
)
292 changed
= ! fixed_type_p
;
298 /* We already check for ambiguous things in the caller, just
302 tree binfo
= get_binfo (last
, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr
))), 0);
303 nonnull_expr
= convert_pointer_to_real (binfo
, nonnull_expr
);
305 ind
= build_indirect_ref (nonnull_expr
, NULL_PTR
);
306 nonnull_expr
= build_vbase_pointer (ind
, last_virtual
);
308 && TREE_CODE (type
) == POINTER_TYPE
309 && null_expr
== NULL_TREE
)
311 null_expr
= build1 (NOP_EXPR
, build_pointer_type (last_virtual
), integer_zero_node
);
312 expr
= build (COND_EXPR
, build_pointer_type (last_virtual
),
313 build (EQ_EXPR
, boolean_type_node
, expr
,
315 null_expr
, nonnull_expr
);
318 /* else we'll figure out the offset below. */
320 /* Happens in the case of parse errors. */
321 if (nonnull_expr
== error_mark_node
)
322 return error_mark_node
;
326 cp_error ("cannot cast up from virtual baseclass `%T'",
328 return error_mark_node
;
332 path
= BINFO_INHERITANCE_CHAIN (path
);
334 /* LAST is now the last basetype assoc on the path. */
336 /* A pointer to a virtual base member of a non-null object
337 is non-null. Therefore, we only need to test for zeroness once.
338 Make EXPR the canonical expression to deal with here. */
341 TREE_OPERAND (expr
, 2) = nonnull_expr
;
342 TREE_TYPE (expr
) = TREE_TYPE (TREE_OPERAND (expr
, 1))
343 = TREE_TYPE (nonnull_expr
);
348 /* If we go through any virtual base pointers, make sure that
349 casts to BASETYPE from the last virtual base class use
350 the right value for BASETYPE. */
353 tree intype
= TREE_TYPE (TREE_TYPE (expr
));
354 if (TYPE_MAIN_VARIANT (intype
) != BINFO_TYPE (last
))
356 tree binfo
= get_binfo (last
, TYPE_MAIN_VARIANT (intype
), 0);
357 offset
= BINFO_OFFSET (binfo
);
364 offset
= BINFO_OFFSET (binfo_member (last_virtual
,
365 CLASSTYPE_VBASECLASSES (basetype
)));
366 offset
= size_binop (PLUS_EXPR
, offset
, BINFO_OFFSET (last
));
369 offset
= BINFO_OFFSET (last
);
372 if (TREE_INT_CST_LOW (offset
))
374 /* Bash types to make the backend happy. */
375 offset
= cp_convert (type
, offset
);
377 /* This shouldn't be necessary. (mrs) */
378 expr
= build1 (NOP_EXPR
, type
, expr
);
381 /* If expr might be 0, we need to preserve that zeroness. */
385 TREE_TYPE (null_expr
) = type
;
387 null_expr
= build1 (NOP_EXPR
, type
, integer_zero_node
);
388 if (TREE_SIDE_EFFECTS (expr
))
389 expr
= save_expr (expr
);
391 return build (COND_EXPR
, type
,
392 build (EQ_EXPR
, boolean_type_node
, expr
, integer_zero_node
),
394 build (code
, type
, expr
, offset
));
396 else return build (code
, type
, expr
, offset
);
399 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
400 be used multiple times in initialization of multiple inheritance. */
403 TREE_TYPE (expr
) = type
;
407 return build1 (NOP_EXPR
, type
, expr
);
410 /* Virtual function things. */
412 /* Build an entry in the virtual function table.
413 DELTA is the offset for the `this' pointer.
414 PFN is an ADDR_EXPR containing a pointer to the virtual function.
415 Note that the index (DELTA2) in the virtual function table
419 build_vtable_entry (delta
, pfn
)
422 if (flag_vtable_thunks
)
424 HOST_WIDE_INT idelta
= TREE_INT_CST_LOW (delta
);
425 if (idelta
&& ! DECL_ABSTRACT_VIRTUAL_P (TREE_OPERAND (pfn
, 0)))
427 pfn
= build1 (ADDR_EXPR
, vtable_entry_type
,
428 make_thunk (pfn
, idelta
));
429 TREE_READONLY (pfn
) = 1;
430 TREE_CONSTANT (pfn
) = 1;
432 #ifdef GATHER_STATISTICS
433 n_vtable_entries
+= 1;
439 extern int flag_huge_objects
;
440 tree elems
= expr_tree_cons (NULL_TREE
, delta
,
441 expr_tree_cons (NULL_TREE
, integer_zero_node
,
442 build_expr_list (NULL_TREE
, pfn
)));
443 tree entry
= build (CONSTRUCTOR
, vtable_entry_type
, NULL_TREE
, elems
);
445 /* DELTA used to be constructed by `size_int' and/or size_binop,
446 which caused overflow problems when it was negative. That should
449 if (! int_fits_type_p (delta
, delta_type_node
))
451 if (flag_huge_objects
)
452 sorry ("object size exceeds built-in limit for virtual function table implementation");
454 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
457 TREE_CONSTANT (entry
) = 1;
458 TREE_STATIC (entry
) = 1;
459 TREE_READONLY (entry
) = 1;
461 #ifdef GATHER_STATISTICS
462 n_vtable_entries
+= 1;
469 /* We want to give the assembler the vtable identifier as well as
470 the offset to the function pointer. So we generate
472 __asm__ __volatile__ (".vtable_entry %c0, %c1"
473 : : "s"(&class_vtable),
474 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
477 build_vtable_entry_ref (basetype
, vtbl
, idx
)
478 tree basetype
, vtbl
, idx
;
480 static char asm_stmt
[] = ".vtable_entry %c0, %c1";
483 s
= build_unary_op (ADDR_EXPR
, TYPE_BINFO_VTABLE (basetype
), 0);
484 s
= build_tree_list (build_string (1, "s"), s
);
486 i
= build_array_ref (vtbl
, idx
);
487 if (!flag_vtable_thunks
)
488 i
= build_component_ref (i
, pfn_identifier
, vtable_entry_type
, 0);
489 i
= build_c_cast (ptrdiff_type_node
, build_unary_op (ADDR_EXPR
, i
, 0));
490 i2
= build_array_ref (vtbl
, build_int_2(0,0));
491 i2
= build_c_cast (ptrdiff_type_node
, build_unary_op (ADDR_EXPR
, i2
, 0));
492 i
= build_binary_op (MINUS_EXPR
, i
, i2
);
493 i
= build_tree_list (build_string (1, "i"), i
);
495 expand_asm_operands (build_string (sizeof(asm_stmt
)-1, asm_stmt
),
496 NULL_TREE
, chainon (s
, i
), NULL_TREE
, 1, NULL
, 0);
499 /* Given an object INSTANCE, return an expression which yields the
500 virtual function vtable element corresponding to INDEX. There are
501 many special cases for INSTANCE which we take care of here, mainly
502 to avoid creating extra tree nodes when we don't have to. */
505 build_vtbl_ref (instance
, idx
)
509 tree basetype
= TREE_TYPE (instance
);
511 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
512 basetype
= TREE_TYPE (basetype
);
514 if (instance
== current_class_ref
)
515 vtbl
= build_vfield_ref (instance
, basetype
);
520 /* Try to figure out what a reference refers to, and
521 access its virtual function table directly. */
522 tree ref
= NULL_TREE
;
524 if (TREE_CODE (instance
) == INDIRECT_REF
525 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance
, 0))) == REFERENCE_TYPE
)
526 ref
= TREE_OPERAND (instance
, 0);
527 else if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
530 if (ref
&& TREE_CODE (ref
) == VAR_DECL
531 && DECL_INITIAL (ref
))
533 tree init
= DECL_INITIAL (ref
);
535 while (TREE_CODE (init
) == NOP_EXPR
536 || TREE_CODE (init
) == NON_LVALUE_EXPR
)
537 init
= TREE_OPERAND (init
, 0);
538 if (TREE_CODE (init
) == ADDR_EXPR
)
540 init
= TREE_OPERAND (init
, 0);
541 if (IS_AGGR_TYPE (TREE_TYPE (init
))
542 && (TREE_CODE (init
) == PARM_DECL
543 || TREE_CODE (init
) == VAR_DECL
))
549 if (IS_AGGR_TYPE (TREE_TYPE (instance
))
550 && (TREE_CODE (instance
) == RESULT_DECL
551 || TREE_CODE (instance
) == PARM_DECL
552 || TREE_CODE (instance
) == VAR_DECL
))
553 vtbl
= TYPE_BINFO_VTABLE (basetype
);
555 vtbl
= build_vfield_ref (instance
, basetype
);
558 assemble_external (vtbl
);
561 build_vtable_entry_ref (basetype
, vtbl
, idx
);
563 aref
= build_array_ref (vtbl
, idx
);
568 /* Given an object INSTANCE, return an expression which yields the
569 virtual function corresponding to INDEX. There are many special
570 cases for INSTANCE which we take care of here, mainly to avoid
571 creating extra tree nodes when we don't have to. */
574 build_vfn_ref (ptr_to_instptr
, instance
, idx
)
575 tree
*ptr_to_instptr
, instance
;
578 tree aref
= build_vtbl_ref (instance
, idx
);
580 /* When using thunks, there is no extra delta, and we get the pfn
582 if (flag_vtable_thunks
)
587 /* Save the intermediate result in a SAVE_EXPR so we don't have to
588 compute each component of the virtual function pointer twice. */
589 if (TREE_CODE (aref
) == INDIRECT_REF
)
590 TREE_OPERAND (aref
, 0) = save_expr (TREE_OPERAND (aref
, 0));
593 = build (PLUS_EXPR
, TREE_TYPE (*ptr_to_instptr
),
595 cp_convert (ptrdiff_type_node
,
596 build_component_ref (aref
, delta_identifier
, NULL_TREE
, 0)));
599 return build_component_ref (aref
, pfn_identifier
, NULL_TREE
, 0);
602 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
603 for the given TYPE. */
606 get_vtable_name (type
)
609 tree type_id
= build_typename_overload (type
);
610 char *buf
= (char *) alloca (strlen (VTABLE_NAME_FORMAT
)
611 + IDENTIFIER_LENGTH (type_id
) + 2);
612 const char *ptr
= IDENTIFIER_POINTER (type_id
);
614 for (i
= 0; ptr
[i
] == OPERATOR_TYPENAME_FORMAT
[i
]; i
++) ;
616 /* We don't take off the numbers; prepare_fresh_vtable uses the
617 DECL_ASSEMBLER_NAME for the type, which includes the number
618 in `3foo'. If we were to pull them off here, we'd end up with
619 something like `_vt.foo.3bar', instead of a uniform definition. */
620 while (ptr
[i
] >= '0' && ptr
[i
] <= '9')
623 sprintf (buf
, VTABLE_NAME_FORMAT
, ptr
+i
);
624 return get_identifier (buf
);
627 /* Return the offset to the main vtable for a given base BINFO. */
630 get_vfield_offset (binfo
)
634 = size_binop (FLOOR_DIV_EXPR
,
635 DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo
))),
636 size_int (BITS_PER_UNIT
));
637 tmp
= convert (sizetype
, tmp
);
638 return size_binop (PLUS_EXPR
, tmp
, BINFO_OFFSET (binfo
));
641 /* Get the offset to the start of the original binfo that we derived
642 this binfo from. If we find TYPE first, return the offset only
643 that far. The shortened search is useful because the this pointer
644 on method calling is expected to point to a DECL_CONTEXT (fndecl)
645 object, and not a baseclass of it. */
648 get_derived_offset (binfo
, type
)
651 tree offset1
= get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo
)));
654 while (BINFO_BASETYPES (binfo
)
655 && (i
=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
))) != -1)
657 tree binfos
= BINFO_BASETYPES (binfo
);
658 if (BINFO_TYPE (binfo
) == type
)
660 binfo
= TREE_VEC_ELT (binfos
, i
);
662 offset2
= get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo
)));
663 return size_binop (MINUS_EXPR
, offset1
, offset2
);
666 /* Update the rtti info for this class. */
669 set_rtti_entry (virtuals
, offset
, type
)
670 tree virtuals
, offset
, type
;
674 if (CLASSTYPE_COM_INTERFACE (type
))
678 vfn
= build1 (ADDR_EXPR
, vfunc_ptr_type_node
, get_tinfo_fn (type
));
680 vfn
= build1 (NOP_EXPR
, vfunc_ptr_type_node
, size_zero_node
);
681 TREE_CONSTANT (vfn
) = 1;
683 if (! flag_vtable_thunks
)
684 TREE_VALUE (virtuals
) = build_vtable_entry (offset
, vfn
);
687 tree voff
= build1 (NOP_EXPR
, vfunc_ptr_type_node
, offset
);
688 TREE_CONSTANT (voff
) = 1;
690 TREE_VALUE (virtuals
) = build_vtable_entry (integer_zero_node
, voff
);
692 /* The second slot is for the tdesc pointer when thunks are used. */
693 TREE_VALUE (TREE_CHAIN (virtuals
))
694 = build_vtable_entry (integer_zero_node
, vfn
);
698 /* Build a virtual function for type TYPE.
699 If BINFO is non-NULL, build the vtable starting with the initial
700 approximation that it is the same as the one which is the head of
701 the association list. */
704 build_vtable (binfo
, type
)
707 tree name
= get_vtable_name (type
);
714 virtuals
= copy_list (BINFO_VIRTUALS (binfo
));
715 decl
= build_lang_decl (VAR_DECL
, name
, TREE_TYPE (BINFO_VTABLE (binfo
)));
717 /* Now do rtti stuff. */
718 offset
= get_derived_offset (TYPE_BINFO (type
), NULL_TREE
);
719 offset
= ssize_binop (MINUS_EXPR
, integer_zero_node
, offset
);
720 set_rtti_entry (virtuals
, offset
, type
);
724 virtuals
= NULL_TREE
;
725 decl
= build_lang_decl (VAR_DECL
, name
, void_type_node
);
728 #ifdef GATHER_STATISTICS
730 n_vtable_elems
+= list_length (virtuals
);
733 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
734 import_export_vtable (decl
, type
, 0);
736 decl
= pushdecl_top_level (decl
);
737 SET_IDENTIFIER_GLOBAL_VALUE (name
, decl
);
738 /* Initialize the association list for this type, based
739 on our first approximation. */
740 TYPE_BINFO_VTABLE (type
) = decl
;
741 TYPE_BINFO_VIRTUALS (type
) = virtuals
;
743 DECL_ARTIFICIAL (decl
) = 1;
744 TREE_STATIC (decl
) = 1;
745 #ifndef WRITABLE_VTABLES
746 /* Make them READONLY by default. (mrs) */
747 TREE_READONLY (decl
) = 1;
749 /* At one time the vtable info was grabbed 2 words at a time. This
750 fails on sparc unless you have 8-byte alignment. (tiemann) */
751 DECL_ALIGN (decl
) = MAX (TYPE_ALIGN (double_type_node
),
754 DECL_VIRTUAL_P (decl
) = 1;
755 DECL_CONTEXT (decl
) = type
;
757 binfo
= TYPE_BINFO (type
);
758 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
762 extern tree signed_size_zero_node
;
764 /* Give TYPE a new virtual function table which is initialized
765 with a skeleton-copy of its original initialization. The only
766 entry that changes is the `delta' entry, so we can really
767 share a lot of structure.
769 FOR_TYPE is the derived type which caused this table to
772 BINFO is the type association which provided TYPE for FOR_TYPE.
774 The order in which vtables are built (by calling this function) for
775 an object must remain the same, otherwise a binary incompatibility
779 prepare_fresh_vtable (binfo
, for_type
)
780 tree binfo
, for_type
;
783 tree orig_decl
= BINFO_VTABLE (binfo
);
796 basetype
= TYPE_MAIN_VARIANT (BINFO_TYPE (binfo
));
798 buf2
= TYPE_ASSEMBLER_NAME_STRING (basetype
);
799 i
= TYPE_ASSEMBLER_NAME_LENGTH (basetype
) + 1;
801 /* We know that the vtable that we are going to create doesn't exist
802 yet in the global namespace, and when we finish, it will be
803 pushed into the global namespace. In complex MI hierarchies, we
804 have to loop while the name we are thinking of adding is globally
805 defined, adding more name components to the vtable name as we
806 loop, until the name is unique. This is because in complex MI
807 cases, we might have the same base more than once. This means
808 that the order in which this function is called for vtables must
809 remain the same, otherwise binary compatibility can be
814 char *buf1
= (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type
)
818 sprintf (buf1
, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type
), joiner
,
820 buf
= (char *) alloca (strlen (VTABLE_NAME_FORMAT
) + strlen (buf1
) + 1);
821 sprintf (buf
, VTABLE_NAME_FORMAT
, buf1
);
822 name
= get_identifier (buf
);
824 /* If this name doesn't clash, then we can use it, otherwise
825 we add more to the name until it is unique. */
827 if (! IDENTIFIER_GLOBAL_VALUE (name
))
830 /* Set values for next loop through, if the name isn't unique. */
832 path
= BINFO_INHERITANCE_CHAIN (path
);
834 /* We better not run out of stuff to make it unique. */
835 my_friendly_assert (path
!= NULL_TREE
, 368);
837 basetype
= TYPE_MAIN_VARIANT (BINFO_TYPE (path
));
839 if (for_type
== basetype
)
841 /* If we run out of basetypes in the path, we have already
842 found created a vtable with that name before, we now
843 resort to tacking on _%d to distinguish them. */
845 i
= TYPE_ASSEMBLER_NAME_LENGTH (basetype
) + 1 + i
+ 1 + 3;
846 buf1
= (char *) alloca (i
);
848 sprintf (buf1
, "%s%c%s%c%d",
849 TYPE_ASSEMBLER_NAME_STRING (basetype
), joiner
,
851 buf
= (char *) alloca (strlen (VTABLE_NAME_FORMAT
)
852 + strlen (buf1
) + 1);
853 sprintf (buf
, VTABLE_NAME_FORMAT
, buf1
);
854 name
= get_identifier (buf
);
856 /* If this name doesn't clash, then we can use it,
857 otherwise we add something different to the name until
859 } while (++j
<= 999 && IDENTIFIER_GLOBAL_VALUE (name
));
861 /* Hey, they really like MI don't they? Increase the 3
862 above to 6, and the 999 to 999999. :-) */
863 my_friendly_assert (j
<= 999, 369);
868 i
= TYPE_ASSEMBLER_NAME_LENGTH (basetype
) + 1 + i
;
869 new_buf2
= (char *) alloca (i
);
870 sprintf (new_buf2
, "%s%c%s",
871 TYPE_ASSEMBLER_NAME_STRING (basetype
), joiner
, buf2
);
875 new_decl
= build_lang_decl (VAR_DECL
, name
, TREE_TYPE (orig_decl
));
876 /* Remember which class this vtable is really for. */
877 DECL_CONTEXT (new_decl
) = for_type
;
879 DECL_ARTIFICIAL (new_decl
) = 1;
880 TREE_STATIC (new_decl
) = 1;
881 BINFO_VTABLE (binfo
) = pushdecl_top_level (new_decl
);
882 DECL_VIRTUAL_P (new_decl
) = 1;
883 #ifndef WRITABLE_VTABLES
884 /* Make them READONLY by default. (mrs) */
885 TREE_READONLY (new_decl
) = 1;
887 DECL_ALIGN (new_decl
) = DECL_ALIGN (orig_decl
);
889 /* Make fresh virtual list, so we can smash it later. */
890 BINFO_VIRTUALS (binfo
) = copy_list (BINFO_VIRTUALS (binfo
));
892 if (TREE_VIA_VIRTUAL (binfo
))
894 tree binfo1
= binfo_member (BINFO_TYPE (binfo
),
895 CLASSTYPE_VBASECLASSES (for_type
));
897 /* XXX - This should never happen, if it does, the caller should
898 ensure that the binfo is from for_type's binfos, not from any
899 base type's. We can remove all this code after a while. */
901 warning ("internal inconsistency: binfo offset error for rtti");
903 offset
= BINFO_OFFSET (binfo1
);
906 offset
= BINFO_OFFSET (binfo
);
908 set_rtti_entry (BINFO_VIRTUALS (binfo
),
909 ssize_binop (MINUS_EXPR
, integer_zero_node
, offset
),
912 #ifdef GATHER_STATISTICS
914 n_vtable_elems
+= list_length (BINFO_VIRTUALS (binfo
));
917 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
918 import_export_vtable (new_decl
, for_type
, 0);
920 if (TREE_VIA_VIRTUAL (binfo
))
921 my_friendly_assert (binfo
== binfo_member (BINFO_TYPE (binfo
),
922 CLASSTYPE_VBASECLASSES (current_class_type
)),
924 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
928 /* Access the virtual function table entry that logically
929 contains BASE_FNDECL. VIRTUALS is the virtual function table's
930 initializer. We can run off the end, when dealing with virtual
931 destructors in MI situations, return NULL_TREE in that case. */
934 get_vtable_entry (virtuals
, base_fndecl
)
935 tree virtuals
, base_fndecl
;
937 unsigned HOST_WIDE_INT n
= (HOST_BITS_PER_WIDE_INT
>= BITS_PER_WORD
938 ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl
))
939 & (((unsigned HOST_WIDE_INT
)1<<(BITS_PER_WORD
-1))-1))
940 : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl
)));
942 #ifdef GATHER_STATISTICS
943 n_vtable_searches
+= n
;
946 while (n
> 0 && virtuals
)
949 virtuals
= TREE_CHAIN (virtuals
);
955 /* Put new entry ENTRY into virtual function table initializer
958 Also update DECL_VINDEX (FNDECL). */
961 modify_vtable_entry (old_entry_in_list
, new_entry
, fndecl
)
962 tree old_entry_in_list
, new_entry
, fndecl
;
964 tree base_fndecl
= TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list
)), 0);
967 cp_warning ("replaced %D with %D", DECL_ASSEMBLER_NAME (base_fndecl
),
968 DECL_ASSEMBLER_NAME (fndecl
));
970 TREE_VALUE (old_entry_in_list
) = new_entry
;
972 /* Now assign virtual dispatch information, if unset. */
973 /* We can dispatch this, through any overridden base function. */
974 if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
976 DECL_VINDEX (fndecl
) = DECL_VINDEX (base_fndecl
);
977 DECL_CONTEXT (fndecl
) = DECL_CONTEXT (base_fndecl
);
981 /* Access the virtual function table entry N. VIRTUALS is the virtual
982 function table's initializer. */
985 get_vtable_entry_n (virtuals
, n
)
987 unsigned HOST_WIDE_INT n
;
992 virtuals
= TREE_CHAIN (virtuals
);
997 /* Add a virtual function to all the appropriate vtables for the class
998 T. DECL_VINDEX(X) should be error_mark_node, if we want to
999 allocate a new slot in our table. If it is error_mark_node, we
1000 know that no other function from another vtable is overridden by X.
1001 HAS_VIRTUAL keeps track of how many virtuals there are in our main
1002 vtable for the type, and we build upon the PENDING_VIRTUALS list
1006 add_virtual_function (pv
, phv
, has_virtual
, fndecl
, t
)
1010 tree t
; /* Structure type. */
1012 tree pending_virtuals
= *pv
;
1013 tree pending_hard_virtuals
= *phv
;
1015 /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
1016 convert to void *. Make such a conversion here. */
1017 tree vfn
= build1 (ADDR_EXPR
, vfunc_ptr_type_node
, fndecl
);
1018 TREE_CONSTANT (vfn
) = 1;
1021 if (current_class_type
== 0)
1022 cp_warning ("internal problem, current_class_type is zero when adding `%D', please report",
1024 if (current_class_type
&& t
!= current_class_type
)
1025 cp_warning ("internal problem, current_class_type differs when adding `%D', please report",
1029 /* If the virtual function is a redefinition of a prior one,
1030 figure out in which base class the new definition goes,
1031 and if necessary, make a fresh virtual function table
1032 to hold that entry. */
1033 if (DECL_VINDEX (fndecl
) == error_mark_node
)
1037 /* We remember that this was the base sub-object for rtti. */
1038 CLASSTYPE_RTTI (t
) = t
;
1040 /* If we are using thunks, use two slots at the front, one
1041 for the offset pointer, one for the tdesc pointer.
1042 For ARM-style vtables, use the same slot for both. */
1043 if (*has_virtual
== 0 && ! CLASSTYPE_COM_INTERFACE (t
))
1045 if (flag_vtable_thunks
)
1051 /* Build a new INT_CST for this DECL_VINDEX. */
1053 static tree index_table
[256];
1055 /* We skip a slot for the offset/tdesc entry. */
1056 int i
= (*has_virtual
)++;
1058 if (i
>= 256 || index_table
[i
] == 0)
1060 idx
= build_int_2 (i
, 0);
1062 index_table
[i
] = idx
;
1065 idx
= index_table
[i
];
1067 /* Now assign virtual dispatch information. */
1068 DECL_VINDEX (fndecl
) = idx
;
1069 DECL_CONTEXT (fndecl
) = t
;
1071 entry
= build_vtable_entry (integer_zero_node
, vfn
);
1072 pending_virtuals
= tree_cons (DECL_VINDEX (fndecl
), entry
, pending_virtuals
);
1074 /* Might already be INTEGER_CST if declared twice in class. We will
1075 give error later or we've already given it. */
1076 else if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
1078 /* Need an entry in some other virtual function table.
1079 Deal with this after we have laid out our virtual base classes. */
1080 pending_hard_virtuals
= temp_tree_cons (fndecl
, vfn
, pending_hard_virtuals
);
1082 *pv
= pending_virtuals
;
1083 *phv
= pending_hard_virtuals
;
1086 /* Obstack on which to build the vector of class methods. */
1087 struct obstack class_obstack
;
1088 extern struct obstack
*current_obstack
;
1090 /* These are method vectors that were too small for the number of
1091 methods in some class, and so were abandoned. */
1092 static tree free_method_vecs
;
1094 /* Returns a method vector with enough room for N methods. N should
1095 be a power of two. */
1104 for (t
= &free_method_vecs
; *t
; t
= &(TREE_CHAIN (*t
)))
1105 /* Note that we don't use >= n here because we don't want to
1106 allocate a very large vector where it isn't needed. */
1107 if (TREE_VEC_LENGTH (*t
) == n
)
1110 *t
= TREE_CHAIN (new_vec
);
1111 TREE_CHAIN (new_vec
) = NULL_TREE
;
1112 bzero ((PTR
) &TREE_VEC_ELT (new_vec
, 0), n
* sizeof (tree
));
1116 new_vec
= make_tree_vec (n
);
1120 /* Free the method vector VEC. */
1123 free_method_vec (vec
)
1126 TREE_CHAIN (vec
) = free_method_vecs
;
1127 free_method_vecs
= vec
;
1130 /* Add method METHOD to class TYPE.
1132 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1133 the class type where the method should be added. */
1136 add_method (type
, fields
, method
)
1137 tree type
, *fields
, method
;
1139 push_obstacks_nochange ();
1140 end_temporary_allocation ();
1142 /* Setting the DECL_CONTEXT and DECL_CLASS_CONTEXT here is probably
1144 DECL_CONTEXT (method
) = type
;
1145 DECL_CLASS_CONTEXT (method
) = type
;
1147 if (fields
&& *fields
)
1148 *fields
= build_overload (method
, *fields
);
1155 if (!CLASSTYPE_METHOD_VEC (type
))
1156 /* Make a new method vector. We start with 8 entries. We must
1157 allocate at least two (for constructors and destructors), and
1158 we're going to end up with an assignment operator at some
1161 We could use a TREE_LIST for now, and convert it to a
1162 TREE_VEC in finish_struct, but we would probably waste more
1163 memory making the links in the list than we would by
1164 over-allocating the size of the vector here. Furthermore,
1165 we would complicate all the code that expects this to be a
1166 vector. We keep a free list of vectors that we outgrew so
1167 that we don't really waste any memory. */
1168 CLASSTYPE_METHOD_VEC (type
) = make_method_vec (8);
1170 method_vec
= CLASSTYPE_METHOD_VEC (type
);
1171 len
= TREE_VEC_LENGTH (method_vec
);
1173 if (DECL_NAME (method
) == constructor_name (type
))
1174 /* A new constructor or destructor. Constructors go in
1175 slot 0; destructors go in slot 1. */
1176 slot
= DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method
)) ? 1 : 0;
1179 /* See if we already have an entry with this name. */
1180 for (slot
= 2; slot
< len
; ++slot
)
1181 if (!TREE_VEC_ELT (method_vec
, slot
)
1182 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec
,
1184 == DECL_NAME (method
)))
1189 /* We need a bigger method vector. */
1190 tree new_vec
= make_method_vec (2 * len
);
1191 bcopy ((PTR
) &TREE_VEC_ELT (method_vec
, 0),
1192 (PTR
) &TREE_VEC_ELT (new_vec
, 0),
1193 len
* sizeof (tree
));
1194 free_method_vec (method_vec
);
1196 method_vec
= CLASSTYPE_METHOD_VEC (type
) = new_vec
;
1199 if (DECL_CONV_FN_P (method
) && !TREE_VEC_ELT (method_vec
, slot
))
1201 /* Type conversion operators have to come before
1202 ordinary methods; add_conversions depends on this to
1203 speed up looking for conversion operators. So, if
1204 necessary, we slide some of the vector elements up.
1205 In theory, this makes this algorithm O(N^2) but we
1206 don't expect many conversion operators. */
1207 for (slot
= 2; slot
< len
; ++slot
)
1209 tree fn
= TREE_VEC_ELT (method_vec
, slot
);
1212 /* There are no more entries in the vector, so we
1213 can insert the new conversion operator here. */
1216 if (!DECL_CONV_FN_P (OVL_CURRENT (fn
)))
1217 /* We can insert the new function right at the
1222 if (!TREE_VEC_ELT (method_vec
, slot
))
1223 /* There is nothing in the Ith slot, so we can avoid
1228 /* We know the last slot in the vector is empty
1229 because we know that at this point there's room
1230 for a new function. */
1231 bcopy ((PTR
) &TREE_VEC_ELT (method_vec
, slot
),
1232 (PTR
) &TREE_VEC_ELT (method_vec
, slot
+ 1),
1233 (len
- slot
- 1) * sizeof (tree
));
1234 TREE_VEC_ELT (method_vec
, slot
) = NULL_TREE
;
1239 if (template_class_depth (type
))
1240 /* TYPE is a template class. Don't issue any errors now; wait
1241 until instantiation time to complain. */
1247 /* Check to see if we've already got this method. */
1248 for (fns
= TREE_VEC_ELT (method_vec
, slot
);
1250 fns
= OVL_NEXT (fns
))
1252 tree fn
= OVL_CURRENT (fns
);
1254 if (TREE_CODE (fn
) != TREE_CODE (method
))
1257 if (TREE_CODE (method
) != TEMPLATE_DECL
)
1259 /* [over.load] Member function declarations with the
1260 same name and the same parameter types cannot be
1261 overloaded if any of them is a static member
1262 function declaration. */
1263 if (DECL_STATIC_FUNCTION_P (fn
)
1264 != DECL_STATIC_FUNCTION_P (method
))
1266 tree parms1
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1267 tree parms2
= TYPE_ARG_TYPES (TREE_TYPE (method
));
1269 if (! DECL_STATIC_FUNCTION_P (fn
))
1270 parms1
= TREE_CHAIN (parms1
);
1272 parms2
= TREE_CHAIN (parms2
);
1274 if (compparms (parms1
, parms2
))
1275 cp_error ("`%#D' and `%#D' cannot be overloaded",
1279 /* Since this is an ordinary function in a
1280 non-template class, it's mangled name can be used
1281 as a unique identifier. This technique is only
1282 an optimization; we would get the same results if
1283 we just used decls_match here. */
1284 if (DECL_ASSEMBLER_NAME (fn
)
1285 != DECL_ASSEMBLER_NAME (method
))
1288 else if (!decls_match (fn
, method
))
1291 /* There has already been a declaration of this method
1292 or member template. */
1293 cp_error_at ("`%D' has already been declared in `%T'",
1296 /* We don't call duplicate_decls here to merge the
1297 declarations because that will confuse things if the
1298 methods have inline definitions. In particular, we
1299 will crash while processing the definitions. */
1304 /* Actually insert the new method. */
1305 TREE_VEC_ELT (method_vec
, slot
)
1306 = build_overload (method
, TREE_VEC_ELT (method_vec
, slot
));
1308 /* Add the new binding. */
1309 if (!DECL_CONSTRUCTOR_P (method
)
1310 && !DECL_DESTRUCTOR_P (method
))
1311 push_class_level_binding (DECL_NAME (method
),
1312 TREE_VEC_ELT (method_vec
, slot
));
1317 /* Subroutines of finish_struct. */
1319 /* Look through the list of fields for this struct, deleting
1320 duplicates as we go. This must be recursive to handle
1323 FIELD is the field which may not appear anywhere in FIELDS.
1324 FIELD_PTR, if non-null, is the starting point at which
1325 chained deletions may take place.
1326 The value returned is the first acceptable entry found
1329 Note that anonymous fields which are not of UNION_TYPE are
1330 not duplicates, they are just anonymous fields. This happens
1331 when we have unnamed bitfields, for example. */
1334 delete_duplicate_fields_1 (field
, fields
)
1339 if (DECL_NAME (field
) == 0)
1341 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1344 for (x
= TYPE_FIELDS (TREE_TYPE (field
)); x
; x
= TREE_CHAIN (x
))
1345 fields
= delete_duplicate_fields_1 (x
, fields
);
1350 for (x
= fields
; x
; prev
= x
, x
= TREE_CHAIN (x
))
1352 if (DECL_NAME (x
) == 0)
1354 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
1356 TYPE_FIELDS (TREE_TYPE (x
))
1357 = delete_duplicate_fields_1 (field
, TYPE_FIELDS (TREE_TYPE (x
)));
1358 if (TYPE_FIELDS (TREE_TYPE (x
)) == 0)
1361 fields
= TREE_CHAIN (fields
);
1363 TREE_CHAIN (prev
) = TREE_CHAIN (x
);
1368 if (DECL_NAME (field
) == DECL_NAME (x
))
1370 if (TREE_CODE (field
) == CONST_DECL
1371 && TREE_CODE (x
) == CONST_DECL
)
1372 cp_error_at ("duplicate enum value `%D'", x
);
1373 else if (TREE_CODE (field
) == CONST_DECL
1374 || TREE_CODE (x
) == CONST_DECL
)
1375 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1377 else if (DECL_DECLARES_TYPE_P (field
)
1378 && DECL_DECLARES_TYPE_P (x
))
1380 if (same_type_p (TREE_TYPE (field
), TREE_TYPE (x
)))
1382 cp_error_at ("duplicate nested type `%D'", x
);
1384 else if (DECL_DECLARES_TYPE_P (field
)
1385 || DECL_DECLARES_TYPE_P (x
))
1387 /* Hide tag decls. */
1388 if ((TREE_CODE (field
) == TYPE_DECL
1389 && DECL_ARTIFICIAL (field
))
1390 || (TREE_CODE (x
) == TYPE_DECL
1391 && DECL_ARTIFICIAL (x
)))
1393 cp_error_at ("duplicate field `%D' (as type and non-type)",
1397 cp_error_at ("duplicate member `%D'", x
);
1399 fields
= TREE_CHAIN (fields
);
1401 TREE_CHAIN (prev
) = TREE_CHAIN (x
);
1410 delete_duplicate_fields (fields
)
1414 for (x
= fields
; x
&& TREE_CHAIN (x
); x
= TREE_CHAIN (x
))
1415 TREE_CHAIN (x
) = delete_duplicate_fields_1 (x
, TREE_CHAIN (x
));
1418 /* Change the access of FDECL to ACCESS in T. The access to FDECL is
1419 along the path given by BINFO. Return 1 if change was legit,
1420 otherwise return 0. */
1423 alter_access (t
, binfo
, fdecl
, access
)
1429 tree elem
= purpose_member (t
, DECL_ACCESS (fdecl
));
1432 if (TREE_VALUE (elem
) != access
)
1434 if (TREE_CODE (TREE_TYPE (fdecl
)) == FUNCTION_DECL
)
1435 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl
));
1437 error ("conflicting access specifications for field `%s', ignored",
1438 IDENTIFIER_POINTER (DECL_NAME (fdecl
)));
1442 /* They're changing the access to the same thing they changed
1443 it to before. That's OK. */
1449 enforce_access (binfo
, fdecl
);
1450 DECL_ACCESS (fdecl
) = tree_cons (t
, access
, DECL_ACCESS (fdecl
));
1456 /* Process the USING_DECL, which is a member of T. The METHOD_VEC, if
1457 non-NULL, is the methods of T. The FIELDS are the fields of T. */
1460 handle_using_decl (using_decl
, t
, method_vec
, fields
)
1466 tree ctype
= DECL_INITIAL (using_decl
);
1467 tree name
= DECL_NAME (using_decl
);
1469 = TREE_PRIVATE (using_decl
) ? access_private_node
1470 : TREE_PROTECTED (using_decl
) ? access_protected_node
1471 : access_public_node
;
1473 tree flist
= NULL_TREE
;
1478 binfo
= binfo_or_else (ctype
, t
);
1482 if (name
== constructor_name (ctype
)
1483 || name
== constructor_name_full (ctype
))
1485 cp_error_at ("using-declaration for constructor", using_decl
);
1489 fdecl
= lookup_member (binfo
, name
, 0, 0);
1493 cp_error_at ("no members matching `%D' in `%#T'", using_decl
, ctype
);
1497 /* Functions are represented as TREE_LIST, with the purpose
1498 being the type and the value the functions. Other members
1499 come as themselves. */
1500 if (TREE_CODE (fdecl
) == TREE_LIST
)
1501 /* Ignore base type this came from. */
1502 fdecl
= TREE_VALUE (fdecl
);
1504 if (TREE_CODE (fdecl
) == OVERLOAD
)
1506 /* We later iterate over all functions. */
1508 fdecl
= OVL_FUNCTION (flist
);
1511 name
= DECL_NAME (fdecl
);
1512 n_methods
= method_vec
? TREE_VEC_LENGTH (method_vec
) : 0;
1513 for (i
= 2; i
< n_methods
&& TREE_VEC_ELT (method_vec
, i
); i
++)
1514 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec
, i
)))
1517 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl
, t
);
1518 cp_error_at (" because of local method `%#D' with same name",
1519 OVL_CURRENT (TREE_VEC_ELT (method_vec
, i
)));
1523 if (! DECL_LANG_SPECIFIC (fdecl
))
1524 /* We don't currently handle DECL_ACCESS for TYPE_DECLs; just return. */
1527 for (tmp
= fields
; tmp
; tmp
= TREE_CHAIN (tmp
))
1528 if (DECL_NAME (tmp
) == name
)
1530 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl
, t
);
1531 cp_error_at (" because of local field `%#D' with same name", tmp
);
1535 /* Make type T see field decl FDECL with access ACCESS.*/
1540 if (alter_access (t
, binfo
, OVL_FUNCTION (flist
),
1543 flist
= OVL_CHAIN (flist
);
1547 alter_access (t
, binfo
, fdecl
, access
);
1553 int max_has_virtual
;
1557 char cant_have_default_ctor
;
1558 char cant_have_const_ctor
;
1559 char no_const_asn_ref
;
1562 /* Record information about type T derived from its base classes.
1563 Store most of that information in T itself, and place the
1564 remaining information in the struct BASE_INFO.
1566 Propagate basetype offsets throughout the lattice. Note that the
1567 lattice topped by T is really a pair: it's a DAG that gives the
1568 structure of the derivation hierarchy, and it's a list of the
1569 virtual baseclasses that appear anywhere in the DAG. When a vbase
1570 type appears in the DAG, it's offset is 0, and it's children start
1571 their offsets from that point. When a vbase type appears in the list,
1572 its offset is the offset it has in the hierarchy, and its children's
1573 offsets include that offset in theirs.
1575 Returns the index of the first base class to have virtual functions,
1576 or -1 if no such base class. */
1579 finish_base_struct (t
, b
)
1581 struct base_info
*b
;
1583 tree binfos
= TYPE_BINFO_BASETYPES (t
);
1584 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
1585 int first_vfn_base_index
= -1;
1586 bzero ((char *) b
, sizeof (struct base_info
));
1588 for (i
= 0; i
< n_baseclasses
; i
++)
1590 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1591 tree basetype
= BINFO_TYPE (base_binfo
);
1593 /* Effective C++ rule 14. We only need to check TYPE_VIRTUAL_P
1594 here because the case of virtual functions but non-virtual
1595 dtor is handled in finish_struct_1. */
1596 if (warn_ecpp
&& ! TYPE_VIRTUAL_P (basetype
)
1597 && TYPE_HAS_DESTRUCTOR (basetype
))
1598 cp_warning ("base class `%#T' has a non-virtual destructor", basetype
);
1600 /* If the type of basetype is incomplete, then
1601 we already complained about that fact
1602 (and we should have fixed it up as well). */
1603 if (TYPE_SIZE (basetype
) == 0)
1606 /* The base type is of incomplete type. It is
1607 probably best to pretend that it does not
1609 if (i
== n_baseclasses
-1)
1610 TREE_VEC_ELT (binfos
, i
) = NULL_TREE
;
1611 TREE_VEC_LENGTH (binfos
) -= 1;
1613 for (j
= i
; j
+1 < n_baseclasses
; j
++)
1614 TREE_VEC_ELT (binfos
, j
) = TREE_VEC_ELT (binfos
, j
+1);
1617 if (! TYPE_HAS_CONST_INIT_REF (basetype
))
1618 b
->cant_have_const_ctor
= 1;
1620 if (TYPE_HAS_CONSTRUCTOR (basetype
)
1621 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
))
1623 b
->cant_have_default_ctor
= 1;
1624 if (! TYPE_HAS_CONSTRUCTOR (t
))
1626 cp_pedwarn ("base `%T' with only non-default constructor",
1628 cp_pedwarn ("in class without a constructor");
1632 if (TYPE_HAS_ASSIGN_REF (basetype
)
1633 && !TYPE_HAS_CONST_ASSIGN_REF (basetype
))
1634 b
->no_const_asn_ref
= 1;
1636 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (basetype
);
1637 TYPE_NEEDS_DESTRUCTOR (t
) |= TYPE_NEEDS_DESTRUCTOR (basetype
);
1638 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype
);
1639 TYPE_HAS_COMPLEX_INIT_REF (t
) |= TYPE_HAS_COMPLEX_INIT_REF (basetype
);
1641 TYPE_OVERLOADS_CALL_EXPR (t
) |= TYPE_OVERLOADS_CALL_EXPR (basetype
);
1642 TYPE_OVERLOADS_ARRAY_REF (t
) |= TYPE_OVERLOADS_ARRAY_REF (basetype
);
1643 TYPE_OVERLOADS_ARROW (t
) |= TYPE_OVERLOADS_ARROW (basetype
);
1645 if (CLASSTYPE_COM_INTERFACE (basetype
))
1647 CLASSTYPE_COM_INTERFACE (t
) = 1;
1650 ("COM interface type `%T' must be the leftmost base class",
1653 else if (CLASSTYPE_COM_INTERFACE (t
) && i
== 0)
1655 cp_error ("COM interface type `%T' with non-COM base class `%T'",
1657 CLASSTYPE_COM_INTERFACE (t
) = 0;
1660 if (TYPE_VIRTUAL_P (basetype
))
1662 /* Ensure that this is set from at least a virtual base
1664 if (b
->rtti
== NULL_TREE
)
1665 b
->rtti
= CLASSTYPE_RTTI (basetype
);
1667 /* Don't borrow virtuals from virtual baseclasses. */
1668 if (TREE_VIA_VIRTUAL (base_binfo
))
1671 if (first_vfn_base_index
< 0)
1674 first_vfn_base_index
= i
;
1676 /* Update these two, now that we know what vtable we are
1677 going to extend. This is so that we can add virtual
1678 functions, and override them properly. */
1679 TYPE_BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (basetype
);
1680 TYPE_BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (basetype
);
1681 b
->has_virtual
= CLASSTYPE_VSIZE (basetype
);
1682 b
->vfield
= CLASSTYPE_VFIELD (basetype
);
1683 b
->vfields
= copy_list (CLASSTYPE_VFIELDS (basetype
));
1684 vfields
= b
->vfields
;
1687 if (VF_BINFO_VALUE (vfields
) == NULL_TREE
1688 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields
)))
1690 tree value
= VF_BASETYPE_VALUE (vfields
);
1691 if (DECL_NAME (CLASSTYPE_VFIELD (value
))
1692 == DECL_NAME (CLASSTYPE_VFIELD (basetype
)))
1693 VF_NORMAL_VALUE (b
->vfields
) = basetype
;
1695 VF_NORMAL_VALUE (b
->vfields
) = VF_NORMAL_VALUE (vfields
);
1697 vfields
= TREE_CHAIN (vfields
);
1699 CLASSTYPE_VFIELD (t
) = b
->vfield
;
1703 /* Only add unique vfields, and flatten them out as we go. */
1704 tree vfields
= CLASSTYPE_VFIELDS (basetype
);
1707 if (VF_BINFO_VALUE (vfields
) == NULL_TREE
1708 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields
)))
1710 tree value
= VF_BASETYPE_VALUE (vfields
);
1711 b
->vfields
= tree_cons (base_binfo
, value
, b
->vfields
);
1712 if (DECL_NAME (CLASSTYPE_VFIELD (value
))
1713 == DECL_NAME (CLASSTYPE_VFIELD (basetype
)))
1714 VF_NORMAL_VALUE (b
->vfields
) = basetype
;
1716 VF_NORMAL_VALUE (b
->vfields
) = VF_NORMAL_VALUE (vfields
);
1718 vfields
= TREE_CHAIN (vfields
);
1721 if (b
->has_virtual
== 0)
1723 first_vfn_base_index
= i
;
1725 /* Update these two, now that we know what vtable we are
1726 going to extend. This is so that we can add virtual
1727 functions, and override them properly. */
1728 TYPE_BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (basetype
);
1729 TYPE_BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (basetype
);
1730 b
->has_virtual
= CLASSTYPE_VSIZE (basetype
);
1731 b
->vfield
= CLASSTYPE_VFIELD (basetype
);
1732 CLASSTYPE_VFIELD (t
) = b
->vfield
;
1733 /* When we install the first one, set the VF_NORMAL_VALUE
1734 to be the current class, as this it is the most derived
1735 class. Hopefully, this is not set to something else
1737 vfields
= b
->vfields
;
1740 if (DECL_NAME (CLASSTYPE_VFIELD (t
))
1741 == DECL_NAME (CLASSTYPE_VFIELD (basetype
)))
1743 VF_NORMAL_VALUE (vfields
) = t
;
1744 /* There should only be one of them! And it should
1745 always be found, if we get into here. (mrs) */
1748 vfields
= TREE_CHAIN (vfields
);
1757 /* Find the base class with the largest number of virtual functions. */
1758 for (vfields
= b
->vfields
; vfields
; vfields
= TREE_CHAIN (vfields
))
1760 if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields
)) > b
->max_has_virtual
)
1761 b
->max_has_virtual
= CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields
));
1762 if (VF_DERIVED_VALUE (vfields
)
1763 && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields
)) > b
->max_has_virtual
)
1764 b
->max_has_virtual
= CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields
));
1769 /* If all virtual functions come only from virtual baseclasses. */
1772 /* Update the rtti base if we have a non-virtual base class version
1774 b
->rtti
= CLASSTYPE_RTTI (BINFO_TYPE (TREE_VEC_ELT (binfos
, first_vfn_base_index
)));
1776 return first_vfn_base_index
;
1779 /* Set memoizing fields and bits of T (and its variants) for later use.
1780 MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables. */
1783 finish_struct_bits (t
, max_has_virtual
)
1785 int max_has_virtual
;
1787 int i
, n_baseclasses
= CLASSTYPE_N_BASECLASSES (t
);
1789 /* Fix up variants (if any). */
1790 tree variants
= TYPE_NEXT_VARIANT (t
);
1793 /* These fields are in the _TYPE part of the node, not in
1794 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1795 TYPE_HAS_CONSTRUCTOR (variants
) = TYPE_HAS_CONSTRUCTOR (t
);
1796 TYPE_HAS_DESTRUCTOR (variants
) = TYPE_HAS_DESTRUCTOR (t
);
1797 TYPE_NEEDS_CONSTRUCTING (variants
) = TYPE_NEEDS_CONSTRUCTING (t
);
1798 TYPE_NEEDS_DESTRUCTOR (variants
) = TYPE_NEEDS_DESTRUCTOR (t
);
1800 TYPE_USES_COMPLEX_INHERITANCE (variants
) = TYPE_USES_COMPLEX_INHERITANCE (t
);
1801 TYPE_VIRTUAL_P (variants
) = TYPE_VIRTUAL_P (t
);
1802 TYPE_USES_VIRTUAL_BASECLASSES (variants
) = TYPE_USES_VIRTUAL_BASECLASSES (t
);
1803 /* Copy whatever these are holding today. */
1804 TYPE_MIN_VALUE (variants
) = TYPE_MIN_VALUE (t
);
1805 TYPE_MAX_VALUE (variants
) = TYPE_MAX_VALUE (t
);
1806 TYPE_FIELDS (variants
) = TYPE_FIELDS (t
);
1807 TYPE_SIZE (variants
) = TYPE_SIZE (t
);
1808 TYPE_SIZE_UNIT (variants
) = TYPE_SIZE_UNIT (t
);
1809 variants
= TYPE_NEXT_VARIANT (variants
);
1812 if (n_baseclasses
&& max_has_virtual
)
1814 /* For a class w/o baseclasses, `finish_struct' has set
1815 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by definition). Similarly
1816 for a class who's base classes do not have vtables. When neither
1817 of these is true, we might have removed abstract virtuals (by
1818 providing a definition), added some (by declaring new ones), or
1819 redeclared ones from a base class. We need to recalculate what's
1820 really an abstract virtual at this point (by looking in the
1822 CLASSTYPE_ABSTRACT_VIRTUALS (t
) = get_abstract_virtuals (t
);
1827 /* Notice whether this class has type conversion functions defined. */
1828 tree binfo
= TYPE_BINFO (t
);
1829 tree binfos
= BINFO_BASETYPES (binfo
);
1832 for (i
= n_baseclasses
-1; i
>= 0; i
--)
1834 basetype
= BINFO_TYPE (TREE_VEC_ELT (binfos
, i
));
1836 TYPE_HAS_CONVERSION (t
) |= TYPE_HAS_CONVERSION (basetype
);
1840 /* If this type has a copy constructor, force its mode to be BLKmode, and
1841 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1842 be passed by invisible reference and prevent it from being returned in
1845 Also do this if the class has BLKmode but can still be returned in
1846 registers, since function_cannot_inline_p won't let us inline
1847 functions returning such a type. This affects the HP-PA. */
1848 if (! TYPE_HAS_TRIVIAL_INIT_REF (t
)
1849 || (TYPE_MODE (t
) == BLKmode
&& ! aggregate_value_p (t
)
1850 && CLASSTYPE_NON_AGGREGATE (t
)))
1853 DECL_MODE (TYPE_MAIN_DECL (t
)) = BLKmode
;
1854 for (variants
= t
; variants
; variants
= TYPE_NEXT_VARIANT (variants
))
1856 TYPE_MODE (variants
) = BLKmode
;
1857 TREE_ADDRESSABLE (variants
) = 1;
1862 /* Issue warnings about T having private constructors, but no friends,
1865 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1866 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1867 non-private static member functions. */
1870 maybe_warn_about_overly_private_class (t
)
1873 int has_member_fn
= 0;
1874 int has_nonprivate_method
= 0;
1877 if (!warn_ctor_dtor_privacy
1878 /* If the class has friends, those entities might create and
1879 access instances, so we should not warn. */
1880 || (CLASSTYPE_FRIEND_CLASSES (t
)
1881 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
1882 /* We will have warned when the template was declared; there's
1883 no need to warn on every instantiation. */
1884 || CLASSTYPE_TEMPLATE_INSTANTIATION (t
))
1885 /* There's no reason to even consider warning about this
1889 /* We only issue one warning, if more than one applies, because
1890 otherwise, on code like:
1893 // Oops - forgot `public:'
1899 we warn several times about essentially the same problem. */
1901 /* Check to see if all (non-constructor, non-destructor) member
1902 functions are private. (Since there are no friends or
1903 non-private statics, we can't ever call any of the private member
1905 for (fn
= TYPE_METHODS (t
); fn
; fn
= TREE_CHAIN (fn
))
1906 /* We're not interested in compiler-generated methods; they don't
1907 provide any way to call private members. */
1908 if (!DECL_ARTIFICIAL (fn
))
1910 if (!TREE_PRIVATE (fn
))
1912 if (DECL_STATIC_FUNCTION_P (fn
))
1913 /* A non-private static member function is just like a
1914 friend; it can create and invoke private member
1915 functions, and be accessed without a class
1919 has_nonprivate_method
= 1;
1922 else if (!DECL_CONSTRUCTOR_P (fn
) && !DECL_DESTRUCTOR_P (fn
))
1926 if (!has_nonprivate_method
&& has_member_fn
)
1928 /* There are no non-private methods, and there's at least one
1929 private member function that isn't a constructor or
1930 destructor. (If all the private members are
1931 constructors/destructors we want to use the code below that
1932 issues error messages specifically referring to
1933 constructors/destructors.) */
1935 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
1936 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (t
); i
++)
1937 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos
, i
))
1938 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos
, i
)))
1940 has_nonprivate_method
= 1;
1943 if (!has_nonprivate_method
)
1945 cp_warning ("all member functions in class `%T' are private", t
);
1950 /* Even if some of the member functions are non-private, the class
1951 won't be useful for much if all the constructors or destructors
1952 are private: such an object can never be created or destroyed. */
1953 if (TYPE_HAS_DESTRUCTOR (t
))
1955 tree dtor
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 1);
1957 if (TREE_PRIVATE (dtor
))
1959 cp_warning ("`%#T' only defines a private destructor and has no friends",
1965 if (TYPE_HAS_CONSTRUCTOR (t
))
1967 int nonprivate_ctor
= 0;
1969 /* If a non-template class does not define a copy
1970 constructor, one is defined for it, enabling it to avoid
1971 this warning. For a template class, this does not
1972 happen, and so we would normally get a warning on:
1974 template <class T> class C { private: C(); };
1976 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1977 complete non-template or fully instantiated classes have this
1979 if (!TYPE_HAS_INIT_REF (t
))
1980 nonprivate_ctor
= 1;
1982 for (fn
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 0);
1986 tree ctor
= OVL_CURRENT (fn
);
1987 /* Ideally, we wouldn't count copy constructors (or, in
1988 fact, any constructor that takes an argument of the
1989 class type as a parameter) because such things cannot
1990 be used to construct an instance of the class unless
1991 you already have one. But, for now at least, we're
1993 if (! TREE_PRIVATE (ctor
))
1995 nonprivate_ctor
= 1;
2000 if (nonprivate_ctor
== 0)
2002 cp_warning ("`%#T' only defines private constructors and has no friends",
2009 /* Function to help qsort sort FIELD_DECLs by name order. */
2012 field_decl_cmp (x
, y
)
2015 if (DECL_NAME (*x
) == DECL_NAME (*y
))
2017 if (DECL_NAME (*x
) == NULL_TREE
)
2019 if (DECL_NAME (*y
) == NULL_TREE
)
2021 if (DECL_NAME (*x
) < DECL_NAME (*y
))
2026 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2029 method_name_cmp (m1
, m2
)
2030 const tree
*m1
, *m2
;
2032 if (*m1
== NULL_TREE
&& *m2
== NULL_TREE
)
2034 if (*m1
== NULL_TREE
)
2036 if (*m2
== NULL_TREE
)
2038 if (DECL_NAME (OVL_CURRENT (*m1
)) < DECL_NAME (OVL_CURRENT (*m2
)))
2043 /* Warn about duplicate methods in fn_fields. Also compact method
2044 lists so that lookup can be made faster.
2046 Data Structure: List of method lists. The outer list is a
2047 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2048 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2049 links the entire list of methods for TYPE_METHODS. Friends are
2050 chained in the same way as member functions (? TREE_CHAIN or
2051 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2052 list. That allows them to be quickly deleted, and requires no
2055 If there are any constructors/destructors, they are moved to the
2056 front of the list. This makes pushclass more efficient.
2058 @@ The above comment is obsolete. It mostly describes what add_method
2059 @@ and add_implicitly_declared_members do.
2061 Sort methods that are not special (i.e., constructors, destructors, and
2062 type conversion operators) so that we can find them faster in search. */
2065 finish_struct_methods (t
)
2069 tree method_vec
= CLASSTYPE_METHOD_VEC (t
);
2070 tree ctor_name
= constructor_name (t
);
2071 int slot
, len
= method_vec
? TREE_VEC_LENGTH (method_vec
) : 0;
2073 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2074 and the next few with type conversion operators (if any). */
2075 for (fn_fields
= TYPE_METHODS (t
); fn_fields
;
2076 fn_fields
= TREE_CHAIN (fn_fields
))
2078 tree fn_name
= DECL_NAME (fn_fields
);
2080 /* Clear out this flag.
2082 @@ Doug may figure out how to break
2083 @@ this with nested classes and friends. */
2084 DECL_IN_AGGR_P (fn_fields
) = 0;
2086 /* Note here that a copy ctor is private, so we don't dare generate
2087 a default copy constructor for a class that has a member
2088 of this type without making sure they have access to it. */
2089 if (fn_name
== ctor_name
)
2091 tree parmtypes
= FUNCTION_ARG_CHAIN (fn_fields
);
2092 tree parmtype
= parmtypes
? TREE_VALUE (parmtypes
) : void_type_node
;
2094 if (TREE_CODE (parmtype
) == REFERENCE_TYPE
2095 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype
)) == t
)
2097 if (TREE_CHAIN (parmtypes
) == NULL_TREE
2098 || TREE_CHAIN (parmtypes
) == void_list_node
2099 || TREE_PURPOSE (TREE_CHAIN (parmtypes
)))
2101 if (TREE_PROTECTED (fn_fields
))
2102 TYPE_HAS_NONPUBLIC_CTOR (t
) = 1;
2103 else if (TREE_PRIVATE (fn_fields
))
2104 TYPE_HAS_NONPUBLIC_CTOR (t
) = 2;
2108 else if (fn_name
== ansi_opname
[(int) MODIFY_EXPR
])
2110 tree parmtype
= TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields
));
2112 if (copy_assignment_arg_p (parmtype
, DECL_VIRTUAL_P (fn_fields
)))
2114 if (TREE_PROTECTED (fn_fields
))
2115 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t
) = 1;
2116 else if (TREE_PRIVATE (fn_fields
))
2117 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t
) = 2;
2122 if (TYPE_HAS_DESTRUCTOR (t
) && !TREE_VEC_ELT (method_vec
, 1))
2123 /* We thought there was a destructor, but there wasn't. Some
2124 parse errors cause this anomalous situation. */
2125 TYPE_HAS_DESTRUCTOR (t
) = 0;
2127 /* Issue warnings about private constructors and such. If there are
2128 no methods, then some public defaults are generated. */
2129 maybe_warn_about_overly_private_class (t
);
2131 if (method_vec
== NULL_TREE
)
2134 /* Now sort the methods. */
2135 while (len
> 2 && TREE_VEC_ELT (method_vec
, len
-1) == NULL_TREE
)
2137 TREE_VEC_LENGTH (method_vec
) = len
;
2139 /* The type conversion ops have to live at the front of the vec, so we
2141 for (slot
= 2; slot
< len
; ++slot
)
2143 tree fn
= TREE_VEC_ELT (method_vec
, slot
);
2145 if (!DECL_CONV_FN_P (OVL_CURRENT (fn
)))
2149 qsort (&TREE_VEC_ELT (method_vec
, slot
), len
-slot
, sizeof (tree
),
2150 (int (*)(const void *, const void *))method_name_cmp
);
2153 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2156 duplicate_tag_error (t
)
2159 cp_error ("redefinition of `%#T'", t
);
2160 cp_error_at ("previous definition here", t
);
2162 /* Pretend we haven't defined this type. */
2164 /* All of the component_decl's were TREE_CHAINed together in the parser.
2165 finish_struct_methods walks these chains and assembles all methods with
2166 the same base name into DECL_CHAINs. Now we don't need the parser chains
2167 anymore, so we unravel them. */
2169 /* This used to be in finish_struct, but it turns out that the
2170 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2172 if (CLASSTYPE_METHOD_VEC (t
))
2174 tree method_vec
= CLASSTYPE_METHOD_VEC (t
);
2175 int i
, len
= TREE_VEC_LENGTH (method_vec
);
2176 for (i
= 0; i
< len
; i
++)
2178 tree unchain
= TREE_VEC_ELT (method_vec
, i
);
2179 while (unchain
!= NULL_TREE
)
2181 TREE_CHAIN (OVL_CURRENT (unchain
)) = NULL_TREE
;
2182 unchain
= OVL_NEXT (unchain
);
2187 if (TYPE_LANG_SPECIFIC (t
))
2189 tree binfo
= TYPE_BINFO (t
);
2190 int interface_only
= CLASSTYPE_INTERFACE_ONLY (t
);
2191 int interface_unknown
= CLASSTYPE_INTERFACE_UNKNOWN (t
);
2193 bzero ((char *) TYPE_LANG_SPECIFIC (t
), sizeof (struct lang_type
));
2194 BINFO_BASETYPES(binfo
) = NULL_TREE
;
2196 TYPE_BINFO (t
) = binfo
;
2197 CLASSTYPE_INTERFACE_ONLY (t
) = interface_only
;
2198 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t
, interface_unknown
);
2199 TYPE_REDEFINED (t
) = 1;
2201 TYPE_SIZE (t
) = NULL_TREE
;
2202 TYPE_MODE (t
) = VOIDmode
;
2203 TYPE_FIELDS (t
) = NULL_TREE
;
2204 TYPE_METHODS (t
) = NULL_TREE
;
2205 TYPE_VFIELD (t
) = NULL_TREE
;
2206 TYPE_CONTEXT (t
) = NULL_TREE
;
2207 TYPE_NONCOPIED_PARTS (t
) = NULL_TREE
;
2210 /* finish up all new vtables. */
2213 finish_vtbls (binfo
, do_self
, t
)
2218 tree binfos
= BINFO_BASETYPES (binfo
);
2219 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2221 /* Should we use something besides CLASSTYPE_VFIELDS? */
2222 if (do_self
&& CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
2224 if (BINFO_NEW_VTABLE_MARKED (binfo
))
2228 decl
= BINFO_VTABLE (binfo
);
2229 context
= DECL_CONTEXT (decl
);
2230 DECL_CONTEXT (decl
) = 0;
2232 /* We make a copy here in case we need to replace pure
2233 virtual functions with __pure_virtual. We don't want to
2234 mess up BINFO_VIRTUALS when we do this. */
2235 DECL_INITIAL (decl
) = copy_list (BINFO_VIRTUALS (binfo
));
2236 DECL_INITIAL (decl
) = build_nt (CONSTRUCTOR
, NULL_TREE
,
2237 DECL_INITIAL (decl
));
2239 cp_finish_decl (decl
, DECL_INITIAL (decl
), NULL_TREE
, 0, 0);
2240 DECL_CONTEXT (decl
) = context
;
2242 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo
);
2245 for (i
= 0; i
< n_baselinks
; i
++)
2247 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2248 int is_not_base_vtable
2249 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
2250 if (TREE_VIA_VIRTUAL (base_binfo
))
2252 base_binfo
= binfo_member (BINFO_TYPE (base_binfo
), CLASSTYPE_VBASECLASSES (t
));
2254 finish_vtbls (base_binfo
, is_not_base_vtable
, t
);
2258 /* True if we should override the given BASE_FNDECL with the given
2262 overrides (fndecl
, base_fndecl
)
2263 tree fndecl
, base_fndecl
;
2265 /* Destructors have special names. */
2266 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl
))
2267 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl
)))
2269 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl
))
2270 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl
)))
2272 if (DECL_NAME (fndecl
) == DECL_NAME (base_fndecl
))
2274 tree types
, base_types
;
2276 retypes
= TREE_TYPE (TREE_TYPE (fndecl
));
2277 base_retypes
= TREE_TYPE (TREE_TYPE (base_fndecl
));
2279 types
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
2280 base_types
= TYPE_ARG_TYPES (TREE_TYPE (base_fndecl
));
2281 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types
)))
2282 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types
))))
2283 && compparms (TREE_CHAIN (base_types
), TREE_CHAIN (types
)))
2290 get_class_offset_1 (parent
, binfo
, context
, t
, fndecl
)
2291 tree parent
, binfo
, context
, t
, fndecl
;
2293 tree binfos
= BINFO_BASETYPES (binfo
);
2294 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2295 tree rval
= NULL_TREE
;
2297 if (binfo
== parent
)
2298 return error_mark_node
;
2300 for (i
= 0; i
< n_baselinks
; i
++)
2302 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2305 if (TREE_VIA_VIRTUAL (base_binfo
))
2306 base_binfo
= binfo_member (BINFO_TYPE (base_binfo
),
2307 CLASSTYPE_VBASECLASSES (t
));
2308 nrval
= get_class_offset_1 (parent
, base_binfo
, context
, t
, fndecl
);
2309 /* See if we have a new value */
2310 if (nrval
&& (nrval
!= error_mark_node
|| rval
==0))
2312 /* Only compare if we have two offsets */
2313 if (rval
&& rval
!= error_mark_node
2314 && ! tree_int_cst_equal (nrval
, rval
))
2316 /* Only give error if the two offsets are different */
2317 error ("every virtual function must have a unique final overrider");
2318 cp_error (" found two (or more) `%T' class subobjects in `%T'", context
, t
);
2319 cp_error (" with virtual `%D' from virtual base class", fndecl
);
2325 if (rval
&& BINFO_TYPE (binfo
) == context
)
2327 my_friendly_assert (rval
== error_mark_node
2328 || tree_int_cst_equal (rval
, BINFO_OFFSET (binfo
)), 999);
2329 rval
= BINFO_OFFSET (binfo
);
2335 /* Get the offset to the CONTEXT subobject that is related to the
2339 get_class_offset (context
, t
, binfo
, fndecl
)
2340 tree context
, t
, binfo
, fndecl
;
2342 tree first_binfo
= binfo
;
2347 return integer_zero_node
;
2349 if (BINFO_TYPE (binfo
) == context
)
2350 return BINFO_OFFSET (binfo
);
2352 /* Check less derived binfos first. */
2353 while (BINFO_BASETYPES (binfo
)
2354 && (i
=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
))) != -1)
2356 tree binfos
= BINFO_BASETYPES (binfo
);
2357 binfo
= TREE_VEC_ELT (binfos
, i
);
2358 if (BINFO_TYPE (binfo
) == context
)
2359 return BINFO_OFFSET (binfo
);
2362 /* Ok, not found in the less derived binfos, now check the more
2364 offset
= get_class_offset_1 (first_binfo
, TYPE_BINFO (t
), context
, t
, fndecl
);
2365 if (offset
==0 || TREE_CODE (offset
) != INTEGER_CST
)
2366 my_friendly_abort (999); /* we have to find it. */
2370 /* Skip RTTI information at the front of the virtual list. */
2372 unsigned HOST_WIDE_INT
2373 skip_rtti_stuff (virtuals
, t
)
2378 if (CLASSTYPE_COM_INTERFACE (t
))
2384 /* We always reserve a slot for the offset/tdesc entry. */
2386 *virtuals
= TREE_CHAIN (*virtuals
);
2388 if (flag_vtable_thunks
&& *virtuals
)
2390 /* The second slot is reserved for the tdesc pointer when thunks
2393 *virtuals
= TREE_CHAIN (*virtuals
);
2399 modify_one_vtable (binfo
, t
, fndecl
, pfn
)
2400 tree binfo
, t
, fndecl
, pfn
;
2402 tree virtuals
= BINFO_VIRTUALS (binfo
);
2403 unsigned HOST_WIDE_INT n
;
2405 /* update rtti entry */
2408 if (binfo
== TYPE_BINFO (t
))
2410 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2411 build_vtable (TYPE_BINFO (DECL_CONTEXT (CLASSTYPE_VFIELD (t
))), t
);
2415 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2416 prepare_fresh_vtable (binfo
, t
);
2419 if (fndecl
== NULL_TREE
)
2422 n
= skip_rtti_stuff (&virtuals
, BINFO_TYPE (binfo
));
2426 tree current_fndecl
= TREE_VALUE (virtuals
);
2427 current_fndecl
= FNADDR_FROM_VTABLE_ENTRY (current_fndecl
);
2428 current_fndecl
= TREE_OPERAND (current_fndecl
, 0);
2429 if (current_fndecl
&& overrides (fndecl
, current_fndecl
))
2431 tree base_offset
, offset
;
2432 tree context
= DECL_CLASS_CONTEXT (fndecl
);
2433 tree vfield
= CLASSTYPE_VFIELD (t
);
2436 offset
= get_class_offset (context
, t
, binfo
, fndecl
);
2438 /* Find the right offset for the this pointer based on the
2439 base class we just found. We have to take into
2440 consideration the virtual base class pointers that we
2441 stick in before the virtual function table pointer.
2443 Also, we want just the delta between the most base class
2444 that we derived this vfield from and us. */
2445 base_offset
= size_binop (PLUS_EXPR
,
2446 get_derived_offset (binfo
, DECL_CONTEXT (current_fndecl
)),
2447 BINFO_OFFSET (binfo
));
2448 this_offset
= ssize_binop (MINUS_EXPR
, offset
, base_offset
);
2450 if (binfo
== TYPE_BINFO (t
))
2452 /* In this case, it is *type*'s vtable we are modifying.
2453 We start with the approximation that it's vtable is that
2454 of the immediate base class. */
2455 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2456 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield
)), t
);
2460 /* This is our very own copy of `basetype' to play with.
2461 Later, we will fill in all the virtual functions
2462 that override the virtual functions in these base classes
2463 which are not defined by the current type. */
2464 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2465 prepare_fresh_vtable (binfo
, t
);
2469 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo
)));
2471 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo
), n
),
2472 build_vtable_entry (this_offset
, pfn
),
2476 virtuals
= TREE_CHAIN (virtuals
);
2480 /* These are the ones that are not through virtual base classes. */
2483 modify_all_direct_vtables (binfo
, do_self
, t
, fndecl
, pfn
)
2486 tree t
, fndecl
, pfn
;
2488 tree binfos
= BINFO_BASETYPES (binfo
);
2489 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2491 /* Should we use something besides CLASSTYPE_VFIELDS? */
2492 if (do_self
&& CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
2494 modify_one_vtable (binfo
, t
, fndecl
, pfn
);
2497 for (i
= 0; i
< n_baselinks
; i
++)
2499 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2500 int is_not_base_vtable
2501 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
2502 if (! TREE_VIA_VIRTUAL (base_binfo
))
2503 modify_all_direct_vtables (base_binfo
, is_not_base_vtable
, t
, fndecl
, pfn
);
2507 /* Fixup all the delta entries in this one vtable that need updating. */
2510 fixup_vtable_deltas1 (binfo
, t
)
2513 tree virtuals
= BINFO_VIRTUALS (binfo
);
2514 unsigned HOST_WIDE_INT n
;
2516 n
= skip_rtti_stuff (&virtuals
, BINFO_TYPE (binfo
));
2520 tree fndecl
= TREE_VALUE (virtuals
);
2521 tree pfn
= FNADDR_FROM_VTABLE_ENTRY (fndecl
);
2522 tree delta
= DELTA_FROM_VTABLE_ENTRY (fndecl
);
2523 fndecl
= TREE_OPERAND (pfn
, 0);
2526 tree base_offset
, offset
;
2527 tree context
= DECL_CLASS_CONTEXT (fndecl
);
2528 tree vfield
= CLASSTYPE_VFIELD (t
);
2531 offset
= get_class_offset (context
, t
, binfo
, fndecl
);
2533 /* Find the right offset for the this pointer based on the
2534 base class we just found. We have to take into
2535 consideration the virtual base class pointers that we
2536 stick in before the virtual function table pointer.
2538 Also, we want just the delta between the most base class
2539 that we derived this vfield from and us. */
2540 base_offset
= size_binop (PLUS_EXPR
,
2541 get_derived_offset (binfo
,
2542 DECL_CONTEXT (fndecl
)),
2543 BINFO_OFFSET (binfo
));
2544 this_offset
= ssize_binop (MINUS_EXPR
, offset
, base_offset
);
2546 if (! tree_int_cst_equal (this_offset
, delta
))
2548 /* Make sure we can modify the derived association with immunity. */
2549 if (binfo
== TYPE_BINFO (t
))
2551 /* In this case, it is *type*'s vtable we are modifying.
2552 We start with the approximation that it's vtable is that
2553 of the immediate base class. */
2554 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2555 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield
)), t
);
2559 /* This is our very own copy of `basetype' to play with.
2560 Later, we will fill in all the virtual functions
2561 that override the virtual functions in these base classes
2562 which are not defined by the current type. */
2563 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2564 prepare_fresh_vtable (binfo
, t
);
2567 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo
), n
),
2568 build_vtable_entry (this_offset
, pfn
),
2573 virtuals
= TREE_CHAIN (virtuals
);
2577 /* Fixup all the delta entries in all the direct vtables that need updating.
2578 This happens when we have non-overridden virtual functions from a
2579 virtual base class, that are at a different offset, in the new
2580 hierarchy, because the layout of the virtual bases has changed. */
2583 fixup_vtable_deltas (binfo
, init_self
, t
)
2588 tree binfos
= BINFO_BASETYPES (binfo
);
2589 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2591 for (i
= 0; i
< n_baselinks
; i
++)
2593 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2594 int is_not_base_vtable
2595 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
2596 if (! TREE_VIA_VIRTUAL (base_binfo
))
2597 fixup_vtable_deltas (base_binfo
, is_not_base_vtable
, t
);
2599 /* Should we use something besides CLASSTYPE_VFIELDS? */
2600 if (init_self
&& CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
2602 fixup_vtable_deltas1 (binfo
, t
);
2606 /* These are the ones that are through virtual base classes. */
2609 modify_all_indirect_vtables (binfo
, do_self
, via_virtual
, t
, fndecl
, pfn
)
2611 int do_self
, via_virtual
;
2612 tree t
, fndecl
, pfn
;
2614 tree binfos
= BINFO_BASETYPES (binfo
);
2615 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2617 /* Should we use something besides CLASSTYPE_VFIELDS? */
2618 if (do_self
&& via_virtual
&& CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
2620 modify_one_vtable (binfo
, t
, fndecl
, pfn
);
2623 for (i
= 0; i
< n_baselinks
; i
++)
2625 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2626 int is_not_base_vtable
2627 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
2628 if (TREE_VIA_VIRTUAL (base_binfo
))
2631 base_binfo
= binfo_member (BINFO_TYPE (base_binfo
), CLASSTYPE_VBASECLASSES (t
));
2633 modify_all_indirect_vtables (base_binfo
, is_not_base_vtable
, via_virtual
, t
, fndecl
, pfn
);
2638 modify_all_vtables (t
, fndecl
, vfn
)
2639 tree t
, fndecl
, vfn
;
2641 /* Do these first, so that we will make use of any non-virtual class's
2642 vtable, over a virtual classes vtable. */
2643 modify_all_direct_vtables (TYPE_BINFO (t
), 1, t
, fndecl
, vfn
);
2644 if (TYPE_USES_VIRTUAL_BASECLASSES (t
))
2645 modify_all_indirect_vtables (TYPE_BINFO (t
), 1, 0, t
, fndecl
, vfn
);
2648 /* Here, we already know that they match in every respect.
2649 All we have to check is where they had their declarations. */
2652 strictly_overrides (fndecl1
, fndecl2
)
2653 tree fndecl1
, fndecl2
;
2655 int distance
= get_base_distance (DECL_CLASS_CONTEXT (fndecl2
),
2656 DECL_CLASS_CONTEXT (fndecl1
),
2658 if (distance
== -2 || distance
> 0)
2663 /* Merge overrides for one vtable.
2664 If we want to merge in same function, we are fine.
2666 if one has a DECL_CLASS_CONTEXT that is a parent of the
2667 other, than choose the more derived one
2669 potentially ill-formed (see 10.3 [class.virtual])
2670 we have to check later to see if there was an
2671 override in this class. If there was ok, if not
2672 then it is ill-formed. (mrs)
2674 We take special care to reuse a vtable, if we can. */
2677 override_one_vtable (binfo
, old
, t
)
2680 tree virtuals
= BINFO_VIRTUALS (binfo
);
2681 tree old_virtuals
= BINFO_VIRTUALS (old
);
2682 enum { REUSE_NEW
, REUSE_OLD
, UNDECIDED
, NEITHER
} choose
= UNDECIDED
;
2684 /* If we have already committed to modifying it, then don't try and
2685 reuse another vtable. */
2686 if (BINFO_NEW_VTABLE_MARKED (binfo
))
2689 skip_rtti_stuff (&virtuals
, BINFO_TYPE (binfo
));
2690 skip_rtti_stuff (&old_virtuals
, BINFO_TYPE (binfo
));
2694 tree fndecl
= TREE_VALUE (virtuals
);
2695 tree old_fndecl
= TREE_VALUE (old_virtuals
);
2696 fndecl
= FNADDR_FROM_VTABLE_ENTRY (fndecl
);
2697 old_fndecl
= FNADDR_FROM_VTABLE_ENTRY (old_fndecl
);
2698 fndecl
= TREE_OPERAND (fndecl
, 0);
2699 old_fndecl
= TREE_OPERAND (old_fndecl
, 0);
2700 /* First check to see if they are the same. */
2701 if (DECL_ASSEMBLER_NAME (fndecl
) == DECL_ASSEMBLER_NAME (old_fndecl
))
2703 /* No need to do anything. */
2705 else if (strictly_overrides (fndecl
, old_fndecl
))
2707 if (choose
== UNDECIDED
)
2709 else if (choose
== REUSE_OLD
)
2712 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2714 prepare_fresh_vtable (binfo
, t
);
2715 override_one_vtable (binfo
, old
, t
);
2720 else if (strictly_overrides (old_fndecl
, fndecl
))
2722 if (choose
== UNDECIDED
)
2724 else if (choose
== REUSE_NEW
)
2727 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2729 prepare_fresh_vtable (binfo
, t
);
2730 override_one_vtable (binfo
, old
, t
);
2733 TREE_VALUE (virtuals
) = TREE_VALUE (old_virtuals
);
2735 else if (choose
== NEITHER
)
2737 TREE_VALUE (virtuals
) = TREE_VALUE (old_virtuals
);
2743 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2745 prepare_fresh_vtable (binfo
, t
);
2746 override_one_vtable (binfo
, old
, t
);
2750 /* This MUST be overridden, or the class is ill-formed. */
2751 tree fndecl
= TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals
)), 0);
2754 fndecl
= copy_node (fndecl
);
2755 copy_lang_decl (fndecl
);
2756 DECL_NEEDS_FINAL_OVERRIDER_P (fndecl
) = 1;
2757 /* Make sure we search for it later. */
2758 if (! CLASSTYPE_ABSTRACT_VIRTUALS (t
))
2759 CLASSTYPE_ABSTRACT_VIRTUALS (t
) = error_mark_node
;
2761 vfn
= build1 (ADDR_EXPR
, vfunc_ptr_type_node
, fndecl
);
2762 TREE_CONSTANT (vfn
) = 1;
2764 /* We can use integer_zero_node, as we will core dump
2765 if this is used anyway. */
2766 TREE_VALUE (virtuals
) = build_vtable_entry (integer_zero_node
, vfn
);
2769 virtuals
= TREE_CHAIN (virtuals
);
2770 old_virtuals
= TREE_CHAIN (old_virtuals
);
2773 /* Let's reuse the old vtable. */
2774 if (choose
== REUSE_OLD
)
2776 BINFO_VTABLE (binfo
) = BINFO_VTABLE (old
);
2777 BINFO_VIRTUALS (binfo
) = BINFO_VIRTUALS (old
);
2781 /* Merge in overrides for virtual bases.
2782 BINFO is the hierarchy we want to modify, and OLD has the potential
2786 merge_overrides (binfo
, old
, do_self
, t
)
2791 tree binfos
= BINFO_BASETYPES (binfo
);
2792 tree old_binfos
= BINFO_BASETYPES (old
);
2793 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2795 /* Should we use something besides CLASSTYPE_VFIELDS? */
2796 if (do_self
&& CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
2798 override_one_vtable (binfo
, old
, t
);
2801 for (i
= 0; i
< n_baselinks
; i
++)
2803 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2804 tree old_base_binfo
= TREE_VEC_ELT (old_binfos
, i
);
2805 int is_not_base_vtable
2806 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
2807 if (! TREE_VIA_VIRTUAL (base_binfo
))
2808 merge_overrides (base_binfo
, old_base_binfo
, is_not_base_vtable
, t
);
2812 /* Get the base virtual function declarations in T that are either
2813 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2814 the overrider/hider. */
2817 get_basefndecls (fndecl
, t
)
2820 tree methods
= TYPE_METHODS (t
);
2821 tree base_fndecls
= NULL_TREE
;
2822 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
2823 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2827 if (TREE_CODE (methods
) == FUNCTION_DECL
2828 && DECL_VINDEX (methods
) != NULL_TREE
2829 && DECL_NAME (fndecl
) == DECL_NAME (methods
))
2830 base_fndecls
= temp_tree_cons (fndecl
, methods
, base_fndecls
);
2832 methods
= TREE_CHAIN (methods
);
2836 return base_fndecls
;
2838 for (i
= 0; i
< n_baseclasses
; i
++)
2840 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2841 tree basetype
= BINFO_TYPE (base_binfo
);
2843 base_fndecls
= chainon (get_basefndecls (fndecl
, basetype
),
2847 return base_fndecls
;
2850 /* Mark the functions that have been hidden with their overriders.
2851 Since we start out with all functions already marked with a hider,
2852 no need to mark functions that are just hidden. */
2855 mark_overriders (fndecl
, base_fndecls
)
2856 tree fndecl
, base_fndecls
;
2858 while (base_fndecls
)
2860 if (overrides (TREE_VALUE (base_fndecls
), fndecl
))
2861 TREE_PURPOSE (base_fndecls
) = fndecl
;
2863 base_fndecls
= TREE_CHAIN (base_fndecls
);
2867 /* If this declaration supersedes the declaration of
2868 a method declared virtual in the base class, then
2869 mark this field as being virtual as well. */
2872 check_for_override (decl
, ctype
)
2875 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (ctype
));
2876 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2877 int virtualp
= DECL_VIRTUAL_P (decl
);
2878 int found_overriden_fn
= 0;
2880 for (i
= 0; i
< n_baselinks
; i
++)
2882 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2883 if (TYPE_VIRTUAL_P (BINFO_TYPE (base_binfo
)))
2885 tree tmp
= get_matching_virtual
2887 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl
)));
2889 if (tmp
&& !found_overriden_fn
)
2891 /* If this function overrides some virtual in some base
2892 class, then the function itself is also necessarily
2893 virtual, even if the user didn't explicitly say so. */
2894 DECL_VIRTUAL_P (decl
) = 1;
2896 /* The TMP we really want is the one from the deepest
2897 baseclass on this path, taking care not to
2898 duplicate if we have already found it (via another
2899 path to its virtual baseclass. */
2900 if (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
)
2902 cp_error_at ("method `%D' may not be declared static",
2904 cp_error_at ("(since `%D' declared virtual in base class.)",
2911 = tree_cons (NULL_TREE
, tmp
, DECL_VINDEX (decl
));
2913 /* We now know that DECL overrides something,
2914 which is all that is important. But, we must
2915 continue to iterate through all the base-classes
2916 in order to allow get_matching_virtual to check for
2917 various illegal overrides. */
2918 found_overriden_fn
= 1;
2924 if (DECL_VINDEX (decl
) == NULL_TREE
)
2925 DECL_VINDEX (decl
) = error_mark_node
;
2926 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
)) = 1;
2930 /* Warn about hidden virtual functions that are not overridden in t.
2931 We know that constructors and destructors don't apply. */
2937 tree method_vec
= CLASSTYPE_METHOD_VEC (t
);
2938 int n_methods
= method_vec
? TREE_VEC_LENGTH (method_vec
) : 0;
2941 /* We go through each separately named virtual function. */
2942 for (i
= 2; i
< n_methods
&& TREE_VEC_ELT (method_vec
, i
); ++i
)
2944 tree fns
= TREE_VEC_ELT (method_vec
, i
);
2947 tree base_fndecls
= NULL_TREE
;
2948 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
2949 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2951 fndecl
= OVL_CURRENT (fns
);
2952 if (DECL_VINDEX (fndecl
) == NULL_TREE
)
2955 /* First we get a list of all possible functions that might be
2956 hidden from each base class. */
2957 for (i
= 0; i
< n_baseclasses
; i
++)
2959 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2960 tree basetype
= BINFO_TYPE (base_binfo
);
2962 base_fndecls
= chainon (get_basefndecls (fndecl
, basetype
),
2966 fns
= OVL_NEXT (fns
);
2968 fndecl
= OVL_CURRENT (fns
);
2972 /* ...then mark up all the base functions with overriders, preferring
2973 overriders to hiders. */
2977 mark_overriders (fndecl
, base_fndecls
);
2979 fns
= OVL_NEXT (fns
);
2981 fndecl
= OVL_CURRENT (fns
);
2986 /* Now give a warning for all base functions without overriders,
2987 as they are hidden. */
2988 while (base_fndecls
)
2990 if (! overrides (TREE_VALUE (base_fndecls
),
2991 TREE_PURPOSE (base_fndecls
)))
2993 /* Here we know it is a hider, and no overrider exists. */
2994 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls
));
2995 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls
));
2998 base_fndecls
= TREE_CHAIN (base_fndecls
);
3003 /* Check for things that are invalid. There are probably plenty of other
3004 things we should check for also. */
3007 finish_struct_anon (t
)
3012 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
3014 if (TREE_STATIC (field
))
3016 if (TREE_CODE (field
) != FIELD_DECL
)
3019 if (DECL_NAME (field
) == NULL_TREE
3020 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
3022 tree elt
= TYPE_FIELDS (TREE_TYPE (field
));
3023 for (; elt
; elt
= TREE_CHAIN (elt
))
3025 if (DECL_ARTIFICIAL (elt
))
3028 if (DECL_NAME (elt
) == constructor_name (t
))
3029 cp_pedwarn_at ("ANSI C++ forbids member `%D' with same name as enclosing class",
3032 if (TREE_CODE (elt
) != FIELD_DECL
)
3034 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3039 if (TREE_PRIVATE (elt
))
3040 cp_pedwarn_at ("private member `%#D' in anonymous union",
3042 else if (TREE_PROTECTED (elt
))
3043 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3046 TREE_PRIVATE (elt
) = TREE_PRIVATE (field
);
3047 TREE_PROTECTED (elt
) = TREE_PROTECTED (field
);
3053 extern int interface_only
, interface_unknown
;
3055 /* Create default constructors, assignment operators, and so forth for
3056 the type indicated by T, if they are needed.
3057 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3058 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3059 cannot have a default constructor, copy constructor taking a const
3060 reference argument, or an assignment operator, respectively. If a
3061 virtual destructor is created, its DECL is returned; otherwise the
3062 return value is NULL_TREE. */
3065 add_implicitly_declared_members (t
, cant_have_default_ctor
,
3066 cant_have_const_cctor
,
3067 cant_have_assignment
)
3069 int cant_have_default_ctor
;
3070 int cant_have_const_cctor
;
3071 int cant_have_assignment
;
3074 tree implicit_fns
= NULL_TREE
;
3075 tree name
= TYPE_IDENTIFIER (t
);
3076 tree virtual_dtor
= NULL_TREE
;
3080 if (TYPE_NEEDS_DESTRUCTOR (t
) && !TYPE_HAS_DESTRUCTOR (t
)
3081 && !IS_SIGNATURE (t
))
3083 default_fn
= cons_up_default_function (t
, name
, 0);
3084 check_for_override (default_fn
, t
);
3086 /* If we couldn't make it work, then pretend we didn't need it. */
3087 if (default_fn
== void_type_node
)
3088 TYPE_NEEDS_DESTRUCTOR (t
) = 0;
3091 TREE_CHAIN (default_fn
) = implicit_fns
;
3092 implicit_fns
= default_fn
;
3094 if (DECL_VINDEX (default_fn
))
3095 virtual_dtor
= default_fn
;
3098 TYPE_NEEDS_DESTRUCTOR (t
) |= TYPE_HAS_DESTRUCTOR (t
);
3100 /* Default constructor. */
3101 if (! TYPE_HAS_CONSTRUCTOR (t
) && ! cant_have_default_ctor
3102 && ! IS_SIGNATURE (t
))
3104 default_fn
= cons_up_default_function (t
, name
, 2);
3105 TREE_CHAIN (default_fn
) = implicit_fns
;
3106 implicit_fns
= default_fn
;
3109 /* Copy constructor. */
3110 if (! TYPE_HAS_INIT_REF (t
) && ! IS_SIGNATURE (t
) && ! TYPE_FOR_JAVA (t
))
3112 /* ARM 12.18: You get either X(X&) or X(const X&), but
3114 default_fn
= cons_up_default_function (t
, name
,
3115 3 + cant_have_const_cctor
);
3116 TREE_CHAIN (default_fn
) = implicit_fns
;
3117 implicit_fns
= default_fn
;
3120 /* Assignment operator. */
3121 if (! TYPE_HAS_ASSIGN_REF (t
) && ! IS_SIGNATURE (t
) && ! TYPE_FOR_JAVA (t
))
3123 default_fn
= cons_up_default_function (t
, name
,
3124 5 + cant_have_assignment
);
3125 TREE_CHAIN (default_fn
) = implicit_fns
;
3126 implicit_fns
= default_fn
;
3129 /* Now, hook all of the new functions on to TYPE_METHODS,
3130 and add them to the CLASSTYPE_METHOD_VEC. */
3131 for (f
= &implicit_fns
; *f
; f
= &TREE_CHAIN (*f
))
3132 add_method (t
, 0, *f
);
3133 *f
= TYPE_METHODS (t
);
3134 TYPE_METHODS (t
) = implicit_fns
;
3136 return virtual_dtor
;
3139 /* Subroutine of finish_struct_1. Recursively count the number of fields
3140 in TYPE, including anonymous union members. */
3143 count_fields (fields
)
3148 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
3150 if (TREE_CODE (x
) == FIELD_DECL
&& ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3151 n_fields
+= count_fields (TYPE_FIELDS (TREE_TYPE (x
)));
3158 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3159 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3162 add_fields_to_vec (fields
, field_vec
, idx
)
3163 tree fields
, field_vec
;
3167 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
3169 if (TREE_CODE (x
) == FIELD_DECL
&& ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3170 idx
= add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x
)), field_vec
, idx
);
3172 TREE_VEC_ELT (field_vec
, idx
++) = x
;
3177 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
3178 (or C++ class declaration).
3180 For C++, we must handle the building of derived classes.
3181 Also, C++ allows static class members. The way that this is
3182 handled is to keep the field name where it is (as the DECL_NAME
3183 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
3184 of the field. layout_record and layout_union will know about this.
3186 More C++ hair: inline functions have text in their
3187 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
3188 meaningful tree structure. After the struct has been laid out, set
3189 things up so that this can happen.
3191 And still more: virtual functions. In the case of single inheritance,
3192 when a new virtual function is seen which redefines a virtual function
3193 from the base class, the new virtual function is placed into
3194 the virtual function table at exactly the same address that
3195 it had in the base class. When this is extended to multiple
3196 inheritance, the same thing happens, except that multiple virtual
3197 function tables must be maintained. The first virtual function
3198 table is treated in exactly the same way as in the case of single
3199 inheritance. Additional virtual function tables have different
3200 DELTAs, which tell how to adjust `this' to point to the right thing.
3202 ATTRIBUTES is the set of decl attributes to be applied, if any. */
3205 finish_struct_1 (t
, warn_anon
)
3210 enum tree_code code
= TREE_CODE (t
);
3211 tree fields
= TYPE_FIELDS (t
);
3212 tree x
, last_x
, method_vec
;
3214 int max_has_virtual
;
3215 tree pending_virtuals
= NULL_TREE
;
3216 tree pending_hard_virtuals
= NULL_TREE
;
3217 tree abstract_virtuals
= NULL_TREE
;
3221 int cant_have_default_ctor
;
3222 int cant_have_const_ctor
;
3223 int no_const_asn_ref
;
3224 int has_mutable
= 0;
3227 /* The index of the first base class which has virtual
3228 functions. Only applied to non-virtual baseclasses. */
3229 int first_vfn_base_index
;
3232 int any_default_members
= 0;
3233 int const_sans_init
= 0;
3234 int ref_sans_init
= 0;
3235 tree access_decls
= NULL_TREE
;
3238 int has_pointers
= 0;
3239 tree inline_friends
;
3243 if (IS_AGGR_TYPE (t
))
3244 cp_error ("redefinition of `%#T'", t
);
3246 my_friendly_abort (172);
3251 GNU_xref_decl (current_function_decl
, t
);
3253 /* If this type was previously laid out as a forward reference,
3254 make sure we lay it out again. */
3256 TYPE_SIZE (t
) = NULL_TREE
;
3257 CLASSTYPE_GOT_SEMICOLON (t
) = 0;
3260 /* This is in general too late to do this. I moved the main case up to
3261 left_curly, what else needs to move? */
3262 if (! IS_SIGNATURE (t
))
3264 my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t
) == interface_only
, 999);
3265 my_friendly_assert (CLASSTYPE_INTERFACE_KNOWN (t
) == ! interface_unknown
, 999);
3269 old
= suspend_momentary ();
3271 /* Install struct as DECL_FIELD_CONTEXT of each field decl.
3272 Also process specified field sizes.
3273 Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
3274 The specified size is found in the DECL_INITIAL.
3275 Store 0 there, except for ": 0" fields (so we can find them
3276 and delete them, below). */
3278 if (TYPE_BINFO_BASETYPES (t
))
3279 n_baseclasses
= TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t
));
3283 if (n_baseclasses
> 0)
3285 struct base_info base_info
;
3287 first_vfn_base_index
= finish_base_struct (t
, &base_info
);
3288 /* Remember where we got our vfield from. */
3289 CLASSTYPE_VFIELD_PARENT (t
) = first_vfn_base_index
;
3290 has_virtual
= base_info
.has_virtual
;
3291 max_has_virtual
= base_info
.max_has_virtual
;
3292 vfield
= base_info
.vfield
;
3293 vfields
= base_info
.vfields
;
3294 CLASSTYPE_RTTI (t
) = base_info
.rtti
;
3295 cant_have_default_ctor
= base_info
.cant_have_default_ctor
;
3296 cant_have_const_ctor
= base_info
.cant_have_const_ctor
;
3297 no_const_asn_ref
= base_info
.no_const_asn_ref
;
3302 first_vfn_base_index
= -1;
3304 max_has_virtual
= has_virtual
;
3306 vfields
= NULL_TREE
;
3307 CLASSTYPE_RTTI (t
) = NULL_TREE
;
3308 cant_have_default_ctor
= 0;
3309 cant_have_const_ctor
= 0;
3310 no_const_asn_ref
= 0;
3313 /* The three of these are approximations which may later be
3314 modified. Needed at this point to make add_virtual_function
3315 and modify_vtable_entries work. */
3316 CLASSTYPE_VFIELDS (t
) = vfields
;
3317 CLASSTYPE_VFIELD (t
) = vfield
;
3319 for (x
= TYPE_METHODS (t
); x
; x
= TREE_CHAIN (x
))
3321 GNU_xref_member (current_class_name
, x
);
3323 /* If this was an evil function, don't keep it in class. */
3324 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x
)))
3327 /* Do both of these, even though they're in the same union;
3328 if the insn `r' member and the size `i' member are
3329 different sizes, as on the alpha, the larger of the two
3330 will end up with garbage in it. */
3331 DECL_SAVED_INSNS (x
) = NULL_RTX
;
3332 DECL_FIELD_SIZE (x
) = 0;
3334 check_for_override (x
, t
);
3335 if (DECL_ABSTRACT_VIRTUAL_P (x
) && ! DECL_VINDEX (x
))
3336 cp_error_at ("initializer specified for non-virtual method `%D'", x
);
3338 /* The name of the field is the original field name
3339 Save this in auxiliary field for later overloading. */
3340 if (DECL_VINDEX (x
))
3342 add_virtual_function (&pending_virtuals
, &pending_hard_virtuals
,
3343 &has_virtual
, x
, t
);
3344 if (DECL_ABSTRACT_VIRTUAL_P (x
))
3345 abstract_virtuals
= tree_cons (NULL_TREE
, x
, abstract_virtuals
);
3347 /* XXX Why did I comment this out? (jason) */
3355 fields
= chainon (build_vbase_pointer_fields (t
), fields
);
3358 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
3360 GNU_xref_member (current_class_name
, x
);
3362 if (TREE_CODE (x
) == FIELD_DECL
)
3364 DECL_PACKED (x
) |= TYPE_PACKED (t
);
3366 if (DECL_C_BIT_FIELD (x
) && integer_zerop (DECL_INITIAL (x
)))
3367 /* A zero-width bitfield doesn't do the trick. */;
3372 if (TREE_CODE (x
) == USING_DECL
)
3374 /* Save access declarations for later. */
3376 TREE_CHAIN (last_x
) = TREE_CHAIN (x
);
3378 fields
= TREE_CHAIN (x
);
3380 access_decls
= scratch_tree_cons (NULL_TREE
, x
, access_decls
);
3386 if (TREE_CODE (x
) == TYPE_DECL
3387 || TREE_CODE (x
) == TEMPLATE_DECL
)
3390 /* If we've gotten this far, it's a data member, possibly static,
3391 or an enumerator. */
3393 DECL_FIELD_CONTEXT (x
) = t
;
3395 /* ``A local class cannot have static data members.'' ARM 9.4 */
3396 if (current_function_decl
&& TREE_STATIC (x
))
3397 cp_error_at ("field `%D' in local class cannot be static", x
);
3399 /* Perform error checking that did not get done in
3401 if (TREE_CODE (TREE_TYPE (x
)) == FUNCTION_TYPE
)
3403 cp_error_at ("field `%D' invalidly declared function type",
3405 TREE_TYPE (x
) = build_pointer_type (TREE_TYPE (x
));
3407 else if (TREE_CODE (TREE_TYPE (x
)) == METHOD_TYPE
)
3409 cp_error_at ("field `%D' invalidly declared method type", x
);
3410 TREE_TYPE (x
) = build_pointer_type (TREE_TYPE (x
));
3412 else if (TREE_CODE (TREE_TYPE (x
)) == OFFSET_TYPE
)
3414 cp_error_at ("field `%D' invalidly declared offset type", x
);
3415 TREE_TYPE (x
) = build_pointer_type (TREE_TYPE (x
));
3419 if (DECL_NAME (x
) == constructor_name (t
))
3420 cant_have_default_ctor
= 1;
3423 if (TREE_TYPE (x
) == error_mark_node
)
3426 DECL_SAVED_INSNS (x
) = NULL_RTX
;
3427 DECL_FIELD_SIZE (x
) = 0;
3429 /* When this goes into scope, it will be a non-local reference. */
3430 DECL_NONLOCAL (x
) = 1;
3432 if (TREE_CODE (x
) == CONST_DECL
)
3435 if (TREE_CODE (x
) == VAR_DECL
)
3437 if (TREE_CODE (t
) == UNION_TYPE
)
3438 /* Unions cannot have static members. */
3439 cp_error_at ("field `%D' declared static in union", x
);
3444 /* Now it can only be a FIELD_DECL. */
3446 if (TREE_PRIVATE (x
) || TREE_PROTECTED (x
))
3449 /* If this is of reference type, check if it needs an init.
3450 Also do a little ANSI jig if necessary. */
3451 if (TREE_CODE (TREE_TYPE (x
)) == REFERENCE_TYPE
)
3453 if (DECL_INITIAL (x
) == NULL_TREE
)
3456 /* ARM $12.6.2: [A member initializer list] (or, for an
3457 aggregate, initialization by a brace-enclosed list) is the
3458 only way to initialize nonstatic const and reference
3460 cant_have_default_ctor
= 1;
3461 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) = 1;
3463 if (! TYPE_HAS_CONSTRUCTOR (t
) && extra_warnings
)
3466 cp_warning_at ("non-static reference `%#D' in class without a constructor", x
);
3468 cp_warning_at ("non-static reference in class without a constructor", x
);
3472 if (TREE_CODE (TREE_TYPE (x
)) == POINTER_TYPE
)
3475 if (DECL_MUTABLE_P (x
) || TYPE_HAS_MUTABLE_P (TREE_TYPE (x
)))
3478 /* If any field is const, the structure type is pseudo-const. */
3479 if (CP_TYPE_CONST_P (TREE_TYPE (x
)))
3481 C_TYPE_FIELDS_READONLY (t
) = 1;
3482 if (DECL_INITIAL (x
) == NULL_TREE
)
3483 const_sans_init
= 1;
3485 /* ARM $12.6.2: [A member initializer list] (or, for an
3486 aggregate, initialization by a brace-enclosed list) is the
3487 only way to initialize nonstatic const and reference
3489 cant_have_default_ctor
= 1;
3490 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) = 1;
3492 if (! TYPE_HAS_CONSTRUCTOR (t
) && !IS_SIGNATURE (t
)
3496 cp_warning_at ("non-static const member `%#D' in class without a constructor", x
);
3498 cp_warning_at ("non-static const member in class without a constructor", x
);
3503 /* A field that is pseudo-const makes the structure
3505 tree t1
= TREE_TYPE (x
);
3506 while (TREE_CODE (t1
) == ARRAY_TYPE
)
3507 t1
= TREE_TYPE (t1
);
3508 if (IS_AGGR_TYPE (t1
))
3510 if (C_TYPE_FIELDS_READONLY (t1
))
3511 C_TYPE_FIELDS_READONLY (t
) = 1;
3512 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1
))
3513 const_sans_init
= 1;
3517 /* We set DECL_C_BIT_FIELD in grokbitfield.
3518 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3519 if (DECL_C_BIT_FIELD (x
))
3521 /* Invalid bit-field size done by grokfield. */
3522 /* Detect invalid bit-field type. */
3523 if (DECL_INITIAL (x
)
3524 && ! INTEGRAL_TYPE_P (TREE_TYPE (x
)))
3526 cp_error_at ("bit-field `%#D' with non-integral type", x
);
3527 DECL_INITIAL (x
) = NULL
;
3530 /* Detect and ignore out of range field width. */
3531 if (DECL_INITIAL (x
))
3533 tree w
= DECL_INITIAL (x
);
3534 register int width
= 0;
3536 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3539 /* detect invalid field size. */
3540 if (TREE_CODE (w
) == CONST_DECL
)
3541 w
= DECL_INITIAL (w
);
3542 else if (TREE_READONLY_DECL_P (w
))
3543 w
= decl_constant_value (w
);
3545 if (TREE_CODE (w
) != INTEGER_CST
)
3547 cp_error_at ("bit-field `%D' width not an integer constant",
3549 DECL_INITIAL (x
) = NULL_TREE
;
3551 else if (width
= TREE_INT_CST_LOW (w
),
3554 DECL_INITIAL (x
) = NULL
;
3555 cp_error_at ("negative width in bit-field `%D'", x
);
3557 else if (width
== 0 && DECL_NAME (x
) != 0)
3559 DECL_INITIAL (x
) = NULL
;
3560 cp_error_at ("zero width for bit-field `%D'", x
);
3563 > TYPE_PRECISION (long_long_unsigned_type_node
))
3565 /* The backend will dump if you try to use something
3566 too big; avoid that. */
3567 DECL_INITIAL (x
) = NULL
;
3568 sorry ("bit-fields larger than %d bits",
3569 TYPE_PRECISION (long_long_unsigned_type_node
));
3570 cp_error_at (" in declaration of `%D'", x
);
3572 else if (width
> TYPE_PRECISION (TREE_TYPE (x
))
3573 && TREE_CODE (TREE_TYPE (x
)) != ENUMERAL_TYPE
3574 && TREE_CODE (TREE_TYPE (x
)) != BOOLEAN_TYPE
)
3576 cp_warning_at ("width of `%D' exceeds its type", x
);
3578 else if (TREE_CODE (TREE_TYPE (x
)) == ENUMERAL_TYPE
3579 && ((min_precision (TYPE_MIN_VALUE (TREE_TYPE (x
)),
3580 TREE_UNSIGNED (TREE_TYPE (x
))) > width
)
3581 || (min_precision (TYPE_MAX_VALUE (TREE_TYPE (x
)),
3582 TREE_UNSIGNED (TREE_TYPE (x
))) > width
)))
3584 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3588 if (DECL_INITIAL (x
))
3590 DECL_INITIAL (x
) = NULL_TREE
;
3591 DECL_FIELD_SIZE (x
) = width
;
3592 DECL_BIT_FIELD (x
) = 1;
3596 #ifdef EMPTY_FIELD_BOUNDARY
3597 DECL_ALIGN (x
) = MAX (DECL_ALIGN (x
),
3598 EMPTY_FIELD_BOUNDARY
);
3600 #ifdef PCC_BITFIELD_TYPE_MATTERS
3601 if (PCC_BITFIELD_TYPE_MATTERS
)
3602 DECL_ALIGN (x
) = MAX (DECL_ALIGN (x
),
3603 TYPE_ALIGN (TREE_TYPE (x
)));
3609 /* Non-bit-fields are aligned for their type. */
3610 DECL_ALIGN (x
) = MAX (DECL_ALIGN (x
), TYPE_ALIGN (TREE_TYPE (x
)));
3614 tree type
= TREE_TYPE (x
);
3616 while (TREE_CODE (type
) == ARRAY_TYPE
)
3617 type
= TREE_TYPE (type
);
3619 if (CLASS_TYPE_P (type
) && ! ANON_AGGR_TYPE_P (type
))
3621 /* Never let anything with uninheritable virtuals
3622 make it through without complaint. */
3623 if (CLASSTYPE_ABSTRACT_VIRTUALS (type
))
3624 abstract_virtuals_error (x
, type
);
3626 /* Don't let signatures make it through either. */
3627 if (IS_SIGNATURE (type
))
3628 signature_error (x
, type
);
3630 if (code
== UNION_TYPE
)
3632 const char *fie
= NULL
;
3633 if (TYPE_NEEDS_CONSTRUCTING (type
))
3634 fie
= "constructor";
3635 else if (TYPE_NEEDS_DESTRUCTOR (type
))
3637 else if (TYPE_HAS_COMPLEX_ASSIGN_REF (type
))
3638 fie
= "copy assignment operator";
3640 cp_error_at ("member `%#D' with %s not allowed in union", x
,
3645 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (type
);
3646 TYPE_NEEDS_DESTRUCTOR (t
) |= TYPE_NEEDS_DESTRUCTOR (type
);
3647 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type
);
3648 TYPE_HAS_COMPLEX_INIT_REF (t
) |= TYPE_HAS_COMPLEX_INIT_REF (type
);
3651 if (!TYPE_HAS_CONST_INIT_REF (type
))
3652 cant_have_const_ctor
= 1;
3654 if (!TYPE_HAS_CONST_ASSIGN_REF (type
))
3655 no_const_asn_ref
= 1;
3657 if (TYPE_HAS_CONSTRUCTOR (type
)
3658 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
3660 cant_have_default_ctor
= 1;
3662 /* This is wrong for aggregates. */
3663 if (! TYPE_HAS_CONSTRUCTOR (t
))
3666 cp_pedwarn_at ("member `%#D' with only non-default constructor", x
);
3668 cp_pedwarn_at ("member with only non-default constructor", x
);
3669 cp_pedwarn_at ("in class without a constructor",
3675 if (DECL_INITIAL (x
) != NULL_TREE
)
3677 /* `build_class_init_list' does not recognize
3679 if (code
== UNION_TYPE
&& any_default_members
!= 0)
3680 cp_error_at ("multiple fields in union `%T' initialized");
3681 any_default_members
= 1;
3686 /* If this type has any constant members which did not come
3687 with their own initialization, mark that fact here. It is
3688 not an error here, since such types can be saved either by their
3689 constructors, or by fortuitous initialization. */
3690 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
) = const_sans_init
;
3691 CLASSTYPE_REF_FIELDS_NEED_INIT (t
) = ref_sans_init
;
3692 CLASSTYPE_ABSTRACT_VIRTUALS (t
) = abstract_virtuals
;
3693 CLASSTYPE_HAS_MUTABLE (t
) = has_mutable
;
3695 /* Effective C++ rule 11. */
3696 if (has_pointers
&& warn_ecpp
&& TYPE_HAS_CONSTRUCTOR (t
)
3697 && ! (TYPE_HAS_INIT_REF (t
) && TYPE_HAS_ASSIGN_REF (t
)))
3699 cp_warning ("`%#T' has pointer data members", t
);
3701 if (! TYPE_HAS_INIT_REF (t
))
3703 cp_warning (" but does not override `%T(const %T&)'", t
, t
);
3704 if (! TYPE_HAS_ASSIGN_REF (t
))
3705 cp_warning (" or `operator=(const %T&)'", t
);
3707 else if (! TYPE_HAS_ASSIGN_REF (t
))
3708 cp_warning (" but does not override `operator=(const %T&)'", t
);
3711 /* Do some bookkeeping that will guide the generation of implicitly
3712 declared member functions. */
3713 TYPE_HAS_COMPLEX_INIT_REF (t
)
3714 |= (TYPE_HAS_INIT_REF (t
) || TYPE_USES_VIRTUAL_BASECLASSES (t
)
3715 || has_virtual
|| any_default_members
);
3716 TYPE_NEEDS_CONSTRUCTING (t
)
3717 |= (TYPE_HAS_CONSTRUCTOR (t
) || TYPE_USES_VIRTUAL_BASECLASSES (t
)
3718 || has_virtual
|| any_default_members
);
3719 if (! IS_SIGNATURE (t
))
3720 CLASSTYPE_NON_AGGREGATE (t
)
3721 = ! aggregate
|| has_virtual
|| TYPE_HAS_CONSTRUCTOR (t
);
3722 TYPE_HAS_REAL_ASSIGN_REF (t
) |= TYPE_HAS_ASSIGN_REF (t
);
3723 TYPE_HAS_COMPLEX_ASSIGN_REF (t
)
3724 |= TYPE_HAS_ASSIGN_REF (t
) || TYPE_USES_VIRTUAL_BASECLASSES (t
);
3726 /* Synthesize any needed methods. Note that methods will be synthesized
3727 for anonymous unions; grok_x_components undoes that. */
3729 = add_implicitly_declared_members (t
, cant_have_default_ctor
,
3730 cant_have_const_ctor
,
3733 add_virtual_function (&pending_virtuals
, &pending_hard_virtuals
,
3734 &has_virtual
, virtual_dtor
, t
);
3736 if (TYPE_METHODS (t
))
3738 finish_struct_methods (t
);
3739 method_vec
= CLASSTYPE_METHOD_VEC (t
);
3745 /* Just in case these got accidentally
3746 filled in by syntax errors. */
3747 TYPE_HAS_CONSTRUCTOR (t
) = 0;
3748 TYPE_HAS_DESTRUCTOR (t
) = 0;
3751 for (access_decls
= nreverse (access_decls
); access_decls
;
3752 access_decls
= TREE_CHAIN (access_decls
))
3753 handle_using_decl (TREE_VALUE (access_decls
), t
, method_vec
, fields
);
3755 if (vfield
== NULL_TREE
&& has_virtual
)
3757 /* We build this decl with vtbl_ptr_type_node, which is a
3758 `vtable_entry_type*'. It might seem more precise to use
3759 `vtable_entry_type (*)[N]' where N is the number of firtual
3760 functions. However, that would require the vtable pointer in
3761 base classes to have a different type than the vtable pointer
3762 in derived classes. We could make that happen, but that
3763 still wouldn't solve all the problems. In particular, the
3764 type-based alias analysis code would decide that assignments
3765 to the base class vtable pointer can't alias assignments to
3766 the derived class vtable pointer, since they have different
3767 types. Thus, in an derived class destructor, where the base
3768 class constructor was inlined, we could generate bad code for
3769 setting up the vtable pointer.
3771 Therefore, we use one type for all vtable pointers. We still
3772 use a type-correct type; it's just doesn't indicate the array
3773 bounds. That's better than using `void*' or some such; it's
3774 cleaner, and it let's the alias analysis code know that these
3775 stores cannot alias stores to void*! */
3776 vfield
= build_lang_field_decl (FIELD_DECL
, get_vfield_name (t
),
3777 vtbl_ptr_type_node
);
3778 /* If you change any of the below, take a look at all the
3779 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
3781 DECL_ASSEMBLER_NAME (vfield
) = get_identifier (VFIELD_BASE
);
3782 CLASSTYPE_VFIELD (t
) = vfield
;
3783 DECL_VIRTUAL_P (vfield
) = 1;
3784 DECL_ARTIFICIAL (vfield
) = 1;
3785 DECL_FIELD_CONTEXT (vfield
) = t
;
3786 DECL_CLASS_CONTEXT (vfield
) = t
;
3787 DECL_FCONTEXT (vfield
) = t
;
3788 DECL_SAVED_INSNS (vfield
) = NULL_RTX
;
3789 DECL_FIELD_SIZE (vfield
) = 0;
3790 DECL_ALIGN (vfield
) = TYPE_ALIGN (ptr_type_node
);
3792 /* This is more efficient, but breaks binary compatibility, turn
3793 it on sometime when we don't care. If we turn it on, we also
3794 have to enable the code in dfs_init_vbase_pointers. */
3795 /* vfield is always first entry in structure. */
3796 TREE_CHAIN (vfield
) = fields
;
3801 my_friendly_assert (TREE_CHAIN (last_x
) == NULL_TREE
, 175);
3802 TREE_CHAIN (last_x
) = vfield
;
3809 vfields
= chainon (vfields
, build_tree_list (NULL_TREE
, t
));
3812 /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
3814 C++: maybe we will support default field initialization some day... */
3816 /* Delete all duplicate fields from the fields */
3817 delete_duplicate_fields (fields
);
3819 /* Now we have the nearly final fieldlist for the data fields. Record it,
3820 then lay out the structure or union (including the fields). */
3822 TYPE_FIELDS (t
) = fields
;
3826 last_x
= build_base_fields (t
);
3828 /* If all our bases are empty, we can be empty too. */
3829 for (x
= last_x
; empty
&& x
; x
= TREE_CHAIN (x
))
3830 if (DECL_SIZE (x
) != integer_zero_node
)
3834 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
3835 we have to save this before we start modifying
3836 TYPE_NONCOPIED_PARTS. */
3837 inline_friends
= CLASSTYPE_INLINE_FRIENDS (t
);
3838 CLASSTYPE_INLINE_FRIENDS (t
) = NULL_TREE
;
3842 /* C++: do not let empty structures exist. */
3843 tree decl
= build_lang_field_decl
3844 (FIELD_DECL
, NULL_TREE
, char_type_node
);
3845 TREE_CHAIN (decl
) = fields
;
3846 TYPE_FIELDS (t
) = decl
;
3847 TYPE_NONCOPIED_PARTS (t
)
3848 = tree_cons (NULL_TREE
, decl
, TYPE_NONCOPIED_PARTS (t
));
3849 TREE_STATIC (TYPE_NONCOPIED_PARTS (t
)) = 1;
3853 TYPE_FIELDS (t
) = chainon (last_x
, TYPE_FIELDS (t
));
3857 /* Remember the size and alignment of the class before adding
3858 the virtual bases. */
3859 if (empty
&& flag_new_abi
)
3860 CLASSTYPE_SIZE (t
) = integer_zero_node
;
3861 else if (flag_new_abi
&& TYPE_HAS_COMPLEX_INIT_REF (t
)
3862 && TYPE_HAS_COMPLEX_ASSIGN_REF (t
))
3863 CLASSTYPE_SIZE (t
) = TYPE_BINFO_SIZE (t
);
3865 CLASSTYPE_SIZE (t
) = TYPE_SIZE (t
);
3866 CLASSTYPE_ALIGN (t
) = TYPE_ALIGN (t
);
3868 finish_struct_anon (t
);
3870 /* Set the TYPE_DECL for this type to contain the right
3871 value for DECL_OFFSET, so that we can use it as part
3872 of a COMPONENT_REF for multiple inheritance. */
3874 layout_decl (TYPE_MAIN_DECL (t
), 0);
3876 /* Now fix up any virtual base class types that we left lying
3877 around. We must get these done before we try to lay out the
3878 virtual function table. */
3879 pending_hard_virtuals
= nreverse (pending_hard_virtuals
);
3882 /* layout_basetypes will remove the base subobject fields. */
3883 max_has_virtual
= layout_basetypes (t
, max_has_virtual
);
3885 TYPE_FIELDS (t
) = fields
;
3887 my_friendly_assert (TYPE_FIELDS (t
) == fields
, 981117);
3889 /* Delete all zero-width bit-fields from the front of the fieldlist */
3890 while (fields
&& DECL_C_BIT_FIELD (fields
)
3891 && DECL_INITIAL (fields
))
3892 fields
= TREE_CHAIN (fields
);
3893 /* Delete all such fields from the rest of the fields. */
3894 for (x
= fields
; x
;)
3896 if (TREE_CHAIN (x
) && DECL_C_BIT_FIELD (TREE_CHAIN (x
))
3897 && DECL_INITIAL (TREE_CHAIN (x
)))
3898 TREE_CHAIN (x
) = TREE_CHAIN (TREE_CHAIN (x
));
3902 TYPE_FIELDS (t
) = fields
;
3904 if (TYPE_USES_VIRTUAL_BASECLASSES (t
))
3908 vbases
= CLASSTYPE_VBASECLASSES (t
);
3911 /* Now fixup overrides of all functions in vtables from all
3912 direct or indirect virtual base classes. */
3913 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
3914 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
3916 for (i
= 0; i
< n_baseclasses
; i
++)
3918 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
3919 tree basetype
= BINFO_TYPE (base_binfo
);
3922 vbases
= CLASSTYPE_VBASECLASSES (basetype
);
3925 merge_overrides (binfo_member (BINFO_TYPE (vbases
),
3926 CLASSTYPE_VBASECLASSES (t
)),
3928 vbases
= TREE_CHAIN (vbases
);
3934 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
3935 might need to know it for setting up the offsets in the vtable
3936 (or in thunks) below. */
3937 if (vfield
!= NULL_TREE
3938 && DECL_FIELD_CONTEXT (vfield
) != t
)
3940 tree binfo
= get_binfo (DECL_FIELD_CONTEXT (vfield
), t
, 0);
3941 tree offset
= BINFO_OFFSET (binfo
);
3943 vfield
= copy_node (vfield
);
3944 copy_lang_decl (vfield
);
3946 if (! integer_zerop (offset
))
3947 offset
= size_binop (MULT_EXPR
, offset
, size_int (BITS_PER_UNIT
));
3948 DECL_FIELD_CONTEXT (vfield
) = t
;
3949 DECL_CLASS_CONTEXT (vfield
) = t
;
3950 DECL_FIELD_BITPOS (vfield
)
3951 = size_binop (PLUS_EXPR
, offset
, DECL_FIELD_BITPOS (vfield
));
3952 CLASSTYPE_VFIELD (t
) = vfield
;
3956 cp_warning ("Doing hard virtuals for %T...", t
);
3959 if (has_virtual
> max_has_virtual
)
3960 max_has_virtual
= has_virtual
;
3961 if (max_has_virtual
> 0)
3962 TYPE_VIRTUAL_P (t
) = 1;
3964 if (flag_rtti
&& TYPE_VIRTUAL_P (t
) && !pending_hard_virtuals
)
3965 modify_all_vtables (t
, NULL_TREE
, NULL_TREE
);
3967 while (pending_hard_virtuals
)
3969 modify_all_vtables (t
,
3970 TREE_PURPOSE (pending_hard_virtuals
),
3971 TREE_VALUE (pending_hard_virtuals
));
3972 pending_hard_virtuals
= TREE_CHAIN (pending_hard_virtuals
);
3975 if (TYPE_USES_VIRTUAL_BASECLASSES (t
))
3978 /* Now fixup any virtual function entries from virtual bases
3979 that have different deltas. This has to come after we do the
3980 pending hard virtuals, as we might have a function that comes
3981 from multiple virtual base instances that is only overridden
3982 by a hard virtual above. */
3983 vbases
= CLASSTYPE_VBASECLASSES (t
);
3986 /* We might be able to shorten the amount of work we do by
3987 only doing this for vtables that come from virtual bases
3988 that have differing offsets, but don't want to miss any
3990 fixup_vtable_deltas (vbases
, 1, t
);
3991 vbases
= TREE_CHAIN (vbases
);
3995 /* Under our model of GC, every C++ class gets its own virtual
3996 function table, at least virtually. */
3997 if (pending_virtuals
)
3999 pending_virtuals
= nreverse (pending_virtuals
);
4000 /* We must enter these virtuals into the table. */
4001 if (first_vfn_base_index
< 0)
4003 if (! CLASSTYPE_COM_INTERFACE (t
))
4005 /* The second slot is for the tdesc pointer when thunks are used. */
4006 if (flag_vtable_thunks
)
4007 pending_virtuals
= tree_cons (NULL_TREE
, NULL_TREE
, pending_virtuals
);
4009 /* The first slot is for the rtti offset. */
4010 pending_virtuals
= tree_cons (NULL_TREE
, NULL_TREE
, pending_virtuals
);
4012 set_rtti_entry (pending_virtuals
,
4013 convert (ssizetype
, integer_zero_node
), t
);
4015 build_vtable (NULL_TREE
, t
);
4019 /* Here we know enough to change the type of our virtual
4020 function table, but we will wait until later this function. */
4022 if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t
)))
4023 build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t
), first_vfn_base_index
), t
);
4026 /* If this type has basetypes with constructors, then those
4027 constructors might clobber the virtual function table. But
4028 they don't if the derived class shares the exact vtable of the base
4031 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t
) = 1;
4033 else if (first_vfn_base_index
>= 0)
4035 tree binfo
= TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t
), first_vfn_base_index
);
4036 /* This class contributes nothing new to the virtual function
4037 table. However, it may have declared functions which
4038 went into the virtual function table "inherited" from the
4039 base class. If so, we grab a copy of those updated functions,
4040 and pretend they are ours. */
4042 /* See if we should steal the virtual info from base class. */
4043 if (TYPE_BINFO_VTABLE (t
) == NULL_TREE
)
4044 TYPE_BINFO_VTABLE (t
) = BINFO_VTABLE (binfo
);
4045 if (TYPE_BINFO_VIRTUALS (t
) == NULL_TREE
)
4046 TYPE_BINFO_VIRTUALS (t
) = BINFO_VIRTUALS (binfo
);
4047 if (TYPE_BINFO_VTABLE (t
) != BINFO_VTABLE (binfo
))
4048 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t
) = 1;
4051 if (max_has_virtual
|| first_vfn_base_index
>= 0)
4053 CLASSTYPE_VSIZE (t
) = has_virtual
;
4054 if (first_vfn_base_index
>= 0)
4056 if (pending_virtuals
)
4057 TYPE_BINFO_VIRTUALS (t
) = chainon (TYPE_BINFO_VIRTUALS (t
),
4060 else if (has_virtual
)
4062 TYPE_BINFO_VIRTUALS (t
) = pending_virtuals
;
4063 DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t
)) = 1;
4067 /* Now lay out the virtual function table. */
4070 /* Use size_int so values are memoized in common cases. */
4071 tree itype
= build_index_type (size_int (has_virtual
));
4072 tree atype
= build_cplus_array_type (vtable_entry_type
, itype
);
4074 layout_type (atype
);
4076 CLASSTYPE_VFIELD (t
) = vfield
;
4078 /* We may have to grow the vtable. */
4079 if (TREE_TYPE (TYPE_BINFO_VTABLE (t
)) != atype
)
4081 TREE_TYPE (TYPE_BINFO_VTABLE (t
)) = atype
;
4082 DECL_SIZE (TYPE_BINFO_VTABLE (t
)) = 0;
4083 layout_decl (TYPE_BINFO_VTABLE (t
), 0);
4084 /* At one time the vtable info was grabbed 2 words at a time. This
4085 fails on sparc unless you have 8-byte alignment. (tiemann) */
4086 DECL_ALIGN (TYPE_BINFO_VTABLE (t
))
4087 = MAX (TYPE_ALIGN (double_type_node
),
4088 DECL_ALIGN (TYPE_BINFO_VTABLE (t
)));
4091 else if (first_vfn_base_index
>= 0)
4092 CLASSTYPE_VFIELD (t
) = vfield
;
4093 CLASSTYPE_VFIELDS (t
) = vfields
;
4095 finish_struct_bits (t
, max_has_virtual
);
4097 /* Complete the rtl for any static member objects of the type we're
4099 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
4101 if (TREE_CODE (x
) == VAR_DECL
&& TREE_STATIC (x
)
4102 && TREE_TYPE (x
) == t
)
4104 DECL_MODE (x
) = TYPE_MODE (t
);
4105 make_decl_rtl (x
, NULL
, 0);
4109 /* Done with FIELDS...now decide whether to sort these for
4110 faster lookups later. Don't worry about optimizing
4111 for structs only declared in inline functions...they're
4112 not going to be referenced anywhere else.
4114 The C front-end only does this when n_fields > 15. We use
4115 a smaller number because most searches fail (succeeding
4116 ultimately as the search bores through the inheritance
4117 hierarchy), and we want this failure to occur quickly. */
4119 n_fields
= count_fields (fields
);
4120 if (n_fields
> 7 && !allocation_temporary_p ())
4122 tree field_vec
= make_tree_vec (n_fields
);
4123 add_fields_to_vec (fields
, field_vec
, 0);
4124 qsort (&TREE_VEC_ELT (field_vec
, 0), n_fields
, sizeof (tree
),
4125 (int (*)(const void *, const void *))field_decl_cmp
);
4126 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t
)))
4127 retrofit_lang_decl (TYPE_MAIN_DECL (t
));
4128 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t
)) = field_vec
;
4131 if (TYPE_HAS_CONSTRUCTOR (t
))
4133 tree vfields
= CLASSTYPE_VFIELDS (t
);
4137 /* Mark the fact that constructor for T
4138 could affect anybody inheriting from T
4139 who wants to initialize vtables for VFIELDS's type. */
4140 if (VF_DERIVED_VALUE (vfields
))
4141 TREE_ADDRESSABLE (vfields
) = 1;
4142 vfields
= TREE_CHAIN (vfields
);
4146 /* Write out inline function definitions. */
4147 do_inline_function_hair (t
, inline_friends
);
4149 if (CLASSTYPE_VSIZE (t
) != 0)
4152 /* This is now done above. */
4153 if (DECL_FIELD_CONTEXT (vfield
) != t
)
4155 tree binfo
= get_binfo (DECL_FIELD_CONTEXT (vfield
), t
, 0);
4156 tree offset
= BINFO_OFFSET (binfo
);
4158 vfield
= copy_node (vfield
);
4159 copy_lang_decl (vfield
);
4161 if (! integer_zerop (offset
))
4162 offset
= size_binop (MULT_EXPR
, offset
, size_int (BITS_PER_UNIT
));
4163 DECL_FIELD_CONTEXT (vfield
) = t
;
4164 DECL_CLASS_CONTEXT (vfield
) = t
;
4165 DECL_FIELD_BITPOS (vfield
)
4166 = size_binop (PLUS_EXPR
, offset
, DECL_FIELD_BITPOS (vfield
));
4167 CLASSTYPE_VFIELD (t
) = vfield
;
4171 /* In addition to this one, all the other vfields should be listed. */
4172 /* Before that can be done, we have to have FIELD_DECLs for them, and
4173 a place to find them. */
4174 TYPE_NONCOPIED_PARTS (t
)
4175 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t
)),
4176 vfield
, TYPE_NONCOPIED_PARTS (t
));
4178 if (warn_nonvdtor
&& TYPE_HAS_DESTRUCTOR (t
)
4179 && DECL_VINDEX (TREE_VEC_ELT (method_vec
, 1)) == NULL_TREE
)
4180 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
4184 /* Make the rtl for any new vtables we have created, and unmark
4185 the base types we marked. */
4186 finish_vtbls (TYPE_BINFO (t
), 1, t
);
4187 hack_incomplete_structures (t
);
4190 if (TYPE_NAME (t
) && TYPE_IDENTIFIER (t
))
4191 undo_template_name_overload (TYPE_IDENTIFIER (t
), 1);
4194 resume_momentary (old
);
4196 if (warn_overloaded_virtual
)
4200 /* This has to be done after we have sorted out what to do with
4201 the enclosing type. */
4202 if (write_symbols
!= DWARF_DEBUG
)
4204 /* Be smarter about nested classes here. If a type is nested,
4205 only output it if we would output the enclosing type. */
4206 if (DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (t
)))
4207 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = TREE_ASM_WRITTEN (TYPE_MAIN_DECL (t
));
4211 if (write_symbols
!= DWARF_DEBUG
&& write_symbols
!= DWARF2_DEBUG
)
4213 /* If the type has methods, we want to think about cutting down
4214 the amount of symbol table stuff we output. The value stored in
4215 the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
4216 For example, if a member function is seen and we decide to
4217 write out that member function, then we can change the value
4218 of the DECL_IGNORED_P slot, and the type will be output when
4219 that member function's debug info is written out.
4221 We can't do this with DWARF, which does not support name
4222 references between translation units. */
4223 if (CLASSTYPE_METHOD_VEC (t
))
4225 /* Don't output full info about any type
4226 which does not have its implementation defined here. */
4227 if (CLASSTYPE_INTERFACE_ONLY (t
))
4228 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 1;
4230 /* XXX do something about this. */
4231 else if (CLASSTYPE_INTERFACE_UNKNOWN (t
))
4232 /* Only a first approximation! */
4233 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 1;
4236 else if (CLASSTYPE_INTERFACE_ONLY (t
))
4237 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 1;
4240 /* Finish debugging output for this type. */
4241 rest_of_type_compilation (t
, toplevel_bindings_p ());
4246 /* When T was built up, the member declarations were added in reverse
4247 order. Rearrange them to declaration order. */
4250 unreverse_member_declarations (t
)
4257 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
4258 reverse order. Put them in declaration order now. */
4259 TYPE_METHODS (t
) = nreverse (TYPE_METHODS (t
));
4260 CLASSTYPE_TAGS (t
) = nreverse (CLASSTYPE_TAGS (t
));
4262 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
4263 reverse order, so we can't just use nreverse. */
4265 for (x
= TYPE_FIELDS (t
);
4266 x
&& TREE_CODE (x
) != TYPE_DECL
;
4269 next
= TREE_CHAIN (x
);
4270 TREE_CHAIN (x
) = prev
;
4275 TREE_CHAIN (TYPE_FIELDS (t
)) = x
;
4277 TYPE_FIELDS (t
) = prev
;
4282 finish_struct (t
, attributes
, warn_anon
)
4286 /* Append the fields we need for constructing signature tables. */
4287 if (IS_SIGNATURE (t
))
4288 append_signature_fields (t
);
4290 /* Now that we've got all the field declarations, reverse everything
4292 unreverse_member_declarations (t
);
4294 cplus_decl_attributes (t
, attributes
, NULL_TREE
);
4296 if (processing_template_decl
)
4298 finish_struct_methods (t
);
4299 TYPE_SIZE (t
) = integer_zero_node
;
4302 finish_struct_1 (t
, warn_anon
);
4304 TYPE_BEING_DEFINED (t
) = 0;
4306 if (current_class_type
)
4309 error ("trying to finish struct, but kicked out due to previous parse errors.");
4314 /* Return the dynamic type of INSTANCE, if known.
4315 Used to determine whether the virtual function table is needed
4318 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4319 of our knowledge of its type. */
4322 fixed_type_or_null (instance
, nonnull
)
4326 switch (TREE_CODE (instance
))
4329 /* Check that we are not going through a cast of some sort. */
4330 if (TREE_TYPE (instance
)
4331 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance
, 0))))
4332 instance
= TREE_OPERAND (instance
, 0);
4333 /* fall through... */
4335 /* This is a call to a constructor, hence it's never zero. */
4336 if (TREE_HAS_CONSTRUCTOR (instance
))
4340 return TREE_TYPE (instance
);
4345 /* This is a call to a constructor, hence it's never zero. */
4346 if (TREE_HAS_CONSTRUCTOR (instance
))
4350 return TREE_TYPE (instance
);
4352 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
4359 if (TREE_CODE (TREE_OPERAND (instance
, 1)) == INTEGER_CST
)
4360 /* Propagate nonnull. */
4361 fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
4362 if (TREE_CODE (TREE_OPERAND (instance
, 0)) == ADDR_EXPR
)
4363 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
4368 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
4373 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
4376 return fixed_type_or_null (TREE_OPERAND (instance
, 1), nonnull
);
4380 if (TREE_CODE (TREE_TYPE (instance
)) == ARRAY_TYPE
4381 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance
))))
4385 return TREE_TYPE (TREE_TYPE (instance
));
4387 /* fall through... */
4390 if (IS_AGGR_TYPE (TREE_TYPE (instance
)))
4394 return TREE_TYPE (instance
);
4398 if (instance
== current_class_ptr
4399 && flag_this_is_variable
<= 0)
4401 /* Normally, 'this' must be non-null. */
4402 if (flag_this_is_variable
== 0)
4405 /* <0 means we're in a constructor and we know our type. */
4406 if (flag_this_is_variable
< 0)
4407 return TREE_TYPE (TREE_TYPE (instance
));
4409 else if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
4410 /* Reference variables should be references to objects. */
4420 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
4421 to the static type. We also handle the case where INSTANCE is really
4424 Used to determine whether the virtual function table is needed
4427 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4428 of our knowledge of its type. */
4431 resolves_to_fixed_type_p (instance
, nonnull
)
4435 tree t
= TREE_TYPE (instance
);
4436 tree fixed
= fixed_type_or_null (instance
, nonnull
);
4437 if (fixed
== NULL_TREE
)
4439 if (POINTER_TYPE_P (t
))
4441 return same_type_p (TYPE_MAIN_VARIANT (t
), TYPE_MAIN_VARIANT (fixed
));
4446 init_class_processing ()
4448 current_class_depth
= 0;
4449 current_class_stack_size
= 10;
4451 = (class_stack_node_t
) xmalloc (current_class_stack_size
4452 * sizeof (struct class_stack_node
));
4454 current_lang_stacksize
= 10;
4455 current_lang_base
= (tree
*)xmalloc(current_lang_stacksize
* sizeof (tree
));
4456 current_lang_stack
= current_lang_base
;
4458 access_default_node
= build_int_2 (0, 0);
4459 access_public_node
= build_int_2 (1, 0);
4460 access_protected_node
= build_int_2 (2, 0);
4461 access_private_node
= build_int_2 (3, 0);
4462 access_default_virtual_node
= build_int_2 (4, 0);
4463 access_public_virtual_node
= build_int_2 (5, 0);
4464 access_protected_virtual_node
= build_int_2 (6, 0);
4465 access_private_virtual_node
= build_int_2 (7, 0);
4467 /* Keep these values lying around. */
4468 base_layout_decl
= build_lang_field_decl (FIELD_DECL
, NULL_TREE
, error_mark_node
);
4469 TREE_TYPE (base_layout_decl
) = make_node (RECORD_TYPE
);
4471 gcc_obstack_init (&class_obstack
);
4474 /* Set current scope to NAME. CODE tells us if this is a
4475 STRUCT, UNION, or ENUM environment.
4477 NAME may end up being NULL_TREE if this is an anonymous or
4478 late-bound struct (as in "struct { ... } foo;") */
4480 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
4481 appropriate values, found by looking up the type definition of
4484 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
4485 which can be seen locally to the class. They are shadowed by
4486 any subsequent local declaration (including parameter names).
4488 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
4489 which have static meaning (i.e., static members, static
4490 member functions, enum declarations, etc).
4492 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
4493 which can be seen locally to the class (as in 1), but
4494 know that we are doing this for declaration purposes
4495 (i.e. friend foo::bar (int)).
4497 So that we may avoid calls to lookup_name, we cache the _TYPE
4498 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
4500 For multiple inheritance, we perform a two-pass depth-first search
4501 of the type lattice. The first pass performs a pre-order search,
4502 marking types after the type has had its fields installed in
4503 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
4504 unmarks the marked types. If a field or member function name
4505 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
4506 that name becomes `error_mark_node'. */
4509 pushclass (type
, modify
)
4513 type
= TYPE_MAIN_VARIANT (type
);
4515 /* Make sure there is enough room for the new entry on the stack. */
4516 if (current_class_depth
+ 1 >= current_class_stack_size
)
4518 current_class_stack_size
*= 2;
4520 = (class_stack_node_t
) xrealloc (current_class_stack
,
4521 current_class_stack_size
4522 * sizeof (struct class_stack_node
));
4525 /* Insert a new entry on the class stack. */
4526 current_class_stack
[current_class_depth
].name
= current_class_name
;
4527 current_class_stack
[current_class_depth
].type
= current_class_type
;
4528 current_class_stack
[current_class_depth
].access
= current_access_specifier
;
4529 current_class_stack
[current_class_depth
].names_used
= 0;
4530 current_class_depth
++;
4532 /* Now set up the new type. */
4533 current_class_name
= TYPE_NAME (type
);
4534 if (TREE_CODE (current_class_name
) == TYPE_DECL
)
4535 current_class_name
= DECL_NAME (current_class_name
);
4536 current_class_type
= type
;
4538 /* By default, things in classes are private, while things in
4539 structures or unions are public. */
4540 current_access_specifier
= (CLASSTYPE_DECLARED_CLASS (type
)
4541 ? access_private_node
4542 : access_public_node
);
4544 if (previous_class_type
!= NULL_TREE
4545 && (type
!= previous_class_type
4546 || TYPE_SIZE (previous_class_type
) == NULL_TREE
)
4547 && current_class_depth
== 1)
4549 /* Forcibly remove any old class remnants. */
4550 invalidate_class_lookup_cache ();
4552 /* Now, free the obstack on which we cached all the values. */
4553 if (class_cache_firstobj
)
4554 obstack_free (&class_cache_obstack
, class_cache_firstobj
);
4555 class_cache_firstobj
4556 = (char*) obstack_finish (&class_cache_obstack
);
4559 /* If we're about to enter a nested class, clear
4560 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
4561 if (modify
&& current_class_depth
> 1)
4562 clear_identifier_class_values ();
4567 if (CLASSTYPE_TEMPLATE_INFO (type
))
4568 overload_template_name (type
);
4573 if (type
!= previous_class_type
|| current_class_depth
> 1)
4574 push_class_decls (type
);
4579 /* We are re-entering the same class we just left, so we
4580 don't have to search the whole inheritance matrix to find
4581 all the decls to bind again. Instead, we install the
4582 cached class_shadowed list, and walk through it binding
4583 names and setting up IDENTIFIER_TYPE_VALUEs. */
4584 set_class_shadows (previous_class_values
);
4585 for (item
= previous_class_values
; item
; item
= TREE_CHAIN (item
))
4587 tree id
= TREE_PURPOSE (item
);
4588 tree decl
= TREE_TYPE (item
);
4590 push_class_binding (id
, decl
);
4591 if (TREE_CODE (decl
) == TYPE_DECL
)
4592 set_identifier_type_value (id
, TREE_TYPE (decl
));
4594 unuse_fields (type
);
4597 storetags (CLASSTYPE_TAGS (type
));
4601 /* When we exit a toplevel class scope, we save the
4602 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
4603 reenter the class. Here, we've entered some other class, so we
4604 must invalidate our cache. */
4607 invalidate_class_lookup_cache ()
4611 /* This code can be seen as a cache miss. When we've cached a
4612 class' scope's bindings and we can't use them, we need to reset
4613 them. This is it! */
4614 for (t
= previous_class_values
; t
; t
= TREE_CHAIN (t
))
4615 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t
)) = NULL_TREE
;
4617 previous_class_type
= NULL_TREE
;
4620 /* Get out of the current class scope. If we were in a class scope
4621 previously, that is the one popped to. */
4627 /* Since poplevel_class does the popping of class decls nowadays,
4628 this really only frees the obstack used for these decls. */
4631 current_class_depth
--;
4632 current_class_name
= current_class_stack
[current_class_depth
].name
;
4633 current_class_type
= current_class_stack
[current_class_depth
].type
;
4634 current_access_specifier
= current_class_stack
[current_class_depth
].access
;
4635 if (current_class_stack
[current_class_depth
].names_used
)
4636 splay_tree_delete (current_class_stack
[current_class_depth
].names_used
);
4639 /* Returns 1 if current_class_type is either T or a nested type of T. */
4642 currently_open_class (t
)
4646 if (t
== current_class_type
)
4648 for (i
= 0; i
< current_class_depth
; ++i
)
4649 if (current_class_stack
[i
].type
== t
)
4654 /* When entering a class scope, all enclosing class scopes' names with
4655 static meaning (static variables, static functions, types and enumerators)
4656 have to be visible. This recursive function calls pushclass for all
4657 enclosing class contexts until global or a local scope is reached.
4658 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
4659 formal of the same name. */
4662 push_nested_class (type
, modify
)
4668 /* A namespace might be passed in error cases, like A::B:C. */
4669 if (type
== NULL_TREE
|| type
== error_mark_node
|| ! IS_AGGR_TYPE (type
)
4670 || TREE_CODE (type
) == NAMESPACE_DECL
4671 || TREE_CODE (type
) == TEMPLATE_TYPE_PARM
4672 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
4675 context
= DECL_CONTEXT (TYPE_MAIN_DECL (type
));
4677 if (context
&& CLASS_TYPE_P (context
))
4678 push_nested_class (context
, 2);
4679 pushclass (type
, modify
);
4682 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
4687 tree context
= DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type
));
4690 if (context
&& CLASS_TYPE_P (context
))
4691 pop_nested_class ();
4694 /* Set global variables CURRENT_LANG_NAME to appropriate value
4695 so that behavior of name-mangling machinery is correct. */
4698 push_lang_context (name
)
4701 *current_lang_stack
++ = current_lang_name
;
4702 if (current_lang_stack
>= current_lang_base
+ current_lang_stacksize
)
4705 = (tree
*)xrealloc (current_lang_base
,
4706 sizeof (tree
) * (current_lang_stacksize
+ 10));
4707 current_lang_stack
= current_lang_base
+ current_lang_stacksize
;
4708 current_lang_stacksize
+= 10;
4711 if (name
== lang_name_cplusplus
)
4713 strict_prototype
= strict_prototypes_lang_cplusplus
;
4714 current_lang_name
= name
;
4716 else if (name
== lang_name_java
)
4718 strict_prototype
= strict_prototypes_lang_cplusplus
;
4719 current_lang_name
= name
;
4720 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
4721 (See record_builtin_java_type in decl.c.) However, that causes
4722 incorrect debug entries if these types are actually used.
4723 So we re-enable debug output after extern "Java". */
4724 DECL_IGNORED_P (java_byte_type_node
) = 0;
4725 DECL_IGNORED_P (java_short_type_node
) = 0;
4726 DECL_IGNORED_P (java_int_type_node
) = 0;
4727 DECL_IGNORED_P (java_long_type_node
) = 0;
4728 DECL_IGNORED_P (java_float_type_node
) = 0;
4729 DECL_IGNORED_P (java_double_type_node
) = 0;
4730 DECL_IGNORED_P (java_char_type_node
) = 0;
4731 DECL_IGNORED_P (java_boolean_type_node
) = 0;
4733 else if (name
== lang_name_c
)
4735 strict_prototype
= strict_prototypes_lang_c
;
4736 current_lang_name
= name
;
4739 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name
));
4742 /* Get out of the current language scope. */
4747 current_lang_name
= *--current_lang_stack
;
4748 if (current_lang_name
== lang_name_cplusplus
4749 || current_lang_name
== lang_name_java
)
4750 strict_prototype
= strict_prototypes_lang_cplusplus
;
4751 else if (current_lang_name
== lang_name_c
)
4752 strict_prototype
= strict_prototypes_lang_c
;
4755 /* Type instantiation routines. */
4757 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
4758 matches the TARGET_TYPE. If there is no satisfactory match, return
4759 error_mark_node, and issue an error message if COMPLAIN is
4760 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
4761 was a template-id, and EXPLICIT_TARGS are the explicitly provided
4762 template arguments. */
4765 resolve_address_of_overloaded_function (target_type
,
4774 tree explicit_targs
;
4776 /* Here's what the standard says:
4780 If the name is a function template, template argument deduction
4781 is done, and if the argument deduction succeeds, the deduced
4782 arguments are used to generate a single template function, which
4783 is added to the set of overloaded functions considered.
4785 Non-member functions and static member functions match targets of
4786 type "pointer-to-function" or "reference-to-function." Nonstatic
4787 member functions match targets of type "pointer-to-member
4788 function;" the function type of the pointer to member is used to
4789 select the member function from the set of overloaded member
4790 functions. If a nonstatic member function is selected, the
4791 reference to the overloaded function name is required to have the
4792 form of a pointer to member as described in 5.3.1.
4794 If more than one function is selected, any template functions in
4795 the set are eliminated if the set also contains a non-template
4796 function, and any given template function is eliminated if the
4797 set contains a second template function that is more specialized
4798 than the first according to the partial ordering rules 14.5.5.2.
4799 After such eliminations, if any, there shall remain exactly one
4800 selected function. */
4803 int is_reference
= 0;
4804 /* We store the matches in a TREE_LIST rooted here. The functions
4805 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
4806 interoperability with most_specialized_instantiation. */
4807 tree matches
= NULL_TREE
;
4810 /* By the time we get here, we should be seeing only real
4811 pointer-to-member types, not the internal POINTER_TYPE to
4812 METHOD_TYPE representation. */
4813 my_friendly_assert (!(TREE_CODE (target_type
) == POINTER_TYPE
4814 && (TREE_CODE (TREE_TYPE (target_type
))
4815 == METHOD_TYPE
)), 0);
4817 /* Check that the TARGET_TYPE is reasonable. */
4818 if (TYPE_PTRFN_P (target_type
))
4821 else if (TYPE_PTRMEMFUNC_P (target_type
))
4822 /* This is OK, too. */
4824 else if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
4826 /* This is OK, too. This comes from a conversion to reference
4828 target_type
= build_reference_type (target_type
);
4834 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
4835 DECL_NAME (OVL_FUNCTION (overload
)), target_type
);
4836 return error_mark_node
;
4839 /* If we can find a non-template function that matches, we can just
4840 use it. There's no point in generating template instantiations
4841 if we're just going to throw them out anyhow. But, of course, we
4842 can only do this when we don't *need* a template function. */
4847 for (fns
= overload
; fns
; fns
= OVL_CHAIN (fns
))
4849 tree fn
= OVL_FUNCTION (fns
);
4852 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4853 /* We're not looking for templates just yet. */
4856 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
4858 /* We're looking for a non-static member, and this isn't
4859 one, or vice versa. */
4862 /* See if there's a match. */
4863 fntype
= TREE_TYPE (fn
);
4865 fntype
= build_ptrmemfunc_type (build_pointer_type (fntype
));
4866 else if (!is_reference
)
4867 fntype
= build_pointer_type (fntype
);
4869 if (can_convert_arg (target_type
, fntype
, fn
))
4870 matches
= scratch_tree_cons (fn
, NULL_TREE
, matches
);
4874 /* Now, if we've already got a match (or matches), there's no need
4875 to proceed to the template functions. But, if we don't have a
4876 match we need to look at them, too. */
4879 tree target_fn_type
;
4880 tree target_arg_types
;
4885 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type
));
4887 target_fn_type
= TREE_TYPE (target_type
);
4888 target_arg_types
= TYPE_ARG_TYPES (target_fn_type
);
4890 for (fns
= overload
; fns
; fns
= OVL_CHAIN (fns
))
4892 tree fn
= OVL_FUNCTION (fns
);
4894 tree instantiation_type
;
4897 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
4898 /* We're only looking for templates. */
4901 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
4903 /* We're not looking for a non-static member, and this is
4904 one, or vice versa. */
4907 /* Try to do argument deduction. */
4908 targs
= make_scratch_vec (DECL_NTPARMS (fn
));
4909 if (fn_type_unification (fn
, explicit_targs
, targs
,
4910 target_arg_types
, NULL_TREE
,
4912 /* Argument deduction failed. */
4915 /* Instantiate the template. */
4916 instantiation
= instantiate_template (fn
, targs
);
4917 if (instantiation
== error_mark_node
)
4918 /* Instantiation failed. */
4921 /* See if there's a match. */
4922 instantiation_type
= TREE_TYPE (instantiation
);
4924 instantiation_type
=
4925 build_ptrmemfunc_type (build_pointer_type (instantiation_type
));
4926 else if (!is_reference
)
4927 instantiation_type
= build_pointer_type (instantiation_type
);
4928 if (can_convert_arg (target_type
, instantiation_type
, instantiation
))
4929 matches
= scratch_tree_cons (instantiation
, fn
, matches
);
4932 /* Now, remove all but the most specialized of the matches. */
4935 tree match
= most_specialized_instantiation (matches
,
4938 if (match
!= error_mark_node
)
4939 matches
= scratch_tree_cons (match
, NULL_TREE
, NULL_TREE
);
4943 /* Now we should have exactly one function in MATCHES. */
4944 if (matches
== NULL_TREE
)
4946 /* There were *no* matches. */
4949 cp_error ("no matches converting function `%D' to type `%#T'",
4950 DECL_NAME (OVL_FUNCTION (overload
)),
4953 /* print_candidates expects a chain with the functions in
4954 TREE_VALUE slots, so we cons one up here (we're losing anyway,
4955 so why be clever?). */
4956 for (; overload
; overload
= OVL_NEXT (overload
))
4957 matches
= scratch_tree_cons (NULL_TREE
, OVL_CURRENT (overload
),
4960 print_candidates (matches
);
4962 return error_mark_node
;
4964 else if (TREE_CHAIN (matches
))
4966 /* There were too many matches. */
4972 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
4973 DECL_NAME (OVL_FUNCTION (overload
)),
4976 /* Since print_candidates expects the functions in the
4977 TREE_VALUE slot, we flip them here. */
4978 for (match
= matches
; match
; match
= TREE_CHAIN (match
))
4979 TREE_VALUE (match
) = TREE_PURPOSE (match
);
4981 print_candidates (matches
);
4984 return error_mark_node
;
4987 /* Good, exactly one match. Now, convert it to the correct type. */
4988 fn
= TREE_PURPOSE (matches
);
4992 if (TYPE_PTRFN_P (target_type
) || TYPE_PTRMEMFUNC_P (target_type
))
4993 return build_unary_op (ADDR_EXPR
, fn
, 0);
4996 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
4997 will mark the function as addressed, but here we must do it
4999 mark_addressable (fn
);
5005 /* This function will instantiate the type of the expression given in
5006 RHS to match the type of LHSTYPE. If errors exist, then return
5007 error_mark_node. We only complain is COMPLAIN is set. If we are
5008 not complaining, never modify rhs, as overload resolution wants to
5009 try many possible instantiations, in hopes that at least one will
5012 FLAGS is a bitmask, as we see at the top of the function.
5014 For non-recursive calls, LHSTYPE should be a function, pointer to
5015 function, or a pointer to member function. */
5018 instantiate_type (lhstype
, rhs
, flags
)
5022 int complain
= (flags
& 1);
5023 int strict
= (flags
& 2) ? COMPARE_NO_ATTRIBUTES
: COMPARE_STRICT
;
5025 if (TREE_CODE (lhstype
) == UNKNOWN_TYPE
)
5028 error ("not enough type information");
5029 return error_mark_node
;
5032 if (TREE_TYPE (rhs
) != NULL_TREE
&& ! (type_unknown_p (rhs
)))
5034 if (comptypes (lhstype
, TREE_TYPE (rhs
), strict
))
5037 cp_error ("argument of type `%T' does not match `%T'",
5038 TREE_TYPE (rhs
), lhstype
);
5039 return error_mark_node
;
5042 /* We don't overwrite rhs if it is an overloaded function.
5043 Copying it would destroy the tree link. */
5044 if (TREE_CODE (rhs
) != OVERLOAD
)
5045 rhs
= copy_node (rhs
);
5047 /* This should really only be used when attempting to distinguish
5048 what sort of a pointer to function we have. For now, any
5049 arithmetic operation which is not supported on pointers
5050 is rejected as an error. */
5052 switch (TREE_CODE (rhs
))
5059 my_friendly_abort (177);
5060 return error_mark_node
;
5067 new_rhs
= instantiate_type (build_pointer_type (lhstype
),
5068 TREE_OPERAND (rhs
, 0), flags
);
5069 if (new_rhs
== error_mark_node
)
5070 return error_mark_node
;
5072 TREE_TYPE (rhs
) = lhstype
;
5073 TREE_OPERAND (rhs
, 0) = new_rhs
;
5078 rhs
= copy_node (TREE_OPERAND (rhs
, 0));
5079 TREE_TYPE (rhs
) = unknown_type_node
;
5080 return instantiate_type (lhstype
, rhs
, flags
);
5084 tree field
= TREE_OPERAND (rhs
, 1);
5087 r
= instantiate_type (lhstype
, field
, flags
);
5089 if (r
!= error_mark_node
&& TYPE_PTRMEMFUNC_P (lhstype
))
5093 tree t
= TYPE_PTRMEMFUNC_OBJECT_TYPE (lhstype
);
5095 if (TREE_CODE (field
) == OVERLOAD
)
5096 field
= OVL_FUNCTION (field
);
5097 if (TREE_CODE (field
) == FUNCTION_DECL
)
5099 cp_pedwarn ("object-dependent reference `%E' can only be used in a call",
5101 cp_pedwarn (" to form a pointer to member function, say `&%T::%E'",
5102 t
, DECL_NAME (field
));
5105 cp_pedwarn ("object-dependent reference can only be used in a call");
5114 rhs
= TREE_OPERAND (rhs
, 1);
5115 if (BASELINK_P (rhs
))
5116 return instantiate_type (lhstype
, TREE_VALUE (rhs
), flags
);
5118 /* This can happen if we are forming a pointer-to-member for a
5120 my_friendly_assert (TREE_CODE (rhs
) == TEMPLATE_ID_EXPR
, 0);
5124 case TEMPLATE_ID_EXPR
:
5126 resolve_address_of_overloaded_function (lhstype
,
5127 TREE_OPERAND (rhs
, 0),
5129 /*template_only=*/1,
5130 TREE_OPERAND (rhs
, 1));
5134 resolve_address_of_overloaded_function (lhstype
,
5137 /*template_only=*/0,
5138 /*explicit_targs=*/NULL_TREE
);
5141 /* Now we should have a baselink. */
5142 my_friendly_assert (BASELINK_P (rhs
), 990412);
5144 return instantiate_type (lhstype
, TREE_VALUE (rhs
), flags
);
5147 /* This is too hard for now. */
5148 my_friendly_abort (183);
5149 return error_mark_node
;
5154 TREE_OPERAND (rhs
, 0)
5155 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), flags
);
5156 if (TREE_OPERAND (rhs
, 0) == error_mark_node
)
5157 return error_mark_node
;
5158 TREE_OPERAND (rhs
, 1)
5159 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
5160 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
5161 return error_mark_node
;
5163 TREE_TYPE (rhs
) = lhstype
;
5167 case TRUNC_DIV_EXPR
:
5168 case FLOOR_DIV_EXPR
:
5170 case ROUND_DIV_EXPR
:
5172 case TRUNC_MOD_EXPR
:
5173 case FLOOR_MOD_EXPR
:
5175 case ROUND_MOD_EXPR
:
5176 case FIX_ROUND_EXPR
:
5177 case FIX_FLOOR_EXPR
:
5179 case FIX_TRUNC_EXPR
:
5195 case PREINCREMENT_EXPR
:
5196 case PREDECREMENT_EXPR
:
5197 case POSTINCREMENT_EXPR
:
5198 case POSTDECREMENT_EXPR
:
5200 error ("invalid operation on uninstantiated type");
5201 return error_mark_node
;
5203 case TRUTH_AND_EXPR
:
5205 case TRUTH_XOR_EXPR
:
5212 case TRUTH_ANDIF_EXPR
:
5213 case TRUTH_ORIF_EXPR
:
5214 case TRUTH_NOT_EXPR
:
5216 error ("not enough type information");
5217 return error_mark_node
;
5220 if (type_unknown_p (TREE_OPERAND (rhs
, 0)))
5223 error ("not enough type information");
5224 return error_mark_node
;
5226 TREE_OPERAND (rhs
, 1)
5227 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
5228 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
5229 return error_mark_node
;
5230 TREE_OPERAND (rhs
, 2)
5231 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 2), flags
);
5232 if (TREE_OPERAND (rhs
, 2) == error_mark_node
)
5233 return error_mark_node
;
5235 TREE_TYPE (rhs
) = lhstype
;
5239 TREE_OPERAND (rhs
, 1)
5240 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
5241 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
5242 return error_mark_node
;
5244 TREE_TYPE (rhs
) = lhstype
;
5248 return instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), flags
);
5250 case ENTRY_VALUE_EXPR
:
5251 my_friendly_abort (184);
5252 return error_mark_node
;
5255 return error_mark_node
;
5258 my_friendly_abort (185);
5259 return error_mark_node
;
5263 /* Return the name of the virtual function pointer field
5264 (as an IDENTIFIER_NODE) for the given TYPE. Note that
5265 this may have to look back through base types to find the
5266 ultimate field name. (For single inheritance, these could
5267 all be the same name. Who knows for multiple inheritance). */
5270 get_vfield_name (type
)
5273 tree binfo
= TYPE_BINFO (type
);
5276 while (BINFO_BASETYPES (binfo
)
5277 && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo
, 0)))
5278 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo
, 0)))
5279 binfo
= BINFO_BASETYPE (binfo
, 0);
5281 type
= BINFO_TYPE (binfo
);
5282 buf
= (char *) alloca (sizeof (VFIELD_NAME_FORMAT
)
5283 + TYPE_NAME_LENGTH (type
) + 2);
5284 sprintf (buf
, VFIELD_NAME_FORMAT
, TYPE_NAME_STRING (type
));
5285 return get_identifier (buf
);
5289 print_class_statistics ()
5291 #ifdef GATHER_STATISTICS
5292 fprintf (stderr
, "convert_harshness = %d\n", n_convert_harshness
);
5293 fprintf (stderr
, "compute_conversion_costs = %d\n", n_compute_conversion_costs
);
5294 fprintf (stderr
, "build_method_call = %d (inner = %d)\n",
5295 n_build_method_call
, n_inner_fields_searched
);
5298 fprintf (stderr
, "vtables = %d; vtable searches = %d\n",
5299 n_vtables
, n_vtable_searches
);
5300 fprintf (stderr
, "vtable entries = %d; vtable elems = %d\n",
5301 n_vtable_entries
, n_vtable_elems
);
5306 /* Push an obstack which is sufficiently long-lived to hold such class
5307 decls that may be cached in the previous_class_values list. The
5308 effect is undone by pop_obstacks. */
5311 push_cache_obstack ()
5313 static int cache_obstack_initialized
;
5315 if (!cache_obstack_initialized
)
5317 gcc_obstack_init (&class_cache_obstack
);
5318 class_cache_firstobj
5319 = (char*) obstack_finish (&class_cache_obstack
);
5320 cache_obstack_initialized
= 1;
5323 push_obstacks_nochange ();
5324 current_obstack
= &class_cache_obstack
;
5327 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
5328 according to [class]:
5329 The class-name is also inserted
5330 into the scope of the class itself. For purposes of access checking,
5331 the inserted class name is treated as if it were a public member name. */
5334 build_self_reference ()
5336 tree name
= constructor_name (current_class_type
);
5337 tree value
= build_lang_decl (TYPE_DECL
, name
, current_class_type
);
5340 DECL_NONLOCAL (value
) = 1;
5341 DECL_CONTEXT (value
) = current_class_type
;
5342 DECL_CLASS_CONTEXT (value
) = current_class_type
;
5343 DECL_ARTIFICIAL (value
) = 1;
5345 saved_cas
= current_access_specifier
;
5346 current_access_specifier
= access_public_node
;
5347 finish_member_declaration (value
);
5348 current_access_specifier
= saved_cas
;
5351 /* Returns 1 if TYPE contains only padding bytes. */
5354 is_empty_class (type
)
5359 if (type
== error_mark_node
)
5362 if (! IS_AGGR_TYPE (type
))
5366 return CLASSTYPE_SIZE (type
) == integer_zero_node
;
5368 if (TYPE_BINFO_BASETYPES (type
))
5370 t
= TYPE_FIELDS (type
);
5371 while (t
&& TREE_CODE (t
) != FIELD_DECL
)
5373 return (t
== NULL_TREE
);
5376 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
5377 a *_TYPE node. NODE can also be a local class. */
5380 get_enclosing_class (type
)
5385 while (node
&& TREE_CODE (node
) != NAMESPACE_DECL
)
5387 switch (TREE_CODE_CLASS (TREE_CODE (node
)))
5390 node
= DECL_CONTEXT (node
);
5396 node
= TYPE_CONTEXT (node
);
5400 my_friendly_abort (0);
5406 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
5409 is_base_of_enclosing_class (base
, type
)
5414 if (get_binfo (base
, type
, 0))
5417 type
= get_enclosing_class (type
);
5422 /* Note that NAME was looked up while the current class was being
5423 defined and that the result of that lookup was DECL. */
5426 maybe_note_name_used_in_class (name
, decl
)
5430 splay_tree names_used
;
5432 /* If we're not defining a class, there's nothing to do. */
5433 if (!current_class_type
|| !TYPE_BEING_DEFINED (current_class_type
))
5436 /* If there's already a binding for this NAME, then we don't have
5437 anything to worry about. */
5438 if (IDENTIFIER_CLASS_VALUE (name
))
5441 if (!current_class_stack
[current_class_depth
- 1].names_used
)
5442 current_class_stack
[current_class_depth
- 1].names_used
5443 = splay_tree_new (splay_tree_compare_pointers
, 0, 0);
5444 names_used
= current_class_stack
[current_class_depth
- 1].names_used
;
5446 splay_tree_insert (names_used
,
5447 (splay_tree_key
) name
,
5448 (splay_tree_value
) decl
);
5451 /* Note that NAME was declared (as DECL) in the current class. Check
5452 to see that the declaration is legal. */
5455 note_name_declared_in_class (name
, decl
)
5459 splay_tree names_used
;
5462 /* Look to see if we ever used this name. */
5464 = current_class_stack
[current_class_depth
- 1].names_used
;
5468 n
= splay_tree_lookup (names_used
, (splay_tree_key
) name
);
5471 /* [basic.scope.class]
5473 A name N used in a class S shall refer to the same declaration
5474 in its context and when re-evaluated in the completed scope of
5476 cp_error ("declaration of `%#D'", decl
);
5477 cp_error_at ("changes meaning of `%s' from `%+#D'",
5478 IDENTIFIER_POINTER (DECL_NAME (decl
)),