* cp-tree.h (CP_INTEGRAL_TYPE_P): New macro.
[official-gcc.git] / gcc / cp / class.c
bloba4cc5a507ace92c7035dd631819ea9bfc125b6d4
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 92-97, 1998, 1999 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
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. */
25 #include "config.h"
26 #include "system.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "flags.h"
30 #include "rtl.h"
31 #include "output.h"
32 #include "toplev.h"
33 #include "splay-tree.h"
35 #include "obstack.h"
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
39 /* This is how we tell when two virtual member functions are really the
40 same. */
41 #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
43 extern void set_class_shadows PROTO ((tree));
45 /* The number of nested classes being processed. If we are not in the
46 scope of any class, this is zero. */
48 int current_class_depth;
50 /* In order to deal with nested classes, we keep a stack of classes.
51 The topmost entry is the innermost class, and is the entry at index
52 CURRENT_CLASS_DEPTH */
54 typedef struct class_stack_node {
55 /* The name of the class. */
56 tree name;
58 /* The _TYPE node for the class. */
59 tree type;
61 /* The access specifier pending for new declarations in the scope of
62 this class. */
63 tree access;
65 /* If were defining TYPE, the names used in this class. */
66 splay_tree names_used;
67 }* class_stack_node_t;
69 /* The stack itself. This is an dynamically resized array. The
70 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
71 static int current_class_stack_size;
72 static class_stack_node_t current_class_stack;
74 /* When we're processing a member function, current_class_ptr is the
75 PARM_DECL for the `this' pointer. The current_class_ref is an
76 expression for `*this'. */
77 tree current_class_ptr, current_class_ref;
79 /* The following two can be derived from the previous one */
80 tree current_class_name; /* IDENTIFIER_NODE: name of current class */
81 tree current_class_type; /* _TYPE: the type of the current class */
82 tree current_access_specifier;
83 tree previous_class_type; /* _TYPE: the previous type that was a class */
84 tree previous_class_values; /* TREE_LIST: copy of the class_shadowed list
85 when leaving an outermost class scope. */
87 /* The obstack on which the cached class declarations are kept. */
88 static struct obstack class_cache_obstack;
89 /* The first object allocated on that obstack. We can use
90 obstack_free with tis value to free the entire obstack. */
91 char *class_cache_firstobj;
93 struct base_info;
95 static tree get_vfield_name PROTO((tree));
96 static void finish_struct_anon PROTO((tree));
97 static tree build_vbase_pointer PROTO((tree, tree));
98 static tree build_vtable_entry PROTO((tree, tree));
99 static tree get_vtable_name PROTO((tree));
100 static tree get_derived_offset PROTO((tree, tree));
101 static tree get_basefndecls PROTO((tree, tree));
102 static void set_rtti_entry PROTO((tree, tree, tree));
103 static tree build_vtable PROTO((tree, tree));
104 static void prepare_fresh_vtable PROTO((tree, tree));
105 static void fixup_vtable_deltas1 PROTO((tree, tree));
106 static void fixup_vtable_deltas PROTO((tree, int, tree));
107 static void finish_vtbls PROTO((tree, int, tree));
108 static void modify_vtable_entry PROTO((tree, tree, tree));
109 static tree get_vtable_entry_n PROTO((tree, unsigned HOST_WIDE_INT));
110 static void add_virtual_function PROTO((tree *, tree *, int *, tree, tree));
111 static tree delete_duplicate_fields_1 PROTO((tree, tree));
112 static void delete_duplicate_fields PROTO((tree));
113 static void finish_struct_bits PROTO((tree, int));
114 static int alter_access PROTO((tree, tree, tree, tree));
115 static void handle_using_decl PROTO((tree, tree, tree, tree));
116 static int overrides PROTO((tree, tree));
117 static int strictly_overrides PROTO((tree, tree));
118 static void merge_overrides PROTO((tree, tree, int, tree));
119 static void override_one_vtable PROTO((tree, tree, tree));
120 static void mark_overriders PROTO((tree, tree));
121 static void check_for_override PROTO((tree, tree));
122 static tree get_class_offset_1 PROTO((tree, tree, tree, tree, tree));
123 static tree get_class_offset PROTO((tree, tree, tree, tree));
124 static void modify_one_vtable PROTO((tree, tree, tree, tree));
125 static void modify_all_vtables PROTO((tree, tree, tree));
126 static void modify_all_direct_vtables PROTO((tree, int, tree, tree,
127 tree));
128 static void modify_all_indirect_vtables PROTO((tree, int, int, tree,
129 tree, tree));
130 static int finish_base_struct PROTO((tree, struct base_info *));
131 static void finish_struct_methods PROTO((tree));
132 static void maybe_warn_about_overly_private_class PROTO ((tree));
133 static int field_decl_cmp PROTO ((const tree *, const tree *));
134 static int method_name_cmp PROTO ((const tree *, const tree *));
135 static tree make_method_vec PROTO((int));
136 static void free_method_vec PROTO((tree));
137 static tree add_implicitly_declared_members PROTO((tree, int, int, int));
138 static tree fixed_type_or_null PROTO((tree, int *));
139 static tree resolve_address_of_overloaded_function PROTO((tree, tree, int,
140 int, tree));
141 static void build_vtable_entry_ref PROTO((tree, tree, tree));
143 /* Way of stacking language names. */
144 tree *current_lang_base, *current_lang_stack;
145 int current_lang_stacksize;
147 /* Names of languages we recognize. */
148 tree lang_name_c, lang_name_cplusplus, lang_name_java;
149 tree current_lang_name;
151 /* When layout out an aggregate type, the size of the
152 basetypes (virtual and non-virtual) is passed to layout_record
153 via this node. */
154 static tree base_layout_decl;
156 /* Constants used for access control. */
157 tree access_default_node; /* 0 */
158 tree access_public_node; /* 1 */
159 tree access_protected_node; /* 2 */
160 tree access_private_node; /* 3 */
161 tree access_default_virtual_node; /* 4 */
162 tree access_public_virtual_node; /* 5 */
163 tree access_protected_virtual_node; /* 6 */
164 tree access_private_virtual_node; /* 7 */
166 /* Variables shared between class.c and call.c. */
168 #ifdef GATHER_STATISTICS
169 int n_vtables = 0;
170 int n_vtable_entries = 0;
171 int n_vtable_searches = 0;
172 int n_vtable_elems = 0;
173 int n_convert_harshness = 0;
174 int n_compute_conversion_costs = 0;
175 int n_build_method_call = 0;
176 int n_inner_fields_searched = 0;
177 #endif
179 /* Virtual baseclass things. */
181 static tree
182 build_vbase_pointer (exp, type)
183 tree exp, type;
185 char *name;
186 FORMAT_VBASE_NAME (name, type);
188 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
191 #if 0
192 /* Is the type of the EXPR, the complete type of the object?
193 If we are going to be wrong, we must be conservative, and return 0. */
195 static int
196 complete_type_p (expr)
197 tree expr;
199 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
200 while (1)
202 switch (TREE_CODE (expr))
204 case SAVE_EXPR:
205 case INDIRECT_REF:
206 case ADDR_EXPR:
207 case NOP_EXPR:
208 case CONVERT_EXPR:
209 expr = TREE_OPERAND (expr, 0);
210 continue;
212 case CALL_EXPR:
213 if (! TREE_HAS_CONSTRUCTOR (expr))
214 break;
215 /* fall through... */
216 case VAR_DECL:
217 case FIELD_DECL:
218 if (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
219 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr)))
220 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
221 return 1;
222 /* fall through... */
223 case TARGET_EXPR:
224 case PARM_DECL:
225 if (IS_AGGR_TYPE (TREE_TYPE (expr))
226 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
227 return 1;
228 /* fall through... */
229 case PLUS_EXPR:
230 default:
231 break;
233 break;
235 return 0;
237 #endif
239 /* Build multi-level access to EXPR using hierarchy path PATH.
240 CODE is PLUS_EXPR if we are going with the grain,
241 and MINUS_EXPR if we are not (in which case, we cannot traverse
242 virtual baseclass links).
244 TYPE is the type we want this path to have on exit.
246 NONNULL is non-zero if we know (for any reason) that EXPR is
247 not, in fact, zero. */
249 tree
250 build_vbase_path (code, type, expr, path, nonnull)
251 enum tree_code code;
252 tree type, expr, path;
253 int nonnull;
255 register int changed = 0;
256 tree last = NULL_TREE, last_virtual = NULL_TREE;
257 int fixed_type_p;
258 tree null_expr = 0, nonnull_expr;
259 tree basetype;
260 tree offset = integer_zero_node;
262 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
263 return build1 (NOP_EXPR, type, expr);
265 /* If -fthis-is-variable, we might have set nonnull incorrectly. We
266 don't care enough to get this right, so just clear it. */
267 if (flag_this_is_variable > 0)
268 nonnull = 0;
270 /* We could do better if we had additional logic to convert back to the
271 unconverted type (the static type of the complete object), and then
272 convert back to the type we want. Until that is done, we only optimize
273 if the complete type is the same type as expr has. */
274 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
276 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
277 expr = save_expr (expr);
278 nonnull_expr = expr;
280 if (BINFO_INHERITANCE_CHAIN (path))
281 path = reverse_path (path);
283 basetype = BINFO_TYPE (path);
285 while (path)
287 if (TREE_VIA_VIRTUAL (path))
289 last_virtual = BINFO_TYPE (path);
290 if (code == PLUS_EXPR)
292 changed = ! fixed_type_p;
294 if (changed)
296 tree ind;
298 /* We already check for ambiguous things in the caller, just
299 find a path. */
300 if (last)
302 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
303 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
305 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
306 nonnull_expr = build_vbase_pointer (ind, last_virtual);
307 if (nonnull == 0
308 && TREE_CODE (type) == POINTER_TYPE
309 && null_expr == NULL_TREE)
311 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
312 expr = build (COND_EXPR, build_pointer_type (last_virtual),
313 build (EQ_EXPR, boolean_type_node, expr,
314 integer_zero_node),
315 null_expr, nonnull_expr);
318 /* else we'll figure out the offset below. */
320 /* Happens in the case of parse errors. */
321 if (nonnull_expr == error_mark_node)
322 return error_mark_node;
324 else
326 cp_error ("cannot cast up from virtual baseclass `%T'",
327 last_virtual);
328 return error_mark_node;
331 last = path;
332 path = BINFO_INHERITANCE_CHAIN (path);
334 /* LAST is now the last basetype assoc on the path. */
336 /* A pointer to a virtual base member of a non-null object
337 is non-null. Therefore, we only need to test for zeroness once.
338 Make EXPR the canonical expression to deal with here. */
339 if (null_expr)
341 TREE_OPERAND (expr, 2) = nonnull_expr;
342 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
343 = TREE_TYPE (nonnull_expr);
345 else
346 expr = nonnull_expr;
348 /* If we go through any virtual base pointers, make sure that
349 casts to BASETYPE from the last virtual base class use
350 the right value for BASETYPE. */
351 if (changed)
353 tree intype = TREE_TYPE (TREE_TYPE (expr));
354 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
356 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
357 offset = BINFO_OFFSET (binfo);
360 else
362 if (last_virtual)
364 offset = BINFO_OFFSET (binfo_member (last_virtual,
365 CLASSTYPE_VBASECLASSES (basetype)));
366 offset = size_binop (PLUS_EXPR, offset, BINFO_OFFSET (last));
368 else
369 offset = BINFO_OFFSET (last);
372 if (TREE_INT_CST_LOW (offset))
374 /* Bash types to make the backend happy. */
375 offset = cp_convert (type, offset);
376 #if 0
377 /* This shouldn't be necessary. (mrs) */
378 expr = build1 (NOP_EXPR, type, expr);
379 #endif
381 /* If expr might be 0, we need to preserve that zeroness. */
382 if (nonnull == 0)
384 if (null_expr)
385 TREE_TYPE (null_expr) = type;
386 else
387 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
388 if (TREE_SIDE_EFFECTS (expr))
389 expr = save_expr (expr);
391 return build (COND_EXPR, type,
392 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
393 null_expr,
394 build (code, type, expr, offset));
396 else return build (code, type, expr, offset);
399 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
400 be used multiple times in initialization of multiple inheritance. */
401 if (null_expr)
403 TREE_TYPE (expr) = type;
404 return expr;
406 else
407 return build1 (NOP_EXPR, type, expr);
410 /* Virtual function things. */
412 /* Build an entry in the virtual function table.
413 DELTA is the offset for the `this' pointer.
414 PFN is an ADDR_EXPR containing a pointer to the virtual function.
415 Note that the index (DELTA2) in the virtual function table
416 is always 0. */
418 static tree
419 build_vtable_entry (delta, pfn)
420 tree delta, pfn;
422 if (flag_vtable_thunks)
424 HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta);
425 if (idelta && ! DECL_ABSTRACT_VIRTUAL_P (TREE_OPERAND (pfn, 0)))
427 pfn = build1 (ADDR_EXPR, vtable_entry_type,
428 make_thunk (pfn, idelta));
429 TREE_READONLY (pfn) = 1;
430 TREE_CONSTANT (pfn) = 1;
432 #ifdef GATHER_STATISTICS
433 n_vtable_entries += 1;
434 #endif
435 return pfn;
437 else
439 extern int flag_huge_objects;
440 tree elems = expr_tree_cons (NULL_TREE, delta,
441 expr_tree_cons (NULL_TREE, integer_zero_node,
442 build_expr_list (NULL_TREE, pfn)));
443 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
445 /* DELTA used to be constructed by `size_int' and/or size_binop,
446 which caused overflow problems when it was negative. That should
447 be fixed now. */
449 if (! int_fits_type_p (delta, delta_type_node))
451 if (flag_huge_objects)
452 sorry ("object size exceeds built-in limit for virtual function table implementation");
453 else
454 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
457 TREE_CONSTANT (entry) = 1;
458 TREE_STATIC (entry) = 1;
459 TREE_READONLY (entry) = 1;
461 #ifdef GATHER_STATISTICS
462 n_vtable_entries += 1;
463 #endif
465 return entry;
469 /* We want to give the assembler the vtable identifier as well as
470 the offset to the function pointer. So we generate
472 __asm__ __volatile__ (".vtable_entry %c0, %c1"
473 : : "s"(&class_vtable),
474 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
476 static void
477 build_vtable_entry_ref (basetype, vtbl, idx)
478 tree basetype, vtbl, idx;
480 static char asm_stmt[] = ".vtable_entry %c0, %c1";
481 tree s, i, i2;
483 s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
484 s = build_tree_list (build_string (1, "s"), s);
486 i = build_array_ref (vtbl, idx);
487 if (!flag_vtable_thunks)
488 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
489 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
490 i2 = build_array_ref (vtbl, build_int_2(0,0));
491 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
492 i = build_binary_op (MINUS_EXPR, i, i2);
493 i = build_tree_list (build_string (1, "i"), i);
495 expand_asm_operands (build_string (sizeof(asm_stmt)-1, asm_stmt),
496 NULL_TREE, chainon (s, i), NULL_TREE, 1, NULL, 0);
499 /* Given an object INSTANCE, return an expression which yields the
500 virtual function vtable element corresponding to INDEX. There are
501 many special cases for INSTANCE which we take care of here, mainly
502 to avoid creating extra tree nodes when we don't have to. */
504 tree
505 build_vtbl_ref (instance, idx)
506 tree instance, idx;
508 tree vtbl, aref;
509 tree basetype = TREE_TYPE (instance);
511 if (TREE_CODE (basetype) == REFERENCE_TYPE)
512 basetype = TREE_TYPE (basetype);
514 if (instance == current_class_ref)
515 vtbl = build_vfield_ref (instance, basetype);
516 else
518 if (optimize)
520 /* Try to figure out what a reference refers to, and
521 access its virtual function table directly. */
522 tree ref = NULL_TREE;
524 if (TREE_CODE (instance) == INDIRECT_REF
525 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
526 ref = TREE_OPERAND (instance, 0);
527 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
528 ref = instance;
530 if (ref && TREE_CODE (ref) == VAR_DECL
531 && DECL_INITIAL (ref))
533 tree init = DECL_INITIAL (ref);
535 while (TREE_CODE (init) == NOP_EXPR
536 || TREE_CODE (init) == NON_LVALUE_EXPR)
537 init = TREE_OPERAND (init, 0);
538 if (TREE_CODE (init) == ADDR_EXPR)
540 init = TREE_OPERAND (init, 0);
541 if (IS_AGGR_TYPE (TREE_TYPE (init))
542 && (TREE_CODE (init) == PARM_DECL
543 || TREE_CODE (init) == VAR_DECL))
544 instance = init;
549 if (IS_AGGR_TYPE (TREE_TYPE (instance))
550 && (TREE_CODE (instance) == RESULT_DECL
551 || TREE_CODE (instance) == PARM_DECL
552 || TREE_CODE (instance) == VAR_DECL))
553 vtbl = TYPE_BINFO_VTABLE (basetype);
554 else
555 vtbl = build_vfield_ref (instance, basetype);
558 assemble_external (vtbl);
560 if (flag_vtable_gc)
561 build_vtable_entry_ref (basetype, vtbl, idx);
563 aref = build_array_ref (vtbl, idx);
565 return aref;
568 /* Given an object INSTANCE, return an expression which yields the
569 virtual function corresponding to INDEX. There are many special
570 cases for INSTANCE which we take care of here, mainly to avoid
571 creating extra tree nodes when we don't have to. */
573 tree
574 build_vfn_ref (ptr_to_instptr, instance, idx)
575 tree *ptr_to_instptr, instance;
576 tree idx;
578 tree aref = build_vtbl_ref (instance, idx);
580 /* When using thunks, there is no extra delta, and we get the pfn
581 directly. */
582 if (flag_vtable_thunks)
583 return aref;
585 if (ptr_to_instptr)
587 /* Save the intermediate result in a SAVE_EXPR so we don't have to
588 compute each component of the virtual function pointer twice. */
589 if (TREE_CODE (aref) == INDIRECT_REF)
590 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
592 *ptr_to_instptr
593 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
594 *ptr_to_instptr,
595 cp_convert (ptrdiff_type_node,
596 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
599 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
602 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
603 for the given TYPE. */
605 static tree
606 get_vtable_name (type)
607 tree type;
609 tree type_id = build_typename_overload (type);
610 char *buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
611 + IDENTIFIER_LENGTH (type_id) + 2);
612 const char *ptr = IDENTIFIER_POINTER (type_id);
613 int i;
614 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
615 #if 0
616 /* We don't take off the numbers; prepare_fresh_vtable uses the
617 DECL_ASSEMBLER_NAME for the type, which includes the number
618 in `3foo'. If we were to pull them off here, we'd end up with
619 something like `_vt.foo.3bar', instead of a uniform definition. */
620 while (ptr[i] >= '0' && ptr[i] <= '9')
621 i += 1;
622 #endif
623 sprintf (buf, VTABLE_NAME_FORMAT, ptr+i);
624 return get_identifier (buf);
627 /* Return the offset to the main vtable for a given base BINFO. */
629 tree
630 get_vfield_offset (binfo)
631 tree binfo;
633 tree tmp
634 = size_binop (FLOOR_DIV_EXPR,
635 DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))),
636 size_int (BITS_PER_UNIT));
637 tmp = convert (sizetype, tmp);
638 return size_binop (PLUS_EXPR, tmp, BINFO_OFFSET (binfo));
641 /* Get the offset to the start of the original binfo that we derived
642 this binfo from. If we find TYPE first, return the offset only
643 that far. The shortened search is useful because the this pointer
644 on method calling is expected to point to a DECL_CONTEXT (fndecl)
645 object, and not a baseclass of it. */
647 static tree
648 get_derived_offset (binfo, type)
649 tree binfo, type;
651 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
652 tree offset2;
653 int i;
654 while (BINFO_BASETYPES (binfo)
655 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
657 tree binfos = BINFO_BASETYPES (binfo);
658 if (BINFO_TYPE (binfo) == type)
659 break;
660 binfo = TREE_VEC_ELT (binfos, i);
662 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
663 return size_binop (MINUS_EXPR, offset1, offset2);
666 /* Update the rtti info for this class. */
668 static void
669 set_rtti_entry (virtuals, offset, type)
670 tree virtuals, offset, type;
672 tree vfn;
674 if (CLASSTYPE_COM_INTERFACE (type))
675 return;
677 if (flag_rtti)
678 vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, get_tinfo_fn (type));
679 else
680 vfn = build1 (NOP_EXPR, vfunc_ptr_type_node, size_zero_node);
681 TREE_CONSTANT (vfn) = 1;
683 if (! flag_vtable_thunks)
684 TREE_VALUE (virtuals) = build_vtable_entry (offset, vfn);
685 else
687 tree voff = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
688 TREE_CONSTANT (voff) = 1;
690 TREE_VALUE (virtuals) = build_vtable_entry (integer_zero_node, voff);
692 /* The second slot is for the tdesc pointer when thunks are used. */
693 TREE_VALUE (TREE_CHAIN (virtuals))
694 = build_vtable_entry (integer_zero_node, vfn);
698 /* Build a virtual function for type TYPE.
699 If BINFO is non-NULL, build the vtable starting with the initial
700 approximation that it is the same as the one which is the head of
701 the association list. */
703 static tree
704 build_vtable (binfo, type)
705 tree binfo, type;
707 tree name = get_vtable_name (type);
708 tree virtuals, decl;
710 if (binfo)
712 tree offset;
714 virtuals = copy_list (BINFO_VIRTUALS (binfo));
715 decl = build_lang_decl (VAR_DECL, name, TREE_TYPE (BINFO_VTABLE (binfo)));
717 /* Now do rtti stuff. */
718 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
719 offset = ssize_binop (MINUS_EXPR, integer_zero_node, offset);
720 set_rtti_entry (virtuals, offset, type);
722 else
724 virtuals = NULL_TREE;
725 decl = build_lang_decl (VAR_DECL, name, void_type_node);
728 #ifdef GATHER_STATISTICS
729 n_vtables += 1;
730 n_vtable_elems += list_length (virtuals);
731 #endif
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;
748 #endif
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),
752 DECL_ALIGN (decl));
754 DECL_VIRTUAL_P (decl) = 1;
755 DECL_CONTEXT (decl) = type;
757 binfo = TYPE_BINFO (type);
758 SET_BINFO_NEW_VTABLE_MARKED (binfo);
759 return decl;
762 extern tree signed_size_zero_node;
764 /* Give TYPE a new virtual function table which is initialized
765 with a skeleton-copy of its original initialization. The only
766 entry that changes is the `delta' entry, so we can really
767 share a lot of structure.
769 FOR_TYPE is the derived type which caused this table to
770 be needed.
772 BINFO is the type association which provided TYPE for FOR_TYPE.
774 The order in which vtables are built (by calling this function) for
775 an object must remain the same, otherwise a binary incompatibility
776 can result. */
778 static void
779 prepare_fresh_vtable (binfo, for_type)
780 tree binfo, for_type;
782 tree basetype;
783 tree orig_decl = BINFO_VTABLE (binfo);
784 tree name;
785 tree new_decl;
786 tree offset;
787 tree path = binfo;
788 char *buf, *buf2;
789 char joiner = '_';
790 int i;
792 #ifdef JOINER
793 joiner = JOINER;
794 #endif
796 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
798 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
799 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
801 /* We know that the vtable that we are going to create doesn't exist
802 yet in the global namespace, and when we finish, it will be
803 pushed into the global namespace. In complex MI hierarchies, we
804 have to loop while the name we are thinking of adding is globally
805 defined, adding more name components to the vtable name as we
806 loop, until the name is unique. This is because in complex MI
807 cases, we might have the same base more than once. This means
808 that the order in which this function is called for vtables must
809 remain the same, otherwise binary compatibility can be
810 compromised. */
812 while (1)
814 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
815 + 1 + i);
816 char *new_buf2;
818 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
819 buf2);
820 buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT) + strlen (buf1) + 1);
821 sprintf (buf, VTABLE_NAME_FORMAT, buf1);
822 name = get_identifier (buf);
824 /* If this name doesn't clash, then we can use it, otherwise
825 we add more to the name until it is unique. */
827 if (! IDENTIFIER_GLOBAL_VALUE (name))
828 break;
830 /* Set values for next loop through, if the name isn't unique. */
832 path = BINFO_INHERITANCE_CHAIN (path);
834 /* We better not run out of stuff to make it unique. */
835 my_friendly_assert (path != NULL_TREE, 368);
837 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
839 if (for_type == basetype)
841 /* If we run out of basetypes in the path, we have already
842 found created a vtable with that name before, we now
843 resort to tacking on _%d to distinguish them. */
844 int j = 2;
845 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
846 buf1 = (char *) alloca (i);
847 do {
848 sprintf (buf1, "%s%c%s%c%d",
849 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
850 buf2, joiner, j);
851 buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
852 + strlen (buf1) + 1);
853 sprintf (buf, VTABLE_NAME_FORMAT, buf1);
854 name = get_identifier (buf);
856 /* If this name doesn't clash, then we can use it,
857 otherwise we add something different to the name until
858 it is unique. */
859 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
861 /* Hey, they really like MI don't they? Increase the 3
862 above to 6, and the 999 to 999999. :-) */
863 my_friendly_assert (j <= 999, 369);
865 break;
868 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
869 new_buf2 = (char *) alloca (i);
870 sprintf (new_buf2, "%s%c%s",
871 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
872 buf2 = new_buf2;
875 new_decl = build_lang_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
876 /* Remember which class this vtable is really for. */
877 DECL_CONTEXT (new_decl) = for_type;
879 DECL_ARTIFICIAL (new_decl) = 1;
880 TREE_STATIC (new_decl) = 1;
881 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
882 DECL_VIRTUAL_P (new_decl) = 1;
883 #ifndef WRITABLE_VTABLES
884 /* Make them READONLY by default. (mrs) */
885 TREE_READONLY (new_decl) = 1;
886 #endif
887 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
889 /* Make fresh virtual list, so we can smash it later. */
890 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
892 if (TREE_VIA_VIRTUAL (binfo))
894 tree binfo1 = binfo_member (BINFO_TYPE (binfo),
895 CLASSTYPE_VBASECLASSES (for_type));
897 /* XXX - This should never happen, if it does, the caller should
898 ensure that the binfo is from for_type's binfos, not from any
899 base type's. We can remove all this code after a while. */
900 if (binfo1 != binfo)
901 warning ("internal inconsistency: binfo offset error for rtti");
903 offset = BINFO_OFFSET (binfo1);
905 else
906 offset = BINFO_OFFSET (binfo);
908 set_rtti_entry (BINFO_VIRTUALS (binfo),
909 ssize_binop (MINUS_EXPR, integer_zero_node, offset),
910 for_type);
912 #ifdef GATHER_STATISTICS
913 n_vtables += 1;
914 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
915 #endif
917 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
918 import_export_vtable (new_decl, for_type, 0);
920 if (TREE_VIA_VIRTUAL (binfo))
921 my_friendly_assert (binfo == binfo_member (BINFO_TYPE (binfo),
922 CLASSTYPE_VBASECLASSES (current_class_type)),
923 170);
924 SET_BINFO_NEW_VTABLE_MARKED (binfo);
927 #if 0
928 /* Access the virtual function table entry that logically
929 contains BASE_FNDECL. VIRTUALS is the virtual function table's
930 initializer. We can run off the end, when dealing with virtual
931 destructors in MI situations, return NULL_TREE in that case. */
933 static tree
934 get_vtable_entry (virtuals, base_fndecl)
935 tree virtuals, base_fndecl;
937 unsigned HOST_WIDE_INT n = (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
938 ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
939 & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1))
940 : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
942 #ifdef GATHER_STATISTICS
943 n_vtable_searches += n;
944 #endif
946 while (n > 0 && virtuals)
948 --n;
949 virtuals = TREE_CHAIN (virtuals);
951 return virtuals;
953 #endif
955 /* Put new entry ENTRY into virtual function table initializer
956 VIRTUALS.
958 Also update DECL_VINDEX (FNDECL). */
960 static void
961 modify_vtable_entry (old_entry_in_list, new_entry, fndecl)
962 tree old_entry_in_list, new_entry, fndecl;
964 tree base_fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list)), 0);
966 #ifdef NOTQUITE
967 cp_warning ("replaced %D with %D", DECL_ASSEMBLER_NAME (base_fndecl),
968 DECL_ASSEMBLER_NAME (fndecl));
969 #endif
970 TREE_VALUE (old_entry_in_list) = new_entry;
972 /* Now assign virtual dispatch information, if unset. */
973 /* We can dispatch this, through any overridden base function. */
974 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
976 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
977 DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
981 /* Access the virtual function table entry N. VIRTUALS is the virtual
982 function table's initializer. */
984 static tree
985 get_vtable_entry_n (virtuals, n)
986 tree virtuals;
987 unsigned HOST_WIDE_INT n;
989 while (n > 0)
991 --n;
992 virtuals = TREE_CHAIN (virtuals);
994 return virtuals;
997 /* Add a virtual function to all the appropriate vtables for the class
998 T. DECL_VINDEX(X) should be error_mark_node, if we want to
999 allocate a new slot in our table. If it is error_mark_node, we
1000 know that no other function from another vtable is overridden by X.
1001 HAS_VIRTUAL keeps track of how many virtuals there are in our main
1002 vtable for the type, and we build upon the PENDING_VIRTUALS list
1003 and return it. */
1005 static void
1006 add_virtual_function (pv, phv, has_virtual, fndecl, t)
1007 tree *pv, *phv;
1008 int *has_virtual;
1009 tree fndecl;
1010 tree t; /* Structure type. */
1012 tree pending_virtuals = *pv;
1013 tree pending_hard_virtuals = *phv;
1015 /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
1016 convert to void *. Make such a conversion here. */
1017 tree vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
1018 TREE_CONSTANT (vfn) = 1;
1020 #ifndef DUMB_USER
1021 if (current_class_type == 0)
1022 cp_warning ("internal problem, current_class_type is zero when adding `%D', please report",
1023 fndecl);
1024 if (current_class_type && t != current_class_type)
1025 cp_warning ("internal problem, current_class_type differs when adding `%D', please report",
1026 fndecl);
1027 #endif
1029 /* If the virtual function is a redefinition of a prior one,
1030 figure out in which base class the new definition goes,
1031 and if necessary, make a fresh virtual function table
1032 to hold that entry. */
1033 if (DECL_VINDEX (fndecl) == error_mark_node)
1035 tree entry;
1037 /* We remember that this was the base sub-object for rtti. */
1038 CLASSTYPE_RTTI (t) = t;
1040 /* If we are using thunks, use two slots at the front, one
1041 for the offset pointer, one for the tdesc pointer.
1042 For ARM-style vtables, use the same slot for both. */
1043 if (*has_virtual == 0 && ! CLASSTYPE_COM_INTERFACE (t))
1045 if (flag_vtable_thunks)
1046 *has_virtual = 2;
1047 else
1048 *has_virtual = 1;
1051 /* Build a new INT_CST for this DECL_VINDEX. */
1053 static tree index_table[256];
1054 tree idx;
1055 /* We skip a slot for the offset/tdesc entry. */
1056 int i = (*has_virtual)++;
1058 if (i >= 256 || index_table[i] == 0)
1060 idx = build_int_2 (i, 0);
1061 if (i < 256)
1062 index_table[i] = idx;
1064 else
1065 idx = index_table[i];
1067 /* Now assign virtual dispatch information. */
1068 DECL_VINDEX (fndecl) = idx;
1069 DECL_CONTEXT (fndecl) = t;
1071 entry = build_vtable_entry (integer_zero_node, vfn);
1072 pending_virtuals = tree_cons (DECL_VINDEX (fndecl), entry, pending_virtuals);
1074 /* Might already be INTEGER_CST if declared twice in class. We will
1075 give error later or we've already given it. */
1076 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1078 /* Need an entry in some other virtual function table.
1079 Deal with this after we have laid out our virtual base classes. */
1080 pending_hard_virtuals = temp_tree_cons (fndecl, vfn, pending_hard_virtuals);
1082 *pv = pending_virtuals;
1083 *phv = pending_hard_virtuals;
1086 /* Obstack on which to build the vector of class methods. */
1087 struct obstack class_obstack;
1088 extern struct obstack *current_obstack;
1090 /* These are method vectors that were too small for the number of
1091 methods in some class, and so were abandoned. */
1092 static tree free_method_vecs;
1094 /* Returns a method vector with enough room for N methods. N should
1095 be a power of two. */
1097 static tree
1098 make_method_vec (n)
1099 int n;
1101 tree new_vec;
1102 tree* t;
1104 for (t = &free_method_vecs; *t; t = &(TREE_CHAIN (*t)))
1105 /* Note that we don't use >= n here because we don't want to
1106 allocate a very large vector where it isn't needed. */
1107 if (TREE_VEC_LENGTH (*t) == n)
1109 new_vec = *t;
1110 *t = TREE_CHAIN (new_vec);
1111 TREE_CHAIN (new_vec) = NULL_TREE;
1112 bzero ((PTR) &TREE_VEC_ELT (new_vec, 0), n * sizeof (tree));
1113 return new_vec;
1116 new_vec = make_tree_vec (n);
1117 return new_vec;
1120 /* Free the method vector VEC. */
1122 static void
1123 free_method_vec (vec)
1124 tree vec;
1126 TREE_CHAIN (vec) = free_method_vecs;
1127 free_method_vecs = vec;
1130 /* Add method METHOD to class TYPE.
1132 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1133 the class type where the method should be added. */
1135 void
1136 add_method (type, fields, method)
1137 tree type, *fields, method;
1139 push_obstacks_nochange ();
1140 end_temporary_allocation ();
1142 /* Setting the DECL_CONTEXT and DECL_CLASS_CONTEXT here is probably
1143 redundant. */
1144 DECL_CONTEXT (method) = type;
1145 DECL_CLASS_CONTEXT (method) = type;
1147 if (fields && *fields)
1148 *fields = build_overload (method, *fields);
1149 else
1151 int len;
1152 int slot;
1153 tree method_vec;
1155 if (!CLASSTYPE_METHOD_VEC (type))
1156 /* Make a new method vector. We start with 8 entries. We must
1157 allocate at least two (for constructors and destructors), and
1158 we're going to end up with an assignment operator at some
1159 point as well.
1161 We could use a TREE_LIST for now, and convert it to a
1162 TREE_VEC in finish_struct, but we would probably waste more
1163 memory making the links in the list than we would by
1164 over-allocating the size of the vector here. Furthermore,
1165 we would complicate all the code that expects this to be a
1166 vector. We keep a free list of vectors that we outgrew so
1167 that we don't really waste any memory. */
1168 CLASSTYPE_METHOD_VEC (type) = make_method_vec (8);
1170 method_vec = CLASSTYPE_METHOD_VEC (type);
1171 len = TREE_VEC_LENGTH (method_vec);
1173 if (DECL_NAME (method) == constructor_name (type))
1174 /* A new constructor or destructor. Constructors go in
1175 slot 0; destructors go in slot 1. */
1176 slot = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
1177 else
1179 /* See if we already have an entry with this name. */
1180 for (slot = 2; slot < len; ++slot)
1181 if (!TREE_VEC_ELT (method_vec, slot)
1182 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1183 slot)))
1184 == DECL_NAME (method)))
1185 break;
1187 if (slot == len)
1189 /* We need a bigger method vector. */
1190 tree new_vec = make_method_vec (2 * len);
1191 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1192 (PTR) &TREE_VEC_ELT (new_vec, 0),
1193 len * sizeof (tree));
1194 free_method_vec (method_vec);
1195 len = 2 * len;
1196 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1199 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1201 /* Type conversion operators have to come before
1202 ordinary methods; add_conversions depends on this to
1203 speed up looking for conversion operators. So, if
1204 necessary, we slide some of the vector elements up.
1205 In theory, this makes this algorithm O(N^2) but we
1206 don't expect many conversion operators. */
1207 for (slot = 2; slot < len; ++slot)
1209 tree fn = TREE_VEC_ELT (method_vec, slot);
1211 if (!fn)
1212 /* There are no more entries in the vector, so we
1213 can insert the new conversion operator here. */
1214 break;
1216 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1217 /* We can insert the new function right at the
1218 SLOTth position. */
1219 break;
1222 if (!TREE_VEC_ELT (method_vec, slot))
1223 /* There is nothing in the Ith slot, so we can avoid
1224 moving anything. */
1226 else
1228 /* We know the last slot in the vector is empty
1229 because we know that at this point there's room
1230 for a new function. */
1231 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1232 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1233 (len - slot - 1) * sizeof (tree));
1234 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1239 if (template_class_depth (type))
1240 /* TYPE is a template class. Don't issue any errors now; wait
1241 until instantiation time to complain. */
1243 else
1245 tree fns;
1247 /* Check to see if we've already got this method. */
1248 for (fns = TREE_VEC_ELT (method_vec, slot);
1249 fns;
1250 fns = OVL_NEXT (fns))
1252 tree fn = OVL_CURRENT (fns);
1254 if (TREE_CODE (fn) != TREE_CODE (method))
1255 continue;
1257 if (TREE_CODE (method) != TEMPLATE_DECL)
1259 /* [over.load] Member function declarations with the
1260 same name and the same parameter types cannot be
1261 overloaded if any of them is a static member
1262 function declaration. */
1263 if (DECL_STATIC_FUNCTION_P (fn)
1264 != DECL_STATIC_FUNCTION_P (method))
1266 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1267 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1269 if (! DECL_STATIC_FUNCTION_P (fn))
1270 parms1 = TREE_CHAIN (parms1);
1271 else
1272 parms2 = TREE_CHAIN (parms2);
1274 if (compparms (parms1, parms2))
1275 cp_error ("`%#D' and `%#D' cannot be overloaded",
1276 fn, method);
1279 /* Since this is an ordinary function in a
1280 non-template class, it's mangled name can be used
1281 as a unique identifier. This technique is only
1282 an optimization; we would get the same results if
1283 we just used decls_match here. */
1284 if (DECL_ASSEMBLER_NAME (fn)
1285 != DECL_ASSEMBLER_NAME (method))
1286 continue;
1288 else if (!decls_match (fn, method))
1289 continue;
1291 /* There has already been a declaration of this method
1292 or member template. */
1293 cp_error_at ("`%D' has already been declared in `%T'",
1294 method, type);
1296 /* We don't call duplicate_decls here to merge the
1297 declarations because that will confuse things if the
1298 methods have inline definitions. In particular, we
1299 will crash while processing the definitions. */
1300 return;
1304 /* Actually insert the new method. */
1305 TREE_VEC_ELT (method_vec, slot)
1306 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1308 /* Add the new binding. */
1309 if (!DECL_CONSTRUCTOR_P (method)
1310 && !DECL_DESTRUCTOR_P (method))
1311 push_class_level_binding (DECL_NAME (method),
1312 TREE_VEC_ELT (method_vec, slot));
1314 pop_obstacks ();
1317 /* Subroutines of finish_struct. */
1319 /* Look through the list of fields for this struct, deleting
1320 duplicates as we go. This must be recursive to handle
1321 anonymous unions.
1323 FIELD is the field which may not appear anywhere in FIELDS.
1324 FIELD_PTR, if non-null, is the starting point at which
1325 chained deletions may take place.
1326 The value returned is the first acceptable entry found
1327 in FIELDS.
1329 Note that anonymous fields which are not of UNION_TYPE are
1330 not duplicates, they are just anonymous fields. This happens
1331 when we have unnamed bitfields, for example. */
1333 static tree
1334 delete_duplicate_fields_1 (field, fields)
1335 tree field, fields;
1337 tree x;
1338 tree prev = 0;
1339 if (DECL_NAME (field) == 0)
1341 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1342 return fields;
1344 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1345 fields = delete_duplicate_fields_1 (x, fields);
1346 return fields;
1348 else
1350 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1352 if (DECL_NAME (x) == 0)
1354 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1355 continue;
1356 TYPE_FIELDS (TREE_TYPE (x))
1357 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1358 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1360 if (prev == 0)
1361 fields = TREE_CHAIN (fields);
1362 else
1363 TREE_CHAIN (prev) = TREE_CHAIN (x);
1366 else
1368 if (DECL_NAME (field) == DECL_NAME (x))
1370 if (TREE_CODE (field) == CONST_DECL
1371 && TREE_CODE (x) == CONST_DECL)
1372 cp_error_at ("duplicate enum value `%D'", x);
1373 else if (TREE_CODE (field) == CONST_DECL
1374 || TREE_CODE (x) == CONST_DECL)
1375 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1377 else if (DECL_DECLARES_TYPE_P (field)
1378 && DECL_DECLARES_TYPE_P (x))
1380 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1381 continue;
1382 cp_error_at ("duplicate nested type `%D'", x);
1384 else if (DECL_DECLARES_TYPE_P (field)
1385 || DECL_DECLARES_TYPE_P (x))
1387 /* Hide tag decls. */
1388 if ((TREE_CODE (field) == TYPE_DECL
1389 && DECL_ARTIFICIAL (field))
1390 || (TREE_CODE (x) == TYPE_DECL
1391 && DECL_ARTIFICIAL (x)))
1392 continue;
1393 cp_error_at ("duplicate field `%D' (as type and non-type)",
1396 else
1397 cp_error_at ("duplicate member `%D'", x);
1398 if (prev == 0)
1399 fields = TREE_CHAIN (fields);
1400 else
1401 TREE_CHAIN (prev) = TREE_CHAIN (x);
1406 return fields;
1409 static void
1410 delete_duplicate_fields (fields)
1411 tree fields;
1413 tree x;
1414 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1415 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1418 /* Change the access of FDECL to ACCESS in T. The access to FDECL is
1419 along the path given by BINFO. Return 1 if change was legit,
1420 otherwise return 0. */
1422 static int
1423 alter_access (t, binfo, fdecl, access)
1424 tree t;
1425 tree binfo;
1426 tree fdecl;
1427 tree access;
1429 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1430 if (elem)
1432 if (TREE_VALUE (elem) != access)
1434 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1435 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1436 else
1437 error ("conflicting access specifications for field `%s', ignored",
1438 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1440 else
1442 /* They're changing the access to the same thing they changed
1443 it to before. That's OK. */
1447 else
1449 enforce_access (binfo, fdecl);
1450 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1451 return 1;
1453 return 0;
1456 /* Process the USING_DECL, which is a member of T. The METHOD_VEC, if
1457 non-NULL, is the methods of T. The FIELDS are the fields of T. */
1459 static void
1460 handle_using_decl (using_decl, t, method_vec, fields)
1461 tree using_decl;
1462 tree t;
1463 tree method_vec;
1464 tree fields;
1466 tree ctype = DECL_INITIAL (using_decl);
1467 tree name = DECL_NAME (using_decl);
1468 tree access
1469 = TREE_PRIVATE (using_decl) ? access_private_node
1470 : TREE_PROTECTED (using_decl) ? access_protected_node
1471 : access_public_node;
1472 tree fdecl, binfo;
1473 tree flist = NULL_TREE;
1474 tree tmp;
1475 int i;
1476 int n_methods;
1478 binfo = binfo_or_else (ctype, t);
1479 if (! binfo)
1480 return;
1482 if (name == constructor_name (ctype)
1483 || name == constructor_name_full (ctype))
1485 cp_error_at ("using-declaration for constructor", using_decl);
1486 return;
1489 fdecl = lookup_member (binfo, name, 0, 0);
1491 if (!fdecl)
1493 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1494 return;
1497 /* Functions are represented as TREE_LIST, with the purpose
1498 being the type and the value the functions. Other members
1499 come as themselves. */
1500 if (TREE_CODE (fdecl) == TREE_LIST)
1501 /* Ignore base type this came from. */
1502 fdecl = TREE_VALUE (fdecl);
1504 if (TREE_CODE (fdecl) == OVERLOAD)
1506 /* We later iterate over all functions. */
1507 flist = fdecl;
1508 fdecl = OVL_FUNCTION (flist);
1511 name = DECL_NAME (fdecl);
1512 n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
1513 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); i++)
1514 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)))
1515 == name)
1517 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1518 cp_error_at (" because of local method `%#D' with same name",
1519 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
1520 return;
1523 if (! DECL_LANG_SPECIFIC (fdecl))
1524 /* We don't currently handle DECL_ACCESS for TYPE_DECLs; just return. */
1525 return;
1527 for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
1528 if (DECL_NAME (tmp) == name)
1530 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1531 cp_error_at (" because of local field `%#D' with same name", tmp);
1532 return;
1535 /* Make type T see field decl FDECL with access ACCESS.*/
1536 if (flist)
1538 while (flist)
1540 if (alter_access (t, binfo, OVL_FUNCTION (flist),
1541 access) == 0)
1542 return;
1543 flist = OVL_CHAIN (flist);
1546 else
1547 alter_access (t, binfo, fdecl, access);
1550 struct base_info
1552 int has_virtual;
1553 int max_has_virtual;
1554 tree vfield;
1555 tree vfields;
1556 tree rtti;
1557 char cant_have_default_ctor;
1558 char cant_have_const_ctor;
1559 char no_const_asn_ref;
1562 /* Record information about type T derived from its base classes.
1563 Store most of that information in T itself, and place the
1564 remaining information in the struct BASE_INFO.
1566 Propagate basetype offsets throughout the lattice. Note that the
1567 lattice topped by T is really a pair: it's a DAG that gives the
1568 structure of the derivation hierarchy, and it's a list of the
1569 virtual baseclasses that appear anywhere in the DAG. When a vbase
1570 type appears in the DAG, it's offset is 0, and it's children start
1571 their offsets from that point. When a vbase type appears in the list,
1572 its offset is the offset it has in the hierarchy, and its children's
1573 offsets include that offset in theirs.
1575 Returns the index of the first base class to have virtual functions,
1576 or -1 if no such base class. */
1578 static int
1579 finish_base_struct (t, b)
1580 tree t;
1581 struct base_info *b;
1583 tree binfos = TYPE_BINFO_BASETYPES (t);
1584 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1585 int first_vfn_base_index = -1;
1586 bzero ((char *) b, sizeof (struct base_info));
1588 for (i = 0; i < n_baseclasses; i++)
1590 tree base_binfo = TREE_VEC_ELT (binfos, i);
1591 tree basetype = BINFO_TYPE (base_binfo);
1593 /* Effective C++ rule 14. We only need to check TYPE_VIRTUAL_P
1594 here because the case of virtual functions but non-virtual
1595 dtor is handled in finish_struct_1. */
1596 if (warn_ecpp && ! TYPE_VIRTUAL_P (basetype)
1597 && TYPE_HAS_DESTRUCTOR (basetype))
1598 cp_warning ("base class `%#T' has a non-virtual destructor", basetype);
1600 /* If the type of basetype is incomplete, then
1601 we already complained about that fact
1602 (and we should have fixed it up as well). */
1603 if (TYPE_SIZE (basetype) == 0)
1605 int j;
1606 /* The base type is of incomplete type. It is
1607 probably best to pretend that it does not
1608 exist. */
1609 if (i == n_baseclasses-1)
1610 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1611 TREE_VEC_LENGTH (binfos) -= 1;
1612 n_baseclasses -= 1;
1613 for (j = i; j+1 < n_baseclasses; j++)
1614 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1617 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1618 b->cant_have_const_ctor = 1;
1620 if (TYPE_HAS_CONSTRUCTOR (basetype)
1621 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1623 b->cant_have_default_ctor = 1;
1624 if (! TYPE_HAS_CONSTRUCTOR (t))
1626 cp_pedwarn ("base `%T' with only non-default constructor",
1627 basetype);
1628 cp_pedwarn ("in class without a constructor");
1632 if (TYPE_HAS_ASSIGN_REF (basetype)
1633 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1634 b->no_const_asn_ref = 1;
1636 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1637 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1638 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1639 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1641 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1642 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1643 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1645 if (CLASSTYPE_COM_INTERFACE (basetype))
1647 CLASSTYPE_COM_INTERFACE (t) = 1;
1648 if (i > 0)
1649 cp_error
1650 ("COM interface type `%T' must be the leftmost base class",
1651 basetype);
1653 else if (CLASSTYPE_COM_INTERFACE (t) && i == 0)
1655 cp_error ("COM interface type `%T' with non-COM base class `%T'",
1656 t, basetype);
1657 CLASSTYPE_COM_INTERFACE (t) = 0;
1660 if (TYPE_VIRTUAL_P (basetype))
1662 /* Ensure that this is set from at least a virtual base
1663 class. */
1664 if (b->rtti == NULL_TREE)
1665 b->rtti = CLASSTYPE_RTTI (basetype);
1667 /* Don't borrow virtuals from virtual baseclasses. */
1668 if (TREE_VIA_VIRTUAL (base_binfo))
1669 continue;
1671 if (first_vfn_base_index < 0)
1673 tree vfields;
1674 first_vfn_base_index = i;
1676 /* Update these two, now that we know what vtable we are
1677 going to extend. This is so that we can add virtual
1678 functions, and override them properly. */
1679 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1680 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1681 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1682 b->vfield = CLASSTYPE_VFIELD (basetype);
1683 b->vfields = copy_list (CLASSTYPE_VFIELDS (basetype));
1684 vfields = b->vfields;
1685 while (vfields)
1687 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1688 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1690 tree value = VF_BASETYPE_VALUE (vfields);
1691 if (DECL_NAME (CLASSTYPE_VFIELD (value))
1692 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1693 VF_NORMAL_VALUE (b->vfields) = basetype;
1694 else
1695 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1697 vfields = TREE_CHAIN (vfields);
1699 CLASSTYPE_VFIELD (t) = b->vfield;
1701 else
1703 /* Only add unique vfields, and flatten them out as we go. */
1704 tree vfields = CLASSTYPE_VFIELDS (basetype);
1705 while (vfields)
1707 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1708 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1710 tree value = VF_BASETYPE_VALUE (vfields);
1711 b->vfields = tree_cons (base_binfo, value, b->vfields);
1712 if (DECL_NAME (CLASSTYPE_VFIELD (value))
1713 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1714 VF_NORMAL_VALUE (b->vfields) = basetype;
1715 else
1716 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1718 vfields = TREE_CHAIN (vfields);
1721 if (b->has_virtual == 0)
1723 first_vfn_base_index = i;
1725 /* Update these two, now that we know what vtable we are
1726 going to extend. This is so that we can add virtual
1727 functions, and override them properly. */
1728 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1729 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1730 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1731 b->vfield = CLASSTYPE_VFIELD (basetype);
1732 CLASSTYPE_VFIELD (t) = b->vfield;
1733 /* When we install the first one, set the VF_NORMAL_VALUE
1734 to be the current class, as this it is the most derived
1735 class. Hopefully, this is not set to something else
1736 later. (mrs) */
1737 vfields = b->vfields;
1738 while (vfields)
1740 if (DECL_NAME (CLASSTYPE_VFIELD (t))
1741 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1743 VF_NORMAL_VALUE (vfields) = t;
1744 /* There should only be one of them! And it should
1745 always be found, if we get into here. (mrs) */
1746 break;
1748 vfields = TREE_CHAIN (vfields);
1756 tree vfields;
1757 /* Find the base class with the largest number of virtual functions. */
1758 for (vfields = b->vfields; vfields; vfields = TREE_CHAIN (vfields))
1760 if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)) > b->max_has_virtual)
1761 b->max_has_virtual = CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields));
1762 if (VF_DERIVED_VALUE (vfields)
1763 && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)) > b->max_has_virtual)
1764 b->max_has_virtual = CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields));
1768 if (b->vfield == 0)
1769 /* If all virtual functions come only from virtual baseclasses. */
1770 return -1;
1772 /* Update the rtti base if we have a non-virtual base class version
1773 of it. */
1774 b->rtti = CLASSTYPE_RTTI (BINFO_TYPE (TREE_VEC_ELT (binfos, first_vfn_base_index)));
1776 return first_vfn_base_index;
1779 /* Set memoizing fields and bits of T (and its variants) for later use.
1780 MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables. */
1782 static void
1783 finish_struct_bits (t, max_has_virtual)
1784 tree t;
1785 int max_has_virtual;
1787 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1789 /* Fix up variants (if any). */
1790 tree variants = TYPE_NEXT_VARIANT (t);
1791 while (variants)
1793 /* These fields are in the _TYPE part of the node, not in
1794 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1795 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1796 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1797 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1798 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
1800 TYPE_USES_COMPLEX_INHERITANCE (variants) = TYPE_USES_COMPLEX_INHERITANCE (t);
1801 TYPE_VIRTUAL_P (variants) = TYPE_VIRTUAL_P (t);
1802 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1803 /* Copy whatever these are holding today. */
1804 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1805 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1806 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1807 TYPE_SIZE (variants) = TYPE_SIZE (t);
1808 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1809 variants = TYPE_NEXT_VARIANT (variants);
1812 if (n_baseclasses && max_has_virtual)
1814 /* For a class w/o baseclasses, `finish_struct' has set
1815 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by definition). Similarly
1816 for a class who's base classes do not have vtables. When neither
1817 of these is true, we might have removed abstract virtuals (by
1818 providing a definition), added some (by declaring new ones), or
1819 redeclared ones from a base class. We need to recalculate what's
1820 really an abstract virtual at this point (by looking in the
1821 vtables). */
1822 CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
1825 if (n_baseclasses)
1827 /* Notice whether this class has type conversion functions defined. */
1828 tree binfo = TYPE_BINFO (t);
1829 tree binfos = BINFO_BASETYPES (binfo);
1830 tree basetype;
1832 for (i = n_baseclasses-1; i >= 0; i--)
1834 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1836 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1840 /* If this type has a copy constructor, force its mode to be BLKmode, and
1841 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1842 be passed by invisible reference and prevent it from being returned in
1843 a register.
1845 Also do this if the class has BLKmode but can still be returned in
1846 registers, since function_cannot_inline_p won't let us inline
1847 functions returning such a type. This affects the HP-PA. */
1848 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1849 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1850 && CLASSTYPE_NON_AGGREGATE (t)))
1852 tree variants;
1853 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1854 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1856 TYPE_MODE (variants) = BLKmode;
1857 TREE_ADDRESSABLE (variants) = 1;
1862 /* Issue warnings about T having private constructors, but no friends,
1863 and so forth.
1865 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1866 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1867 non-private static member functions. */
1869 static void
1870 maybe_warn_about_overly_private_class (t)
1871 tree t;
1873 int has_member_fn = 0;
1874 int has_nonprivate_method = 0;
1875 tree fn;
1877 if (!warn_ctor_dtor_privacy
1878 /* If the class has friends, those entities might create and
1879 access instances, so we should not warn. */
1880 || (CLASSTYPE_FRIEND_CLASSES (t)
1881 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1882 /* We will have warned when the template was declared; there's
1883 no need to warn on every instantiation. */
1884 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1885 /* There's no reason to even consider warning about this
1886 class. */
1887 return;
1889 /* We only issue one warning, if more than one applies, because
1890 otherwise, on code like:
1892 class A {
1893 // Oops - forgot `public:'
1894 A();
1895 A(const A&);
1896 ~A();
1899 we warn several times about essentially the same problem. */
1901 /* Check to see if all (non-constructor, non-destructor) member
1902 functions are private. (Since there are no friends or
1903 non-private statics, we can't ever call any of the private member
1904 functions.) */
1905 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1906 /* We're not interested in compiler-generated methods; they don't
1907 provide any way to call private members. */
1908 if (!DECL_ARTIFICIAL (fn))
1910 if (!TREE_PRIVATE (fn))
1912 if (DECL_STATIC_FUNCTION_P (fn))
1913 /* A non-private static member function is just like a
1914 friend; it can create and invoke private member
1915 functions, and be accessed without a class
1916 instance. */
1917 return;
1919 has_nonprivate_method = 1;
1920 break;
1922 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1923 has_member_fn = 1;
1926 if (!has_nonprivate_method && has_member_fn)
1928 /* There are no non-private methods, and there's at least one
1929 private member function that isn't a constructor or
1930 destructor. (If all the private members are
1931 constructors/destructors we want to use the code below that
1932 issues error messages specifically referring to
1933 constructors/destructors.) */
1934 int i;
1935 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1936 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1937 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1938 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1940 has_nonprivate_method = 1;
1941 break;
1943 if (!has_nonprivate_method)
1945 cp_warning ("all member functions in class `%T' are private", t);
1946 return;
1950 /* Even if some of the member functions are non-private, the class
1951 won't be useful for much if all the constructors or destructors
1952 are private: such an object can never be created or destroyed. */
1953 if (TYPE_HAS_DESTRUCTOR (t))
1955 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
1957 if (TREE_PRIVATE (dtor))
1959 cp_warning ("`%#T' only defines a private destructor and has no friends",
1961 return;
1965 if (TYPE_HAS_CONSTRUCTOR (t))
1967 int nonprivate_ctor = 0;
1969 /* If a non-template class does not define a copy
1970 constructor, one is defined for it, enabling it to avoid
1971 this warning. For a template class, this does not
1972 happen, and so we would normally get a warning on:
1974 template <class T> class C { private: C(); };
1976 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1977 complete non-template or fully instantiated classes have this
1978 flag set. */
1979 if (!TYPE_HAS_INIT_REF (t))
1980 nonprivate_ctor = 1;
1981 else
1982 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
1984 fn = OVL_NEXT (fn))
1986 tree ctor = OVL_CURRENT (fn);
1987 /* Ideally, we wouldn't count copy constructors (or, in
1988 fact, any constructor that takes an argument of the
1989 class type as a parameter) because such things cannot
1990 be used to construct an instance of the class unless
1991 you already have one. But, for now at least, we're
1992 more generous. */
1993 if (! TREE_PRIVATE (ctor))
1995 nonprivate_ctor = 1;
1996 break;
2000 if (nonprivate_ctor == 0)
2002 cp_warning ("`%#T' only defines private constructors and has no friends",
2004 return;
2009 /* Function to help qsort sort FIELD_DECLs by name order. */
2011 static int
2012 field_decl_cmp (x, y)
2013 const tree *x, *y;
2015 if (DECL_NAME (*x) == DECL_NAME (*y))
2016 return 0;
2017 if (DECL_NAME (*x) == NULL_TREE)
2018 return -1;
2019 if (DECL_NAME (*y) == NULL_TREE)
2020 return 1;
2021 if (DECL_NAME (*x) < DECL_NAME (*y))
2022 return -1;
2023 return 1;
2026 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2028 static int
2029 method_name_cmp (m1, m2)
2030 const tree *m1, *m2;
2032 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2033 return 0;
2034 if (*m1 == NULL_TREE)
2035 return -1;
2036 if (*m2 == NULL_TREE)
2037 return 1;
2038 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2039 return -1;
2040 return 1;
2043 /* Warn about duplicate methods in fn_fields. Also compact method
2044 lists so that lookup can be made faster.
2046 Data Structure: List of method lists. The outer list is a
2047 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2048 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2049 links the entire list of methods for TYPE_METHODS. Friends are
2050 chained in the same way as member functions (? TREE_CHAIN or
2051 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2052 list. That allows them to be quickly deleted, and requires no
2053 extra storage.
2055 If there are any constructors/destructors, they are moved to the
2056 front of the list. This makes pushclass more efficient.
2058 @@ The above comment is obsolete. It mostly describes what add_method
2059 @@ and add_implicitly_declared_members do.
2061 Sort methods that are not special (i.e., constructors, destructors, and
2062 type conversion operators) so that we can find them faster in search. */
2064 static void
2065 finish_struct_methods (t)
2066 tree t;
2068 tree fn_fields;
2069 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2070 tree ctor_name = constructor_name (t);
2071 int slot, len = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2073 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2074 and the next few with type conversion operators (if any). */
2075 for (fn_fields = TYPE_METHODS (t); fn_fields;
2076 fn_fields = TREE_CHAIN (fn_fields))
2078 tree fn_name = DECL_NAME (fn_fields);
2080 /* Clear out this flag.
2082 @@ Doug may figure out how to break
2083 @@ this with nested classes and friends. */
2084 DECL_IN_AGGR_P (fn_fields) = 0;
2086 /* Note here that a copy ctor is private, so we don't dare generate
2087 a default copy constructor for a class that has a member
2088 of this type without making sure they have access to it. */
2089 if (fn_name == ctor_name)
2091 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2092 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2094 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2095 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2097 if (TREE_CHAIN (parmtypes) == NULL_TREE
2098 || TREE_CHAIN (parmtypes) == void_list_node
2099 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2101 if (TREE_PROTECTED (fn_fields))
2102 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2103 else if (TREE_PRIVATE (fn_fields))
2104 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2108 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2110 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2112 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2114 if (TREE_PROTECTED (fn_fields))
2115 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2116 else if (TREE_PRIVATE (fn_fields))
2117 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2122 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2123 /* We thought there was a destructor, but there wasn't. Some
2124 parse errors cause this anomalous situation. */
2125 TYPE_HAS_DESTRUCTOR (t) = 0;
2127 /* Issue warnings about private constructors and such. If there are
2128 no methods, then some public defaults are generated. */
2129 maybe_warn_about_overly_private_class (t);
2131 if (method_vec == NULL_TREE)
2132 return;
2134 /* Now sort the methods. */
2135 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2136 len--;
2137 TREE_VEC_LENGTH (method_vec) = len;
2139 /* The type conversion ops have to live at the front of the vec, so we
2140 can't sort them. */
2141 for (slot = 2; slot < len; ++slot)
2143 tree fn = TREE_VEC_ELT (method_vec, slot);
2145 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2146 break;
2148 if (len - slot > 1)
2149 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2150 (int (*)(const void *, const void *))method_name_cmp);
2153 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2155 void
2156 duplicate_tag_error (t)
2157 tree t;
2159 cp_error ("redefinition of `%#T'", t);
2160 cp_error_at ("previous definition here", t);
2162 /* Pretend we haven't defined this type. */
2164 /* All of the component_decl's were TREE_CHAINed together in the parser.
2165 finish_struct_methods walks these chains and assembles all methods with
2166 the same base name into DECL_CHAINs. Now we don't need the parser chains
2167 anymore, so we unravel them. */
2169 /* This used to be in finish_struct, but it turns out that the
2170 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2171 things... */
2172 if (CLASSTYPE_METHOD_VEC (t))
2174 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2175 int i, len = TREE_VEC_LENGTH (method_vec);
2176 for (i = 0; i < len; i++)
2178 tree unchain = TREE_VEC_ELT (method_vec, i);
2179 while (unchain != NULL_TREE)
2181 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2182 unchain = OVL_NEXT (unchain);
2187 if (TYPE_LANG_SPECIFIC (t))
2189 tree binfo = TYPE_BINFO (t);
2190 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2191 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2193 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2194 BINFO_BASETYPES(binfo) = NULL_TREE;
2196 TYPE_BINFO (t) = binfo;
2197 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2198 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2199 TYPE_REDEFINED (t) = 1;
2201 TYPE_SIZE (t) = NULL_TREE;
2202 TYPE_MODE (t) = VOIDmode;
2203 TYPE_FIELDS (t) = NULL_TREE;
2204 TYPE_METHODS (t) = NULL_TREE;
2205 TYPE_VFIELD (t) = NULL_TREE;
2206 TYPE_CONTEXT (t) = NULL_TREE;
2207 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2210 /* finish up all new vtables. */
2212 static void
2213 finish_vtbls (binfo, do_self, t)
2214 tree binfo;
2215 int do_self;
2216 tree t;
2218 tree binfos = BINFO_BASETYPES (binfo);
2219 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2221 /* Should we use something besides CLASSTYPE_VFIELDS? */
2222 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2224 if (BINFO_NEW_VTABLE_MARKED (binfo))
2226 tree decl, context;
2228 decl = BINFO_VTABLE (binfo);
2229 context = DECL_CONTEXT (decl);
2230 DECL_CONTEXT (decl) = 0;
2232 /* We make a copy here in case we need to replace pure
2233 virtual functions with __pure_virtual. We don't want to
2234 mess up BINFO_VIRTUALS when we do this. */
2235 DECL_INITIAL (decl) = copy_list (BINFO_VIRTUALS (binfo));
2236 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE,
2237 DECL_INITIAL (decl));
2239 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0);
2240 DECL_CONTEXT (decl) = context;
2242 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2245 for (i = 0; i < n_baselinks; i++)
2247 tree base_binfo = TREE_VEC_ELT (binfos, i);
2248 int is_not_base_vtable
2249 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2250 if (TREE_VIA_VIRTUAL (base_binfo))
2252 base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2254 finish_vtbls (base_binfo, is_not_base_vtable, t);
2258 /* True if we should override the given BASE_FNDECL with the given
2259 FNDECL. */
2261 static int
2262 overrides (fndecl, base_fndecl)
2263 tree fndecl, base_fndecl;
2265 /* Destructors have special names. */
2266 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2267 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2268 return 1;
2269 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2270 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2271 return 0;
2272 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2274 tree types, base_types;
2275 #if 0
2276 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2277 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2278 #endif
2279 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2280 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2281 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2282 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2283 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2284 return 1;
2286 return 0;
2289 static tree
2290 get_class_offset_1 (parent, binfo, context, t, fndecl)
2291 tree parent, binfo, context, t, fndecl;
2293 tree binfos = BINFO_BASETYPES (binfo);
2294 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2295 tree rval = NULL_TREE;
2297 if (binfo == parent)
2298 return error_mark_node;
2300 for (i = 0; i < n_baselinks; i++)
2302 tree base_binfo = TREE_VEC_ELT (binfos, i);
2303 tree nrval;
2305 if (TREE_VIA_VIRTUAL (base_binfo))
2306 base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2307 CLASSTYPE_VBASECLASSES (t));
2308 nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl);
2309 /* See if we have a new value */
2310 if (nrval && (nrval != error_mark_node || rval==0))
2312 /* Only compare if we have two offsets */
2313 if (rval && rval != error_mark_node
2314 && ! tree_int_cst_equal (nrval, rval))
2316 /* Only give error if the two offsets are different */
2317 error ("every virtual function must have a unique final overrider");
2318 cp_error (" found two (or more) `%T' class subobjects in `%T'", context, t);
2319 cp_error (" with virtual `%D' from virtual base class", fndecl);
2320 return rval;
2322 rval = nrval;
2325 if (rval && BINFO_TYPE (binfo) == context)
2327 my_friendly_assert (rval == error_mark_node
2328 || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999);
2329 rval = BINFO_OFFSET (binfo);
2332 return rval;
2335 /* Get the offset to the CONTEXT subobject that is related to the
2336 given BINFO. */
2338 static tree
2339 get_class_offset (context, t, binfo, fndecl)
2340 tree context, t, binfo, fndecl;
2342 tree first_binfo = binfo;
2343 tree offset;
2344 int i;
2346 if (context == t)
2347 return integer_zero_node;
2349 if (BINFO_TYPE (binfo) == context)
2350 return BINFO_OFFSET (binfo);
2352 /* Check less derived binfos first. */
2353 while (BINFO_BASETYPES (binfo)
2354 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
2356 tree binfos = BINFO_BASETYPES (binfo);
2357 binfo = TREE_VEC_ELT (binfos, i);
2358 if (BINFO_TYPE (binfo) == context)
2359 return BINFO_OFFSET (binfo);
2362 /* Ok, not found in the less derived binfos, now check the more
2363 derived binfos. */
2364 offset = get_class_offset_1 (first_binfo, TYPE_BINFO (t), context, t, fndecl);
2365 if (offset==0 || TREE_CODE (offset) != INTEGER_CST)
2366 my_friendly_abort (999); /* we have to find it. */
2367 return offset;
2370 /* Skip RTTI information at the front of the virtual list. */
2372 unsigned HOST_WIDE_INT
2373 skip_rtti_stuff (virtuals, t)
2374 tree *virtuals, t;
2376 int n;
2378 if (CLASSTYPE_COM_INTERFACE (t))
2379 return 0;
2381 n = 0;
2382 if (*virtuals)
2384 /* We always reserve a slot for the offset/tdesc entry. */
2385 ++n;
2386 *virtuals = TREE_CHAIN (*virtuals);
2388 if (flag_vtable_thunks && *virtuals)
2390 /* The second slot is reserved for the tdesc pointer when thunks
2391 are used. */
2392 ++n;
2393 *virtuals = TREE_CHAIN (*virtuals);
2395 return n;
2398 static void
2399 modify_one_vtable (binfo, t, fndecl, pfn)
2400 tree binfo, t, fndecl, pfn;
2402 tree virtuals = BINFO_VIRTUALS (binfo);
2403 unsigned HOST_WIDE_INT n;
2405 /* update rtti entry */
2406 if (flag_rtti)
2408 if (binfo == TYPE_BINFO (t))
2410 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2411 build_vtable (TYPE_BINFO (DECL_CONTEXT (CLASSTYPE_VFIELD (t))), t);
2413 else
2415 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2416 prepare_fresh_vtable (binfo, t);
2419 if (fndecl == NULL_TREE)
2420 return;
2422 n = skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo));
2424 while (virtuals)
2426 tree current_fndecl = TREE_VALUE (virtuals);
2427 current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl);
2428 current_fndecl = TREE_OPERAND (current_fndecl, 0);
2429 if (current_fndecl && overrides (fndecl, current_fndecl))
2431 tree base_offset, offset;
2432 tree context = DECL_CLASS_CONTEXT (fndecl);
2433 tree vfield = CLASSTYPE_VFIELD (t);
2434 tree this_offset;
2436 offset = get_class_offset (context, t, binfo, fndecl);
2438 /* Find the right offset for the this pointer based on the
2439 base class we just found. We have to take into
2440 consideration the virtual base class pointers that we
2441 stick in before the virtual function table pointer.
2443 Also, we want just the delta between the most base class
2444 that we derived this vfield from and us. */
2445 base_offset = size_binop (PLUS_EXPR,
2446 get_derived_offset (binfo, DECL_CONTEXT (current_fndecl)),
2447 BINFO_OFFSET (binfo));
2448 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2450 if (binfo == TYPE_BINFO (t))
2452 /* In this case, it is *type*'s vtable we are modifying.
2453 We start with the approximation that it's vtable is that
2454 of the immediate base class. */
2455 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2456 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2458 else
2460 /* This is our very own copy of `basetype' to play with.
2461 Later, we will fill in all the virtual functions
2462 that override the virtual functions in these base classes
2463 which are not defined by the current type. */
2464 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2465 prepare_fresh_vtable (binfo, t);
2468 #ifdef NOTQUITE
2469 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
2470 #endif
2471 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2472 build_vtable_entry (this_offset, pfn),
2473 fndecl);
2475 ++n;
2476 virtuals = TREE_CHAIN (virtuals);
2480 /* These are the ones that are not through virtual base classes. */
2482 static void
2483 modify_all_direct_vtables (binfo, do_self, t, fndecl, pfn)
2484 tree binfo;
2485 int do_self;
2486 tree t, fndecl, pfn;
2488 tree binfos = BINFO_BASETYPES (binfo);
2489 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2491 /* Should we use something besides CLASSTYPE_VFIELDS? */
2492 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2494 modify_one_vtable (binfo, t, fndecl, pfn);
2497 for (i = 0; i < n_baselinks; i++)
2499 tree base_binfo = TREE_VEC_ELT (binfos, i);
2500 int is_not_base_vtable
2501 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2502 if (! TREE_VIA_VIRTUAL (base_binfo))
2503 modify_all_direct_vtables (base_binfo, is_not_base_vtable, t, fndecl, pfn);
2507 /* Fixup all the delta entries in this one vtable that need updating. */
2509 static void
2510 fixup_vtable_deltas1 (binfo, t)
2511 tree binfo, t;
2513 tree virtuals = BINFO_VIRTUALS (binfo);
2514 unsigned HOST_WIDE_INT n;
2516 n = skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo));
2518 while (virtuals)
2520 tree fndecl = TREE_VALUE (virtuals);
2521 tree pfn = FNADDR_FROM_VTABLE_ENTRY (fndecl);
2522 tree delta = DELTA_FROM_VTABLE_ENTRY (fndecl);
2523 fndecl = TREE_OPERAND (pfn, 0);
2524 if (fndecl)
2526 tree base_offset, offset;
2527 tree context = DECL_CLASS_CONTEXT (fndecl);
2528 tree vfield = CLASSTYPE_VFIELD (t);
2529 tree this_offset;
2531 offset = get_class_offset (context, t, binfo, fndecl);
2533 /* Find the right offset for the this pointer based on the
2534 base class we just found. We have to take into
2535 consideration the virtual base class pointers that we
2536 stick in before the virtual function table pointer.
2538 Also, we want just the delta between the most base class
2539 that we derived this vfield from and us. */
2540 base_offset = size_binop (PLUS_EXPR,
2541 get_derived_offset (binfo,
2542 DECL_CONTEXT (fndecl)),
2543 BINFO_OFFSET (binfo));
2544 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2546 if (! tree_int_cst_equal (this_offset, delta))
2548 /* Make sure we can modify the derived association with immunity. */
2549 if (binfo == TYPE_BINFO (t))
2551 /* In this case, it is *type*'s vtable we are modifying.
2552 We start with the approximation that it's vtable is that
2553 of the immediate base class. */
2554 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2555 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2557 else
2559 /* This is our very own copy of `basetype' to play with.
2560 Later, we will fill in all the virtual functions
2561 that override the virtual functions in these base classes
2562 which are not defined by the current type. */
2563 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2564 prepare_fresh_vtable (binfo, t);
2567 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2568 build_vtable_entry (this_offset, pfn),
2569 fndecl);
2572 ++n;
2573 virtuals = TREE_CHAIN (virtuals);
2577 /* Fixup all the delta entries in all the direct vtables that need updating.
2578 This happens when we have non-overridden virtual functions from a
2579 virtual base class, that are at a different offset, in the new
2580 hierarchy, because the layout of the virtual bases has changed. */
2582 static void
2583 fixup_vtable_deltas (binfo, init_self, t)
2584 tree binfo;
2585 int init_self;
2586 tree t;
2588 tree binfos = BINFO_BASETYPES (binfo);
2589 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2591 for (i = 0; i < n_baselinks; i++)
2593 tree base_binfo = TREE_VEC_ELT (binfos, i);
2594 int is_not_base_vtable
2595 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2596 if (! TREE_VIA_VIRTUAL (base_binfo))
2597 fixup_vtable_deltas (base_binfo, is_not_base_vtable, t);
2599 /* Should we use something besides CLASSTYPE_VFIELDS? */
2600 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2602 fixup_vtable_deltas1 (binfo, t);
2606 /* These are the ones that are through virtual base classes. */
2608 static void
2609 modify_all_indirect_vtables (binfo, do_self, via_virtual, t, fndecl, pfn)
2610 tree binfo;
2611 int do_self, via_virtual;
2612 tree t, fndecl, pfn;
2614 tree binfos = BINFO_BASETYPES (binfo);
2615 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2617 /* Should we use something besides CLASSTYPE_VFIELDS? */
2618 if (do_self && via_virtual && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2620 modify_one_vtable (binfo, t, fndecl, pfn);
2623 for (i = 0; i < n_baselinks; i++)
2625 tree base_binfo = TREE_VEC_ELT (binfos, i);
2626 int is_not_base_vtable
2627 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2628 if (TREE_VIA_VIRTUAL (base_binfo))
2630 via_virtual = 1;
2631 base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2633 modify_all_indirect_vtables (base_binfo, is_not_base_vtable, via_virtual, t, fndecl, pfn);
2637 static void
2638 modify_all_vtables (t, fndecl, vfn)
2639 tree t, fndecl, vfn;
2641 /* Do these first, so that we will make use of any non-virtual class's
2642 vtable, over a virtual classes vtable. */
2643 modify_all_direct_vtables (TYPE_BINFO (t), 1, t, fndecl, vfn);
2644 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
2645 modify_all_indirect_vtables (TYPE_BINFO (t), 1, 0, t, fndecl, vfn);
2648 /* Here, we already know that they match in every respect.
2649 All we have to check is where they had their declarations. */
2651 static int
2652 strictly_overrides (fndecl1, fndecl2)
2653 tree fndecl1, fndecl2;
2655 int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
2656 DECL_CLASS_CONTEXT (fndecl1),
2657 0, (tree *)0);
2658 if (distance == -2 || distance > 0)
2659 return 1;
2660 return 0;
2663 /* Merge overrides for one vtable.
2664 If we want to merge in same function, we are fine.
2665 else
2666 if one has a DECL_CLASS_CONTEXT that is a parent of the
2667 other, than choose the more derived one
2668 else
2669 potentially ill-formed (see 10.3 [class.virtual])
2670 we have to check later to see if there was an
2671 override in this class. If there was ok, if not
2672 then it is ill-formed. (mrs)
2674 We take special care to reuse a vtable, if we can. */
2676 static void
2677 override_one_vtable (binfo, old, t)
2678 tree binfo, old, t;
2680 tree virtuals = BINFO_VIRTUALS (binfo);
2681 tree old_virtuals = BINFO_VIRTUALS (old);
2682 enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
2684 /* If we have already committed to modifying it, then don't try and
2685 reuse another vtable. */
2686 if (BINFO_NEW_VTABLE_MARKED (binfo))
2687 choose = NEITHER;
2689 skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo));
2690 skip_rtti_stuff (&old_virtuals, BINFO_TYPE (binfo));
2692 while (virtuals)
2694 tree fndecl = TREE_VALUE (virtuals);
2695 tree old_fndecl = TREE_VALUE (old_virtuals);
2696 fndecl = FNADDR_FROM_VTABLE_ENTRY (fndecl);
2697 old_fndecl = FNADDR_FROM_VTABLE_ENTRY (old_fndecl);
2698 fndecl = TREE_OPERAND (fndecl, 0);
2699 old_fndecl = TREE_OPERAND (old_fndecl, 0);
2700 /* First check to see if they are the same. */
2701 if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
2703 /* No need to do anything. */
2705 else if (strictly_overrides (fndecl, old_fndecl))
2707 if (choose == UNDECIDED)
2708 choose = REUSE_NEW;
2709 else if (choose == REUSE_OLD)
2711 choose = NEITHER;
2712 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2714 prepare_fresh_vtable (binfo, t);
2715 override_one_vtable (binfo, old, t);
2716 return;
2720 else if (strictly_overrides (old_fndecl, fndecl))
2722 if (choose == UNDECIDED)
2723 choose = REUSE_OLD;
2724 else if (choose == REUSE_NEW)
2726 choose = NEITHER;
2727 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2729 prepare_fresh_vtable (binfo, t);
2730 override_one_vtable (binfo, old, t);
2731 return;
2733 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2735 else if (choose == NEITHER)
2737 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2740 else
2742 choose = NEITHER;
2743 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2745 prepare_fresh_vtable (binfo, t);
2746 override_one_vtable (binfo, old, t);
2747 return;
2750 /* This MUST be overridden, or the class is ill-formed. */
2751 tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
2752 tree vfn;
2754 fndecl = copy_node (fndecl);
2755 copy_lang_decl (fndecl);
2756 DECL_NEEDS_FINAL_OVERRIDER_P (fndecl) = 1;
2757 /* Make sure we search for it later. */
2758 if (! CLASSTYPE_ABSTRACT_VIRTUALS (t))
2759 CLASSTYPE_ABSTRACT_VIRTUALS (t) = error_mark_node;
2761 vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
2762 TREE_CONSTANT (vfn) = 1;
2764 /* We can use integer_zero_node, as we will core dump
2765 if this is used anyway. */
2766 TREE_VALUE (virtuals) = build_vtable_entry (integer_zero_node, vfn);
2769 virtuals = TREE_CHAIN (virtuals);
2770 old_virtuals = TREE_CHAIN (old_virtuals);
2773 /* Let's reuse the old vtable. */
2774 if (choose == REUSE_OLD)
2776 BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
2777 BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
2781 /* Merge in overrides for virtual bases.
2782 BINFO is the hierarchy we want to modify, and OLD has the potential
2783 overrides. */
2785 static void
2786 merge_overrides (binfo, old, do_self, t)
2787 tree binfo, old;
2788 int do_self;
2789 tree t;
2791 tree binfos = BINFO_BASETYPES (binfo);
2792 tree old_binfos = BINFO_BASETYPES (old);
2793 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2795 /* Should we use something besides CLASSTYPE_VFIELDS? */
2796 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2798 override_one_vtable (binfo, old, t);
2801 for (i = 0; i < n_baselinks; i++)
2803 tree base_binfo = TREE_VEC_ELT (binfos, i);
2804 tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
2805 int is_not_base_vtable
2806 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2807 if (! TREE_VIA_VIRTUAL (base_binfo))
2808 merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
2812 /* Get the base virtual function declarations in T that are either
2813 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2814 the overrider/hider. */
2816 static tree
2817 get_basefndecls (fndecl, t)
2818 tree fndecl, t;
2820 tree methods = TYPE_METHODS (t);
2821 tree base_fndecls = NULL_TREE;
2822 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2823 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2825 while (methods)
2827 if (TREE_CODE (methods) == FUNCTION_DECL
2828 && DECL_VINDEX (methods) != NULL_TREE
2829 && DECL_NAME (fndecl) == DECL_NAME (methods))
2830 base_fndecls = temp_tree_cons (fndecl, methods, base_fndecls);
2832 methods = TREE_CHAIN (methods);
2835 if (base_fndecls)
2836 return base_fndecls;
2838 for (i = 0; i < n_baseclasses; i++)
2840 tree base_binfo = TREE_VEC_ELT (binfos, i);
2841 tree basetype = BINFO_TYPE (base_binfo);
2843 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2844 base_fndecls);
2847 return base_fndecls;
2850 /* Mark the functions that have been hidden with their overriders.
2851 Since we start out with all functions already marked with a hider,
2852 no need to mark functions that are just hidden. */
2854 static void
2855 mark_overriders (fndecl, base_fndecls)
2856 tree fndecl, base_fndecls;
2858 while (base_fndecls)
2860 if (overrides (TREE_VALUE (base_fndecls), fndecl))
2861 TREE_PURPOSE (base_fndecls) = fndecl;
2863 base_fndecls = TREE_CHAIN (base_fndecls);
2867 /* If this declaration supersedes the declaration of
2868 a method declared virtual in the base class, then
2869 mark this field as being virtual as well. */
2871 static void
2872 check_for_override (decl, ctype)
2873 tree decl, ctype;
2875 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
2876 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2877 int virtualp = DECL_VIRTUAL_P (decl);
2878 int found_overriden_fn = 0;
2880 for (i = 0; i < n_baselinks; i++)
2882 tree base_binfo = TREE_VEC_ELT (binfos, i);
2883 if (TYPE_VIRTUAL_P (BINFO_TYPE (base_binfo)))
2885 tree tmp = get_matching_virtual
2886 (base_binfo, decl,
2887 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
2889 if (tmp && !found_overriden_fn)
2891 /* If this function overrides some virtual in some base
2892 class, then the function itself is also necessarily
2893 virtual, even if the user didn't explicitly say so. */
2894 DECL_VIRTUAL_P (decl) = 1;
2896 /* The TMP we really want is the one from the deepest
2897 baseclass on this path, taking care not to
2898 duplicate if we have already found it (via another
2899 path to its virtual baseclass. */
2900 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
2902 cp_error_at ("method `%D' may not be declared static",
2903 decl);
2904 cp_error_at ("(since `%D' declared virtual in base class.)",
2905 tmp);
2906 break;
2908 virtualp = 1;
2910 DECL_VINDEX (decl)
2911 = tree_cons (NULL_TREE, tmp, DECL_VINDEX (decl));
2913 /* We now know that DECL overrides something,
2914 which is all that is important. But, we must
2915 continue to iterate through all the base-classes
2916 in order to allow get_matching_virtual to check for
2917 various illegal overrides. */
2918 found_overriden_fn = 1;
2922 if (virtualp)
2924 if (DECL_VINDEX (decl) == NULL_TREE)
2925 DECL_VINDEX (decl) = error_mark_node;
2926 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2930 /* Warn about hidden virtual functions that are not overridden in t.
2931 We know that constructors and destructors don't apply. */
2933 void
2934 warn_hidden (t)
2935 tree t;
2937 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2938 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2939 int i;
2941 /* We go through each separately named virtual function. */
2942 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2944 tree fns = TREE_VEC_ELT (method_vec, i);
2945 tree fndecl;
2947 tree base_fndecls = NULL_TREE;
2948 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2949 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2951 fndecl = OVL_CURRENT (fns);
2952 if (DECL_VINDEX (fndecl) == NULL_TREE)
2953 continue;
2955 /* First we get a list of all possible functions that might be
2956 hidden from each base class. */
2957 for (i = 0; i < n_baseclasses; i++)
2959 tree base_binfo = TREE_VEC_ELT (binfos, i);
2960 tree basetype = BINFO_TYPE (base_binfo);
2962 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2963 base_fndecls);
2966 fns = OVL_NEXT (fns);
2967 if (fns)
2968 fndecl = OVL_CURRENT (fns);
2969 else
2970 fndecl = NULL_TREE;
2972 /* ...then mark up all the base functions with overriders, preferring
2973 overriders to hiders. */
2974 if (base_fndecls)
2975 while (fndecl)
2977 mark_overriders (fndecl, base_fndecls);
2979 fns = OVL_NEXT (fns);
2980 if (fns)
2981 fndecl = OVL_CURRENT (fns);
2982 else
2983 fndecl = NULL_TREE;
2986 /* Now give a warning for all base functions without overriders,
2987 as they are hidden. */
2988 while (base_fndecls)
2990 if (! overrides (TREE_VALUE (base_fndecls),
2991 TREE_PURPOSE (base_fndecls)))
2993 /* Here we know it is a hider, and no overrider exists. */
2994 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2995 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
2998 base_fndecls = TREE_CHAIN (base_fndecls);
3003 /* Check for things that are invalid. There are probably plenty of other
3004 things we should check for also. */
3006 static void
3007 finish_struct_anon (t)
3008 tree t;
3010 tree field;
3012 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3014 if (TREE_STATIC (field))
3015 continue;
3016 if (TREE_CODE (field) != FIELD_DECL)
3017 continue;
3019 if (DECL_NAME (field) == NULL_TREE
3020 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3022 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3023 for (; elt; elt = TREE_CHAIN (elt))
3025 if (DECL_ARTIFICIAL (elt))
3026 continue;
3028 if (DECL_NAME (elt) == constructor_name (t))
3029 cp_pedwarn_at ("ANSI C++ forbids member `%D' with same name as enclosing class",
3030 elt);
3032 if (TREE_CODE (elt) != FIELD_DECL)
3034 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3035 elt);
3036 continue;
3039 if (TREE_PRIVATE (elt))
3040 cp_pedwarn_at ("private member `%#D' in anonymous union",
3041 elt);
3042 else if (TREE_PROTECTED (elt))
3043 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3044 elt);
3046 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3047 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3053 extern int interface_only, interface_unknown;
3055 /* Create default constructors, assignment operators, and so forth for
3056 the type indicated by T, if they are needed.
3057 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3058 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3059 cannot have a default constructor, copy constructor taking a const
3060 reference argument, or an assignment operator, respectively. If a
3061 virtual destructor is created, its DECL is returned; otherwise the
3062 return value is NULL_TREE. */
3064 static tree
3065 add_implicitly_declared_members (t, cant_have_default_ctor,
3066 cant_have_const_cctor,
3067 cant_have_assignment)
3068 tree t;
3069 int cant_have_default_ctor;
3070 int cant_have_const_cctor;
3071 int cant_have_assignment;
3073 tree default_fn;
3074 tree implicit_fns = NULL_TREE;
3075 tree name = TYPE_IDENTIFIER (t);
3076 tree virtual_dtor = NULL_TREE;
3077 tree *f;
3079 /* Destructor. */
3080 if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t)
3081 && !IS_SIGNATURE (t))
3083 default_fn = cons_up_default_function (t, name, 0);
3084 check_for_override (default_fn, t);
3086 /* If we couldn't make it work, then pretend we didn't need it. */
3087 if (default_fn == void_type_node)
3088 TYPE_NEEDS_DESTRUCTOR (t) = 0;
3089 else
3091 TREE_CHAIN (default_fn) = implicit_fns;
3092 implicit_fns = default_fn;
3094 if (DECL_VINDEX (default_fn))
3095 virtual_dtor = default_fn;
3098 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3100 /* Default constructor. */
3101 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor
3102 && ! IS_SIGNATURE (t))
3104 default_fn = cons_up_default_function (t, name, 2);
3105 TREE_CHAIN (default_fn) = implicit_fns;
3106 implicit_fns = default_fn;
3109 /* Copy constructor. */
3110 if (! TYPE_HAS_INIT_REF (t) && ! IS_SIGNATURE (t) && ! TYPE_FOR_JAVA (t))
3112 /* ARM 12.18: You get either X(X&) or X(const X&), but
3113 not both. --Chip */
3114 default_fn = cons_up_default_function (t, name,
3115 3 + cant_have_const_cctor);
3116 TREE_CHAIN (default_fn) = implicit_fns;
3117 implicit_fns = default_fn;
3120 /* Assignment operator. */
3121 if (! TYPE_HAS_ASSIGN_REF (t) && ! IS_SIGNATURE (t) && ! TYPE_FOR_JAVA (t))
3123 default_fn = cons_up_default_function (t, name,
3124 5 + cant_have_assignment);
3125 TREE_CHAIN (default_fn) = implicit_fns;
3126 implicit_fns = default_fn;
3129 /* Now, hook all of the new functions on to TYPE_METHODS,
3130 and add them to the CLASSTYPE_METHOD_VEC. */
3131 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3132 add_method (t, 0, *f);
3133 *f = TYPE_METHODS (t);
3134 TYPE_METHODS (t) = implicit_fns;
3136 return virtual_dtor;
3139 /* Subroutine of finish_struct_1. Recursively count the number of fields
3140 in TYPE, including anonymous union members. */
3142 static int
3143 count_fields (fields)
3144 tree fields;
3146 tree x;
3147 int n_fields = 0;
3148 for (x = fields; x; x = TREE_CHAIN (x))
3150 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3151 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3152 else
3153 n_fields += 1;
3155 return n_fields;
3158 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3159 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3161 static int
3162 add_fields_to_vec (fields, field_vec, idx)
3163 tree fields, field_vec;
3164 int idx;
3166 tree x;
3167 for (x = fields; x; x = TREE_CHAIN (x))
3169 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3170 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3171 else
3172 TREE_VEC_ELT (field_vec, idx++) = x;
3174 return idx;
3177 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
3178 (or C++ class declaration).
3180 For C++, we must handle the building of derived classes.
3181 Also, C++ allows static class members. The way that this is
3182 handled is to keep the field name where it is (as the DECL_NAME
3183 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
3184 of the field. layout_record and layout_union will know about this.
3186 More C++ hair: inline functions have text in their
3187 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
3188 meaningful tree structure. After the struct has been laid out, set
3189 things up so that this can happen.
3191 And still more: virtual functions. In the case of single inheritance,
3192 when a new virtual function is seen which redefines a virtual function
3193 from the base class, the new virtual function is placed into
3194 the virtual function table at exactly the same address that
3195 it had in the base class. When this is extended to multiple
3196 inheritance, the same thing happens, except that multiple virtual
3197 function tables must be maintained. The first virtual function
3198 table is treated in exactly the same way as in the case of single
3199 inheritance. Additional virtual function tables have different
3200 DELTAs, which tell how to adjust `this' to point to the right thing.
3202 ATTRIBUTES is the set of decl attributes to be applied, if any. */
3204 void
3205 finish_struct_1 (t)
3206 tree t;
3208 int old;
3209 enum tree_code code = TREE_CODE (t);
3210 tree fields = TYPE_FIELDS (t);
3211 tree x, last_x, method_vec;
3212 int has_virtual;
3213 int max_has_virtual;
3214 tree pending_virtuals = NULL_TREE;
3215 tree pending_hard_virtuals = NULL_TREE;
3216 tree abstract_virtuals = NULL_TREE;
3217 tree vfield;
3218 tree vfields;
3219 tree virtual_dtor;
3220 int cant_have_default_ctor;
3221 int cant_have_const_ctor;
3222 int no_const_asn_ref;
3223 int has_mutable = 0;
3224 int n_fields = 0;
3226 /* The index of the first base class which has virtual
3227 functions. Only applied to non-virtual baseclasses. */
3228 int first_vfn_base_index;
3230 int n_baseclasses;
3231 int any_default_members = 0;
3232 int const_sans_init = 0;
3233 int ref_sans_init = 0;
3234 tree access_decls = NULL_TREE;
3235 int aggregate = 1;
3236 int empty = 1;
3237 int has_pointers = 0;
3238 tree inline_friends;
3240 if (TYPE_SIZE (t))
3242 if (IS_AGGR_TYPE (t))
3243 cp_error ("redefinition of `%#T'", t);
3244 else
3245 my_friendly_abort (172);
3246 popclass ();
3247 return;
3250 GNU_xref_decl (current_function_decl, t);
3252 /* If this type was previously laid out as a forward reference,
3253 make sure we lay it out again. */
3255 TYPE_SIZE (t) = NULL_TREE;
3256 CLASSTYPE_GOT_SEMICOLON (t) = 0;
3258 #if 0
3259 /* This is in general too late to do this. I moved the main case up to
3260 left_curly, what else needs to move? */
3261 if (! IS_SIGNATURE (t))
3263 my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t) == interface_only, 999);
3264 my_friendly_assert (CLASSTYPE_INTERFACE_KNOWN (t) == ! interface_unknown, 999);
3266 #endif
3268 old = suspend_momentary ();
3270 /* Install struct as DECL_FIELD_CONTEXT of each field decl.
3271 Also process specified field sizes.
3272 Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
3273 The specified size is found in the DECL_INITIAL.
3274 Store 0 there, except for ": 0" fields (so we can find them
3275 and delete them, below). */
3277 if (TYPE_BINFO_BASETYPES (t))
3278 n_baseclasses = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t));
3279 else
3280 n_baseclasses = 0;
3282 if (n_baseclasses > 0)
3284 struct base_info base_info;
3286 first_vfn_base_index = finish_base_struct (t, &base_info);
3287 /* Remember where we got our vfield from. */
3288 CLASSTYPE_VFIELD_PARENT (t) = first_vfn_base_index;
3289 has_virtual = base_info.has_virtual;
3290 max_has_virtual = base_info.max_has_virtual;
3291 vfield = base_info.vfield;
3292 vfields = base_info.vfields;
3293 CLASSTYPE_RTTI (t) = base_info.rtti;
3294 cant_have_default_ctor = base_info.cant_have_default_ctor;
3295 cant_have_const_ctor = base_info.cant_have_const_ctor;
3296 no_const_asn_ref = base_info.no_const_asn_ref;
3297 aggregate = 0;
3299 else
3301 first_vfn_base_index = -1;
3302 has_virtual = 0;
3303 max_has_virtual = has_virtual;
3304 vfield = NULL_TREE;
3305 vfields = NULL_TREE;
3306 CLASSTYPE_RTTI (t) = NULL_TREE;
3307 cant_have_default_ctor = 0;
3308 cant_have_const_ctor = 0;
3309 no_const_asn_ref = 0;
3312 /* The three of these are approximations which may later be
3313 modified. Needed at this point to make add_virtual_function
3314 and modify_vtable_entries work. */
3315 CLASSTYPE_VFIELDS (t) = vfields;
3316 CLASSTYPE_VFIELD (t) = vfield;
3318 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3320 GNU_xref_member (current_class_name, x);
3322 /* If this was an evil function, don't keep it in class. */
3323 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3324 continue;
3326 /* Do both of these, even though they're in the same union;
3327 if the insn `r' member and the size `i' member are
3328 different sizes, as on the alpha, the larger of the two
3329 will end up with garbage in it. */
3330 DECL_SAVED_INSNS (x) = NULL_RTX;
3331 DECL_FIELD_SIZE (x) = 0;
3333 check_for_override (x, t);
3334 if (DECL_ABSTRACT_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3335 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3337 /* The name of the field is the original field name
3338 Save this in auxiliary field for later overloading. */
3339 if (DECL_VINDEX (x))
3341 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3342 &has_virtual, x, t);
3343 if (DECL_ABSTRACT_VIRTUAL_P (x))
3344 abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
3345 #if 0
3346 /* XXX Why did I comment this out? (jason) */
3347 else
3348 TREE_USED (x) = 1;
3349 #endif
3353 if (n_baseclasses)
3354 fields = chainon (build_vbase_pointer_fields (t), fields);
3356 last_x = NULL_TREE;
3357 for (x = fields; x; x = TREE_CHAIN (x))
3359 GNU_xref_member (current_class_name, x);
3361 if (TREE_CODE (x) == FIELD_DECL)
3363 DECL_PACKED (x) |= TYPE_PACKED (t);
3365 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3366 /* A zero-width bitfield doesn't do the trick. */;
3367 else
3368 empty = 0;
3371 if (TREE_CODE (x) == USING_DECL)
3373 /* Save access declarations for later. */
3374 if (last_x)
3375 TREE_CHAIN (last_x) = TREE_CHAIN (x);
3376 else
3377 fields = TREE_CHAIN (x);
3379 access_decls = scratch_tree_cons (NULL_TREE, x, access_decls);
3380 continue;
3383 last_x = x;
3385 if (TREE_CODE (x) == TYPE_DECL
3386 || TREE_CODE (x) == TEMPLATE_DECL)
3387 continue;
3389 /* If we've gotten this far, it's a data member, possibly static,
3390 or an enumerator. */
3392 DECL_FIELD_CONTEXT (x) = t;
3394 /* ``A local class cannot have static data members.'' ARM 9.4 */
3395 if (current_function_decl && TREE_STATIC (x))
3396 cp_error_at ("field `%D' in local class cannot be static", x);
3398 /* Perform error checking that did not get done in
3399 grokdeclarator. */
3400 if (TREE_CODE (TREE_TYPE (x)) == FUNCTION_TYPE)
3402 cp_error_at ("field `%D' invalidly declared function type",
3404 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3406 else if (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE)
3408 cp_error_at ("field `%D' invalidly declared method type", x);
3409 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3411 else if (TREE_CODE (TREE_TYPE (x)) == OFFSET_TYPE)
3413 cp_error_at ("field `%D' invalidly declared offset type", x);
3414 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3417 #if 0
3418 if (DECL_NAME (x) == constructor_name (t))
3419 cant_have_default_ctor = 1;
3420 #endif
3422 if (TREE_TYPE (x) == error_mark_node)
3423 continue;
3425 DECL_SAVED_INSNS (x) = NULL_RTX;
3426 DECL_FIELD_SIZE (x) = 0;
3428 /* When this goes into scope, it will be a non-local reference. */
3429 DECL_NONLOCAL (x) = 1;
3431 if (TREE_CODE (x) == CONST_DECL)
3432 continue;
3434 if (TREE_CODE (x) == VAR_DECL)
3436 if (TREE_CODE (t) == UNION_TYPE)
3437 /* Unions cannot have static members. */
3438 cp_error_at ("field `%D' declared static in union", x);
3440 continue;
3443 /* Now it can only be a FIELD_DECL. */
3445 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3446 aggregate = 0;
3448 /* If this is of reference type, check if it needs an init.
3449 Also do a little ANSI jig if necessary. */
3450 if (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE)
3452 if (DECL_INITIAL (x) == NULL_TREE)
3453 ref_sans_init = 1;
3455 /* ARM $12.6.2: [A member initializer list] (or, for an
3456 aggregate, initialization by a brace-enclosed list) is the
3457 only way to initialize nonstatic const and reference
3458 members. */
3459 cant_have_default_ctor = 1;
3460 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3462 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3464 if (DECL_NAME (x))
3465 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3466 else
3467 cp_warning_at ("non-static reference in class without a constructor", x);
3471 if (TREE_CODE (TREE_TYPE (x)) == POINTER_TYPE)
3472 has_pointers = 1;
3474 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (TREE_TYPE (x)))
3475 has_mutable = 1;
3477 /* If any field is const, the structure type is pseudo-const. */
3478 if (CP_TYPE_CONST_P (TREE_TYPE (x)))
3480 C_TYPE_FIELDS_READONLY (t) = 1;
3481 if (DECL_INITIAL (x) == NULL_TREE)
3482 const_sans_init = 1;
3484 /* ARM $12.6.2: [A member initializer list] (or, for an
3485 aggregate, initialization by a brace-enclosed list) is the
3486 only way to initialize nonstatic const and reference
3487 members. */
3488 cant_have_default_ctor = 1;
3489 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3491 if (! TYPE_HAS_CONSTRUCTOR (t) && !IS_SIGNATURE (t)
3492 && extra_warnings)
3494 if (DECL_NAME (x))
3495 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3496 else
3497 cp_warning_at ("non-static const member in class without a constructor", x);
3500 else
3502 /* A field that is pseudo-const makes the structure
3503 likewise. */
3504 tree t1 = TREE_TYPE (x);
3505 while (TREE_CODE (t1) == ARRAY_TYPE)
3506 t1 = TREE_TYPE (t1);
3507 if (IS_AGGR_TYPE (t1))
3509 if (C_TYPE_FIELDS_READONLY (t1))
3510 C_TYPE_FIELDS_READONLY (t) = 1;
3511 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1))
3512 const_sans_init = 1;
3516 /* We set DECL_C_BIT_FIELD in grokbitfield.
3517 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3518 if (DECL_C_BIT_FIELD (x))
3520 /* Invalid bit-field size done by grokfield. */
3521 /* Detect invalid bit-field type. */
3522 if (DECL_INITIAL (x)
3523 && ! INTEGRAL_TYPE_P (TREE_TYPE (x)))
3525 cp_error_at ("bit-field `%#D' with non-integral type", x);
3526 DECL_INITIAL (x) = NULL;
3529 /* Detect and ignore out of range field width. */
3530 if (DECL_INITIAL (x))
3532 tree w = DECL_INITIAL (x);
3533 register int width = 0;
3535 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3536 STRIP_NOPS (w);
3538 /* detect invalid field size. */
3539 if (TREE_CODE (w) == CONST_DECL)
3540 w = DECL_INITIAL (w);
3541 else if (TREE_READONLY_DECL_P (w))
3542 w = decl_constant_value (w);
3544 if (TREE_CODE (w) != INTEGER_CST)
3546 cp_error_at ("bit-field `%D' width not an integer constant",
3548 DECL_INITIAL (x) = NULL_TREE;
3550 else if (width = TREE_INT_CST_LOW (w),
3551 width < 0)
3553 DECL_INITIAL (x) = NULL;
3554 cp_error_at ("negative width in bit-field `%D'", x);
3556 else if (width == 0 && DECL_NAME (x) != 0)
3558 DECL_INITIAL (x) = NULL;
3559 cp_error_at ("zero width for bit-field `%D'", x);
3561 else if (width
3562 > TYPE_PRECISION (long_long_unsigned_type_node))
3564 /* The backend will dump if you try to use something
3565 too big; avoid that. */
3566 DECL_INITIAL (x) = NULL;
3567 sorry ("bit-fields larger than %d bits",
3568 TYPE_PRECISION (long_long_unsigned_type_node));
3569 cp_error_at (" in declaration of `%D'", x);
3571 else if (width > TYPE_PRECISION (TREE_TYPE (x))
3572 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE
3573 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE)
3575 cp_warning_at ("width of `%D' exceeds its type", x);
3577 else if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
3578 && ((min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
3579 TREE_UNSIGNED (TREE_TYPE (x))) > width)
3580 || (min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
3581 TREE_UNSIGNED (TREE_TYPE (x))) > width)))
3583 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3584 x, TREE_TYPE (x));
3587 if (DECL_INITIAL (x))
3589 DECL_INITIAL (x) = NULL_TREE;
3590 DECL_FIELD_SIZE (x) = width;
3591 DECL_BIT_FIELD (x) = 1;
3593 if (width == 0)
3595 #ifdef EMPTY_FIELD_BOUNDARY
3596 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3597 EMPTY_FIELD_BOUNDARY);
3598 #endif
3599 #ifdef PCC_BITFIELD_TYPE_MATTERS
3600 if (PCC_BITFIELD_TYPE_MATTERS)
3601 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3602 TYPE_ALIGN (TREE_TYPE (x)));
3603 #endif
3607 else
3608 /* Non-bit-fields are aligned for their type. */
3609 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x)));
3611 else
3613 tree type = TREE_TYPE (x);
3615 while (TREE_CODE (type) == ARRAY_TYPE)
3616 type = TREE_TYPE (type);
3618 if (CLASS_TYPE_P (type) && ! ANON_AGGR_TYPE_P (type))
3620 /* Never let anything with uninheritable virtuals
3621 make it through without complaint. */
3622 abstract_virtuals_error (x, type);
3624 /* Don't let signatures make it through either. */
3625 if (IS_SIGNATURE (type))
3626 signature_error (x, type);
3628 if (code == UNION_TYPE)
3630 const char *fie = NULL;
3631 if (TYPE_NEEDS_CONSTRUCTING (type))
3632 fie = "constructor";
3633 else if (TYPE_NEEDS_DESTRUCTOR (type))
3634 fie = "destructor";
3635 else if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3636 fie = "copy assignment operator";
3637 if (fie)
3638 cp_error_at ("member `%#D' with %s not allowed in union", x,
3639 fie);
3641 else
3643 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3644 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3645 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3646 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3649 if (!TYPE_HAS_CONST_INIT_REF (type))
3650 cant_have_const_ctor = 1;
3652 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3653 no_const_asn_ref = 1;
3655 if (TYPE_HAS_CONSTRUCTOR (type)
3656 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3658 cant_have_default_ctor = 1;
3659 #if 0
3660 /* This is wrong for aggregates. */
3661 if (! TYPE_HAS_CONSTRUCTOR (t))
3663 if (DECL_NAME (x))
3664 cp_pedwarn_at ("member `%#D' with only non-default constructor", x);
3665 else
3666 cp_pedwarn_at ("member with only non-default constructor", x);
3667 cp_pedwarn_at ("in class without a constructor",
3670 #endif
3673 if (DECL_INITIAL (x) != NULL_TREE)
3675 /* `build_class_init_list' does not recognize
3676 non-FIELD_DECLs. */
3677 if (code == UNION_TYPE && any_default_members != 0)
3678 cp_error_at ("multiple fields in union `%T' initialized");
3679 any_default_members = 1;
3684 /* If this type has any constant members which did not come
3685 with their own initialization, mark that fact here. It is
3686 not an error here, since such types can be saved either by their
3687 constructors, or by fortuitous initialization. */
3688 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
3689 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
3690 CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
3691 CLASSTYPE_HAS_MUTABLE (t) = has_mutable;
3693 /* Effective C++ rule 11. */
3694 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3695 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3697 cp_warning ("`%#T' has pointer data members", t);
3699 if (! TYPE_HAS_INIT_REF (t))
3701 cp_warning (" but does not override `%T(const %T&)'", t, t);
3702 if (! TYPE_HAS_ASSIGN_REF (t))
3703 cp_warning (" or `operator=(const %T&)'", t);
3705 else if (! TYPE_HAS_ASSIGN_REF (t))
3706 cp_warning (" but does not override `operator=(const %T&)'", t);
3709 /* Do some bookkeeping that will guide the generation of implicitly
3710 declared member functions. */
3711 TYPE_HAS_COMPLEX_INIT_REF (t)
3712 |= (TYPE_HAS_INIT_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3713 || has_virtual || any_default_members);
3714 TYPE_NEEDS_CONSTRUCTING (t)
3715 |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3716 || has_virtual || any_default_members);
3717 if (! IS_SIGNATURE (t))
3718 CLASSTYPE_NON_AGGREGATE (t)
3719 = ! aggregate || has_virtual || TYPE_HAS_CONSTRUCTOR (t);
3720 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
3721 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
3722 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
3724 /* Synthesize any needed methods. Note that methods will be synthesized
3725 for anonymous unions; grok_x_components undoes that. */
3726 virtual_dtor
3727 = add_implicitly_declared_members (t, cant_have_default_ctor,
3728 cant_have_const_ctor,
3729 no_const_asn_ref);
3730 if (virtual_dtor)
3731 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3732 &has_virtual, virtual_dtor, t);
3734 if (TYPE_METHODS (t))
3736 finish_struct_methods (t);
3737 method_vec = CLASSTYPE_METHOD_VEC (t);
3739 else
3741 method_vec = 0;
3743 /* Just in case these got accidentally
3744 filled in by syntax errors. */
3745 TYPE_HAS_CONSTRUCTOR (t) = 0;
3746 TYPE_HAS_DESTRUCTOR (t) = 0;
3749 for (access_decls = nreverse (access_decls); access_decls;
3750 access_decls = TREE_CHAIN (access_decls))
3751 handle_using_decl (TREE_VALUE (access_decls), t, method_vec, fields);
3753 if (vfield == NULL_TREE && has_virtual)
3755 /* We build this decl with vtbl_ptr_type_node, which is a
3756 `vtable_entry_type*'. It might seem more precise to use
3757 `vtable_entry_type (*)[N]' where N is the number of firtual
3758 functions. However, that would require the vtable pointer in
3759 base classes to have a different type than the vtable pointer
3760 in derived classes. We could make that happen, but that
3761 still wouldn't solve all the problems. In particular, the
3762 type-based alias analysis code would decide that assignments
3763 to the base class vtable pointer can't alias assignments to
3764 the derived class vtable pointer, since they have different
3765 types. Thus, in an derived class destructor, where the base
3766 class constructor was inlined, we could generate bad code for
3767 setting up the vtable pointer.
3769 Therefore, we use one type for all vtable pointers. We still
3770 use a type-correct type; it's just doesn't indicate the array
3771 bounds. That's better than using `void*' or some such; it's
3772 cleaner, and it let's the alias analysis code know that these
3773 stores cannot alias stores to void*! */
3774 vfield = build_lang_field_decl (FIELD_DECL, get_vfield_name (t),
3775 vtbl_ptr_type_node);
3776 /* If you change any of the below, take a look at all the
3777 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
3778 them too. */
3779 DECL_ASSEMBLER_NAME (vfield) = get_identifier (VFIELD_BASE);
3780 CLASSTYPE_VFIELD (t) = vfield;
3781 DECL_VIRTUAL_P (vfield) = 1;
3782 DECL_ARTIFICIAL (vfield) = 1;
3783 DECL_FIELD_CONTEXT (vfield) = t;
3784 DECL_CLASS_CONTEXT (vfield) = t;
3785 DECL_FCONTEXT (vfield) = t;
3786 DECL_SAVED_INSNS (vfield) = NULL_RTX;
3787 DECL_FIELD_SIZE (vfield) = 0;
3788 DECL_ALIGN (vfield) = TYPE_ALIGN (ptr_type_node);
3789 #if 0
3790 /* This is more efficient, but breaks binary compatibility, turn
3791 it on sometime when we don't care. If we turn it on, we also
3792 have to enable the code in dfs_init_vbase_pointers. */
3793 /* vfield is always first entry in structure. */
3794 TREE_CHAIN (vfield) = fields;
3795 fields = vfield;
3796 #else
3797 if (last_x)
3799 my_friendly_assert (TREE_CHAIN (last_x) == NULL_TREE, 175);
3800 TREE_CHAIN (last_x) = vfield;
3801 last_x = vfield;
3803 else
3804 fields = vfield;
3805 #endif
3806 empty = 0;
3807 vfields = chainon (vfields, build_tree_list (NULL_TREE, t));
3810 /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
3812 C++: maybe we will support default field initialization some day... */
3814 /* Delete all duplicate fields from the fields */
3815 delete_duplicate_fields (fields);
3817 /* Now we have the nearly final fieldlist for the data fields. Record it,
3818 then lay out the structure or union (including the fields). */
3820 TYPE_FIELDS (t) = fields;
3822 if (n_baseclasses)
3824 last_x = build_base_fields (t);
3826 /* If all our bases are empty, we can be empty too. */
3827 for (x = last_x; empty && x; x = TREE_CHAIN (x))
3828 if (DECL_SIZE (x) != integer_zero_node)
3829 empty = 0;
3832 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
3833 we have to save this before we start modifying
3834 TYPE_NONCOPIED_PARTS. */
3835 inline_friends = CLASSTYPE_INLINE_FRIENDS (t);
3836 CLASSTYPE_INLINE_FRIENDS (t) = NULL_TREE;
3838 if (empty)
3840 /* C++: do not let empty structures exist. */
3841 tree decl = build_lang_field_decl
3842 (FIELD_DECL, NULL_TREE, char_type_node);
3843 TREE_CHAIN (decl) = fields;
3844 TYPE_FIELDS (t) = decl;
3845 TYPE_NONCOPIED_PARTS (t)
3846 = tree_cons (NULL_TREE, decl, TYPE_NONCOPIED_PARTS (t));
3847 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
3850 if (n_baseclasses)
3851 TYPE_FIELDS (t) = chainon (last_x, TYPE_FIELDS (t));
3853 layout_type (t);
3855 /* Remember the size and alignment of the class before adding
3856 the virtual bases. */
3857 if (empty && flag_new_abi)
3858 CLASSTYPE_SIZE (t) = integer_zero_node;
3859 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
3860 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
3861 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
3862 else
3863 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
3864 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
3866 finish_struct_anon (t);
3868 /* Set the TYPE_DECL for this type to contain the right
3869 value for DECL_OFFSET, so that we can use it as part
3870 of a COMPONENT_REF for multiple inheritance. */
3872 layout_decl (TYPE_MAIN_DECL (t), 0);
3874 /* Now fix up any virtual base class types that we left lying
3875 around. We must get these done before we try to lay out the
3876 virtual function table. */
3877 pending_hard_virtuals = nreverse (pending_hard_virtuals);
3879 if (n_baseclasses)
3880 /* layout_basetypes will remove the base subobject fields. */
3881 max_has_virtual = layout_basetypes (t, max_has_virtual);
3882 if (empty)
3883 TYPE_FIELDS (t) = fields;
3885 my_friendly_assert (TYPE_FIELDS (t) == fields, 981117);
3887 /* Delete all zero-width bit-fields from the front of the fieldlist */
3888 while (fields && DECL_C_BIT_FIELD (fields)
3889 && DECL_INITIAL (fields))
3890 fields = TREE_CHAIN (fields);
3891 /* Delete all such fields from the rest of the fields. */
3892 for (x = fields; x;)
3894 if (TREE_CHAIN (x) && DECL_C_BIT_FIELD (TREE_CHAIN (x))
3895 && DECL_INITIAL (TREE_CHAIN (x)))
3896 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
3897 else
3898 x = TREE_CHAIN (x);
3900 TYPE_FIELDS (t) = fields;
3902 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
3904 tree vbases;
3906 vbases = CLASSTYPE_VBASECLASSES (t);
3909 /* Now fixup overrides of all functions in vtables from all
3910 direct or indirect virtual base classes. */
3911 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3912 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3914 for (i = 0; i < n_baseclasses; i++)
3916 tree base_binfo = TREE_VEC_ELT (binfos, i);
3917 tree basetype = BINFO_TYPE (base_binfo);
3918 tree vbases;
3920 vbases = CLASSTYPE_VBASECLASSES (basetype);
3921 while (vbases)
3923 merge_overrides (binfo_member (BINFO_TYPE (vbases),
3924 CLASSTYPE_VBASECLASSES (t)),
3925 vbases, 1, t);
3926 vbases = TREE_CHAIN (vbases);
3932 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
3933 might need to know it for setting up the offsets in the vtable
3934 (or in thunks) below. */
3935 if (vfield != NULL_TREE
3936 && DECL_FIELD_CONTEXT (vfield) != t)
3938 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
3939 tree offset = BINFO_OFFSET (binfo);
3941 vfield = copy_node (vfield);
3942 copy_lang_decl (vfield);
3944 if (! integer_zerop (offset))
3945 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
3946 DECL_FIELD_CONTEXT (vfield) = t;
3947 DECL_CLASS_CONTEXT (vfield) = t;
3948 DECL_FIELD_BITPOS (vfield)
3949 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
3950 CLASSTYPE_VFIELD (t) = vfield;
3953 #ifdef NOTQUITE
3954 cp_warning ("Doing hard virtuals for %T...", t);
3955 #endif
3957 if (has_virtual > max_has_virtual)
3958 max_has_virtual = has_virtual;
3959 if (max_has_virtual > 0)
3960 TYPE_VIRTUAL_P (t) = 1;
3962 if (flag_rtti && TYPE_VIRTUAL_P (t) && !pending_hard_virtuals)
3963 modify_all_vtables (t, NULL_TREE, NULL_TREE);
3965 while (pending_hard_virtuals)
3967 modify_all_vtables (t,
3968 TREE_PURPOSE (pending_hard_virtuals),
3969 TREE_VALUE (pending_hard_virtuals));
3970 pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
3973 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
3975 tree vbases;
3976 /* Now fixup any virtual function entries from virtual bases
3977 that have different deltas. This has to come after we do the
3978 pending hard virtuals, as we might have a function that comes
3979 from multiple virtual base instances that is only overridden
3980 by a hard virtual above. */
3981 vbases = CLASSTYPE_VBASECLASSES (t);
3982 while (vbases)
3984 /* We might be able to shorten the amount of work we do by
3985 only doing this for vtables that come from virtual bases
3986 that have differing offsets, but don't want to miss any
3987 entries. */
3988 fixup_vtable_deltas (vbases, 1, t);
3989 vbases = TREE_CHAIN (vbases);
3993 /* Under our model of GC, every C++ class gets its own virtual
3994 function table, at least virtually. */
3995 if (pending_virtuals)
3997 pending_virtuals = nreverse (pending_virtuals);
3998 /* We must enter these virtuals into the table. */
3999 if (first_vfn_base_index < 0)
4001 if (! CLASSTYPE_COM_INTERFACE (t))
4003 /* The second slot is for the tdesc pointer when thunks are used. */
4004 if (flag_vtable_thunks)
4005 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4007 /* The first slot is for the rtti offset. */
4008 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4010 set_rtti_entry (pending_virtuals,
4011 convert (ssizetype, integer_zero_node), t);
4013 build_vtable (NULL_TREE, t);
4015 else
4017 /* Here we know enough to change the type of our virtual
4018 function table, but we will wait until later this function. */
4020 if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
4021 build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index), t);
4024 /* If this type has basetypes with constructors, then those
4025 constructors might clobber the virtual function table. But
4026 they don't if the derived class shares the exact vtable of the base
4027 class. */
4029 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4031 else if (first_vfn_base_index >= 0)
4033 tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index);
4034 /* This class contributes nothing new to the virtual function
4035 table. However, it may have declared functions which
4036 went into the virtual function table "inherited" from the
4037 base class. If so, we grab a copy of those updated functions,
4038 and pretend they are ours. */
4040 /* See if we should steal the virtual info from base class. */
4041 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
4042 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
4043 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
4044 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
4045 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
4046 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4049 if (max_has_virtual || first_vfn_base_index >= 0)
4051 CLASSTYPE_VSIZE (t) = has_virtual;
4052 if (first_vfn_base_index >= 0)
4054 if (pending_virtuals)
4055 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t),
4056 pending_virtuals);
4058 else if (has_virtual)
4060 TYPE_BINFO_VIRTUALS (t) = pending_virtuals;
4061 DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1;
4065 /* Now lay out the virtual function table. */
4066 if (has_virtual)
4068 /* Use size_int so values are memoized in common cases. */
4069 tree itype = build_index_type (size_int (has_virtual));
4070 tree atype = build_cplus_array_type (vtable_entry_type, itype);
4072 layout_type (atype);
4074 CLASSTYPE_VFIELD (t) = vfield;
4076 /* We may have to grow the vtable. */
4077 if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
4079 TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
4080 DECL_SIZE (TYPE_BINFO_VTABLE (t)) = 0;
4081 layout_decl (TYPE_BINFO_VTABLE (t), 0);
4082 /* At one time the vtable info was grabbed 2 words at a time. This
4083 fails on sparc unless you have 8-byte alignment. (tiemann) */
4084 DECL_ALIGN (TYPE_BINFO_VTABLE (t))
4085 = MAX (TYPE_ALIGN (double_type_node),
4086 DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
4089 else if (first_vfn_base_index >= 0)
4090 CLASSTYPE_VFIELD (t) = vfield;
4091 CLASSTYPE_VFIELDS (t) = vfields;
4093 finish_struct_bits (t, max_has_virtual);
4095 /* Complete the rtl for any static member objects of the type we're
4096 working on. */
4097 for (x = fields; x; x = TREE_CHAIN (x))
4099 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4100 && TREE_TYPE (x) == t)
4102 DECL_MODE (x) = TYPE_MODE (t);
4103 make_decl_rtl (x, NULL, 0);
4107 /* Done with FIELDS...now decide whether to sort these for
4108 faster lookups later. Don't worry about optimizing
4109 for structs only declared in inline functions...they're
4110 not going to be referenced anywhere else.
4112 The C front-end only does this when n_fields > 15. We use
4113 a smaller number because most searches fail (succeeding
4114 ultimately as the search bores through the inheritance
4115 hierarchy), and we want this failure to occur quickly. */
4117 n_fields = count_fields (fields);
4118 if (n_fields > 7 && !allocation_temporary_p ())
4120 tree field_vec = make_tree_vec (n_fields);
4121 add_fields_to_vec (fields, field_vec, 0);
4122 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
4123 (int (*)(const void *, const void *))field_decl_cmp);
4124 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
4125 retrofit_lang_decl (TYPE_MAIN_DECL (t));
4126 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
4129 if (TYPE_HAS_CONSTRUCTOR (t))
4131 tree vfields = CLASSTYPE_VFIELDS (t);
4133 while (vfields)
4135 /* Mark the fact that constructor for T
4136 could affect anybody inheriting from T
4137 who wants to initialize vtables for VFIELDS's type. */
4138 if (VF_DERIVED_VALUE (vfields))
4139 TREE_ADDRESSABLE (vfields) = 1;
4140 vfields = TREE_CHAIN (vfields);
4144 /* Write out inline function definitions. */
4145 do_inline_function_hair (t, inline_friends);
4147 if (CLASSTYPE_VSIZE (t) != 0)
4149 #if 0
4150 /* This is now done above. */
4151 if (DECL_FIELD_CONTEXT (vfield) != t)
4153 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4154 tree offset = BINFO_OFFSET (binfo);
4156 vfield = copy_node (vfield);
4157 copy_lang_decl (vfield);
4159 if (! integer_zerop (offset))
4160 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4161 DECL_FIELD_CONTEXT (vfield) = t;
4162 DECL_CLASS_CONTEXT (vfield) = t;
4163 DECL_FIELD_BITPOS (vfield)
4164 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4165 CLASSTYPE_VFIELD (t) = vfield;
4167 #endif
4169 /* In addition to this one, all the other vfields should be listed. */
4170 /* Before that can be done, we have to have FIELD_DECLs for them, and
4171 a place to find them. */
4172 TYPE_NONCOPIED_PARTS (t)
4173 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
4174 vfield, TYPE_NONCOPIED_PARTS (t));
4176 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
4177 && DECL_VINDEX (TREE_VEC_ELT (method_vec, 1)) == NULL_TREE)
4178 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
4182 /* Make the rtl for any new vtables we have created, and unmark
4183 the base types we marked. */
4184 finish_vtbls (TYPE_BINFO (t), 1, t);
4185 hack_incomplete_structures (t);
4187 #if 0
4188 if (TYPE_NAME (t) && TYPE_IDENTIFIER (t))
4189 undo_template_name_overload (TYPE_IDENTIFIER (t), 1);
4190 #endif
4192 resume_momentary (old);
4194 if (warn_overloaded_virtual)
4195 warn_hidden (t);
4197 #if 0
4198 /* This has to be done after we have sorted out what to do with
4199 the enclosing type. */
4200 if (write_symbols != DWARF_DEBUG)
4202 /* Be smarter about nested classes here. If a type is nested,
4203 only output it if we would output the enclosing type. */
4204 if (DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (t)))
4205 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = TREE_ASM_WRITTEN (TYPE_MAIN_DECL (t));
4207 #endif
4209 if (write_symbols != DWARF_DEBUG && write_symbols != DWARF2_DEBUG)
4211 /* If the type has methods, we want to think about cutting down
4212 the amount of symbol table stuff we output. The value stored in
4213 the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
4214 For example, if a member function is seen and we decide to
4215 write out that member function, then we can change the value
4216 of the DECL_IGNORED_P slot, and the type will be output when
4217 that member function's debug info is written out.
4219 We can't do this with DWARF, which does not support name
4220 references between translation units. */
4221 if (CLASSTYPE_METHOD_VEC (t))
4223 /* Don't output full info about any type
4224 which does not have its implementation defined here. */
4225 if (CLASSTYPE_INTERFACE_ONLY (t))
4226 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4227 #if 0
4228 /* XXX do something about this. */
4229 else if (CLASSTYPE_INTERFACE_UNKNOWN (t))
4230 /* Only a first approximation! */
4231 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4232 #endif
4234 else if (CLASSTYPE_INTERFACE_ONLY (t))
4235 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4238 /* Finish debugging output for this type. */
4239 rest_of_type_compilation (t, toplevel_bindings_p ());
4241 return;
4244 /* When T was built up, the member declarations were added in reverse
4245 order. Rearrange them to declaration order. */
4247 void
4248 unreverse_member_declarations (t)
4249 tree t;
4251 tree next;
4252 tree prev;
4253 tree x;
4255 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
4256 reverse order. Put them in declaration order now. */
4257 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
4258 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
4260 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
4261 reverse order, so we can't just use nreverse. */
4262 prev = NULL_TREE;
4263 for (x = TYPE_FIELDS (t);
4264 x && TREE_CODE (x) != TYPE_DECL;
4265 x = next)
4267 next = TREE_CHAIN (x);
4268 TREE_CHAIN (x) = prev;
4269 prev = x;
4271 if (prev)
4273 TREE_CHAIN (TYPE_FIELDS (t)) = x;
4274 if (prev)
4275 TYPE_FIELDS (t) = prev;
4279 tree
4280 finish_struct (t, attributes)
4281 tree t, attributes;
4283 /* Append the fields we need for constructing signature tables. */
4284 if (IS_SIGNATURE (t))
4285 append_signature_fields (t);
4287 /* Now that we've got all the field declarations, reverse everything
4288 as necessary. */
4289 unreverse_member_declarations (t);
4291 cplus_decl_attributes (t, attributes, NULL_TREE);
4293 if (processing_template_decl)
4295 finish_struct_methods (t);
4296 TYPE_SIZE (t) = integer_zero_node;
4298 else
4299 finish_struct_1 (t);
4301 TYPE_BEING_DEFINED (t) = 0;
4303 if (current_class_type)
4304 popclass ();
4305 else
4306 error ("trying to finish struct, but kicked out due to previous parse errors.");
4308 return t;
4311 /* Return the dynamic type of INSTANCE, if known.
4312 Used to determine whether the virtual function table is needed
4313 or not.
4315 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4316 of our knowledge of its type. */
4318 static tree
4319 fixed_type_or_null (instance, nonnull)
4320 tree instance;
4321 int *nonnull;
4323 switch (TREE_CODE (instance))
4325 case INDIRECT_REF:
4326 /* Check that we are not going through a cast of some sort. */
4327 if (TREE_TYPE (instance)
4328 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
4329 instance = TREE_OPERAND (instance, 0);
4330 /* fall through... */
4331 case CALL_EXPR:
4332 /* This is a call to a constructor, hence it's never zero. */
4333 if (TREE_HAS_CONSTRUCTOR (instance))
4335 if (nonnull)
4336 *nonnull = 1;
4337 return TREE_TYPE (instance);
4339 return NULL_TREE;
4341 case SAVE_EXPR:
4342 /* This is a call to a constructor, hence it's never zero. */
4343 if (TREE_HAS_CONSTRUCTOR (instance))
4345 if (nonnull)
4346 *nonnull = 1;
4347 return TREE_TYPE (instance);
4349 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4351 case RTL_EXPR:
4352 return NULL_TREE;
4354 case PLUS_EXPR:
4355 case MINUS_EXPR:
4356 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
4357 /* Propagate nonnull. */
4358 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4359 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
4360 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4361 return NULL_TREE;
4363 case NOP_EXPR:
4364 case CONVERT_EXPR:
4365 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4367 case ADDR_EXPR:
4368 if (nonnull)
4369 *nonnull = 1;
4370 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4372 case COMPONENT_REF:
4373 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
4375 case VAR_DECL:
4376 case FIELD_DECL:
4377 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
4378 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
4380 if (nonnull)
4381 *nonnull = 1;
4382 return TREE_TYPE (TREE_TYPE (instance));
4384 /* fall through... */
4385 case TARGET_EXPR:
4386 case PARM_DECL:
4387 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
4389 if (nonnull)
4390 *nonnull = 1;
4391 return TREE_TYPE (instance);
4393 else if (nonnull)
4395 if (instance == current_class_ptr
4396 && flag_this_is_variable <= 0)
4398 /* Normally, 'this' must be non-null. */
4399 if (flag_this_is_variable == 0)
4400 *nonnull = 1;
4402 /* <0 means we're in a constructor and we know our type. */
4403 if (flag_this_is_variable < 0)
4404 return TREE_TYPE (TREE_TYPE (instance));
4406 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4407 /* Reference variables should be references to objects. */
4408 *nonnull = 1;
4410 return NULL_TREE;
4412 default:
4413 return NULL_TREE;
4417 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
4418 to the static type. We also handle the case where INSTANCE is really
4419 a pointer.
4421 Used to determine whether the virtual function table is needed
4422 or not.
4424 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4425 of our knowledge of its type. */
4428 resolves_to_fixed_type_p (instance, nonnull)
4429 tree instance;
4430 int *nonnull;
4432 tree t = TREE_TYPE (instance);
4433 tree fixed = fixed_type_or_null (instance, nonnull);
4434 if (fixed == NULL_TREE)
4435 return 0;
4436 if (POINTER_TYPE_P (t))
4437 t = TREE_TYPE (t);
4438 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
4442 void
4443 init_class_processing ()
4445 current_class_depth = 0;
4446 current_class_stack_size = 10;
4447 current_class_stack
4448 = (class_stack_node_t) xmalloc (current_class_stack_size
4449 * sizeof (struct class_stack_node));
4451 current_lang_stacksize = 10;
4452 current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree));
4453 current_lang_stack = current_lang_base;
4455 access_default_node = build_int_2 (0, 0);
4456 access_public_node = build_int_2 (1, 0);
4457 access_protected_node = build_int_2 (2, 0);
4458 access_private_node = build_int_2 (3, 0);
4459 access_default_virtual_node = build_int_2 (4, 0);
4460 access_public_virtual_node = build_int_2 (5, 0);
4461 access_protected_virtual_node = build_int_2 (6, 0);
4462 access_private_virtual_node = build_int_2 (7, 0);
4464 /* Keep these values lying around. */
4465 base_layout_decl = build_lang_field_decl (FIELD_DECL, NULL_TREE, error_mark_node);
4466 TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE);
4468 gcc_obstack_init (&class_obstack);
4471 /* Set current scope to NAME. CODE tells us if this is a
4472 STRUCT, UNION, or ENUM environment.
4474 NAME may end up being NULL_TREE if this is an anonymous or
4475 late-bound struct (as in "struct { ... } foo;") */
4477 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
4478 appropriate values, found by looking up the type definition of
4479 NAME (as a CODE).
4481 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
4482 which can be seen locally to the class. They are shadowed by
4483 any subsequent local declaration (including parameter names).
4485 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
4486 which have static meaning (i.e., static members, static
4487 member functions, enum declarations, etc).
4489 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
4490 which can be seen locally to the class (as in 1), but
4491 know that we are doing this for declaration purposes
4492 (i.e. friend foo::bar (int)).
4494 So that we may avoid calls to lookup_name, we cache the _TYPE
4495 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
4497 For multiple inheritance, we perform a two-pass depth-first search
4498 of the type lattice. The first pass performs a pre-order search,
4499 marking types after the type has had its fields installed in
4500 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
4501 unmarks the marked types. If a field or member function name
4502 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
4503 that name becomes `error_mark_node'. */
4505 void
4506 pushclass (type, modify)
4507 tree type;
4508 int modify;
4510 type = TYPE_MAIN_VARIANT (type);
4512 /* Make sure there is enough room for the new entry on the stack. */
4513 if (current_class_depth + 1 >= current_class_stack_size)
4515 current_class_stack_size *= 2;
4516 current_class_stack
4517 = (class_stack_node_t) xrealloc (current_class_stack,
4518 current_class_stack_size
4519 * sizeof (struct class_stack_node));
4522 /* Insert a new entry on the class stack. */
4523 current_class_stack[current_class_depth].name = current_class_name;
4524 current_class_stack[current_class_depth].type = current_class_type;
4525 current_class_stack[current_class_depth].access = current_access_specifier;
4526 current_class_stack[current_class_depth].names_used = 0;
4527 current_class_depth++;
4529 /* Now set up the new type. */
4530 current_class_name = TYPE_NAME (type);
4531 if (TREE_CODE (current_class_name) == TYPE_DECL)
4532 current_class_name = DECL_NAME (current_class_name);
4533 current_class_type = type;
4535 /* By default, things in classes are private, while things in
4536 structures or unions are public. */
4537 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
4538 ? access_private_node
4539 : access_public_node);
4541 if (previous_class_type != NULL_TREE
4542 && (type != previous_class_type
4543 || TYPE_SIZE (previous_class_type) == NULL_TREE)
4544 && current_class_depth == 1)
4546 /* Forcibly remove any old class remnants. */
4547 invalidate_class_lookup_cache ();
4549 /* Now, free the obstack on which we cached all the values. */
4550 if (class_cache_firstobj)
4551 obstack_free (&class_cache_obstack, class_cache_firstobj);
4552 class_cache_firstobj
4553 = (char*) obstack_finish (&class_cache_obstack);
4556 /* If we're about to enter a nested class, clear
4557 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
4558 if (modify && current_class_depth > 1)
4559 clear_identifier_class_values ();
4561 pushlevel_class ();
4563 #if 0
4564 if (CLASSTYPE_TEMPLATE_INFO (type))
4565 overload_template_name (type);
4566 #endif
4568 if (modify)
4570 if (type != previous_class_type || current_class_depth > 1)
4571 push_class_decls (type);
4572 else
4574 tree item;
4576 /* We are re-entering the same class we just left, so we
4577 don't have to search the whole inheritance matrix to find
4578 all the decls to bind again. Instead, we install the
4579 cached class_shadowed list, and walk through it binding
4580 names and setting up IDENTIFIER_TYPE_VALUEs. */
4581 set_class_shadows (previous_class_values);
4582 for (item = previous_class_values; item; item = TREE_CHAIN (item))
4584 tree id = TREE_PURPOSE (item);
4585 tree decl = TREE_TYPE (item);
4587 push_class_binding (id, decl);
4588 if (TREE_CODE (decl) == TYPE_DECL)
4589 set_identifier_type_value (id, TREE_TYPE (decl));
4591 unuse_fields (type);
4594 storetags (CLASSTYPE_TAGS (type));
4598 /* When we exit a toplevel class scope, we save the
4599 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
4600 reenter the class. Here, we've entered some other class, so we
4601 must invalidate our cache. */
4603 void
4604 invalidate_class_lookup_cache ()
4606 tree t;
4608 /* This code can be seen as a cache miss. When we've cached a
4609 class' scope's bindings and we can't use them, we need to reset
4610 them. This is it! */
4611 for (t = previous_class_values; t; t = TREE_CHAIN (t))
4612 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
4614 previous_class_type = NULL_TREE;
4617 /* Get out of the current class scope. If we were in a class scope
4618 previously, that is the one popped to. */
4620 void
4621 popclass ()
4623 poplevel (1, 0, 0);
4624 /* Since poplevel_class does the popping of class decls nowadays,
4625 this really only frees the obstack used for these decls. */
4626 pop_class_decls ();
4628 current_class_depth--;
4629 current_class_name = current_class_stack[current_class_depth].name;
4630 current_class_type = current_class_stack[current_class_depth].type;
4631 current_access_specifier = current_class_stack[current_class_depth].access;
4632 if (current_class_stack[current_class_depth].names_used)
4633 splay_tree_delete (current_class_stack[current_class_depth].names_used);
4636 /* Returns 1 if current_class_type is either T or a nested type of T. */
4639 currently_open_class (t)
4640 tree t;
4642 int i;
4643 if (t == current_class_type)
4644 return 1;
4645 for (i = 0; i < current_class_depth; ++i)
4646 if (current_class_stack [i].type == t)
4647 return 1;
4648 return 0;
4651 /* When entering a class scope, all enclosing class scopes' names with
4652 static meaning (static variables, static functions, types and enumerators)
4653 have to be visible. This recursive function calls pushclass for all
4654 enclosing class contexts until global or a local scope is reached.
4655 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
4656 formal of the same name. */
4658 void
4659 push_nested_class (type, modify)
4660 tree type;
4661 int modify;
4663 tree context;
4665 /* A namespace might be passed in error cases, like A::B:C. */
4666 if (type == NULL_TREE || type == error_mark_node || ! IS_AGGR_TYPE (type)
4667 || TREE_CODE (type) == NAMESPACE_DECL
4668 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
4669 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
4670 return;
4672 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
4674 if (context && CLASS_TYPE_P (context))
4675 push_nested_class (context, 2);
4676 pushclass (type, modify);
4679 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
4681 void
4682 pop_nested_class ()
4684 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
4686 popclass ();
4687 if (context && CLASS_TYPE_P (context))
4688 pop_nested_class ();
4691 /* Set global variables CURRENT_LANG_NAME to appropriate value
4692 so that behavior of name-mangling machinery is correct. */
4694 void
4695 push_lang_context (name)
4696 tree name;
4698 *current_lang_stack++ = current_lang_name;
4699 if (current_lang_stack >= current_lang_base + current_lang_stacksize)
4701 current_lang_base
4702 = (tree *)xrealloc (current_lang_base,
4703 sizeof (tree) * (current_lang_stacksize + 10));
4704 current_lang_stack = current_lang_base + current_lang_stacksize;
4705 current_lang_stacksize += 10;
4708 if (name == lang_name_cplusplus)
4710 strict_prototype = strict_prototypes_lang_cplusplus;
4711 current_lang_name = name;
4713 else if (name == lang_name_java)
4715 strict_prototype = strict_prototypes_lang_cplusplus;
4716 current_lang_name = name;
4717 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
4718 (See record_builtin_java_type in decl.c.) However, that causes
4719 incorrect debug entries if these types are actually used.
4720 So we re-enable debug output after extern "Java". */
4721 DECL_IGNORED_P (java_byte_type_node) = 0;
4722 DECL_IGNORED_P (java_short_type_node) = 0;
4723 DECL_IGNORED_P (java_int_type_node) = 0;
4724 DECL_IGNORED_P (java_long_type_node) = 0;
4725 DECL_IGNORED_P (java_float_type_node) = 0;
4726 DECL_IGNORED_P (java_double_type_node) = 0;
4727 DECL_IGNORED_P (java_char_type_node) = 0;
4728 DECL_IGNORED_P (java_boolean_type_node) = 0;
4730 else if (name == lang_name_c)
4732 strict_prototype = strict_prototypes_lang_c;
4733 current_lang_name = name;
4735 else
4736 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
4739 /* Get out of the current language scope. */
4741 void
4742 pop_lang_context ()
4744 current_lang_name = *--current_lang_stack;
4745 if (current_lang_name == lang_name_cplusplus
4746 || current_lang_name == lang_name_java)
4747 strict_prototype = strict_prototypes_lang_cplusplus;
4748 else if (current_lang_name == lang_name_c)
4749 strict_prototype = strict_prototypes_lang_c;
4752 /* Type instantiation routines. */
4754 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
4755 matches the TARGET_TYPE. If there is no satisfactory match, return
4756 error_mark_node, and issue an error message if COMPLAIN is
4757 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
4758 was a template-id, and EXPLICIT_TARGS are the explicitly provided
4759 template arguments. */
4761 static tree
4762 resolve_address_of_overloaded_function (target_type,
4763 overload,
4764 complain,
4765 template_only,
4766 explicit_targs)
4767 tree target_type;
4768 tree overload;
4769 int complain;
4770 int template_only;
4771 tree explicit_targs;
4773 /* Here's what the standard says:
4775 [over.over]
4777 If the name is a function template, template argument deduction
4778 is done, and if the argument deduction succeeds, the deduced
4779 arguments are used to generate a single template function, which
4780 is added to the set of overloaded functions considered.
4782 Non-member functions and static member functions match targets of
4783 type "pointer-to-function" or "reference-to-function." Nonstatic
4784 member functions match targets of type "pointer-to-member
4785 function;" the function type of the pointer to member is used to
4786 select the member function from the set of overloaded member
4787 functions. If a nonstatic member function is selected, the
4788 reference to the overloaded function name is required to have the
4789 form of a pointer to member as described in 5.3.1.
4791 If more than one function is selected, any template functions in
4792 the set are eliminated if the set also contains a non-template
4793 function, and any given template function is eliminated if the
4794 set contains a second template function that is more specialized
4795 than the first according to the partial ordering rules 14.5.5.2.
4796 After such eliminations, if any, there shall remain exactly one
4797 selected function. */
4799 int is_ptrmem = 0;
4800 int is_reference = 0;
4801 /* We store the matches in a TREE_LIST rooted here. The functions
4802 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
4803 interoperability with most_specialized_instantiation. */
4804 tree matches = NULL_TREE;
4805 tree fn;
4807 /* By the time we get here, we should be seeing only real
4808 pointer-to-member types, not the internal POINTER_TYPE to
4809 METHOD_TYPE representation. */
4810 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
4811 && (TREE_CODE (TREE_TYPE (target_type))
4812 == METHOD_TYPE)), 0);
4814 /* Check that the TARGET_TYPE is reasonable. */
4815 if (TYPE_PTRFN_P (target_type))
4816 /* This is OK. */
4818 else if (TYPE_PTRMEMFUNC_P (target_type))
4819 /* This is OK, too. */
4820 is_ptrmem = 1;
4821 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
4823 /* This is OK, too. This comes from a conversion to reference
4824 type. */
4825 target_type = build_reference_type (target_type);
4826 is_reference = 1;
4828 else
4830 if (complain)
4831 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
4832 DECL_NAME (OVL_FUNCTION (overload)), target_type);
4833 return error_mark_node;
4836 /* If we can find a non-template function that matches, we can just
4837 use it. There's no point in generating template instantiations
4838 if we're just going to throw them out anyhow. But, of course, we
4839 can only do this when we don't *need* a template function. */
4840 if (!template_only)
4842 tree fns;
4844 for (fns = overload; fns; fns = OVL_CHAIN (fns))
4846 tree fn = OVL_FUNCTION (fns);
4847 tree fntype;
4849 if (TREE_CODE (fn) == TEMPLATE_DECL)
4850 /* We're not looking for templates just yet. */
4851 continue;
4853 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4854 != is_ptrmem)
4855 /* We're looking for a non-static member, and this isn't
4856 one, or vice versa. */
4857 continue;
4859 /* See if there's a match. */
4860 fntype = TREE_TYPE (fn);
4861 if (is_ptrmem)
4862 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
4863 else if (!is_reference)
4864 fntype = build_pointer_type (fntype);
4866 if (can_convert_arg (target_type, fntype, fn))
4867 matches = scratch_tree_cons (fn, NULL_TREE, matches);
4871 /* Now, if we've already got a match (or matches), there's no need
4872 to proceed to the template functions. But, if we don't have a
4873 match we need to look at them, too. */
4874 if (!matches)
4876 tree target_fn_type;
4877 tree target_arg_types;
4878 tree fns;
4880 if (is_ptrmem)
4881 target_fn_type
4882 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
4883 else
4884 target_fn_type = TREE_TYPE (target_type);
4885 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
4887 for (fns = overload; fns; fns = OVL_CHAIN (fns))
4889 tree fn = OVL_FUNCTION (fns);
4890 tree instantiation;
4891 tree instantiation_type;
4892 tree targs;
4894 if (TREE_CODE (fn) != TEMPLATE_DECL)
4895 /* We're only looking for templates. */
4896 continue;
4898 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4899 != is_ptrmem)
4900 /* We're not looking for a non-static member, and this is
4901 one, or vice versa. */
4902 continue;
4904 /* Try to do argument deduction. */
4905 targs = make_scratch_vec (DECL_NTPARMS (fn));
4906 if (fn_type_unification (fn, explicit_targs, targs,
4907 target_arg_types, NULL_TREE,
4908 DEDUCE_EXACT) != 0)
4909 /* Argument deduction failed. */
4910 continue;
4912 /* Instantiate the template. */
4913 instantiation = instantiate_template (fn, targs);
4914 if (instantiation == error_mark_node)
4915 /* Instantiation failed. */
4916 continue;
4918 /* See if there's a match. */
4919 instantiation_type = TREE_TYPE (instantiation);
4920 if (is_ptrmem)
4921 instantiation_type =
4922 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
4923 else if (!is_reference)
4924 instantiation_type = build_pointer_type (instantiation_type);
4925 if (can_convert_arg (target_type, instantiation_type, instantiation))
4926 matches = scratch_tree_cons (instantiation, fn, matches);
4929 /* Now, remove all but the most specialized of the matches. */
4930 if (matches)
4932 tree match = most_specialized_instantiation (matches,
4933 explicit_targs);
4935 if (match != error_mark_node)
4936 matches = scratch_tree_cons (match, NULL_TREE, NULL_TREE);
4940 /* Now we should have exactly one function in MATCHES. */
4941 if (matches == NULL_TREE)
4943 /* There were *no* matches. */
4944 if (complain)
4946 cp_error ("no matches converting function `%D' to type `%#T'",
4947 DECL_NAME (OVL_FUNCTION (overload)),
4948 target_type);
4950 /* print_candidates expects a chain with the functions in
4951 TREE_VALUE slots, so we cons one up here (we're losing anyway,
4952 so why be clever?). */
4953 for (; overload; overload = OVL_NEXT (overload))
4954 matches = scratch_tree_cons (NULL_TREE, OVL_CURRENT (overload),
4955 matches);
4957 print_candidates (matches);
4959 return error_mark_node;
4961 else if (TREE_CHAIN (matches))
4963 /* There were too many matches. */
4965 if (complain)
4967 tree match;
4969 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
4970 DECL_NAME (OVL_FUNCTION (overload)),
4971 target_type);
4973 /* Since print_candidates expects the functions in the
4974 TREE_VALUE slot, we flip them here. */
4975 for (match = matches; match; match = TREE_CHAIN (match))
4976 TREE_VALUE (match) = TREE_PURPOSE (match);
4978 print_candidates (matches);
4981 return error_mark_node;
4984 /* Good, exactly one match. Now, convert it to the correct type. */
4985 fn = TREE_PURPOSE (matches);
4987 mark_used (fn);
4989 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
4990 return build_unary_op (ADDR_EXPR, fn, 0);
4991 else
4993 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
4994 will mark the function as addressed, but here we must do it
4995 explicitly. */
4996 mark_addressable (fn);
4998 return fn;
5002 /* This function will instantiate the type of the expression given in
5003 RHS to match the type of LHSTYPE. If errors exist, then return
5004 error_mark_node. We only complain is COMPLAIN is set. If we are
5005 not complaining, never modify rhs, as overload resolution wants to
5006 try many possible instantiations, in hopes that at least one will
5007 work.
5009 FLAGS is a bitmask, as we see at the top of the function.
5011 For non-recursive calls, LHSTYPE should be a function, pointer to
5012 function, or a pointer to member function. */
5014 tree
5015 instantiate_type (lhstype, rhs, flags)
5016 tree lhstype, rhs;
5017 int flags;
5019 int complain = (flags & 1);
5020 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
5022 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
5024 if (complain)
5025 error ("not enough type information");
5026 return error_mark_node;
5029 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
5031 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
5032 return rhs;
5033 if (complain)
5034 cp_error ("argument of type `%T' does not match `%T'",
5035 TREE_TYPE (rhs), lhstype);
5036 return error_mark_node;
5039 /* We don't overwrite rhs if it is an overloaded function.
5040 Copying it would destroy the tree link. */
5041 if (TREE_CODE (rhs) != OVERLOAD)
5042 rhs = copy_node (rhs);
5044 /* This should really only be used when attempting to distinguish
5045 what sort of a pointer to function we have. For now, any
5046 arithmetic operation which is not supported on pointers
5047 is rejected as an error. */
5049 switch (TREE_CODE (rhs))
5051 case TYPE_EXPR:
5052 case CONVERT_EXPR:
5053 case SAVE_EXPR:
5054 case CONSTRUCTOR:
5055 case BUFFER_REF:
5056 my_friendly_abort (177);
5057 return error_mark_node;
5059 case INDIRECT_REF:
5060 case ARRAY_REF:
5062 tree new_rhs;
5064 new_rhs = instantiate_type (build_pointer_type (lhstype),
5065 TREE_OPERAND (rhs, 0), flags);
5066 if (new_rhs == error_mark_node)
5067 return error_mark_node;
5069 TREE_TYPE (rhs) = lhstype;
5070 TREE_OPERAND (rhs, 0) = new_rhs;
5071 return rhs;
5074 case NOP_EXPR:
5075 rhs = copy_node (TREE_OPERAND (rhs, 0));
5076 TREE_TYPE (rhs) = unknown_type_node;
5077 return instantiate_type (lhstype, rhs, flags);
5079 case COMPONENT_REF:
5081 tree field = TREE_OPERAND (rhs, 1);
5082 tree r;
5084 r = instantiate_type (lhstype, field, flags);
5086 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype))
5088 if (complain)
5090 tree t = TYPE_PTRMEMFUNC_OBJECT_TYPE (lhstype);
5092 if (TREE_CODE (field) == OVERLOAD)
5093 field = OVL_FUNCTION (field);
5094 if (TREE_CODE (field) == FUNCTION_DECL)
5096 cp_pedwarn ("object-dependent reference `%E' can only be used in a call",
5097 DECL_NAME (field));
5098 cp_pedwarn (" to form a pointer to member function, say `&%T::%E'",
5099 t, DECL_NAME (field));
5101 else
5102 cp_pedwarn ("object-dependent reference can only be used in a call");
5104 return r;
5107 return r;
5110 case OFFSET_REF:
5111 rhs = TREE_OPERAND (rhs, 1);
5112 if (BASELINK_P (rhs))
5113 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5115 /* This can happen if we are forming a pointer-to-member for a
5116 member template. */
5117 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
5119 /* Fall through. */
5121 case TEMPLATE_ID_EXPR:
5122 return
5123 resolve_address_of_overloaded_function (lhstype,
5124 TREE_OPERAND (rhs, 0),
5125 complain,
5126 /*template_only=*/1,
5127 TREE_OPERAND (rhs, 1));
5129 case OVERLOAD:
5130 return
5131 resolve_address_of_overloaded_function (lhstype,
5132 rhs,
5133 complain,
5134 /*template_only=*/0,
5135 /*explicit_targs=*/NULL_TREE);
5137 case TREE_LIST:
5138 /* Now we should have a baselink. */
5139 my_friendly_assert (BASELINK_P (rhs), 990412);
5141 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5143 case CALL_EXPR:
5144 /* This is too hard for now. */
5145 my_friendly_abort (183);
5146 return error_mark_node;
5148 case PLUS_EXPR:
5149 case MINUS_EXPR:
5150 case COMPOUND_EXPR:
5151 TREE_OPERAND (rhs, 0)
5152 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5153 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5154 return error_mark_node;
5155 TREE_OPERAND (rhs, 1)
5156 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5157 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5158 return error_mark_node;
5160 TREE_TYPE (rhs) = lhstype;
5161 return rhs;
5163 case MULT_EXPR:
5164 case TRUNC_DIV_EXPR:
5165 case FLOOR_DIV_EXPR:
5166 case CEIL_DIV_EXPR:
5167 case ROUND_DIV_EXPR:
5168 case RDIV_EXPR:
5169 case TRUNC_MOD_EXPR:
5170 case FLOOR_MOD_EXPR:
5171 case CEIL_MOD_EXPR:
5172 case ROUND_MOD_EXPR:
5173 case FIX_ROUND_EXPR:
5174 case FIX_FLOOR_EXPR:
5175 case FIX_CEIL_EXPR:
5176 case FIX_TRUNC_EXPR:
5177 case FLOAT_EXPR:
5178 case NEGATE_EXPR:
5179 case ABS_EXPR:
5180 case MAX_EXPR:
5181 case MIN_EXPR:
5182 case FFS_EXPR:
5184 case BIT_AND_EXPR:
5185 case BIT_IOR_EXPR:
5186 case BIT_XOR_EXPR:
5187 case LSHIFT_EXPR:
5188 case RSHIFT_EXPR:
5189 case LROTATE_EXPR:
5190 case RROTATE_EXPR:
5192 case PREINCREMENT_EXPR:
5193 case PREDECREMENT_EXPR:
5194 case POSTINCREMENT_EXPR:
5195 case POSTDECREMENT_EXPR:
5196 if (complain)
5197 error ("invalid operation on uninstantiated type");
5198 return error_mark_node;
5200 case TRUTH_AND_EXPR:
5201 case TRUTH_OR_EXPR:
5202 case TRUTH_XOR_EXPR:
5203 case LT_EXPR:
5204 case LE_EXPR:
5205 case GT_EXPR:
5206 case GE_EXPR:
5207 case EQ_EXPR:
5208 case NE_EXPR:
5209 case TRUTH_ANDIF_EXPR:
5210 case TRUTH_ORIF_EXPR:
5211 case TRUTH_NOT_EXPR:
5212 if (complain)
5213 error ("not enough type information");
5214 return error_mark_node;
5216 case COND_EXPR:
5217 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
5219 if (complain)
5220 error ("not enough type information");
5221 return error_mark_node;
5223 TREE_OPERAND (rhs, 1)
5224 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5225 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5226 return error_mark_node;
5227 TREE_OPERAND (rhs, 2)
5228 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
5229 if (TREE_OPERAND (rhs, 2) == error_mark_node)
5230 return error_mark_node;
5232 TREE_TYPE (rhs) = lhstype;
5233 return rhs;
5235 case MODIFY_EXPR:
5236 TREE_OPERAND (rhs, 1)
5237 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5238 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5239 return error_mark_node;
5241 TREE_TYPE (rhs) = lhstype;
5242 return rhs;
5244 case ADDR_EXPR:
5245 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5247 case ENTRY_VALUE_EXPR:
5248 my_friendly_abort (184);
5249 return error_mark_node;
5251 case ERROR_MARK:
5252 return error_mark_node;
5254 default:
5255 my_friendly_abort (185);
5256 return error_mark_node;
5260 /* Return the name of the virtual function pointer field
5261 (as an IDENTIFIER_NODE) for the given TYPE. Note that
5262 this may have to look back through base types to find the
5263 ultimate field name. (For single inheritance, these could
5264 all be the same name. Who knows for multiple inheritance). */
5266 static tree
5267 get_vfield_name (type)
5268 tree type;
5270 tree binfo = TYPE_BINFO (type);
5271 char *buf;
5273 while (BINFO_BASETYPES (binfo)
5274 && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
5275 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
5276 binfo = BINFO_BASETYPE (binfo, 0);
5278 type = BINFO_TYPE (binfo);
5279 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
5280 + TYPE_NAME_LENGTH (type) + 2);
5281 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
5282 return get_identifier (buf);
5285 void
5286 print_class_statistics ()
5288 #ifdef GATHER_STATISTICS
5289 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
5290 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
5291 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
5292 n_build_method_call, n_inner_fields_searched);
5293 if (n_vtables)
5295 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
5296 n_vtables, n_vtable_searches);
5297 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
5298 n_vtable_entries, n_vtable_elems);
5300 #endif
5303 /* Push an obstack which is sufficiently long-lived to hold such class
5304 decls that may be cached in the previous_class_values list. The
5305 effect is undone by pop_obstacks. */
5307 void
5308 push_cache_obstack ()
5310 static int cache_obstack_initialized;
5312 if (!cache_obstack_initialized)
5314 gcc_obstack_init (&class_cache_obstack);
5315 class_cache_firstobj
5316 = (char*) obstack_finish (&class_cache_obstack);
5317 cache_obstack_initialized = 1;
5320 push_obstacks_nochange ();
5321 current_obstack = &class_cache_obstack;
5324 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
5325 according to [class]:
5326 The class-name is also inserted
5327 into the scope of the class itself. For purposes of access checking,
5328 the inserted class name is treated as if it were a public member name. */
5330 void
5331 build_self_reference ()
5333 tree name = constructor_name (current_class_type);
5334 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
5335 tree saved_cas;
5337 DECL_NONLOCAL (value) = 1;
5338 DECL_CONTEXT (value) = current_class_type;
5339 DECL_CLASS_CONTEXT (value) = current_class_type;
5340 DECL_ARTIFICIAL (value) = 1;
5342 saved_cas = current_access_specifier;
5343 current_access_specifier = access_public_node;
5344 finish_member_declaration (value);
5345 current_access_specifier = saved_cas;
5348 /* Returns 1 if TYPE contains only padding bytes. */
5351 is_empty_class (type)
5352 tree type;
5354 tree t;
5356 if (type == error_mark_node)
5357 return 0;
5359 if (! IS_AGGR_TYPE (type))
5360 return 0;
5362 if (flag_new_abi)
5363 return CLASSTYPE_SIZE (type) == integer_zero_node;
5365 if (TYPE_BINFO_BASETYPES (type))
5366 return 0;
5367 t = TYPE_FIELDS (type);
5368 while (t && TREE_CODE (t) != FIELD_DECL)
5369 t = TREE_CHAIN (t);
5370 return (t == NULL_TREE);
5373 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
5374 a *_TYPE node. NODE can also be a local class. */
5376 tree
5377 get_enclosing_class (type)
5378 tree type;
5380 tree node = type;
5382 while (node && TREE_CODE (node) != NAMESPACE_DECL)
5384 switch (TREE_CODE_CLASS (TREE_CODE (node)))
5386 case 'd':
5387 node = DECL_CONTEXT (node);
5388 break;
5390 case 't':
5391 if (node != type)
5392 return node;
5393 node = TYPE_CONTEXT (node);
5394 break;
5396 default:
5397 my_friendly_abort (0);
5400 return NULL_TREE;
5403 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
5406 is_base_of_enclosing_class (base, type)
5407 tree base, type;
5409 while (type)
5411 if (get_binfo (base, type, 0))
5412 return 1;
5414 type = get_enclosing_class (type);
5416 return 0;
5419 /* Note that NAME was looked up while the current class was being
5420 defined and that the result of that lookup was DECL. */
5422 void
5423 maybe_note_name_used_in_class (name, decl)
5424 tree name;
5425 tree decl;
5427 splay_tree names_used;
5429 /* If we're not defining a class, there's nothing to do. */
5430 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
5431 return;
5433 /* If there's already a binding for this NAME, then we don't have
5434 anything to worry about. */
5435 if (IDENTIFIER_CLASS_VALUE (name))
5436 return;
5438 if (!current_class_stack[current_class_depth - 1].names_used)
5439 current_class_stack[current_class_depth - 1].names_used
5440 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
5441 names_used = current_class_stack[current_class_depth - 1].names_used;
5443 splay_tree_insert (names_used,
5444 (splay_tree_key) name,
5445 (splay_tree_value) decl);
5448 /* Note that NAME was declared (as DECL) in the current class. Check
5449 to see that the declaration is legal. */
5451 void
5452 note_name_declared_in_class (name, decl)
5453 tree name;
5454 tree decl;
5456 splay_tree names_used;
5457 splay_tree_node n;
5459 /* Look to see if we ever used this name. */
5460 names_used
5461 = current_class_stack[current_class_depth - 1].names_used;
5462 if (!names_used)
5463 return;
5465 n = splay_tree_lookup (names_used, (splay_tree_key) name);
5466 if (n)
5468 /* [basic.scope.class]
5470 A name N used in a class S shall refer to the same declaration
5471 in its context and when re-evaluated in the completed scope of
5472 S. */
5473 cp_error ("declaration of `%#D'", decl);
5474 cp_error_at ("changes meaning of `%s' from `%+#D'",
5475 IDENTIFIER_POINTER (DECL_NAME (decl)),
5476 (tree) n->value);