1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 92-99, 2000 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. */
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;
75 static tree get_vfield_name PROTO((tree));
76 static void finish_struct_anon PROTO((tree));
77 static tree build_vbase_pointer PROTO((tree, tree));
78 static tree build_vtable_entry PROTO((tree, tree));
79 static tree get_vtable_name PROTO((tree));
80 static tree get_derived_offset PROTO((tree, tree));
81 static tree get_basefndecls PROTO((tree, tree));
82 static void set_rtti_entry PROTO((tree, tree, tree));
83 static void build_vtable PROTO((tree, tree));
84 static void prepare_fresh_vtable PROTO((tree, tree));
85 static void fixup_vtable_deltas1 PROTO((tree, tree));
86 static void fixup_vtable_deltas PROTO((tree, int, tree));
87 static tree dfs_finish_vtbls PROTO((tree, void *));
88 static void finish_vtbls PROTO((tree));
89 static void modify_vtable_entry PROTO((tree, tree, tree));
90 static tree get_vtable_entry_n PROTO((tree, unsigned HOST_WIDE_INT));
91 static void add_virtual_function PROTO((tree *, tree *, int *, tree, tree));
92 static tree delete_duplicate_fields_1 PROTO((tree, tree));
93 static void delete_duplicate_fields PROTO((tree));
94 static void finish_struct_bits PROTO((tree));
95 static int alter_access PROTO((tree, tree, tree, tree));
96 static void handle_using_decl PROTO((tree, tree));
97 static int overrides PROTO((tree, tree));
98 static int strictly_overrides PROTO((tree, tree));
99 static void merge_overrides PROTO((tree, tree, int, tree));
100 static void override_one_vtable PROTO((tree, tree, tree));
101 static void mark_overriders PROTO((tree, tree));
102 static void check_for_override PROTO((tree, tree));
103 static tree get_class_offset_1 PROTO((tree, tree, tree, tree, tree));
104 static tree get_class_offset PROTO((tree, tree, tree, tree));
105 static void modify_one_vtable PROTO((tree, tree, tree));
106 static tree dfs_modify_vtables_queue_p PROTO((tree, void *));
107 static tree dfs_modify_vtables PROTO((tree, void *));
108 static void modify_all_vtables PROTO((tree, tree));
109 static void determine_primary_base PROTO((tree, int *));
110 static void finish_struct_methods PROTO((tree));
111 static void maybe_warn_about_overly_private_class PROTO ((tree));
112 static int field_decl_cmp PROTO ((const tree *, const tree *));
113 static int method_name_cmp PROTO ((const tree *, const tree *));
114 static tree add_implicitly_declared_members PROTO((tree, int, int, int));
115 static tree fixed_type_or_null PROTO((tree, int *));
116 static tree resolve_address_of_overloaded_function PROTO((tree, tree, int,
118 static void build_vtable_entry_ref PROTO((tree, tree, tree));
119 static tree build_vtable_entry_for_fn PROTO((tree, tree));
120 static tree build_vtbl_initializer PROTO((tree));
121 static int count_fields PROTO((tree));
122 static int add_fields_to_vec PROTO((tree, tree, int));
123 static void check_bitfield_decl PROTO((tree));
124 static void check_field_decl PROTO((tree, tree, int *, int *, int *, int *));
125 static void check_field_decls PROTO((tree, tree *, int *, int *, int *,
127 static int avoid_overlap PROTO((tree, tree, int *));
128 static tree build_base_field PROTO((tree, tree, int *, int *, unsigned int *));
129 static tree build_base_fields PROTO((tree, int *));
130 static tree build_vbase_pointer_fields PROTO((tree, int *));
131 static tree build_vtbl_or_vbase_field PROTO((tree, tree, tree, tree, int *));
132 static void check_methods PROTO((tree));
133 static void remove_zero_width_bit_fields PROTO((tree));
134 static void check_bases PROTO((tree, int *, int *, int *));
135 static void check_bases_and_members PROTO((tree, int *));
136 static void create_vtable_ptr PROTO((tree, int *, int *, tree *, tree *));
137 static void layout_class_type PROTO((tree, int *, int *, tree *, tree *));
138 static void fixup_pending_inline PROTO((struct pending_inline *));
139 static void fixup_inline_methods PROTO((tree));
140 static void set_primary_base PROTO((tree, int, int *));
141 static tree dfs_propagate_binfo_offsets PROTO((tree, void *));
142 static void propagate_binfo_offsets PROTO((tree, tree));
143 static void layout_basetypes PROTO((tree));
144 static void layout_virtual_bases PROTO((tree));
145 static void remove_base_field PROTO((tree, tree, tree *));
146 static void remove_base_fields PROTO((tree));
147 static tree dfs_set_offset_for_shared_vbases PROTO((tree, void *));
148 static tree dfs_set_offset_for_unshared_vbases PROTO((tree, void *));
150 /* Variables shared between class.c and call.c. */
152 #ifdef GATHER_STATISTICS
154 int n_vtable_entries = 0;
155 int n_vtable_searches = 0;
156 int n_vtable_elems = 0;
157 int n_convert_harshness = 0;
158 int n_compute_conversion_costs = 0;
159 int n_build_method_call = 0;
160 int n_inner_fields_searched = 0;
163 /* Virtual baseclass things. */
166 build_vbase_pointer (exp, type)
170 FORMAT_VBASE_NAME (name, type);
172 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
176 /* Is the type of the EXPR, the complete type of the object?
177 If we are going to be wrong, we must be conservative, and return 0. */
180 complete_type_p (expr)
183 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
186 switch (TREE_CODE (expr))
193 expr = TREE_OPERAND (expr, 0);
197 if (! TREE_HAS_CONSTRUCTOR (expr))
199 /* fall through... */
202 if (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
203 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr)))
204 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
206 /* fall through... */
209 if (IS_AGGR_TYPE (TREE_TYPE (expr))
210 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
212 /* fall through... */
223 /* Build multi-level access to EXPR using hierarchy path PATH.
224 CODE is PLUS_EXPR if we are going with the grain,
225 and MINUS_EXPR if we are not (in which case, we cannot traverse
226 virtual baseclass links).
228 TYPE is the type we want this path to have on exit.
230 NONNULL is non-zero if we know (for any reason) that EXPR is
231 not, in fact, zero. */
234 build_vbase_path (code, type, expr, path, nonnull)
236 tree type, expr, path;
239 register int changed = 0;
240 tree last = NULL_TREE, last_virtual = NULL_TREE;
242 tree null_expr = 0, nonnull_expr;
244 tree offset = integer_zero_node;
246 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
247 return build1 (NOP_EXPR, type, expr);
249 /* We could do better if we had additional logic to convert back to the
250 unconverted type (the static type of the complete object), and then
251 convert back to the type we want. Until that is done, we only optimize
252 if the complete type is the same type as expr has. */
253 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
255 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
256 expr = save_expr (expr);
259 if (BINFO_INHERITANCE_CHAIN (path))
260 path = reverse_path (path);
262 basetype = BINFO_TYPE (path);
266 if (TREE_VIA_VIRTUAL (path))
268 last_virtual = BINFO_TYPE (path);
269 if (code == PLUS_EXPR)
271 changed = ! fixed_type_p;
277 /* We already check for ambiguous things in the caller, just
281 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
282 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
284 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
285 nonnull_expr = build_vbase_pointer (ind, last_virtual);
287 && TREE_CODE (type) == POINTER_TYPE
288 && null_expr == NULL_TREE)
290 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
291 expr = build (COND_EXPR, build_pointer_type (last_virtual),
292 build (EQ_EXPR, boolean_type_node, expr,
294 null_expr, nonnull_expr);
297 /* else we'll figure out the offset below. */
299 /* Happens in the case of parse errors. */
300 if (nonnull_expr == error_mark_node)
301 return error_mark_node;
305 cp_error ("cannot cast up from virtual baseclass `%T'",
307 return error_mark_node;
311 path = BINFO_INHERITANCE_CHAIN (path);
313 /* LAST is now the last basetype assoc on the path. */
315 /* A pointer to a virtual base member of a non-null object
316 is non-null. Therefore, we only need to test for zeroness once.
317 Make EXPR the canonical expression to deal with here. */
320 TREE_OPERAND (expr, 2) = nonnull_expr;
321 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
322 = TREE_TYPE (nonnull_expr);
327 /* If we go through any virtual base pointers, make sure that
328 casts to BASETYPE from the last virtual base class use
329 the right value for BASETYPE. */
332 tree intype = TREE_TYPE (TREE_TYPE (expr));
333 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
335 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
336 offset = BINFO_OFFSET (binfo);
340 offset = BINFO_OFFSET (last);
342 if (TREE_INT_CST_LOW (offset))
344 /* Bash types to make the backend happy. */
345 offset = cp_convert (type, offset);
347 /* This shouldn't be necessary. (mrs) */
348 expr = build1 (NOP_EXPR, type, expr);
351 /* If expr might be 0, we need to preserve that zeroness. */
355 TREE_TYPE (null_expr) = type;
357 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
358 if (TREE_SIDE_EFFECTS (expr))
359 expr = save_expr (expr);
361 return build (COND_EXPR, type,
362 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
364 build (code, type, expr, offset));
366 else return build (code, type, expr, offset);
369 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
370 be used multiple times in initialization of multiple inheritance. */
373 TREE_TYPE (expr) = type;
377 return build1 (NOP_EXPR, type, expr);
380 /* Virtual function things. */
382 /* Build an entry in the virtual function table.
383 DELTA is the offset for the `this' pointer.
384 PFN is an ADDR_EXPR containing a pointer to the virtual function.
385 Note that the index (DELTA2) in the virtual function table
389 build_vtable_entry (delta, pfn)
392 if (flag_vtable_thunks)
394 HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta);
395 if (idelta && ! DECL_PURE_VIRTUAL_P (TREE_OPERAND (pfn, 0)))
397 pfn = build1 (ADDR_EXPR, vtable_entry_type,
398 make_thunk (pfn, idelta));
399 TREE_READONLY (pfn) = 1;
400 TREE_CONSTANT (pfn) = 1;
402 #ifdef GATHER_STATISTICS
403 n_vtable_entries += 1;
409 extern int flag_huge_objects;
410 tree elems = tree_cons (NULL_TREE, delta,
411 tree_cons (NULL_TREE, integer_zero_node,
412 build_expr_list (NULL_TREE, pfn)));
413 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
415 /* DELTA used to be constructed by `size_int' and/or size_binop,
416 which caused overflow problems when it was negative. That should
419 if (! int_fits_type_p (delta, delta_type_node))
421 if (flag_huge_objects)
422 sorry ("object size exceeds built-in limit for virtual function table implementation");
424 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
427 TREE_CONSTANT (entry) = 1;
428 TREE_STATIC (entry) = 1;
429 TREE_READONLY (entry) = 1;
431 #ifdef GATHER_STATISTICS
432 n_vtable_entries += 1;
439 /* Build a vtable entry for FNDECL. DELTA is the amount by which we
440 must adjust the this pointer when calling F. */
443 build_vtable_entry_for_fn (delta, fndecl)
449 /* Take the address of the function, considering it to be of an
450 appropriate generic type. */
451 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
452 /* The address of a function can't change. */
453 TREE_CONSTANT (pfn) = 1;
454 /* Now build the vtable entry itself. */
455 return build_vtable_entry (delta, pfn);
458 /* We want to give the assembler the vtable identifier as well as
459 the offset to the function pointer. So we generate
461 __asm__ __volatile__ (".vtable_entry %c0, %c1"
462 : : "s"(&class_vtable),
463 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
466 build_vtable_entry_ref (basetype, vtbl, idx)
467 tree basetype, vtbl, idx;
469 static char asm_stmt[] = ".vtable_entry %c0, %c1";
472 s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
473 s = build_tree_list (build_string (1, "s"), s);
475 i = build_array_ref (vtbl, idx);
476 if (!flag_vtable_thunks)
477 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
478 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
479 i2 = build_array_ref (vtbl, build_int_2(0,0));
480 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
481 i = build_binary_op (MINUS_EXPR, i, i2);
482 i = build_tree_list (build_string (1, "i"), i);
484 finish_asm_stmt (ridpointers[RID_VOLATILE],
485 build_string (sizeof(asm_stmt)-1, asm_stmt),
486 NULL_TREE, chainon (s, i), NULL_TREE);
489 /* Given an object INSTANCE, return an expression which yields the
490 virtual function vtable element corresponding to INDEX. There are
491 many special cases for INSTANCE which we take care of here, mainly
492 to avoid creating extra tree nodes when we don't have to. */
495 build_vtbl_ref (instance, idx)
499 tree basetype = TREE_TYPE (instance);
501 if (TREE_CODE (basetype) == REFERENCE_TYPE)
502 basetype = TREE_TYPE (basetype);
504 if (instance == current_class_ref)
505 vtbl = build_vfield_ref (instance, basetype);
510 /* Try to figure out what a reference refers to, and
511 access its virtual function table directly. */
512 tree ref = NULL_TREE;
514 if (TREE_CODE (instance) == INDIRECT_REF
515 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
516 ref = TREE_OPERAND (instance, 0);
517 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
520 if (ref && TREE_CODE (ref) == VAR_DECL
521 && DECL_INITIAL (ref))
523 tree init = DECL_INITIAL (ref);
525 while (TREE_CODE (init) == NOP_EXPR
526 || TREE_CODE (init) == NON_LVALUE_EXPR)
527 init = TREE_OPERAND (init, 0);
528 if (TREE_CODE (init) == ADDR_EXPR)
530 init = TREE_OPERAND (init, 0);
531 if (IS_AGGR_TYPE (TREE_TYPE (init))
532 && (TREE_CODE (init) == PARM_DECL
533 || TREE_CODE (init) == VAR_DECL))
539 if (IS_AGGR_TYPE (TREE_TYPE (instance))
540 && (TREE_CODE (instance) == RESULT_DECL
541 || TREE_CODE (instance) == PARM_DECL
542 || TREE_CODE (instance) == VAR_DECL))
543 vtbl = TYPE_BINFO_VTABLE (basetype);
545 vtbl = build_vfield_ref (instance, basetype);
548 assemble_external (vtbl);
551 build_vtable_entry_ref (basetype, vtbl, idx);
553 aref = build_array_ref (vtbl, idx);
558 /* Given an object INSTANCE, return an expression which yields the
559 virtual function corresponding to INDEX. There are many special
560 cases for INSTANCE which we take care of here, mainly to avoid
561 creating extra tree nodes when we don't have to. */
564 build_vfn_ref (ptr_to_instptr, instance, idx)
565 tree *ptr_to_instptr, instance;
568 tree aref = build_vtbl_ref (instance, idx);
570 /* When using thunks, there is no extra delta, and we get the pfn
572 if (flag_vtable_thunks)
577 /* Save the intermediate result in a SAVE_EXPR so we don't have to
578 compute each component of the virtual function pointer twice. */
579 if (TREE_CODE (aref) == INDIRECT_REF)
580 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
583 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
585 cp_convert (ptrdiff_type_node,
586 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
589 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
592 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
593 for the given TYPE. */
596 get_vtable_name (type)
599 tree type_id = build_typename_overload (type);
600 char *buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
601 + IDENTIFIER_LENGTH (type_id) + 2);
602 const char *ptr = IDENTIFIER_POINTER (type_id);
604 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
606 /* We don't take off the numbers; prepare_fresh_vtable uses the
607 DECL_ASSEMBLER_NAME for the type, which includes the number
608 in `3foo'. If we were to pull them off here, we'd end up with
609 something like `_vt.foo.3bar', instead of a uniform definition. */
610 while (ptr[i] >= '0' && ptr[i] <= '9')
613 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, ptr+i);
614 return get_identifier (buf);
617 /* Return the offset to the main vtable for a given base BINFO. */
620 get_vfield_offset (binfo)
624 = size_binop (FLOOR_DIV_EXPR,
625 DECL_FIELD_BITPOS (TYPE_VFIELD (BINFO_TYPE (binfo))),
626 size_int (BITS_PER_UNIT));
627 tmp = convert (sizetype, tmp);
628 return size_binop (PLUS_EXPR, tmp, BINFO_OFFSET (binfo));
631 /* Get the offset to the start of the original binfo that we derived
632 this binfo from. If we find TYPE first, return the offset only
633 that far. The shortened search is useful because the this pointer
634 on method calling is expected to point to a DECL_CONTEXT (fndecl)
635 object, and not a baseclass of it. */
638 get_derived_offset (binfo, type)
641 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
644 while (BINFO_BASETYPES (binfo)
645 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
647 tree binfos = BINFO_BASETYPES (binfo);
648 if (BINFO_TYPE (binfo) == type)
650 binfo = TREE_VEC_ELT (binfos, i);
652 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
653 return size_binop (MINUS_EXPR, offset1, offset2);
656 /* Update the rtti info for this class. */
659 set_rtti_entry (virtuals, offset, type)
660 tree virtuals, offset, type;
664 if (CLASSTYPE_COM_INTERFACE (type))
668 fn = get_tinfo_fn_unused (type);
670 /* If someone tries to get RTTI information for a type compiled
671 without RTTI, they're out of luck. By calling __pure_virtual
672 in this case, we give a small clue as to what went wrong. We
673 could consider having a __no_typeinfo function as well, for a
674 more specific hint. */
677 if (flag_vtable_thunks)
679 /* The first slot holds the offset. */
680 TREE_PURPOSE (virtuals) = offset;
682 /* The next node holds the function. */
683 virtuals = TREE_CHAIN (virtuals);
684 offset = integer_zero_node;
687 /* This slot holds the function to call. */
688 TREE_PURPOSE (virtuals) = offset;
689 TREE_VALUE (virtuals) = fn;
692 /* Build a virtual function for type TYPE.
693 If BINFO is non-NULL, build the vtable starting with the initial
694 approximation that it is the same as the one which is the head of
695 the association list. */
698 build_vtable (binfo, type)
701 tree name = get_vtable_name (type);
708 if (BINFO_NEW_VTABLE_MARKED (binfo))
709 /* We have already created a vtable for this base, so there's
710 no need to do it again. */
713 virtuals = copy_list (BINFO_VIRTUALS (binfo));
714 decl = build_lang_decl (VAR_DECL, name,
715 TREE_TYPE (BINFO_VTABLE (binfo)));
717 /* Now do rtti stuff. */
718 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
719 offset = ssize_binop (MINUS_EXPR, integer_zero_node, offset);
720 set_rtti_entry (virtuals, offset, type);
724 virtuals = NULL_TREE;
725 decl = build_lang_decl (VAR_DECL, name, void_type_node);
728 #ifdef GATHER_STATISTICS
730 n_vtable_elems += list_length (virtuals);
733 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
734 import_export_vtable (decl, type, 0);
736 decl = pushdecl_top_level (decl);
737 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
738 /* Initialize the association list for this type, based
739 on our first approximation. */
740 TYPE_BINFO_VTABLE (type) = decl;
741 TYPE_BINFO_VIRTUALS (type) = virtuals;
743 DECL_ARTIFICIAL (decl) = 1;
744 TREE_STATIC (decl) = 1;
745 #ifndef WRITABLE_VTABLES
746 /* Make them READONLY by default. (mrs) */
747 TREE_READONLY (decl) = 1;
749 /* At one time the vtable info was grabbed 2 words at a time. This
750 fails on sparc unless you have 8-byte alignment. (tiemann) */
751 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
754 DECL_VIRTUAL_P (decl) = 1;
755 DECL_CONTEXT (decl) = type;
757 binfo = TYPE_BINFO (type);
758 SET_BINFO_NEW_VTABLE_MARKED (binfo);
761 /* Give TYPE a new virtual function table which is initialized
762 with a skeleton-copy of its original initialization. The only
763 entry that changes is the `delta' entry, so we can really
764 share a lot of structure.
766 FOR_TYPE is the derived type which caused this table to
769 BINFO is the type association which provided TYPE for FOR_TYPE.
771 The order in which vtables are built (by calling this function) for
772 an object must remain the same, otherwise a binary incompatibility
776 prepare_fresh_vtable (binfo, for_type)
777 tree binfo, for_type;
780 tree orig_decl = BINFO_VTABLE (binfo);
793 if (BINFO_NEW_VTABLE_MARKED (binfo))
794 /* We already created a vtable for this base. There's no need to
798 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
800 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
801 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
803 /* We know that the vtable that we are going to create doesn't exist
804 yet in the global namespace, and when we finish, it will be
805 pushed into the global namespace. In complex MI hierarchies, we
806 have to loop while the name we are thinking of adding is globally
807 defined, adding more name components to the vtable name as we
808 loop, until the name is unique. This is because in complex MI
809 cases, we might have the same base more than once. This means
810 that the order in which this function is called for vtables must
811 remain the same, otherwise binary compatibility can be
816 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
820 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
822 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX) + strlen (buf1) + 1);
823 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
824 name = get_identifier (buf);
826 /* If this name doesn't clash, then we can use it, otherwise
827 we add more to the name until it is unique. */
829 if (! IDENTIFIER_GLOBAL_VALUE (name))
832 /* Set values for next loop through, if the name isn't unique. */
834 path = BINFO_INHERITANCE_CHAIN (path);
836 /* We better not run out of stuff to make it unique. */
837 my_friendly_assert (path != NULL_TREE, 368);
839 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
841 if (for_type == basetype)
843 /* If we run out of basetypes in the path, we have already
844 found created a vtable with that name before, we now
845 resort to tacking on _%d to distinguish them. */
847 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
848 buf1 = (char *) alloca (i);
850 sprintf (buf1, "%s%c%s%c%d",
851 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
853 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
854 + strlen (buf1) + 1);
855 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
856 name = get_identifier (buf);
858 /* If this name doesn't clash, then we can use it,
859 otherwise we add something different to the name until
861 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
863 /* Hey, they really like MI don't they? Increase the 3
864 above to 6, and the 999 to 999999. :-) */
865 my_friendly_assert (j <= 999, 369);
870 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
871 new_buf2 = (char *) alloca (i);
872 sprintf (new_buf2, "%s%c%s",
873 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
877 new_decl = build_lang_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
878 /* Remember which class this vtable is really for. */
879 DECL_CONTEXT (new_decl) = for_type;
881 DECL_ARTIFICIAL (new_decl) = 1;
882 TREE_STATIC (new_decl) = 1;
883 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
884 DECL_VIRTUAL_P (new_decl) = 1;
885 #ifndef WRITABLE_VTABLES
886 /* Make them READONLY by default. (mrs) */
887 TREE_READONLY (new_decl) = 1;
889 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
891 /* Make fresh virtual list, so we can smash it later. */
892 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
894 if (TREE_VIA_VIRTUAL (binfo))
896 tree binfo1 = BINFO_FOR_VBASE (BINFO_TYPE (binfo), for_type);
898 /* XXX - This should never happen, if it does, the caller should
899 ensure that the binfo is from for_type's binfos, not from any
900 base type's. We can remove all this code after a while. */
902 warning ("internal inconsistency: binfo offset error for rtti");
904 offset = BINFO_OFFSET (binfo1);
907 offset = BINFO_OFFSET (binfo);
909 set_rtti_entry (BINFO_VIRTUALS (binfo),
910 ssize_binop (MINUS_EXPR, integer_zero_node, offset),
913 #ifdef GATHER_STATISTICS
915 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
918 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
919 import_export_vtable (new_decl, for_type, 0);
921 if (TREE_VIA_VIRTUAL (binfo))
922 my_friendly_assert (binfo == BINFO_FOR_VBASE (BINFO_TYPE (binfo),
925 SET_BINFO_NEW_VTABLE_MARKED (binfo);
928 /* Change the offset for the FNDECL entry to NEW_OFFSET. Also update
929 DECL_VINDEX (FNDECL). */
932 modify_vtable_entry (old_entry_in_list, new_offset, fndecl)
933 tree old_entry_in_list, new_offset, fndecl;
935 tree base_fndecl = TREE_VALUE (old_entry_in_list);
937 /* Update the entry. */
938 TREE_PURPOSE (old_entry_in_list) = new_offset;
939 TREE_VALUE (old_entry_in_list) = fndecl;
941 /* Now assign virtual dispatch information, if unset. We can
942 dispatch this, through any overridden base function. */
943 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
945 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
946 DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
950 /* Access the virtual function table entry N. VIRTUALS is the virtual
951 function table's initializer. */
954 get_vtable_entry_n (virtuals, n)
956 unsigned HOST_WIDE_INT n;
961 virtuals = TREE_CHAIN (virtuals);
966 /* Add a virtual function to all the appropriate vtables for the class
967 T. DECL_VINDEX(X) should be error_mark_node, if we want to
968 allocate a new slot in our table. If it is error_mark_node, we
969 know that no other function from another vtable is overridden by X.
970 HAS_VIRTUAL keeps track of how many virtuals there are in our main
971 vtable for the type, and we build upon the PENDING_VIRTUALS list
975 add_virtual_function (pv, phv, has_virtual, fndecl, t)
979 tree t; /* Structure type. */
981 tree pending_virtuals = *pv;
982 tree pending_hard_virtuals = *phv;
985 if (current_class_type == 0)
986 cp_warning ("internal problem, current_class_type is zero when adding `%D', please report",
988 if (current_class_type && t != current_class_type)
989 cp_warning ("internal problem, current_class_type differs when adding `%D', please report",
993 /* If the virtual function is a redefinition of a prior one,
994 figure out in which base class the new definition goes,
995 and if necessary, make a fresh virtual function table
996 to hold that entry. */
997 if (DECL_VINDEX (fndecl) == error_mark_node)
999 /* We remember that this was the base sub-object for rtti. */
1000 CLASSTYPE_RTTI (t) = t;
1002 /* If we are using thunks, use two slots at the front, one
1003 for the offset pointer, one for the tdesc pointer.
1004 For ARM-style vtables, use the same slot for both. */
1005 if (*has_virtual == 0 && ! CLASSTYPE_COM_INTERFACE (t))
1007 if (flag_vtable_thunks)
1013 /* Build a new INT_CST for this DECL_VINDEX. */
1015 static tree index_table[256];
1017 /* We skip a slot for the offset/tdesc entry. */
1018 int i = (*has_virtual)++;
1020 if (i >= 256 || index_table[i] == 0)
1022 idx = build_int_2 (i, 0);
1024 index_table[i] = idx;
1027 idx = index_table[i];
1029 /* Now assign virtual dispatch information. */
1030 DECL_VINDEX (fndecl) = idx;
1031 DECL_CONTEXT (fndecl) = t;
1033 /* Save the state we've computed on the PENDING_VIRTUALS list. */
1034 pending_virtuals = tree_cons (integer_zero_node,
1038 /* Might already be INTEGER_CST if declared twice in class. We will
1039 give error later or we've already given it. */
1040 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1042 /* Need an entry in some other virtual function table.
1043 Deal with this after we have laid out our virtual base classes. */
1044 pending_hard_virtuals = tree_cons (NULL_TREE,
1046 pending_hard_virtuals);
1048 *pv = pending_virtuals;
1049 *phv = pending_hard_virtuals;
1052 extern struct obstack *current_obstack;
1054 /* Add method METHOD to class TYPE.
1056 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1057 the class type where the method should be added. */
1060 add_method (type, fields, method)
1061 tree type, *fields, method;
1063 /* Setting the DECL_CONTEXT and DECL_CLASS_CONTEXT here is probably
1065 DECL_CONTEXT (method) = type;
1066 DECL_CLASS_CONTEXT (method) = type;
1068 if (fields && *fields)
1069 *fields = build_overload (method, *fields);
1076 if (!CLASSTYPE_METHOD_VEC (type))
1077 /* Make a new method vector. We start with 8 entries. We must
1078 allocate at least two (for constructors and destructors), and
1079 we're going to end up with an assignment operator at some
1082 We could use a TREE_LIST for now, and convert it to a
1083 TREE_VEC in finish_struct, but we would probably waste more
1084 memory making the links in the list than we would by
1085 over-allocating the size of the vector here. Furthermore,
1086 we would complicate all the code that expects this to be a
1088 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1090 method_vec = CLASSTYPE_METHOD_VEC (type);
1091 len = TREE_VEC_LENGTH (method_vec);
1093 if (DECL_NAME (method) == constructor_name (type))
1094 /* A new constructor or destructor. Constructors go in
1095 slot 0; destructors go in slot 1. */
1096 slot = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
1099 /* See if we already have an entry with this name. */
1100 for (slot = 2; slot < len; ++slot)
1101 if (!TREE_VEC_ELT (method_vec, slot)
1102 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1104 == DECL_NAME (method)))
1109 /* We need a bigger method vector. */
1110 tree new_vec = make_tree_vec (2 * len);
1111 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1112 (PTR) &TREE_VEC_ELT (new_vec, 0),
1113 len * sizeof (tree));
1115 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1118 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1120 /* Type conversion operators have to come before
1121 ordinary methods; add_conversions depends on this to
1122 speed up looking for conversion operators. So, if
1123 necessary, we slide some of the vector elements up.
1124 In theory, this makes this algorithm O(N^2) but we
1125 don't expect many conversion operators. */
1126 for (slot = 2; slot < len; ++slot)
1128 tree fn = TREE_VEC_ELT (method_vec, slot);
1131 /* There are no more entries in the vector, so we
1132 can insert the new conversion operator here. */
1135 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1136 /* We can insert the new function right at the
1141 if (!TREE_VEC_ELT (method_vec, slot))
1142 /* There is nothing in the Ith slot, so we can avoid
1147 /* We know the last slot in the vector is empty
1148 because we know that at this point there's room
1149 for a new function. */
1150 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1151 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1152 (len - slot - 1) * sizeof (tree));
1153 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1158 if (template_class_depth (type))
1159 /* TYPE is a template class. Don't issue any errors now; wait
1160 until instantiation time to complain. */
1166 /* Check to see if we've already got this method. */
1167 for (fns = TREE_VEC_ELT (method_vec, slot);
1169 fns = OVL_NEXT (fns))
1171 tree fn = OVL_CURRENT (fns);
1173 if (TREE_CODE (fn) != TREE_CODE (method))
1176 if (TREE_CODE (method) != TEMPLATE_DECL)
1178 /* [over.load] Member function declarations with the
1179 same name and the same parameter types cannot be
1180 overloaded if any of them is a static member
1181 function declaration. */
1182 if (DECL_STATIC_FUNCTION_P (fn)
1183 != DECL_STATIC_FUNCTION_P (method))
1185 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1186 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1188 if (! DECL_STATIC_FUNCTION_P (fn))
1189 parms1 = TREE_CHAIN (parms1);
1191 parms2 = TREE_CHAIN (parms2);
1193 if (compparms (parms1, parms2))
1194 cp_error ("`%#D' and `%#D' cannot be overloaded",
1198 /* Since this is an ordinary function in a
1199 non-template class, it's mangled name can be used
1200 as a unique identifier. This technique is only
1201 an optimization; we would get the same results if
1202 we just used decls_match here. */
1203 if (DECL_ASSEMBLER_NAME (fn)
1204 != DECL_ASSEMBLER_NAME (method))
1207 else if (!decls_match (fn, method))
1210 /* There has already been a declaration of this method
1211 or member template. */
1212 cp_error_at ("`%D' has already been declared in `%T'",
1215 /* We don't call duplicate_decls here to merge the
1216 declarations because that will confuse things if the
1217 methods have inline definitions. In particular, we
1218 will crash while processing the definitions. */
1223 /* Actually insert the new method. */
1224 TREE_VEC_ELT (method_vec, slot)
1225 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1227 /* Add the new binding. */
1228 if (!DECL_CONSTRUCTOR_P (method)
1229 && !DECL_DESTRUCTOR_P (method))
1230 push_class_level_binding (DECL_NAME (method),
1231 TREE_VEC_ELT (method_vec, slot));
1235 /* Subroutines of finish_struct. */
1237 /* Look through the list of fields for this struct, deleting
1238 duplicates as we go. This must be recursive to handle
1241 FIELD is the field which may not appear anywhere in FIELDS.
1242 FIELD_PTR, if non-null, is the starting point at which
1243 chained deletions may take place.
1244 The value returned is the first acceptable entry found
1247 Note that anonymous fields which are not of UNION_TYPE are
1248 not duplicates, they are just anonymous fields. This happens
1249 when we have unnamed bitfields, for example. */
1252 delete_duplicate_fields_1 (field, fields)
1257 if (DECL_NAME (field) == 0)
1259 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1262 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1263 fields = delete_duplicate_fields_1 (x, fields);
1268 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1270 if (DECL_NAME (x) == 0)
1272 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1274 TYPE_FIELDS (TREE_TYPE (x))
1275 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1276 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1279 fields = TREE_CHAIN (fields);
1281 TREE_CHAIN (prev) = TREE_CHAIN (x);
1284 else if (TREE_CODE (field) == USING_DECL)
1285 /* A using declaration may is allowed to appear more than
1286 once. We'll prune these from the field list later, and
1287 handle_using_decl will complain about invalid multiple
1290 else if (DECL_NAME (field) == DECL_NAME (x))
1292 if (TREE_CODE (field) == CONST_DECL
1293 && TREE_CODE (x) == CONST_DECL)
1294 cp_error_at ("duplicate enum value `%D'", x);
1295 else if (TREE_CODE (field) == CONST_DECL
1296 || TREE_CODE (x) == CONST_DECL)
1297 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1299 else if (DECL_DECLARES_TYPE_P (field)
1300 && DECL_DECLARES_TYPE_P (x))
1302 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1304 cp_error_at ("duplicate nested type `%D'", x);
1306 else if (DECL_DECLARES_TYPE_P (field)
1307 || DECL_DECLARES_TYPE_P (x))
1309 /* Hide tag decls. */
1310 if ((TREE_CODE (field) == TYPE_DECL
1311 && DECL_ARTIFICIAL (field))
1312 || (TREE_CODE (x) == TYPE_DECL
1313 && DECL_ARTIFICIAL (x)))
1315 cp_error_at ("duplicate field `%D' (as type and non-type)",
1319 cp_error_at ("duplicate member `%D'", x);
1321 fields = TREE_CHAIN (fields);
1323 TREE_CHAIN (prev) = TREE_CHAIN (x);
1331 delete_duplicate_fields (fields)
1335 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1336 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1339 /* Change the access of FDECL to ACCESS in T. The access to FDECL is
1340 along the path given by BINFO. Return 1 if change was legit,
1341 otherwise return 0. */
1344 alter_access (t, binfo, fdecl, access)
1350 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1353 if (TREE_VALUE (elem) != access)
1355 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1356 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1358 error ("conflicting access specifications for field `%s', ignored",
1359 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1363 /* They're changing the access to the same thing they changed
1364 it to before. That's OK. */
1370 enforce_access (binfo, fdecl);
1371 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1377 /* Process the USING_DECL, which is a member of T. */
1380 handle_using_decl (using_decl, t)
1384 tree ctype = DECL_INITIAL (using_decl);
1385 tree name = DECL_NAME (using_decl);
1387 = TREE_PRIVATE (using_decl) ? access_private_node
1388 : TREE_PROTECTED (using_decl) ? access_protected_node
1389 : access_public_node;
1391 tree flist = NULL_TREE;
1392 tree fields = TYPE_FIELDS (t);
1393 tree method_vec = CLASSTYPE_METHOD_VEC (t);
1398 binfo = binfo_or_else (ctype, t);
1402 if (name == constructor_name (ctype)
1403 || name == constructor_name_full (ctype))
1405 cp_error_at ("using-declaration for constructor", using_decl);
1409 fdecl = lookup_member (binfo, name, 0, 0);
1413 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1417 /* Functions are represented as TREE_LIST, with the purpose
1418 being the type and the value the functions. Other members
1419 come as themselves. */
1420 if (TREE_CODE (fdecl) == TREE_LIST)
1421 /* Ignore base type this came from. */
1422 fdecl = TREE_VALUE (fdecl);
1424 if (TREE_CODE (fdecl) == OVERLOAD)
1426 /* We later iterate over all functions. */
1428 fdecl = OVL_FUNCTION (flist);
1431 name = DECL_NAME (fdecl);
1432 n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
1433 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); i++)
1434 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)))
1437 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1438 cp_error_at (" because of local method `%#D' with same name",
1439 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
1443 if (! DECL_LANG_SPECIFIC (fdecl))
1444 /* We don't currently handle DECL_ACCESS for TYPE_DECLs; just return. */
1447 for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
1448 if (DECL_NAME (tmp) == name)
1450 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1451 cp_error_at (" because of local field `%#D' with same name", tmp);
1455 /* Make type T see field decl FDECL with access ACCESS.*/
1460 if (alter_access (t, binfo, OVL_FUNCTION (flist),
1463 flist = OVL_CHAIN (flist);
1467 alter_access (t, binfo, fdecl, access);
1470 /* Run through the base clases of T, updating
1471 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1472 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1476 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1479 int *cant_have_default_ctor_p;
1480 int *cant_have_const_ctor_p;
1481 int *no_const_asn_ref_p;
1485 int seen_nearly_empty_base_p;
1488 binfos = TYPE_BINFO_BASETYPES (t);
1489 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1490 seen_nearly_empty_base_p = 0;
1492 /* An aggregate cannot have baseclasses. */
1493 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1495 for (i = 0; i < n_baseclasses; ++i)
1500 /* Figure out what base we're looking at. */
1501 base_binfo = TREE_VEC_ELT (binfos, i);
1502 basetype = TREE_TYPE (base_binfo);
1504 /* If the type of basetype is incomplete, then we already
1505 complained about that fact (and we should have fixed it up as
1507 if (TYPE_SIZE (basetype) == 0)
1510 /* The base type is of incomplete type. It is
1511 probably best to pretend that it does not
1513 if (i == n_baseclasses-1)
1514 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1515 TREE_VEC_LENGTH (binfos) -= 1;
1517 for (j = i; j+1 < n_baseclasses; j++)
1518 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1522 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1523 here because the case of virtual functions but non-virtual
1524 dtor is handled in finish_struct_1. */
1525 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1526 && TYPE_HAS_DESTRUCTOR (basetype))
1527 cp_warning ("base class `%#T' has a non-virtual destructor",
1530 /* If the base class doesn't have copy constructors or
1531 assignment operators that take const references, then the
1532 derived class cannot have such a member automatically
1534 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1535 *cant_have_const_ctor_p = 1;
1536 if (TYPE_HAS_ASSIGN_REF (basetype)
1537 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1538 *no_const_asn_ref_p = 1;
1539 /* Similarly, if the base class doesn't have a default
1540 constructor, then the derived class won't have an
1541 automatically generated default constructor. */
1542 if (TYPE_HAS_CONSTRUCTOR (basetype)
1543 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1545 *cant_have_default_ctor_p = 1;
1546 if (! TYPE_HAS_CONSTRUCTOR (t))
1548 cp_pedwarn ("base `%T' with only non-default constructor",
1550 cp_pedwarn ("in class without a constructor");
1554 /* If the base class is not empty or nearly empty, then this
1555 class cannot be nearly empty. */
1556 if (!CLASSTYPE_NEARLY_EMPTY_P (basetype) && !is_empty_class (basetype))
1557 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1558 /* And if there is more than one nearly empty base, then the
1559 derived class is not nearly empty either. */
1560 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype)
1561 && seen_nearly_empty_base_p)
1562 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1563 /* If this is the first nearly empty base class, then remember
1565 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1566 seen_nearly_empty_base_p = 1;
1568 /* A lot of properties from the bases also apply to the derived
1570 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1571 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1572 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1573 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1574 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1575 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1576 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1577 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1578 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1580 /* Derived classes can implicitly become COMified if their bases
1582 if (CLASSTYPE_COM_INTERFACE (basetype))
1583 CLASSTYPE_COM_INTERFACE (t) = 1;
1584 else if (i == 0 && CLASSTYPE_COM_INTERFACE (t))
1587 ("COM interface type `%T' with non-COM leftmost base class `%T'",
1589 CLASSTYPE_COM_INTERFACE (t) = 0;
1594 /* Make the Ith baseclass of T its primary base. */
1597 set_primary_base (t, i, has_virtual_p)
1604 CLASSTYPE_VFIELD_PARENT (t) = i;
1605 basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
1606 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1607 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1608 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1609 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1610 *has_virtual_p = CLASSTYPE_VSIZE (basetype);
1613 /* Determine the primary class for T. */
1616 determine_primary_base (t, has_virtual_p)
1620 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1622 /* If there are no baseclasses, there is certainly no primary base. */
1623 if (n_baseclasses == 0)
1628 for (i = 0; i < n_baseclasses; i++)
1630 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
1631 tree basetype = BINFO_TYPE (base_binfo);
1633 if (TYPE_POLYMORPHIC_P (basetype))
1635 /* Even a virtual baseclass can contain our RTTI
1636 information. But, we prefer a non-virtual polymorphic
1638 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1639 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1641 /* A virtual baseclass can't be the primary base under the
1642 old ABI. And under the new ABI we still prefer a
1643 non-virtual base. */
1644 if (TREE_VIA_VIRTUAL (base_binfo))
1647 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1649 set_primary_base (t, i, has_virtual_p);
1650 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1656 /* Only add unique vfields, and flatten them out as we go. */
1657 for (vfields = CLASSTYPE_VFIELDS (basetype);
1659 vfields = TREE_CHAIN (vfields))
1660 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1661 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1662 CLASSTYPE_VFIELDS (t)
1663 = tree_cons (base_binfo,
1664 VF_BASETYPE_VALUE (vfields),
1665 CLASSTYPE_VFIELDS (t));
1667 if (*has_virtual_p == 0)
1668 set_primary_base (t, i, has_virtual_p);
1673 if (!TYPE_VFIELD (t))
1674 CLASSTYPE_VFIELD_PARENT (t) = -1;
1676 /* Now that we know what the primary base class is, we can run
1677 through the entire hierarchy marking the primary bases for future
1679 mark_primary_bases (t);
1682 /* Set memoizing fields and bits of T (and its variants) for later
1686 finish_struct_bits (t)
1689 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1691 /* Fix up variants (if any). */
1692 tree variants = TYPE_NEXT_VARIANT (t);
1695 /* These fields are in the _TYPE part of the node, not in
1696 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1697 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1698 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1699 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1700 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
1702 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1703 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1704 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1705 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1706 /* Copy whatever these are holding today. */
1707 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1708 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1709 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1710 TYPE_SIZE (variants) = TYPE_SIZE (t);
1711 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1712 variants = TYPE_NEXT_VARIANT (variants);
1715 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1716 /* For a class w/o baseclasses, `finish_struct' has set
1717 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1718 definition). Similarly for a class whose base classes do not
1719 have vtables. When neither of these is true, we might have
1720 removed abstract virtuals (by providing a definition), added
1721 some (by declaring new ones), or redeclared ones from a base
1722 class. We need to recalculate what's really an abstract virtual
1723 at this point (by looking in the vtables). */
1724 get_pure_virtuals (t);
1728 /* Notice whether this class has type conversion functions defined. */
1729 tree binfo = TYPE_BINFO (t);
1730 tree binfos = BINFO_BASETYPES (binfo);
1733 for (i = n_baseclasses-1; i >= 0; i--)
1735 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1737 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1741 /* If this type has a copy constructor, force its mode to be BLKmode, and
1742 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1743 be passed by invisible reference and prevent it from being returned in
1746 Also do this if the class has BLKmode but can still be returned in
1747 registers, since function_cannot_inline_p won't let us inline
1748 functions returning such a type. This affects the HP-PA. */
1749 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1750 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1751 && CLASSTYPE_NON_AGGREGATE (t)))
1754 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1755 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1757 TYPE_MODE (variants) = BLKmode;
1758 TREE_ADDRESSABLE (variants) = 1;
1763 /* Issue warnings about T having private constructors, but no friends,
1766 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1767 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1768 non-private static member functions. */
1771 maybe_warn_about_overly_private_class (t)
1774 int has_member_fn = 0;
1775 int has_nonprivate_method = 0;
1778 if (!warn_ctor_dtor_privacy
1779 /* If the class has friends, those entities might create and
1780 access instances, so we should not warn. */
1781 || (CLASSTYPE_FRIEND_CLASSES (t)
1782 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1783 /* We will have warned when the template was declared; there's
1784 no need to warn on every instantiation. */
1785 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1786 /* There's no reason to even consider warning about this
1790 /* We only issue one warning, if more than one applies, because
1791 otherwise, on code like:
1794 // Oops - forgot `public:'
1800 we warn several times about essentially the same problem. */
1802 /* Check to see if all (non-constructor, non-destructor) member
1803 functions are private. (Since there are no friends or
1804 non-private statics, we can't ever call any of the private member
1806 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1807 /* We're not interested in compiler-generated methods; they don't
1808 provide any way to call private members. */
1809 if (!DECL_ARTIFICIAL (fn))
1811 if (!TREE_PRIVATE (fn))
1813 if (DECL_STATIC_FUNCTION_P (fn))
1814 /* A non-private static member function is just like a
1815 friend; it can create and invoke private member
1816 functions, and be accessed without a class
1820 has_nonprivate_method = 1;
1823 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1827 if (!has_nonprivate_method && has_member_fn)
1829 /* There are no non-private methods, and there's at least one
1830 private member function that isn't a constructor or
1831 destructor. (If all the private members are
1832 constructors/destructors we want to use the code below that
1833 issues error messages specifically referring to
1834 constructors/destructors.) */
1836 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1837 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1838 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1839 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1841 has_nonprivate_method = 1;
1844 if (!has_nonprivate_method)
1846 cp_warning ("all member functions in class `%T' are private", t);
1851 /* Even if some of the member functions are non-private, the class
1852 won't be useful for much if all the constructors or destructors
1853 are private: such an object can never be created or destroyed. */
1854 if (TYPE_HAS_DESTRUCTOR (t))
1856 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
1858 if (TREE_PRIVATE (dtor))
1860 cp_warning ("`%#T' only defines a private destructor and has no friends",
1866 if (TYPE_HAS_CONSTRUCTOR (t))
1868 int nonprivate_ctor = 0;
1870 /* If a non-template class does not define a copy
1871 constructor, one is defined for it, enabling it to avoid
1872 this warning. For a template class, this does not
1873 happen, and so we would normally get a warning on:
1875 template <class T> class C { private: C(); };
1877 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1878 complete non-template or fully instantiated classes have this
1880 if (!TYPE_HAS_INIT_REF (t))
1881 nonprivate_ctor = 1;
1883 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
1887 tree ctor = OVL_CURRENT (fn);
1888 /* Ideally, we wouldn't count copy constructors (or, in
1889 fact, any constructor that takes an argument of the
1890 class type as a parameter) because such things cannot
1891 be used to construct an instance of the class unless
1892 you already have one. But, for now at least, we're
1894 if (! TREE_PRIVATE (ctor))
1896 nonprivate_ctor = 1;
1901 if (nonprivate_ctor == 0)
1903 cp_warning ("`%#T' only defines private constructors and has no friends",
1910 /* Function to help qsort sort FIELD_DECLs by name order. */
1913 field_decl_cmp (x, y)
1916 if (DECL_NAME (*x) == DECL_NAME (*y))
1917 /* A nontype is "greater" than a type. */
1918 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
1919 if (DECL_NAME (*x) == NULL_TREE)
1921 if (DECL_NAME (*y) == NULL_TREE)
1923 if (DECL_NAME (*x) < DECL_NAME (*y))
1928 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1931 method_name_cmp (m1, m2)
1932 const tree *m1, *m2;
1934 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1936 if (*m1 == NULL_TREE)
1938 if (*m2 == NULL_TREE)
1940 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1945 /* Warn about duplicate methods in fn_fields. Also compact method
1946 lists so that lookup can be made faster.
1948 Data Structure: List of method lists. The outer list is a
1949 TREE_LIST, whose TREE_PURPOSE field is the field name and the
1950 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
1951 links the entire list of methods for TYPE_METHODS. Friends are
1952 chained in the same way as member functions (? TREE_CHAIN or
1953 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
1954 list. That allows them to be quickly deleted, and requires no
1957 If there are any constructors/destructors, they are moved to the
1958 front of the list. This makes pushclass more efficient.
1960 @@ The above comment is obsolete. It mostly describes what add_method
1961 @@ and add_implicitly_declared_members do.
1963 Sort methods that are not special (i.e., constructors, destructors, and
1964 type conversion operators) so that we can find them faster in search. */
1967 finish_struct_methods (t)
1972 tree ctor_name = constructor_name (t);
1975 if (!TYPE_METHODS (t))
1977 /* Clear these for safety; perhaps some parsing error could set
1978 these incorrectly. */
1979 TYPE_HAS_CONSTRUCTOR (t) = 0;
1980 TYPE_HAS_DESTRUCTOR (t) = 0;
1981 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
1985 method_vec = CLASSTYPE_METHOD_VEC (t);
1986 my_friendly_assert (method_vec != NULL_TREE, 19991215);
1987 len = TREE_VEC_LENGTH (method_vec);
1989 /* First fill in entry 0 with the constructors, entry 1 with destructors,
1990 and the next few with type conversion operators (if any). */
1991 for (fn_fields = TYPE_METHODS (t); fn_fields;
1992 fn_fields = TREE_CHAIN (fn_fields))
1994 tree fn_name = DECL_NAME (fn_fields);
1996 /* Clear out this flag.
1998 @@ Doug may figure out how to break
1999 @@ this with nested classes and friends. */
2000 DECL_IN_AGGR_P (fn_fields) = 0;
2002 /* Note here that a copy ctor is private, so we don't dare generate
2003 a default copy constructor for a class that has a member
2004 of this type without making sure they have access to it. */
2005 if (fn_name == ctor_name)
2007 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2008 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2010 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2011 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2013 if (TREE_CHAIN (parmtypes) == NULL_TREE
2014 || TREE_CHAIN (parmtypes) == void_list_node
2015 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2017 if (TREE_PROTECTED (fn_fields))
2018 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2019 else if (TREE_PRIVATE (fn_fields))
2020 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2024 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2026 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2028 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2030 if (TREE_PROTECTED (fn_fields))
2031 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2032 else if (TREE_PRIVATE (fn_fields))
2033 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2038 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2039 /* We thought there was a destructor, but there wasn't. Some
2040 parse errors cause this anomalous situation. */
2041 TYPE_HAS_DESTRUCTOR (t) = 0;
2043 /* Issue warnings about private constructors and such. If there are
2044 no methods, then some public defaults are generated. */
2045 maybe_warn_about_overly_private_class (t);
2047 /* Now sort the methods. */
2048 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2050 TREE_VEC_LENGTH (method_vec) = len;
2052 /* The type conversion ops have to live at the front of the vec, so we
2054 for (slot = 2; slot < len; ++slot)
2056 tree fn = TREE_VEC_ELT (method_vec, slot);
2058 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2062 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2063 (int (*)(const void *, const void *))method_name_cmp);
2066 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2069 duplicate_tag_error (t)
2072 cp_error ("redefinition of `%#T'", t);
2073 cp_error_at ("previous definition here", t);
2075 /* Pretend we haven't defined this type. */
2077 /* All of the component_decl's were TREE_CHAINed together in the parser.
2078 finish_struct_methods walks these chains and assembles all methods with
2079 the same base name into DECL_CHAINs. Now we don't need the parser chains
2080 anymore, so we unravel them. */
2082 /* This used to be in finish_struct, but it turns out that the
2083 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2085 if (CLASSTYPE_METHOD_VEC (t))
2087 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2088 int i, len = TREE_VEC_LENGTH (method_vec);
2089 for (i = 0; i < len; i++)
2091 tree unchain = TREE_VEC_ELT (method_vec, i);
2092 while (unchain != NULL_TREE)
2094 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2095 unchain = OVL_NEXT (unchain);
2100 if (TYPE_LANG_SPECIFIC (t))
2102 tree binfo = TYPE_BINFO (t);
2103 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2104 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2105 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2106 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2108 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2109 BINFO_BASETYPES(binfo) = NULL_TREE;
2111 TYPE_BINFO (t) = binfo;
2112 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2113 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2114 TYPE_REDEFINED (t) = 1;
2115 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2116 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2118 TYPE_SIZE (t) = NULL_TREE;
2119 TYPE_MODE (t) = VOIDmode;
2120 TYPE_FIELDS (t) = NULL_TREE;
2121 TYPE_METHODS (t) = NULL_TREE;
2122 TYPE_VFIELD (t) = NULL_TREE;
2123 TYPE_CONTEXT (t) = NULL_TREE;
2124 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2127 /* Construct the initializer for BINFOs virtual function table. */
2130 build_vtbl_initializer (binfo)
2133 tree v = BINFO_VIRTUALS (binfo);
2134 tree inits = NULL_TREE;
2136 /* Process the RTTI stuff at the head of the list. If we're not
2137 using vtable thunks, then the RTTI entry is just an ordinary
2138 function, and we can process it just like the other virtual
2139 function entries. */
2140 if (!CLASSTYPE_COM_INTERFACE (BINFO_TYPE (binfo))
2141 && flag_vtable_thunks)
2146 /* The first entry is an offset. */
2147 offset = TREE_PURPOSE (v);
2148 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST,
2151 /* Convert the offset to look like a function pointer, so that
2152 we can put it in the vtable. */
2153 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
2154 TREE_CONSTANT (init) = 1;
2155 init = build_vtable_entry (integer_zero_node, init);
2156 inits = tree_cons (NULL_TREE, init, inits);
2158 /* Even in this case, the second entry (the tdesc pointer) is
2159 just an ordinary function. */
2163 /* Go through all the ordinary virtual functions, building up
2171 /* Pull the offset for `this', and the function to call, out of
2173 delta = TREE_PURPOSE (v);
2174 fn = TREE_VALUE (v);
2175 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
2176 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
2178 /* You can't call an abstract virtual function; it's abstract.
2179 So, we replace these functions with __pure_virtual. */
2180 if (DECL_PURE_VIRTUAL_P (fn))
2183 /* Package up that information for the vtable. */
2184 init = build_vtable_entry_for_fn (delta, fn);
2185 /* And add it to the chain of initializers. */
2186 inits = tree_cons (NULL_TREE, init, inits);
2192 /* The initializers were built up in reverse order; straighten them
2194 inits = nreverse (inits);
2195 /* Package all the initializers up as an array initializer. */
2196 return build_nt (CONSTRUCTOR, NULL_TREE, inits);
2199 /* Called from finish_vtbls via dfs_walk. */
2202 dfs_finish_vtbls (binfo, data)
2204 void *data ATTRIBUTE_UNUSED;
2206 if (!BINFO_PRIMARY_MARKED_P (binfo)
2207 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
2208 && BINFO_NEW_VTABLE_MARKED (binfo))
2213 decl = BINFO_VTABLE (binfo);
2214 context = DECL_CONTEXT (decl);
2215 DECL_CONTEXT (decl) = 0;
2216 DECL_INITIAL (decl) = build_vtbl_initializer (binfo);
2217 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
2218 DECL_CONTEXT (decl) = context;
2221 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2222 SET_BINFO_MARKED (binfo);
2227 /* Create all the necessary vtables for T and its base classes. */
2233 dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls,
2234 dfs_unmarked_real_bases_queue_p, t);
2235 dfs_walk (TYPE_BINFO (t), dfs_unmark,
2236 dfs_marked_real_bases_queue_p, t);
2239 /* True if we should override the given BASE_FNDECL with the given
2243 overrides (fndecl, base_fndecl)
2244 tree fndecl, base_fndecl;
2246 /* Destructors have special names. */
2247 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2248 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2250 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2251 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2253 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2255 tree types, base_types;
2257 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2258 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2260 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2261 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2262 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2263 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2264 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2271 get_class_offset_1 (parent, binfo, context, t, fndecl)
2272 tree parent, binfo, context, t, fndecl;
2274 tree binfos = BINFO_BASETYPES (binfo);
2275 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2276 tree rval = NULL_TREE;
2278 if (binfo == parent)
2279 return error_mark_node;
2281 for (i = 0; i < n_baselinks; i++)
2283 tree base_binfo = TREE_VEC_ELT (binfos, i);
2286 if (TREE_VIA_VIRTUAL (base_binfo))
2287 base_binfo = BINFO_FOR_VBASE (BINFO_TYPE (base_binfo), t);
2288 nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl);
2289 /* See if we have a new value */
2290 if (nrval && (nrval != error_mark_node || rval==0))
2292 /* Only compare if we have two offsets */
2293 if (rval && rval != error_mark_node
2294 && ! tree_int_cst_equal (nrval, rval))
2296 /* Only give error if the two offsets are different */
2297 error ("every virtual function must have a unique final overrider");
2298 cp_error (" found two (or more) `%T' class subobjects in `%T'", context, t);
2299 cp_error (" with virtual `%D' from virtual base class", fndecl);
2305 if (rval && BINFO_TYPE (binfo) == context)
2307 my_friendly_assert (rval == error_mark_node
2308 || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999);
2309 rval = BINFO_OFFSET (binfo);
2315 /* Get the offset to the CONTEXT subobject that is related to the
2319 get_class_offset (context, t, binfo, fndecl)
2320 tree context, t, binfo, fndecl;
2322 tree first_binfo = binfo;
2327 return integer_zero_node;
2329 if (BINFO_TYPE (binfo) == context)
2330 return BINFO_OFFSET (binfo);
2332 /* Check less derived binfos first. */
2333 while (BINFO_BASETYPES (binfo)
2334 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
2336 tree binfos = BINFO_BASETYPES (binfo);
2337 binfo = TREE_VEC_ELT (binfos, i);
2338 if (BINFO_TYPE (binfo) == context)
2339 return BINFO_OFFSET (binfo);
2342 /* Ok, not found in the less derived binfos, now check the more
2344 offset = get_class_offset_1 (first_binfo, TYPE_BINFO (t), context, t, fndecl);
2345 if (offset==0 || TREE_CODE (offset) != INTEGER_CST)
2346 my_friendly_abort (999); /* we have to find it. */
2350 /* Return the BINFO_VIRTUALS list for BINFO, without the RTTI stuff at
2351 the front. If non-NULL, N is set to the number of entries
2355 skip_rtti_stuff (binfo, t, n)
2358 unsigned HOST_WIDE_INT *n;
2362 if (CLASSTYPE_COM_INTERFACE (t))
2367 virtuals = BINFO_VIRTUALS (binfo);
2370 /* We always reserve a slot for the offset/tdesc entry. */
2373 virtuals = TREE_CHAIN (virtuals);
2375 if (flag_vtable_thunks && virtuals)
2377 /* The second slot is reserved for the tdesc pointer when thunks
2381 virtuals = TREE_CHAIN (virtuals);
2388 modify_one_vtable (binfo, t, fndecl)
2389 tree binfo, t, fndecl;
2392 unsigned HOST_WIDE_INT n;
2394 /* update rtti entry */
2397 if (binfo == TYPE_BINFO (t))
2398 build_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))), t);
2400 prepare_fresh_vtable (binfo, t);
2402 if (fndecl == NULL_TREE)
2405 virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), &n);
2409 tree current_fndecl = TREE_VALUE (virtuals);
2411 /* We should never have an instance of __pure_virtual on the
2412 BINFO_VIRTUALS list. If we do, then we will never notice
2413 that the function that should have been there instead has
2415 my_friendly_assert (current_fndecl != abort_fndecl,
2418 if (current_fndecl && overrides (fndecl, current_fndecl))
2420 tree base_offset, offset;
2421 tree context = DECL_CLASS_CONTEXT (fndecl);
2422 tree vfield = TYPE_VFIELD (t);
2425 offset = get_class_offset (context, t, binfo, fndecl);
2427 /* Find the right offset for the this pointer based on the
2428 base class we just found. We have to take into
2429 consideration the virtual base class pointers that we
2430 stick in before the virtual function table pointer.
2432 Also, we want just the delta between the most base class
2433 that we derived this vfield from and us. */
2434 base_offset = size_binop (PLUS_EXPR,
2435 get_derived_offset (binfo, DECL_CONTEXT (current_fndecl)),
2436 BINFO_OFFSET (binfo));
2437 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2439 if (binfo == TYPE_BINFO (t))
2440 /* In this case, it is *type*'s vtable we are modifying.
2441 We start with the approximation that it's vtable is
2442 that of the immediate base class. */
2443 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2445 /* This is our very own copy of `basetype' to play with.
2446 Later, we will fill in all the virtual functions that
2447 override the virtual functions in these base classes
2448 which are not defined by the current type. */
2449 prepare_fresh_vtable (binfo, t);
2452 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
2454 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2459 virtuals = TREE_CHAIN (virtuals);
2463 /* Called from modify_all_vtables via dfs_walk. */
2466 dfs_modify_vtables_queue_p (binfo, data)
2470 tree list = (tree) data;
2472 if (TREE_VIA_VIRTUAL (binfo))
2473 binfo = BINFO_FOR_VBASE (BINFO_TYPE (binfo), TREE_PURPOSE (list));
2475 return (TREE_ADDRESSABLE (list)
2476 ? markedp (binfo, NULL)
2477 : unmarkedp (binfo, NULL));
2480 /* Called from modify_all_vtables via dfs_walk. */
2483 dfs_modify_vtables (binfo, data)
2487 if (/* There's no need to modify the vtable for a primary base;
2488 we're not going to use that vtable anyhow. */
2489 !BINFO_PRIMARY_MARKED_P (binfo)
2490 /* Similarly, a base without a vtable needs no modification. */
2491 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2493 tree list = (tree) data;
2495 if (TREE_VIA_VIRTUAL (binfo))
2496 binfo = BINFO_FOR_VBASE (BINFO_TYPE (binfo), TREE_PURPOSE (list));
2497 modify_one_vtable (binfo, TREE_PURPOSE (list), TREE_VALUE (list));
2500 SET_BINFO_MARKED (binfo);
2506 modify_all_vtables (t, fndecl)
2512 list = build_tree_list (t, fndecl);
2513 dfs_walk (TYPE_BINFO (t), dfs_modify_vtables, dfs_modify_vtables_queue_p,
2515 /* Let dfs_modify_vtables_queue_p know to check that the mark is
2516 present before queueing a base, rather than checking to see that
2517 it is *not* present. */
2518 TREE_ADDRESSABLE (list) = 1;
2519 dfs_walk (TYPE_BINFO (t), dfs_unmark, dfs_modify_vtables_queue_p, list);
2522 /* Fixup all the delta entries in this one vtable that need updating. */
2525 fixup_vtable_deltas1 (binfo, t)
2529 unsigned HOST_WIDE_INT n;
2531 virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), &n);
2535 tree fndecl = TREE_VALUE (virtuals);
2536 tree delta = TREE_PURPOSE (virtuals);
2540 tree base_offset, offset;
2541 tree context = DECL_CLASS_CONTEXT (fndecl);
2542 tree vfield = TYPE_VFIELD (t);
2545 offset = get_class_offset (context, t, binfo, fndecl);
2547 /* Find the right offset for the this pointer based on the
2548 base class we just found. We have to take into
2549 consideration the virtual base class pointers that we
2550 stick in before the virtual function table pointer.
2552 Also, we want just the delta between the most base class
2553 that we derived this vfield from and us. */
2554 base_offset = size_binop (PLUS_EXPR,
2555 get_derived_offset (binfo,
2556 DECL_CONTEXT (fndecl)),
2557 BINFO_OFFSET (binfo));
2558 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2560 if (! tree_int_cst_equal (this_offset, delta))
2562 /* Make sure we can modify the derived association with immunity. */
2563 if (binfo == TYPE_BINFO (t))
2564 /* In this case, it is *type*'s vtable we are modifying.
2565 We start with the approximation that it's vtable is that
2566 of the immediate base class. */
2567 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2569 /* This is our very own copy of `basetype' to play
2570 with. Later, we will fill in all the virtual
2571 functions that override the virtual functions in
2572 these base classes which are not defined by the
2574 prepare_fresh_vtable (binfo, t);
2576 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2582 virtuals = TREE_CHAIN (virtuals);
2586 /* Fixup all the delta entries in all the direct vtables that need updating.
2587 This happens when we have non-overridden virtual functions from a
2588 virtual base class, that are at a different offset, in the new
2589 hierarchy, because the layout of the virtual bases has changed. */
2592 fixup_vtable_deltas (binfo, init_self, t)
2597 tree binfos = BINFO_BASETYPES (binfo);
2598 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2600 for (i = 0; i < n_baselinks; i++)
2602 tree base_binfo = TREE_VEC_ELT (binfos, i);
2603 int is_not_base_vtable
2604 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2605 if (! TREE_VIA_VIRTUAL (base_binfo))
2606 fixup_vtable_deltas (base_binfo, is_not_base_vtable, t);
2608 /* Should we use something besides CLASSTYPE_VFIELDS? */
2609 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2610 fixup_vtable_deltas1 (binfo, t);
2613 /* Here, we already know that they match in every respect.
2614 All we have to check is where they had their declarations. */
2617 strictly_overrides (fndecl1, fndecl2)
2618 tree fndecl1, fndecl2;
2620 int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
2621 DECL_CLASS_CONTEXT (fndecl1),
2623 if (distance == -2 || distance > 0)
2628 /* Merge overrides for one vtable.
2629 If we want to merge in same function, we are fine.
2631 if one has a DECL_CLASS_CONTEXT that is a parent of the
2632 other, than choose the more derived one
2634 potentially ill-formed (see 10.3 [class.virtual])
2635 we have to check later to see if there was an
2636 override in this class. If there was ok, if not
2637 then it is ill-formed. (mrs)
2639 We take special care to reuse a vtable, if we can. */
2642 override_one_vtable (binfo, old, t)
2647 enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
2649 /* If we have already committed to modifying it, then don't try and
2650 reuse another vtable. */
2651 if (BINFO_NEW_VTABLE_MARKED (binfo))
2654 virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), NULL);
2655 old_virtuals = skip_rtti_stuff (old, BINFO_TYPE (binfo), NULL);
2659 tree fndecl = TREE_VALUE (virtuals);
2660 tree old_fndecl = TREE_VALUE (old_virtuals);
2662 /* First check to see if they are the same. */
2663 if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
2665 /* No need to do anything. */
2667 else if (strictly_overrides (fndecl, old_fndecl))
2669 if (choose == UNDECIDED)
2671 else if (choose == REUSE_OLD)
2674 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2676 prepare_fresh_vtable (binfo, t);
2677 override_one_vtable (binfo, old, t);
2682 else if (strictly_overrides (old_fndecl, fndecl))
2684 if (choose == UNDECIDED)
2686 else if (choose == REUSE_NEW)
2689 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2691 prepare_fresh_vtable (binfo, t);
2692 override_one_vtable (binfo, old, t);
2695 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2697 else if (choose == NEITHER)
2699 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2705 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2707 prepare_fresh_vtable (binfo, t);
2708 override_one_vtable (binfo, old, t);
2712 /* This MUST be overridden, or the class is ill-formed. */
2713 tree fndecl = TREE_VALUE (virtuals);
2715 fndecl = copy_node (fndecl);
2716 copy_lang_decl (fndecl);
2717 DECL_NEEDS_FINAL_OVERRIDER_P (fndecl) = 1;
2718 /* Make sure we search for it later. */
2719 if (! CLASSTYPE_PURE_VIRTUALS (t))
2720 CLASSTYPE_PURE_VIRTUALS (t) = error_mark_node;
2722 /* We can use integer_zero_node, as we will core dump
2723 if this is used anyway. */
2724 TREE_PURPOSE (virtuals) = integer_zero_node;
2725 TREE_VALUE (virtuals) = fndecl;
2728 virtuals = TREE_CHAIN (virtuals);
2729 old_virtuals = TREE_CHAIN (old_virtuals);
2732 /* Let's reuse the old vtable. */
2733 if (choose == REUSE_OLD)
2735 BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
2736 BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
2740 /* Merge in overrides for virtual bases.
2741 BINFO is the hierarchy we want to modify, and OLD has the potential
2745 merge_overrides (binfo, old, do_self, t)
2750 tree binfos = BINFO_BASETYPES (binfo);
2751 tree old_binfos = BINFO_BASETYPES (old);
2752 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2754 /* Should we use something besides CLASSTYPE_VFIELDS? */
2755 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2757 override_one_vtable (binfo, old, t);
2760 for (i = 0; i < n_baselinks; i++)
2762 tree base_binfo = TREE_VEC_ELT (binfos, i);
2763 tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
2764 int is_not_base_vtable
2765 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2766 if (! TREE_VIA_VIRTUAL (base_binfo))
2767 merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
2771 /* Get the base virtual function declarations in T that are either
2772 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2773 the overrider/hider. */
2776 get_basefndecls (fndecl, t)
2779 tree methods = TYPE_METHODS (t);
2780 tree base_fndecls = NULL_TREE;
2781 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2782 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2786 if (TREE_CODE (methods) == FUNCTION_DECL
2787 && DECL_VINDEX (methods) != NULL_TREE
2788 && DECL_NAME (fndecl) == DECL_NAME (methods))
2789 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
2791 methods = TREE_CHAIN (methods);
2795 return base_fndecls;
2797 for (i = 0; i < n_baseclasses; i++)
2799 tree base_binfo = TREE_VEC_ELT (binfos, i);
2800 tree basetype = BINFO_TYPE (base_binfo);
2802 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2806 return base_fndecls;
2809 /* Mark the functions that have been hidden with their overriders.
2810 Since we start out with all functions already marked with a hider,
2811 no need to mark functions that are just hidden.
2813 Subroutine of warn_hidden. */
2816 mark_overriders (fndecl, base_fndecls)
2817 tree fndecl, base_fndecls;
2819 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2821 if (overrides (fndecl, TREE_VALUE (base_fndecls)))
2822 TREE_PURPOSE (base_fndecls) = fndecl;
2826 /* If this declaration supersedes the declaration of
2827 a method declared virtual in the base class, then
2828 mark this field as being virtual as well. */
2831 check_for_override (decl, ctype)
2834 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
2835 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2836 int virtualp = DECL_VIRTUAL_P (decl);
2837 int found_overriden_fn = 0;
2839 for (i = 0; i < n_baselinks; i++)
2841 tree base_binfo = TREE_VEC_ELT (binfos, i);
2842 if (TYPE_POLYMORPHIC_P (BINFO_TYPE (base_binfo)))
2844 tree tmp = get_matching_virtual
2846 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
2848 if (tmp && !found_overriden_fn)
2850 /* If this function overrides some virtual in some base
2851 class, then the function itself is also necessarily
2852 virtual, even if the user didn't explicitly say so. */
2853 DECL_VIRTUAL_P (decl) = 1;
2855 /* The TMP we really want is the one from the deepest
2856 baseclass on this path, taking care not to
2857 duplicate if we have already found it (via another
2858 path to its virtual baseclass. */
2859 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
2861 cp_error_at ("`static %#D' cannot be declared", decl);
2862 cp_error_at (" since `virtual %#D' declared in base class",
2869 = tree_cons (NULL_TREE, tmp, DECL_VINDEX (decl));
2871 /* We now know that DECL overrides something,
2872 which is all that is important. But, we must
2873 continue to iterate through all the base-classes
2874 in order to allow get_matching_virtual to check for
2875 various illegal overrides. */
2876 found_overriden_fn = 1;
2882 if (DECL_VINDEX (decl) == NULL_TREE)
2883 DECL_VINDEX (decl) = error_mark_node;
2884 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2888 /* Warn about hidden virtual functions that are not overridden in t.
2889 We know that constructors and destructors don't apply. */
2895 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2896 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2899 /* We go through each separately named virtual function. */
2900 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2902 tree fns = TREE_VEC_ELT (method_vec, i);
2903 tree fndecl = NULL_TREE;
2905 tree base_fndecls = NULL_TREE;
2906 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2907 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2909 /* First see if we have any virtual functions in this batch. */
2910 for (; fns; fns = OVL_NEXT (fns))
2912 fndecl = OVL_CURRENT (fns);
2913 if (DECL_VINDEX (fndecl))
2917 if (fns == NULL_TREE)
2920 /* First we get a list of all possible functions that might be
2921 hidden from each base class. */
2922 for (i = 0; i < n_baseclasses; i++)
2924 tree base_binfo = TREE_VEC_ELT (binfos, i);
2925 tree basetype = BINFO_TYPE (base_binfo);
2927 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2931 fns = OVL_NEXT (fns);
2933 /* ...then mark up all the base functions with overriders, preferring
2934 overriders to hiders. */
2936 for (; fns; fns = OVL_NEXT (fns))
2938 fndecl = OVL_CURRENT (fns);
2939 if (DECL_VINDEX (fndecl))
2940 mark_overriders (fndecl, base_fndecls);
2943 /* Now give a warning for all base functions without overriders,
2944 as they are hidden. */
2945 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2947 if (! overrides (TREE_PURPOSE (base_fndecls),
2948 TREE_VALUE (base_fndecls)))
2950 /* Here we know it is a hider, and no overrider exists. */
2951 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2952 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
2958 /* Check for things that are invalid. There are probably plenty of other
2959 things we should check for also. */
2962 finish_struct_anon (t)
2967 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2969 if (TREE_STATIC (field))
2971 if (TREE_CODE (field) != FIELD_DECL)
2974 if (DECL_NAME (field) == NULL_TREE
2975 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2977 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2978 for (; elt; elt = TREE_CHAIN (elt))
2980 if (DECL_ARTIFICIAL (elt))
2983 if (DECL_NAME (elt) == constructor_name (t))
2984 cp_pedwarn_at ("ANSI C++ forbids member `%D' with same name as enclosing class",
2987 if (TREE_CODE (elt) != FIELD_DECL)
2989 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2994 if (TREE_PRIVATE (elt))
2995 cp_pedwarn_at ("private member `%#D' in anonymous union",
2997 else if (TREE_PROTECTED (elt))
2998 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3001 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3002 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3008 extern int interface_only, interface_unknown;
3010 /* Create default constructors, assignment operators, and so forth for
3011 the type indicated by T, if they are needed.
3012 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3013 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3014 cannot have a default constructor, copy constructor taking a const
3015 reference argument, or an assignment operator, respectively. If a
3016 virtual destructor is created, its DECL is returned; otherwise the
3017 return value is NULL_TREE. */
3020 add_implicitly_declared_members (t, cant_have_default_ctor,
3021 cant_have_const_cctor,
3022 cant_have_assignment)
3024 int cant_have_default_ctor;
3025 int cant_have_const_cctor;
3026 int cant_have_assignment;
3029 tree implicit_fns = NULL_TREE;
3030 tree name = TYPE_IDENTIFIER (t);
3031 tree virtual_dtor = NULL_TREE;
3035 if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
3037 default_fn = cons_up_default_function (t, name, 0);
3038 check_for_override (default_fn, t);
3040 /* If we couldn't make it work, then pretend we didn't need it. */
3041 if (default_fn == void_type_node)
3042 TYPE_NEEDS_DESTRUCTOR (t) = 0;
3045 TREE_CHAIN (default_fn) = implicit_fns;
3046 implicit_fns = default_fn;
3048 if (DECL_VINDEX (default_fn))
3049 virtual_dtor = default_fn;
3052 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3054 /* Default constructor. */
3055 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3057 default_fn = cons_up_default_function (t, name, 2);
3058 TREE_CHAIN (default_fn) = implicit_fns;
3059 implicit_fns = default_fn;
3062 /* Copy constructor. */
3063 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3065 /* ARM 12.18: You get either X(X&) or X(const X&), but
3067 default_fn = cons_up_default_function (t, name,
3068 3 + cant_have_const_cctor);
3069 TREE_CHAIN (default_fn) = implicit_fns;
3070 implicit_fns = default_fn;
3073 /* Assignment operator. */
3074 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3076 default_fn = cons_up_default_function (t, name,
3077 5 + cant_have_assignment);
3078 TREE_CHAIN (default_fn) = implicit_fns;
3079 implicit_fns = default_fn;
3082 /* Now, hook all of the new functions on to TYPE_METHODS,
3083 and add them to the CLASSTYPE_METHOD_VEC. */
3084 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3085 add_method (t, 0, *f);
3086 *f = TYPE_METHODS (t);
3087 TYPE_METHODS (t) = implicit_fns;
3089 return virtual_dtor;
3092 /* Subroutine of finish_struct_1. Recursively count the number of fields
3093 in TYPE, including anonymous union members. */
3096 count_fields (fields)
3101 for (x = fields; x; x = TREE_CHAIN (x))
3103 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3104 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3111 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3112 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3115 add_fields_to_vec (fields, field_vec, idx)
3116 tree fields, field_vec;
3120 for (x = fields; x; x = TREE_CHAIN (x))
3122 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3123 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3125 TREE_VEC_ELT (field_vec, idx++) = x;
3130 /* FIELD is a bit-field. We are finishing the processing for its
3131 enclosing type. Issue any appropriate messages and set appropriate
3135 check_bitfield_decl (field)
3138 tree type = TREE_TYPE (field);
3140 /* Invalid bit-field size done by grokfield. */
3141 /* Detect invalid bit-field type. Simply checking if TYPE is
3142 integral is insufficient, as that is the array core of the field
3143 type. If TREE_TYPE (field) is integral, then TYPE must be the same. */
3144 if (DECL_INITIAL (field)
3145 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3147 cp_error_at ("bit-field `%#D' with non-integral type", field);
3148 DECL_INITIAL (field) = NULL;
3151 /* Detect and ignore out of range field width. */
3152 if (DECL_INITIAL (field))
3154 tree w = DECL_INITIAL (field);
3155 register int width = 0;
3157 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3160 /* detect invalid field size. */
3161 if (TREE_CODE (w) == CONST_DECL)
3162 w = DECL_INITIAL (w);
3163 else if (TREE_READONLY_DECL_P (w))
3164 w = decl_constant_value (w);
3166 if (TREE_CODE (w) != INTEGER_CST)
3168 cp_error_at ("bit-field `%D' width not an integer constant",
3170 DECL_INITIAL (field) = NULL_TREE;
3172 else if (width = TREE_INT_CST_LOW (w),
3175 DECL_INITIAL (field) = NULL;
3176 cp_error_at ("negative width in bit-field `%D'", field);
3178 else if (width == 0 && DECL_NAME (field) != 0)
3180 DECL_INITIAL (field) = NULL;
3181 cp_error_at ("zero width for bit-field `%D'", field);
3184 > TYPE_PRECISION (long_long_unsigned_type_node))
3186 /* The backend will dump if you try to use something too
3188 DECL_INITIAL (field) = NULL;
3189 sorry ("bit-fields larger than %d bits",
3190 TYPE_PRECISION (long_long_unsigned_type_node));
3191 cp_error_at (" in declaration of `%D'", field);
3193 else if (width > TYPE_PRECISION (type)
3194 && TREE_CODE (type) != ENUMERAL_TYPE
3195 && TREE_CODE (type) != BOOLEAN_TYPE)
3196 cp_warning_at ("width of `%D' exceeds its type", field);
3197 else if (TREE_CODE (type) == ENUMERAL_TYPE
3198 && ((min_precision (TYPE_MIN_VALUE (type),
3199 TREE_UNSIGNED (type)) > width)
3200 || (min_precision (TYPE_MAX_VALUE (type),
3201 TREE_UNSIGNED (type)) > width)))
3202 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3205 if (DECL_INITIAL (field))
3207 DECL_INITIAL (field) = NULL_TREE;
3208 DECL_FIELD_SIZE (field) = width;
3209 DECL_BIT_FIELD (field) = 1;
3213 #ifdef EMPTY_FIELD_BOUNDARY
3214 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3215 EMPTY_FIELD_BOUNDARY);
3217 #ifdef PCC_BITFIELD_TYPE_MATTERS
3218 if (PCC_BITFIELD_TYPE_MATTERS)
3219 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3226 /* Non-bit-fields are aligned for their type. */
3227 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3230 /* FIELD is a non bit-field. We are finishing the processing for its
3231 enclosing type T. Issue any appropriate messages and set appropriate
3235 check_field_decl (field, t, cant_have_const_ctor,
3236 cant_have_default_ctor, no_const_asn_ref,
3237 any_default_members)
3240 int *cant_have_const_ctor;
3241 int *cant_have_default_ctor;
3242 int *no_const_asn_ref;
3243 int *any_default_members;
3245 tree type = strip_array_types (TREE_TYPE (field));
3247 /* An anonymous union cannot contain any fields which would change
3248 the settings of CANT_HAVE_CONST_CTOR and friends. */
3249 if (ANON_UNION_TYPE_P (type))
3251 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3252 structs. So, we recurse through their fields here. */
3253 else if (ANON_AGGR_TYPE_P (type))
3257 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3258 if (TREE_CODE (field) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3259 check_field_decl (fields, t, cant_have_const_ctor,
3260 cant_have_default_ctor, no_const_asn_ref,
3261 any_default_members);
3263 /* Check members with class type for constructors, destructors,
3265 else if (CLASS_TYPE_P (type))
3267 /* Never let anything with uninheritable virtuals
3268 make it through without complaint. */
3269 abstract_virtuals_error (field, type);
3271 if (TREE_CODE (t) == UNION_TYPE)
3273 if (TYPE_NEEDS_CONSTRUCTING (type))
3274 cp_error_at ("member `%#D' with constructor not allowed in union",
3276 if (TYPE_NEEDS_DESTRUCTOR (type))
3277 cp_error_at ("member `%#D' with destructor not allowed in union",
3279 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3280 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3285 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3286 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3287 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3288 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3291 if (!TYPE_HAS_CONST_INIT_REF (type))
3292 *cant_have_const_ctor = 1;
3294 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3295 *no_const_asn_ref = 1;
3297 if (TYPE_HAS_CONSTRUCTOR (type)
3298 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3299 *cant_have_default_ctor = 1;
3301 if (DECL_INITIAL (field) != NULL_TREE)
3303 /* `build_class_init_list' does not recognize
3305 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3306 cp_error_at ("multiple fields in union `%T' initialized");
3307 *any_default_members = 1;
3310 /* Non-bit-fields are aligned for their type, except packed fields
3311 which require only BITS_PER_UNIT alignment. */
3312 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3313 (DECL_PACKED (field)
3315 : TYPE_ALIGN (TREE_TYPE (field))));
3318 /* Check the data members (both static and non-static), class-scoped
3319 typedefs, etc., appearing in the declaration of T. Issue
3320 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3321 declaration order) of access declarations; each TREE_VALUE in this
3322 list is a USING_DECL.
3324 In addition, set the following flags:
3327 The class is empty, i.e., contains no non-static data members.
3329 CANT_HAVE_DEFAULT_CTOR_P
3330 This class cannot have an implicitly generated default
3333 CANT_HAVE_CONST_CTOR_P
3334 This class cannot have an implicitly generated copy constructor
3335 taking a const reference.
3337 CANT_HAVE_CONST_ASN_REF
3338 This class cannot have an implicitly generated assignment
3339 operator taking a const reference.
3341 All of these flags should be initialized before calling this
3344 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3345 fields can be added by adding to this chain. */
3348 check_field_decls (t, access_decls, empty_p,
3349 cant_have_default_ctor_p, cant_have_const_ctor_p,
3354 int *cant_have_default_ctor_p;
3355 int *cant_have_const_ctor_p;
3356 int *no_const_asn_ref_p;
3361 int any_default_members;
3363 /* First, delete any duplicate fields. */
3364 delete_duplicate_fields (TYPE_FIELDS (t));
3366 /* Assume there are no access declarations. */
3367 *access_decls = NULL_TREE;
3368 /* Assume this class has no pointer members. */
3370 /* Assume none of the members of this class have default
3372 any_default_members = 0;
3374 for (field = &TYPE_FIELDS (t); *field; field = next)
3377 tree type = TREE_TYPE (x);
3379 GNU_xref_member (current_class_name, x);
3381 next = &TREE_CHAIN (x);
3383 if (TREE_CODE (x) == FIELD_DECL)
3385 DECL_PACKED (x) |= TYPE_PACKED (t);
3387 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3388 /* We don't treat zero-width bitfields as making a class
3393 /* The class is non-empty. */
3395 /* The class is not even nearly empty. */
3396 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3400 if (TREE_CODE (x) == USING_DECL)
3402 /* Prune the access declaration from the list of fields. */
3403 *field = TREE_CHAIN (x);
3405 /* Save the access declarations for our caller. */
3406 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3408 /* Since we've reset *FIELD there's no reason to skip to the
3414 if (TREE_CODE (x) == TYPE_DECL
3415 || TREE_CODE (x) == TEMPLATE_DECL)
3418 /* If we've gotten this far, it's a data member, possibly static,
3419 or an enumerator. */
3421 DECL_FIELD_CONTEXT (x) = t;
3423 /* ``A local class cannot have static data members.'' ARM 9.4 */
3424 if (current_function_decl && TREE_STATIC (x))
3425 cp_error_at ("field `%D' in local class cannot be static", x);
3427 /* Perform error checking that did not get done in
3429 if (TREE_CODE (type) == FUNCTION_TYPE)
3431 cp_error_at ("field `%D' invalidly declared function type",
3433 type = build_pointer_type (type);
3434 TREE_TYPE (x) = type;
3436 else if (TREE_CODE (type) == METHOD_TYPE)
3438 cp_error_at ("field `%D' invalidly declared method type", x);
3439 type = build_pointer_type (type);
3440 TREE_TYPE (x) = type;
3442 else if (TREE_CODE (type) == OFFSET_TYPE)
3444 cp_error_at ("field `%D' invalidly declared offset type", x);
3445 type = build_pointer_type (type);
3446 TREE_TYPE (x) = type;
3449 if (type == error_mark_node)
3452 DECL_SAVED_INSNS (x) = 0;
3453 DECL_FIELD_SIZE (x) = 0;
3455 /* When this goes into scope, it will be a non-local reference. */
3456 DECL_NONLOCAL (x) = 1;
3458 if (TREE_CODE (x) == CONST_DECL)
3461 if (TREE_CODE (x) == VAR_DECL)
3463 if (TREE_CODE (t) == UNION_TYPE)
3464 /* Unions cannot have static members. */
3465 cp_error_at ("field `%D' declared static in union", x);
3470 /* Now it can only be a FIELD_DECL. */
3472 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3473 CLASSTYPE_NON_AGGREGATE (t) = 1;
3475 /* If this is of reference type, check if it needs an init.
3476 Also do a little ANSI jig if necessary. */
3477 if (TREE_CODE (type) == REFERENCE_TYPE)
3479 CLASSTYPE_NON_POD_P (t) = 1;
3480 if (DECL_INITIAL (x) == NULL_TREE)
3481 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3483 /* ARM $12.6.2: [A member initializer list] (or, for an
3484 aggregate, initialization by a brace-enclosed list) is the
3485 only way to initialize nonstatic const and reference
3487 *cant_have_default_ctor_p = 1;
3488 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3490 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3493 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3495 cp_warning_at ("non-static reference in class without a constructor", x);
3499 type = strip_array_types (type);
3501 if (TREE_CODE (type) == POINTER_TYPE)
3504 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3505 CLASSTYPE_HAS_MUTABLE (t) = 1;
3507 if (! pod_type_p (type)
3508 /* For some reason, pointers to members are POD types themselves,
3509 but are not allowed in POD structs. Silly. */
3510 || TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
3511 CLASSTYPE_NON_POD_P (t) = 1;
3513 /* If any field is const, the structure type is pseudo-const. */
3514 if (CP_TYPE_CONST_P (type))
3516 C_TYPE_FIELDS_READONLY (t) = 1;
3517 if (DECL_INITIAL (x) == NULL_TREE)
3518 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3520 /* ARM $12.6.2: [A member initializer list] (or, for an
3521 aggregate, initialization by a brace-enclosed list) is the
3522 only way to initialize nonstatic const and reference
3524 *cant_have_default_ctor_p = 1;
3525 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3527 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3530 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3532 cp_warning_at ("non-static const member in class without a constructor", x);
3535 /* A field that is pseudo-const makes the structure likewise. */
3536 else if (IS_AGGR_TYPE (type))
3538 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3539 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3540 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
3543 /* We set DECL_C_BIT_FIELD in grokbitfield.
3544 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3545 if (DECL_C_BIT_FIELD (x))
3546 check_bitfield_decl (x);
3548 check_field_decl (x, t,
3549 cant_have_const_ctor_p,
3550 cant_have_default_ctor_p,
3552 &any_default_members);
3555 /* Effective C++ rule 11. */
3556 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3557 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3559 cp_warning ("`%#T' has pointer data members", t);
3561 if (! TYPE_HAS_INIT_REF (t))
3563 cp_warning (" but does not override `%T(const %T&)'", t, t);
3564 if (! TYPE_HAS_ASSIGN_REF (t))
3565 cp_warning (" or `operator=(const %T&)'", t);
3567 else if (! TYPE_HAS_ASSIGN_REF (t))
3568 cp_warning (" but does not override `operator=(const %T&)'", t);
3572 /* Check anonymous struct/anonymous union fields. */
3573 finish_struct_anon (t);
3575 /* We've built up the list of access declarations in reverse order.
3577 *access_decls = nreverse (*access_decls);
3580 /* Return a FIELD_DECL for a pointer-to-virtual-table or
3581 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
3582 field are as indicated. The CLASS_TYPE in which this field occurs
3583 is also indicated. *EMPTY_P is set to a non-zero value by this
3584 function to indicate that a class containing this field is
3588 build_vtbl_or_vbase_field (name, assembler_name, type, class_type,
3591 tree assembler_name;
3598 /* This class is non-empty. */
3601 /* Build the FIELD_DECL. */
3602 field = build_lang_decl (FIELD_DECL, name, type);
3603 DECL_ASSEMBLER_NAME (field) = assembler_name;
3604 DECL_VIRTUAL_P (field) = 1;
3605 DECL_ARTIFICIAL (field) = 1;
3606 DECL_FIELD_CONTEXT (field) = class_type;
3607 DECL_CLASS_CONTEXT (field) = class_type;
3608 DECL_FCONTEXT (field) = class_type;
3609 DECL_SAVED_INSNS (field) = 0;
3610 DECL_FIELD_SIZE (field) = 0;
3611 DECL_ALIGN (field) = TYPE_ALIGN (type);
3617 /* Returns list of virtual base class pointers in a FIELD_DECL chain. */
3620 build_vbase_pointer_fields (rec, empty_p)
3624 /* Chain to hold all the new FIELD_DECLs which point at virtual
3626 tree vbase_decls = NULL_TREE;
3627 tree binfos = TYPE_BINFO_BASETYPES (rec);
3628 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
3632 /* Handle basetypes almost like fields, but record their
3633 offsets differently. */
3635 for (i = 0; i < n_baseclasses; i++)
3637 register tree base_binfo = TREE_VEC_ELT (binfos, i);
3638 register tree basetype = BINFO_TYPE (base_binfo);
3640 if (TYPE_SIZE (basetype) == 0)
3641 /* This error is now reported in xref_tag, thus giving better
3642 location information. */
3645 /* All basetypes are recorded in the association list of the
3648 if (TREE_VIA_VIRTUAL (base_binfo))
3653 /* The offset for a virtual base class is only used in computing
3654 virtual function tables and for initializing virtual base
3655 pointers. It is built once `get_vbase_types' is called. */
3657 /* If this basetype can come from another vbase pointer
3658 without an additional indirection, we will share
3659 that pointer. If an indirection is involved, we
3660 make our own pointer. */
3661 for (j = 0; j < n_baseclasses; j++)
3663 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
3664 if (! TREE_VIA_VIRTUAL (other_base_binfo)
3665 && BINFO_FOR_VBASE (basetype, BINFO_TYPE (other_base_binfo)))
3668 FORMAT_VBASE_NAME (name, basetype);
3669 decl = build_vtbl_or_vbase_field (get_identifier (name),
3670 get_identifier (VTABLE_BASE),
3671 build_pointer_type (basetype),
3674 BINFO_VPTR_FIELD (base_binfo) = decl;
3675 TREE_CHAIN (decl) = vbase_decls;
3680 /* The space this decl occupies has already been accounted for. */
3688 /* If the empty base field in DECL overlaps with a base of the same type in
3689 NEWDECL, which is either another base field or the first data field of
3690 the class, pad the base just before NEWDECL and return 1. Otherwise,
3694 avoid_overlap (decl, newdecl, empty_p)
3700 if (newdecl == NULL_TREE
3701 || ! types_overlap_p (TREE_TYPE (decl), TREE_TYPE (newdecl)))
3704 for (field = decl; TREE_CHAIN (field) && TREE_CHAIN (field) != newdecl;
3705 field = TREE_CHAIN (field))
3708 DECL_SIZE (field) = integer_one_node;
3709 /* The containing class cannot be empty; this field takes up space. */
3715 /* Build a FIELD_DECL for the base given by BINFO in T. If the new
3716 object is non-empty, clear *EMPTY_P. Otherwise, set *SAW_EMPTY_P.
3717 *BASE_ALIGN is a running maximum of the alignments of any base
3721 build_base_field (t, binfo, empty_p, saw_empty_p, base_align)
3726 unsigned int *base_align;
3728 tree basetype = BINFO_TYPE (binfo);
3731 if (TYPE_SIZE (basetype) == 0)
3732 /* This error is now reported in xref_tag, thus giving better
3733 location information. */
3736 decl = build_lang_decl (FIELD_DECL, NULL_TREE, basetype);
3737 DECL_ARTIFICIAL (decl) = 1;
3738 DECL_FIELD_CONTEXT (decl) = DECL_CLASS_CONTEXT (decl) = t;
3739 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3740 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3742 if (flag_new_abi && DECL_SIZE (decl) == integer_zero_node)
3748 /* The containing class is non-empty because it has a non-empty base
3754 /* Brain damage for backwards compatibility. For no good
3755 reason, the old layout_basetypes made every base at least
3756 as large as the alignment for the bases up to that point,
3757 gratuitously wasting space. So we do the same thing
3759 *base_align = MAX (*base_align, DECL_ALIGN (decl));
3761 = size_int (MAX (TREE_INT_CST_LOW (DECL_SIZE (decl)),
3762 (int) (*base_align)));
3768 /* Returns a list of fields to stand in for the base class subobjects
3769 of REC. These fields are later removed by layout_basetypes. */
3772 build_base_fields (rec, empty_p)
3776 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3778 tree base_decls = NULL_TREE;
3779 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
3780 tree decl, nextdecl;
3781 int i, saw_empty = 0;
3782 unsigned int base_align = 0;
3784 /* Under the new ABI, the primary base class is always allocated
3786 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
3790 primary_base = CLASSTYPE_PRIMARY_BINFO (rec);
3791 base_decls = chainon (build_base_field (rec,
3799 /* Now allocate the rest of the bases. */
3800 for (i = 0; i < n_baseclasses; ++i)
3804 /* Under the new ABI, the primary base was already allocated
3805 above, so we don't need to allocate it again here. */
3806 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (rec))
3809 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
3811 /* A primary virtual base class is allocated just like any other
3812 base class, but a non-primary virtual base is allocated
3813 later, in layout_basetypes. */
3814 if (TREE_VIA_VIRTUAL (base_binfo)
3815 && !BINFO_PRIMARY_MARKED_P (base_binfo))
3818 base_decls = chainon (build_base_field (rec, base_binfo,
3825 /* Reverse the list of fields so we allocate the bases in the proper
3827 base_decls = nreverse (base_decls);
3829 /* In the presence of empty base classes, we run the risk of allocating
3830 two objects of the same class on top of one another. Avoid that. */
3831 if (flag_new_abi && saw_empty)
3832 for (decl = base_decls; decl; decl = TREE_CHAIN (decl))
3834 if (DECL_SIZE (decl) == integer_zero_node)
3836 /* First step through the following bases until we find
3837 an overlap or a non-empty base. */
3838 for (nextdecl = TREE_CHAIN (decl); nextdecl;
3839 nextdecl = TREE_CHAIN (nextdecl))
3841 if (avoid_overlap (decl, nextdecl, empty_p)
3842 || DECL_SIZE (nextdecl) != integer_zero_node)
3846 /* If we're still looking, also check against the first
3848 for (nextdecl = TYPE_FIELDS (rec);
3849 nextdecl && TREE_CODE (nextdecl) != FIELD_DECL;
3850 nextdecl = TREE_CHAIN (nextdecl))
3852 avoid_overlap (decl, nextdecl, empty_p);
3860 /* Go through the TYPE_METHODS of T issuing any appropriate
3861 diagnostics, figuring out which methods override which other
3862 methods, and so forth. */
3870 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3872 GNU_xref_member (current_class_name, x);
3874 /* If this was an evil function, don't keep it in class. */
3875 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3878 /* Do both of these, even though they're in the same union;
3879 if the insn `r' member and the size `i' member are
3880 different sizes, as on the alpha, the larger of the two
3881 will end up with garbage in it. */
3882 DECL_SAVED_INSNS (x) = 0;
3883 DECL_FIELD_SIZE (x) = 0;
3885 check_for_override (x, t);
3886 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3887 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3889 /* The name of the field is the original field name
3890 Save this in auxiliary field for later overloading. */
3891 if (DECL_VINDEX (x))
3893 TYPE_POLYMORPHIC_P (t) = 1;
3894 if (DECL_PURE_VIRTUAL_P (x))
3895 CLASSTYPE_PURE_VIRTUALS (t)
3896 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
3901 /* Remove all zero-width bit-fields from T. */
3904 remove_zero_width_bit_fields (t)
3909 fieldsp = &TYPE_FIELDS (t);
3912 if (TREE_CODE (*fieldsp) == FIELD_DECL
3913 && DECL_C_BIT_FIELD (*fieldsp)
3914 && DECL_INITIAL (*fieldsp))
3915 *fieldsp = TREE_CHAIN (*fieldsp);
3917 fieldsp = &TREE_CHAIN (*fieldsp);
3921 /* Check the validity of the bases and members declared in T. Add any
3922 implicitly-generated functions (like copy-constructors and
3923 assignment operators). Compute various flag bits (like
3924 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
3925 level: i.e., independently of the ABI in use. */
3928 check_bases_and_members (t, empty_p)
3932 /* Nonzero if we are not allowed to generate a default constructor
3934 int cant_have_default_ctor;
3935 /* Nonzero if the implicitly generated copy constructor should take
3936 a non-const reference argument. */
3937 int cant_have_const_ctor;
3938 /* Nonzero if the the implicitly generated assignment operator
3939 should take a non-const reference argument. */
3940 int no_const_asn_ref;
3943 /* By default, we use const reference arguments and generate default
3945 cant_have_default_ctor = 0;
3946 cant_have_const_ctor = 0;
3947 no_const_asn_ref = 0;
3949 /* Assume that the class is nearly empty; we'll clear this flag if
3950 it turns out not to be nearly empty. */
3951 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
3953 /* Check all the base-classes. */
3954 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
3957 /* Check all the data member declarations. */
3958 check_field_decls (t, &access_decls, empty_p,
3959 &cant_have_default_ctor,
3960 &cant_have_const_ctor,
3963 /* Check all the method declarations. */
3966 /* A nearly-empty class has to be polymorphic; a nearly empty class
3968 if (!TYPE_POLYMORPHIC_P (t))
3969 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3971 /* Do some bookkeeping that will guide the generation of implicitly
3972 declared member functions. */
3973 TYPE_HAS_COMPLEX_INIT_REF (t)
3974 |= (TYPE_HAS_INIT_REF (t)
3975 || TYPE_USES_VIRTUAL_BASECLASSES (t)
3976 || TYPE_POLYMORPHIC_P (t));
3977 TYPE_NEEDS_CONSTRUCTING (t)
3978 |= (TYPE_HAS_CONSTRUCTOR (t)
3979 || TYPE_USES_VIRTUAL_BASECLASSES (t)
3980 || TYPE_POLYMORPHIC_P (t));
3981 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
3982 || TYPE_POLYMORPHIC_P (t));
3983 CLASSTYPE_NON_POD_P (t)
3984 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
3985 || TYPE_HAS_ASSIGN_REF (t));
3986 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
3987 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
3988 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
3990 /* Synthesize any needed methods. Note that methods will be synthesized
3991 for anonymous unions; grok_x_components undoes that. */
3992 add_implicitly_declared_members (t, cant_have_default_ctor,
3993 cant_have_const_ctor,
3996 /* Build and sort the CLASSTYPE_METHOD_VEC. */
3997 finish_struct_methods (t);
3999 /* Process the access-declarations. We wait until now to do this
4000 because handle_using_decls requires that the CLASSTYPE_METHOD_VEC
4001 be set up correctly. */
4002 while (access_decls)
4004 handle_using_decl (TREE_VALUE (access_decls), t);
4005 access_decls = TREE_CHAIN (access_decls);
4009 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4010 accordingly, and, if necessary, add the TYPE_VFIELD to the
4011 TYPE_FIELDS list. */
4014 create_vtable_ptr (t, empty_p, has_virtual_p,
4015 pending_virtuals_p, pending_hard_virtuals_p)
4019 tree *pending_virtuals_p;
4020 tree *pending_hard_virtuals_p;
4024 /* Loop over the virtual functions, adding them to our various
4026 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4027 if (DECL_VINDEX (fn))
4028 add_virtual_function (pending_virtuals_p, pending_hard_virtuals_p,
4029 has_virtual_p, fn, t);
4031 /* If we couldn't find an appropriate base class, create a new field
4033 if (*has_virtual_p && !TYPE_VFIELD (t))
4035 /* We build this decl with vtbl_ptr_type_node, which is a
4036 `vtable_entry_type*'. It might seem more precise to use
4037 `vtable_entry_type (*)[N]' where N is the number of firtual
4038 functions. However, that would require the vtable pointer in
4039 base classes to have a different type than the vtable pointer
4040 in derived classes. We could make that happen, but that
4041 still wouldn't solve all the problems. In particular, the
4042 type-based alias analysis code would decide that assignments
4043 to the base class vtable pointer can't alias assignments to
4044 the derived class vtable pointer, since they have different
4045 types. Thus, in an derived class destructor, where the base
4046 class constructor was inlined, we could generate bad code for
4047 setting up the vtable pointer.
4049 Therefore, we use one type for all vtable pointers. We still
4050 use a type-correct type; it's just doesn't indicate the array
4051 bounds. That's better than using `void*' or some such; it's
4052 cleaner, and it let's the alias analysis code know that these
4053 stores cannot alias stores to void*! */
4055 = build_vtbl_or_vbase_field (get_vfield_name (t),
4056 get_identifier (VFIELD_BASE),
4061 /* Add the new field to the list of fields in this class. */
4063 /* In the old ABI, the vtable pointer goes at the end of the
4065 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), TYPE_VFIELD (t));
4068 /* But in the new ABI, the vtable pointer is the first thing
4070 TYPE_FIELDS (t) = chainon (TYPE_VFIELD (t), TYPE_FIELDS (t));
4071 /* If there were any baseclasses, they can't possibly be at
4072 offset zero any more, because that's where the vtable
4073 pointer is. So, converting to a base class is going to
4075 if (CLASSTYPE_N_BASECLASSES (t))
4076 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4079 /* We can't yet add this new field to the list of all virtual
4080 function table pointers in this class. The
4081 modify_all_vtables function depends on this not being done.
4082 So, it is done later, in finish_struct_1. */
4086 /* Fixup the inline function given by INFO now that the class is
4090 fixup_pending_inline (info)
4091 struct pending_inline *info;
4096 tree fn = info->fndecl;
4098 args = DECL_ARGUMENTS (fn);
4101 DECL_CONTEXT (args) = fn;
4102 args = TREE_CHAIN (args);
4107 /* Fixup the inline methods and friends in TYPE now that TYPE is
4111 fixup_inline_methods (type)
4114 tree method = TYPE_METHODS (type);
4116 if (method && TREE_CODE (method) == TREE_VEC)
4118 if (TREE_VEC_ELT (method, 1))
4119 method = TREE_VEC_ELT (method, 1);
4120 else if (TREE_VEC_ELT (method, 0))
4121 method = TREE_VEC_ELT (method, 0);
4123 method = TREE_VEC_ELT (method, 2);
4126 /* Do inline member functions. */
4127 for (; method; method = TREE_CHAIN (method))
4128 fixup_pending_inline (DECL_PENDING_INLINE_INFO (method));
4131 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4133 method = TREE_CHAIN (method))
4134 fixup_pending_inline (DECL_PENDING_INLINE_INFO (TREE_VALUE (method)));
4135 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4138 /* Called from propagate_binfo_offsets via dfs_walk. */
4141 dfs_propagate_binfo_offsets (binfo, data)
4145 tree offset = (tree) data;
4147 /* Update the BINFO_OFFSET for this base. */
4148 BINFO_OFFSET (binfo)
4149 = size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), offset);
4151 SET_BINFO_MARKED (binfo);
4156 /* Add OFFSET to all base types of BINFO which is a base in the
4157 hierarchy dominated by T.
4159 OFFSET, which is a type offset, is number of bytes.
4161 Note that we don't have to worry about having two paths to the
4162 same base type, since this type owns its association list. */
4165 propagate_binfo_offsets (binfo, offset)
4170 dfs_propagate_binfo_offsets,
4171 dfs_skip_nonprimary_vbases_unmarkedp,
4175 dfs_skip_nonprimary_vbases_markedp,
4179 /* Remove *FIELD (which corresponds to the base given by BINFO) from
4180 the field list for T. */
4183 remove_base_field (t, binfo, field)
4188 tree basetype = BINFO_TYPE (binfo);
4191 my_friendly_assert (TREE_TYPE (*field) == basetype, 23897);
4193 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4194 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4198 = size_int (CEIL (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (*field)),
4200 propagate_binfo_offsets (binfo, offset);
4202 /* Remove this field. */
4203 *field = TREE_CHAIN (*field);
4206 /* Remove the FIELD_DECLs created for T's base classes in
4207 build_base_fields. Simultaneously, update BINFO_OFFSET for all the
4208 bases, except for non-primary virtual baseclasses. */
4211 remove_base_fields (t)
4217 /* Now propagate offset information throughout the lattice.
4218 Simultaneously, remove the temporary FIELD_DECLS we created in
4219 build_base_fields to refer to base types. */
4220 field = &TYPE_FIELDS (t);
4221 if (TYPE_VFIELD (t) == *field)
4223 /* If this class did not have a primary base, we create a
4224 virtual function table pointer. It will be the first thing
4225 in the class, under the new ABI. Skip it; the base fields
4227 my_friendly_assert (flag_new_abi
4228 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t),
4230 field = &TREE_CHAIN (*field);
4233 /* Under the new ABI, the primary base is always allocated first. */
4234 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4235 remove_base_field (t, CLASSTYPE_PRIMARY_BINFO (t), field);
4237 /* Now remove the rest of the bases. */
4238 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
4242 /* Under the new ABI, we've already removed the primary base
4244 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (t))
4247 binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4249 /* We treat a primary virtual base class just like an ordinary base
4250 class. But, non-primary virtual bases are laid out later. */
4251 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4254 remove_base_field (t, binfo, field);
4258 /* Called via dfs_walk from layout_virtual bases. */
4261 dfs_set_offset_for_shared_vbases (binfo, data)
4265 if (TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_MARKED_P (binfo))
4267 /* Update the shared copy. */
4270 shared_binfo = BINFO_FOR_VBASE (BINFO_TYPE (binfo), (tree) data);
4271 BINFO_OFFSET (shared_binfo) = BINFO_OFFSET (binfo);
4277 /* Called via dfs_walk from layout_virtual bases. */
4280 dfs_set_offset_for_unshared_vbases (binfo, data)
4284 /* If this is a virtual base, make sure it has the same offset as
4285 the shared copy. If it's a primary base, then we know it's
4287 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4289 tree t = (tree) data;
4293 vbase = BINFO_FOR_VBASE (BINFO_TYPE (binfo), t);
4294 offset = ssize_binop (MINUS_EXPR,
4295 BINFO_OFFSET (vbase),
4296 BINFO_OFFSET (binfo));
4297 propagate_binfo_offsets (binfo, offset);
4303 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4304 TYPE_ALIGN and TYPE_SIZE for T. */
4307 layout_virtual_bases (t)
4313 /* DSIZE is the size of the class without the virtual bases. */
4314 dsize = TREE_INT_CST_LOW (TYPE_SIZE (t));
4315 /* Make every class have alignment of at least one. */
4316 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4318 /* Go through the virtual bases, allocating space for each virtual
4319 base that is not already a primary base class. */
4320 for (vbase = CLASSTYPE_VBASECLASSES (t);
4322 vbase = TREE_CHAIN (vbase))
4323 if (!BINFO_VBASE_PRIMARY_P (vbase))
4325 /* This virtual base is not a primary base of any class in the
4326 hierarchy, so we have to add space for it. */
4328 unsigned int desired_align;
4330 basetype = BINFO_TYPE (vbase);
4331 desired_align = TYPE_ALIGN (basetype);
4332 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4334 /* Add padding so that we can put the virtual base class at an
4335 appropriately aligned offset. */
4336 dsize = CEIL (dsize, desired_align) * desired_align;
4337 /* And compute the offset of the virtual base. */
4338 propagate_binfo_offsets (vbase,
4339 size_int (CEIL (dsize, BITS_PER_UNIT)));
4340 /* Every virtual baseclass takes a least a UNIT, so that we can
4341 take it's address and get something different for each base. */
4342 dsize += MAX (BITS_PER_UNIT,
4343 TREE_INT_CST_LOW (CLASSTYPE_SIZE (basetype)));
4346 /* Make sure that all of the CLASSTYPE_VBASECLASSES have their
4347 BINFO_OFFSET set correctly. Those we just allocated certainly
4348 will. The others are primary baseclasses; we walk the hierarchy
4349 to find the primary copies and update the shared copy. */
4350 dfs_walk (TYPE_BINFO (t),
4351 dfs_set_offset_for_shared_vbases,
4352 dfs_unmarked_real_bases_queue_p,
4355 /* Now, go through the TYPE_BINFO hierarchy again, setting the
4356 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4357 bases and their direct and indirect bases. The ambiguity checks
4358 in get_base_distance depend on the BINFO_OFFSETs being set
4360 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4362 /* Now, make sure that the total size of the type is a multiple of
4364 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4365 TYPE_SIZE (t) = size_int (dsize);
4366 TYPE_SIZE_UNIT (t) = size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (t),
4367 size_int (BITS_PER_UNIT));
4370 /* Finish the work of layout_record, now taking virtual bases into account.
4371 Also compute the actual offsets that our base classes will have.
4372 This must be performed after the fields are laid out, since virtual
4373 baseclasses must lay down at the end of the record. */
4376 layout_basetypes (rec)
4381 #ifdef STRUCTURE_SIZE_BOUNDARY
4382 /* Packed structures don't need to have minimum size. */
4383 if (! TYPE_PACKED (rec))
4384 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), STRUCTURE_SIZE_BOUNDARY);
4387 /* Remove the FIELD_DECLs we created for baseclasses in
4388 build_base_fields. Simultaneously, update the BINFO_OFFSETs for
4389 everything in the hierarcy except non-primary virtual bases. */
4390 remove_base_fields (rec);
4392 /* Allocate the virtual base classes. */
4393 layout_virtual_bases (rec);
4395 /* Get all the virtual base types that this type uses. The
4396 TREE_VALUE slot holds the virtual baseclass type. Note that
4397 get_vbase_types makes copies of the virtual base BINFOs, so that
4398 the vbase_types are unshared. */
4399 for (vbase_types = CLASSTYPE_VBASECLASSES (rec); vbase_types;
4400 vbase_types = TREE_CHAIN (vbase_types))
4403 tree basetype = BINFO_TYPE (vbase_types);
4404 if (get_base_distance (basetype, rec, 0, (tree*)0) == -2)
4405 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4410 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4411 BINFO_OFFSETs for all of the base-classes. Position the vtable
4415 layout_class_type (t, empty_p, has_virtual_p,
4416 pending_virtuals_p, pending_hard_virtuals_p)
4420 tree *pending_virtuals_p;
4421 tree *pending_hard_virtuals_p;
4423 /* If possible, we reuse the virtual function table pointer from one
4424 of our base classes. */
4425 determine_primary_base (t, has_virtual_p);
4427 /* Add pointers to all of our virtual base-classes. */
4428 TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (t, empty_p),
4430 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4431 TYPE_FIELDS (t) = chainon (build_base_fields (t, empty_p),
4434 /* Create a pointer to our virtual function table. */
4435 create_vtable_ptr (t, empty_p, has_virtual_p,
4436 pending_virtuals_p, pending_hard_virtuals_p);
4438 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4439 we have to save this before we start modifying
4440 TYPE_NONCOPIED_PARTS. */
4441 fixup_inline_methods (t);
4443 /* We make all structures have at least one element, so that they
4444 have non-zero size. The field that we add here is fake, in the
4445 sense that, for example, we don't want people to be able to
4446 initialize it later. So, we add it just long enough to let the
4447 back-end lay out the type, and then remove it. */
4450 tree decl = build_lang_decl
4451 (FIELD_DECL, NULL_TREE, char_type_node);
4452 TREE_CHAIN (decl) = TYPE_FIELDS (t);
4453 TYPE_FIELDS (t) = decl;
4454 TYPE_NONCOPIED_PARTS (t)
4455 = tree_cons (NULL_TREE, decl, TYPE_NONCOPIED_PARTS (t));
4456 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
4459 /* Let the back-end lay out the type. Note that at this point we
4460 have only included non-virtual base-classes; we will lay out the
4461 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
4462 this call are not necessarily correct; they are just the size and
4463 alignment when no virtual base clases are used. */
4466 /* If we added an extra field to make this class non-empty, remove
4469 TYPE_FIELDS (t) = TREE_CHAIN (TYPE_FIELDS (t));
4471 /* Delete all zero-width bit-fields from the list of fields. Now
4472 that the type is laid out they are no longer important. */
4473 remove_zero_width_bit_fields (t);
4475 /* Remember the size and alignment of the class before adding
4476 the virtual bases. */
4477 if (*empty_p && flag_new_abi)
4478 CLASSTYPE_SIZE (t) = integer_zero_node;
4479 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
4480 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
4481 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
4483 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
4484 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
4486 /* Set the TYPE_DECL for this type to contain the right
4487 value for DECL_OFFSET, so that we can use it as part
4488 of a COMPONENT_REF for multiple inheritance. */
4489 layout_decl (TYPE_MAIN_DECL (t), 0);
4491 /* Now fix up any virtual base class types that we left lying
4492 around. We must get these done before we try to lay out the
4493 virtual function table. */
4494 if (CLASSTYPE_N_BASECLASSES (t))
4495 /* layout_basetypes will remove the base subobject fields. */
4496 layout_basetypes (t);
4499 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
4500 (or C++ class declaration).
4502 For C++, we must handle the building of derived classes.
4503 Also, C++ allows static class members. The way that this is
4504 handled is to keep the field name where it is (as the DECL_NAME
4505 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
4506 of the field. layout_record and layout_union will know about this.
4508 More C++ hair: inline functions have text in their
4509 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
4510 meaningful tree structure. After the struct has been laid out, set
4511 things up so that this can happen.
4513 And still more: virtual functions. In the case of single inheritance,
4514 when a new virtual function is seen which redefines a virtual function
4515 from the base class, the new virtual function is placed into
4516 the virtual function table at exactly the same address that
4517 it had in the base class. When this is extended to multiple
4518 inheritance, the same thing happens, except that multiple virtual
4519 function tables must be maintained. The first virtual function
4520 table is treated in exactly the same way as in the case of single
4521 inheritance. Additional virtual function tables have different
4522 DELTAs, which tell how to adjust `this' to point to the right thing.
4524 ATTRIBUTES is the set of decl attributes to be applied, if any. */
4532 tree pending_virtuals = NULL_TREE;
4533 tree pending_hard_virtuals = NULL_TREE;
4540 if (IS_AGGR_TYPE (t))
4541 cp_error ("redefinition of `%#T'", t);
4543 my_friendly_abort (172);
4548 GNU_xref_decl (current_function_decl, t);
4550 /* If this type was previously laid out as a forward reference,
4551 make sure we lay it out again. */
4553 TYPE_SIZE (t) = NULL_TREE;
4554 CLASSTYPE_GOT_SEMICOLON (t) = 0;
4555 CLASSTYPE_VFIELD_PARENT (t) = -1;
4557 CLASSTYPE_RTTI (t) = NULL_TREE;
4559 /* Do end-of-class semantic processing: checking the validity of the
4560 bases and members and add implicitly generated methods. */
4561 check_bases_and_members (t, &empty);
4563 /* Layout the class itself. */
4564 layout_class_type (t, &empty, &has_virtual,
4565 &pending_virtuals, &pending_hard_virtuals);
4567 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
4571 vbases = CLASSTYPE_VBASECLASSES (t);
4574 /* Now fixup overrides of all functions in vtables from all
4575 direct or indirect virtual base classes. */
4576 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
4577 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
4579 for (i = 0; i < n_baseclasses; i++)
4581 tree base_binfo = TREE_VEC_ELT (binfos, i);
4582 tree basetype = BINFO_TYPE (base_binfo);
4585 vbases = CLASSTYPE_VBASECLASSES (basetype);
4588 merge_overrides (binfo_member (BINFO_TYPE (vbases),
4589 CLASSTYPE_VBASECLASSES (t)),
4591 vbases = TREE_CHAIN (vbases);
4597 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
4598 might need to know it for setting up the offsets in the vtable
4599 (or in thunks) below. */
4600 vfield = TYPE_VFIELD (t);
4601 if (vfield != NULL_TREE
4602 && DECL_FIELD_CONTEXT (vfield) != t)
4604 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4605 tree offset = BINFO_OFFSET (binfo);
4607 vfield = copy_node (vfield);
4608 copy_lang_decl (vfield);
4610 if (! integer_zerop (offset))
4611 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4612 DECL_FIELD_CONTEXT (vfield) = t;
4613 DECL_CLASS_CONTEXT (vfield) = t;
4614 DECL_FIELD_BITPOS (vfield)
4615 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4616 TYPE_VFIELD (t) = vfield;
4619 if (flag_rtti && TYPE_POLYMORPHIC_P (t) && !pending_hard_virtuals)
4620 modify_all_vtables (t, NULL_TREE);
4622 for (pending_hard_virtuals = nreverse (pending_hard_virtuals);
4623 pending_hard_virtuals;
4624 pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals))
4625 modify_all_vtables (t, TREE_VALUE (pending_hard_virtuals));
4627 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
4630 /* Now fixup any virtual function entries from virtual bases
4631 that have different deltas. This has to come after we do the
4632 pending hard virtuals, as we might have a function that comes
4633 from multiple virtual base instances that is only overridden
4634 by a hard virtual above. */
4635 vbases = CLASSTYPE_VBASECLASSES (t);
4638 /* We might be able to shorten the amount of work we do by
4639 only doing this for vtables that come from virtual bases
4640 that have differing offsets, but don't want to miss any
4642 fixup_vtable_deltas (vbases, 1, t);
4643 vbases = TREE_CHAIN (vbases);
4647 /* Under our model of GC, every C++ class gets its own virtual
4648 function table, at least virtually. */
4649 if (pending_virtuals)
4651 pending_virtuals = nreverse (pending_virtuals);
4652 /* We must enter these virtuals into the table. */
4653 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4655 if (! CLASSTYPE_COM_INTERFACE (t))
4657 /* The second slot is for the tdesc pointer when thunks are used. */
4658 if (flag_vtable_thunks)
4659 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4661 /* The first slot is for the rtti offset. */
4662 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4664 set_rtti_entry (pending_virtuals,
4665 convert (ssizetype, integer_zero_node), t);
4667 build_vtable (NULL_TREE, t);
4669 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
4670 /* Here we know enough to change the type of our virtual
4671 function table, but we will wait until later this function. */
4672 build_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
4674 /* If this type has basetypes with constructors, then those
4675 constructors might clobber the virtual function table. But
4676 they don't if the derived class shares the exact vtable of the base
4679 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4681 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4683 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
4685 /* This class contributes nothing new to the virtual function
4686 table. However, it may have declared functions which
4687 went into the virtual function table "inherited" from the
4688 base class. If so, we grab a copy of those updated functions,
4689 and pretend they are ours. */
4691 /* See if we should steal the virtual info from base class. */
4692 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
4693 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
4694 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
4695 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
4696 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
4697 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4700 if (TYPE_POLYMORPHIC_P (t))
4702 CLASSTYPE_VSIZE (t) = has_virtual;
4703 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4705 if (pending_virtuals)
4706 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t),
4709 else if (has_virtual)
4711 TYPE_BINFO_VIRTUALS (t) = pending_virtuals;
4712 DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1;
4716 /* Now lay out the virtual function table. */
4719 /* Use size_int so values are memoized in common cases. */
4720 tree itype = build_index_type (size_int (has_virtual));
4721 tree atype = build_cplus_array_type (vtable_entry_type, itype);
4723 layout_type (atype);
4725 /* We may have to grow the vtable. */
4726 if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
4728 TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
4729 DECL_SIZE (TYPE_BINFO_VTABLE (t)) = 0;
4730 layout_decl (TYPE_BINFO_VTABLE (t), 0);
4731 /* At one time the vtable info was grabbed 2 words at a time. This
4732 fails on sparc unless you have 8-byte alignment. (tiemann) */
4733 DECL_ALIGN (TYPE_BINFO_VTABLE (t))
4734 = MAX (TYPE_ALIGN (double_type_node),
4735 DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
4739 /* If we created a new vtbl pointer for this class, add it to the
4741 if (TYPE_VFIELD (t) && CLASSTYPE_VFIELD_PARENT (t) == -1)
4742 CLASSTYPE_VFIELDS (t)
4743 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
4745 finish_struct_bits (t);
4747 /* Complete the rtl for any static member objects of the type we're
4749 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
4751 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4752 && TREE_TYPE (x) == t)
4754 DECL_MODE (x) = TYPE_MODE (t);
4755 make_decl_rtl (x, NULL, 0);
4759 /* Done with FIELDS...now decide whether to sort these for
4760 faster lookups later.
4762 The C front-end only does this when n_fields > 15. We use
4763 a smaller number because most searches fail (succeeding
4764 ultimately as the search bores through the inheritance
4765 hierarchy), and we want this failure to occur quickly. */
4767 n_fields = count_fields (TYPE_FIELDS (t));
4770 tree field_vec = make_tree_vec (n_fields);
4771 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
4772 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
4773 (int (*)(const void *, const void *))field_decl_cmp);
4774 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
4775 retrofit_lang_decl (TYPE_MAIN_DECL (t));
4776 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
4779 if (TYPE_HAS_CONSTRUCTOR (t))
4781 tree vfields = CLASSTYPE_VFIELDS (t);
4785 /* Mark the fact that constructor for T
4786 could affect anybody inheriting from T
4787 who wants to initialize vtables for VFIELDS's type. */
4788 if (VF_DERIVED_VALUE (vfields))
4789 TREE_ADDRESSABLE (vfields) = 1;
4790 vfields = TREE_CHAIN (vfields);
4794 if (CLASSTYPE_VSIZE (t) != 0)
4796 /* In addition to this one, all the other vfields should be listed. */
4797 /* Before that can be done, we have to have FIELD_DECLs for them, and
4798 a place to find them. */
4799 TYPE_NONCOPIED_PARTS (t)
4800 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
4801 TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
4803 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
4804 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
4805 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
4809 /* Make the rtl for any new vtables we have created, and unmark
4810 the base types we marked. */
4812 hack_incomplete_structures (t);
4814 if (warn_overloaded_virtual)
4817 maybe_suppress_debug_info (t);
4819 /* Finish debugging output for this type. */
4820 rest_of_type_compilation (t, toplevel_bindings_p ());
4823 /* When T was built up, the member declarations were added in reverse
4824 order. Rearrange them to declaration order. */
4827 unreverse_member_declarations (t)
4834 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
4835 reverse order. Put them in declaration order now. */
4836 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
4837 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
4839 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
4840 reverse order, so we can't just use nreverse. */
4842 for (x = TYPE_FIELDS (t);
4843 x && TREE_CODE (x) != TYPE_DECL;
4846 next = TREE_CHAIN (x);
4847 TREE_CHAIN (x) = prev;
4852 TREE_CHAIN (TYPE_FIELDS (t)) = x;
4854 TYPE_FIELDS (t) = prev;
4859 finish_struct (t, attributes)
4862 /* Now that we've got all the field declarations, reverse everything
4864 unreverse_member_declarations (t);
4866 cplus_decl_attributes (t, attributes, NULL_TREE);
4868 if (processing_template_decl)
4870 finish_struct_methods (t);
4871 TYPE_SIZE (t) = integer_zero_node;
4874 finish_struct_1 (t);
4876 TYPE_BEING_DEFINED (t) = 0;
4878 if (current_class_type)
4881 error ("trying to finish struct, but kicked out due to previous parse errors.");
4883 if (processing_template_decl)
4885 tree scope = current_scope ();
4886 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
4887 add_tree (build_min (TAG_DEFN, t));
4893 /* Return the dynamic type of INSTANCE, if known.
4894 Used to determine whether the virtual function table is needed
4897 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4898 of our knowledge of its type. */
4901 fixed_type_or_null (instance, nonnull)
4908 switch (TREE_CODE (instance))
4911 /* Check that we are not going through a cast of some sort. */
4912 if (TREE_TYPE (instance)
4913 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
4914 instance = TREE_OPERAND (instance, 0);
4915 /* fall through... */
4917 /* This is a call to a constructor, hence it's never zero. */
4918 if (TREE_HAS_CONSTRUCTOR (instance))
4922 return TREE_TYPE (instance);
4927 /* This is a call to a constructor, hence it's never zero. */
4928 if (TREE_HAS_CONSTRUCTOR (instance))
4932 return TREE_TYPE (instance);
4934 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4941 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
4942 /* Propagate nonnull. */
4943 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4944 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
4945 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4950 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4955 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4958 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
4962 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
4963 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
4967 return TREE_TYPE (TREE_TYPE (instance));
4969 /* fall through... */
4972 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
4976 return TREE_TYPE (instance);
4980 if (instance == current_class_ptr
4981 && flag_this_is_variable <= 0)
4983 /* Normally, 'this' must be non-null. */
4984 if (flag_this_is_variable == 0)
4987 /* <0 means we're in a constructor and we know our type. */
4988 if (flag_this_is_variable < 0)
4989 return TREE_TYPE (TREE_TYPE (instance));
4991 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4992 /* Reference variables should be references to objects. */
5002 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5003 to the static type. We also handle the case where INSTANCE is really
5006 Used to determine whether the virtual function table is needed
5009 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5010 of our knowledge of its type. */
5013 resolves_to_fixed_type_p (instance, nonnull)
5017 tree t = TREE_TYPE (instance);
5018 tree fixed = fixed_type_or_null (instance, nonnull);
5019 if (fixed == NULL_TREE)
5021 if (POINTER_TYPE_P (t))
5023 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
5028 init_class_processing ()
5030 current_class_depth = 0;
5031 current_class_stack_size = 10;
5033 = (class_stack_node_t) xmalloc (current_class_stack_size
5034 * sizeof (struct class_stack_node));
5036 access_default_node = build_int_2 (0, 0);
5037 access_public_node = build_int_2 (1, 0);
5038 access_protected_node = build_int_2 (2, 0);
5039 access_private_node = build_int_2 (3, 0);
5040 access_default_virtual_node = build_int_2 (4, 0);
5041 access_public_virtual_node = build_int_2 (5, 0);
5042 access_protected_virtual_node = build_int_2 (6, 0);
5043 access_private_virtual_node = build_int_2 (7, 0);
5046 /* Set current scope to NAME. CODE tells us if this is a
5047 STRUCT, UNION, or ENUM environment.
5049 NAME may end up being NULL_TREE if this is an anonymous or
5050 late-bound struct (as in "struct { ... } foo;") */
5052 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5053 appropriate values, found by looking up the type definition of
5056 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5057 which can be seen locally to the class. They are shadowed by
5058 any subsequent local declaration (including parameter names).
5060 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5061 which have static meaning (i.e., static members, static
5062 member functions, enum declarations, etc).
5064 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5065 which can be seen locally to the class (as in 1), but
5066 know that we are doing this for declaration purposes
5067 (i.e. friend foo::bar (int)).
5069 So that we may avoid calls to lookup_name, we cache the _TYPE
5070 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5072 For multiple inheritance, we perform a two-pass depth-first search
5073 of the type lattice. The first pass performs a pre-order search,
5074 marking types after the type has had its fields installed in
5075 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5076 unmarks the marked types. If a field or member function name
5077 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5078 that name becomes `error_mark_node'. */
5081 pushclass (type, modify)
5085 type = TYPE_MAIN_VARIANT (type);
5087 /* Make sure there is enough room for the new entry on the stack. */
5088 if (current_class_depth + 1 >= current_class_stack_size)
5090 current_class_stack_size *= 2;
5092 = (class_stack_node_t) xrealloc (current_class_stack,
5093 current_class_stack_size
5094 * sizeof (struct class_stack_node));
5097 /* Insert a new entry on the class stack. */
5098 current_class_stack[current_class_depth].name = current_class_name;
5099 current_class_stack[current_class_depth].type = current_class_type;
5100 current_class_stack[current_class_depth].access = current_access_specifier;
5101 current_class_stack[current_class_depth].names_used = 0;
5102 current_class_depth++;
5104 /* Now set up the new type. */
5105 current_class_name = TYPE_NAME (type);
5106 if (TREE_CODE (current_class_name) == TYPE_DECL)
5107 current_class_name = DECL_NAME (current_class_name);
5108 current_class_type = type;
5110 /* By default, things in classes are private, while things in
5111 structures or unions are public. */
5112 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5113 ? access_private_node
5114 : access_public_node);
5116 if (previous_class_type != NULL_TREE
5117 && (type != previous_class_type
5118 || TYPE_SIZE (previous_class_type) == NULL_TREE)
5119 && current_class_depth == 1)
5121 /* Forcibly remove any old class remnants. */
5122 invalidate_class_lookup_cache ();
5125 /* If we're about to enter a nested class, clear
5126 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5127 if (modify && current_class_depth > 1)
5128 clear_identifier_class_values ();
5133 if (CLASSTYPE_TEMPLATE_INFO (type))
5134 overload_template_name (type);
5139 if (type != previous_class_type || current_class_depth > 1)
5140 push_class_decls (type);
5145 /* We are re-entering the same class we just left, so we
5146 don't have to search the whole inheritance matrix to find
5147 all the decls to bind again. Instead, we install the
5148 cached class_shadowed list, and walk through it binding
5149 names and setting up IDENTIFIER_TYPE_VALUEs. */
5150 set_class_shadows (previous_class_values);
5151 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5153 tree id = TREE_PURPOSE (item);
5154 tree decl = TREE_TYPE (item);
5156 push_class_binding (id, decl);
5157 if (TREE_CODE (decl) == TYPE_DECL)
5158 set_identifier_type_value (id, TREE_TYPE (decl));
5160 unuse_fields (type);
5163 storetags (CLASSTYPE_TAGS (type));
5167 /* When we exit a toplevel class scope, we save the
5168 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5169 reenter the class. Here, we've entered some other class, so we
5170 must invalidate our cache. */
5173 invalidate_class_lookup_cache ()
5177 /* This code can be seen as a cache miss. When we've cached a
5178 class' scope's bindings and we can't use them, we need to reset
5179 them. This is it! */
5180 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5181 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5183 previous_class_type = NULL_TREE;
5186 /* Get out of the current class scope. If we were in a class scope
5187 previously, that is the one popped to. */
5193 /* Since poplevel_class does the popping of class decls nowadays,
5194 this really only frees the obstack used for these decls. */
5197 current_class_depth--;
5198 current_class_name = current_class_stack[current_class_depth].name;
5199 current_class_type = current_class_stack[current_class_depth].type;
5200 current_access_specifier = current_class_stack[current_class_depth].access;
5201 if (current_class_stack[current_class_depth].names_used)
5202 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5205 /* Returns 1 if current_class_type is either T or a nested type of T. */
5208 currently_open_class (t)
5212 if (t == current_class_type)
5214 for (i = 0; i < current_class_depth; ++i)
5215 if (current_class_stack [i].type == t)
5220 /* When entering a class scope, all enclosing class scopes' names with
5221 static meaning (static variables, static functions, types and enumerators)
5222 have to be visible. This recursive function calls pushclass for all
5223 enclosing class contexts until global or a local scope is reached.
5224 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5225 formal of the same name. */
5228 push_nested_class (type, modify)
5234 /* A namespace might be passed in error cases, like A::B:C. */
5235 if (type == NULL_TREE || type == error_mark_node || ! IS_AGGR_TYPE (type)
5236 || TREE_CODE (type) == NAMESPACE_DECL
5237 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5238 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
5241 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5243 if (context && CLASS_TYPE_P (context))
5244 push_nested_class (context, 2);
5245 pushclass (type, modify);
5248 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5253 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5256 if (context && CLASS_TYPE_P (context))
5257 pop_nested_class ();
5260 /* Set global variables CURRENT_LANG_NAME to appropriate value
5261 so that behavior of name-mangling machinery is correct. */
5264 push_lang_context (name)
5267 *current_lang_stack++ = current_lang_name;
5268 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
5269 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
5271 size_t old_size = VARRAY_SIZE (current_lang_base);
5273 VARRAY_GROW (current_lang_base, old_size + 10);
5274 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
5277 if (name == lang_name_cplusplus)
5279 strict_prototype = strict_prototypes_lang_cplusplus;
5280 current_lang_name = name;
5282 else if (name == lang_name_java)
5284 strict_prototype = strict_prototypes_lang_cplusplus;
5285 current_lang_name = name;
5286 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5287 (See record_builtin_java_type in decl.c.) However, that causes
5288 incorrect debug entries if these types are actually used.
5289 So we re-enable debug output after extern "Java". */
5290 DECL_IGNORED_P (java_byte_type_node) = 0;
5291 DECL_IGNORED_P (java_short_type_node) = 0;
5292 DECL_IGNORED_P (java_int_type_node) = 0;
5293 DECL_IGNORED_P (java_long_type_node) = 0;
5294 DECL_IGNORED_P (java_float_type_node) = 0;
5295 DECL_IGNORED_P (java_double_type_node) = 0;
5296 DECL_IGNORED_P (java_char_type_node) = 0;
5297 DECL_IGNORED_P (java_boolean_type_node) = 0;
5299 else if (name == lang_name_c)
5301 strict_prototype = strict_prototypes_lang_c;
5302 current_lang_name = name;
5305 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5308 /* Get out of the current language scope. */
5313 /* Clear the current entry so that garbage collector won't hold on
5315 *current_lang_stack = NULL_TREE;
5316 current_lang_name = *--current_lang_stack;
5317 if (current_lang_name == lang_name_cplusplus
5318 || current_lang_name == lang_name_java)
5319 strict_prototype = strict_prototypes_lang_cplusplus;
5320 else if (current_lang_name == lang_name_c)
5321 strict_prototype = strict_prototypes_lang_c;
5324 /* Type instantiation routines. */
5326 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5327 matches the TARGET_TYPE. If there is no satisfactory match, return
5328 error_mark_node, and issue an error message if COMPLAIN is
5329 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
5330 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5331 template arguments. */
5334 resolve_address_of_overloaded_function (target_type,
5343 tree explicit_targs;
5345 /* Here's what the standard says:
5349 If the name is a function template, template argument deduction
5350 is done, and if the argument deduction succeeds, the deduced
5351 arguments are used to generate a single template function, which
5352 is added to the set of overloaded functions considered.
5354 Non-member functions and static member functions match targets of
5355 type "pointer-to-function" or "reference-to-function." Nonstatic
5356 member functions match targets of type "pointer-to-member
5357 function;" the function type of the pointer to member is used to
5358 select the member function from the set of overloaded member
5359 functions. If a nonstatic member function is selected, the
5360 reference to the overloaded function name is required to have the
5361 form of a pointer to member as described in 5.3.1.
5363 If more than one function is selected, any template functions in
5364 the set are eliminated if the set also contains a non-template
5365 function, and any given template function is eliminated if the
5366 set contains a second template function that is more specialized
5367 than the first according to the partial ordering rules 14.5.5.2.
5368 After such eliminations, if any, there shall remain exactly one
5369 selected function. */
5372 int is_reference = 0;
5373 /* We store the matches in a TREE_LIST rooted here. The functions
5374 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5375 interoperability with most_specialized_instantiation. */
5376 tree matches = NULL_TREE;
5379 /* By the time we get here, we should be seeing only real
5380 pointer-to-member types, not the internal POINTER_TYPE to
5381 METHOD_TYPE representation. */
5382 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5383 && (TREE_CODE (TREE_TYPE (target_type))
5384 == METHOD_TYPE)), 0);
5386 /* Check that the TARGET_TYPE is reasonable. */
5387 if (TYPE_PTRFN_P (target_type))
5390 else if (TYPE_PTRMEMFUNC_P (target_type))
5391 /* This is OK, too. */
5393 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5395 /* This is OK, too. This comes from a conversion to reference
5397 target_type = build_reference_type (target_type);
5403 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
5404 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5405 return error_mark_node;
5408 /* If we can find a non-template function that matches, we can just
5409 use it. There's no point in generating template instantiations
5410 if we're just going to throw them out anyhow. But, of course, we
5411 can only do this when we don't *need* a template function. */
5416 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5418 tree fn = OVL_FUNCTION (fns);
5421 if (TREE_CODE (fn) == TEMPLATE_DECL)
5422 /* We're not looking for templates just yet. */
5425 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5427 /* We're looking for a non-static member, and this isn't
5428 one, or vice versa. */
5431 /* See if there's a match. */
5432 fntype = TREE_TYPE (fn);
5434 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5435 else if (!is_reference)
5436 fntype = build_pointer_type (fntype);
5438 if (can_convert_arg (target_type, fntype, fn))
5439 matches = tree_cons (fn, NULL_TREE, matches);
5443 /* Now, if we've already got a match (or matches), there's no need
5444 to proceed to the template functions. But, if we don't have a
5445 match we need to look at them, too. */
5448 tree target_fn_type;
5449 tree target_arg_types;
5454 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5456 target_fn_type = TREE_TYPE (target_type);
5457 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5459 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5461 tree fn = OVL_FUNCTION (fns);
5463 tree instantiation_type;
5466 if (TREE_CODE (fn) != TEMPLATE_DECL)
5467 /* We're only looking for templates. */
5470 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5472 /* We're not looking for a non-static member, and this is
5473 one, or vice versa. */
5476 /* Try to do argument deduction. */
5477 targs = make_tree_vec (DECL_NTPARMS (fn));
5478 if (fn_type_unification (fn, explicit_targs, targs,
5479 target_arg_types, NULL_TREE,
5481 /* Argument deduction failed. */
5484 /* Instantiate the template. */
5485 instantiation = instantiate_template (fn, targs);
5486 if (instantiation == error_mark_node)
5487 /* Instantiation failed. */
5490 /* See if there's a match. */
5491 instantiation_type = TREE_TYPE (instantiation);
5493 instantiation_type =
5494 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5495 else if (!is_reference)
5496 instantiation_type = build_pointer_type (instantiation_type);
5497 if (can_convert_arg (target_type, instantiation_type, instantiation))
5498 matches = tree_cons (instantiation, fn, matches);
5501 /* Now, remove all but the most specialized of the matches. */
5504 tree match = most_specialized_instantiation (matches,
5507 if (match != error_mark_node)
5508 matches = tree_cons (match, NULL_TREE, NULL_TREE);
5512 /* Now we should have exactly one function in MATCHES. */
5513 if (matches == NULL_TREE)
5515 /* There were *no* matches. */
5518 cp_error ("no matches converting function `%D' to type `%#T'",
5519 DECL_NAME (OVL_FUNCTION (overload)),
5522 /* print_candidates expects a chain with the functions in
5523 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5524 so why be clever?). */
5525 for (; overload; overload = OVL_NEXT (overload))
5526 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
5529 print_candidates (matches);
5531 return error_mark_node;
5533 else if (TREE_CHAIN (matches))
5535 /* There were too many matches. */
5541 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
5542 DECL_NAME (OVL_FUNCTION (overload)),
5545 /* Since print_candidates expects the functions in the
5546 TREE_VALUE slot, we flip them here. */
5547 for (match = matches; match; match = TREE_CHAIN (match))
5548 TREE_VALUE (match) = TREE_PURPOSE (match);
5550 print_candidates (matches);
5553 return error_mark_node;
5556 /* Good, exactly one match. Now, convert it to the correct type. */
5557 fn = TREE_PURPOSE (matches);
5561 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
5562 return build_unary_op (ADDR_EXPR, fn, 0);
5565 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
5566 will mark the function as addressed, but here we must do it
5568 mark_addressable (fn);
5574 /* This function will instantiate the type of the expression given in
5575 RHS to match the type of LHSTYPE. If errors exist, then return
5576 error_mark_node. We only complain is COMPLAIN is set. If we are
5577 not complaining, never modify rhs, as overload resolution wants to
5578 try many possible instantiations, in hopes that at least one will
5581 FLAGS is a bitmask, as we see at the top of the function.
5583 For non-recursive calls, LHSTYPE should be a function, pointer to
5584 function, or a pointer to member function. */
5587 instantiate_type (lhstype, rhs, flags)
5591 int complain = (flags & 1);
5592 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
5594 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
5597 error ("not enough type information");
5598 return error_mark_node;
5601 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
5603 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
5606 cp_error ("argument of type `%T' does not match `%T'",
5607 TREE_TYPE (rhs), lhstype);
5608 return error_mark_node;
5611 /* We don't overwrite rhs if it is an overloaded function.
5612 Copying it would destroy the tree link. */
5613 if (TREE_CODE (rhs) != OVERLOAD)
5614 rhs = copy_node (rhs);
5616 /* This should really only be used when attempting to distinguish
5617 what sort of a pointer to function we have. For now, any
5618 arithmetic operation which is not supported on pointers
5619 is rejected as an error. */
5621 switch (TREE_CODE (rhs))
5628 my_friendly_abort (177);
5629 return error_mark_node;
5636 new_rhs = instantiate_type (build_pointer_type (lhstype),
5637 TREE_OPERAND (rhs, 0), flags);
5638 if (new_rhs == error_mark_node)
5639 return error_mark_node;
5641 TREE_TYPE (rhs) = lhstype;
5642 TREE_OPERAND (rhs, 0) = new_rhs;
5647 rhs = copy_node (TREE_OPERAND (rhs, 0));
5648 TREE_TYPE (rhs) = unknown_type_node;
5649 return instantiate_type (lhstype, rhs, flags);
5653 tree r = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5655 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype)
5656 && complain && !flag_ms_extensions)
5658 /* Note: we check this after the recursive call to avoid
5659 complaining about cases where overload resolution fails. */
5661 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
5662 tree fn = PTRMEM_CST_MEMBER (r);
5664 my_friendly_assert (TREE_CODE (r) == PTRMEM_CST, 990811);
5667 ("object-dependent reference to `%E' can only be used in a call",
5670 (" to form a pointer to member function, say `&%T::%E'",
5678 rhs = TREE_OPERAND (rhs, 1);
5679 if (BASELINK_P (rhs))
5680 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5682 /* This can happen if we are forming a pointer-to-member for a
5684 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
5688 case TEMPLATE_ID_EXPR:
5690 resolve_address_of_overloaded_function (lhstype,
5691 TREE_OPERAND (rhs, 0),
5693 /*template_only=*/1,
5694 TREE_OPERAND (rhs, 1));
5698 resolve_address_of_overloaded_function (lhstype,
5701 /*template_only=*/0,
5702 /*explicit_targs=*/NULL_TREE);
5705 /* Now we should have a baselink. */
5706 my_friendly_assert (BASELINK_P (rhs), 990412);
5708 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5711 /* This is too hard for now. */
5712 my_friendly_abort (183);
5713 return error_mark_node;
5718 TREE_OPERAND (rhs, 0)
5719 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5720 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5721 return error_mark_node;
5722 TREE_OPERAND (rhs, 1)
5723 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5724 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5725 return error_mark_node;
5727 TREE_TYPE (rhs) = lhstype;
5731 case TRUNC_DIV_EXPR:
5732 case FLOOR_DIV_EXPR:
5734 case ROUND_DIV_EXPR:
5736 case TRUNC_MOD_EXPR:
5737 case FLOOR_MOD_EXPR:
5739 case ROUND_MOD_EXPR:
5740 case FIX_ROUND_EXPR:
5741 case FIX_FLOOR_EXPR:
5743 case FIX_TRUNC_EXPR:
5759 case PREINCREMENT_EXPR:
5760 case PREDECREMENT_EXPR:
5761 case POSTINCREMENT_EXPR:
5762 case POSTDECREMENT_EXPR:
5764 error ("invalid operation on uninstantiated type");
5765 return error_mark_node;
5767 case TRUTH_AND_EXPR:
5769 case TRUTH_XOR_EXPR:
5776 case TRUTH_ANDIF_EXPR:
5777 case TRUTH_ORIF_EXPR:
5778 case TRUTH_NOT_EXPR:
5780 error ("not enough type information");
5781 return error_mark_node;
5784 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
5787 error ("not enough type information");
5788 return error_mark_node;
5790 TREE_OPERAND (rhs, 1)
5791 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5792 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5793 return error_mark_node;
5794 TREE_OPERAND (rhs, 2)
5795 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
5796 if (TREE_OPERAND (rhs, 2) == error_mark_node)
5797 return error_mark_node;
5799 TREE_TYPE (rhs) = lhstype;
5803 TREE_OPERAND (rhs, 1)
5804 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5805 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5806 return error_mark_node;
5808 TREE_TYPE (rhs) = lhstype;
5812 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5814 case ENTRY_VALUE_EXPR:
5815 my_friendly_abort (184);
5816 return error_mark_node;
5819 return error_mark_node;
5822 my_friendly_abort (185);
5823 return error_mark_node;
5827 /* Return the name of the virtual function pointer field
5828 (as an IDENTIFIER_NODE) for the given TYPE. Note that
5829 this may have to look back through base types to find the
5830 ultimate field name. (For single inheritance, these could
5831 all be the same name. Who knows for multiple inheritance). */
5834 get_vfield_name (type)
5837 tree binfo = TYPE_BINFO (type);
5840 while (BINFO_BASETYPES (binfo)
5841 && TYPE_POLYMORPHIC_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
5842 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
5843 binfo = BINFO_BASETYPE (binfo, 0);
5845 type = BINFO_TYPE (binfo);
5846 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
5847 + TYPE_NAME_LENGTH (type) + 2);
5848 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
5849 return get_identifier (buf);
5853 print_class_statistics ()
5855 #ifdef GATHER_STATISTICS
5856 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
5857 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
5858 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
5859 n_build_method_call, n_inner_fields_searched);
5862 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
5863 n_vtables, n_vtable_searches);
5864 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
5865 n_vtable_entries, n_vtable_elems);
5870 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
5871 according to [class]:
5872 The class-name is also inserted
5873 into the scope of the class itself. For purposes of access checking,
5874 the inserted class name is treated as if it were a public member name. */
5877 build_self_reference ()
5879 tree name = constructor_name (current_class_type);
5880 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
5883 DECL_NONLOCAL (value) = 1;
5884 DECL_CONTEXT (value) = current_class_type;
5885 DECL_CLASS_CONTEXT (value) = current_class_type;
5886 DECL_ARTIFICIAL (value) = 1;
5888 if (processing_template_decl)
5889 value = push_template_decl (value);
5891 saved_cas = current_access_specifier;
5892 current_access_specifier = access_public_node;
5893 finish_member_declaration (value);
5894 current_access_specifier = saved_cas;
5897 /* Returns 1 if TYPE contains only padding bytes. */
5900 is_empty_class (type)
5905 if (type == error_mark_node)
5908 if (! IS_AGGR_TYPE (type))
5912 return CLASSTYPE_SIZE (type) == integer_zero_node;
5914 if (TYPE_BINFO_BASETYPES (type))
5916 t = TYPE_FIELDS (type);
5917 while (t && TREE_CODE (t) != FIELD_DECL)
5919 return (t == NULL_TREE);
5922 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
5923 a *_TYPE node. NODE can also be a local class. */
5926 get_enclosing_class (type)
5931 while (node && TREE_CODE (node) != NAMESPACE_DECL)
5933 switch (TREE_CODE_CLASS (TREE_CODE (node)))
5936 node = DECL_CONTEXT (node);
5942 node = TYPE_CONTEXT (node);
5946 my_friendly_abort (0);
5952 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
5955 is_base_of_enclosing_class (base, type)
5960 if (get_binfo (base, type, 0))
5963 type = get_enclosing_class (type);
5968 /* Note that NAME was looked up while the current class was being
5969 defined and that the result of that lookup was DECL. */
5972 maybe_note_name_used_in_class (name, decl)
5976 splay_tree names_used;
5978 /* If we're not defining a class, there's nothing to do. */
5979 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
5982 /* If there's already a binding for this NAME, then we don't have
5983 anything to worry about. */
5984 if (IDENTIFIER_CLASS_VALUE (name))
5987 if (!current_class_stack[current_class_depth - 1].names_used)
5988 current_class_stack[current_class_depth - 1].names_used
5989 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
5990 names_used = current_class_stack[current_class_depth - 1].names_used;
5992 splay_tree_insert (names_used,
5993 (splay_tree_key) name,
5994 (splay_tree_value) decl);
5997 /* Note that NAME was declared (as DECL) in the current class. Check
5998 to see that the declaration is legal. */
6001 note_name_declared_in_class (name, decl)
6005 splay_tree names_used;
6008 /* Look to see if we ever used this name. */
6010 = current_class_stack[current_class_depth - 1].names_used;
6014 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6017 /* [basic.scope.class]
6019 A name N used in a class S shall refer to the same declaration
6020 in its context and when re-evaluated in the completed scope of
6022 cp_error ("declaration of `%#D'", decl);
6023 cp_error_at ("changes meaning of `%s' from `%+#D'",
6024 IDENTIFIER_POINTER (DECL_NAME (decl)),
6029 /* Dump the offsets of all the bases rooted at BINFO to stderr.
6030 INDENT should be zero when called from the top level; it is
6031 incremented recursively. */
6034 dump_class_hierarchy (binfo, indent)
6040 fprintf (stderr, "%*s0x%x (%s) %d\n", indent, "",
6041 (unsigned int) binfo,
6042 type_as_string (binfo, TS_PLAIN),
6043 TREE_INT_CST_LOW (BINFO_OFFSET (binfo)));
6045 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6046 dump_class_hierarchy (BINFO_BASETYPE (binfo, i), indent + 2);