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"
37 #define obstack_chunk_alloc xmalloc
38 #define obstack_chunk_free free
40 /* This is how we tell when two virtual member functions are really the
42 #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
44 extern void set_class_shadows
PROTO ((tree
));
46 /* The number of nested classes being processed. If we are not in the
47 scope of any class, this is zero. */
49 int current_class_depth
;
51 /* In order to deal with nested classes, we keep a stack of classes.
52 The topmost entry is the innermost class, and is the entry at index
53 CURRENT_CLASS_DEPTH */
55 typedef struct class_stack_node
{
56 /* The name of the class. */
59 /* The _TYPE node for the class. */
62 /* The access specifier pending for new declarations in the scope of
66 /* If were defining TYPE, the names used in this class. */
67 splay_tree names_used
;
68 }* class_stack_node_t
;
70 /* The stack itself. This is an dynamically resized array. The
71 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
72 static int current_class_stack_size
;
73 static class_stack_node_t current_class_stack
;
77 static tree get_vfield_name
PROTO((tree
));
78 static void finish_struct_anon
PROTO((tree
));
79 static tree build_vbase_pointer
PROTO((tree
, tree
));
80 static tree build_vtable_entry
PROTO((tree
, tree
));
81 static tree get_vtable_name
PROTO((tree
));
82 static tree get_derived_offset
PROTO((tree
, tree
));
83 static tree get_basefndecls
PROTO((tree
, tree
));
84 static void set_rtti_entry
PROTO((tree
, tree
, tree
));
85 static tree build_vtable
PROTO((tree
, tree
));
86 static void prepare_fresh_vtable
PROTO((tree
, tree
));
87 static void fixup_vtable_deltas1
PROTO((tree
, tree
));
88 static void fixup_vtable_deltas
PROTO((tree
, int, tree
));
89 static void finish_vtbls
PROTO((tree
, int, tree
));
90 static void modify_vtable_entry
PROTO((tree
, tree
, tree
));
91 static tree get_vtable_entry_n
PROTO((tree
, unsigned HOST_WIDE_INT
));
92 static void add_virtual_function
PROTO((tree
*, tree
*, int *, tree
, tree
));
93 static tree delete_duplicate_fields_1
PROTO((tree
, tree
));
94 static void delete_duplicate_fields
PROTO((tree
));
95 static void finish_struct_bits
PROTO((tree
, int));
96 static int alter_access
PROTO((tree
, tree
, tree
, tree
));
97 static void handle_using_decl
PROTO((tree
, tree
, tree
, tree
));
98 static int overrides
PROTO((tree
, tree
));
99 static int strictly_overrides
PROTO((tree
, tree
));
100 static void merge_overrides
PROTO((tree
, tree
, int, tree
));
101 static void override_one_vtable
PROTO((tree
, tree
, tree
));
102 static void mark_overriders
PROTO((tree
, tree
));
103 static void check_for_override
PROTO((tree
, tree
));
104 static tree get_class_offset_1
PROTO((tree
, tree
, tree
, tree
, tree
));
105 static tree get_class_offset
PROTO((tree
, tree
, tree
, tree
));
106 static void modify_one_vtable
PROTO((tree
, tree
, tree
));
107 static void modify_all_vtables
PROTO((tree
, tree
));
108 static void modify_all_direct_vtables
PROTO((tree
, int, tree
, tree
));
109 static void modify_all_indirect_vtables
PROTO((tree
, int, int, tree
, tree
));
110 static int finish_base_struct
PROTO((tree
, struct base_info
*));
111 static void finish_struct_methods
PROTO((tree
));
112 static void maybe_warn_about_overly_private_class
PROTO ((tree
));
113 static int field_decl_cmp
PROTO ((const tree
*, const tree
*));
114 static int method_name_cmp
PROTO ((const tree
*, const tree
*));
115 static tree add_implicitly_declared_members
PROTO((tree
, int, int, int));
116 static tree fixed_type_or_null
PROTO((tree
, int *));
117 static tree resolve_address_of_overloaded_function
PROTO((tree
, tree
, int,
119 static void build_vtable_entry_ref
PROTO((tree
, tree
, tree
));
120 static tree build_vtable_entry_for_fn
PROTO((tree
, tree
));
121 static tree build_vtbl_initializer
PROTO((tree
));
122 static int count_fields
PROTO((tree
));
123 static int add_fields_to_vec
PROTO((tree
, tree
, int));
125 /* Variables shared between class.c and call.c. */
127 #ifdef GATHER_STATISTICS
129 int n_vtable_entries
= 0;
130 int n_vtable_searches
= 0;
131 int n_vtable_elems
= 0;
132 int n_convert_harshness
= 0;
133 int n_compute_conversion_costs
= 0;
134 int n_build_method_call
= 0;
135 int n_inner_fields_searched
= 0;
138 /* Virtual baseclass things. */
141 build_vbase_pointer (exp
, type
)
145 FORMAT_VBASE_NAME (name
, type
);
147 return build_component_ref (exp
, get_identifier (name
), NULL_TREE
, 0);
151 /* Is the type of the EXPR, the complete type of the object?
152 If we are going to be wrong, we must be conservative, and return 0. */
155 complete_type_p (expr
)
158 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (expr
));
161 switch (TREE_CODE (expr
))
168 expr
= TREE_OPERAND (expr
, 0);
172 if (! TREE_HAS_CONSTRUCTOR (expr
))
174 /* fall through... */
177 if (TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
178 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr
)))
179 && TYPE_MAIN_VARIANT (TREE_TYPE (expr
)) == type
)
181 /* fall through... */
184 if (IS_AGGR_TYPE (TREE_TYPE (expr
))
185 && TYPE_MAIN_VARIANT (TREE_TYPE (expr
)) == type
)
187 /* fall through... */
198 /* Build multi-level access to EXPR using hierarchy path PATH.
199 CODE is PLUS_EXPR if we are going with the grain,
200 and MINUS_EXPR if we are not (in which case, we cannot traverse
201 virtual baseclass links).
203 TYPE is the type we want this path to have on exit.
205 NONNULL is non-zero if we know (for any reason) that EXPR is
206 not, in fact, zero. */
209 build_vbase_path (code
, type
, expr
, path
, nonnull
)
211 tree type
, expr
, path
;
214 register int changed
= 0;
215 tree last
= NULL_TREE
, last_virtual
= NULL_TREE
;
217 tree null_expr
= 0, nonnull_expr
;
219 tree offset
= integer_zero_node
;
221 if (BINFO_INHERITANCE_CHAIN (path
) == NULL_TREE
)
222 return build1 (NOP_EXPR
, type
, expr
);
224 /* We could do better if we had additional logic to convert back to the
225 unconverted type (the static type of the complete object), and then
226 convert back to the type we want. Until that is done, we only optimize
227 if the complete type is the same type as expr has. */
228 fixed_type_p
= resolves_to_fixed_type_p (expr
, &nonnull
);
230 if (!fixed_type_p
&& TREE_SIDE_EFFECTS (expr
))
231 expr
= save_expr (expr
);
234 if (BINFO_INHERITANCE_CHAIN (path
))
235 path
= reverse_path (path
);
237 basetype
= BINFO_TYPE (path
);
241 if (TREE_VIA_VIRTUAL (path
))
243 last_virtual
= BINFO_TYPE (path
);
244 if (code
== PLUS_EXPR
)
246 changed
= ! fixed_type_p
;
252 /* We already check for ambiguous things in the caller, just
256 tree binfo
= get_binfo (last
, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr
))), 0);
257 nonnull_expr
= convert_pointer_to_real (binfo
, nonnull_expr
);
259 ind
= build_indirect_ref (nonnull_expr
, NULL_PTR
);
260 nonnull_expr
= build_vbase_pointer (ind
, last_virtual
);
262 && TREE_CODE (type
) == POINTER_TYPE
263 && null_expr
== NULL_TREE
)
265 null_expr
= build1 (NOP_EXPR
, build_pointer_type (last_virtual
), integer_zero_node
);
266 expr
= build (COND_EXPR
, build_pointer_type (last_virtual
),
267 build (EQ_EXPR
, boolean_type_node
, expr
,
269 null_expr
, nonnull_expr
);
272 /* else we'll figure out the offset below. */
274 /* Happens in the case of parse errors. */
275 if (nonnull_expr
== error_mark_node
)
276 return error_mark_node
;
280 cp_error ("cannot cast up from virtual baseclass `%T'",
282 return error_mark_node
;
286 path
= BINFO_INHERITANCE_CHAIN (path
);
288 /* LAST is now the last basetype assoc on the path. */
290 /* A pointer to a virtual base member of a non-null object
291 is non-null. Therefore, we only need to test for zeroness once.
292 Make EXPR the canonical expression to deal with here. */
295 TREE_OPERAND (expr
, 2) = nonnull_expr
;
296 TREE_TYPE (expr
) = TREE_TYPE (TREE_OPERAND (expr
, 1))
297 = TREE_TYPE (nonnull_expr
);
302 /* If we go through any virtual base pointers, make sure that
303 casts to BASETYPE from the last virtual base class use
304 the right value for BASETYPE. */
307 tree intype
= TREE_TYPE (TREE_TYPE (expr
));
308 if (TYPE_MAIN_VARIANT (intype
) != BINFO_TYPE (last
))
310 tree binfo
= get_binfo (last
, TYPE_MAIN_VARIANT (intype
), 0);
311 offset
= BINFO_OFFSET (binfo
);
318 offset
= BINFO_OFFSET (binfo_member (last_virtual
,
319 CLASSTYPE_VBASECLASSES (basetype
)));
320 offset
= size_binop (PLUS_EXPR
, offset
, BINFO_OFFSET (last
));
323 offset
= BINFO_OFFSET (last
);
326 if (TREE_INT_CST_LOW (offset
))
328 /* Bash types to make the backend happy. */
329 offset
= cp_convert (type
, offset
);
331 /* This shouldn't be necessary. (mrs) */
332 expr
= build1 (NOP_EXPR
, type
, expr
);
335 /* If expr might be 0, we need to preserve that zeroness. */
339 TREE_TYPE (null_expr
) = type
;
341 null_expr
= build1 (NOP_EXPR
, type
, integer_zero_node
);
342 if (TREE_SIDE_EFFECTS (expr
))
343 expr
= save_expr (expr
);
345 return build (COND_EXPR
, type
,
346 build (EQ_EXPR
, boolean_type_node
, expr
, integer_zero_node
),
348 build (code
, type
, expr
, offset
));
350 else return build (code
, type
, expr
, offset
);
353 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
354 be used multiple times in initialization of multiple inheritance. */
357 TREE_TYPE (expr
) = type
;
361 return build1 (NOP_EXPR
, type
, expr
);
364 /* Virtual function things. */
366 /* Build an entry in the virtual function table.
367 DELTA is the offset for the `this' pointer.
368 PFN is an ADDR_EXPR containing a pointer to the virtual function.
369 Note that the index (DELTA2) in the virtual function table
373 build_vtable_entry (delta
, pfn
)
376 if (flag_vtable_thunks
)
378 HOST_WIDE_INT idelta
= TREE_INT_CST_LOW (delta
);
379 if (idelta
&& ! DECL_ABSTRACT_VIRTUAL_P (TREE_OPERAND (pfn
, 0)))
381 pfn
= build1 (ADDR_EXPR
, vtable_entry_type
,
382 make_thunk (pfn
, idelta
));
383 TREE_READONLY (pfn
) = 1;
384 TREE_CONSTANT (pfn
) = 1;
386 #ifdef GATHER_STATISTICS
387 n_vtable_entries
+= 1;
393 extern int flag_huge_objects
;
394 tree elems
= tree_cons (NULL_TREE
, delta
,
395 tree_cons (NULL_TREE
, integer_zero_node
,
396 build_expr_list (NULL_TREE
, pfn
)));
397 tree entry
= build (CONSTRUCTOR
, vtable_entry_type
, NULL_TREE
, elems
);
399 /* DELTA used to be constructed by `size_int' and/or size_binop,
400 which caused overflow problems when it was negative. That should
403 if (! int_fits_type_p (delta
, delta_type_node
))
405 if (flag_huge_objects
)
406 sorry ("object size exceeds built-in limit for virtual function table implementation");
408 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
411 TREE_CONSTANT (entry
) = 1;
412 TREE_STATIC (entry
) = 1;
413 TREE_READONLY (entry
) = 1;
415 #ifdef GATHER_STATISTICS
416 n_vtable_entries
+= 1;
423 /* Build a vtable entry for FNDECL. DELTA is the amount by which we
424 must adjust the this pointer when calling F. */
427 build_vtable_entry_for_fn (delta
, fndecl
)
433 /* Take the address of the function, considering it to be of an
434 appropriate generic type. */
435 pfn
= build1 (ADDR_EXPR
, vfunc_ptr_type_node
, fndecl
);
436 /* The address of a function can't change. */
437 TREE_CONSTANT (pfn
) = 1;
438 /* Now build the vtable entry itself. */
439 return build_vtable_entry (delta
, pfn
);
442 /* We want to give the assembler the vtable identifier as well as
443 the offset to the function pointer. So we generate
445 __asm__ __volatile__ (".vtable_entry %c0, %c1"
446 : : "s"(&class_vtable),
447 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
450 build_vtable_entry_ref (basetype
, vtbl
, idx
)
451 tree basetype
, vtbl
, idx
;
453 static char asm_stmt
[] = ".vtable_entry %c0, %c1";
456 s
= build_unary_op (ADDR_EXPR
, TYPE_BINFO_VTABLE (basetype
), 0);
457 s
= build_tree_list (build_string (1, "s"), s
);
459 i
= build_array_ref (vtbl
, idx
);
460 if (!flag_vtable_thunks
)
461 i
= build_component_ref (i
, pfn_identifier
, vtable_entry_type
, 0);
462 i
= build_c_cast (ptrdiff_type_node
, build_unary_op (ADDR_EXPR
, i
, 0));
463 i2
= build_array_ref (vtbl
, build_int_2(0,0));
464 i2
= build_c_cast (ptrdiff_type_node
, build_unary_op (ADDR_EXPR
, i2
, 0));
465 i
= build_binary_op (MINUS_EXPR
, i
, i2
);
466 i
= build_tree_list (build_string (1, "i"), i
);
468 expand_asm_operands (build_string (sizeof(asm_stmt
)-1, asm_stmt
),
469 NULL_TREE
, chainon (s
, i
), NULL_TREE
, 1, NULL
, 0);
472 /* Given an object INSTANCE, return an expression which yields the
473 virtual function vtable element corresponding to INDEX. There are
474 many special cases for INSTANCE which we take care of here, mainly
475 to avoid creating extra tree nodes when we don't have to. */
478 build_vtbl_ref (instance
, idx
)
482 tree basetype
= TREE_TYPE (instance
);
484 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
485 basetype
= TREE_TYPE (basetype
);
487 if (instance
== current_class_ref
)
488 vtbl
= build_vfield_ref (instance
, basetype
);
493 /* Try to figure out what a reference refers to, and
494 access its virtual function table directly. */
495 tree ref
= NULL_TREE
;
497 if (TREE_CODE (instance
) == INDIRECT_REF
498 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance
, 0))) == REFERENCE_TYPE
)
499 ref
= TREE_OPERAND (instance
, 0);
500 else if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
503 if (ref
&& TREE_CODE (ref
) == VAR_DECL
504 && DECL_INITIAL (ref
))
506 tree init
= DECL_INITIAL (ref
);
508 while (TREE_CODE (init
) == NOP_EXPR
509 || TREE_CODE (init
) == NON_LVALUE_EXPR
)
510 init
= TREE_OPERAND (init
, 0);
511 if (TREE_CODE (init
) == ADDR_EXPR
)
513 init
= TREE_OPERAND (init
, 0);
514 if (IS_AGGR_TYPE (TREE_TYPE (init
))
515 && (TREE_CODE (init
) == PARM_DECL
516 || TREE_CODE (init
) == VAR_DECL
))
522 if (IS_AGGR_TYPE (TREE_TYPE (instance
))
523 && (TREE_CODE (instance
) == RESULT_DECL
524 || TREE_CODE (instance
) == PARM_DECL
525 || TREE_CODE (instance
) == VAR_DECL
))
526 vtbl
= TYPE_BINFO_VTABLE (basetype
);
528 vtbl
= build_vfield_ref (instance
, basetype
);
531 assemble_external (vtbl
);
534 build_vtable_entry_ref (basetype
, vtbl
, idx
);
536 aref
= build_array_ref (vtbl
, idx
);
541 /* Given an object INSTANCE, return an expression which yields the
542 virtual function corresponding to INDEX. There are many special
543 cases for INSTANCE which we take care of here, mainly to avoid
544 creating extra tree nodes when we don't have to. */
547 build_vfn_ref (ptr_to_instptr
, instance
, idx
)
548 tree
*ptr_to_instptr
, instance
;
551 tree aref
= build_vtbl_ref (instance
, idx
);
553 /* When using thunks, there is no extra delta, and we get the pfn
555 if (flag_vtable_thunks
)
560 /* Save the intermediate result in a SAVE_EXPR so we don't have to
561 compute each component of the virtual function pointer twice. */
562 if (TREE_CODE (aref
) == INDIRECT_REF
)
563 TREE_OPERAND (aref
, 0) = save_expr (TREE_OPERAND (aref
, 0));
566 = build (PLUS_EXPR
, TREE_TYPE (*ptr_to_instptr
),
568 cp_convert (ptrdiff_type_node
,
569 build_component_ref (aref
, delta_identifier
, NULL_TREE
, 0)));
572 return build_component_ref (aref
, pfn_identifier
, NULL_TREE
, 0);
575 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
576 for the given TYPE. */
579 get_vtable_name (type
)
582 tree type_id
= build_typename_overload (type
);
583 char *buf
= (char *) alloca (strlen (VTABLE_NAME_FORMAT
)
584 + IDENTIFIER_LENGTH (type_id
) + 2);
585 const char *ptr
= IDENTIFIER_POINTER (type_id
);
587 for (i
= 0; ptr
[i
] == OPERATOR_TYPENAME_FORMAT
[i
]; i
++) ;
589 /* We don't take off the numbers; prepare_fresh_vtable uses the
590 DECL_ASSEMBLER_NAME for the type, which includes the number
591 in `3foo'. If we were to pull them off here, we'd end up with
592 something like `_vt.foo.3bar', instead of a uniform definition. */
593 while (ptr
[i
] >= '0' && ptr
[i
] <= '9')
596 sprintf (buf
, VTABLE_NAME_FORMAT
, ptr
+i
);
597 return get_identifier (buf
);
600 /* Return the offset to the main vtable for a given base BINFO. */
603 get_vfield_offset (binfo
)
607 = size_binop (FLOOR_DIV_EXPR
,
608 DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo
))),
609 size_int (BITS_PER_UNIT
));
610 tmp
= convert (sizetype
, tmp
);
611 return size_binop (PLUS_EXPR
, tmp
, BINFO_OFFSET (binfo
));
614 /* Get the offset to the start of the original binfo that we derived
615 this binfo from. If we find TYPE first, return the offset only
616 that far. The shortened search is useful because the this pointer
617 on method calling is expected to point to a DECL_CONTEXT (fndecl)
618 object, and not a baseclass of it. */
621 get_derived_offset (binfo
, type
)
624 tree offset1
= get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo
)));
627 while (BINFO_BASETYPES (binfo
)
628 && (i
=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
))) != -1)
630 tree binfos
= BINFO_BASETYPES (binfo
);
631 if (BINFO_TYPE (binfo
) == type
)
633 binfo
= TREE_VEC_ELT (binfos
, i
);
635 offset2
= get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo
)));
636 return size_binop (MINUS_EXPR
, offset1
, offset2
);
639 /* Update the rtti info for this class. */
642 set_rtti_entry (virtuals
, offset
, type
)
643 tree virtuals
, offset
, type
;
647 if (CLASSTYPE_COM_INTERFACE (type
))
651 fn
= get_tinfo_fn (type
);
653 /* If someone tries to get RTTI information for a type compiled
654 without RTTI, they're out of luck. By calling __pure_virtual
655 in this case, we give a small clue as to what went wrong. We
656 could consider having a __no_typeinfo function as well, for a
657 more specific hint. */
660 if (flag_vtable_thunks
)
662 /* The first slot holds the offset. */
663 TREE_PURPOSE (virtuals
) = offset
;
665 /* The next node holds the function. */
666 virtuals
= TREE_CHAIN (virtuals
);
667 offset
= integer_zero_node
;
670 /* This slot holds the function to call. */
671 TREE_PURPOSE (virtuals
) = offset
;
672 TREE_VALUE (virtuals
) = fn
;
675 /* Build a virtual function for type TYPE.
676 If BINFO is non-NULL, build the vtable starting with the initial
677 approximation that it is the same as the one which is the head of
678 the association list. */
681 build_vtable (binfo
, type
)
684 tree name
= get_vtable_name (type
);
691 virtuals
= copy_list (BINFO_VIRTUALS (binfo
));
692 decl
= build_lang_decl (VAR_DECL
, name
,
693 TREE_TYPE (BINFO_VTABLE (binfo
)));
695 /* Now do rtti stuff. */
696 offset
= get_derived_offset (TYPE_BINFO (type
), NULL_TREE
);
697 offset
= ssize_binop (MINUS_EXPR
, integer_zero_node
, offset
);
698 set_rtti_entry (virtuals
, offset
, type
);
702 virtuals
= NULL_TREE
;
703 decl
= build_lang_decl (VAR_DECL
, name
, void_type_node
);
706 #ifdef GATHER_STATISTICS
708 n_vtable_elems
+= list_length (virtuals
);
711 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
712 import_export_vtable (decl
, type
, 0);
714 decl
= pushdecl_top_level (decl
);
715 SET_IDENTIFIER_GLOBAL_VALUE (name
, decl
);
716 /* Initialize the association list for this type, based
717 on our first approximation. */
718 TYPE_BINFO_VTABLE (type
) = decl
;
719 TYPE_BINFO_VIRTUALS (type
) = virtuals
;
721 DECL_ARTIFICIAL (decl
) = 1;
722 TREE_STATIC (decl
) = 1;
723 #ifndef WRITABLE_VTABLES
724 /* Make them READONLY by default. (mrs) */
725 TREE_READONLY (decl
) = 1;
727 /* At one time the vtable info was grabbed 2 words at a time. This
728 fails on sparc unless you have 8-byte alignment. (tiemann) */
729 DECL_ALIGN (decl
) = MAX (TYPE_ALIGN (double_type_node
),
732 DECL_VIRTUAL_P (decl
) = 1;
733 DECL_CONTEXT (decl
) = type
;
735 binfo
= TYPE_BINFO (type
);
736 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
740 /* Give TYPE a new virtual function table which is initialized
741 with a skeleton-copy of its original initialization. The only
742 entry that changes is the `delta' entry, so we can really
743 share a lot of structure.
745 FOR_TYPE is the derived type which caused this table to
748 BINFO is the type association which provided TYPE for FOR_TYPE.
750 The order in which vtables are built (by calling this function) for
751 an object must remain the same, otherwise a binary incompatibility
755 prepare_fresh_vtable (binfo
, for_type
)
756 tree binfo
, for_type
;
759 tree orig_decl
= BINFO_VTABLE (binfo
);
772 basetype
= TYPE_MAIN_VARIANT (BINFO_TYPE (binfo
));
774 buf2
= TYPE_ASSEMBLER_NAME_STRING (basetype
);
775 i
= TYPE_ASSEMBLER_NAME_LENGTH (basetype
) + 1;
777 /* We know that the vtable that we are going to create doesn't exist
778 yet in the global namespace, and when we finish, it will be
779 pushed into the global namespace. In complex MI hierarchies, we
780 have to loop while the name we are thinking of adding is globally
781 defined, adding more name components to the vtable name as we
782 loop, until the name is unique. This is because in complex MI
783 cases, we might have the same base more than once. This means
784 that the order in which this function is called for vtables must
785 remain the same, otherwise binary compatibility can be
790 char *buf1
= (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type
)
794 sprintf (buf1
, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type
), joiner
,
796 buf
= (char *) alloca (strlen (VTABLE_NAME_FORMAT
) + strlen (buf1
) + 1);
797 sprintf (buf
, VTABLE_NAME_FORMAT
, buf1
);
798 name
= get_identifier (buf
);
800 /* If this name doesn't clash, then we can use it, otherwise
801 we add more to the name until it is unique. */
803 if (! IDENTIFIER_GLOBAL_VALUE (name
))
806 /* Set values for next loop through, if the name isn't unique. */
808 path
= BINFO_INHERITANCE_CHAIN (path
);
810 /* We better not run out of stuff to make it unique. */
811 my_friendly_assert (path
!= NULL_TREE
, 368);
813 basetype
= TYPE_MAIN_VARIANT (BINFO_TYPE (path
));
815 if (for_type
== basetype
)
817 /* If we run out of basetypes in the path, we have already
818 found created a vtable with that name before, we now
819 resort to tacking on _%d to distinguish them. */
821 i
= TYPE_ASSEMBLER_NAME_LENGTH (basetype
) + 1 + i
+ 1 + 3;
822 buf1
= (char *) alloca (i
);
824 sprintf (buf1
, "%s%c%s%c%d",
825 TYPE_ASSEMBLER_NAME_STRING (basetype
), joiner
,
827 buf
= (char *) alloca (strlen (VTABLE_NAME_FORMAT
)
828 + strlen (buf1
) + 1);
829 sprintf (buf
, VTABLE_NAME_FORMAT
, buf1
);
830 name
= get_identifier (buf
);
832 /* If this name doesn't clash, then we can use it,
833 otherwise we add something different to the name until
835 } while (++j
<= 999 && IDENTIFIER_GLOBAL_VALUE (name
));
837 /* Hey, they really like MI don't they? Increase the 3
838 above to 6, and the 999 to 999999. :-) */
839 my_friendly_assert (j
<= 999, 369);
844 i
= TYPE_ASSEMBLER_NAME_LENGTH (basetype
) + 1 + i
;
845 new_buf2
= (char *) alloca (i
);
846 sprintf (new_buf2
, "%s%c%s",
847 TYPE_ASSEMBLER_NAME_STRING (basetype
), joiner
, buf2
);
851 new_decl
= build_lang_decl (VAR_DECL
, name
, TREE_TYPE (orig_decl
));
852 /* Remember which class this vtable is really for. */
853 DECL_CONTEXT (new_decl
) = for_type
;
855 DECL_ARTIFICIAL (new_decl
) = 1;
856 TREE_STATIC (new_decl
) = 1;
857 BINFO_VTABLE (binfo
) = pushdecl_top_level (new_decl
);
858 DECL_VIRTUAL_P (new_decl
) = 1;
859 #ifndef WRITABLE_VTABLES
860 /* Make them READONLY by default. (mrs) */
861 TREE_READONLY (new_decl
) = 1;
863 DECL_ALIGN (new_decl
) = DECL_ALIGN (orig_decl
);
865 /* Make fresh virtual list, so we can smash it later. */
866 BINFO_VIRTUALS (binfo
) = copy_list (BINFO_VIRTUALS (binfo
));
868 if (TREE_VIA_VIRTUAL (binfo
))
870 tree binfo1
= binfo_member (BINFO_TYPE (binfo
),
871 CLASSTYPE_VBASECLASSES (for_type
));
873 /* XXX - This should never happen, if it does, the caller should
874 ensure that the binfo is from for_type's binfos, not from any
875 base type's. We can remove all this code after a while. */
877 warning ("internal inconsistency: binfo offset error for rtti");
879 offset
= BINFO_OFFSET (binfo1
);
882 offset
= BINFO_OFFSET (binfo
);
884 set_rtti_entry (BINFO_VIRTUALS (binfo
),
885 ssize_binop (MINUS_EXPR
, integer_zero_node
, offset
),
888 #ifdef GATHER_STATISTICS
890 n_vtable_elems
+= list_length (BINFO_VIRTUALS (binfo
));
893 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
894 import_export_vtable (new_decl
, for_type
, 0);
896 if (TREE_VIA_VIRTUAL (binfo
))
897 my_friendly_assert (binfo
== binfo_member (BINFO_TYPE (binfo
),
898 CLASSTYPE_VBASECLASSES (current_class_type
)),
900 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
904 /* Access the virtual function table entry that logically
905 contains BASE_FNDECL. VIRTUALS is the virtual function table's
906 initializer. We can run off the end, when dealing with virtual
907 destructors in MI situations, return NULL_TREE in that case. */
910 get_vtable_entry (virtuals
, base_fndecl
)
911 tree virtuals
, base_fndecl
;
913 unsigned HOST_WIDE_INT n
= (HOST_BITS_PER_WIDE_INT
>= BITS_PER_WORD
914 ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl
))
915 & (((unsigned HOST_WIDE_INT
)1<<(BITS_PER_WORD
-1))-1))
916 : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl
)));
918 #ifdef GATHER_STATISTICS
919 n_vtable_searches
+= n
;
922 while (n
> 0 && virtuals
)
925 virtuals
= TREE_CHAIN (virtuals
);
931 /* Change the offset for the FNDECL entry to NEW_OFFSET. Also update
932 DECL_VINDEX (FNDECL). */
935 modify_vtable_entry (old_entry_in_list
, new_offset
, fndecl
)
936 tree old_entry_in_list
, new_offset
, fndecl
;
938 tree base_fndecl
= TREE_VALUE (old_entry_in_list
);
940 /* Update the entry. */
941 TREE_PURPOSE (old_entry_in_list
) = new_offset
;
942 TREE_VALUE (old_entry_in_list
) = fndecl
;
944 /* Now assign virtual dispatch information, if unset. We can
945 dispatch this, through any overridden base function. */
946 if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
948 DECL_VINDEX (fndecl
) = DECL_VINDEX (base_fndecl
);
949 DECL_CONTEXT (fndecl
) = DECL_CONTEXT (base_fndecl
);
953 /* Access the virtual function table entry N. VIRTUALS is the virtual
954 function table's initializer. */
957 get_vtable_entry_n (virtuals
, n
)
959 unsigned HOST_WIDE_INT n
;
964 virtuals
= TREE_CHAIN (virtuals
);
969 /* Add a virtual function to all the appropriate vtables for the class
970 T. DECL_VINDEX(X) should be error_mark_node, if we want to
971 allocate a new slot in our table. If it is error_mark_node, we
972 know that no other function from another vtable is overridden by X.
973 HAS_VIRTUAL keeps track of how many virtuals there are in our main
974 vtable for the type, and we build upon the PENDING_VIRTUALS list
978 add_virtual_function (pv
, phv
, has_virtual
, fndecl
, t
)
982 tree t
; /* Structure type. */
984 tree pending_virtuals
= *pv
;
985 tree pending_hard_virtuals
= *phv
;
988 if (current_class_type
== 0)
989 cp_warning ("internal problem, current_class_type is zero when adding `%D', please report",
991 if (current_class_type
&& t
!= current_class_type
)
992 cp_warning ("internal problem, current_class_type differs when adding `%D', please report",
996 /* If the virtual function is a redefinition of a prior one,
997 figure out in which base class the new definition goes,
998 and if necessary, make a fresh virtual function table
999 to hold that entry. */
1000 if (DECL_VINDEX (fndecl
) == error_mark_node
)
1002 /* We remember that this was the base sub-object for rtti. */
1003 CLASSTYPE_RTTI (t
) = t
;
1005 /* If we are using thunks, use two slots at the front, one
1006 for the offset pointer, one for the tdesc pointer.
1007 For ARM-style vtables, use the same slot for both. */
1008 if (*has_virtual
== 0 && ! CLASSTYPE_COM_INTERFACE (t
))
1010 if (flag_vtable_thunks
)
1016 /* Build a new INT_CST for this DECL_VINDEX. */
1018 static tree index_table
[256];
1020 /* We skip a slot for the offset/tdesc entry. */
1021 int i
= (*has_virtual
)++;
1023 if (i
>= 256 || index_table
[i
] == 0)
1025 idx
= build_int_2 (i
, 0);
1027 index_table
[i
] = idx
;
1030 idx
= index_table
[i
];
1032 /* Now assign virtual dispatch information. */
1033 DECL_VINDEX (fndecl
) = idx
;
1034 DECL_CONTEXT (fndecl
) = t
;
1036 /* Save the state we've computed on the PENDING_VIRTUALS list. */
1037 pending_virtuals
= tree_cons (integer_zero_node
,
1041 /* Might already be INTEGER_CST if declared twice in class. We will
1042 give error later or we've already given it. */
1043 else if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
1045 /* Need an entry in some other virtual function table.
1046 Deal with this after we have laid out our virtual base classes. */
1047 pending_hard_virtuals
= temp_tree_cons (NULL_TREE
,
1049 pending_hard_virtuals
);
1051 *pv
= pending_virtuals
;
1052 *phv
= pending_hard_virtuals
;
1055 extern struct obstack
*current_obstack
;
1057 /* Add method METHOD to class TYPE.
1059 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1060 the class type where the method should be added. */
1063 add_method (type
, fields
, method
)
1064 tree type
, *fields
, method
;
1066 push_permanent_obstack ();
1068 /* Setting the DECL_CONTEXT and DECL_CLASS_CONTEXT here is probably
1070 DECL_CONTEXT (method
) = type
;
1071 DECL_CLASS_CONTEXT (method
) = type
;
1073 if (fields
&& *fields
)
1074 *fields
= build_overload (method
, *fields
);
1081 if (!CLASSTYPE_METHOD_VEC (type
))
1082 /* Make a new method vector. We start with 8 entries. We must
1083 allocate at least two (for constructors and destructors), and
1084 we're going to end up with an assignment operator at some
1087 We could use a TREE_LIST for now, and convert it to a
1088 TREE_VEC in finish_struct, but we would probably waste more
1089 memory making the links in the list than we would by
1090 over-allocating the size of the vector here. Furthermore,
1091 we would complicate all the code that expects this to be a
1093 CLASSTYPE_METHOD_VEC (type
) = make_tree_vec (8);
1095 method_vec
= CLASSTYPE_METHOD_VEC (type
);
1096 len
= TREE_VEC_LENGTH (method_vec
);
1098 if (DECL_NAME (method
) == constructor_name (type
))
1099 /* A new constructor or destructor. Constructors go in
1100 slot 0; destructors go in slot 1. */
1101 slot
= DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method
)) ? 1 : 0;
1104 /* See if we already have an entry with this name. */
1105 for (slot
= 2; slot
< len
; ++slot
)
1106 if (!TREE_VEC_ELT (method_vec
, slot
)
1107 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec
,
1109 == DECL_NAME (method
)))
1114 /* We need a bigger method vector. */
1115 tree new_vec
= make_tree_vec (2 * len
);
1116 bcopy ((PTR
) &TREE_VEC_ELT (method_vec
, 0),
1117 (PTR
) &TREE_VEC_ELT (new_vec
, 0),
1118 len
* sizeof (tree
));
1120 method_vec
= CLASSTYPE_METHOD_VEC (type
) = new_vec
;
1123 if (DECL_CONV_FN_P (method
) && !TREE_VEC_ELT (method_vec
, slot
))
1125 /* Type conversion operators have to come before
1126 ordinary methods; add_conversions depends on this to
1127 speed up looking for conversion operators. So, if
1128 necessary, we slide some of the vector elements up.
1129 In theory, this makes this algorithm O(N^2) but we
1130 don't expect many conversion operators. */
1131 for (slot
= 2; slot
< len
; ++slot
)
1133 tree fn
= TREE_VEC_ELT (method_vec
, slot
);
1136 /* There are no more entries in the vector, so we
1137 can insert the new conversion operator here. */
1140 if (!DECL_CONV_FN_P (OVL_CURRENT (fn
)))
1141 /* We can insert the new function right at the
1146 if (!TREE_VEC_ELT (method_vec
, slot
))
1147 /* There is nothing in the Ith slot, so we can avoid
1152 /* We know the last slot in the vector is empty
1153 because we know that at this point there's room
1154 for a new function. */
1155 bcopy ((PTR
) &TREE_VEC_ELT (method_vec
, slot
),
1156 (PTR
) &TREE_VEC_ELT (method_vec
, slot
+ 1),
1157 (len
- slot
- 1) * sizeof (tree
));
1158 TREE_VEC_ELT (method_vec
, slot
) = NULL_TREE
;
1163 if (template_class_depth (type
))
1164 /* TYPE is a template class. Don't issue any errors now; wait
1165 until instantiation time to complain. */
1171 /* Check to see if we've already got this method. */
1172 for (fns
= TREE_VEC_ELT (method_vec
, slot
);
1174 fns
= OVL_NEXT (fns
))
1176 tree fn
= OVL_CURRENT (fns
);
1178 if (TREE_CODE (fn
) != TREE_CODE (method
))
1181 if (TREE_CODE (method
) != TEMPLATE_DECL
)
1183 /* [over.load] Member function declarations with the
1184 same name and the same parameter types cannot be
1185 overloaded if any of them is a static member
1186 function declaration. */
1187 if (DECL_STATIC_FUNCTION_P (fn
)
1188 != DECL_STATIC_FUNCTION_P (method
))
1190 tree parms1
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1191 tree parms2
= TYPE_ARG_TYPES (TREE_TYPE (method
));
1193 if (! DECL_STATIC_FUNCTION_P (fn
))
1194 parms1
= TREE_CHAIN (parms1
);
1196 parms2
= TREE_CHAIN (parms2
);
1198 if (compparms (parms1
, parms2
))
1199 cp_error ("`%#D' and `%#D' cannot be overloaded",
1203 /* Since this is an ordinary function in a
1204 non-template class, it's mangled name can be used
1205 as a unique identifier. This technique is only
1206 an optimization; we would get the same results if
1207 we just used decls_match here. */
1208 if (DECL_ASSEMBLER_NAME (fn
)
1209 != DECL_ASSEMBLER_NAME (method
))
1212 else if (!decls_match (fn
, method
))
1215 /* There has already been a declaration of this method
1216 or member template. */
1217 cp_error_at ("`%D' has already been declared in `%T'",
1220 /* We don't call duplicate_decls here to merge the
1221 declarations because that will confuse things if the
1222 methods have inline definitions. In particular, we
1223 will crash while processing the definitions. */
1228 /* Actually insert the new method. */
1229 TREE_VEC_ELT (method_vec
, slot
)
1230 = build_overload (method
, TREE_VEC_ELT (method_vec
, slot
));
1232 /* Add the new binding. */
1233 if (!DECL_CONSTRUCTOR_P (method
)
1234 && !DECL_DESTRUCTOR_P (method
))
1235 push_class_level_binding (DECL_NAME (method
),
1236 TREE_VEC_ELT (method_vec
, slot
));
1241 /* Subroutines of finish_struct. */
1243 /* Look through the list of fields for this struct, deleting
1244 duplicates as we go. This must be recursive to handle
1247 FIELD is the field which may not appear anywhere in FIELDS.
1248 FIELD_PTR, if non-null, is the starting point at which
1249 chained deletions may take place.
1250 The value returned is the first acceptable entry found
1253 Note that anonymous fields which are not of UNION_TYPE are
1254 not duplicates, they are just anonymous fields. This happens
1255 when we have unnamed bitfields, for example. */
1258 delete_duplicate_fields_1 (field
, fields
)
1263 if (DECL_NAME (field
) == 0)
1265 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1268 for (x
= TYPE_FIELDS (TREE_TYPE (field
)); x
; x
= TREE_CHAIN (x
))
1269 fields
= delete_duplicate_fields_1 (x
, fields
);
1274 for (x
= fields
; x
; prev
= x
, x
= TREE_CHAIN (x
))
1276 if (DECL_NAME (x
) == 0)
1278 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
1280 TYPE_FIELDS (TREE_TYPE (x
))
1281 = delete_duplicate_fields_1 (field
, TYPE_FIELDS (TREE_TYPE (x
)));
1282 if (TYPE_FIELDS (TREE_TYPE (x
)) == 0)
1285 fields
= TREE_CHAIN (fields
);
1287 TREE_CHAIN (prev
) = TREE_CHAIN (x
);
1292 if (DECL_NAME (field
) == DECL_NAME (x
))
1294 if (TREE_CODE (field
) == CONST_DECL
1295 && TREE_CODE (x
) == CONST_DECL
)
1296 cp_error_at ("duplicate enum value `%D'", x
);
1297 else if (TREE_CODE (field
) == CONST_DECL
1298 || TREE_CODE (x
) == CONST_DECL
)
1299 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1301 else if (DECL_DECLARES_TYPE_P (field
)
1302 && DECL_DECLARES_TYPE_P (x
))
1304 if (same_type_p (TREE_TYPE (field
), TREE_TYPE (x
)))
1306 cp_error_at ("duplicate nested type `%D'", x
);
1308 else if (DECL_DECLARES_TYPE_P (field
)
1309 || DECL_DECLARES_TYPE_P (x
))
1311 /* Hide tag decls. */
1312 if ((TREE_CODE (field
) == TYPE_DECL
1313 && DECL_ARTIFICIAL (field
))
1314 || (TREE_CODE (x
) == TYPE_DECL
1315 && DECL_ARTIFICIAL (x
)))
1317 cp_error_at ("duplicate field `%D' (as type and non-type)",
1321 cp_error_at ("duplicate member `%D'", x
);
1323 fields
= TREE_CHAIN (fields
);
1325 TREE_CHAIN (prev
) = TREE_CHAIN (x
);
1334 delete_duplicate_fields (fields
)
1338 for (x
= fields
; x
&& TREE_CHAIN (x
); x
= TREE_CHAIN (x
))
1339 TREE_CHAIN (x
) = delete_duplicate_fields_1 (x
, TREE_CHAIN (x
));
1342 /* Change the access of FDECL to ACCESS in T. The access to FDECL is
1343 along the path given by BINFO. Return 1 if change was legit,
1344 otherwise return 0. */
1347 alter_access (t
, binfo
, fdecl
, access
)
1353 tree elem
= purpose_member (t
, DECL_ACCESS (fdecl
));
1356 if (TREE_VALUE (elem
) != access
)
1358 if (TREE_CODE (TREE_TYPE (fdecl
)) == FUNCTION_DECL
)
1359 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl
));
1361 error ("conflicting access specifications for field `%s', ignored",
1362 IDENTIFIER_POINTER (DECL_NAME (fdecl
)));
1366 /* They're changing the access to the same thing they changed
1367 it to before. That's OK. */
1373 enforce_access (binfo
, fdecl
);
1374 DECL_ACCESS (fdecl
) = tree_cons (t
, access
, DECL_ACCESS (fdecl
));
1380 /* Process the USING_DECL, which is a member of T. The METHOD_VEC, if
1381 non-NULL, is the methods of T. The FIELDS are the fields of T. */
1384 handle_using_decl (using_decl
, t
, method_vec
, fields
)
1390 tree ctype
= DECL_INITIAL (using_decl
);
1391 tree name
= DECL_NAME (using_decl
);
1393 = TREE_PRIVATE (using_decl
) ? access_private_node
1394 : TREE_PROTECTED (using_decl
) ? access_protected_node
1395 : access_public_node
;
1397 tree flist
= NULL_TREE
;
1402 binfo
= binfo_or_else (ctype
, t
);
1406 if (name
== constructor_name (ctype
)
1407 || name
== constructor_name_full (ctype
))
1409 cp_error_at ("using-declaration for constructor", using_decl
);
1413 fdecl
= lookup_member (binfo
, name
, 0, 0);
1417 cp_error_at ("no members matching `%D' in `%#T'", using_decl
, ctype
);
1421 /* Functions are represented as TREE_LIST, with the purpose
1422 being the type and the value the functions. Other members
1423 come as themselves. */
1424 if (TREE_CODE (fdecl
) == TREE_LIST
)
1425 /* Ignore base type this came from. */
1426 fdecl
= TREE_VALUE (fdecl
);
1428 if (TREE_CODE (fdecl
) == OVERLOAD
)
1430 /* We later iterate over all functions. */
1432 fdecl
= OVL_FUNCTION (flist
);
1435 name
= DECL_NAME (fdecl
);
1436 n_methods
= method_vec
? TREE_VEC_LENGTH (method_vec
) : 0;
1437 for (i
= 2; i
< n_methods
&& TREE_VEC_ELT (method_vec
, i
); i
++)
1438 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec
, i
)))
1441 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl
, t
);
1442 cp_error_at (" because of local method `%#D' with same name",
1443 OVL_CURRENT (TREE_VEC_ELT (method_vec
, i
)));
1447 if (! DECL_LANG_SPECIFIC (fdecl
))
1448 /* We don't currently handle DECL_ACCESS for TYPE_DECLs; just return. */
1451 for (tmp
= fields
; tmp
; tmp
= TREE_CHAIN (tmp
))
1452 if (DECL_NAME (tmp
) == name
)
1454 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl
, t
);
1455 cp_error_at (" because of local field `%#D' with same name", tmp
);
1459 /* Make type T see field decl FDECL with access ACCESS.*/
1464 if (alter_access (t
, binfo
, OVL_FUNCTION (flist
),
1467 flist
= OVL_CHAIN (flist
);
1471 alter_access (t
, binfo
, fdecl
, access
);
1477 int max_has_virtual
;
1481 char cant_have_default_ctor
;
1482 char cant_have_const_ctor
;
1483 char no_const_asn_ref
;
1486 /* Record information about type T derived from its base classes.
1487 Store most of that information in T itself, and place the
1488 remaining information in the struct BASE_INFO.
1490 Propagate basetype offsets throughout the lattice. Note that the
1491 lattice topped by T is really a pair: it's a DAG that gives the
1492 structure of the derivation hierarchy, and it's a list of the
1493 virtual baseclasses that appear anywhere in the DAG. When a vbase
1494 type appears in the DAG, it's offset is 0, and it's children start
1495 their offsets from that point. When a vbase type appears in the list,
1496 its offset is the offset it has in the hierarchy, and its children's
1497 offsets include that offset in theirs.
1499 Returns the index of the first base class to have virtual functions,
1500 or -1 if no such base class. */
1503 finish_base_struct (t
, b
)
1505 struct base_info
*b
;
1507 tree binfos
= TYPE_BINFO_BASETYPES (t
);
1508 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
1509 int first_vfn_base_index
= -1;
1510 bzero ((char *) b
, sizeof (struct base_info
));
1512 for (i
= 0; i
< n_baseclasses
; i
++)
1514 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1515 tree basetype
= BINFO_TYPE (base_binfo
);
1517 /* Effective C++ rule 14. We only need to check TYPE_VIRTUAL_P
1518 here because the case of virtual functions but non-virtual
1519 dtor is handled in finish_struct_1. */
1520 if (warn_ecpp
&& ! TYPE_VIRTUAL_P (basetype
)
1521 && TYPE_HAS_DESTRUCTOR (basetype
))
1522 cp_warning ("base class `%#T' has a non-virtual destructor", basetype
);
1524 /* If the type of basetype is incomplete, then
1525 we already complained about that fact
1526 (and we should have fixed it up as well). */
1527 if (TYPE_SIZE (basetype
) == 0)
1530 /* The base type is of incomplete type. It is
1531 probably best to pretend that it does not
1533 if (i
== n_baseclasses
-1)
1534 TREE_VEC_ELT (binfos
, i
) = NULL_TREE
;
1535 TREE_VEC_LENGTH (binfos
) -= 1;
1537 for (j
= i
; j
+1 < n_baseclasses
; j
++)
1538 TREE_VEC_ELT (binfos
, j
) = TREE_VEC_ELT (binfos
, j
+1);
1541 if (! TYPE_HAS_CONST_INIT_REF (basetype
))
1542 b
->cant_have_const_ctor
= 1;
1544 if (TYPE_HAS_CONSTRUCTOR (basetype
)
1545 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
))
1547 b
->cant_have_default_ctor
= 1;
1548 if (! TYPE_HAS_CONSTRUCTOR (t
))
1550 cp_pedwarn ("base `%T' with only non-default constructor",
1552 cp_pedwarn ("in class without a constructor");
1556 if (TYPE_HAS_ASSIGN_REF (basetype
)
1557 && !TYPE_HAS_CONST_ASSIGN_REF (basetype
))
1558 b
->no_const_asn_ref
= 1;
1560 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (basetype
);
1561 TYPE_NEEDS_DESTRUCTOR (t
) |= TYPE_NEEDS_DESTRUCTOR (basetype
);
1562 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype
);
1563 TYPE_HAS_COMPLEX_INIT_REF (t
) |= TYPE_HAS_COMPLEX_INIT_REF (basetype
);
1565 TYPE_OVERLOADS_CALL_EXPR (t
) |= TYPE_OVERLOADS_CALL_EXPR (basetype
);
1566 TYPE_OVERLOADS_ARRAY_REF (t
) |= TYPE_OVERLOADS_ARRAY_REF (basetype
);
1567 TYPE_OVERLOADS_ARROW (t
) |= TYPE_OVERLOADS_ARROW (basetype
);
1569 if (CLASSTYPE_COM_INTERFACE (basetype
))
1571 CLASSTYPE_COM_INTERFACE (t
) = 1;
1574 ("COM interface type `%T' must be the leftmost base class",
1577 else if (CLASSTYPE_COM_INTERFACE (t
) && i
== 0)
1579 cp_error ("COM interface type `%T' with non-COM base class `%T'",
1581 CLASSTYPE_COM_INTERFACE (t
) = 0;
1584 if (TYPE_VIRTUAL_P (basetype
))
1586 /* Ensure that this is set from at least a virtual base
1588 if (b
->rtti
== NULL_TREE
)
1589 b
->rtti
= CLASSTYPE_RTTI (basetype
);
1591 /* Don't borrow virtuals from virtual baseclasses. */
1592 if (TREE_VIA_VIRTUAL (base_binfo
))
1595 if (first_vfn_base_index
< 0)
1598 first_vfn_base_index
= i
;
1600 /* Update these two, now that we know what vtable we are
1601 going to extend. This is so that we can add virtual
1602 functions, and override them properly. */
1603 TYPE_BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (basetype
);
1604 TYPE_BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (basetype
);
1605 b
->has_virtual
= CLASSTYPE_VSIZE (basetype
);
1606 b
->vfield
= CLASSTYPE_VFIELD (basetype
);
1607 b
->vfields
= copy_list (CLASSTYPE_VFIELDS (basetype
));
1608 vfields
= b
->vfields
;
1611 if (VF_BINFO_VALUE (vfields
) == NULL_TREE
1612 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields
)))
1614 tree value
= VF_BASETYPE_VALUE (vfields
);
1615 if (DECL_NAME (CLASSTYPE_VFIELD (value
))
1616 == DECL_NAME (CLASSTYPE_VFIELD (basetype
)))
1617 VF_NORMAL_VALUE (b
->vfields
) = basetype
;
1619 VF_NORMAL_VALUE (b
->vfields
) = VF_NORMAL_VALUE (vfields
);
1621 vfields
= TREE_CHAIN (vfields
);
1623 CLASSTYPE_VFIELD (t
) = b
->vfield
;
1627 /* Only add unique vfields, and flatten them out as we go. */
1628 tree vfields
= CLASSTYPE_VFIELDS (basetype
);
1631 if (VF_BINFO_VALUE (vfields
) == NULL_TREE
1632 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields
)))
1634 tree value
= VF_BASETYPE_VALUE (vfields
);
1635 b
->vfields
= tree_cons (base_binfo
, value
, b
->vfields
);
1636 if (DECL_NAME (CLASSTYPE_VFIELD (value
))
1637 == DECL_NAME (CLASSTYPE_VFIELD (basetype
)))
1638 VF_NORMAL_VALUE (b
->vfields
) = basetype
;
1640 VF_NORMAL_VALUE (b
->vfields
) = VF_NORMAL_VALUE (vfields
);
1642 vfields
= TREE_CHAIN (vfields
);
1645 if (b
->has_virtual
== 0)
1647 first_vfn_base_index
= i
;
1649 /* Update these two, now that we know what vtable we are
1650 going to extend. This is so that we can add virtual
1651 functions, and override them properly. */
1652 TYPE_BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (basetype
);
1653 TYPE_BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (basetype
);
1654 b
->has_virtual
= CLASSTYPE_VSIZE (basetype
);
1655 b
->vfield
= CLASSTYPE_VFIELD (basetype
);
1656 CLASSTYPE_VFIELD (t
) = b
->vfield
;
1657 /* When we install the first one, set the VF_NORMAL_VALUE
1658 to be the current class, as this it is the most derived
1659 class. Hopefully, this is not set to something else
1661 vfields
= b
->vfields
;
1664 if (DECL_NAME (CLASSTYPE_VFIELD (t
))
1665 == DECL_NAME (CLASSTYPE_VFIELD (basetype
)))
1667 VF_NORMAL_VALUE (vfields
) = t
;
1668 /* There should only be one of them! And it should
1669 always be found, if we get into here. (mrs) */
1672 vfields
= TREE_CHAIN (vfields
);
1681 /* Find the base class with the largest number of virtual functions. */
1682 for (vfields
= b
->vfields
; vfields
; vfields
= TREE_CHAIN (vfields
))
1684 if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields
)) > b
->max_has_virtual
)
1685 b
->max_has_virtual
= CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields
));
1686 if (VF_DERIVED_VALUE (vfields
)
1687 && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields
)) > b
->max_has_virtual
)
1688 b
->max_has_virtual
= CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields
));
1693 /* If all virtual functions come only from virtual baseclasses. */
1696 /* Update the rtti base if we have a non-virtual base class version
1698 b
->rtti
= CLASSTYPE_RTTI (BINFO_TYPE (TREE_VEC_ELT (binfos
, first_vfn_base_index
)));
1700 return first_vfn_base_index
;
1703 /* Set memoizing fields and bits of T (and its variants) for later use.
1704 MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables. */
1707 finish_struct_bits (t
, max_has_virtual
)
1709 int max_has_virtual
;
1711 int i
, n_baseclasses
= CLASSTYPE_N_BASECLASSES (t
);
1713 /* Fix up variants (if any). */
1714 tree variants
= TYPE_NEXT_VARIANT (t
);
1717 /* These fields are in the _TYPE part of the node, not in
1718 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1719 TYPE_HAS_CONSTRUCTOR (variants
) = TYPE_HAS_CONSTRUCTOR (t
);
1720 TYPE_HAS_DESTRUCTOR (variants
) = TYPE_HAS_DESTRUCTOR (t
);
1721 TYPE_NEEDS_CONSTRUCTING (variants
) = TYPE_NEEDS_CONSTRUCTING (t
);
1722 TYPE_NEEDS_DESTRUCTOR (variants
) = TYPE_NEEDS_DESTRUCTOR (t
);
1724 TYPE_USES_COMPLEX_INHERITANCE (variants
) = TYPE_USES_COMPLEX_INHERITANCE (t
);
1725 TYPE_VIRTUAL_P (variants
) = TYPE_VIRTUAL_P (t
);
1726 TYPE_USES_VIRTUAL_BASECLASSES (variants
) = TYPE_USES_VIRTUAL_BASECLASSES (t
);
1727 /* Copy whatever these are holding today. */
1728 TYPE_MIN_VALUE (variants
) = TYPE_MIN_VALUE (t
);
1729 TYPE_MAX_VALUE (variants
) = TYPE_MAX_VALUE (t
);
1730 TYPE_FIELDS (variants
) = TYPE_FIELDS (t
);
1731 TYPE_SIZE (variants
) = TYPE_SIZE (t
);
1732 TYPE_SIZE_UNIT (variants
) = TYPE_SIZE_UNIT (t
);
1733 variants
= TYPE_NEXT_VARIANT (variants
);
1736 if (n_baseclasses
&& max_has_virtual
)
1738 /* For a class w/o baseclasses, `finish_struct' has set
1739 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by definition). Similarly
1740 for a class who's base classes do not have vtables. When neither
1741 of these is true, we might have removed abstract virtuals (by
1742 providing a definition), added some (by declaring new ones), or
1743 redeclared ones from a base class. We need to recalculate what's
1744 really an abstract virtual at this point (by looking in the
1746 CLASSTYPE_ABSTRACT_VIRTUALS (t
) = get_abstract_virtuals (t
);
1751 /* Notice whether this class has type conversion functions defined. */
1752 tree binfo
= TYPE_BINFO (t
);
1753 tree binfos
= BINFO_BASETYPES (binfo
);
1756 for (i
= n_baseclasses
-1; i
>= 0; i
--)
1758 basetype
= BINFO_TYPE (TREE_VEC_ELT (binfos
, i
));
1760 TYPE_HAS_CONVERSION (t
) |= TYPE_HAS_CONVERSION (basetype
);
1764 /* If this type has a copy constructor, force its mode to be BLKmode, and
1765 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1766 be passed by invisible reference and prevent it from being returned in
1769 Also do this if the class has BLKmode but can still be returned in
1770 registers, since function_cannot_inline_p won't let us inline
1771 functions returning such a type. This affects the HP-PA. */
1772 if (! TYPE_HAS_TRIVIAL_INIT_REF (t
)
1773 || (TYPE_MODE (t
) == BLKmode
&& ! aggregate_value_p (t
)
1774 && CLASSTYPE_NON_AGGREGATE (t
)))
1777 DECL_MODE (TYPE_MAIN_DECL (t
)) = BLKmode
;
1778 for (variants
= t
; variants
; variants
= TYPE_NEXT_VARIANT (variants
))
1780 TYPE_MODE (variants
) = BLKmode
;
1781 TREE_ADDRESSABLE (variants
) = 1;
1786 /* Issue warnings about T having private constructors, but no friends,
1789 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1790 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1791 non-private static member functions. */
1794 maybe_warn_about_overly_private_class (t
)
1797 int has_member_fn
= 0;
1798 int has_nonprivate_method
= 0;
1801 if (!warn_ctor_dtor_privacy
1802 /* If the class has friends, those entities might create and
1803 access instances, so we should not warn. */
1804 || (CLASSTYPE_FRIEND_CLASSES (t
)
1805 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
1806 /* We will have warned when the template was declared; there's
1807 no need to warn on every instantiation. */
1808 || CLASSTYPE_TEMPLATE_INSTANTIATION (t
))
1809 /* There's no reason to even consider warning about this
1813 /* We only issue one warning, if more than one applies, because
1814 otherwise, on code like:
1817 // Oops - forgot `public:'
1823 we warn several times about essentially the same problem. */
1825 /* Check to see if all (non-constructor, non-destructor) member
1826 functions are private. (Since there are no friends or
1827 non-private statics, we can't ever call any of the private member
1829 for (fn
= TYPE_METHODS (t
); fn
; fn
= TREE_CHAIN (fn
))
1830 /* We're not interested in compiler-generated methods; they don't
1831 provide any way to call private members. */
1832 if (!DECL_ARTIFICIAL (fn
))
1834 if (!TREE_PRIVATE (fn
))
1836 if (DECL_STATIC_FUNCTION_P (fn
))
1837 /* A non-private static member function is just like a
1838 friend; it can create and invoke private member
1839 functions, and be accessed without a class
1843 has_nonprivate_method
= 1;
1846 else if (!DECL_CONSTRUCTOR_P (fn
) && !DECL_DESTRUCTOR_P (fn
))
1850 if (!has_nonprivate_method
&& has_member_fn
)
1852 /* There are no non-private methods, and there's at least one
1853 private member function that isn't a constructor or
1854 destructor. (If all the private members are
1855 constructors/destructors we want to use the code below that
1856 issues error messages specifically referring to
1857 constructors/destructors.) */
1859 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
1860 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (t
); i
++)
1861 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos
, i
))
1862 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos
, i
)))
1864 has_nonprivate_method
= 1;
1867 if (!has_nonprivate_method
)
1869 cp_warning ("all member functions in class `%T' are private", t
);
1874 /* Even if some of the member functions are non-private, the class
1875 won't be useful for much if all the constructors or destructors
1876 are private: such an object can never be created or destroyed. */
1877 if (TYPE_HAS_DESTRUCTOR (t
))
1879 tree dtor
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 1);
1881 if (TREE_PRIVATE (dtor
))
1883 cp_warning ("`%#T' only defines a private destructor and has no friends",
1889 if (TYPE_HAS_CONSTRUCTOR (t
))
1891 int nonprivate_ctor
= 0;
1893 /* If a non-template class does not define a copy
1894 constructor, one is defined for it, enabling it to avoid
1895 this warning. For a template class, this does not
1896 happen, and so we would normally get a warning on:
1898 template <class T> class C { private: C(); };
1900 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1901 complete non-template or fully instantiated classes have this
1903 if (!TYPE_HAS_INIT_REF (t
))
1904 nonprivate_ctor
= 1;
1906 for (fn
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 0);
1910 tree ctor
= OVL_CURRENT (fn
);
1911 /* Ideally, we wouldn't count copy constructors (or, in
1912 fact, any constructor that takes an argument of the
1913 class type as a parameter) because such things cannot
1914 be used to construct an instance of the class unless
1915 you already have one. But, for now at least, we're
1917 if (! TREE_PRIVATE (ctor
))
1919 nonprivate_ctor
= 1;
1924 if (nonprivate_ctor
== 0)
1926 cp_warning ("`%#T' only defines private constructors and has no friends",
1933 /* Function to help qsort sort FIELD_DECLs by name order. */
1936 field_decl_cmp (x
, y
)
1939 if (DECL_NAME (*x
) == DECL_NAME (*y
))
1941 if (DECL_NAME (*x
) == NULL_TREE
)
1943 if (DECL_NAME (*y
) == NULL_TREE
)
1945 if (DECL_NAME (*x
) < DECL_NAME (*y
))
1950 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1953 method_name_cmp (m1
, m2
)
1954 const tree
*m1
, *m2
;
1956 if (*m1
== NULL_TREE
&& *m2
== NULL_TREE
)
1958 if (*m1
== NULL_TREE
)
1960 if (*m2
== NULL_TREE
)
1962 if (DECL_NAME (OVL_CURRENT (*m1
)) < DECL_NAME (OVL_CURRENT (*m2
)))
1967 /* Warn about duplicate methods in fn_fields. Also compact method
1968 lists so that lookup can be made faster.
1970 Data Structure: List of method lists. The outer list is a
1971 TREE_LIST, whose TREE_PURPOSE field is the field name and the
1972 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
1973 links the entire list of methods for TYPE_METHODS. Friends are
1974 chained in the same way as member functions (? TREE_CHAIN or
1975 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
1976 list. That allows them to be quickly deleted, and requires no
1979 If there are any constructors/destructors, they are moved to the
1980 front of the list. This makes pushclass more efficient.
1982 @@ The above comment is obsolete. It mostly describes what add_method
1983 @@ and add_implicitly_declared_members do.
1985 Sort methods that are not special (i.e., constructors, destructors, and
1986 type conversion operators) so that we can find them faster in search. */
1989 finish_struct_methods (t
)
1993 tree method_vec
= CLASSTYPE_METHOD_VEC (t
);
1994 tree ctor_name
= constructor_name (t
);
1995 int slot
, len
= method_vec
? TREE_VEC_LENGTH (method_vec
) : 0;
1997 /* First fill in entry 0 with the constructors, entry 1 with destructors,
1998 and the next few with type conversion operators (if any). */
1999 for (fn_fields
= TYPE_METHODS (t
); fn_fields
;
2000 fn_fields
= TREE_CHAIN (fn_fields
))
2002 tree fn_name
= DECL_NAME (fn_fields
);
2004 /* Clear out this flag.
2006 @@ Doug may figure out how to break
2007 @@ this with nested classes and friends. */
2008 DECL_IN_AGGR_P (fn_fields
) = 0;
2010 /* Note here that a copy ctor is private, so we don't dare generate
2011 a default copy constructor for a class that has a member
2012 of this type without making sure they have access to it. */
2013 if (fn_name
== ctor_name
)
2015 tree parmtypes
= FUNCTION_ARG_CHAIN (fn_fields
);
2016 tree parmtype
= parmtypes
? TREE_VALUE (parmtypes
) : void_type_node
;
2018 if (TREE_CODE (parmtype
) == REFERENCE_TYPE
2019 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype
)) == t
)
2021 if (TREE_CHAIN (parmtypes
) == NULL_TREE
2022 || TREE_CHAIN (parmtypes
) == void_list_node
2023 || TREE_PURPOSE (TREE_CHAIN (parmtypes
)))
2025 if (TREE_PROTECTED (fn_fields
))
2026 TYPE_HAS_NONPUBLIC_CTOR (t
) = 1;
2027 else if (TREE_PRIVATE (fn_fields
))
2028 TYPE_HAS_NONPUBLIC_CTOR (t
) = 2;
2032 else if (fn_name
== ansi_opname
[(int) MODIFY_EXPR
])
2034 tree parmtype
= TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields
));
2036 if (copy_assignment_arg_p (parmtype
, DECL_VIRTUAL_P (fn_fields
)))
2038 if (TREE_PROTECTED (fn_fields
))
2039 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t
) = 1;
2040 else if (TREE_PRIVATE (fn_fields
))
2041 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t
) = 2;
2046 if (TYPE_HAS_DESTRUCTOR (t
) && !TREE_VEC_ELT (method_vec
, 1))
2047 /* We thought there was a destructor, but there wasn't. Some
2048 parse errors cause this anomalous situation. */
2049 TYPE_HAS_DESTRUCTOR (t
) = 0;
2051 /* Issue warnings about private constructors and such. If there are
2052 no methods, then some public defaults are generated. */
2053 maybe_warn_about_overly_private_class (t
);
2055 if (method_vec
== NULL_TREE
)
2058 /* Now sort the methods. */
2059 while (len
> 2 && TREE_VEC_ELT (method_vec
, len
-1) == NULL_TREE
)
2061 TREE_VEC_LENGTH (method_vec
) = len
;
2063 /* The type conversion ops have to live at the front of the vec, so we
2065 for (slot
= 2; slot
< len
; ++slot
)
2067 tree fn
= TREE_VEC_ELT (method_vec
, slot
);
2069 if (!DECL_CONV_FN_P (OVL_CURRENT (fn
)))
2073 qsort (&TREE_VEC_ELT (method_vec
, slot
), len
-slot
, sizeof (tree
),
2074 (int (*)(const void *, const void *))method_name_cmp
);
2077 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2080 duplicate_tag_error (t
)
2083 cp_error ("redefinition of `%#T'", t
);
2084 cp_error_at ("previous definition here", t
);
2086 /* Pretend we haven't defined this type. */
2088 /* All of the component_decl's were TREE_CHAINed together in the parser.
2089 finish_struct_methods walks these chains and assembles all methods with
2090 the same base name into DECL_CHAINs. Now we don't need the parser chains
2091 anymore, so we unravel them. */
2093 /* This used to be in finish_struct, but it turns out that the
2094 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2096 if (CLASSTYPE_METHOD_VEC (t
))
2098 tree method_vec
= CLASSTYPE_METHOD_VEC (t
);
2099 int i
, len
= TREE_VEC_LENGTH (method_vec
);
2100 for (i
= 0; i
< len
; i
++)
2102 tree unchain
= TREE_VEC_ELT (method_vec
, i
);
2103 while (unchain
!= NULL_TREE
)
2105 TREE_CHAIN (OVL_CURRENT (unchain
)) = NULL_TREE
;
2106 unchain
= OVL_NEXT (unchain
);
2111 if (TYPE_LANG_SPECIFIC (t
))
2113 tree binfo
= TYPE_BINFO (t
);
2114 int interface_only
= CLASSTYPE_INTERFACE_ONLY (t
);
2115 int interface_unknown
= CLASSTYPE_INTERFACE_UNKNOWN (t
);
2116 tree template_info
= CLASSTYPE_TEMPLATE_INFO (t
);
2117 int use_template
= CLASSTYPE_USE_TEMPLATE (t
);
2119 bzero ((char *) TYPE_LANG_SPECIFIC (t
), sizeof (struct lang_type
));
2120 BINFO_BASETYPES(binfo
) = NULL_TREE
;
2122 TYPE_BINFO (t
) = binfo
;
2123 CLASSTYPE_INTERFACE_ONLY (t
) = interface_only
;
2124 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t
, interface_unknown
);
2125 TYPE_REDEFINED (t
) = 1;
2126 CLASSTYPE_TEMPLATE_INFO (t
) = template_info
;
2127 CLASSTYPE_USE_TEMPLATE (t
) = use_template
;
2129 TYPE_SIZE (t
) = NULL_TREE
;
2130 TYPE_MODE (t
) = VOIDmode
;
2131 TYPE_FIELDS (t
) = NULL_TREE
;
2132 TYPE_METHODS (t
) = NULL_TREE
;
2133 TYPE_VFIELD (t
) = NULL_TREE
;
2134 TYPE_CONTEXT (t
) = NULL_TREE
;
2135 TYPE_NONCOPIED_PARTS (t
) = NULL_TREE
;
2138 /* Construct the initializer for BINFOs virtual function table. */
2141 build_vtbl_initializer (binfo
)
2144 tree v
= BINFO_VIRTUALS (binfo
);
2145 tree inits
= NULL_TREE
;
2147 /* Process the RTTI stuff at the head of the list. If we're not
2148 using vtable thunks, then the RTTI entry is just an ordinary
2149 function, and we can process it just like the other virtual
2150 function entries. */
2151 if (!CLASSTYPE_COM_INTERFACE (BINFO_TYPE (binfo
))
2152 && flag_vtable_thunks
)
2157 /* The first entry is an offset. */
2158 offset
= TREE_PURPOSE (v
);
2159 my_friendly_assert (TREE_CODE (offset
) == INTEGER_CST
,
2162 /* Convert the offset to look like a function pointer, so that
2163 we can put it in the vtable. */
2164 init
= build1 (NOP_EXPR
, vfunc_ptr_type_node
, offset
);
2165 TREE_CONSTANT (init
) = 1;
2166 init
= build_vtable_entry (integer_zero_node
, init
);
2167 inits
= tree_cons (NULL_TREE
, init
, inits
);
2169 /* Even in this case, the second entry (the tdesc pointer) is
2170 just an ordinary function. */
2174 /* Go through all the ordinary virtual functions, building up
2182 /* Pull the offset for `this', and the function to call, out of
2184 delta
= TREE_PURPOSE (v
);
2185 fn
= TREE_VALUE (v
);
2186 my_friendly_assert (TREE_CODE (delta
) == INTEGER_CST
, 19990727);
2187 my_friendly_assert (TREE_CODE (fn
) == FUNCTION_DECL
, 19990727);
2189 /* You can't call an abstract virtual function; it's abstract.
2190 So, we replace these functions with __pure_virtual. */
2191 if (DECL_ABSTRACT_VIRTUAL_P (fn
))
2194 /* Package up that information for the vtable. */
2195 init
= build_vtable_entry_for_fn (delta
, fn
);
2196 /* And add it to the chain of initializers. */
2197 inits
= tree_cons (NULL_TREE
, init
, inits
);
2203 /* The initializers were built up in reverse order; straighten them
2205 inits
= nreverse (inits
);
2206 /* Package all the initializers up as an array initializer. */
2207 return build_nt (CONSTRUCTOR
, NULL_TREE
, inits
);
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;
2231 DECL_INITIAL (decl
) = build_vtbl_initializer (binfo
);
2232 cp_finish_decl (decl
, DECL_INITIAL (decl
), NULL_TREE
, 0, 0);
2233 DECL_CONTEXT (decl
) = context
;
2235 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo
);
2238 for (i
= 0; i
< n_baselinks
; i
++)
2240 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2241 int is_not_base_vtable
2242 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
2243 if (TREE_VIA_VIRTUAL (base_binfo
))
2244 base_binfo
= binfo_member (BINFO_TYPE (base_binfo
),
2245 CLASSTYPE_VBASECLASSES (t
));
2246 finish_vtbls (base_binfo
, is_not_base_vtable
, t
);
2250 /* True if we should override the given BASE_FNDECL with the given
2254 overrides (fndecl
, base_fndecl
)
2255 tree fndecl
, base_fndecl
;
2257 /* Destructors have special names. */
2258 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl
))
2259 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl
)))
2261 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl
))
2262 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl
)))
2264 if (DECL_NAME (fndecl
) == DECL_NAME (base_fndecl
))
2266 tree types
, base_types
;
2268 retypes
= TREE_TYPE (TREE_TYPE (fndecl
));
2269 base_retypes
= TREE_TYPE (TREE_TYPE (base_fndecl
));
2271 types
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
2272 base_types
= TYPE_ARG_TYPES (TREE_TYPE (base_fndecl
));
2273 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types
)))
2274 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types
))))
2275 && compparms (TREE_CHAIN (base_types
), TREE_CHAIN (types
)))
2282 get_class_offset_1 (parent
, binfo
, context
, t
, fndecl
)
2283 tree parent
, binfo
, context
, t
, fndecl
;
2285 tree binfos
= BINFO_BASETYPES (binfo
);
2286 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2287 tree rval
= NULL_TREE
;
2289 if (binfo
== parent
)
2290 return error_mark_node
;
2292 for (i
= 0; i
< n_baselinks
; i
++)
2294 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2297 if (TREE_VIA_VIRTUAL (base_binfo
))
2298 base_binfo
= binfo_member (BINFO_TYPE (base_binfo
),
2299 CLASSTYPE_VBASECLASSES (t
));
2300 nrval
= get_class_offset_1 (parent
, base_binfo
, context
, t
, fndecl
);
2301 /* See if we have a new value */
2302 if (nrval
&& (nrval
!= error_mark_node
|| rval
==0))
2304 /* Only compare if we have two offsets */
2305 if (rval
&& rval
!= error_mark_node
2306 && ! tree_int_cst_equal (nrval
, rval
))
2308 /* Only give error if the two offsets are different */
2309 error ("every virtual function must have a unique final overrider");
2310 cp_error (" found two (or more) `%T' class subobjects in `%T'", context
, t
);
2311 cp_error (" with virtual `%D' from virtual base class", fndecl
);
2317 if (rval
&& BINFO_TYPE (binfo
) == context
)
2319 my_friendly_assert (rval
== error_mark_node
2320 || tree_int_cst_equal (rval
, BINFO_OFFSET (binfo
)), 999);
2321 rval
= BINFO_OFFSET (binfo
);
2327 /* Get the offset to the CONTEXT subobject that is related to the
2331 get_class_offset (context
, t
, binfo
, fndecl
)
2332 tree context
, t
, binfo
, fndecl
;
2334 tree first_binfo
= binfo
;
2339 return integer_zero_node
;
2341 if (BINFO_TYPE (binfo
) == context
)
2342 return BINFO_OFFSET (binfo
);
2344 /* Check less derived binfos first. */
2345 while (BINFO_BASETYPES (binfo
)
2346 && (i
=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
))) != -1)
2348 tree binfos
= BINFO_BASETYPES (binfo
);
2349 binfo
= TREE_VEC_ELT (binfos
, i
);
2350 if (BINFO_TYPE (binfo
) == context
)
2351 return BINFO_OFFSET (binfo
);
2354 /* Ok, not found in the less derived binfos, now check the more
2356 offset
= get_class_offset_1 (first_binfo
, TYPE_BINFO (t
), context
, t
, fndecl
);
2357 if (offset
==0 || TREE_CODE (offset
) != INTEGER_CST
)
2358 my_friendly_abort (999); /* we have to find it. */
2362 /* Skip RTTI information at the front of the virtual list. */
2364 unsigned HOST_WIDE_INT
2365 skip_rtti_stuff (virtuals
, t
)
2370 if (CLASSTYPE_COM_INTERFACE (t
))
2376 /* We always reserve a slot for the offset/tdesc entry. */
2378 *virtuals
= TREE_CHAIN (*virtuals
);
2380 if (flag_vtable_thunks
&& *virtuals
)
2382 /* The second slot is reserved for the tdesc pointer when thunks
2385 *virtuals
= TREE_CHAIN (*virtuals
);
2391 modify_one_vtable (binfo
, t
, fndecl
)
2392 tree binfo
, t
, fndecl
;
2394 tree virtuals
= BINFO_VIRTUALS (binfo
);
2395 unsigned HOST_WIDE_INT n
;
2397 /* update rtti entry */
2400 if (binfo
== TYPE_BINFO (t
))
2402 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2403 build_vtable (TYPE_BINFO (DECL_CONTEXT (CLASSTYPE_VFIELD (t
))), t
);
2407 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2408 prepare_fresh_vtable (binfo
, t
);
2411 if (fndecl
== NULL_TREE
)
2414 n
= skip_rtti_stuff (&virtuals
, BINFO_TYPE (binfo
));
2418 tree current_fndecl
= TREE_VALUE (virtuals
);
2420 /* We should never have an instance of __pure_virtual on the
2421 BINFO_VIRTUALS list. If we do, then we will never notice
2422 that the function that should have been there instead has
2424 my_friendly_assert (current_fndecl
!= abort_fndecl
,
2427 if (current_fndecl
&& overrides (fndecl
, current_fndecl
))
2429 tree base_offset
, offset
;
2430 tree context
= DECL_CLASS_CONTEXT (fndecl
);
2431 tree vfield
= CLASSTYPE_VFIELD (t
);
2434 offset
= get_class_offset (context
, t
, binfo
, fndecl
);
2436 /* Find the right offset for the this pointer based on the
2437 base class we just found. We have to take into
2438 consideration the virtual base class pointers that we
2439 stick in before the virtual function table pointer.
2441 Also, we want just the delta between the most base class
2442 that we derived this vfield from and us. */
2443 base_offset
= size_binop (PLUS_EXPR
,
2444 get_derived_offset (binfo
, DECL_CONTEXT (current_fndecl
)),
2445 BINFO_OFFSET (binfo
));
2446 this_offset
= ssize_binop (MINUS_EXPR
, offset
, base_offset
);
2448 if (binfo
== TYPE_BINFO (t
))
2450 /* In this case, it is *type*'s vtable we are modifying.
2451 We start with the approximation that it's vtable is that
2452 of the immediate base class. */
2453 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2454 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield
)), t
);
2458 /* This is our very own copy of `basetype' to play with.
2459 Later, we will fill in all the virtual functions
2460 that override the virtual functions in these base classes
2461 which are not defined by the current type. */
2462 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2463 prepare_fresh_vtable (binfo
, t
);
2467 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo
)));
2469 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo
), n
),
2474 virtuals
= TREE_CHAIN (virtuals
);
2478 /* These are the ones that are not through virtual base classes. */
2481 modify_all_direct_vtables (binfo
, do_self
, t
, fndecl
)
2486 tree binfos
= BINFO_BASETYPES (binfo
);
2487 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2489 /* Should we use something besides CLASSTYPE_VFIELDS? */
2490 if (do_self
&& CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
2491 modify_one_vtable (binfo
, t
, fndecl
);
2493 for (i
= 0; i
< n_baselinks
; i
++)
2495 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2496 int is_not_base_vtable
2497 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
2498 if (! TREE_VIA_VIRTUAL (base_binfo
))
2499 modify_all_direct_vtables (base_binfo
, is_not_base_vtable
, t
, fndecl
);
2503 /* Fixup all the delta entries in this one vtable that need updating. */
2506 fixup_vtable_deltas1 (binfo
, t
)
2509 tree virtuals
= BINFO_VIRTUALS (binfo
);
2510 unsigned HOST_WIDE_INT n
;
2512 n
= skip_rtti_stuff (&virtuals
, BINFO_TYPE (binfo
));
2516 tree fndecl
= TREE_VALUE (virtuals
);
2517 tree delta
= TREE_PURPOSE (virtuals
);
2521 tree base_offset
, offset
;
2522 tree context
= DECL_CLASS_CONTEXT (fndecl
);
2523 tree vfield
= CLASSTYPE_VFIELD (t
);
2526 offset
= get_class_offset (context
, t
, binfo
, fndecl
);
2528 /* Find the right offset for the this pointer based on the
2529 base class we just found. We have to take into
2530 consideration the virtual base class pointers that we
2531 stick in before the virtual function table pointer.
2533 Also, we want just the delta between the most base class
2534 that we derived this vfield from and us. */
2535 base_offset
= size_binop (PLUS_EXPR
,
2536 get_derived_offset (binfo
,
2537 DECL_CONTEXT (fndecl
)),
2538 BINFO_OFFSET (binfo
));
2539 this_offset
= ssize_binop (MINUS_EXPR
, offset
, base_offset
);
2541 if (! tree_int_cst_equal (this_offset
, delta
))
2543 /* Make sure we can modify the derived association with immunity. */
2544 if (binfo
== TYPE_BINFO (t
))
2546 /* In this case, it is *type*'s vtable we are modifying.
2547 We start with the approximation that it's vtable is that
2548 of the immediate base class. */
2549 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2550 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield
)), t
);
2554 /* This is our very own copy of `basetype' to play with.
2555 Later, we will fill in all the virtual functions
2556 that override the virtual functions in these base classes
2557 which are not defined by the current type. */
2558 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2559 prepare_fresh_vtable (binfo
, t
);
2562 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo
), n
),
2568 virtuals
= TREE_CHAIN (virtuals
);
2572 /* Fixup all the delta entries in all the direct vtables that need updating.
2573 This happens when we have non-overridden virtual functions from a
2574 virtual base class, that are at a different offset, in the new
2575 hierarchy, because the layout of the virtual bases has changed. */
2578 fixup_vtable_deltas (binfo
, init_self
, t
)
2583 tree binfos
= BINFO_BASETYPES (binfo
);
2584 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2586 for (i
= 0; i
< n_baselinks
; i
++)
2588 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2589 int is_not_base_vtable
2590 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
2591 if (! TREE_VIA_VIRTUAL (base_binfo
))
2592 fixup_vtable_deltas (base_binfo
, is_not_base_vtable
, t
);
2594 /* Should we use something besides CLASSTYPE_VFIELDS? */
2595 if (init_self
&& CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
2596 fixup_vtable_deltas1 (binfo
, t
);
2599 /* These are the ones that are through virtual base classes. */
2602 modify_all_indirect_vtables (binfo
, do_self
, via_virtual
, t
, fndecl
)
2604 int do_self
, via_virtual
;
2607 tree binfos
= BINFO_BASETYPES (binfo
);
2608 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2610 /* Should we use something besides CLASSTYPE_VFIELDS? */
2611 if (do_self
&& via_virtual
&& CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
2612 modify_one_vtable (binfo
, t
, fndecl
);
2614 for (i
= 0; i
< n_baselinks
; i
++)
2616 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2617 int is_not_base_vtable
2618 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
2619 if (TREE_VIA_VIRTUAL (base_binfo
))
2622 base_binfo
= binfo_member (BINFO_TYPE (base_binfo
), CLASSTYPE_VBASECLASSES (t
));
2624 modify_all_indirect_vtables (base_binfo
, is_not_base_vtable
, via_virtual
, t
, fndecl
);
2629 modify_all_vtables (t
, fndecl
)
2633 /* Do these first, so that we will make use of any non-virtual class's
2634 vtable, over a virtual classes vtable. */
2635 modify_all_direct_vtables (TYPE_BINFO (t
), 1, t
, fndecl
);
2636 if (TYPE_USES_VIRTUAL_BASECLASSES (t
))
2637 modify_all_indirect_vtables (TYPE_BINFO (t
), 1, 0, t
, fndecl
);
2640 /* Here, we already know that they match in every respect.
2641 All we have to check is where they had their declarations. */
2644 strictly_overrides (fndecl1
, fndecl2
)
2645 tree fndecl1
, fndecl2
;
2647 int distance
= get_base_distance (DECL_CLASS_CONTEXT (fndecl2
),
2648 DECL_CLASS_CONTEXT (fndecl1
),
2650 if (distance
== -2 || distance
> 0)
2655 /* Merge overrides for one vtable.
2656 If we want to merge in same function, we are fine.
2658 if one has a DECL_CLASS_CONTEXT that is a parent of the
2659 other, than choose the more derived one
2661 potentially ill-formed (see 10.3 [class.virtual])
2662 we have to check later to see if there was an
2663 override in this class. If there was ok, if not
2664 then it is ill-formed. (mrs)
2666 We take special care to reuse a vtable, if we can. */
2669 override_one_vtable (binfo
, old
, t
)
2672 tree virtuals
= BINFO_VIRTUALS (binfo
);
2673 tree old_virtuals
= BINFO_VIRTUALS (old
);
2674 enum { REUSE_NEW
, REUSE_OLD
, UNDECIDED
, NEITHER
} choose
= UNDECIDED
;
2676 /* If we have already committed to modifying it, then don't try and
2677 reuse another vtable. */
2678 if (BINFO_NEW_VTABLE_MARKED (binfo
))
2681 skip_rtti_stuff (&virtuals
, BINFO_TYPE (binfo
));
2682 skip_rtti_stuff (&old_virtuals
, BINFO_TYPE (binfo
));
2686 tree fndecl
= TREE_VALUE (virtuals
);
2687 tree old_fndecl
= TREE_VALUE (old_virtuals
);
2689 /* First check to see if they are the same. */
2690 if (DECL_ASSEMBLER_NAME (fndecl
) == DECL_ASSEMBLER_NAME (old_fndecl
))
2692 /* No need to do anything. */
2694 else if (strictly_overrides (fndecl
, old_fndecl
))
2696 if (choose
== UNDECIDED
)
2698 else if (choose
== REUSE_OLD
)
2701 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2703 prepare_fresh_vtable (binfo
, t
);
2704 override_one_vtable (binfo
, old
, t
);
2709 else if (strictly_overrides (old_fndecl
, fndecl
))
2711 if (choose
== UNDECIDED
)
2713 else if (choose
== REUSE_NEW
)
2716 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2718 prepare_fresh_vtable (binfo
, t
);
2719 override_one_vtable (binfo
, old
, t
);
2722 TREE_VALUE (virtuals
) = TREE_VALUE (old_virtuals
);
2724 else if (choose
== NEITHER
)
2726 TREE_VALUE (virtuals
) = TREE_VALUE (old_virtuals
);
2732 if (! BINFO_NEW_VTABLE_MARKED (binfo
))
2734 prepare_fresh_vtable (binfo
, t
);
2735 override_one_vtable (binfo
, old
, t
);
2739 /* This MUST be overridden, or the class is ill-formed. */
2740 tree fndecl
= TREE_VALUE (virtuals
);
2742 fndecl
= copy_node (fndecl
);
2743 copy_lang_decl (fndecl
);
2744 DECL_NEEDS_FINAL_OVERRIDER_P (fndecl
) = 1;
2745 /* Make sure we search for it later. */
2746 if (! CLASSTYPE_ABSTRACT_VIRTUALS (t
))
2747 CLASSTYPE_ABSTRACT_VIRTUALS (t
) = error_mark_node
;
2749 /* We can use integer_zero_node, as we will core dump
2750 if this is used anyway. */
2751 TREE_PURPOSE (virtuals
) = integer_zero_node
;
2752 TREE_VALUE (virtuals
) = fndecl
;
2755 virtuals
= TREE_CHAIN (virtuals
);
2756 old_virtuals
= TREE_CHAIN (old_virtuals
);
2759 /* Let's reuse the old vtable. */
2760 if (choose
== REUSE_OLD
)
2762 BINFO_VTABLE (binfo
) = BINFO_VTABLE (old
);
2763 BINFO_VIRTUALS (binfo
) = BINFO_VIRTUALS (old
);
2767 /* Merge in overrides for virtual bases.
2768 BINFO is the hierarchy we want to modify, and OLD has the potential
2772 merge_overrides (binfo
, old
, do_self
, t
)
2777 tree binfos
= BINFO_BASETYPES (binfo
);
2778 tree old_binfos
= BINFO_BASETYPES (old
);
2779 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2781 /* Should we use something besides CLASSTYPE_VFIELDS? */
2782 if (do_self
&& CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
2784 override_one_vtable (binfo
, old
, t
);
2787 for (i
= 0; i
< n_baselinks
; i
++)
2789 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2790 tree old_base_binfo
= TREE_VEC_ELT (old_binfos
, i
);
2791 int is_not_base_vtable
2792 = i
!= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
));
2793 if (! TREE_VIA_VIRTUAL (base_binfo
))
2794 merge_overrides (base_binfo
, old_base_binfo
, is_not_base_vtable
, t
);
2798 /* Get the base virtual function declarations in T that are either
2799 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2800 the overrider/hider. */
2803 get_basefndecls (fndecl
, t
)
2806 tree methods
= TYPE_METHODS (t
);
2807 tree base_fndecls
= NULL_TREE
;
2808 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
2809 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2813 if (TREE_CODE (methods
) == FUNCTION_DECL
2814 && DECL_VINDEX (methods
) != NULL_TREE
2815 && DECL_NAME (fndecl
) == DECL_NAME (methods
))
2816 base_fndecls
= temp_tree_cons (fndecl
, methods
, base_fndecls
);
2818 methods
= TREE_CHAIN (methods
);
2822 return base_fndecls
;
2824 for (i
= 0; i
< n_baseclasses
; i
++)
2826 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2827 tree basetype
= BINFO_TYPE (base_binfo
);
2829 base_fndecls
= chainon (get_basefndecls (fndecl
, basetype
),
2833 return base_fndecls
;
2836 /* Mark the functions that have been hidden with their overriders.
2837 Since we start out with all functions already marked with a hider,
2838 no need to mark functions that are just hidden.
2840 Subroutine of warn_hidden. */
2843 mark_overriders (fndecl
, base_fndecls
)
2844 tree fndecl
, base_fndecls
;
2846 for (; base_fndecls
; base_fndecls
= TREE_CHAIN (base_fndecls
))
2848 if (overrides (fndecl
, TREE_VALUE (base_fndecls
)))
2849 TREE_PURPOSE (base_fndecls
) = fndecl
;
2853 /* If this declaration supersedes the declaration of
2854 a method declared virtual in the base class, then
2855 mark this field as being virtual as well. */
2858 check_for_override (decl
, ctype
)
2861 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (ctype
));
2862 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2863 int virtualp
= DECL_VIRTUAL_P (decl
);
2864 int found_overriden_fn
= 0;
2866 for (i
= 0; i
< n_baselinks
; i
++)
2868 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2869 if (TYPE_VIRTUAL_P (BINFO_TYPE (base_binfo
)))
2871 tree tmp
= get_matching_virtual
2873 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl
)));
2875 if (tmp
&& !found_overriden_fn
)
2877 /* If this function overrides some virtual in some base
2878 class, then the function itself is also necessarily
2879 virtual, even if the user didn't explicitly say so. */
2880 DECL_VIRTUAL_P (decl
) = 1;
2882 /* The TMP we really want is the one from the deepest
2883 baseclass on this path, taking care not to
2884 duplicate if we have already found it (via another
2885 path to its virtual baseclass. */
2886 if (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
)
2888 cp_error_at ("`static %#D' cannot be declared", decl
);
2889 cp_error_at (" since `virtual %#D' declared in base class",
2896 = tree_cons (NULL_TREE
, tmp
, DECL_VINDEX (decl
));
2898 /* We now know that DECL overrides something,
2899 which is all that is important. But, we must
2900 continue to iterate through all the base-classes
2901 in order to allow get_matching_virtual to check for
2902 various illegal overrides. */
2903 found_overriden_fn
= 1;
2909 if (DECL_VINDEX (decl
) == NULL_TREE
)
2910 DECL_VINDEX (decl
) = error_mark_node
;
2911 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
)) = 1;
2915 /* Warn about hidden virtual functions that are not overridden in t.
2916 We know that constructors and destructors don't apply. */
2922 tree method_vec
= CLASSTYPE_METHOD_VEC (t
);
2923 int n_methods
= method_vec
? TREE_VEC_LENGTH (method_vec
) : 0;
2926 /* We go through each separately named virtual function. */
2927 for (i
= 2; i
< n_methods
&& TREE_VEC_ELT (method_vec
, i
); ++i
)
2929 tree fns
= TREE_VEC_ELT (method_vec
, i
);
2932 tree base_fndecls
= NULL_TREE
;
2933 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
2934 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2936 /* First see if we have any virtual functions in this batch. */
2937 for (; fns
; fns
= OVL_NEXT (fns
))
2939 fndecl
= OVL_CURRENT (fns
);
2940 if (DECL_VINDEX (fndecl
))
2944 if (fns
== NULL_TREE
)
2947 /* First we get a list of all possible functions that might be
2948 hidden from each base class. */
2949 for (i
= 0; i
< n_baseclasses
; i
++)
2951 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2952 tree basetype
= BINFO_TYPE (base_binfo
);
2954 base_fndecls
= chainon (get_basefndecls (fndecl
, basetype
),
2958 fns
= OVL_NEXT (fns
);
2960 /* ...then mark up all the base functions with overriders, preferring
2961 overriders to hiders. */
2963 for (; fns
; fns
= OVL_NEXT (fns
))
2965 fndecl
= OVL_CURRENT (fns
);
2966 if (DECL_VINDEX (fndecl
))
2967 mark_overriders (fndecl
, base_fndecls
);
2970 /* Now give a warning for all base functions without overriders,
2971 as they are hidden. */
2972 for (; base_fndecls
; base_fndecls
= TREE_CHAIN (base_fndecls
))
2974 if (! overrides (TREE_PURPOSE (base_fndecls
),
2975 TREE_VALUE (base_fndecls
)))
2977 /* Here we know it is a hider, and no overrider exists. */
2978 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls
));
2979 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls
));
2985 /* Check for things that are invalid. There are probably plenty of other
2986 things we should check for also. */
2989 finish_struct_anon (t
)
2994 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
2996 if (TREE_STATIC (field
))
2998 if (TREE_CODE (field
) != FIELD_DECL
)
3001 if (DECL_NAME (field
) == NULL_TREE
3002 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
3004 tree elt
= TYPE_FIELDS (TREE_TYPE (field
));
3005 for (; elt
; elt
= TREE_CHAIN (elt
))
3007 if (DECL_ARTIFICIAL (elt
))
3010 if (DECL_NAME (elt
) == constructor_name (t
))
3011 cp_pedwarn_at ("ANSI C++ forbids member `%D' with same name as enclosing class",
3014 if (TREE_CODE (elt
) != FIELD_DECL
)
3016 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3021 if (TREE_PRIVATE (elt
))
3022 cp_pedwarn_at ("private member `%#D' in anonymous union",
3024 else if (TREE_PROTECTED (elt
))
3025 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3028 TREE_PRIVATE (elt
) = TREE_PRIVATE (field
);
3029 TREE_PROTECTED (elt
) = TREE_PROTECTED (field
);
3035 extern int interface_only
, interface_unknown
;
3037 /* Create default constructors, assignment operators, and so forth for
3038 the type indicated by T, if they are needed.
3039 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3040 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3041 cannot have a default constructor, copy constructor taking a const
3042 reference argument, or an assignment operator, respectively. If a
3043 virtual destructor is created, its DECL is returned; otherwise the
3044 return value is NULL_TREE. */
3047 add_implicitly_declared_members (t
, cant_have_default_ctor
,
3048 cant_have_const_cctor
,
3049 cant_have_assignment
)
3051 int cant_have_default_ctor
;
3052 int cant_have_const_cctor
;
3053 int cant_have_assignment
;
3056 tree implicit_fns
= NULL_TREE
;
3057 tree name
= TYPE_IDENTIFIER (t
);
3058 tree virtual_dtor
= NULL_TREE
;
3062 if (TYPE_NEEDS_DESTRUCTOR (t
) && !TYPE_HAS_DESTRUCTOR (t
))
3064 default_fn
= cons_up_default_function (t
, name
, 0);
3065 check_for_override (default_fn
, t
);
3067 /* If we couldn't make it work, then pretend we didn't need it. */
3068 if (default_fn
== void_type_node
)
3069 TYPE_NEEDS_DESTRUCTOR (t
) = 0;
3072 TREE_CHAIN (default_fn
) = implicit_fns
;
3073 implicit_fns
= default_fn
;
3075 if (DECL_VINDEX (default_fn
))
3076 virtual_dtor
= default_fn
;
3079 TYPE_NEEDS_DESTRUCTOR (t
) |= TYPE_HAS_DESTRUCTOR (t
);
3081 /* Default constructor. */
3082 if (! TYPE_HAS_CONSTRUCTOR (t
) && ! cant_have_default_ctor
)
3084 default_fn
= cons_up_default_function (t
, name
, 2);
3085 TREE_CHAIN (default_fn
) = implicit_fns
;
3086 implicit_fns
= default_fn
;
3089 /* Copy constructor. */
3090 if (! TYPE_HAS_INIT_REF (t
) && ! TYPE_FOR_JAVA (t
))
3092 /* ARM 12.18: You get either X(X&) or X(const X&), but
3094 default_fn
= cons_up_default_function (t
, name
,
3095 3 + cant_have_const_cctor
);
3096 TREE_CHAIN (default_fn
) = implicit_fns
;
3097 implicit_fns
= default_fn
;
3100 /* Assignment operator. */
3101 if (! TYPE_HAS_ASSIGN_REF (t
) && ! TYPE_FOR_JAVA (t
))
3103 default_fn
= cons_up_default_function (t
, name
,
3104 5 + cant_have_assignment
);
3105 TREE_CHAIN (default_fn
) = implicit_fns
;
3106 implicit_fns
= default_fn
;
3109 /* Now, hook all of the new functions on to TYPE_METHODS,
3110 and add them to the CLASSTYPE_METHOD_VEC. */
3111 for (f
= &implicit_fns
; *f
; f
= &TREE_CHAIN (*f
))
3112 add_method (t
, 0, *f
);
3113 *f
= TYPE_METHODS (t
);
3114 TYPE_METHODS (t
) = implicit_fns
;
3116 return virtual_dtor
;
3119 /* Subroutine of finish_struct_1. Recursively count the number of fields
3120 in TYPE, including anonymous union members. */
3123 count_fields (fields
)
3128 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
3130 if (TREE_CODE (x
) == FIELD_DECL
&& ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3131 n_fields
+= count_fields (TYPE_FIELDS (TREE_TYPE (x
)));
3138 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3139 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3142 add_fields_to_vec (fields
, field_vec
, idx
)
3143 tree fields
, field_vec
;
3147 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
3149 if (TREE_CODE (x
) == FIELD_DECL
&& ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3150 idx
= add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x
)), field_vec
, idx
);
3152 TREE_VEC_ELT (field_vec
, idx
++) = x
;
3157 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
3158 (or C++ class declaration).
3160 For C++, we must handle the building of derived classes.
3161 Also, C++ allows static class members. The way that this is
3162 handled is to keep the field name where it is (as the DECL_NAME
3163 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
3164 of the field. layout_record and layout_union will know about this.
3166 More C++ hair: inline functions have text in their
3167 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
3168 meaningful tree structure. After the struct has been laid out, set
3169 things up so that this can happen.
3171 And still more: virtual functions. In the case of single inheritance,
3172 when a new virtual function is seen which redefines a virtual function
3173 from the base class, the new virtual function is placed into
3174 the virtual function table at exactly the same address that
3175 it had in the base class. When this is extended to multiple
3176 inheritance, the same thing happens, except that multiple virtual
3177 function tables must be maintained. The first virtual function
3178 table is treated in exactly the same way as in the case of single
3179 inheritance. Additional virtual function tables have different
3180 DELTAs, which tell how to adjust `this' to point to the right thing.
3182 ATTRIBUTES is the set of decl attributes to be applied, if any. */
3189 enum tree_code code
= TREE_CODE (t
);
3190 tree fields
= TYPE_FIELDS (t
);
3191 tree x
, last_x
, method_vec
;
3193 int max_has_virtual
;
3194 tree pending_virtuals
= NULL_TREE
;
3195 tree pending_hard_virtuals
= NULL_TREE
;
3196 tree abstract_virtuals
= NULL_TREE
;
3200 int cant_have_default_ctor
;
3201 int cant_have_const_ctor
;
3202 int no_const_asn_ref
;
3203 int has_mutable
= 0;
3205 int non_pod_class
= 0;
3207 /* The index of the first base class which has virtual
3208 functions. Only applied to non-virtual baseclasses. */
3209 int first_vfn_base_index
;
3212 int any_default_members
= 0;
3213 int const_sans_init
= 0;
3214 int ref_sans_init
= 0;
3215 tree access_decls
= NULL_TREE
;
3218 int has_pointers
= 0;
3219 tree inline_friends
;
3223 if (IS_AGGR_TYPE (t
))
3224 cp_error ("redefinition of `%#T'", t
);
3226 my_friendly_abort (172);
3231 GNU_xref_decl (current_function_decl
, t
);
3233 /* If this type was previously laid out as a forward reference,
3234 make sure we lay it out again. */
3236 TYPE_SIZE (t
) = NULL_TREE
;
3237 CLASSTYPE_GOT_SEMICOLON (t
) = 0;
3239 old
= suspend_momentary ();
3241 /* Install struct as DECL_FIELD_CONTEXT of each field decl.
3242 Also process specified field sizes.
3243 Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
3244 The specified size is found in the DECL_INITIAL.
3245 Store 0 there, except for ": 0" fields (so we can find them
3246 and delete them, below). */
3248 if (TYPE_BINFO_BASETYPES (t
))
3249 n_baseclasses
= TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t
));
3253 if (n_baseclasses
> 0)
3255 struct base_info base_info
;
3257 first_vfn_base_index
= finish_base_struct (t
, &base_info
);
3258 /* Remember where we got our vfield from. */
3259 CLASSTYPE_VFIELD_PARENT (t
) = first_vfn_base_index
;
3260 has_virtual
= base_info
.has_virtual
;
3261 max_has_virtual
= base_info
.max_has_virtual
;
3262 vfield
= base_info
.vfield
;
3263 vfields
= base_info
.vfields
;
3264 CLASSTYPE_RTTI (t
) = base_info
.rtti
;
3265 cant_have_default_ctor
= base_info
.cant_have_default_ctor
;
3266 cant_have_const_ctor
= base_info
.cant_have_const_ctor
;
3267 no_const_asn_ref
= base_info
.no_const_asn_ref
;
3272 first_vfn_base_index
= -1;
3274 max_has_virtual
= has_virtual
;
3276 vfields
= NULL_TREE
;
3277 CLASSTYPE_RTTI (t
) = NULL_TREE
;
3278 cant_have_default_ctor
= 0;
3279 cant_have_const_ctor
= 0;
3280 no_const_asn_ref
= 0;
3283 /* The three of these are approximations which may later be
3284 modified. Needed at this point to make add_virtual_function
3285 and modify_vtable_entries work. */
3286 CLASSTYPE_VFIELDS (t
) = vfields
;
3287 CLASSTYPE_VFIELD (t
) = vfield
;
3289 for (x
= TYPE_METHODS (t
); x
; x
= TREE_CHAIN (x
))
3291 GNU_xref_member (current_class_name
, x
);
3293 /* If this was an evil function, don't keep it in class. */
3294 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x
)))
3297 /* Do both of these, even though they're in the same union;
3298 if the insn `r' member and the size `i' member are
3299 different sizes, as on the alpha, the larger of the two
3300 will end up with garbage in it. */
3301 DECL_SAVED_INSNS (x
) = 0;
3302 DECL_FIELD_SIZE (x
) = 0;
3304 check_for_override (x
, t
);
3305 if (DECL_ABSTRACT_VIRTUAL_P (x
) && ! DECL_VINDEX (x
))
3306 cp_error_at ("initializer specified for non-virtual method `%D'", x
);
3308 /* The name of the field is the original field name
3309 Save this in auxiliary field for later overloading. */
3310 if (DECL_VINDEX (x
))
3312 add_virtual_function (&pending_virtuals
, &pending_hard_virtuals
,
3313 &has_virtual
, x
, t
);
3314 if (DECL_ABSTRACT_VIRTUAL_P (x
))
3315 abstract_virtuals
= tree_cons (NULL_TREE
, x
, abstract_virtuals
);
3317 /* XXX Why did I comment this out? (jason) */
3325 fields
= chainon (build_vbase_pointer_fields (t
), fields
);
3328 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
3330 tree type
= TREE_TYPE (x
);
3331 GNU_xref_member (current_class_name
, x
);
3333 if (TREE_CODE (x
) == FIELD_DECL
)
3335 DECL_PACKED (x
) |= TYPE_PACKED (t
);
3337 if (DECL_C_BIT_FIELD (x
) && integer_zerop (DECL_INITIAL (x
)))
3338 /* A zero-width bitfield doesn't do the trick. */;
3343 if (TREE_CODE (x
) == USING_DECL
)
3345 /* Save access declarations for later. */
3347 TREE_CHAIN (last_x
) = TREE_CHAIN (x
);
3349 fields
= TREE_CHAIN (x
);
3351 access_decls
= tree_cons (NULL_TREE
, x
, access_decls
);
3357 if (TREE_CODE (x
) == TYPE_DECL
3358 || TREE_CODE (x
) == TEMPLATE_DECL
)
3361 /* If we've gotten this far, it's a data member, possibly static,
3362 or an enumerator. */
3364 DECL_FIELD_CONTEXT (x
) = t
;
3366 /* ``A local class cannot have static data members.'' ARM 9.4 */
3367 if (current_function_decl
&& TREE_STATIC (x
))
3368 cp_error_at ("field `%D' in local class cannot be static", x
);
3370 /* Perform error checking that did not get done in
3372 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3374 cp_error_at ("field `%D' invalidly declared function type",
3376 type
= build_pointer_type (type
);
3377 TREE_TYPE (x
) = type
;
3379 else if (TREE_CODE (type
) == METHOD_TYPE
)
3381 cp_error_at ("field `%D' invalidly declared method type", x
);
3382 type
= build_pointer_type (type
);
3383 TREE_TYPE (x
) = type
;
3385 else if (TREE_CODE (type
) == OFFSET_TYPE
)
3387 cp_error_at ("field `%D' invalidly declared offset type", x
);
3388 type
= build_pointer_type (type
);
3389 TREE_TYPE (x
) = type
;
3393 if (DECL_NAME (x
) == constructor_name (t
))
3394 cant_have_default_ctor
= 1;
3397 if (type
== error_mark_node
)
3400 DECL_SAVED_INSNS (x
) = 0;
3401 DECL_FIELD_SIZE (x
) = 0;
3403 /* When this goes into scope, it will be a non-local reference. */
3404 DECL_NONLOCAL (x
) = 1;
3406 if (TREE_CODE (x
) == CONST_DECL
)
3409 if (TREE_CODE (x
) == VAR_DECL
)
3411 if (TREE_CODE (t
) == UNION_TYPE
)
3412 /* Unions cannot have static members. */
3413 cp_error_at ("field `%D' declared static in union", x
);
3418 /* Now it can only be a FIELD_DECL. */
3420 if (TREE_PRIVATE (x
) || TREE_PROTECTED (x
))
3423 /* If this is of reference type, check if it needs an init.
3424 Also do a little ANSI jig if necessary. */
3425 if (TREE_CODE (type
) == REFERENCE_TYPE
)
3429 if (DECL_INITIAL (x
) == NULL_TREE
)
3432 /* ARM $12.6.2: [A member initializer list] (or, for an
3433 aggregate, initialization by a brace-enclosed list) is the
3434 only way to initialize nonstatic const and reference
3436 cant_have_default_ctor
= 1;
3437 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) = 1;
3439 if (! TYPE_HAS_CONSTRUCTOR (t
) && extra_warnings
)
3442 cp_warning_at ("non-static reference `%#D' in class without a constructor", x
);
3444 cp_warning_at ("non-static reference in class without a constructor", x
);
3448 while (TREE_CODE (type
) == ARRAY_TYPE
)
3449 type
= TREE_TYPE (type
);
3451 if (TREE_CODE (type
) == POINTER_TYPE
)
3454 if (DECL_MUTABLE_P (x
) || TYPE_HAS_MUTABLE_P (type
))
3457 if (! pod_type_p (type
) || TYPE_PTRMEM_P (type
)
3458 || TYPE_PTRMEMFUNC_P (type
))
3461 /* If any field is const, the structure type is pseudo-const. */
3462 if (CP_TYPE_CONST_P (type
))
3464 C_TYPE_FIELDS_READONLY (t
) = 1;
3465 if (DECL_INITIAL (x
) == NULL_TREE
)
3466 const_sans_init
= 1;
3468 /* ARM $12.6.2: [A member initializer list] (or, for an
3469 aggregate, initialization by a brace-enclosed list) is the
3470 only way to initialize nonstatic const and reference
3472 cant_have_default_ctor
= 1;
3473 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) = 1;
3475 if (! TYPE_HAS_CONSTRUCTOR (t
) && extra_warnings
)
3478 cp_warning_at ("non-static const member `%#D' in class without a constructor", x
);
3480 cp_warning_at ("non-static const member in class without a constructor", x
);
3485 /* A field that is pseudo-const makes the structure
3487 if (IS_AGGR_TYPE (type
))
3489 if (C_TYPE_FIELDS_READONLY (type
))
3490 C_TYPE_FIELDS_READONLY (t
) = 1;
3491 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
))
3492 const_sans_init
= 1;
3496 /* We set DECL_C_BIT_FIELD in grokbitfield.
3497 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3498 if (DECL_C_BIT_FIELD (x
))
3500 /* Invalid bit-field size done by grokfield. */
3501 /* Detect invalid bit-field type. Simply checking if TYPE is
3502 integral is insufficient, as that is the array core of the
3503 field type. If TREE_TYPE (x) is integral, then TYPE must be
3505 if (DECL_INITIAL (x
)
3506 && ! INTEGRAL_TYPE_P (TREE_TYPE (x
)))
3508 cp_error_at ("bit-field `%#D' with non-integral type", x
);
3509 DECL_INITIAL (x
) = NULL
;
3512 /* Detect and ignore out of range field width. */
3513 if (DECL_INITIAL (x
))
3515 tree w
= DECL_INITIAL (x
);
3516 register int width
= 0;
3518 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3521 /* detect invalid field size. */
3522 if (TREE_CODE (w
) == CONST_DECL
)
3523 w
= DECL_INITIAL (w
);
3524 else if (TREE_READONLY_DECL_P (w
))
3525 w
= decl_constant_value (w
);
3527 if (TREE_CODE (w
) != INTEGER_CST
)
3529 cp_error_at ("bit-field `%D' width not an integer constant",
3531 DECL_INITIAL (x
) = NULL_TREE
;
3533 else if (width
= TREE_INT_CST_LOW (w
),
3536 DECL_INITIAL (x
) = NULL
;
3537 cp_error_at ("negative width in bit-field `%D'", x
);
3539 else if (width
== 0 && DECL_NAME (x
) != 0)
3541 DECL_INITIAL (x
) = NULL
;
3542 cp_error_at ("zero width for bit-field `%D'", x
);
3545 > TYPE_PRECISION (long_long_unsigned_type_node
))
3547 /* The backend will dump if you try to use something
3548 too big; avoid that. */
3549 DECL_INITIAL (x
) = NULL
;
3550 sorry ("bit-fields larger than %d bits",
3551 TYPE_PRECISION (long_long_unsigned_type_node
));
3552 cp_error_at (" in declaration of `%D'", x
);
3554 else if (width
> TYPE_PRECISION (type
)
3555 && TREE_CODE (type
) != ENUMERAL_TYPE
3556 && TREE_CODE (type
) != BOOLEAN_TYPE
)
3558 cp_warning_at ("width of `%D' exceeds its type", x
);
3560 else if (TREE_CODE (type
) == ENUMERAL_TYPE
3561 && ((min_precision (TYPE_MIN_VALUE (type
),
3562 TREE_UNSIGNED (type
)) > width
)
3563 || (min_precision (TYPE_MAX_VALUE (type
),
3564 TREE_UNSIGNED (type
)) > width
)))
3566 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3570 if (DECL_INITIAL (x
))
3572 DECL_INITIAL (x
) = NULL_TREE
;
3573 DECL_FIELD_SIZE (x
) = width
;
3574 DECL_BIT_FIELD (x
) = 1;
3578 #ifdef EMPTY_FIELD_BOUNDARY
3579 DECL_ALIGN (x
) = MAX (DECL_ALIGN (x
),
3580 EMPTY_FIELD_BOUNDARY
);
3582 #ifdef PCC_BITFIELD_TYPE_MATTERS
3583 if (PCC_BITFIELD_TYPE_MATTERS
)
3584 DECL_ALIGN (x
) = MAX (DECL_ALIGN (x
),
3591 /* Non-bit-fields are aligned for their type. */
3592 DECL_ALIGN (x
) = MAX (DECL_ALIGN (x
), TYPE_ALIGN (type
));
3596 if (CLASS_TYPE_P (type
) && ! ANON_AGGR_TYPE_P (type
))
3598 /* Never let anything with uninheritable virtuals
3599 make it through without complaint. */
3600 abstract_virtuals_error (x
, type
);
3602 if (code
== UNION_TYPE
)
3604 const char *fie
= NULL
;
3605 if (TYPE_NEEDS_CONSTRUCTING (type
))
3606 fie
= "constructor";
3607 else if (TYPE_NEEDS_DESTRUCTOR (type
))
3609 else if (TYPE_HAS_COMPLEX_ASSIGN_REF (type
))
3610 fie
= "copy assignment operator";
3612 cp_error_at ("member `%#D' with %s not allowed in union", x
,
3617 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (type
);
3618 TYPE_NEEDS_DESTRUCTOR (t
) |= TYPE_NEEDS_DESTRUCTOR (type
);
3619 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type
);
3620 TYPE_HAS_COMPLEX_INIT_REF (t
) |= TYPE_HAS_COMPLEX_INIT_REF (type
);
3623 if (!TYPE_HAS_CONST_INIT_REF (type
))
3624 cant_have_const_ctor
= 1;
3626 if (!TYPE_HAS_CONST_ASSIGN_REF (type
))
3627 no_const_asn_ref
= 1;
3629 if (TYPE_HAS_CONSTRUCTOR (type
)
3630 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
3632 cant_have_default_ctor
= 1;
3634 /* This is wrong for aggregates. */
3635 if (! TYPE_HAS_CONSTRUCTOR (t
))
3638 cp_pedwarn_at ("member `%#D' with only non-default constructor", x
);
3640 cp_pedwarn_at ("member with only non-default constructor", x
);
3641 cp_pedwarn_at ("in class without a constructor",
3647 if (DECL_INITIAL (x
) != NULL_TREE
)
3649 /* `build_class_init_list' does not recognize
3651 if (code
== UNION_TYPE
&& any_default_members
!= 0)
3652 cp_error_at ("multiple fields in union `%T' initialized");
3653 any_default_members
= 1;
3658 /* If this type has any constant members which did not come
3659 with their own initialization, mark that fact here. It is
3660 not an error here, since such types can be saved either by their
3661 constructors, or by fortuitous initialization. */
3662 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
) = const_sans_init
;
3663 CLASSTYPE_REF_FIELDS_NEED_INIT (t
) = ref_sans_init
;
3664 CLASSTYPE_ABSTRACT_VIRTUALS (t
) = abstract_virtuals
;
3665 CLASSTYPE_HAS_MUTABLE (t
) = has_mutable
;
3667 /* Effective C++ rule 11. */
3668 if (has_pointers
&& warn_ecpp
&& TYPE_HAS_CONSTRUCTOR (t
)
3669 && ! (TYPE_HAS_INIT_REF (t
) && TYPE_HAS_ASSIGN_REF (t
)))
3671 cp_warning ("`%#T' has pointer data members", t
);
3673 if (! TYPE_HAS_INIT_REF (t
))
3675 cp_warning (" but does not override `%T(const %T&)'", t
, t
);
3676 if (! TYPE_HAS_ASSIGN_REF (t
))
3677 cp_warning (" or `operator=(const %T&)'", t
);
3679 else if (! TYPE_HAS_ASSIGN_REF (t
))
3680 cp_warning (" but does not override `operator=(const %T&)'", t
);
3683 /* Do some bookkeeping that will guide the generation of implicitly
3684 declared member functions. */
3685 TYPE_HAS_COMPLEX_INIT_REF (t
)
3686 |= (TYPE_HAS_INIT_REF (t
) || TYPE_USES_VIRTUAL_BASECLASSES (t
)
3687 || has_virtual
|| any_default_members
);
3688 TYPE_NEEDS_CONSTRUCTING (t
)
3689 |= (TYPE_HAS_CONSTRUCTOR (t
) || TYPE_USES_VIRTUAL_BASECLASSES (t
)
3690 || has_virtual
|| any_default_members
);
3691 CLASSTYPE_NON_AGGREGATE (t
)
3692 = ! aggregate
|| has_virtual
|| TYPE_HAS_CONSTRUCTOR (t
);
3693 CLASSTYPE_NON_POD_P (t
)
3694 = non_pod_class
|| CLASSTYPE_NON_AGGREGATE (t
)
3695 || TYPE_HAS_DESTRUCTOR (t
) || TYPE_HAS_ASSIGN_REF (t
);
3696 TYPE_HAS_REAL_ASSIGN_REF (t
) |= TYPE_HAS_ASSIGN_REF (t
);
3697 TYPE_HAS_COMPLEX_ASSIGN_REF (t
)
3698 |= TYPE_HAS_ASSIGN_REF (t
) || TYPE_USES_VIRTUAL_BASECLASSES (t
);
3700 /* Synthesize any needed methods. Note that methods will be synthesized
3701 for anonymous unions; grok_x_components undoes that. */
3703 = add_implicitly_declared_members (t
, cant_have_default_ctor
,
3704 cant_have_const_ctor
,
3707 add_virtual_function (&pending_virtuals
, &pending_hard_virtuals
,
3708 &has_virtual
, virtual_dtor
, t
);
3710 if (TYPE_METHODS (t
))
3712 finish_struct_methods (t
);
3713 method_vec
= CLASSTYPE_METHOD_VEC (t
);
3719 /* Just in case these got accidentally
3720 filled in by syntax errors. */
3721 TYPE_HAS_CONSTRUCTOR (t
) = 0;
3722 TYPE_HAS_DESTRUCTOR (t
) = 0;
3725 for (access_decls
= nreverse (access_decls
); access_decls
;
3726 access_decls
= TREE_CHAIN (access_decls
))
3727 handle_using_decl (TREE_VALUE (access_decls
), t
, method_vec
, fields
);
3729 if (vfield
== NULL_TREE
&& has_virtual
)
3731 /* We build this decl with vtbl_ptr_type_node, which is a
3732 `vtable_entry_type*'. It might seem more precise to use
3733 `vtable_entry_type (*)[N]' where N is the number of firtual
3734 functions. However, that would require the vtable pointer in
3735 base classes to have a different type than the vtable pointer
3736 in derived classes. We could make that happen, but that
3737 still wouldn't solve all the problems. In particular, the
3738 type-based alias analysis code would decide that assignments
3739 to the base class vtable pointer can't alias assignments to
3740 the derived class vtable pointer, since they have different
3741 types. Thus, in an derived class destructor, where the base
3742 class constructor was inlined, we could generate bad code for
3743 setting up the vtable pointer.
3745 Therefore, we use one type for all vtable pointers. We still
3746 use a type-correct type; it's just doesn't indicate the array
3747 bounds. That's better than using `void*' or some such; it's
3748 cleaner, and it let's the alias analysis code know that these
3749 stores cannot alias stores to void*! */
3750 vfield
= build_lang_decl (FIELD_DECL
, get_vfield_name (t
),
3751 vtbl_ptr_type_node
);
3752 /* If you change any of the below, take a look at all the
3753 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
3755 DECL_ASSEMBLER_NAME (vfield
) = get_identifier (VFIELD_BASE
);
3756 CLASSTYPE_VFIELD (t
) = vfield
;
3757 DECL_VIRTUAL_P (vfield
) = 1;
3758 DECL_ARTIFICIAL (vfield
) = 1;
3759 DECL_FIELD_CONTEXT (vfield
) = t
;
3760 DECL_CLASS_CONTEXT (vfield
) = t
;
3761 DECL_FCONTEXT (vfield
) = t
;
3762 DECL_SAVED_INSNS (vfield
) = 0;
3763 DECL_FIELD_SIZE (vfield
) = 0;
3764 DECL_ALIGN (vfield
) = TYPE_ALIGN (ptr_type_node
);
3766 /* This is more efficient, but breaks binary compatibility, turn
3767 it on sometime when we don't care. If we turn it on, we also
3768 have to enable the code in dfs_init_vbase_pointers. */
3769 /* vfield is always first entry in structure. */
3770 TREE_CHAIN (vfield
) = fields
;
3775 my_friendly_assert (TREE_CHAIN (last_x
) == NULL_TREE
, 175);
3776 TREE_CHAIN (last_x
) = vfield
;
3783 vfields
= chainon (vfields
, build_tree_list (NULL_TREE
, t
));
3786 /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
3788 C++: maybe we will support default field initialization some day... */
3790 /* Delete all duplicate fields from the fields */
3791 delete_duplicate_fields (fields
);
3793 /* Now we have the nearly final fieldlist for the data fields. Record it,
3794 then lay out the structure or union (including the fields). */
3796 TYPE_FIELDS (t
) = fields
;
3800 last_x
= build_base_fields (t
);
3802 /* If all our bases are empty, we can be empty too. */
3803 for (x
= last_x
; empty
&& x
; x
= TREE_CHAIN (x
))
3804 if (DECL_SIZE (x
) != integer_zero_node
)
3808 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
3809 we have to save this before we start modifying
3810 TYPE_NONCOPIED_PARTS. */
3811 inline_friends
= CLASSTYPE_INLINE_FRIENDS (t
);
3812 CLASSTYPE_INLINE_FRIENDS (t
) = NULL_TREE
;
3816 /* C++: do not let empty structures exist. */
3817 tree decl
= build_lang_decl
3818 (FIELD_DECL
, NULL_TREE
, char_type_node
);
3819 TREE_CHAIN (decl
) = fields
;
3820 TYPE_FIELDS (t
) = decl
;
3821 TYPE_NONCOPIED_PARTS (t
)
3822 = tree_cons (NULL_TREE
, decl
, TYPE_NONCOPIED_PARTS (t
));
3823 TREE_STATIC (TYPE_NONCOPIED_PARTS (t
)) = 1;
3827 TYPE_FIELDS (t
) = chainon (last_x
, TYPE_FIELDS (t
));
3831 /* Remember the size and alignment of the class before adding
3832 the virtual bases. */
3833 if (empty
&& flag_new_abi
)
3834 CLASSTYPE_SIZE (t
) = integer_zero_node
;
3835 else if (flag_new_abi
&& TYPE_HAS_COMPLEX_INIT_REF (t
)
3836 && TYPE_HAS_COMPLEX_ASSIGN_REF (t
))
3837 CLASSTYPE_SIZE (t
) = TYPE_BINFO_SIZE (t
);
3839 CLASSTYPE_SIZE (t
) = TYPE_SIZE (t
);
3840 CLASSTYPE_ALIGN (t
) = TYPE_ALIGN (t
);
3842 finish_struct_anon (t
);
3844 /* Set the TYPE_DECL for this type to contain the right
3845 value for DECL_OFFSET, so that we can use it as part
3846 of a COMPONENT_REF for multiple inheritance. */
3848 layout_decl (TYPE_MAIN_DECL (t
), 0);
3850 /* Now fix up any virtual base class types that we left lying
3851 around. We must get these done before we try to lay out the
3852 virtual function table. */
3853 pending_hard_virtuals
= nreverse (pending_hard_virtuals
);
3856 /* layout_basetypes will remove the base subobject fields. */
3857 max_has_virtual
= layout_basetypes (t
, max_has_virtual
);
3859 TYPE_FIELDS (t
) = fields
;
3861 my_friendly_assert (TYPE_FIELDS (t
) == fields
, 981117);
3863 /* Delete all zero-width bit-fields from the front of the fieldlist */
3864 while (fields
&& DECL_C_BIT_FIELD (fields
)
3865 && DECL_INITIAL (fields
))
3866 fields
= TREE_CHAIN (fields
);
3867 /* Delete all such fields from the rest of the fields. */
3868 for (x
= fields
; x
;)
3870 if (TREE_CHAIN (x
) && DECL_C_BIT_FIELD (TREE_CHAIN (x
))
3871 && DECL_INITIAL (TREE_CHAIN (x
)))
3872 TREE_CHAIN (x
) = TREE_CHAIN (TREE_CHAIN (x
));
3876 TYPE_FIELDS (t
) = fields
;
3878 if (TYPE_USES_VIRTUAL_BASECLASSES (t
))
3882 vbases
= CLASSTYPE_VBASECLASSES (t
);
3885 /* Now fixup overrides of all functions in vtables from all
3886 direct or indirect virtual base classes. */
3887 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
3888 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
3890 for (i
= 0; i
< n_baseclasses
; i
++)
3892 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
3893 tree basetype
= BINFO_TYPE (base_binfo
);
3896 vbases
= CLASSTYPE_VBASECLASSES (basetype
);
3899 merge_overrides (binfo_member (BINFO_TYPE (vbases
),
3900 CLASSTYPE_VBASECLASSES (t
)),
3902 vbases
= TREE_CHAIN (vbases
);
3908 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
3909 might need to know it for setting up the offsets in the vtable
3910 (or in thunks) below. */
3911 if (vfield
!= NULL_TREE
3912 && DECL_FIELD_CONTEXT (vfield
) != t
)
3914 tree binfo
= get_binfo (DECL_FIELD_CONTEXT (vfield
), t
, 0);
3915 tree offset
= BINFO_OFFSET (binfo
);
3917 vfield
= copy_node (vfield
);
3918 copy_lang_decl (vfield
);
3920 if (! integer_zerop (offset
))
3921 offset
= size_binop (MULT_EXPR
, offset
, size_int (BITS_PER_UNIT
));
3922 DECL_FIELD_CONTEXT (vfield
) = t
;
3923 DECL_CLASS_CONTEXT (vfield
) = t
;
3924 DECL_FIELD_BITPOS (vfield
)
3925 = size_binop (PLUS_EXPR
, offset
, DECL_FIELD_BITPOS (vfield
));
3926 CLASSTYPE_VFIELD (t
) = vfield
;
3930 cp_warning ("Doing hard virtuals for %T...", t
);
3933 if (has_virtual
> max_has_virtual
)
3934 max_has_virtual
= has_virtual
;
3935 if (max_has_virtual
> 0)
3936 TYPE_VIRTUAL_P (t
) = 1;
3938 if (flag_rtti
&& TYPE_VIRTUAL_P (t
) && !pending_hard_virtuals
)
3939 modify_all_vtables (t
, NULL_TREE
);
3941 while (pending_hard_virtuals
)
3943 modify_all_vtables (t
,
3944 TREE_VALUE (pending_hard_virtuals
));
3945 pending_hard_virtuals
= TREE_CHAIN (pending_hard_virtuals
);
3948 if (TYPE_USES_VIRTUAL_BASECLASSES (t
))
3951 /* Now fixup any virtual function entries from virtual bases
3952 that have different deltas. This has to come after we do the
3953 pending hard virtuals, as we might have a function that comes
3954 from multiple virtual base instances that is only overridden
3955 by a hard virtual above. */
3956 vbases
= CLASSTYPE_VBASECLASSES (t
);
3959 /* We might be able to shorten the amount of work we do by
3960 only doing this for vtables that come from virtual bases
3961 that have differing offsets, but don't want to miss any
3963 fixup_vtable_deltas (vbases
, 1, t
);
3964 vbases
= TREE_CHAIN (vbases
);
3968 /* Under our model of GC, every C++ class gets its own virtual
3969 function table, at least virtually. */
3970 if (pending_virtuals
)
3972 pending_virtuals
= nreverse (pending_virtuals
);
3973 /* We must enter these virtuals into the table. */
3974 if (first_vfn_base_index
< 0)
3976 if (! CLASSTYPE_COM_INTERFACE (t
))
3978 /* The second slot is for the tdesc pointer when thunks are used. */
3979 if (flag_vtable_thunks
)
3980 pending_virtuals
= tree_cons (NULL_TREE
, NULL_TREE
, pending_virtuals
);
3982 /* The first slot is for the rtti offset. */
3983 pending_virtuals
= tree_cons (NULL_TREE
, NULL_TREE
, pending_virtuals
);
3985 set_rtti_entry (pending_virtuals
,
3986 convert (ssizetype
, integer_zero_node
), t
);
3988 build_vtable (NULL_TREE
, t
);
3992 /* Here we know enough to change the type of our virtual
3993 function table, but we will wait until later this function. */
3995 if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t
)))
3996 build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t
), first_vfn_base_index
), t
);
3999 /* If this type has basetypes with constructors, then those
4000 constructors might clobber the virtual function table. But
4001 they don't if the derived class shares the exact vtable of the base
4004 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t
) = 1;
4006 else if (first_vfn_base_index
>= 0)
4008 tree binfo
= TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t
), first_vfn_base_index
);
4009 /* This class contributes nothing new to the virtual function
4010 table. However, it may have declared functions which
4011 went into the virtual function table "inherited" from the
4012 base class. If so, we grab a copy of those updated functions,
4013 and pretend they are ours. */
4015 /* See if we should steal the virtual info from base class. */
4016 if (TYPE_BINFO_VTABLE (t
) == NULL_TREE
)
4017 TYPE_BINFO_VTABLE (t
) = BINFO_VTABLE (binfo
);
4018 if (TYPE_BINFO_VIRTUALS (t
) == NULL_TREE
)
4019 TYPE_BINFO_VIRTUALS (t
) = BINFO_VIRTUALS (binfo
);
4020 if (TYPE_BINFO_VTABLE (t
) != BINFO_VTABLE (binfo
))
4021 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t
) = 1;
4024 if (max_has_virtual
|| first_vfn_base_index
>= 0)
4026 CLASSTYPE_VSIZE (t
) = has_virtual
;
4027 if (first_vfn_base_index
>= 0)
4029 if (pending_virtuals
)
4030 TYPE_BINFO_VIRTUALS (t
) = chainon (TYPE_BINFO_VIRTUALS (t
),
4033 else if (has_virtual
)
4035 TYPE_BINFO_VIRTUALS (t
) = pending_virtuals
;
4036 DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t
)) = 1;
4040 /* Now lay out the virtual function table. */
4043 /* Use size_int so values are memoized in common cases. */
4044 tree itype
= build_index_type (size_int (has_virtual
));
4045 tree atype
= build_cplus_array_type (vtable_entry_type
, itype
);
4047 layout_type (atype
);
4049 CLASSTYPE_VFIELD (t
) = vfield
;
4051 /* We may have to grow the vtable. */
4052 if (TREE_TYPE (TYPE_BINFO_VTABLE (t
)) != atype
)
4054 TREE_TYPE (TYPE_BINFO_VTABLE (t
)) = atype
;
4055 DECL_SIZE (TYPE_BINFO_VTABLE (t
)) = 0;
4056 layout_decl (TYPE_BINFO_VTABLE (t
), 0);
4057 /* At one time the vtable info was grabbed 2 words at a time. This
4058 fails on sparc unless you have 8-byte alignment. (tiemann) */
4059 DECL_ALIGN (TYPE_BINFO_VTABLE (t
))
4060 = MAX (TYPE_ALIGN (double_type_node
),
4061 DECL_ALIGN (TYPE_BINFO_VTABLE (t
)));
4064 else if (first_vfn_base_index
>= 0)
4065 CLASSTYPE_VFIELD (t
) = vfield
;
4066 CLASSTYPE_VFIELDS (t
) = vfields
;
4068 finish_struct_bits (t
, max_has_virtual
);
4070 /* Complete the rtl for any static member objects of the type we're
4072 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
4074 if (TREE_CODE (x
) == VAR_DECL
&& TREE_STATIC (x
)
4075 && TREE_TYPE (x
) == t
)
4077 DECL_MODE (x
) = TYPE_MODE (t
);
4078 make_decl_rtl (x
, NULL
, 0);
4082 /* Done with FIELDS...now decide whether to sort these for
4083 faster lookups later. Don't worry about optimizing
4084 for structs only declared in inline functions...they're
4085 not going to be referenced anywhere else.
4087 The C front-end only does this when n_fields > 15. We use
4088 a smaller number because most searches fail (succeeding
4089 ultimately as the search bores through the inheritance
4090 hierarchy), and we want this failure to occur quickly. */
4092 n_fields
= count_fields (fields
);
4093 if (n_fields
> 7 && !allocation_temporary_p ())
4095 tree field_vec
= make_tree_vec (n_fields
);
4096 add_fields_to_vec (fields
, field_vec
, 0);
4097 qsort (&TREE_VEC_ELT (field_vec
, 0), n_fields
, sizeof (tree
),
4098 (int (*)(const void *, const void *))field_decl_cmp
);
4099 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t
)))
4100 retrofit_lang_decl (TYPE_MAIN_DECL (t
));
4101 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t
)) = field_vec
;
4104 if (TYPE_HAS_CONSTRUCTOR (t
))
4106 tree vfields
= CLASSTYPE_VFIELDS (t
);
4110 /* Mark the fact that constructor for T
4111 could affect anybody inheriting from T
4112 who wants to initialize vtables for VFIELDS's type. */
4113 if (VF_DERIVED_VALUE (vfields
))
4114 TREE_ADDRESSABLE (vfields
) = 1;
4115 vfields
= TREE_CHAIN (vfields
);
4119 /* Write out inline function definitions. */
4120 do_inline_function_hair (t
, inline_friends
);
4122 if (CLASSTYPE_VSIZE (t
) != 0)
4124 /* In addition to this one, all the other vfields should be listed. */
4125 /* Before that can be done, we have to have FIELD_DECLs for them, and
4126 a place to find them. */
4127 TYPE_NONCOPIED_PARTS (t
)
4128 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t
)),
4129 vfield
, TYPE_NONCOPIED_PARTS (t
));
4131 if (warn_nonvdtor
&& TYPE_HAS_DESTRUCTOR (t
)
4132 && DECL_VINDEX (TREE_VEC_ELT (method_vec
, 1)) == NULL_TREE
)
4133 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
4137 /* Make the rtl for any new vtables we have created, and unmark
4138 the base types we marked. */
4139 finish_vtbls (TYPE_BINFO (t
), 1, t
);
4140 hack_incomplete_structures (t
);
4143 if (TYPE_NAME (t
) && TYPE_IDENTIFIER (t
))
4144 undo_template_name_overload (TYPE_IDENTIFIER (t
), 1);
4147 resume_momentary (old
);
4149 if (warn_overloaded_virtual
)
4153 /* This has to be done after we have sorted out what to do with
4154 the enclosing type. */
4155 if (write_symbols
!= DWARF_DEBUG
)
4157 /* Be smarter about nested classes here. If a type is nested,
4158 only output it if we would output the enclosing type. */
4159 if (DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (t
)))
4160 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = TREE_ASM_WRITTEN (TYPE_MAIN_DECL (t
));
4164 if (write_symbols
!= DWARF_DEBUG
&& write_symbols
!= DWARF2_DEBUG
)
4166 /* If the type has methods, we want to think about cutting down
4167 the amount of symbol table stuff we output. The value stored in
4168 the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
4169 For example, if a member function is seen and we decide to
4170 write out that member function, then we can change the value
4171 of the DECL_IGNORED_P slot, and the type will be output when
4172 that member function's debug info is written out.
4174 We can't do this with DWARF, which does not support name
4175 references between translation units. */
4176 if (CLASSTYPE_METHOD_VEC (t
))
4178 /* Don't output full info about any type
4179 which does not have its implementation defined here. */
4180 if (CLASSTYPE_INTERFACE_ONLY (t
))
4181 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 1;
4183 /* XXX do something about this. */
4184 else if (CLASSTYPE_INTERFACE_UNKNOWN (t
))
4185 /* Only a first approximation! */
4186 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 1;
4189 else if (CLASSTYPE_INTERFACE_ONLY (t
))
4190 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 1;
4193 /* Finish debugging output for this type. */
4194 rest_of_type_compilation (t
, toplevel_bindings_p ());
4199 /* When T was built up, the member declarations were added in reverse
4200 order. Rearrange them to declaration order. */
4203 unreverse_member_declarations (t
)
4210 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
4211 reverse order. Put them in declaration order now. */
4212 TYPE_METHODS (t
) = nreverse (TYPE_METHODS (t
));
4213 CLASSTYPE_TAGS (t
) = nreverse (CLASSTYPE_TAGS (t
));
4215 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
4216 reverse order, so we can't just use nreverse. */
4218 for (x
= TYPE_FIELDS (t
);
4219 x
&& TREE_CODE (x
) != TYPE_DECL
;
4222 next
= TREE_CHAIN (x
);
4223 TREE_CHAIN (x
) = prev
;
4228 TREE_CHAIN (TYPE_FIELDS (t
)) = x
;
4230 TYPE_FIELDS (t
) = prev
;
4235 finish_struct (t
, attributes
)
4238 /* Now that we've got all the field declarations, reverse everything
4240 unreverse_member_declarations (t
);
4242 cplus_decl_attributes (t
, attributes
, NULL_TREE
);
4244 if (processing_template_decl
)
4246 finish_struct_methods (t
);
4247 TYPE_SIZE (t
) = integer_zero_node
;
4250 finish_struct_1 (t
);
4252 TYPE_BEING_DEFINED (t
) = 0;
4254 if (current_class_type
)
4257 error ("trying to finish struct, but kicked out due to previous parse errors.");
4262 /* Return the dynamic type of INSTANCE, if known.
4263 Used to determine whether the virtual function table is needed
4266 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4267 of our knowledge of its type. */
4270 fixed_type_or_null (instance
, nonnull
)
4274 switch (TREE_CODE (instance
))
4277 /* Check that we are not going through a cast of some sort. */
4278 if (TREE_TYPE (instance
)
4279 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance
, 0))))
4280 instance
= TREE_OPERAND (instance
, 0);
4281 /* fall through... */
4283 /* This is a call to a constructor, hence it's never zero. */
4284 if (TREE_HAS_CONSTRUCTOR (instance
))
4288 return TREE_TYPE (instance
);
4293 /* This is a call to a constructor, hence it's never zero. */
4294 if (TREE_HAS_CONSTRUCTOR (instance
))
4298 return TREE_TYPE (instance
);
4300 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
4307 if (TREE_CODE (TREE_OPERAND (instance
, 1)) == INTEGER_CST
)
4308 /* Propagate nonnull. */
4309 fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
4310 if (TREE_CODE (TREE_OPERAND (instance
, 0)) == ADDR_EXPR
)
4311 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
4316 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
4321 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
4324 return fixed_type_or_null (TREE_OPERAND (instance
, 1), nonnull
);
4328 if (TREE_CODE (TREE_TYPE (instance
)) == ARRAY_TYPE
4329 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance
))))
4333 return TREE_TYPE (TREE_TYPE (instance
));
4335 /* fall through... */
4338 if (IS_AGGR_TYPE (TREE_TYPE (instance
)))
4342 return TREE_TYPE (instance
);
4346 if (instance
== current_class_ptr
4347 && flag_this_is_variable
<= 0)
4349 /* Normally, 'this' must be non-null. */
4350 if (flag_this_is_variable
== 0)
4353 /* <0 means we're in a constructor and we know our type. */
4354 if (flag_this_is_variable
< 0)
4355 return TREE_TYPE (TREE_TYPE (instance
));
4357 else if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
4358 /* Reference variables should be references to objects. */
4368 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
4369 to the static type. We also handle the case where INSTANCE is really
4372 Used to determine whether the virtual function table is needed
4375 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4376 of our knowledge of its type. */
4379 resolves_to_fixed_type_p (instance
, nonnull
)
4383 tree t
= TREE_TYPE (instance
);
4384 tree fixed
= fixed_type_or_null (instance
, nonnull
);
4385 if (fixed
== NULL_TREE
)
4387 if (POINTER_TYPE_P (t
))
4389 return same_type_p (TYPE_MAIN_VARIANT (t
), TYPE_MAIN_VARIANT (fixed
));
4394 init_class_processing ()
4396 current_class_depth
= 0;
4397 current_class_stack_size
= 10;
4399 = (class_stack_node_t
) xmalloc (current_class_stack_size
4400 * sizeof (struct class_stack_node
));
4402 access_default_node
= build_int_2 (0, 0);
4403 access_public_node
= build_int_2 (1, 0);
4404 access_protected_node
= build_int_2 (2, 0);
4405 access_private_node
= build_int_2 (3, 0);
4406 access_default_virtual_node
= build_int_2 (4, 0);
4407 access_public_virtual_node
= build_int_2 (5, 0);
4408 access_protected_virtual_node
= build_int_2 (6, 0);
4409 access_private_virtual_node
= build_int_2 (7, 0);
4412 /* Set current scope to NAME. CODE tells us if this is a
4413 STRUCT, UNION, or ENUM environment.
4415 NAME may end up being NULL_TREE if this is an anonymous or
4416 late-bound struct (as in "struct { ... } foo;") */
4418 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
4419 appropriate values, found by looking up the type definition of
4422 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
4423 which can be seen locally to the class. They are shadowed by
4424 any subsequent local declaration (including parameter names).
4426 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
4427 which have static meaning (i.e., static members, static
4428 member functions, enum declarations, etc).
4430 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
4431 which can be seen locally to the class (as in 1), but
4432 know that we are doing this for declaration purposes
4433 (i.e. friend foo::bar (int)).
4435 So that we may avoid calls to lookup_name, we cache the _TYPE
4436 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
4438 For multiple inheritance, we perform a two-pass depth-first search
4439 of the type lattice. The first pass performs a pre-order search,
4440 marking types after the type has had its fields installed in
4441 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
4442 unmarks the marked types. If a field or member function name
4443 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
4444 that name becomes `error_mark_node'. */
4447 pushclass (type
, modify
)
4451 type
= TYPE_MAIN_VARIANT (type
);
4453 /* Make sure there is enough room for the new entry on the stack. */
4454 if (current_class_depth
+ 1 >= current_class_stack_size
)
4456 current_class_stack_size
*= 2;
4458 = (class_stack_node_t
) xrealloc (current_class_stack
,
4459 current_class_stack_size
4460 * sizeof (struct class_stack_node
));
4463 /* Insert a new entry on the class stack. */
4464 current_class_stack
[current_class_depth
].name
= current_class_name
;
4465 current_class_stack
[current_class_depth
].type
= current_class_type
;
4466 current_class_stack
[current_class_depth
].access
= current_access_specifier
;
4467 current_class_stack
[current_class_depth
].names_used
= 0;
4468 current_class_depth
++;
4470 /* Now set up the new type. */
4471 current_class_name
= TYPE_NAME (type
);
4472 if (TREE_CODE (current_class_name
) == TYPE_DECL
)
4473 current_class_name
= DECL_NAME (current_class_name
);
4474 current_class_type
= type
;
4476 /* By default, things in classes are private, while things in
4477 structures or unions are public. */
4478 current_access_specifier
= (CLASSTYPE_DECLARED_CLASS (type
)
4479 ? access_private_node
4480 : access_public_node
);
4482 if (previous_class_type
!= NULL_TREE
4483 && (type
!= previous_class_type
4484 || TYPE_SIZE (previous_class_type
) == NULL_TREE
)
4485 && current_class_depth
== 1)
4487 /* Forcibly remove any old class remnants. */
4488 invalidate_class_lookup_cache ();
4491 /* If we're about to enter a nested class, clear
4492 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
4493 if (modify
&& current_class_depth
> 1)
4494 clear_identifier_class_values ();
4499 if (CLASSTYPE_TEMPLATE_INFO (type
))
4500 overload_template_name (type
);
4505 if (type
!= previous_class_type
|| current_class_depth
> 1)
4506 push_class_decls (type
);
4511 /* We are re-entering the same class we just left, so we
4512 don't have to search the whole inheritance matrix to find
4513 all the decls to bind again. Instead, we install the
4514 cached class_shadowed list, and walk through it binding
4515 names and setting up IDENTIFIER_TYPE_VALUEs. */
4516 set_class_shadows (previous_class_values
);
4517 for (item
= previous_class_values
; item
; item
= TREE_CHAIN (item
))
4519 tree id
= TREE_PURPOSE (item
);
4520 tree decl
= TREE_TYPE (item
);
4522 push_class_binding (id
, decl
);
4523 if (TREE_CODE (decl
) == TYPE_DECL
)
4524 set_identifier_type_value (id
, TREE_TYPE (decl
));
4526 unuse_fields (type
);
4529 storetags (CLASSTYPE_TAGS (type
));
4533 /* When we exit a toplevel class scope, we save the
4534 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
4535 reenter the class. Here, we've entered some other class, so we
4536 must invalidate our cache. */
4539 invalidate_class_lookup_cache ()
4543 /* This code can be seen as a cache miss. When we've cached a
4544 class' scope's bindings and we can't use them, we need to reset
4545 them. This is it! */
4546 for (t
= previous_class_values
; t
; t
= TREE_CHAIN (t
))
4547 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t
)) = NULL_TREE
;
4549 previous_class_type
= NULL_TREE
;
4552 /* Get out of the current class scope. If we were in a class scope
4553 previously, that is the one popped to. */
4559 /* Since poplevel_class does the popping of class decls nowadays,
4560 this really only frees the obstack used for these decls. */
4563 current_class_depth
--;
4564 current_class_name
= current_class_stack
[current_class_depth
].name
;
4565 current_class_type
= current_class_stack
[current_class_depth
].type
;
4566 current_access_specifier
= current_class_stack
[current_class_depth
].access
;
4567 if (current_class_stack
[current_class_depth
].names_used
)
4568 splay_tree_delete (current_class_stack
[current_class_depth
].names_used
);
4571 /* Returns 1 if current_class_type is either T or a nested type of T. */
4574 currently_open_class (t
)
4578 if (t
== current_class_type
)
4580 for (i
= 0; i
< current_class_depth
; ++i
)
4581 if (current_class_stack
[i
].type
== t
)
4586 /* When entering a class scope, all enclosing class scopes' names with
4587 static meaning (static variables, static functions, types and enumerators)
4588 have to be visible. This recursive function calls pushclass for all
4589 enclosing class contexts until global or a local scope is reached.
4590 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
4591 formal of the same name. */
4594 push_nested_class (type
, modify
)
4600 /* A namespace might be passed in error cases, like A::B:C. */
4601 if (type
== NULL_TREE
|| type
== error_mark_node
|| ! IS_AGGR_TYPE (type
)
4602 || TREE_CODE (type
) == NAMESPACE_DECL
4603 || TREE_CODE (type
) == TEMPLATE_TYPE_PARM
4604 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
4607 context
= DECL_CONTEXT (TYPE_MAIN_DECL (type
));
4609 if (context
&& CLASS_TYPE_P (context
))
4610 push_nested_class (context
, 2);
4611 pushclass (type
, modify
);
4614 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
4619 tree context
= DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type
));
4622 if (context
&& CLASS_TYPE_P (context
))
4623 pop_nested_class ();
4626 /* Set global variables CURRENT_LANG_NAME to appropriate value
4627 so that behavior of name-mangling machinery is correct. */
4630 push_lang_context (name
)
4633 *current_lang_stack
++ = current_lang_name
;
4634 if (current_lang_stack
- &VARRAY_TREE (current_lang_base
, 0)
4635 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base
))
4637 size_t old_size
= VARRAY_SIZE (current_lang_base
);
4639 VARRAY_GROW (current_lang_base
, old_size
+ 10);
4640 current_lang_stack
= &VARRAY_TREE (current_lang_base
, old_size
);
4643 if (name
== lang_name_cplusplus
)
4645 strict_prototype
= strict_prototypes_lang_cplusplus
;
4646 current_lang_name
= name
;
4648 else if (name
== lang_name_java
)
4650 strict_prototype
= strict_prototypes_lang_cplusplus
;
4651 current_lang_name
= name
;
4652 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
4653 (See record_builtin_java_type in decl.c.) However, that causes
4654 incorrect debug entries if these types are actually used.
4655 So we re-enable debug output after extern "Java". */
4656 DECL_IGNORED_P (java_byte_type_node
) = 0;
4657 DECL_IGNORED_P (java_short_type_node
) = 0;
4658 DECL_IGNORED_P (java_int_type_node
) = 0;
4659 DECL_IGNORED_P (java_long_type_node
) = 0;
4660 DECL_IGNORED_P (java_float_type_node
) = 0;
4661 DECL_IGNORED_P (java_double_type_node
) = 0;
4662 DECL_IGNORED_P (java_char_type_node
) = 0;
4663 DECL_IGNORED_P (java_boolean_type_node
) = 0;
4665 else if (name
== lang_name_c
)
4667 strict_prototype
= strict_prototypes_lang_c
;
4668 current_lang_name
= name
;
4671 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name
));
4674 /* Get out of the current language scope. */
4679 /* Clear the current entry so that garbage collector won't hold on
4681 *current_lang_stack
= NULL_TREE
;
4682 current_lang_name
= *--current_lang_stack
;
4683 if (current_lang_name
== lang_name_cplusplus
4684 || current_lang_name
== lang_name_java
)
4685 strict_prototype
= strict_prototypes_lang_cplusplus
;
4686 else if (current_lang_name
== lang_name_c
)
4687 strict_prototype
= strict_prototypes_lang_c
;
4690 /* Type instantiation routines. */
4692 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
4693 matches the TARGET_TYPE. If there is no satisfactory match, return
4694 error_mark_node, and issue an error message if COMPLAIN is
4695 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
4696 was a template-id, and EXPLICIT_TARGS are the explicitly provided
4697 template arguments. */
4700 resolve_address_of_overloaded_function (target_type
,
4709 tree explicit_targs
;
4711 /* Here's what the standard says:
4715 If the name is a function template, template argument deduction
4716 is done, and if the argument deduction succeeds, the deduced
4717 arguments are used to generate a single template function, which
4718 is added to the set of overloaded functions considered.
4720 Non-member functions and static member functions match targets of
4721 type "pointer-to-function" or "reference-to-function." Nonstatic
4722 member functions match targets of type "pointer-to-member
4723 function;" the function type of the pointer to member is used to
4724 select the member function from the set of overloaded member
4725 functions. If a nonstatic member function is selected, the
4726 reference to the overloaded function name is required to have the
4727 form of a pointer to member as described in 5.3.1.
4729 If more than one function is selected, any template functions in
4730 the set are eliminated if the set also contains a non-template
4731 function, and any given template function is eliminated if the
4732 set contains a second template function that is more specialized
4733 than the first according to the partial ordering rules 14.5.5.2.
4734 After such eliminations, if any, there shall remain exactly one
4735 selected function. */
4738 int is_reference
= 0;
4739 /* We store the matches in a TREE_LIST rooted here. The functions
4740 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
4741 interoperability with most_specialized_instantiation. */
4742 tree matches
= NULL_TREE
;
4745 /* By the time we get here, we should be seeing only real
4746 pointer-to-member types, not the internal POINTER_TYPE to
4747 METHOD_TYPE representation. */
4748 my_friendly_assert (!(TREE_CODE (target_type
) == POINTER_TYPE
4749 && (TREE_CODE (TREE_TYPE (target_type
))
4750 == METHOD_TYPE
)), 0);
4752 /* Check that the TARGET_TYPE is reasonable. */
4753 if (TYPE_PTRFN_P (target_type
))
4756 else if (TYPE_PTRMEMFUNC_P (target_type
))
4757 /* This is OK, too. */
4759 else if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
4761 /* This is OK, too. This comes from a conversion to reference
4763 target_type
= build_reference_type (target_type
);
4769 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
4770 DECL_NAME (OVL_FUNCTION (overload
)), target_type
);
4771 return error_mark_node
;
4774 /* If we can find a non-template function that matches, we can just
4775 use it. There's no point in generating template instantiations
4776 if we're just going to throw them out anyhow. But, of course, we
4777 can only do this when we don't *need* a template function. */
4782 for (fns
= overload
; fns
; fns
= OVL_CHAIN (fns
))
4784 tree fn
= OVL_FUNCTION (fns
);
4787 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4788 /* We're not looking for templates just yet. */
4791 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
4793 /* We're looking for a non-static member, and this isn't
4794 one, or vice versa. */
4797 /* See if there's a match. */
4798 fntype
= TREE_TYPE (fn
);
4800 fntype
= build_ptrmemfunc_type (build_pointer_type (fntype
));
4801 else if (!is_reference
)
4802 fntype
= build_pointer_type (fntype
);
4804 if (can_convert_arg (target_type
, fntype
, fn
))
4805 matches
= tree_cons (fn
, NULL_TREE
, matches
);
4809 /* Now, if we've already got a match (or matches), there's no need
4810 to proceed to the template functions. But, if we don't have a
4811 match we need to look at them, too. */
4814 tree target_fn_type
;
4815 tree target_arg_types
;
4820 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type
));
4822 target_fn_type
= TREE_TYPE (target_type
);
4823 target_arg_types
= TYPE_ARG_TYPES (target_fn_type
);
4825 for (fns
= overload
; fns
; fns
= OVL_CHAIN (fns
))
4827 tree fn
= OVL_FUNCTION (fns
);
4829 tree instantiation_type
;
4832 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
4833 /* We're only looking for templates. */
4836 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
4838 /* We're not looking for a non-static member, and this is
4839 one, or vice versa. */
4842 /* Try to do argument deduction. */
4843 targs
= make_scratch_vec (DECL_NTPARMS (fn
));
4844 if (fn_type_unification (fn
, explicit_targs
, targs
,
4845 target_arg_types
, NULL_TREE
,
4847 /* Argument deduction failed. */
4850 /* Instantiate the template. */
4851 instantiation
= instantiate_template (fn
, targs
);
4852 if (instantiation
== error_mark_node
)
4853 /* Instantiation failed. */
4856 /* See if there's a match. */
4857 instantiation_type
= TREE_TYPE (instantiation
);
4859 instantiation_type
=
4860 build_ptrmemfunc_type (build_pointer_type (instantiation_type
));
4861 else if (!is_reference
)
4862 instantiation_type
= build_pointer_type (instantiation_type
);
4863 if (can_convert_arg (target_type
, instantiation_type
, instantiation
))
4864 matches
= tree_cons (instantiation
, fn
, matches
);
4867 /* Now, remove all but the most specialized of the matches. */
4870 tree match
= most_specialized_instantiation (matches
,
4873 if (match
!= error_mark_node
)
4874 matches
= tree_cons (match
, NULL_TREE
, NULL_TREE
);
4878 /* Now we should have exactly one function in MATCHES. */
4879 if (matches
== NULL_TREE
)
4881 /* There were *no* matches. */
4884 cp_error ("no matches converting function `%D' to type `%#T'",
4885 DECL_NAME (OVL_FUNCTION (overload
)),
4888 /* print_candidates expects a chain with the functions in
4889 TREE_VALUE slots, so we cons one up here (we're losing anyway,
4890 so why be clever?). */
4891 for (; overload
; overload
= OVL_NEXT (overload
))
4892 matches
= tree_cons (NULL_TREE
, OVL_CURRENT (overload
),
4895 print_candidates (matches
);
4897 return error_mark_node
;
4899 else if (TREE_CHAIN (matches
))
4901 /* There were too many matches. */
4907 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
4908 DECL_NAME (OVL_FUNCTION (overload
)),
4911 /* Since print_candidates expects the functions in the
4912 TREE_VALUE slot, we flip them here. */
4913 for (match
= matches
; match
; match
= TREE_CHAIN (match
))
4914 TREE_VALUE (match
) = TREE_PURPOSE (match
);
4916 print_candidates (matches
);
4919 return error_mark_node
;
4922 /* Good, exactly one match. Now, convert it to the correct type. */
4923 fn
= TREE_PURPOSE (matches
);
4927 if (TYPE_PTRFN_P (target_type
) || TYPE_PTRMEMFUNC_P (target_type
))
4928 return build_unary_op (ADDR_EXPR
, fn
, 0);
4931 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
4932 will mark the function as addressed, but here we must do it
4934 mark_addressable (fn
);
4940 /* This function will instantiate the type of the expression given in
4941 RHS to match the type of LHSTYPE. If errors exist, then return
4942 error_mark_node. We only complain is COMPLAIN is set. If we are
4943 not complaining, never modify rhs, as overload resolution wants to
4944 try many possible instantiations, in hopes that at least one will
4947 FLAGS is a bitmask, as we see at the top of the function.
4949 For non-recursive calls, LHSTYPE should be a function, pointer to
4950 function, or a pointer to member function. */
4953 instantiate_type (lhstype
, rhs
, flags
)
4957 int complain
= (flags
& 1);
4958 int strict
= (flags
& 2) ? COMPARE_NO_ATTRIBUTES
: COMPARE_STRICT
;
4960 if (TREE_CODE (lhstype
) == UNKNOWN_TYPE
)
4963 error ("not enough type information");
4964 return error_mark_node
;
4967 if (TREE_TYPE (rhs
) != NULL_TREE
&& ! (type_unknown_p (rhs
)))
4969 if (comptypes (lhstype
, TREE_TYPE (rhs
), strict
))
4972 cp_error ("argument of type `%T' does not match `%T'",
4973 TREE_TYPE (rhs
), lhstype
);
4974 return error_mark_node
;
4977 /* We don't overwrite rhs if it is an overloaded function.
4978 Copying it would destroy the tree link. */
4979 if (TREE_CODE (rhs
) != OVERLOAD
)
4980 rhs
= copy_node (rhs
);
4982 /* This should really only be used when attempting to distinguish
4983 what sort of a pointer to function we have. For now, any
4984 arithmetic operation which is not supported on pointers
4985 is rejected as an error. */
4987 switch (TREE_CODE (rhs
))
4994 my_friendly_abort (177);
4995 return error_mark_node
;
5002 new_rhs
= instantiate_type (build_pointer_type (lhstype
),
5003 TREE_OPERAND (rhs
, 0), flags
);
5004 if (new_rhs
== error_mark_node
)
5005 return error_mark_node
;
5007 TREE_TYPE (rhs
) = lhstype
;
5008 TREE_OPERAND (rhs
, 0) = new_rhs
;
5013 rhs
= copy_node (TREE_OPERAND (rhs
, 0));
5014 TREE_TYPE (rhs
) = unknown_type_node
;
5015 return instantiate_type (lhstype
, rhs
, flags
);
5019 tree r
= instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
5021 if (r
!= error_mark_node
&& TYPE_PTRMEMFUNC_P (lhstype
)
5022 && complain
&& !flag_ms_extensions
)
5024 /* Note: we check this after the recursive call to avoid
5025 complaining about cases where overload resolution fails. */
5027 tree t
= TREE_TYPE (TREE_OPERAND (rhs
, 0));
5028 tree fn
= PTRMEM_CST_MEMBER (r
);
5030 my_friendly_assert (TREE_CODE (r
) == PTRMEM_CST
, 990811);
5033 ("object-dependent reference to `%E' can only be used in a call",
5036 (" to form a pointer to member function, say `&%T::%E'",
5044 rhs
= TREE_OPERAND (rhs
, 1);
5045 if (BASELINK_P (rhs
))
5046 return instantiate_type (lhstype
, TREE_VALUE (rhs
), flags
);
5048 /* This can happen if we are forming a pointer-to-member for a
5050 my_friendly_assert (TREE_CODE (rhs
) == TEMPLATE_ID_EXPR
, 0);
5054 case TEMPLATE_ID_EXPR
:
5056 resolve_address_of_overloaded_function (lhstype
,
5057 TREE_OPERAND (rhs
, 0),
5059 /*template_only=*/1,
5060 TREE_OPERAND (rhs
, 1));
5064 resolve_address_of_overloaded_function (lhstype
,
5067 /*template_only=*/0,
5068 /*explicit_targs=*/NULL_TREE
);
5071 /* Now we should have a baselink. */
5072 my_friendly_assert (BASELINK_P (rhs
), 990412);
5074 return instantiate_type (lhstype
, TREE_VALUE (rhs
), flags
);
5077 /* This is too hard for now. */
5078 my_friendly_abort (183);
5079 return error_mark_node
;
5084 TREE_OPERAND (rhs
, 0)
5085 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), flags
);
5086 if (TREE_OPERAND (rhs
, 0) == error_mark_node
)
5087 return error_mark_node
;
5088 TREE_OPERAND (rhs
, 1)
5089 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
5090 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
5091 return error_mark_node
;
5093 TREE_TYPE (rhs
) = lhstype
;
5097 case TRUNC_DIV_EXPR
:
5098 case FLOOR_DIV_EXPR
:
5100 case ROUND_DIV_EXPR
:
5102 case TRUNC_MOD_EXPR
:
5103 case FLOOR_MOD_EXPR
:
5105 case ROUND_MOD_EXPR
:
5106 case FIX_ROUND_EXPR
:
5107 case FIX_FLOOR_EXPR
:
5109 case FIX_TRUNC_EXPR
:
5125 case PREINCREMENT_EXPR
:
5126 case PREDECREMENT_EXPR
:
5127 case POSTINCREMENT_EXPR
:
5128 case POSTDECREMENT_EXPR
:
5130 error ("invalid operation on uninstantiated type");
5131 return error_mark_node
;
5133 case TRUTH_AND_EXPR
:
5135 case TRUTH_XOR_EXPR
:
5142 case TRUTH_ANDIF_EXPR
:
5143 case TRUTH_ORIF_EXPR
:
5144 case TRUTH_NOT_EXPR
:
5146 error ("not enough type information");
5147 return error_mark_node
;
5150 if (type_unknown_p (TREE_OPERAND (rhs
, 0)))
5153 error ("not enough type information");
5154 return error_mark_node
;
5156 TREE_OPERAND (rhs
, 1)
5157 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
5158 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
5159 return error_mark_node
;
5160 TREE_OPERAND (rhs
, 2)
5161 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 2), flags
);
5162 if (TREE_OPERAND (rhs
, 2) == error_mark_node
)
5163 return error_mark_node
;
5165 TREE_TYPE (rhs
) = lhstype
;
5169 TREE_OPERAND (rhs
, 1)
5170 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
5171 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
5172 return error_mark_node
;
5174 TREE_TYPE (rhs
) = lhstype
;
5178 return instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), flags
);
5180 case ENTRY_VALUE_EXPR
:
5181 my_friendly_abort (184);
5182 return error_mark_node
;
5185 return error_mark_node
;
5188 my_friendly_abort (185);
5189 return error_mark_node
;
5193 /* Return the name of the virtual function pointer field
5194 (as an IDENTIFIER_NODE) for the given TYPE. Note that
5195 this may have to look back through base types to find the
5196 ultimate field name. (For single inheritance, these could
5197 all be the same name. Who knows for multiple inheritance). */
5200 get_vfield_name (type
)
5203 tree binfo
= TYPE_BINFO (type
);
5206 while (BINFO_BASETYPES (binfo
)
5207 && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo
, 0)))
5208 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo
, 0)))
5209 binfo
= BINFO_BASETYPE (binfo
, 0);
5211 type
= BINFO_TYPE (binfo
);
5212 buf
= (char *) alloca (sizeof (VFIELD_NAME_FORMAT
)
5213 + TYPE_NAME_LENGTH (type
) + 2);
5214 sprintf (buf
, VFIELD_NAME_FORMAT
, TYPE_NAME_STRING (type
));
5215 return get_identifier (buf
);
5219 print_class_statistics ()
5221 #ifdef GATHER_STATISTICS
5222 fprintf (stderr
, "convert_harshness = %d\n", n_convert_harshness
);
5223 fprintf (stderr
, "compute_conversion_costs = %d\n", n_compute_conversion_costs
);
5224 fprintf (stderr
, "build_method_call = %d (inner = %d)\n",
5225 n_build_method_call
, n_inner_fields_searched
);
5228 fprintf (stderr
, "vtables = %d; vtable searches = %d\n",
5229 n_vtables
, n_vtable_searches
);
5230 fprintf (stderr
, "vtable entries = %d; vtable elems = %d\n",
5231 n_vtable_entries
, n_vtable_elems
);
5236 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
5237 according to [class]:
5238 The class-name is also inserted
5239 into the scope of the class itself. For purposes of access checking,
5240 the inserted class name is treated as if it were a public member name. */
5243 build_self_reference ()
5245 tree name
= constructor_name (current_class_type
);
5246 tree value
= build_lang_decl (TYPE_DECL
, name
, current_class_type
);
5249 DECL_NONLOCAL (value
) = 1;
5250 DECL_CONTEXT (value
) = current_class_type
;
5251 DECL_CLASS_CONTEXT (value
) = current_class_type
;
5252 DECL_ARTIFICIAL (value
) = 1;
5254 if (processing_template_decl
)
5255 value
= push_template_decl (value
);
5257 saved_cas
= current_access_specifier
;
5258 current_access_specifier
= access_public_node
;
5259 finish_member_declaration (value
);
5260 current_access_specifier
= saved_cas
;
5263 /* Returns 1 if TYPE contains only padding bytes. */
5266 is_empty_class (type
)
5271 if (type
== error_mark_node
)
5274 if (! IS_AGGR_TYPE (type
))
5278 return CLASSTYPE_SIZE (type
) == integer_zero_node
;
5280 if (TYPE_BINFO_BASETYPES (type
))
5282 t
= TYPE_FIELDS (type
);
5283 while (t
&& TREE_CODE (t
) != FIELD_DECL
)
5285 return (t
== NULL_TREE
);
5288 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
5289 a *_TYPE node. NODE can also be a local class. */
5292 get_enclosing_class (type
)
5297 while (node
&& TREE_CODE (node
) != NAMESPACE_DECL
)
5299 switch (TREE_CODE_CLASS (TREE_CODE (node
)))
5302 node
= DECL_CONTEXT (node
);
5308 node
= TYPE_CONTEXT (node
);
5312 my_friendly_abort (0);
5318 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
5321 is_base_of_enclosing_class (base
, type
)
5326 if (get_binfo (base
, type
, 0))
5329 type
= get_enclosing_class (type
);
5334 /* Note that NAME was looked up while the current class was being
5335 defined and that the result of that lookup was DECL. */
5338 maybe_note_name_used_in_class (name
, decl
)
5342 splay_tree names_used
;
5344 /* If we're not defining a class, there's nothing to do. */
5345 if (!current_class_type
|| !TYPE_BEING_DEFINED (current_class_type
))
5348 /* If there's already a binding for this NAME, then we don't have
5349 anything to worry about. */
5350 if (IDENTIFIER_CLASS_VALUE (name
))
5353 if (!current_class_stack
[current_class_depth
- 1].names_used
)
5354 current_class_stack
[current_class_depth
- 1].names_used
5355 = splay_tree_new (splay_tree_compare_pointers
, 0, 0);
5356 names_used
= current_class_stack
[current_class_depth
- 1].names_used
;
5358 splay_tree_insert (names_used
,
5359 (splay_tree_key
) name
,
5360 (splay_tree_value
) decl
);
5363 /* Note that NAME was declared (as DECL) in the current class. Check
5364 to see that the declaration is legal. */
5367 note_name_declared_in_class (name
, decl
)
5371 splay_tree names_used
;
5374 /* Look to see if we ever used this name. */
5376 = current_class_stack
[current_class_depth
- 1].names_used
;
5380 n
= splay_tree_lookup (names_used
, (splay_tree_key
) name
);
5383 /* [basic.scope.class]
5385 A name N used in a class S shall refer to the same declaration
5386 in its context and when re-evaluated in the completed scope of
5388 cp_error ("declaration of `%#D'", decl
);
5389 cp_error_at ("changes meaning of `%s' from `%+#D'",
5390 IDENTIFIER_POINTER (DECL_NAME (decl
)),