* class.c (create_vtable_ptr): Put the vtable at the beginning of
[official-gcc.git] / gcc / cp / class.c
blob94869e8c7e965d2c5a1d9e885f94ab6eb1a25695
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 "ggc.h"
34 #include "lex.h"
36 #include "obstack.h"
37 #define obstack_chunk_alloc xmalloc
38 #define obstack_chunk_free free
40 /* This is how we tell when two virtual member functions are really the
41 same. */
42 #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
44 extern void set_class_shadows PROTO ((tree));
46 /* The number of nested classes being processed. If we are not in the
47 scope of any class, this is zero. */
49 int current_class_depth;
51 /* In order to deal with nested classes, we keep a stack of classes.
52 The topmost entry is the innermost class, and is the entry at index
53 CURRENT_CLASS_DEPTH */
55 typedef struct class_stack_node {
56 /* The name of the class. */
57 tree name;
59 /* The _TYPE node for the class. */
60 tree type;
62 /* The access specifier pending for new declarations in the scope of
63 this class. */
64 tree access;
66 /* If were defining TYPE, the names used in this class. */
67 splay_tree names_used;
68 }* class_stack_node_t;
70 /* The stack itself. This is an dynamically resized array. The
71 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
72 static int current_class_stack_size;
73 static class_stack_node_t current_class_stack;
75 struct base_info;
77 static tree get_vfield_name PROTO((tree));
78 static void finish_struct_anon PROTO((tree));
79 static tree build_vbase_pointer PROTO((tree, tree));
80 static tree build_vtable_entry PROTO((tree, tree));
81 static tree get_vtable_name PROTO((tree));
82 static tree get_derived_offset PROTO((tree, tree));
83 static tree get_basefndecls PROTO((tree, tree));
84 static void set_rtti_entry PROTO((tree, tree, tree));
85 static tree build_vtable PROTO((tree, tree));
86 static void prepare_fresh_vtable PROTO((tree, tree));
87 static void fixup_vtable_deltas1 PROTO((tree, tree));
88 static void fixup_vtable_deltas PROTO((tree, int, tree));
89 static void finish_vtbls PROTO((tree, int, tree));
90 static void modify_vtable_entry PROTO((tree, tree, tree));
91 static tree get_vtable_entry_n PROTO((tree, unsigned HOST_WIDE_INT));
92 static void add_virtual_function PROTO((tree *, tree *, int *, tree, tree));
93 static tree delete_duplicate_fields_1 PROTO((tree, tree));
94 static void delete_duplicate_fields PROTO((tree));
95 static void finish_struct_bits PROTO((tree, int));
96 static int alter_access PROTO((tree, tree, tree, tree));
97 static void handle_using_decl PROTO((tree, tree));
98 static int overrides PROTO((tree, tree));
99 static int strictly_overrides PROTO((tree, tree));
100 static void merge_overrides PROTO((tree, tree, int, tree));
101 static void override_one_vtable PROTO((tree, tree, tree));
102 static void mark_overriders PROTO((tree, tree));
103 static void check_for_override PROTO((tree, tree));
104 static tree get_class_offset_1 PROTO((tree, tree, tree, tree, tree));
105 static tree get_class_offset PROTO((tree, tree, tree, tree));
106 static void modify_one_vtable PROTO((tree, tree, tree));
107 static void modify_all_vtables PROTO((tree, tree));
108 static void modify_all_direct_vtables PROTO((tree, int, tree, tree));
109 static void modify_all_indirect_vtables PROTO((tree, int, int, tree, tree));
110 static int finish_base_struct PROTO((tree, struct base_info *));
111 static void finish_struct_methods PROTO((tree));
112 static void maybe_warn_about_overly_private_class PROTO ((tree));
113 static int field_decl_cmp PROTO ((const tree *, const tree *));
114 static int method_name_cmp PROTO ((const tree *, const tree *));
115 static tree add_implicitly_declared_members PROTO((tree, int, int, int));
116 static tree fixed_type_or_null PROTO((tree, int *));
117 static tree resolve_address_of_overloaded_function PROTO((tree, tree, int,
118 int, tree));
119 static void build_vtable_entry_ref PROTO((tree, tree, tree));
120 static tree build_vtable_entry_for_fn PROTO((tree, tree));
121 static tree build_vtbl_initializer PROTO((tree));
122 static int count_fields PROTO((tree));
123 static int add_fields_to_vec PROTO((tree, tree, int));
124 static void check_bitfield_decl PROTO((tree));
125 static void check_field_decl PROTO((tree, tree, int *, int *, int *, int *));
126 static void check_field_decls PROTO((tree, tree *, int *, int *, int *,
127 int *));
128 static int avoid_overlap PROTO((tree, tree, int *));
129 static tree build_base_fields PROTO((tree, int *));
130 static tree build_vbase_pointer_fields PROTO((tree, int *));
131 static tree build_vtbl_or_vbase_field PROTO((tree, tree, tree, tree, int *));
132 static void check_methods PROTO((tree));
133 static void remove_zero_width_bit_fields PROTO((tree));
134 static void check_bases PROTO((tree, int *, int *, int *));
135 static void check_bases_and_members PROTO((tree, int *));
136 static void create_vtable_ptr PROTO((tree, int *, int *, int *, tree *, tree *));
138 /* Variables shared between class.c and call.c. */
140 #ifdef GATHER_STATISTICS
141 int n_vtables = 0;
142 int n_vtable_entries = 0;
143 int n_vtable_searches = 0;
144 int n_vtable_elems = 0;
145 int n_convert_harshness = 0;
146 int n_compute_conversion_costs = 0;
147 int n_build_method_call = 0;
148 int n_inner_fields_searched = 0;
149 #endif
151 /* Virtual baseclass things. */
153 static tree
154 build_vbase_pointer (exp, type)
155 tree exp, type;
157 char *name;
158 FORMAT_VBASE_NAME (name, type);
160 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
163 #if 0
164 /* Is the type of the EXPR, the complete type of the object?
165 If we are going to be wrong, we must be conservative, and return 0. */
167 static int
168 complete_type_p (expr)
169 tree expr;
171 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
172 while (1)
174 switch (TREE_CODE (expr))
176 case SAVE_EXPR:
177 case INDIRECT_REF:
178 case ADDR_EXPR:
179 case NOP_EXPR:
180 case CONVERT_EXPR:
181 expr = TREE_OPERAND (expr, 0);
182 continue;
184 case CALL_EXPR:
185 if (! TREE_HAS_CONSTRUCTOR (expr))
186 break;
187 /* fall through... */
188 case VAR_DECL:
189 case FIELD_DECL:
190 if (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
191 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr)))
192 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
193 return 1;
194 /* fall through... */
195 case TARGET_EXPR:
196 case PARM_DECL:
197 if (IS_AGGR_TYPE (TREE_TYPE (expr))
198 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
199 return 1;
200 /* fall through... */
201 case PLUS_EXPR:
202 default:
203 break;
205 break;
207 return 0;
209 #endif
211 /* Build multi-level access to EXPR using hierarchy path PATH.
212 CODE is PLUS_EXPR if we are going with the grain,
213 and MINUS_EXPR if we are not (in which case, we cannot traverse
214 virtual baseclass links).
216 TYPE is the type we want this path to have on exit.
218 NONNULL is non-zero if we know (for any reason) that EXPR is
219 not, in fact, zero. */
221 tree
222 build_vbase_path (code, type, expr, path, nonnull)
223 enum tree_code code;
224 tree type, expr, path;
225 int nonnull;
227 register int changed = 0;
228 tree last = NULL_TREE, last_virtual = NULL_TREE;
229 int fixed_type_p;
230 tree null_expr = 0, nonnull_expr;
231 tree basetype;
232 tree offset = integer_zero_node;
234 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
235 return build1 (NOP_EXPR, type, expr);
237 /* We could do better if we had additional logic to convert back to the
238 unconverted type (the static type of the complete object), and then
239 convert back to the type we want. Until that is done, we only optimize
240 if the complete type is the same type as expr has. */
241 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
243 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
244 expr = save_expr (expr);
245 nonnull_expr = expr;
247 if (BINFO_INHERITANCE_CHAIN (path))
248 path = reverse_path (path);
250 basetype = BINFO_TYPE (path);
252 while (path)
254 if (TREE_VIA_VIRTUAL (path))
256 last_virtual = BINFO_TYPE (path);
257 if (code == PLUS_EXPR)
259 changed = ! fixed_type_p;
261 if (changed)
263 tree ind;
265 /* We already check for ambiguous things in the caller, just
266 find a path. */
267 if (last)
269 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
270 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
272 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
273 nonnull_expr = build_vbase_pointer (ind, last_virtual);
274 if (nonnull == 0
275 && TREE_CODE (type) == POINTER_TYPE
276 && null_expr == NULL_TREE)
278 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
279 expr = build (COND_EXPR, build_pointer_type (last_virtual),
280 build (EQ_EXPR, boolean_type_node, expr,
281 integer_zero_node),
282 null_expr, nonnull_expr);
285 /* else we'll figure out the offset below. */
287 /* Happens in the case of parse errors. */
288 if (nonnull_expr == error_mark_node)
289 return error_mark_node;
291 else
293 cp_error ("cannot cast up from virtual baseclass `%T'",
294 last_virtual);
295 return error_mark_node;
298 last = path;
299 path = BINFO_INHERITANCE_CHAIN (path);
301 /* LAST is now the last basetype assoc on the path. */
303 /* A pointer to a virtual base member of a non-null object
304 is non-null. Therefore, we only need to test for zeroness once.
305 Make EXPR the canonical expression to deal with here. */
306 if (null_expr)
308 TREE_OPERAND (expr, 2) = nonnull_expr;
309 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
310 = TREE_TYPE (nonnull_expr);
312 else
313 expr = nonnull_expr;
315 /* If we go through any virtual base pointers, make sure that
316 casts to BASETYPE from the last virtual base class use
317 the right value for BASETYPE. */
318 if (changed)
320 tree intype = TREE_TYPE (TREE_TYPE (expr));
321 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
323 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
324 offset = BINFO_OFFSET (binfo);
327 else
329 if (last_virtual)
331 offset = BINFO_OFFSET (binfo_member (last_virtual,
332 CLASSTYPE_VBASECLASSES (basetype)));
333 offset = size_binop (PLUS_EXPR, offset, BINFO_OFFSET (last));
335 else
336 offset = BINFO_OFFSET (last);
339 if (TREE_INT_CST_LOW (offset))
341 /* Bash types to make the backend happy. */
342 offset = cp_convert (type, offset);
343 #if 0
344 /* This shouldn't be necessary. (mrs) */
345 expr = build1 (NOP_EXPR, type, expr);
346 #endif
348 /* If expr might be 0, we need to preserve that zeroness. */
349 if (nonnull == 0)
351 if (null_expr)
352 TREE_TYPE (null_expr) = type;
353 else
354 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
355 if (TREE_SIDE_EFFECTS (expr))
356 expr = save_expr (expr);
358 return build (COND_EXPR, type,
359 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
360 null_expr,
361 build (code, type, expr, offset));
363 else return build (code, type, expr, offset);
366 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
367 be used multiple times in initialization of multiple inheritance. */
368 if (null_expr)
370 TREE_TYPE (expr) = type;
371 return expr;
373 else
374 return build1 (NOP_EXPR, type, expr);
377 /* Virtual function things. */
379 /* Build an entry in the virtual function table.
380 DELTA is the offset for the `this' pointer.
381 PFN is an ADDR_EXPR containing a pointer to the virtual function.
382 Note that the index (DELTA2) in the virtual function table
383 is always 0. */
385 static tree
386 build_vtable_entry (delta, pfn)
387 tree delta, pfn;
389 if (flag_vtable_thunks)
391 HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta);
392 if (idelta && ! DECL_ABSTRACT_VIRTUAL_P (TREE_OPERAND (pfn, 0)))
394 pfn = build1 (ADDR_EXPR, vtable_entry_type,
395 make_thunk (pfn, idelta));
396 TREE_READONLY (pfn) = 1;
397 TREE_CONSTANT (pfn) = 1;
399 #ifdef GATHER_STATISTICS
400 n_vtable_entries += 1;
401 #endif
402 return pfn;
404 else
406 extern int flag_huge_objects;
407 tree elems = tree_cons (NULL_TREE, delta,
408 tree_cons (NULL_TREE, integer_zero_node,
409 build_expr_list (NULL_TREE, pfn)));
410 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
412 /* DELTA used to be constructed by `size_int' and/or size_binop,
413 which caused overflow problems when it was negative. That should
414 be fixed now. */
416 if (! int_fits_type_p (delta, delta_type_node))
418 if (flag_huge_objects)
419 sorry ("object size exceeds built-in limit for virtual function table implementation");
420 else
421 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
424 TREE_CONSTANT (entry) = 1;
425 TREE_STATIC (entry) = 1;
426 TREE_READONLY (entry) = 1;
428 #ifdef GATHER_STATISTICS
429 n_vtable_entries += 1;
430 #endif
432 return entry;
436 /* Build a vtable entry for FNDECL. DELTA is the amount by which we
437 must adjust the this pointer when calling F. */
439 static tree
440 build_vtable_entry_for_fn (delta, fndecl)
441 tree delta;
442 tree fndecl;
444 tree pfn;
446 /* Take the address of the function, considering it to be of an
447 appropriate generic type. */
448 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
449 /* The address of a function can't change. */
450 TREE_CONSTANT (pfn) = 1;
451 /* Now build the vtable entry itself. */
452 return build_vtable_entry (delta, pfn);
455 /* We want to give the assembler the vtable identifier as well as
456 the offset to the function pointer. So we generate
458 __asm__ __volatile__ (".vtable_entry %c0, %c1"
459 : : "s"(&class_vtable),
460 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
462 static void
463 build_vtable_entry_ref (basetype, vtbl, idx)
464 tree basetype, vtbl, idx;
466 static char asm_stmt[] = ".vtable_entry %c0, %c1";
467 tree s, i, i2;
469 s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
470 s = build_tree_list (build_string (1, "s"), s);
472 i = build_array_ref (vtbl, idx);
473 if (!flag_vtable_thunks)
474 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
475 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
476 i2 = build_array_ref (vtbl, build_int_2(0,0));
477 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
478 i = build_binary_op (MINUS_EXPR, i, i2);
479 i = build_tree_list (build_string (1, "i"), i);
481 finish_asm_stmt (ridpointers[RID_VOLATILE],
482 build_string (sizeof(asm_stmt)-1, asm_stmt),
483 NULL_TREE, chainon (s, i), NULL_TREE);
486 /* Given an object INSTANCE, return an expression which yields the
487 virtual function vtable element corresponding to INDEX. There are
488 many special cases for INSTANCE which we take care of here, mainly
489 to avoid creating extra tree nodes when we don't have to. */
491 tree
492 build_vtbl_ref (instance, idx)
493 tree instance, idx;
495 tree vtbl, aref;
496 tree basetype = TREE_TYPE (instance);
498 if (TREE_CODE (basetype) == REFERENCE_TYPE)
499 basetype = TREE_TYPE (basetype);
501 if (instance == current_class_ref)
502 vtbl = build_vfield_ref (instance, basetype);
503 else
505 if (optimize)
507 /* Try to figure out what a reference refers to, and
508 access its virtual function table directly. */
509 tree ref = NULL_TREE;
511 if (TREE_CODE (instance) == INDIRECT_REF
512 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
513 ref = TREE_OPERAND (instance, 0);
514 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
515 ref = instance;
517 if (ref && TREE_CODE (ref) == VAR_DECL
518 && DECL_INITIAL (ref))
520 tree init = DECL_INITIAL (ref);
522 while (TREE_CODE (init) == NOP_EXPR
523 || TREE_CODE (init) == NON_LVALUE_EXPR)
524 init = TREE_OPERAND (init, 0);
525 if (TREE_CODE (init) == ADDR_EXPR)
527 init = TREE_OPERAND (init, 0);
528 if (IS_AGGR_TYPE (TREE_TYPE (init))
529 && (TREE_CODE (init) == PARM_DECL
530 || TREE_CODE (init) == VAR_DECL))
531 instance = init;
536 if (IS_AGGR_TYPE (TREE_TYPE (instance))
537 && (TREE_CODE (instance) == RESULT_DECL
538 || TREE_CODE (instance) == PARM_DECL
539 || TREE_CODE (instance) == VAR_DECL))
540 vtbl = TYPE_BINFO_VTABLE (basetype);
541 else
542 vtbl = build_vfield_ref (instance, basetype);
545 assemble_external (vtbl);
547 if (flag_vtable_gc)
548 build_vtable_entry_ref (basetype, vtbl, idx);
550 aref = build_array_ref (vtbl, idx);
552 return aref;
555 /* Given an object INSTANCE, return an expression which yields the
556 virtual function corresponding to INDEX. There are many special
557 cases for INSTANCE which we take care of here, mainly to avoid
558 creating extra tree nodes when we don't have to. */
560 tree
561 build_vfn_ref (ptr_to_instptr, instance, idx)
562 tree *ptr_to_instptr, instance;
563 tree idx;
565 tree aref = build_vtbl_ref (instance, idx);
567 /* When using thunks, there is no extra delta, and we get the pfn
568 directly. */
569 if (flag_vtable_thunks)
570 return aref;
572 if (ptr_to_instptr)
574 /* Save the intermediate result in a SAVE_EXPR so we don't have to
575 compute each component of the virtual function pointer twice. */
576 if (TREE_CODE (aref) == INDIRECT_REF)
577 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
579 *ptr_to_instptr
580 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
581 *ptr_to_instptr,
582 cp_convert (ptrdiff_type_node,
583 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
586 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
589 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
590 for the given TYPE. */
592 static tree
593 get_vtable_name (type)
594 tree type;
596 tree type_id = build_typename_overload (type);
597 char *buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
598 + IDENTIFIER_LENGTH (type_id) + 2);
599 const char *ptr = IDENTIFIER_POINTER (type_id);
600 int i;
601 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
602 #if 0
603 /* We don't take off the numbers; prepare_fresh_vtable uses the
604 DECL_ASSEMBLER_NAME for the type, which includes the number
605 in `3foo'. If we were to pull them off here, we'd end up with
606 something like `_vt.foo.3bar', instead of a uniform definition. */
607 while (ptr[i] >= '0' && ptr[i] <= '9')
608 i += 1;
609 #endif
610 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, ptr+i);
611 return get_identifier (buf);
614 /* Return the offset to the main vtable for a given base BINFO. */
616 tree
617 get_vfield_offset (binfo)
618 tree binfo;
620 tree tmp
621 = size_binop (FLOOR_DIV_EXPR,
622 DECL_FIELD_BITPOS (TYPE_VFIELD (BINFO_TYPE (binfo))),
623 size_int (BITS_PER_UNIT));
624 tmp = convert (sizetype, tmp);
625 return size_binop (PLUS_EXPR, tmp, BINFO_OFFSET (binfo));
628 /* Get the offset to the start of the original binfo that we derived
629 this binfo from. If we find TYPE first, return the offset only
630 that far. The shortened search is useful because the this pointer
631 on method calling is expected to point to a DECL_CONTEXT (fndecl)
632 object, and not a baseclass of it. */
634 static tree
635 get_derived_offset (binfo, type)
636 tree binfo, type;
638 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
639 tree offset2;
640 int i;
641 while (BINFO_BASETYPES (binfo)
642 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
644 tree binfos = BINFO_BASETYPES (binfo);
645 if (BINFO_TYPE (binfo) == type)
646 break;
647 binfo = TREE_VEC_ELT (binfos, i);
649 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
650 return size_binop (MINUS_EXPR, offset1, offset2);
653 /* Update the rtti info for this class. */
655 static void
656 set_rtti_entry (virtuals, offset, type)
657 tree virtuals, offset, type;
659 tree fn;
661 if (CLASSTYPE_COM_INTERFACE (type))
662 return;
664 if (flag_rtti)
665 fn = get_tinfo_fn_unused (type);
666 else
667 /* If someone tries to get RTTI information for a type compiled
668 without RTTI, they're out of luck. By calling __pure_virtual
669 in this case, we give a small clue as to what went wrong. We
670 could consider having a __no_typeinfo function as well, for a
671 more specific hint. */
672 fn = abort_fndecl;
674 if (flag_vtable_thunks)
676 /* The first slot holds the offset. */
677 TREE_PURPOSE (virtuals) = offset;
679 /* The next node holds the function. */
680 virtuals = TREE_CHAIN (virtuals);
681 offset = integer_zero_node;
684 /* This slot holds the function to call. */
685 TREE_PURPOSE (virtuals) = offset;
686 TREE_VALUE (virtuals) = fn;
689 /* Build a virtual function for type TYPE.
690 If BINFO is non-NULL, build the vtable starting with the initial
691 approximation that it is the same as the one which is the head of
692 the association list. */
694 static tree
695 build_vtable (binfo, type)
696 tree binfo, type;
698 tree name = get_vtable_name (type);
699 tree virtuals, decl;
701 if (binfo)
703 tree offset;
705 virtuals = copy_list (BINFO_VIRTUALS (binfo));
706 decl = build_lang_decl (VAR_DECL, name,
707 TREE_TYPE (BINFO_VTABLE (binfo)));
709 /* Now do rtti stuff. */
710 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
711 offset = ssize_binop (MINUS_EXPR, integer_zero_node, offset);
712 set_rtti_entry (virtuals, offset, type);
714 else
716 virtuals = NULL_TREE;
717 decl = build_lang_decl (VAR_DECL, name, void_type_node);
720 #ifdef GATHER_STATISTICS
721 n_vtables += 1;
722 n_vtable_elems += list_length (virtuals);
723 #endif
725 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
726 import_export_vtable (decl, type, 0);
728 decl = pushdecl_top_level (decl);
729 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
730 /* Initialize the association list for this type, based
731 on our first approximation. */
732 TYPE_BINFO_VTABLE (type) = decl;
733 TYPE_BINFO_VIRTUALS (type) = virtuals;
735 DECL_ARTIFICIAL (decl) = 1;
736 TREE_STATIC (decl) = 1;
737 #ifndef WRITABLE_VTABLES
738 /* Make them READONLY by default. (mrs) */
739 TREE_READONLY (decl) = 1;
740 #endif
741 /* At one time the vtable info was grabbed 2 words at a time. This
742 fails on sparc unless you have 8-byte alignment. (tiemann) */
743 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
744 DECL_ALIGN (decl));
746 DECL_VIRTUAL_P (decl) = 1;
747 DECL_CONTEXT (decl) = type;
749 binfo = TYPE_BINFO (type);
750 SET_BINFO_NEW_VTABLE_MARKED (binfo);
751 return decl;
754 /* Give TYPE a new virtual function table which is initialized
755 with a skeleton-copy of its original initialization. The only
756 entry that changes is the `delta' entry, so we can really
757 share a lot of structure.
759 FOR_TYPE is the derived type which caused this table to
760 be needed.
762 BINFO is the type association which provided TYPE for FOR_TYPE.
764 The order in which vtables are built (by calling this function) for
765 an object must remain the same, otherwise a binary incompatibility
766 can result. */
768 static void
769 prepare_fresh_vtable (binfo, for_type)
770 tree binfo, for_type;
772 tree basetype;
773 tree orig_decl = BINFO_VTABLE (binfo);
774 tree name;
775 tree new_decl;
776 tree offset;
777 tree path = binfo;
778 char *buf, *buf2;
779 char joiner = '_';
780 int i;
782 #ifdef JOINER
783 joiner = JOINER;
784 #endif
786 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
788 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
789 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
791 /* We know that the vtable that we are going to create doesn't exist
792 yet in the global namespace, and when we finish, it will be
793 pushed into the global namespace. In complex MI hierarchies, we
794 have to loop while the name we are thinking of adding is globally
795 defined, adding more name components to the vtable name as we
796 loop, until the name is unique. This is because in complex MI
797 cases, we might have the same base more than once. This means
798 that the order in which this function is called for vtables must
799 remain the same, otherwise binary compatibility can be
800 compromised. */
802 while (1)
804 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
805 + 1 + i);
806 char *new_buf2;
808 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
809 buf2);
810 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX) + strlen (buf1) + 1);
811 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
812 name = get_identifier (buf);
814 /* If this name doesn't clash, then we can use it, otherwise
815 we add more to the name until it is unique. */
817 if (! IDENTIFIER_GLOBAL_VALUE (name))
818 break;
820 /* Set values for next loop through, if the name isn't unique. */
822 path = BINFO_INHERITANCE_CHAIN (path);
824 /* We better not run out of stuff to make it unique. */
825 my_friendly_assert (path != NULL_TREE, 368);
827 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
829 if (for_type == basetype)
831 /* If we run out of basetypes in the path, we have already
832 found created a vtable with that name before, we now
833 resort to tacking on _%d to distinguish them. */
834 int j = 2;
835 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
836 buf1 = (char *) alloca (i);
837 do {
838 sprintf (buf1, "%s%c%s%c%d",
839 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
840 buf2, joiner, j);
841 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
842 + strlen (buf1) + 1);
843 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
844 name = get_identifier (buf);
846 /* If this name doesn't clash, then we can use it,
847 otherwise we add something different to the name until
848 it is unique. */
849 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
851 /* Hey, they really like MI don't they? Increase the 3
852 above to 6, and the 999 to 999999. :-) */
853 my_friendly_assert (j <= 999, 369);
855 break;
858 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
859 new_buf2 = (char *) alloca (i);
860 sprintf (new_buf2, "%s%c%s",
861 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
862 buf2 = new_buf2;
865 new_decl = build_lang_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
866 /* Remember which class this vtable is really for. */
867 DECL_CONTEXT (new_decl) = for_type;
869 DECL_ARTIFICIAL (new_decl) = 1;
870 TREE_STATIC (new_decl) = 1;
871 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
872 DECL_VIRTUAL_P (new_decl) = 1;
873 #ifndef WRITABLE_VTABLES
874 /* Make them READONLY by default. (mrs) */
875 TREE_READONLY (new_decl) = 1;
876 #endif
877 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
879 /* Make fresh virtual list, so we can smash it later. */
880 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
882 if (TREE_VIA_VIRTUAL (binfo))
884 tree binfo1 = binfo_member (BINFO_TYPE (binfo),
885 CLASSTYPE_VBASECLASSES (for_type));
887 /* XXX - This should never happen, if it does, the caller should
888 ensure that the binfo is from for_type's binfos, not from any
889 base type's. We can remove all this code after a while. */
890 if (binfo1 != binfo)
891 warning ("internal inconsistency: binfo offset error for rtti");
893 offset = BINFO_OFFSET (binfo1);
895 else
896 offset = BINFO_OFFSET (binfo);
898 set_rtti_entry (BINFO_VIRTUALS (binfo),
899 ssize_binop (MINUS_EXPR, integer_zero_node, offset),
900 for_type);
902 #ifdef GATHER_STATISTICS
903 n_vtables += 1;
904 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
905 #endif
907 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
908 import_export_vtable (new_decl, for_type, 0);
910 if (TREE_VIA_VIRTUAL (binfo))
911 my_friendly_assert (binfo == binfo_member (BINFO_TYPE (binfo),
912 CLASSTYPE_VBASECLASSES (current_class_type)),
913 170);
914 SET_BINFO_NEW_VTABLE_MARKED (binfo);
917 #if 0
918 /* Access the virtual function table entry that logically
919 contains BASE_FNDECL. VIRTUALS is the virtual function table's
920 initializer. We can run off the end, when dealing with virtual
921 destructors in MI situations, return NULL_TREE in that case. */
923 static tree
924 get_vtable_entry (virtuals, base_fndecl)
925 tree virtuals, base_fndecl;
927 unsigned HOST_WIDE_INT n = (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
928 ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
929 & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1))
930 : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
932 #ifdef GATHER_STATISTICS
933 n_vtable_searches += n;
934 #endif
936 while (n > 0 && virtuals)
938 --n;
939 virtuals = TREE_CHAIN (virtuals);
941 return virtuals;
943 #endif
945 /* Change the offset for the FNDECL entry to NEW_OFFSET. Also update
946 DECL_VINDEX (FNDECL). */
948 static void
949 modify_vtable_entry (old_entry_in_list, new_offset, fndecl)
950 tree old_entry_in_list, new_offset, fndecl;
952 tree base_fndecl = TREE_VALUE (old_entry_in_list);
954 /* Update the entry. */
955 TREE_PURPOSE (old_entry_in_list) = new_offset;
956 TREE_VALUE (old_entry_in_list) = fndecl;
958 /* Now assign virtual dispatch information, if unset. We can
959 dispatch this, through any overridden base function. */
960 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
962 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
963 DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
967 /* Access the virtual function table entry N. VIRTUALS is the virtual
968 function table's initializer. */
970 static tree
971 get_vtable_entry_n (virtuals, n)
972 tree virtuals;
973 unsigned HOST_WIDE_INT n;
975 while (n > 0)
977 --n;
978 virtuals = TREE_CHAIN (virtuals);
980 return virtuals;
983 /* Add a virtual function to all the appropriate vtables for the class
984 T. DECL_VINDEX(X) should be error_mark_node, if we want to
985 allocate a new slot in our table. If it is error_mark_node, we
986 know that no other function from another vtable is overridden by X.
987 HAS_VIRTUAL keeps track of how many virtuals there are in our main
988 vtable for the type, and we build upon the PENDING_VIRTUALS list
989 and return it. */
991 static void
992 add_virtual_function (pv, phv, has_virtual, fndecl, t)
993 tree *pv, *phv;
994 int *has_virtual;
995 tree fndecl;
996 tree t; /* Structure type. */
998 tree pending_virtuals = *pv;
999 tree pending_hard_virtuals = *phv;
1001 #ifndef DUMB_USER
1002 if (current_class_type == 0)
1003 cp_warning ("internal problem, current_class_type is zero when adding `%D', please report",
1004 fndecl);
1005 if (current_class_type && t != current_class_type)
1006 cp_warning ("internal problem, current_class_type differs when adding `%D', please report",
1007 fndecl);
1008 #endif
1010 /* If the virtual function is a redefinition of a prior one,
1011 figure out in which base class the new definition goes,
1012 and if necessary, make a fresh virtual function table
1013 to hold that entry. */
1014 if (DECL_VINDEX (fndecl) == error_mark_node)
1016 /* We remember that this was the base sub-object for rtti. */
1017 CLASSTYPE_RTTI (t) = t;
1019 /* If we are using thunks, use two slots at the front, one
1020 for the offset pointer, one for the tdesc pointer.
1021 For ARM-style vtables, use the same slot for both. */
1022 if (*has_virtual == 0 && ! CLASSTYPE_COM_INTERFACE (t))
1024 if (flag_vtable_thunks)
1025 *has_virtual = 2;
1026 else
1027 *has_virtual = 1;
1030 /* Build a new INT_CST for this DECL_VINDEX. */
1032 static tree index_table[256];
1033 tree idx;
1034 /* We skip a slot for the offset/tdesc entry. */
1035 int i = (*has_virtual)++;
1037 if (i >= 256 || index_table[i] == 0)
1039 idx = build_int_2 (i, 0);
1040 if (i < 256)
1041 index_table[i] = idx;
1043 else
1044 idx = index_table[i];
1046 /* Now assign virtual dispatch information. */
1047 DECL_VINDEX (fndecl) = idx;
1048 DECL_CONTEXT (fndecl) = t;
1050 /* Save the state we've computed on the PENDING_VIRTUALS list. */
1051 pending_virtuals = tree_cons (integer_zero_node,
1052 fndecl,
1053 pending_virtuals);
1055 /* Might already be INTEGER_CST if declared twice in class. We will
1056 give error later or we've already given it. */
1057 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1059 /* Need an entry in some other virtual function table.
1060 Deal with this after we have laid out our virtual base classes. */
1061 pending_hard_virtuals = tree_cons (NULL_TREE,
1062 fndecl,
1063 pending_hard_virtuals);
1065 *pv = pending_virtuals;
1066 *phv = pending_hard_virtuals;
1069 extern struct obstack *current_obstack;
1071 /* Add method METHOD to class TYPE.
1073 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1074 the class type where the method should be added. */
1076 void
1077 add_method (type, fields, method)
1078 tree type, *fields, method;
1080 /* Setting the DECL_CONTEXT and DECL_CLASS_CONTEXT here is probably
1081 redundant. */
1082 DECL_CONTEXT (method) = type;
1083 DECL_CLASS_CONTEXT (method) = type;
1085 if (fields && *fields)
1086 *fields = build_overload (method, *fields);
1087 else
1089 int len;
1090 int slot;
1091 tree method_vec;
1093 if (!CLASSTYPE_METHOD_VEC (type))
1094 /* Make a new method vector. We start with 8 entries. We must
1095 allocate at least two (for constructors and destructors), and
1096 we're going to end up with an assignment operator at some
1097 point as well.
1099 We could use a TREE_LIST for now, and convert it to a
1100 TREE_VEC in finish_struct, but we would probably waste more
1101 memory making the links in the list than we would by
1102 over-allocating the size of the vector here. Furthermore,
1103 we would complicate all the code that expects this to be a
1104 vector. */
1105 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1107 method_vec = CLASSTYPE_METHOD_VEC (type);
1108 len = TREE_VEC_LENGTH (method_vec);
1110 if (DECL_NAME (method) == constructor_name (type))
1111 /* A new constructor or destructor. Constructors go in
1112 slot 0; destructors go in slot 1. */
1113 slot = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
1114 else
1116 /* See if we already have an entry with this name. */
1117 for (slot = 2; slot < len; ++slot)
1118 if (!TREE_VEC_ELT (method_vec, slot)
1119 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1120 slot)))
1121 == DECL_NAME (method)))
1122 break;
1124 if (slot == len)
1126 /* We need a bigger method vector. */
1127 tree new_vec = make_tree_vec (2 * len);
1128 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1129 (PTR) &TREE_VEC_ELT (new_vec, 0),
1130 len * sizeof (tree));
1131 len = 2 * len;
1132 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1135 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1137 /* Type conversion operators have to come before
1138 ordinary methods; add_conversions depends on this to
1139 speed up looking for conversion operators. So, if
1140 necessary, we slide some of the vector elements up.
1141 In theory, this makes this algorithm O(N^2) but we
1142 don't expect many conversion operators. */
1143 for (slot = 2; slot < len; ++slot)
1145 tree fn = TREE_VEC_ELT (method_vec, slot);
1147 if (!fn)
1148 /* There are no more entries in the vector, so we
1149 can insert the new conversion operator here. */
1150 break;
1152 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1153 /* We can insert the new function right at the
1154 SLOTth position. */
1155 break;
1158 if (!TREE_VEC_ELT (method_vec, slot))
1159 /* There is nothing in the Ith slot, so we can avoid
1160 moving anything. */
1162 else
1164 /* We know the last slot in the vector is empty
1165 because we know that at this point there's room
1166 for a new function. */
1167 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1168 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1169 (len - slot - 1) * sizeof (tree));
1170 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1175 if (template_class_depth (type))
1176 /* TYPE is a template class. Don't issue any errors now; wait
1177 until instantiation time to complain. */
1179 else
1181 tree fns;
1183 /* Check to see if we've already got this method. */
1184 for (fns = TREE_VEC_ELT (method_vec, slot);
1185 fns;
1186 fns = OVL_NEXT (fns))
1188 tree fn = OVL_CURRENT (fns);
1190 if (TREE_CODE (fn) != TREE_CODE (method))
1191 continue;
1193 if (TREE_CODE (method) != TEMPLATE_DECL)
1195 /* [over.load] Member function declarations with the
1196 same name and the same parameter types cannot be
1197 overloaded if any of them is a static member
1198 function declaration. */
1199 if (DECL_STATIC_FUNCTION_P (fn)
1200 != DECL_STATIC_FUNCTION_P (method))
1202 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1203 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1205 if (! DECL_STATIC_FUNCTION_P (fn))
1206 parms1 = TREE_CHAIN (parms1);
1207 else
1208 parms2 = TREE_CHAIN (parms2);
1210 if (compparms (parms1, parms2))
1211 cp_error ("`%#D' and `%#D' cannot be overloaded",
1212 fn, method);
1215 /* Since this is an ordinary function in a
1216 non-template class, it's mangled name can be used
1217 as a unique identifier. This technique is only
1218 an optimization; we would get the same results if
1219 we just used decls_match here. */
1220 if (DECL_ASSEMBLER_NAME (fn)
1221 != DECL_ASSEMBLER_NAME (method))
1222 continue;
1224 else if (!decls_match (fn, method))
1225 continue;
1227 /* There has already been a declaration of this method
1228 or member template. */
1229 cp_error_at ("`%D' has already been declared in `%T'",
1230 method, type);
1232 /* We don't call duplicate_decls here to merge the
1233 declarations because that will confuse things if the
1234 methods have inline definitions. In particular, we
1235 will crash while processing the definitions. */
1236 return;
1240 /* Actually insert the new method. */
1241 TREE_VEC_ELT (method_vec, slot)
1242 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1244 /* Add the new binding. */
1245 if (!DECL_CONSTRUCTOR_P (method)
1246 && !DECL_DESTRUCTOR_P (method))
1247 push_class_level_binding (DECL_NAME (method),
1248 TREE_VEC_ELT (method_vec, slot));
1252 /* Subroutines of finish_struct. */
1254 /* Look through the list of fields for this struct, deleting
1255 duplicates as we go. This must be recursive to handle
1256 anonymous unions.
1258 FIELD is the field which may not appear anywhere in FIELDS.
1259 FIELD_PTR, if non-null, is the starting point at which
1260 chained deletions may take place.
1261 The value returned is the first acceptable entry found
1262 in FIELDS.
1264 Note that anonymous fields which are not of UNION_TYPE are
1265 not duplicates, they are just anonymous fields. This happens
1266 when we have unnamed bitfields, for example. */
1268 static tree
1269 delete_duplicate_fields_1 (field, fields)
1270 tree field, fields;
1272 tree x;
1273 tree prev = 0;
1274 if (DECL_NAME (field) == 0)
1276 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1277 return fields;
1279 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1280 fields = delete_duplicate_fields_1 (x, fields);
1281 return fields;
1283 else
1285 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1287 if (DECL_NAME (x) == 0)
1289 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1290 continue;
1291 TYPE_FIELDS (TREE_TYPE (x))
1292 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1293 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1295 if (prev == 0)
1296 fields = TREE_CHAIN (fields);
1297 else
1298 TREE_CHAIN (prev) = TREE_CHAIN (x);
1301 else if (TREE_CODE (field) == USING_DECL)
1302 /* A using declaration may is allowed to appear more than
1303 once. We'll prune these from the field list later, and
1304 handle_using_decl will complain about invalid multiple
1305 uses. */
1307 else if (DECL_NAME (field) == DECL_NAME (x))
1309 if (TREE_CODE (field) == CONST_DECL
1310 && TREE_CODE (x) == CONST_DECL)
1311 cp_error_at ("duplicate enum value `%D'", x);
1312 else if (TREE_CODE (field) == CONST_DECL
1313 || TREE_CODE (x) == CONST_DECL)
1314 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1316 else if (DECL_DECLARES_TYPE_P (field)
1317 && DECL_DECLARES_TYPE_P (x))
1319 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1320 continue;
1321 cp_error_at ("duplicate nested type `%D'", x);
1323 else if (DECL_DECLARES_TYPE_P (field)
1324 || DECL_DECLARES_TYPE_P (x))
1326 /* Hide tag decls. */
1327 if ((TREE_CODE (field) == TYPE_DECL
1328 && DECL_ARTIFICIAL (field))
1329 || (TREE_CODE (x) == TYPE_DECL
1330 && DECL_ARTIFICIAL (x)))
1331 continue;
1332 cp_error_at ("duplicate field `%D' (as type and non-type)",
1335 else
1336 cp_error_at ("duplicate member `%D'", x);
1337 if (prev == 0)
1338 fields = TREE_CHAIN (fields);
1339 else
1340 TREE_CHAIN (prev) = TREE_CHAIN (x);
1344 return fields;
1347 static void
1348 delete_duplicate_fields (fields)
1349 tree fields;
1351 tree x;
1352 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1353 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1356 /* Change the access of FDECL to ACCESS in T. The access to FDECL is
1357 along the path given by BINFO. Return 1 if change was legit,
1358 otherwise return 0. */
1360 static int
1361 alter_access (t, binfo, fdecl, access)
1362 tree t;
1363 tree binfo;
1364 tree fdecl;
1365 tree access;
1367 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1368 if (elem)
1370 if (TREE_VALUE (elem) != access)
1372 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1373 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1374 else
1375 error ("conflicting access specifications for field `%s', ignored",
1376 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1378 else
1380 /* They're changing the access to the same thing they changed
1381 it to before. That's OK. */
1385 else
1387 enforce_access (binfo, fdecl);
1388 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1389 return 1;
1391 return 0;
1394 /* Process the USING_DECL, which is a member of T. */
1396 static void
1397 handle_using_decl (using_decl, t)
1398 tree using_decl;
1399 tree t;
1401 tree ctype = DECL_INITIAL (using_decl);
1402 tree name = DECL_NAME (using_decl);
1403 tree access
1404 = TREE_PRIVATE (using_decl) ? access_private_node
1405 : TREE_PROTECTED (using_decl) ? access_protected_node
1406 : access_public_node;
1407 tree fdecl, binfo;
1408 tree flist = NULL_TREE;
1409 tree fields = TYPE_FIELDS (t);
1410 tree method_vec = CLASSTYPE_METHOD_VEC (t);
1411 tree tmp;
1412 int i;
1413 int n_methods;
1415 binfo = binfo_or_else (ctype, t);
1416 if (! binfo)
1417 return;
1419 if (name == constructor_name (ctype)
1420 || name == constructor_name_full (ctype))
1422 cp_error_at ("using-declaration for constructor", using_decl);
1423 return;
1426 fdecl = lookup_member (binfo, name, 0, 0);
1428 if (!fdecl)
1430 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1431 return;
1434 /* Functions are represented as TREE_LIST, with the purpose
1435 being the type and the value the functions. Other members
1436 come as themselves. */
1437 if (TREE_CODE (fdecl) == TREE_LIST)
1438 /* Ignore base type this came from. */
1439 fdecl = TREE_VALUE (fdecl);
1441 if (TREE_CODE (fdecl) == OVERLOAD)
1443 /* We later iterate over all functions. */
1444 flist = fdecl;
1445 fdecl = OVL_FUNCTION (flist);
1448 name = DECL_NAME (fdecl);
1449 n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
1450 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); i++)
1451 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)))
1452 == name)
1454 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1455 cp_error_at (" because of local method `%#D' with same name",
1456 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
1457 return;
1460 if (! DECL_LANG_SPECIFIC (fdecl))
1461 /* We don't currently handle DECL_ACCESS for TYPE_DECLs; just return. */
1462 return;
1464 for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
1465 if (DECL_NAME (tmp) == name)
1467 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1468 cp_error_at (" because of local field `%#D' with same name", tmp);
1469 return;
1472 /* Make type T see field decl FDECL with access ACCESS.*/
1473 if (flist)
1475 while (flist)
1477 if (alter_access (t, binfo, OVL_FUNCTION (flist),
1478 access) == 0)
1479 return;
1480 flist = OVL_CHAIN (flist);
1483 else
1484 alter_access (t, binfo, fdecl, access);
1487 struct base_info
1489 int has_virtual;
1490 int max_has_virtual;
1491 tree vfield;
1492 tree vfields;
1493 tree rtti;
1496 /* Run through the base clases of T, updating
1497 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1498 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1499 the bases. */
1501 static void
1502 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1503 no_const_asn_ref_p)
1504 tree t;
1505 int *cant_have_default_ctor_p;
1506 int *cant_have_const_ctor_p;
1507 int *no_const_asn_ref_p;
1509 int n_baseclasses;
1510 int i;
1511 tree binfos;
1513 binfos = TYPE_BINFO_BASETYPES (t);
1514 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1516 /* An aggregate cannot have baseclasses. */
1517 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1519 for (i = 0; i < n_baseclasses; ++i)
1521 tree base_binfo;
1522 tree basetype;
1524 /* Figure out what base we're looking at. */
1525 base_binfo = TREE_VEC_ELT (binfos, i);
1526 basetype = TREE_TYPE (base_binfo);
1528 /* If the type of basetype is incomplete, then we already
1529 complained about that fact (and we should have fixed it up as
1530 well). */
1531 if (TYPE_SIZE (basetype) == 0)
1533 int j;
1534 /* The base type is of incomplete type. It is
1535 probably best to pretend that it does not
1536 exist. */
1537 if (i == n_baseclasses-1)
1538 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1539 TREE_VEC_LENGTH (binfos) -= 1;
1540 n_baseclasses -= 1;
1541 for (j = i; j+1 < n_baseclasses; j++)
1542 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1543 continue;
1546 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1547 here because the case of virtual functions but non-virtual
1548 dtor is handled in finish_struct_1. */
1549 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1550 && TYPE_HAS_DESTRUCTOR (basetype))
1551 cp_warning ("base class `%#T' has a non-virtual destructor",
1552 basetype);
1554 /* If the base class doesn't have copy constructors or
1555 assignment operators that take const references, then the
1556 derived class cannot have such a member automatically
1557 generated. */
1558 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1559 *cant_have_const_ctor_p = 1;
1560 if (TYPE_HAS_ASSIGN_REF (basetype)
1561 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1562 *no_const_asn_ref_p = 1;
1563 /* Similarly, if the base class doesn't have a default
1564 constructor, then the derived class won't have an
1565 automatically generated default constructor. */
1566 if (TYPE_HAS_CONSTRUCTOR (basetype)
1567 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1569 *cant_have_default_ctor_p = 1;
1570 if (! TYPE_HAS_CONSTRUCTOR (t))
1572 cp_pedwarn ("base `%T' with only non-default constructor",
1573 basetype);
1574 cp_pedwarn ("in class without a constructor");
1578 /* A lot of properties from the bases also apply to the derived
1579 class. */
1580 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1581 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1582 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1583 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1584 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1585 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1586 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1587 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1588 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1590 /* Derived classes can implicitly become COMified if their bases
1591 are COM. */
1592 if (CLASSTYPE_COM_INTERFACE (basetype))
1593 CLASSTYPE_COM_INTERFACE (t) = 1;
1594 else if (i == 0 && CLASSTYPE_COM_INTERFACE (t))
1596 cp_error
1597 ("COM interface type `%T' with non-COM leftmost base class `%T'",
1598 t, basetype);
1599 CLASSTYPE_COM_INTERFACE (t) = 0;
1604 /* Record information about type T derived from its base classes.
1605 Store most of that information in T itself, and place the
1606 remaining information in the struct BASE_INFO.
1608 Propagate basetype offsets throughout the lattice. Note that the
1609 lattice topped by T is really a pair: it's a DAG that gives the
1610 structure of the derivation hierarchy, and it's a list of the
1611 virtual baseclasses that appear anywhere in the DAG. When a vbase
1612 type appears in the DAG, it's offset is 0, and it's children start
1613 their offsets from that point. When a vbase type appears in the list,
1614 its offset is the offset it has in the hierarchy, and its children's
1615 offsets include that offset in theirs.
1617 Returns the index of the first base class to have virtual functions,
1618 or -1 if no such base class. */
1620 static int
1621 finish_base_struct (t, b)
1622 tree t;
1623 struct base_info *b;
1625 tree binfos = TYPE_BINFO_BASETYPES (t);
1626 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1627 int first_vfn_base_index = -1;
1628 bzero ((char *) b, sizeof (struct base_info));
1630 for (i = 0; i < n_baseclasses; i++)
1632 tree base_binfo = TREE_VEC_ELT (binfos, i);
1633 tree basetype = BINFO_TYPE (base_binfo);
1635 if (TYPE_POLYMORPHIC_P (basetype))
1637 /* Ensure that this is set from at least a virtual base
1638 class. */
1639 if (b->rtti == NULL_TREE)
1640 b->rtti = CLASSTYPE_RTTI (basetype);
1642 /* Don't borrow virtuals from virtual baseclasses. */
1643 if (TREE_VIA_VIRTUAL (base_binfo))
1644 continue;
1646 if (first_vfn_base_index < 0)
1648 tree vfields;
1649 first_vfn_base_index = i;
1651 /* Update these two, now that we know what vtable we are
1652 going to extend. This is so that we can add virtual
1653 functions, and override them properly. */
1654 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1655 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1656 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1657 b->vfield = TYPE_VFIELD (basetype);
1658 b->vfields = copy_list (CLASSTYPE_VFIELDS (basetype));
1659 vfields = b->vfields;
1660 while (vfields)
1662 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1663 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1665 tree value = VF_BASETYPE_VALUE (vfields);
1666 if (DECL_NAME (TYPE_VFIELD (value))
1667 == DECL_NAME (TYPE_VFIELD (basetype)))
1668 VF_NORMAL_VALUE (b->vfields) = basetype;
1669 else
1670 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1672 vfields = TREE_CHAIN (vfields);
1674 TYPE_VFIELD (t) = b->vfield;
1676 else
1678 /* Only add unique vfields, and flatten them out as we go. */
1679 tree vfields = CLASSTYPE_VFIELDS (basetype);
1680 while (vfields)
1682 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1683 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1685 tree value = VF_BASETYPE_VALUE (vfields);
1686 b->vfields = tree_cons (base_binfo, value, b->vfields);
1687 if (DECL_NAME (TYPE_VFIELD (value))
1688 == DECL_NAME (TYPE_VFIELD (basetype)))
1689 VF_NORMAL_VALUE (b->vfields) = basetype;
1690 else
1691 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1693 vfields = TREE_CHAIN (vfields);
1696 if (b->has_virtual == 0)
1698 first_vfn_base_index = i;
1700 /* Update these two, now that we know what vtable we are
1701 going to extend. This is so that we can add virtual
1702 functions, and override them properly. */
1703 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1704 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1705 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1706 b->vfield = TYPE_VFIELD (basetype);
1707 TYPE_VFIELD (t) = b->vfield;
1708 /* When we install the first one, set the VF_NORMAL_VALUE
1709 to be the current class, as this it is the most derived
1710 class. Hopefully, this is not set to something else
1711 later. (mrs) */
1712 vfields = b->vfields;
1713 while (vfields)
1715 if (DECL_NAME (TYPE_VFIELD (t))
1716 == DECL_NAME (TYPE_VFIELD (basetype)))
1718 VF_NORMAL_VALUE (vfields) = t;
1719 /* There should only be one of them! And it should
1720 always be found, if we get into here. (mrs) */
1721 break;
1723 vfields = TREE_CHAIN (vfields);
1731 tree vfields;
1732 /* Find the base class with the largest number of virtual functions. */
1733 for (vfields = b->vfields; vfields; vfields = TREE_CHAIN (vfields))
1735 if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)) > b->max_has_virtual)
1736 b->max_has_virtual = CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields));
1737 if (VF_DERIVED_VALUE (vfields)
1738 && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)) > b->max_has_virtual)
1739 b->max_has_virtual = CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields));
1743 if (b->vfield == 0)
1744 /* If all virtual functions come only from virtual baseclasses. */
1745 return -1;
1747 /* Update the rtti base if we have a non-virtual base class version
1748 of it. */
1749 b->rtti = CLASSTYPE_RTTI (BINFO_TYPE (TREE_VEC_ELT (binfos, first_vfn_base_index)));
1751 return first_vfn_base_index;
1754 /* Set memoizing fields and bits of T (and its variants) for later use.
1755 MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables. */
1757 static void
1758 finish_struct_bits (t, max_has_virtual)
1759 tree t;
1760 int max_has_virtual;
1762 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1764 /* Fix up variants (if any). */
1765 tree variants = TYPE_NEXT_VARIANT (t);
1766 while (variants)
1768 /* These fields are in the _TYPE part of the node, not in
1769 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1770 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1771 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1772 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1773 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
1775 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1776 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1777 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1778 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1779 /* Copy whatever these are holding today. */
1780 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1781 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1782 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1783 TYPE_SIZE (variants) = TYPE_SIZE (t);
1784 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1785 variants = TYPE_NEXT_VARIANT (variants);
1788 if (n_baseclasses && max_has_virtual)
1790 /* For a class w/o baseclasses, `finish_struct' has set
1791 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by definition). Similarly
1792 for a class who's base classes do not have vtables. When neither
1793 of these is true, we might have removed abstract virtuals (by
1794 providing a definition), added some (by declaring new ones), or
1795 redeclared ones from a base class. We need to recalculate what's
1796 really an abstract virtual at this point (by looking in the
1797 vtables). */
1798 CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
1801 if (n_baseclasses)
1803 /* Notice whether this class has type conversion functions defined. */
1804 tree binfo = TYPE_BINFO (t);
1805 tree binfos = BINFO_BASETYPES (binfo);
1806 tree basetype;
1808 for (i = n_baseclasses-1; i >= 0; i--)
1810 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1812 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1816 /* If this type has a copy constructor, force its mode to be BLKmode, and
1817 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1818 be passed by invisible reference and prevent it from being returned in
1819 a register.
1821 Also do this if the class has BLKmode but can still be returned in
1822 registers, since function_cannot_inline_p won't let us inline
1823 functions returning such a type. This affects the HP-PA. */
1824 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1825 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1826 && CLASSTYPE_NON_AGGREGATE (t)))
1828 tree variants;
1829 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1830 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1832 TYPE_MODE (variants) = BLKmode;
1833 TREE_ADDRESSABLE (variants) = 1;
1838 /* Issue warnings about T having private constructors, but no friends,
1839 and so forth.
1841 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1842 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1843 non-private static member functions. */
1845 static void
1846 maybe_warn_about_overly_private_class (t)
1847 tree t;
1849 int has_member_fn = 0;
1850 int has_nonprivate_method = 0;
1851 tree fn;
1853 if (!warn_ctor_dtor_privacy
1854 /* If the class has friends, those entities might create and
1855 access instances, so we should not warn. */
1856 || (CLASSTYPE_FRIEND_CLASSES (t)
1857 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1858 /* We will have warned when the template was declared; there's
1859 no need to warn on every instantiation. */
1860 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1861 /* There's no reason to even consider warning about this
1862 class. */
1863 return;
1865 /* We only issue one warning, if more than one applies, because
1866 otherwise, on code like:
1868 class A {
1869 // Oops - forgot `public:'
1870 A();
1871 A(const A&);
1872 ~A();
1875 we warn several times about essentially the same problem. */
1877 /* Check to see if all (non-constructor, non-destructor) member
1878 functions are private. (Since there are no friends or
1879 non-private statics, we can't ever call any of the private member
1880 functions.) */
1881 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1882 /* We're not interested in compiler-generated methods; they don't
1883 provide any way to call private members. */
1884 if (!DECL_ARTIFICIAL (fn))
1886 if (!TREE_PRIVATE (fn))
1888 if (DECL_STATIC_FUNCTION_P (fn))
1889 /* A non-private static member function is just like a
1890 friend; it can create and invoke private member
1891 functions, and be accessed without a class
1892 instance. */
1893 return;
1895 has_nonprivate_method = 1;
1896 break;
1898 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1899 has_member_fn = 1;
1902 if (!has_nonprivate_method && has_member_fn)
1904 /* There are no non-private methods, and there's at least one
1905 private member function that isn't a constructor or
1906 destructor. (If all the private members are
1907 constructors/destructors we want to use the code below that
1908 issues error messages specifically referring to
1909 constructors/destructors.) */
1910 int i;
1911 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1912 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1913 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1914 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1916 has_nonprivate_method = 1;
1917 break;
1919 if (!has_nonprivate_method)
1921 cp_warning ("all member functions in class `%T' are private", t);
1922 return;
1926 /* Even if some of the member functions are non-private, the class
1927 won't be useful for much if all the constructors or destructors
1928 are private: such an object can never be created or destroyed. */
1929 if (TYPE_HAS_DESTRUCTOR (t))
1931 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
1933 if (TREE_PRIVATE (dtor))
1935 cp_warning ("`%#T' only defines a private destructor and has no friends",
1937 return;
1941 if (TYPE_HAS_CONSTRUCTOR (t))
1943 int nonprivate_ctor = 0;
1945 /* If a non-template class does not define a copy
1946 constructor, one is defined for it, enabling it to avoid
1947 this warning. For a template class, this does not
1948 happen, and so we would normally get a warning on:
1950 template <class T> class C { private: C(); };
1952 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1953 complete non-template or fully instantiated classes have this
1954 flag set. */
1955 if (!TYPE_HAS_INIT_REF (t))
1956 nonprivate_ctor = 1;
1957 else
1958 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
1960 fn = OVL_NEXT (fn))
1962 tree ctor = OVL_CURRENT (fn);
1963 /* Ideally, we wouldn't count copy constructors (or, in
1964 fact, any constructor that takes an argument of the
1965 class type as a parameter) because such things cannot
1966 be used to construct an instance of the class unless
1967 you already have one. But, for now at least, we're
1968 more generous. */
1969 if (! TREE_PRIVATE (ctor))
1971 nonprivate_ctor = 1;
1972 break;
1976 if (nonprivate_ctor == 0)
1978 cp_warning ("`%#T' only defines private constructors and has no friends",
1980 return;
1985 /* Function to help qsort sort FIELD_DECLs by name order. */
1987 static int
1988 field_decl_cmp (x, y)
1989 const tree *x, *y;
1991 if (DECL_NAME (*x) == DECL_NAME (*y))
1992 return 0;
1993 if (DECL_NAME (*x) == NULL_TREE)
1994 return -1;
1995 if (DECL_NAME (*y) == NULL_TREE)
1996 return 1;
1997 if (DECL_NAME (*x) < DECL_NAME (*y))
1998 return -1;
1999 return 1;
2002 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2004 static int
2005 method_name_cmp (m1, m2)
2006 const tree *m1, *m2;
2008 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2009 return 0;
2010 if (*m1 == NULL_TREE)
2011 return -1;
2012 if (*m2 == NULL_TREE)
2013 return 1;
2014 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2015 return -1;
2016 return 1;
2019 /* Warn about duplicate methods in fn_fields. Also compact method
2020 lists so that lookup can be made faster.
2022 Data Structure: List of method lists. The outer list is a
2023 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2024 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2025 links the entire list of methods for TYPE_METHODS. Friends are
2026 chained in the same way as member functions (? TREE_CHAIN or
2027 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2028 list. That allows them to be quickly deleted, and requires no
2029 extra storage.
2031 If there are any constructors/destructors, they are moved to the
2032 front of the list. This makes pushclass more efficient.
2034 @@ The above comment is obsolete. It mostly describes what add_method
2035 @@ and add_implicitly_declared_members do.
2037 Sort methods that are not special (i.e., constructors, destructors, and
2038 type conversion operators) so that we can find them faster in search. */
2040 static void
2041 finish_struct_methods (t)
2042 tree t;
2044 tree fn_fields;
2045 tree method_vec;
2046 tree ctor_name = constructor_name (t);
2047 int slot, len;
2049 if (!TYPE_METHODS (t))
2051 /* Clear these for safety; perhaps some parsing error could set
2052 these incorrectly. */
2053 TYPE_HAS_CONSTRUCTOR (t) = 0;
2054 TYPE_HAS_DESTRUCTOR (t) = 0;
2055 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2056 return;
2059 method_vec = CLASSTYPE_METHOD_VEC (t);
2060 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2061 len = TREE_VEC_LENGTH (method_vec);
2063 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2064 and the next few with type conversion operators (if any). */
2065 for (fn_fields = TYPE_METHODS (t); fn_fields;
2066 fn_fields = TREE_CHAIN (fn_fields))
2068 tree fn_name = DECL_NAME (fn_fields);
2070 /* Clear out this flag.
2072 @@ Doug may figure out how to break
2073 @@ this with nested classes and friends. */
2074 DECL_IN_AGGR_P (fn_fields) = 0;
2076 /* Note here that a copy ctor is private, so we don't dare generate
2077 a default copy constructor for a class that has a member
2078 of this type without making sure they have access to it. */
2079 if (fn_name == ctor_name)
2081 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2082 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2084 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2085 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2087 if (TREE_CHAIN (parmtypes) == NULL_TREE
2088 || TREE_CHAIN (parmtypes) == void_list_node
2089 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2091 if (TREE_PROTECTED (fn_fields))
2092 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2093 else if (TREE_PRIVATE (fn_fields))
2094 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2098 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2100 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2102 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2104 if (TREE_PROTECTED (fn_fields))
2105 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2106 else if (TREE_PRIVATE (fn_fields))
2107 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2112 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2113 /* We thought there was a destructor, but there wasn't. Some
2114 parse errors cause this anomalous situation. */
2115 TYPE_HAS_DESTRUCTOR (t) = 0;
2117 /* Issue warnings about private constructors and such. If there are
2118 no methods, then some public defaults are generated. */
2119 maybe_warn_about_overly_private_class (t);
2121 /* Now sort the methods. */
2122 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2123 len--;
2124 TREE_VEC_LENGTH (method_vec) = len;
2126 /* The type conversion ops have to live at the front of the vec, so we
2127 can't sort them. */
2128 for (slot = 2; slot < len; ++slot)
2130 tree fn = TREE_VEC_ELT (method_vec, slot);
2132 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2133 break;
2135 if (len - slot > 1)
2136 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2137 (int (*)(const void *, const void *))method_name_cmp);
2140 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2142 void
2143 duplicate_tag_error (t)
2144 tree t;
2146 cp_error ("redefinition of `%#T'", t);
2147 cp_error_at ("previous definition here", t);
2149 /* Pretend we haven't defined this type. */
2151 /* All of the component_decl's were TREE_CHAINed together in the parser.
2152 finish_struct_methods walks these chains and assembles all methods with
2153 the same base name into DECL_CHAINs. Now we don't need the parser chains
2154 anymore, so we unravel them. */
2156 /* This used to be in finish_struct, but it turns out that the
2157 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2158 things... */
2159 if (CLASSTYPE_METHOD_VEC (t))
2161 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2162 int i, len = TREE_VEC_LENGTH (method_vec);
2163 for (i = 0; i < len; i++)
2165 tree unchain = TREE_VEC_ELT (method_vec, i);
2166 while (unchain != NULL_TREE)
2168 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2169 unchain = OVL_NEXT (unchain);
2174 if (TYPE_LANG_SPECIFIC (t))
2176 tree binfo = TYPE_BINFO (t);
2177 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2178 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2179 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2180 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2182 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2183 BINFO_BASETYPES(binfo) = NULL_TREE;
2185 TYPE_BINFO (t) = binfo;
2186 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2187 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2188 TYPE_REDEFINED (t) = 1;
2189 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2190 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2192 TYPE_SIZE (t) = NULL_TREE;
2193 TYPE_MODE (t) = VOIDmode;
2194 TYPE_FIELDS (t) = NULL_TREE;
2195 TYPE_METHODS (t) = NULL_TREE;
2196 TYPE_VFIELD (t) = NULL_TREE;
2197 TYPE_CONTEXT (t) = NULL_TREE;
2198 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2201 /* Construct the initializer for BINFOs virtual function table. */
2203 static tree
2204 build_vtbl_initializer (binfo)
2205 tree binfo;
2207 tree v = BINFO_VIRTUALS (binfo);
2208 tree inits = NULL_TREE;
2210 /* Process the RTTI stuff at the head of the list. If we're not
2211 using vtable thunks, then the RTTI entry is just an ordinary
2212 function, and we can process it just like the other virtual
2213 function entries. */
2214 if (!CLASSTYPE_COM_INTERFACE (BINFO_TYPE (binfo))
2215 && flag_vtable_thunks)
2217 tree offset;
2218 tree init;
2220 /* The first entry is an offset. */
2221 offset = TREE_PURPOSE (v);
2222 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST,
2223 19990727);
2225 /* Convert the offset to look like a function pointer, so that
2226 we can put it in the vtable. */
2227 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
2228 TREE_CONSTANT (init) = 1;
2229 init = build_vtable_entry (integer_zero_node, init);
2230 inits = tree_cons (NULL_TREE, init, inits);
2232 /* Even in this case, the second entry (the tdesc pointer) is
2233 just an ordinary function. */
2234 v = TREE_CHAIN (v);
2237 /* Go through all the ordinary virtual functions, building up
2238 initializers. */
2239 while (v)
2241 tree delta;
2242 tree fn;
2243 tree init;
2245 /* Pull the offset for `this', and the function to call, out of
2246 the list. */
2247 delta = TREE_PURPOSE (v);
2248 fn = TREE_VALUE (v);
2249 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
2250 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
2252 /* You can't call an abstract virtual function; it's abstract.
2253 So, we replace these functions with __pure_virtual. */
2254 if (DECL_ABSTRACT_VIRTUAL_P (fn))
2255 fn = abort_fndecl;
2257 /* Package up that information for the vtable. */
2258 init = build_vtable_entry_for_fn (delta, fn);
2259 /* And add it to the chain of initializers. */
2260 inits = tree_cons (NULL_TREE, init, inits);
2262 /* Keep going. */
2263 v = TREE_CHAIN (v);
2266 /* The initializers were built up in reverse order; straighten them
2267 out now. */
2268 inits = nreverse (inits);
2269 /* Package all the initializers up as an array initializer. */
2270 return build_nt (CONSTRUCTOR, NULL_TREE, inits);
2273 /* finish up all new vtables. */
2275 static void
2276 finish_vtbls (binfo, do_self, t)
2277 tree binfo;
2278 int do_self;
2279 tree t;
2281 tree binfos = BINFO_BASETYPES (binfo);
2282 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2284 /* Should we use something besides CLASSTYPE_VFIELDS? */
2285 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2287 if (BINFO_NEW_VTABLE_MARKED (binfo))
2289 tree decl, context;
2291 decl = BINFO_VTABLE (binfo);
2292 context = DECL_CONTEXT (decl);
2293 DECL_CONTEXT (decl) = 0;
2294 DECL_INITIAL (decl) = build_vtbl_initializer (binfo);
2295 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
2296 DECL_CONTEXT (decl) = context;
2298 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2301 for (i = 0; i < n_baselinks; i++)
2303 tree base_binfo = TREE_VEC_ELT (binfos, i);
2304 int is_not_base_vtable
2305 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2306 if (TREE_VIA_VIRTUAL (base_binfo))
2307 base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2308 CLASSTYPE_VBASECLASSES (t));
2309 finish_vtbls (base_binfo, is_not_base_vtable, t);
2313 /* True if we should override the given BASE_FNDECL with the given
2314 FNDECL. */
2316 static int
2317 overrides (fndecl, base_fndecl)
2318 tree fndecl, base_fndecl;
2320 /* Destructors have special names. */
2321 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2322 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2323 return 1;
2324 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2325 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2326 return 0;
2327 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2329 tree types, base_types;
2330 #if 0
2331 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2332 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2333 #endif
2334 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2335 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2336 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2337 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2338 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2339 return 1;
2341 return 0;
2344 static tree
2345 get_class_offset_1 (parent, binfo, context, t, fndecl)
2346 tree parent, binfo, context, t, fndecl;
2348 tree binfos = BINFO_BASETYPES (binfo);
2349 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2350 tree rval = NULL_TREE;
2352 if (binfo == parent)
2353 return error_mark_node;
2355 for (i = 0; i < n_baselinks; i++)
2357 tree base_binfo = TREE_VEC_ELT (binfos, i);
2358 tree nrval;
2360 if (TREE_VIA_VIRTUAL (base_binfo))
2361 base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2362 CLASSTYPE_VBASECLASSES (t));
2363 nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl);
2364 /* See if we have a new value */
2365 if (nrval && (nrval != error_mark_node || rval==0))
2367 /* Only compare if we have two offsets */
2368 if (rval && rval != error_mark_node
2369 && ! tree_int_cst_equal (nrval, rval))
2371 /* Only give error if the two offsets are different */
2372 error ("every virtual function must have a unique final overrider");
2373 cp_error (" found two (or more) `%T' class subobjects in `%T'", context, t);
2374 cp_error (" with virtual `%D' from virtual base class", fndecl);
2375 return rval;
2377 rval = nrval;
2380 if (rval && BINFO_TYPE (binfo) == context)
2382 my_friendly_assert (rval == error_mark_node
2383 || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999);
2384 rval = BINFO_OFFSET (binfo);
2387 return rval;
2390 /* Get the offset to the CONTEXT subobject that is related to the
2391 given BINFO. */
2393 static tree
2394 get_class_offset (context, t, binfo, fndecl)
2395 tree context, t, binfo, fndecl;
2397 tree first_binfo = binfo;
2398 tree offset;
2399 int i;
2401 if (context == t)
2402 return integer_zero_node;
2404 if (BINFO_TYPE (binfo) == context)
2405 return BINFO_OFFSET (binfo);
2407 /* Check less derived binfos first. */
2408 while (BINFO_BASETYPES (binfo)
2409 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
2411 tree binfos = BINFO_BASETYPES (binfo);
2412 binfo = TREE_VEC_ELT (binfos, i);
2413 if (BINFO_TYPE (binfo) == context)
2414 return BINFO_OFFSET (binfo);
2417 /* Ok, not found in the less derived binfos, now check the more
2418 derived binfos. */
2419 offset = get_class_offset_1 (first_binfo, TYPE_BINFO (t), context, t, fndecl);
2420 if (offset==0 || TREE_CODE (offset) != INTEGER_CST)
2421 my_friendly_abort (999); /* we have to find it. */
2422 return offset;
2425 /* Skip RTTI information at the front of the virtual list. */
2427 unsigned HOST_WIDE_INT
2428 skip_rtti_stuff (virtuals, t)
2429 tree *virtuals, t;
2431 int n;
2433 if (CLASSTYPE_COM_INTERFACE (t))
2434 return 0;
2436 n = 0;
2437 if (*virtuals)
2439 /* We always reserve a slot for the offset/tdesc entry. */
2440 ++n;
2441 *virtuals = TREE_CHAIN (*virtuals);
2443 if (flag_vtable_thunks && *virtuals)
2445 /* The second slot is reserved for the tdesc pointer when thunks
2446 are used. */
2447 ++n;
2448 *virtuals = TREE_CHAIN (*virtuals);
2450 return n;
2453 static void
2454 modify_one_vtable (binfo, t, fndecl)
2455 tree binfo, t, fndecl;
2457 tree virtuals = BINFO_VIRTUALS (binfo);
2458 unsigned HOST_WIDE_INT n;
2460 /* update rtti entry */
2461 if (flag_rtti)
2463 if (binfo == TYPE_BINFO (t))
2465 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2466 build_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))), t);
2468 else
2470 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2471 prepare_fresh_vtable (binfo, t);
2474 if (fndecl == NULL_TREE)
2475 return;
2477 n = skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo));
2479 while (virtuals)
2481 tree current_fndecl = TREE_VALUE (virtuals);
2483 /* We should never have an instance of __pure_virtual on the
2484 BINFO_VIRTUALS list. If we do, then we will never notice
2485 that the function that should have been there instead has
2486 been overridden. */
2487 my_friendly_assert (current_fndecl != abort_fndecl,
2488 19990727);
2490 if (current_fndecl && overrides (fndecl, current_fndecl))
2492 tree base_offset, offset;
2493 tree context = DECL_CLASS_CONTEXT (fndecl);
2494 tree vfield = TYPE_VFIELD (t);
2495 tree this_offset;
2497 offset = get_class_offset (context, t, binfo, fndecl);
2499 /* Find the right offset for the this pointer based on the
2500 base class we just found. We have to take into
2501 consideration the virtual base class pointers that we
2502 stick in before the virtual function table pointer.
2504 Also, we want just the delta between the most base class
2505 that we derived this vfield from and us. */
2506 base_offset = size_binop (PLUS_EXPR,
2507 get_derived_offset (binfo, DECL_CONTEXT (current_fndecl)),
2508 BINFO_OFFSET (binfo));
2509 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2511 if (binfo == TYPE_BINFO (t))
2513 /* In this case, it is *type*'s vtable we are modifying.
2514 We start with the approximation that it's vtable is that
2515 of the immediate base class. */
2516 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2517 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2519 else
2521 /* This is our very own copy of `basetype' to play with.
2522 Later, we will fill in all the virtual functions
2523 that override the virtual functions in these base classes
2524 which are not defined by the current type. */
2525 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2526 prepare_fresh_vtable (binfo, t);
2529 #ifdef NOTQUITE
2530 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
2531 #endif
2532 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2533 this_offset,
2534 fndecl);
2536 ++n;
2537 virtuals = TREE_CHAIN (virtuals);
2541 /* These are the ones that are not through virtual base classes. */
2543 static void
2544 modify_all_direct_vtables (binfo, do_self, t, fndecl)
2545 tree binfo;
2546 int do_self;
2547 tree t, fndecl;
2549 tree binfos = BINFO_BASETYPES (binfo);
2550 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2552 /* Should we use something besides CLASSTYPE_VFIELDS? */
2553 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2554 modify_one_vtable (binfo, t, fndecl);
2556 for (i = 0; i < n_baselinks; i++)
2558 tree base_binfo = TREE_VEC_ELT (binfos, i);
2559 int is_not_base_vtable
2560 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2561 if (! TREE_VIA_VIRTUAL (base_binfo))
2562 modify_all_direct_vtables (base_binfo, is_not_base_vtable, t, fndecl);
2566 /* Fixup all the delta entries in this one vtable that need updating. */
2568 static void
2569 fixup_vtable_deltas1 (binfo, t)
2570 tree binfo, t;
2572 tree virtuals = BINFO_VIRTUALS (binfo);
2573 unsigned HOST_WIDE_INT n;
2575 n = skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo));
2577 while (virtuals)
2579 tree fndecl = TREE_VALUE (virtuals);
2580 tree delta = TREE_PURPOSE (virtuals);
2582 if (fndecl)
2584 tree base_offset, offset;
2585 tree context = DECL_CLASS_CONTEXT (fndecl);
2586 tree vfield = TYPE_VFIELD (t);
2587 tree this_offset;
2589 offset = get_class_offset (context, t, binfo, fndecl);
2591 /* Find the right offset for the this pointer based on the
2592 base class we just found. We have to take into
2593 consideration the virtual base class pointers that we
2594 stick in before the virtual function table pointer.
2596 Also, we want just the delta between the most base class
2597 that we derived this vfield from and us. */
2598 base_offset = size_binop (PLUS_EXPR,
2599 get_derived_offset (binfo,
2600 DECL_CONTEXT (fndecl)),
2601 BINFO_OFFSET (binfo));
2602 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2604 if (! tree_int_cst_equal (this_offset, delta))
2606 /* Make sure we can modify the derived association with immunity. */
2607 if (binfo == TYPE_BINFO (t))
2609 /* In this case, it is *type*'s vtable we are modifying.
2610 We start with the approximation that it's vtable is that
2611 of the immediate base class. */
2612 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2613 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2615 else
2617 /* This is our very own copy of `basetype' to play with.
2618 Later, we will fill in all the virtual functions
2619 that override the virtual functions in these base classes
2620 which are not defined by the current type. */
2621 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2622 prepare_fresh_vtable (binfo, t);
2625 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2626 this_offset,
2627 fndecl);
2630 ++n;
2631 virtuals = TREE_CHAIN (virtuals);
2635 /* Fixup all the delta entries in all the direct vtables that need updating.
2636 This happens when we have non-overridden virtual functions from a
2637 virtual base class, that are at a different offset, in the new
2638 hierarchy, because the layout of the virtual bases has changed. */
2640 static void
2641 fixup_vtable_deltas (binfo, init_self, t)
2642 tree binfo;
2643 int init_self;
2644 tree t;
2646 tree binfos = BINFO_BASETYPES (binfo);
2647 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2649 for (i = 0; i < n_baselinks; i++)
2651 tree base_binfo = TREE_VEC_ELT (binfos, i);
2652 int is_not_base_vtable
2653 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2654 if (! TREE_VIA_VIRTUAL (base_binfo))
2655 fixup_vtable_deltas (base_binfo, is_not_base_vtable, t);
2657 /* Should we use something besides CLASSTYPE_VFIELDS? */
2658 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2659 fixup_vtable_deltas1 (binfo, t);
2662 /* These are the ones that are through virtual base classes. */
2664 static void
2665 modify_all_indirect_vtables (binfo, do_self, via_virtual, t, fndecl)
2666 tree binfo;
2667 int do_self, via_virtual;
2668 tree t, fndecl;
2670 tree binfos = BINFO_BASETYPES (binfo);
2671 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2673 /* Should we use something besides CLASSTYPE_VFIELDS? */
2674 if (do_self && via_virtual && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2675 modify_one_vtable (binfo, t, fndecl);
2677 for (i = 0; i < n_baselinks; i++)
2679 tree base_binfo = TREE_VEC_ELT (binfos, i);
2680 int is_not_base_vtable
2681 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2682 if (TREE_VIA_VIRTUAL (base_binfo))
2684 via_virtual = 1;
2685 base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2687 modify_all_indirect_vtables (base_binfo, is_not_base_vtable, via_virtual, t, fndecl);
2691 static void
2692 modify_all_vtables (t, fndecl)
2693 tree t;
2694 tree fndecl;
2696 /* Do these first, so that we will make use of any non-virtual class's
2697 vtable, over a virtual classes vtable. */
2698 modify_all_direct_vtables (TYPE_BINFO (t), 1, t, fndecl);
2699 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
2700 modify_all_indirect_vtables (TYPE_BINFO (t), 1, 0, t, fndecl);
2703 /* Here, we already know that they match in every respect.
2704 All we have to check is where they had their declarations. */
2706 static int
2707 strictly_overrides (fndecl1, fndecl2)
2708 tree fndecl1, fndecl2;
2710 int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
2711 DECL_CLASS_CONTEXT (fndecl1),
2712 0, (tree *)0);
2713 if (distance == -2 || distance > 0)
2714 return 1;
2715 return 0;
2718 /* Merge overrides for one vtable.
2719 If we want to merge in same function, we are fine.
2720 else
2721 if one has a DECL_CLASS_CONTEXT that is a parent of the
2722 other, than choose the more derived one
2723 else
2724 potentially ill-formed (see 10.3 [class.virtual])
2725 we have to check later to see if there was an
2726 override in this class. If there was ok, if not
2727 then it is ill-formed. (mrs)
2729 We take special care to reuse a vtable, if we can. */
2731 static void
2732 override_one_vtable (binfo, old, t)
2733 tree binfo, old, t;
2735 tree virtuals = BINFO_VIRTUALS (binfo);
2736 tree old_virtuals = BINFO_VIRTUALS (old);
2737 enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
2739 /* If we have already committed to modifying it, then don't try and
2740 reuse another vtable. */
2741 if (BINFO_NEW_VTABLE_MARKED (binfo))
2742 choose = NEITHER;
2744 skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo));
2745 skip_rtti_stuff (&old_virtuals, BINFO_TYPE (binfo));
2747 while (virtuals)
2749 tree fndecl = TREE_VALUE (virtuals);
2750 tree old_fndecl = TREE_VALUE (old_virtuals);
2752 /* First check to see if they are the same. */
2753 if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
2755 /* No need to do anything. */
2757 else if (strictly_overrides (fndecl, old_fndecl))
2759 if (choose == UNDECIDED)
2760 choose = REUSE_NEW;
2761 else if (choose == REUSE_OLD)
2763 choose = NEITHER;
2764 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2766 prepare_fresh_vtable (binfo, t);
2767 override_one_vtable (binfo, old, t);
2768 return;
2772 else if (strictly_overrides (old_fndecl, fndecl))
2774 if (choose == UNDECIDED)
2775 choose = REUSE_OLD;
2776 else if (choose == REUSE_NEW)
2778 choose = NEITHER;
2779 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2781 prepare_fresh_vtable (binfo, t);
2782 override_one_vtable (binfo, old, t);
2783 return;
2785 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2787 else if (choose == NEITHER)
2789 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2792 else
2794 choose = NEITHER;
2795 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2797 prepare_fresh_vtable (binfo, t);
2798 override_one_vtable (binfo, old, t);
2799 return;
2802 /* This MUST be overridden, or the class is ill-formed. */
2803 tree fndecl = TREE_VALUE (virtuals);
2805 fndecl = copy_node (fndecl);
2806 copy_lang_decl (fndecl);
2807 DECL_NEEDS_FINAL_OVERRIDER_P (fndecl) = 1;
2808 /* Make sure we search for it later. */
2809 if (! CLASSTYPE_ABSTRACT_VIRTUALS (t))
2810 CLASSTYPE_ABSTRACT_VIRTUALS (t) = error_mark_node;
2812 /* We can use integer_zero_node, as we will core dump
2813 if this is used anyway. */
2814 TREE_PURPOSE (virtuals) = integer_zero_node;
2815 TREE_VALUE (virtuals) = fndecl;
2818 virtuals = TREE_CHAIN (virtuals);
2819 old_virtuals = TREE_CHAIN (old_virtuals);
2822 /* Let's reuse the old vtable. */
2823 if (choose == REUSE_OLD)
2825 BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
2826 BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
2830 /* Merge in overrides for virtual bases.
2831 BINFO is the hierarchy we want to modify, and OLD has the potential
2832 overrides. */
2834 static void
2835 merge_overrides (binfo, old, do_self, t)
2836 tree binfo, old;
2837 int do_self;
2838 tree t;
2840 tree binfos = BINFO_BASETYPES (binfo);
2841 tree old_binfos = BINFO_BASETYPES (old);
2842 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2844 /* Should we use something besides CLASSTYPE_VFIELDS? */
2845 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2847 override_one_vtable (binfo, old, t);
2850 for (i = 0; i < n_baselinks; i++)
2852 tree base_binfo = TREE_VEC_ELT (binfos, i);
2853 tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
2854 int is_not_base_vtable
2855 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2856 if (! TREE_VIA_VIRTUAL (base_binfo))
2857 merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
2861 /* Get the base virtual function declarations in T that are either
2862 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2863 the overrider/hider. */
2865 static tree
2866 get_basefndecls (fndecl, t)
2867 tree fndecl, t;
2869 tree methods = TYPE_METHODS (t);
2870 tree base_fndecls = NULL_TREE;
2871 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2872 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2874 while (methods)
2876 if (TREE_CODE (methods) == FUNCTION_DECL
2877 && DECL_VINDEX (methods) != NULL_TREE
2878 && DECL_NAME (fndecl) == DECL_NAME (methods))
2879 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
2881 methods = TREE_CHAIN (methods);
2884 if (base_fndecls)
2885 return base_fndecls;
2887 for (i = 0; i < n_baseclasses; i++)
2889 tree base_binfo = TREE_VEC_ELT (binfos, i);
2890 tree basetype = BINFO_TYPE (base_binfo);
2892 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2893 base_fndecls);
2896 return base_fndecls;
2899 /* Mark the functions that have been hidden with their overriders.
2900 Since we start out with all functions already marked with a hider,
2901 no need to mark functions that are just hidden.
2903 Subroutine of warn_hidden. */
2905 static void
2906 mark_overriders (fndecl, base_fndecls)
2907 tree fndecl, base_fndecls;
2909 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2911 if (overrides (fndecl, TREE_VALUE (base_fndecls)))
2912 TREE_PURPOSE (base_fndecls) = fndecl;
2916 /* If this declaration supersedes the declaration of
2917 a method declared virtual in the base class, then
2918 mark this field as being virtual as well. */
2920 static void
2921 check_for_override (decl, ctype)
2922 tree decl, ctype;
2924 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
2925 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2926 int virtualp = DECL_VIRTUAL_P (decl);
2927 int found_overriden_fn = 0;
2929 for (i = 0; i < n_baselinks; i++)
2931 tree base_binfo = TREE_VEC_ELT (binfos, i);
2932 if (TYPE_POLYMORPHIC_P (BINFO_TYPE (base_binfo)))
2934 tree tmp = get_matching_virtual
2935 (base_binfo, decl,
2936 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
2938 if (tmp && !found_overriden_fn)
2940 /* If this function overrides some virtual in some base
2941 class, then the function itself is also necessarily
2942 virtual, even if the user didn't explicitly say so. */
2943 DECL_VIRTUAL_P (decl) = 1;
2945 /* The TMP we really want is the one from the deepest
2946 baseclass on this path, taking care not to
2947 duplicate if we have already found it (via another
2948 path to its virtual baseclass. */
2949 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
2951 cp_error_at ("`static %#D' cannot be declared", decl);
2952 cp_error_at (" since `virtual %#D' declared in base class",
2953 tmp);
2954 break;
2956 virtualp = 1;
2958 DECL_VINDEX (decl)
2959 = tree_cons (NULL_TREE, tmp, DECL_VINDEX (decl));
2961 /* We now know that DECL overrides something,
2962 which is all that is important. But, we must
2963 continue to iterate through all the base-classes
2964 in order to allow get_matching_virtual to check for
2965 various illegal overrides. */
2966 found_overriden_fn = 1;
2970 if (virtualp)
2972 if (DECL_VINDEX (decl) == NULL_TREE)
2973 DECL_VINDEX (decl) = error_mark_node;
2974 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2978 /* Warn about hidden virtual functions that are not overridden in t.
2979 We know that constructors and destructors don't apply. */
2981 void
2982 warn_hidden (t)
2983 tree t;
2985 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2986 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2987 int i;
2989 /* We go through each separately named virtual function. */
2990 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2992 tree fns = TREE_VEC_ELT (method_vec, i);
2993 tree fndecl;
2995 tree base_fndecls = NULL_TREE;
2996 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2997 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2999 /* First see if we have any virtual functions in this batch. */
3000 for (; fns; fns = OVL_NEXT (fns))
3002 fndecl = OVL_CURRENT (fns);
3003 if (DECL_VINDEX (fndecl))
3004 break;
3007 if (fns == NULL_TREE)
3008 continue;
3010 /* First we get a list of all possible functions that might be
3011 hidden from each base class. */
3012 for (i = 0; i < n_baseclasses; i++)
3014 tree base_binfo = TREE_VEC_ELT (binfos, i);
3015 tree basetype = BINFO_TYPE (base_binfo);
3017 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3018 base_fndecls);
3021 fns = OVL_NEXT (fns);
3023 /* ...then mark up all the base functions with overriders, preferring
3024 overriders to hiders. */
3025 if (base_fndecls)
3026 for (; fns; fns = OVL_NEXT (fns))
3028 fndecl = OVL_CURRENT (fns);
3029 if (DECL_VINDEX (fndecl))
3030 mark_overriders (fndecl, base_fndecls);
3033 /* Now give a warning for all base functions without overriders,
3034 as they are hidden. */
3035 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3037 if (! overrides (TREE_PURPOSE (base_fndecls),
3038 TREE_VALUE (base_fndecls)))
3040 /* Here we know it is a hider, and no overrider exists. */
3041 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
3042 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
3048 /* Check for things that are invalid. There are probably plenty of other
3049 things we should check for also. */
3051 static void
3052 finish_struct_anon (t)
3053 tree t;
3055 tree field;
3057 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3059 if (TREE_STATIC (field))
3060 continue;
3061 if (TREE_CODE (field) != FIELD_DECL)
3062 continue;
3064 if (DECL_NAME (field) == NULL_TREE
3065 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3067 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3068 for (; elt; elt = TREE_CHAIN (elt))
3070 if (DECL_ARTIFICIAL (elt))
3071 continue;
3073 if (DECL_NAME (elt) == constructor_name (t))
3074 cp_pedwarn_at ("ANSI C++ forbids member `%D' with same name as enclosing class",
3075 elt);
3077 if (TREE_CODE (elt) != FIELD_DECL)
3079 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3080 elt);
3081 continue;
3084 if (TREE_PRIVATE (elt))
3085 cp_pedwarn_at ("private member `%#D' in anonymous union",
3086 elt);
3087 else if (TREE_PROTECTED (elt))
3088 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3089 elt);
3091 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3092 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3098 extern int interface_only, interface_unknown;
3100 /* Create default constructors, assignment operators, and so forth for
3101 the type indicated by T, if they are needed.
3102 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3103 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3104 cannot have a default constructor, copy constructor taking a const
3105 reference argument, or an assignment operator, respectively. If a
3106 virtual destructor is created, its DECL is returned; otherwise the
3107 return value is NULL_TREE. */
3109 static tree
3110 add_implicitly_declared_members (t, cant_have_default_ctor,
3111 cant_have_const_cctor,
3112 cant_have_assignment)
3113 tree t;
3114 int cant_have_default_ctor;
3115 int cant_have_const_cctor;
3116 int cant_have_assignment;
3118 tree default_fn;
3119 tree implicit_fns = NULL_TREE;
3120 tree name = TYPE_IDENTIFIER (t);
3121 tree virtual_dtor = NULL_TREE;
3122 tree *f;
3124 /* Destructor. */
3125 if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
3127 default_fn = cons_up_default_function (t, name, 0);
3128 check_for_override (default_fn, t);
3130 /* If we couldn't make it work, then pretend we didn't need it. */
3131 if (default_fn == void_type_node)
3132 TYPE_NEEDS_DESTRUCTOR (t) = 0;
3133 else
3135 TREE_CHAIN (default_fn) = implicit_fns;
3136 implicit_fns = default_fn;
3138 if (DECL_VINDEX (default_fn))
3139 virtual_dtor = default_fn;
3142 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3144 /* Default constructor. */
3145 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3147 default_fn = cons_up_default_function (t, name, 2);
3148 TREE_CHAIN (default_fn) = implicit_fns;
3149 implicit_fns = default_fn;
3152 /* Copy constructor. */
3153 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3155 /* ARM 12.18: You get either X(X&) or X(const X&), but
3156 not both. --Chip */
3157 default_fn = cons_up_default_function (t, name,
3158 3 + cant_have_const_cctor);
3159 TREE_CHAIN (default_fn) = implicit_fns;
3160 implicit_fns = default_fn;
3163 /* Assignment operator. */
3164 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3166 default_fn = cons_up_default_function (t, name,
3167 5 + cant_have_assignment);
3168 TREE_CHAIN (default_fn) = implicit_fns;
3169 implicit_fns = default_fn;
3172 /* Now, hook all of the new functions on to TYPE_METHODS,
3173 and add them to the CLASSTYPE_METHOD_VEC. */
3174 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3175 add_method (t, 0, *f);
3176 *f = TYPE_METHODS (t);
3177 TYPE_METHODS (t) = implicit_fns;
3179 return virtual_dtor;
3182 /* Subroutine of finish_struct_1. Recursively count the number of fields
3183 in TYPE, including anonymous union members. */
3185 static int
3186 count_fields (fields)
3187 tree fields;
3189 tree x;
3190 int n_fields = 0;
3191 for (x = fields; x; x = TREE_CHAIN (x))
3193 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3194 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3195 else
3196 n_fields += 1;
3198 return n_fields;
3201 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3202 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3204 static int
3205 add_fields_to_vec (fields, field_vec, idx)
3206 tree fields, field_vec;
3207 int idx;
3209 tree x;
3210 for (x = fields; x; x = TREE_CHAIN (x))
3212 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3213 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3214 else
3215 TREE_VEC_ELT (field_vec, idx++) = x;
3217 return idx;
3220 /* FIELD is a bit-field. We are finishing the processing for its
3221 enclosing type. Issue any appropriate messages and set appropriate
3222 flags. */
3224 static void
3225 check_bitfield_decl (field)
3226 tree field;
3228 tree type = TREE_TYPE (field);
3230 /* Invalid bit-field size done by grokfield. */
3231 /* Detect invalid bit-field type. Simply checking if TYPE is
3232 integral is insufficient, as that is the array core of the field
3233 type. If TREE_TYPE (field) is integral, then TYPE must be the same. */
3234 if (DECL_INITIAL (field)
3235 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3237 cp_error_at ("bit-field `%#D' with non-integral type", field);
3238 DECL_INITIAL (field) = NULL;
3241 /* Detect and ignore out of range field width. */
3242 if (DECL_INITIAL (field))
3244 tree w = DECL_INITIAL (field);
3245 register int width = 0;
3247 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3248 STRIP_NOPS (w);
3250 /* detect invalid field size. */
3251 if (TREE_CODE (w) == CONST_DECL)
3252 w = DECL_INITIAL (w);
3253 else if (TREE_READONLY_DECL_P (w))
3254 w = decl_constant_value (w);
3256 if (TREE_CODE (w) != INTEGER_CST)
3258 cp_error_at ("bit-field `%D' width not an integer constant",
3259 field);
3260 DECL_INITIAL (field) = NULL_TREE;
3262 else if (width = TREE_INT_CST_LOW (w),
3263 width < 0)
3265 DECL_INITIAL (field) = NULL;
3266 cp_error_at ("negative width in bit-field `%D'", field);
3268 else if (width == 0 && DECL_NAME (field) != 0)
3270 DECL_INITIAL (field) = NULL;
3271 cp_error_at ("zero width for bit-field `%D'", field);
3273 else if (width
3274 > TYPE_PRECISION (long_long_unsigned_type_node))
3276 /* The backend will dump if you try to use something too
3277 big; avoid that. */
3278 DECL_INITIAL (field) = NULL;
3279 sorry ("bit-fields larger than %d bits",
3280 TYPE_PRECISION (long_long_unsigned_type_node));
3281 cp_error_at (" in declaration of `%D'", field);
3283 else if (width > TYPE_PRECISION (type)
3284 && TREE_CODE (type) != ENUMERAL_TYPE
3285 && TREE_CODE (type) != BOOLEAN_TYPE)
3286 cp_warning_at ("width of `%D' exceeds its type", field);
3287 else if (TREE_CODE (type) == ENUMERAL_TYPE
3288 && ((min_precision (TYPE_MIN_VALUE (type),
3289 TREE_UNSIGNED (type)) > width)
3290 || (min_precision (TYPE_MAX_VALUE (type),
3291 TREE_UNSIGNED (type)) > width)))
3292 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3293 field, type);
3295 if (DECL_INITIAL (field))
3297 DECL_INITIAL (field) = NULL_TREE;
3298 DECL_FIELD_SIZE (field) = width;
3299 DECL_BIT_FIELD (field) = 1;
3301 if (width == 0)
3303 #ifdef EMPTY_FIELD_BOUNDARY
3304 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3305 EMPTY_FIELD_BOUNDARY);
3306 #endif
3307 #ifdef PCC_BITFIELD_TYPE_MATTERS
3308 if (PCC_BITFIELD_TYPE_MATTERS)
3309 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3310 TYPE_ALIGN (type));
3311 #endif
3315 else
3316 /* Non-bit-fields are aligned for their type. */
3317 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3320 /* FIELD is a non bit-field. We are finishing the processing for its
3321 enclosing type T. Issue any appropriate messages and set appropriate
3322 flags. */
3324 static void
3325 check_field_decl (field, t, cant_have_const_ctor,
3326 cant_have_default_ctor, no_const_asn_ref,
3327 any_default_members)
3328 tree field;
3329 tree t;
3330 int *cant_have_const_ctor;
3331 int *cant_have_default_ctor;
3332 int *no_const_asn_ref;
3333 int *any_default_members;
3335 tree type = strip_array_types (TREE_TYPE (field));
3337 /* An anonymous union cannot contain any fields which would change
3338 the settings of CANT_HAVE_CONST_CTOR and friends. */
3339 if (ANON_UNION_TYPE_P (type))
3341 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3342 structs. So, we recurse through their fields here. */
3343 else if (ANON_AGGR_TYPE_P (type))
3345 tree fields;
3347 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3348 if (TREE_CODE (field) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3349 check_field_decl (fields, t, cant_have_const_ctor,
3350 cant_have_default_ctor, no_const_asn_ref,
3351 any_default_members);
3353 /* Check members with class type for constructors, destructors,
3354 etc. */
3355 else if (CLASS_TYPE_P (type))
3357 /* Never let anything with uninheritable virtuals
3358 make it through without complaint. */
3359 abstract_virtuals_error (field, type);
3361 if (TREE_CODE (t) == UNION_TYPE)
3363 if (TYPE_NEEDS_CONSTRUCTING (type))
3364 cp_error_at ("member `%#D' with constructor not allowed in union",
3365 field);
3366 if (TYPE_NEEDS_DESTRUCTOR (type))
3367 cp_error_at ("member `%#D' with destructor not allowed in union",
3368 field);
3369 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3370 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3371 field);
3373 else
3375 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3376 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3377 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3378 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3381 if (!TYPE_HAS_CONST_INIT_REF (type))
3382 *cant_have_const_ctor = 1;
3384 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3385 *no_const_asn_ref = 1;
3387 if (TYPE_HAS_CONSTRUCTOR (type)
3388 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3389 *cant_have_default_ctor = 1;
3391 if (DECL_INITIAL (field) != NULL_TREE)
3393 /* `build_class_init_list' does not recognize
3394 non-FIELD_DECLs. */
3395 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3396 cp_error_at ("multiple fields in union `%T' initialized");
3397 *any_default_members = 1;
3400 /* Non-bit-fields are aligned for their type, except packed fields
3401 which require only BITS_PER_UNIT alignment. */
3402 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3403 (DECL_PACKED (field)
3404 ? BITS_PER_UNIT
3405 : TYPE_ALIGN (TREE_TYPE (field))));
3408 /* Check the data members (both static and non-static), class-scoped
3409 typedefs, etc., appearing in the declaration of T. Issue
3410 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3411 declaration order) of access declarations; each TREE_VALUE in this
3412 list is a USING_DECL.
3414 In addition, set the following flags:
3416 EMPTY_P
3417 The class is empty, i.e., contains no non-static data members.
3419 CANT_HAVE_DEFAULT_CTOR_P
3420 This class cannot have an implicitly generated default
3421 constructor.
3423 CANT_HAVE_CONST_CTOR_P
3424 This class cannot have an implicitly generated copy constructor
3425 taking a const reference.
3427 CANT_HAVE_CONST_ASN_REF
3428 This class cannot have an implicitly generated assignment
3429 operator taking a const reference.
3431 All of these flags should be initialized before calling this
3432 function.
3434 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3435 fields can be added by adding to this chain. */
3437 static void
3438 check_field_decls (t, access_decls, empty_p,
3439 cant_have_default_ctor_p, cant_have_const_ctor_p,
3440 no_const_asn_ref_p)
3441 tree t;
3442 tree *access_decls;
3443 int *empty_p;
3444 int *cant_have_default_ctor_p;
3445 int *cant_have_const_ctor_p;
3446 int *no_const_asn_ref_p;
3448 tree *field;
3449 tree *next;
3450 int has_pointers;
3451 int any_default_members;
3453 /* First, delete any duplicate fields. */
3454 delete_duplicate_fields (TYPE_FIELDS (t));
3456 /* Assume there are no access declarations. */
3457 *access_decls = NULL_TREE;
3458 /* Assume this class has no pointer members. */
3459 has_pointers = 0;
3460 /* Assume none of the members of this class have default
3461 initializations. */
3462 any_default_members = 0;
3464 for (field = &TYPE_FIELDS (t); *field; field = next)
3466 tree x = *field;
3467 tree type = TREE_TYPE (x);
3469 GNU_xref_member (current_class_name, x);
3471 next = &TREE_CHAIN (x);
3473 if (TREE_CODE (x) == FIELD_DECL)
3475 DECL_PACKED (x) |= TYPE_PACKED (t);
3477 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3478 /* We don't treat zero-width bitfields as making a class
3479 non-empty. */
3481 else
3482 *empty_p = 0;
3485 if (TREE_CODE (x) == USING_DECL)
3487 /* Prune the access declaration from the list of fields. */
3488 *field = TREE_CHAIN (x);
3490 /* Save the access declarations for our caller. */
3491 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3493 /* Since we've reset *FIELD there's no reason to skip to the
3494 next field. */
3495 next = field;
3496 continue;
3499 if (TREE_CODE (x) == TYPE_DECL
3500 || TREE_CODE (x) == TEMPLATE_DECL)
3501 continue;
3503 /* If we've gotten this far, it's a data member, possibly static,
3504 or an enumerator. */
3506 DECL_FIELD_CONTEXT (x) = t;
3508 /* ``A local class cannot have static data members.'' ARM 9.4 */
3509 if (current_function_decl && TREE_STATIC (x))
3510 cp_error_at ("field `%D' in local class cannot be static", x);
3512 /* Perform error checking that did not get done in
3513 grokdeclarator. */
3514 if (TREE_CODE (type) == FUNCTION_TYPE)
3516 cp_error_at ("field `%D' invalidly declared function type",
3518 type = build_pointer_type (type);
3519 TREE_TYPE (x) = type;
3521 else if (TREE_CODE (type) == METHOD_TYPE)
3523 cp_error_at ("field `%D' invalidly declared method type", x);
3524 type = build_pointer_type (type);
3525 TREE_TYPE (x) = type;
3527 else if (TREE_CODE (type) == OFFSET_TYPE)
3529 cp_error_at ("field `%D' invalidly declared offset type", x);
3530 type = build_pointer_type (type);
3531 TREE_TYPE (x) = type;
3534 if (type == error_mark_node)
3535 continue;
3537 DECL_SAVED_INSNS (x) = 0;
3538 DECL_FIELD_SIZE (x) = 0;
3540 /* When this goes into scope, it will be a non-local reference. */
3541 DECL_NONLOCAL (x) = 1;
3543 if (TREE_CODE (x) == CONST_DECL)
3544 continue;
3546 if (TREE_CODE (x) == VAR_DECL)
3548 if (TREE_CODE (t) == UNION_TYPE)
3549 /* Unions cannot have static members. */
3550 cp_error_at ("field `%D' declared static in union", x);
3552 continue;
3555 /* Now it can only be a FIELD_DECL. */
3557 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3558 CLASSTYPE_NON_AGGREGATE (t) = 1;
3560 /* If this is of reference type, check if it needs an init.
3561 Also do a little ANSI jig if necessary. */
3562 if (TREE_CODE (type) == REFERENCE_TYPE)
3564 CLASSTYPE_NON_POD_P (t) = 1;
3565 if (DECL_INITIAL (x) == NULL_TREE)
3566 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3568 /* ARM $12.6.2: [A member initializer list] (or, for an
3569 aggregate, initialization by a brace-enclosed list) is the
3570 only way to initialize nonstatic const and reference
3571 members. */
3572 *cant_have_default_ctor_p = 1;
3573 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3575 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3577 if (DECL_NAME (x))
3578 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3579 else
3580 cp_warning_at ("non-static reference in class without a constructor", x);
3584 type = strip_array_types (type);
3586 if (TREE_CODE (type) == POINTER_TYPE)
3587 has_pointers = 1;
3589 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3590 CLASSTYPE_HAS_MUTABLE (t) = 1;
3592 if (! pod_type_p (type)
3593 /* For some reason, pointers to members are POD types themselves,
3594 but are not allowed in POD structs. Silly. */
3595 || TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
3596 CLASSTYPE_NON_POD_P (t) = 1;
3598 /* If any field is const, the structure type is pseudo-const. */
3599 if (CP_TYPE_CONST_P (type))
3601 C_TYPE_FIELDS_READONLY (t) = 1;
3602 if (DECL_INITIAL (x) == NULL_TREE)
3603 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3605 /* ARM $12.6.2: [A member initializer list] (or, for an
3606 aggregate, initialization by a brace-enclosed list) is the
3607 only way to initialize nonstatic const and reference
3608 members. */
3609 *cant_have_default_ctor_p = 1;
3610 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3612 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3614 if (DECL_NAME (x))
3615 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3616 else
3617 cp_warning_at ("non-static const member in class without a constructor", x);
3620 /* A field that is pseudo-const makes the structure likewise. */
3621 else if (IS_AGGR_TYPE (type))
3623 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3624 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3625 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
3628 /* We set DECL_C_BIT_FIELD in grokbitfield.
3629 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3630 if (DECL_C_BIT_FIELD (x))
3631 check_bitfield_decl (x);
3632 else
3633 check_field_decl (x, t,
3634 cant_have_const_ctor_p,
3635 cant_have_default_ctor_p,
3636 no_const_asn_ref_p,
3637 &any_default_members);
3640 /* Effective C++ rule 11. */
3641 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3642 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3644 cp_warning ("`%#T' has pointer data members", t);
3646 if (! TYPE_HAS_INIT_REF (t))
3648 cp_warning (" but does not override `%T(const %T&)'", t, t);
3649 if (! TYPE_HAS_ASSIGN_REF (t))
3650 cp_warning (" or `operator=(const %T&)'", t);
3652 else if (! TYPE_HAS_ASSIGN_REF (t))
3653 cp_warning (" but does not override `operator=(const %T&)'", t);
3657 /* Check anonymous struct/anonymous union fields. */
3658 finish_struct_anon (t);
3660 /* We've built up the list of access declarations in reverse order.
3661 Fix that now. */
3662 *access_decls = nreverse (*access_decls);
3665 /* Return a FIELD_DECL for a pointer-to-virtual-table or
3666 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
3667 field are as indicated. The CLASS_TYPE in which this field occurs
3668 is also indicated. *EMPTY_P is set to a non-zero value by this
3669 function to indicate that a class containing this field is
3670 non-empty. */
3672 static tree
3673 build_vtbl_or_vbase_field (name, assembler_name, type, class_type,
3674 empty_p)
3675 tree name;
3676 tree assembler_name;
3677 tree type;
3678 tree class_type;
3679 int *empty_p;
3681 tree field;
3683 /* This class is non-empty. */
3684 *empty_p = 0;
3686 /* Build the FIELD_DECL. */
3687 field = build_lang_decl (FIELD_DECL, name, type);
3688 DECL_ASSEMBLER_NAME (field) = assembler_name;
3689 DECL_VIRTUAL_P (field) = 1;
3690 DECL_ARTIFICIAL (field) = 1;
3691 DECL_FIELD_CONTEXT (field) = class_type;
3692 DECL_CLASS_CONTEXT (field) = class_type;
3693 DECL_FCONTEXT (field) = class_type;
3694 DECL_SAVED_INSNS (field) = 0;
3695 DECL_FIELD_SIZE (field) = 0;
3696 DECL_ALIGN (field) = TYPE_ALIGN (type);
3698 /* Return it. */
3699 return field;
3702 /* Returns list of virtual base class pointers in a FIELD_DECL chain. */
3704 static tree
3705 build_vbase_pointer_fields (rec, empty_p)
3706 tree rec;
3707 int *empty_p;
3709 /* Chain to hold all the new FIELD_DECLs which point at virtual
3710 base classes. */
3711 tree vbase_decls = NULL_TREE;
3712 tree binfos = TYPE_BINFO_BASETYPES (rec);
3713 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
3714 tree decl;
3715 int i;
3717 /* Handle basetypes almost like fields, but record their
3718 offsets differently. */
3720 for (i = 0; i < n_baseclasses; i++)
3722 register tree base_binfo = TREE_VEC_ELT (binfos, i);
3723 register tree basetype = BINFO_TYPE (base_binfo);
3725 if (TYPE_SIZE (basetype) == 0)
3726 /* This error is now reported in xref_tag, thus giving better
3727 location information. */
3728 continue;
3730 /* All basetypes are recorded in the association list of the
3731 derived type. */
3733 if (TREE_VIA_VIRTUAL (base_binfo))
3735 int j;
3736 const char *name;
3738 /* The offset for a virtual base class is only used in computing
3739 virtual function tables and for initializing virtual base
3740 pointers. It is built once `get_vbase_types' is called. */
3742 /* If this basetype can come from another vbase pointer
3743 without an additional indirection, we will share
3744 that pointer. If an indirection is involved, we
3745 make our own pointer. */
3746 for (j = 0; j < n_baseclasses; j++)
3748 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
3749 if (! TREE_VIA_VIRTUAL (other_base_binfo)
3750 && binfo_member (basetype,
3751 CLASSTYPE_VBASECLASSES (BINFO_TYPE
3752 (other_base_binfo))
3754 goto got_it;
3756 FORMAT_VBASE_NAME (name, basetype);
3757 decl = build_vtbl_or_vbase_field (get_identifier (name),
3758 get_identifier (VTABLE_BASE),
3759 build_pointer_type (basetype),
3760 rec,
3761 empty_p);
3762 BINFO_VPTR_FIELD (base_binfo) = decl;
3763 TREE_CHAIN (decl) = vbase_decls;
3764 vbase_decls = decl;
3765 *empty_p = 0;
3767 got_it:
3768 /* The space this decl occupies has already been accounted for. */
3773 return vbase_decls;
3776 /* If the empty base field in DECL overlaps with a base of the same type in
3777 NEWDECL, which is either another base field or the first data field of
3778 the class, pad the base just before NEWDECL and return 1. Otherwise,
3779 return 0. */
3781 static int
3782 avoid_overlap (decl, newdecl, empty_p)
3783 tree decl, newdecl;
3784 int *empty_p;
3786 tree field;
3788 if (newdecl == NULL_TREE
3789 || ! types_overlap_p (TREE_TYPE (decl), TREE_TYPE (newdecl)))
3790 return 0;
3792 for (field = decl; TREE_CHAIN (field) && TREE_CHAIN (field) != newdecl;
3793 field = TREE_CHAIN (field))
3796 DECL_SIZE (field) = integer_one_node;
3797 /* The containing class cannot be empty; this field takes up space. */
3798 *empty_p = 0;
3800 return 1;
3803 /* Returns a list of fields to stand in for the base class subobjects
3804 of REC. These fields are later removed by layout_basetypes. */
3806 static tree
3807 build_base_fields (rec, empty_p)
3808 tree rec;
3809 int *empty_p;
3811 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3812 subobjects. */
3813 tree base_decls = NULL_TREE;
3814 tree binfos = TYPE_BINFO_BASETYPES (rec);
3815 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
3816 tree decl, nextdecl;
3817 int i, saw_empty = 0;
3818 unsigned int base_align = 0;
3820 for (i = 0; i < n_baseclasses; ++i)
3822 register tree base_binfo = TREE_VEC_ELT (binfos, i);
3823 register tree basetype = BINFO_TYPE (base_binfo);
3825 if (TYPE_SIZE (basetype) == 0)
3826 /* This error is now reported in xref_tag, thus giving better
3827 location information. */
3828 continue;
3830 if (TREE_VIA_VIRTUAL (base_binfo))
3831 continue;
3833 decl = build_lang_decl (FIELD_DECL, NULL_TREE, basetype);
3834 DECL_ARTIFICIAL (decl) = 1;
3835 DECL_FIELD_CONTEXT (decl) = DECL_CLASS_CONTEXT (decl) = rec;
3836 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3837 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3838 TREE_CHAIN (decl) = base_decls;
3839 base_decls = decl;
3841 if (flag_new_abi && DECL_SIZE (decl) == integer_zero_node)
3842 saw_empty = 1;
3843 else
3845 /* The containing class is non-empty because it has a
3846 non-empty base class. */
3847 *empty_p = 0;
3849 if (! flag_new_abi)
3851 /* Brain damage for backwards compatibility. For no
3852 good reason, the old layout_basetypes made every base
3853 at least as large as the alignment for the bases up
3854 to that point, gratuitously wasting space. So we do
3855 the same thing here. */
3856 base_align = MAX (base_align, DECL_ALIGN (decl));
3857 DECL_SIZE (decl)
3858 = size_int (MAX (TREE_INT_CST_LOW (DECL_SIZE (decl)),
3859 (int) base_align));
3864 /* Reverse the list of fields so we allocate the bases in the proper
3865 order. */
3866 base_decls = nreverse (base_decls);
3868 /* In the presence of empty base classes, we run the risk of allocating
3869 two objects of the same class on top of one another. Avoid that. */
3870 if (flag_new_abi && saw_empty)
3871 for (decl = base_decls; decl; decl = TREE_CHAIN (decl))
3873 if (DECL_SIZE (decl) == integer_zero_node)
3875 /* First step through the following bases until we find
3876 an overlap or a non-empty base. */
3877 for (nextdecl = TREE_CHAIN (decl); nextdecl;
3878 nextdecl = TREE_CHAIN (nextdecl))
3880 if (avoid_overlap (decl, nextdecl, empty_p)
3881 || DECL_SIZE (nextdecl) != integer_zero_node)
3882 goto nextbase;
3885 /* If we're still looking, also check against the first
3886 field. */
3887 for (nextdecl = TYPE_FIELDS (rec);
3888 nextdecl && TREE_CODE (nextdecl) != FIELD_DECL;
3889 nextdecl = TREE_CHAIN (nextdecl))
3890 /* keep looking */;
3891 avoid_overlap (decl, nextdecl, empty_p);
3893 nextbase:;
3896 return base_decls;
3899 /* Go through the TYPE_METHODS of T issuing any appropriate
3900 diagnostics, figuring out which methods override which other
3901 methods, and so forth. */
3903 static void
3904 check_methods (t)
3905 tree t;
3907 tree x;
3909 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3911 GNU_xref_member (current_class_name, x);
3913 /* If this was an evil function, don't keep it in class. */
3914 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3915 continue;
3917 /* Do both of these, even though they're in the same union;
3918 if the insn `r' member and the size `i' member are
3919 different sizes, as on the alpha, the larger of the two
3920 will end up with garbage in it. */
3921 DECL_SAVED_INSNS (x) = 0;
3922 DECL_FIELD_SIZE (x) = 0;
3924 check_for_override (x, t);
3925 if (DECL_ABSTRACT_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3926 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3928 /* The name of the field is the original field name
3929 Save this in auxiliary field for later overloading. */
3930 if (DECL_VINDEX (x))
3932 TYPE_POLYMORPHIC_P (t) = 1;
3933 if (DECL_ABSTRACT_VIRTUAL_P (x))
3934 CLASSTYPE_ABSTRACT_VIRTUALS (t)
3935 = tree_cons (NULL_TREE, x, CLASSTYPE_ABSTRACT_VIRTUALS (t));
3940 /* Remove all zero-width bit-fields from T. */
3942 static void
3943 remove_zero_width_bit_fields (t)
3944 tree t;
3946 tree *fieldsp;
3948 fieldsp = &TYPE_FIELDS (t);
3949 while (*fieldsp)
3951 if (TREE_CODE (*fieldsp) == FIELD_DECL
3952 && DECL_C_BIT_FIELD (*fieldsp)
3953 && DECL_INITIAL (*fieldsp))
3954 *fieldsp = TREE_CHAIN (*fieldsp);
3955 else
3956 fieldsp = &TREE_CHAIN (*fieldsp);
3960 /* Check the validity of the bases and members declared in T. Add any
3961 implicitly-generated functions (like copy-constructors and
3962 assignment operators). Compute various flag bits (like
3963 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
3964 level: i.e., independently of the ABI in use. */
3966 static void
3967 check_bases_and_members (t, empty_p)
3968 tree t;
3969 int *empty_p;
3971 /* Nonzero if we are not allowed to generate a default constructor
3972 for this case. */
3973 int cant_have_default_ctor;
3974 /* Nonzero if the implicitly generated copy constructor should take
3975 a non-const reference argument. */
3976 int cant_have_const_ctor;
3977 /* Nonzero if the the implicitly generated assignment operator
3978 should take a non-const reference argument. */
3979 int no_const_asn_ref;
3980 tree access_decls;
3982 /* By default, we use const reference arguments and generate default
3983 constructors. */
3984 cant_have_default_ctor = 0;
3985 cant_have_const_ctor = 0;
3986 no_const_asn_ref = 0;
3988 /* Check all the base-classes. */
3989 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
3990 &no_const_asn_ref);
3992 /* Check all the data member declarations. */
3993 check_field_decls (t, &access_decls, empty_p,
3994 &cant_have_default_ctor,
3995 &cant_have_const_ctor,
3996 &no_const_asn_ref);
3998 /* Check all the method declarations. */
3999 check_methods (t);
4001 /* Do some bookkeeping that will guide the generation of implicitly
4002 declared member functions. */
4003 TYPE_HAS_COMPLEX_INIT_REF (t)
4004 |= (TYPE_HAS_INIT_REF (t)
4005 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4006 || TYPE_POLYMORPHIC_P (t));
4007 TYPE_NEEDS_CONSTRUCTING (t)
4008 |= (TYPE_HAS_CONSTRUCTOR (t)
4009 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4010 || TYPE_POLYMORPHIC_P (t));
4011 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4012 || TYPE_POLYMORPHIC_P (t));
4013 CLASSTYPE_NON_POD_P (t)
4014 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4015 || TYPE_HAS_ASSIGN_REF (t));
4016 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4017 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4018 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
4020 /* Synthesize any needed methods. Note that methods will be synthesized
4021 for anonymous unions; grok_x_components undoes that. */
4022 add_implicitly_declared_members (t, cant_have_default_ctor,
4023 cant_have_const_ctor,
4024 no_const_asn_ref);
4026 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4027 finish_struct_methods (t);
4029 /* Process the access-declarations. We wait until now to do this
4030 because handle_using_decls requires that the CLASSTYPE_METHOD_VEC
4031 be set up correctly. */
4032 while (access_decls)
4034 handle_using_decl (TREE_VALUE (access_decls), t);
4035 access_decls = TREE_CHAIN (access_decls);
4039 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4040 accordingly, and, if necessary, add the TYPE_VFIELD to the
4041 TYPE_FIELDS list. */
4043 static void
4044 create_vtable_ptr (t, empty_p, has_virtual_p, max_has_virtual_p,
4045 pending_virtuals_p, pending_hard_virtuals_p)
4046 tree t;
4047 int *empty_p;
4048 int *has_virtual_p;
4049 int *max_has_virtual_p;
4050 tree *pending_virtuals_p;
4051 tree *pending_hard_virtuals_p;
4053 tree fn;
4055 /* If possible, we reuse the virtual function table pointer from one
4056 of our base classes. */
4057 if (CLASSTYPE_N_BASECLASSES (t))
4059 struct base_info base_info;
4061 /* Remember where we got our vfield from. */
4062 CLASSTYPE_VFIELD_PARENT (t) = finish_base_struct (t, &base_info);
4063 *has_virtual_p = base_info.has_virtual;
4064 *max_has_virtual_p = base_info.max_has_virtual;
4065 TYPE_VFIELD (t) = base_info.vfield;
4066 CLASSTYPE_VFIELDS (t) = base_info.vfields;
4067 CLASSTYPE_RTTI (t) = base_info.rtti;
4070 /* Loop over the virtual functions, adding them to our various
4071 vtables. */
4072 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4073 if (DECL_VINDEX (fn))
4074 add_virtual_function (pending_virtuals_p, pending_hard_virtuals_p,
4075 has_virtual_p, fn, t);
4077 /* If we couldn't find an appropriate base class, create a new field
4078 here. */
4079 if (*has_virtual_p && !TYPE_VFIELD (t))
4081 /* We build this decl with vtbl_ptr_type_node, which is a
4082 `vtable_entry_type*'. It might seem more precise to use
4083 `vtable_entry_type (*)[N]' where N is the number of firtual
4084 functions. However, that would require the vtable pointer in
4085 base classes to have a different type than the vtable pointer
4086 in derived classes. We could make that happen, but that
4087 still wouldn't solve all the problems. In particular, the
4088 type-based alias analysis code would decide that assignments
4089 to the base class vtable pointer can't alias assignments to
4090 the derived class vtable pointer, since they have different
4091 types. Thus, in an derived class destructor, where the base
4092 class constructor was inlined, we could generate bad code for
4093 setting up the vtable pointer.
4095 Therefore, we use one type for all vtable pointers. We still
4096 use a type-correct type; it's just doesn't indicate the array
4097 bounds. That's better than using `void*' or some such; it's
4098 cleaner, and it let's the alias analysis code know that these
4099 stores cannot alias stores to void*! */
4100 TYPE_VFIELD (t)
4101 = build_vtbl_or_vbase_field (get_vfield_name (t),
4102 get_identifier (VFIELD_BASE),
4103 vtbl_ptr_type_node,
4105 empty_p);
4107 /* Add the new field to the list of fields in this class. */
4108 if (!flag_new_abi)
4109 /* In the old ABI, the vtable pointer goes at the end of the
4110 class. */
4111 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), TYPE_VFIELD (t));
4112 else
4114 /* But in the new ABI, the vtable pointer is the first thing
4115 in the class. */
4116 TYPE_FIELDS (t) = chainon (TYPE_VFIELD (t), TYPE_FIELDS (t));
4117 /* If there were any baseclasses, they can't possibly be at
4118 offset zero any more, because that's where the vtable
4119 pointer is. So, converting to a base class is going to
4120 take work. */
4121 if (CLASSTYPE_N_BASECLASSES (t))
4122 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4125 /* We can't yet add this new field to the list of all virtual
4126 function table pointers in this class. The
4127 modify_all_vtables function depends on this not being done.
4128 So, it is done later, in finish_struct_1. */
4132 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
4133 (or C++ class declaration).
4135 For C++, we must handle the building of derived classes.
4136 Also, C++ allows static class members. The way that this is
4137 handled is to keep the field name where it is (as the DECL_NAME
4138 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
4139 of the field. layout_record and layout_union will know about this.
4141 More C++ hair: inline functions have text in their
4142 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
4143 meaningful tree structure. After the struct has been laid out, set
4144 things up so that this can happen.
4146 And still more: virtual functions. In the case of single inheritance,
4147 when a new virtual function is seen which redefines a virtual function
4148 from the base class, the new virtual function is placed into
4149 the virtual function table at exactly the same address that
4150 it had in the base class. When this is extended to multiple
4151 inheritance, the same thing happens, except that multiple virtual
4152 function tables must be maintained. The first virtual function
4153 table is treated in exactly the same way as in the case of single
4154 inheritance. Additional virtual function tables have different
4155 DELTAs, which tell how to adjust `this' to point to the right thing.
4157 ATTRIBUTES is the set of decl attributes to be applied, if any. */
4159 void
4160 finish_struct_1 (t)
4161 tree t;
4163 tree x;
4164 int has_virtual;
4165 int max_has_virtual;
4166 tree pending_virtuals = NULL_TREE;
4167 tree pending_hard_virtuals = NULL_TREE;
4168 int n_fields = 0;
4169 tree vfield;
4170 int n_baseclasses;
4171 int empty = 1;
4172 tree inline_friends;
4174 if (TYPE_SIZE (t))
4176 if (IS_AGGR_TYPE (t))
4177 cp_error ("redefinition of `%#T'", t);
4178 else
4179 my_friendly_abort (172);
4180 popclass ();
4181 return;
4184 GNU_xref_decl (current_function_decl, t);
4186 /* If this type was previously laid out as a forward reference,
4187 make sure we lay it out again. */
4189 TYPE_SIZE (t) = NULL_TREE;
4190 CLASSTYPE_GOT_SEMICOLON (t) = 0;
4192 CLASSTYPE_VFIELD_PARENT (t) = -1;
4193 has_virtual = 0;
4194 max_has_virtual = 0;
4195 CLASSTYPE_RTTI (t) = NULL_TREE;
4196 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
4198 /* Do end-of-class semantic processing: checking the validity of the
4199 bases and members and adding implicitly generated methods. */
4200 check_bases_and_members (t, &empty);
4202 /* Add pointers to all of our virtual base-classes. */
4203 TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (t, &empty),
4204 TYPE_FIELDS (t));
4205 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4206 TYPE_FIELDS (t) = chainon (build_base_fields (t, &empty),
4207 TYPE_FIELDS (t));
4209 /* Create a pointer to our virtual function table. */
4210 create_vtable_ptr (t, &empty, &has_virtual, &max_has_virtual,
4211 &pending_virtuals, &pending_hard_virtuals);
4213 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4214 we have to save this before we start modifying
4215 TYPE_NONCOPIED_PARTS. */
4216 inline_friends = CLASSTYPE_INLINE_FRIENDS (t);
4217 CLASSTYPE_INLINE_FRIENDS (t) = NULL_TREE;
4219 /* We make all structures have at least one element, so that they
4220 have non-zero size. The field that we add here is fake, in the
4221 sense that, for example, we don't want people to be able to
4222 initialize it later. So, we add it just long enough to let the
4223 back-end lay out the type, and then remove it. */
4224 if (empty)
4226 tree decl = build_lang_decl
4227 (FIELD_DECL, NULL_TREE, char_type_node);
4228 TREE_CHAIN (decl) = TYPE_FIELDS (t);
4229 TYPE_FIELDS (t) = decl;
4230 TYPE_NONCOPIED_PARTS (t)
4231 = tree_cons (NULL_TREE, decl, TYPE_NONCOPIED_PARTS (t));
4232 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
4235 /* Let the back-end lay out the type. Note that at this point we
4236 have only included non-virtual base-classes; we will lay out the
4237 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
4238 this call are not necessarily correct; they are just the size and
4239 alignment when no virtual base clases are used. */
4240 layout_type (t);
4242 /* If we added an extra field to make this class non-empty, remove
4243 it now. */
4244 if (empty)
4245 TYPE_FIELDS (t) = TREE_CHAIN (TYPE_FIELDS (t));
4247 /* Delete all zero-width bit-fields from the list of fields. Now
4248 that the type is laid out they are no longer important. */
4249 remove_zero_width_bit_fields (t);
4251 /* Remember the size and alignment of the class before adding
4252 the virtual bases. */
4253 if (empty && flag_new_abi)
4254 CLASSTYPE_SIZE (t) = integer_zero_node;
4255 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
4256 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
4257 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
4258 else
4259 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
4260 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
4262 /* Set the TYPE_DECL for this type to contain the right
4263 value for DECL_OFFSET, so that we can use it as part
4264 of a COMPONENT_REF for multiple inheritance. */
4266 layout_decl (TYPE_MAIN_DECL (t), 0);
4268 /* Now fix up any virtual base class types that we left lying
4269 around. We must get these done before we try to lay out the
4270 virtual function table. */
4272 if (n_baseclasses)
4273 /* layout_basetypes will remove the base subobject fields. */
4274 max_has_virtual = layout_basetypes (t, max_has_virtual);
4276 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
4278 tree vbases;
4280 vbases = CLASSTYPE_VBASECLASSES (t);
4283 /* Now fixup overrides of all functions in vtables from all
4284 direct or indirect virtual base classes. */
4285 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
4286 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
4288 for (i = 0; i < n_baseclasses; i++)
4290 tree base_binfo = TREE_VEC_ELT (binfos, i);
4291 tree basetype = BINFO_TYPE (base_binfo);
4292 tree vbases;
4294 vbases = CLASSTYPE_VBASECLASSES (basetype);
4295 while (vbases)
4297 merge_overrides (binfo_member (BINFO_TYPE (vbases),
4298 CLASSTYPE_VBASECLASSES (t)),
4299 vbases, 1, t);
4300 vbases = TREE_CHAIN (vbases);
4306 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
4307 might need to know it for setting up the offsets in the vtable
4308 (or in thunks) below. */
4309 vfield = TYPE_VFIELD (t);
4310 if (vfield != NULL_TREE
4311 && DECL_FIELD_CONTEXT (vfield) != t)
4313 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4314 tree offset = BINFO_OFFSET (binfo);
4316 vfield = copy_node (vfield);
4317 copy_lang_decl (vfield);
4319 if (! integer_zerop (offset))
4320 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4321 DECL_FIELD_CONTEXT (vfield) = t;
4322 DECL_CLASS_CONTEXT (vfield) = t;
4323 DECL_FIELD_BITPOS (vfield)
4324 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4325 TYPE_VFIELD (t) = vfield;
4328 if (has_virtual > max_has_virtual)
4329 max_has_virtual = has_virtual;
4330 if (max_has_virtual > 0)
4331 TYPE_POLYMORPHIC_P (t) = 1;
4333 if (flag_rtti && TYPE_POLYMORPHIC_P (t) && !pending_hard_virtuals)
4334 modify_all_vtables (t, NULL_TREE);
4336 for (pending_hard_virtuals = nreverse (pending_hard_virtuals);
4337 pending_hard_virtuals;
4338 pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals))
4339 modify_all_vtables (t, TREE_VALUE (pending_hard_virtuals));
4341 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
4343 tree vbases;
4344 /* Now fixup any virtual function entries from virtual bases
4345 that have different deltas. This has to come after we do the
4346 pending hard virtuals, as we might have a function that comes
4347 from multiple virtual base instances that is only overridden
4348 by a hard virtual above. */
4349 vbases = CLASSTYPE_VBASECLASSES (t);
4350 while (vbases)
4352 /* We might be able to shorten the amount of work we do by
4353 only doing this for vtables that come from virtual bases
4354 that have differing offsets, but don't want to miss any
4355 entries. */
4356 fixup_vtable_deltas (vbases, 1, t);
4357 vbases = TREE_CHAIN (vbases);
4361 /* Under our model of GC, every C++ class gets its own virtual
4362 function table, at least virtually. */
4363 if (pending_virtuals)
4365 pending_virtuals = nreverse (pending_virtuals);
4366 /* We must enter these virtuals into the table. */
4367 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4369 if (! CLASSTYPE_COM_INTERFACE (t))
4371 /* The second slot is for the tdesc pointer when thunks are used. */
4372 if (flag_vtable_thunks)
4373 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4375 /* The first slot is for the rtti offset. */
4376 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4378 set_rtti_entry (pending_virtuals,
4379 convert (ssizetype, integer_zero_node), t);
4381 build_vtable (NULL_TREE, t);
4383 else
4385 /* Here we know enough to change the type of our virtual
4386 function table, but we will wait until later this function. */
4388 if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
4389 build_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
4392 /* If this type has basetypes with constructors, then those
4393 constructors might clobber the virtual function table. But
4394 they don't if the derived class shares the exact vtable of the base
4395 class. */
4397 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4399 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4401 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
4403 /* This class contributes nothing new to the virtual function
4404 table. However, it may have declared functions which
4405 went into the virtual function table "inherited" from the
4406 base class. If so, we grab a copy of those updated functions,
4407 and pretend they are ours. */
4409 /* See if we should steal the virtual info from base class. */
4410 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
4411 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
4412 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
4413 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
4414 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
4415 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4418 if (max_has_virtual || CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4420 CLASSTYPE_VSIZE (t) = has_virtual;
4421 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4423 if (pending_virtuals)
4424 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t),
4425 pending_virtuals);
4427 else if (has_virtual)
4429 TYPE_BINFO_VIRTUALS (t) = pending_virtuals;
4430 DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1;
4434 /* Now lay out the virtual function table. */
4435 if (has_virtual)
4437 /* Use size_int so values are memoized in common cases. */
4438 tree itype = build_index_type (size_int (has_virtual));
4439 tree atype = build_cplus_array_type (vtable_entry_type, itype);
4441 layout_type (atype);
4443 /* We may have to grow the vtable. */
4444 if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
4446 TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
4447 DECL_SIZE (TYPE_BINFO_VTABLE (t)) = 0;
4448 layout_decl (TYPE_BINFO_VTABLE (t), 0);
4449 /* At one time the vtable info was grabbed 2 words at a time. This
4450 fails on sparc unless you have 8-byte alignment. (tiemann) */
4451 DECL_ALIGN (TYPE_BINFO_VTABLE (t))
4452 = MAX (TYPE_ALIGN (double_type_node),
4453 DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
4457 /* If we created a new vtbl pointer for this class, add it to the
4458 list. */
4459 if (TYPE_VFIELD (t) && CLASSTYPE_VFIELD_PARENT (t) == -1)
4460 CLASSTYPE_VFIELDS (t)
4461 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
4463 finish_struct_bits (t, max_has_virtual);
4465 /* Complete the rtl for any static member objects of the type we're
4466 working on. */
4467 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
4469 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4470 && TREE_TYPE (x) == t)
4472 DECL_MODE (x) = TYPE_MODE (t);
4473 make_decl_rtl (x, NULL, 0);
4477 /* Done with FIELDS...now decide whether to sort these for
4478 faster lookups later.
4480 The C front-end only does this when n_fields > 15. We use
4481 a smaller number because most searches fail (succeeding
4482 ultimately as the search bores through the inheritance
4483 hierarchy), and we want this failure to occur quickly. */
4485 n_fields = count_fields (TYPE_FIELDS (t));
4486 if (n_fields > 7)
4488 tree field_vec = make_tree_vec (n_fields);
4489 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
4490 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
4491 (int (*)(const void *, const void *))field_decl_cmp);
4492 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
4493 retrofit_lang_decl (TYPE_MAIN_DECL (t));
4494 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
4497 if (TYPE_HAS_CONSTRUCTOR (t))
4499 tree vfields = CLASSTYPE_VFIELDS (t);
4501 while (vfields)
4503 /* Mark the fact that constructor for T
4504 could affect anybody inheriting from T
4505 who wants to initialize vtables for VFIELDS's type. */
4506 if (VF_DERIVED_VALUE (vfields))
4507 TREE_ADDRESSABLE (vfields) = 1;
4508 vfields = TREE_CHAIN (vfields);
4512 /* Write out inline function definitions. */
4513 do_inline_function_hair (t, inline_friends);
4515 if (CLASSTYPE_VSIZE (t) != 0)
4517 /* In addition to this one, all the other vfields should be listed. */
4518 /* Before that can be done, we have to have FIELD_DECLs for them, and
4519 a place to find them. */
4520 TYPE_NONCOPIED_PARTS (t)
4521 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
4522 TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
4524 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
4525 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
4526 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
4530 /* Make the rtl for any new vtables we have created, and unmark
4531 the base types we marked. */
4532 finish_vtbls (TYPE_BINFO (t), 1, t);
4533 hack_incomplete_structures (t);
4535 if (warn_overloaded_virtual)
4536 warn_hidden (t);
4538 maybe_suppress_debug_info (t);
4540 /* Finish debugging output for this type. */
4541 rest_of_type_compilation (t, toplevel_bindings_p ());
4544 /* When T was built up, the member declarations were added in reverse
4545 order. Rearrange them to declaration order. */
4547 void
4548 unreverse_member_declarations (t)
4549 tree t;
4551 tree next;
4552 tree prev;
4553 tree x;
4555 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
4556 reverse order. Put them in declaration order now. */
4557 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
4558 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
4560 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
4561 reverse order, so we can't just use nreverse. */
4562 prev = NULL_TREE;
4563 for (x = TYPE_FIELDS (t);
4564 x && TREE_CODE (x) != TYPE_DECL;
4565 x = next)
4567 next = TREE_CHAIN (x);
4568 TREE_CHAIN (x) = prev;
4569 prev = x;
4571 if (prev)
4573 TREE_CHAIN (TYPE_FIELDS (t)) = x;
4574 if (prev)
4575 TYPE_FIELDS (t) = prev;
4579 tree
4580 finish_struct (t, attributes)
4581 tree t, attributes;
4583 /* Now that we've got all the field declarations, reverse everything
4584 as necessary. */
4585 unreverse_member_declarations (t);
4587 cplus_decl_attributes (t, attributes, NULL_TREE);
4589 if (processing_template_decl)
4591 finish_struct_methods (t);
4592 TYPE_SIZE (t) = integer_zero_node;
4594 else
4595 finish_struct_1 (t);
4597 TYPE_BEING_DEFINED (t) = 0;
4599 if (current_class_type)
4600 popclass ();
4601 else
4602 error ("trying to finish struct, but kicked out due to previous parse errors.");
4604 if (processing_template_decl)
4606 tree scope = current_scope ();
4607 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
4608 add_tree (build_min (TAG_DEFN, t));
4611 return t;
4614 /* Return the dynamic type of INSTANCE, if known.
4615 Used to determine whether the virtual function table is needed
4616 or not.
4618 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4619 of our knowledge of its type. */
4621 static tree
4622 fixed_type_or_null (instance, nonnull)
4623 tree instance;
4624 int *nonnull;
4626 if (nonnull)
4627 *nonnull = 0;
4629 switch (TREE_CODE (instance))
4631 case INDIRECT_REF:
4632 /* Check that we are not going through a cast of some sort. */
4633 if (TREE_TYPE (instance)
4634 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
4635 instance = TREE_OPERAND (instance, 0);
4636 /* fall through... */
4637 case CALL_EXPR:
4638 /* This is a call to a constructor, hence it's never zero. */
4639 if (TREE_HAS_CONSTRUCTOR (instance))
4641 if (nonnull)
4642 *nonnull = 1;
4643 return TREE_TYPE (instance);
4645 return NULL_TREE;
4647 case SAVE_EXPR:
4648 /* This is a call to a constructor, hence it's never zero. */
4649 if (TREE_HAS_CONSTRUCTOR (instance))
4651 if (nonnull)
4652 *nonnull = 1;
4653 return TREE_TYPE (instance);
4655 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4657 case RTL_EXPR:
4658 return NULL_TREE;
4660 case PLUS_EXPR:
4661 case MINUS_EXPR:
4662 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
4663 /* Propagate nonnull. */
4664 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4665 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
4666 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4667 return NULL_TREE;
4669 case NOP_EXPR:
4670 case CONVERT_EXPR:
4671 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4673 case ADDR_EXPR:
4674 if (nonnull)
4675 *nonnull = 1;
4676 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4678 case COMPONENT_REF:
4679 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
4681 case VAR_DECL:
4682 case FIELD_DECL:
4683 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
4684 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
4686 if (nonnull)
4687 *nonnull = 1;
4688 return TREE_TYPE (TREE_TYPE (instance));
4690 /* fall through... */
4691 case TARGET_EXPR:
4692 case PARM_DECL:
4693 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
4695 if (nonnull)
4696 *nonnull = 1;
4697 return TREE_TYPE (instance);
4699 else if (nonnull)
4701 if (instance == current_class_ptr
4702 && flag_this_is_variable <= 0)
4704 /* Normally, 'this' must be non-null. */
4705 if (flag_this_is_variable == 0)
4706 *nonnull = 1;
4708 /* <0 means we're in a constructor and we know our type. */
4709 if (flag_this_is_variable < 0)
4710 return TREE_TYPE (TREE_TYPE (instance));
4712 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4713 /* Reference variables should be references to objects. */
4714 *nonnull = 1;
4716 return NULL_TREE;
4718 default:
4719 return NULL_TREE;
4723 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
4724 to the static type. We also handle the case where INSTANCE is really
4725 a pointer.
4727 Used to determine whether the virtual function table is needed
4728 or not.
4730 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4731 of our knowledge of its type. */
4734 resolves_to_fixed_type_p (instance, nonnull)
4735 tree instance;
4736 int *nonnull;
4738 tree t = TREE_TYPE (instance);
4739 tree fixed = fixed_type_or_null (instance, nonnull);
4740 if (fixed == NULL_TREE)
4741 return 0;
4742 if (POINTER_TYPE_P (t))
4743 t = TREE_TYPE (t);
4744 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
4748 void
4749 init_class_processing ()
4751 current_class_depth = 0;
4752 current_class_stack_size = 10;
4753 current_class_stack
4754 = (class_stack_node_t) xmalloc (current_class_stack_size
4755 * sizeof (struct class_stack_node));
4757 access_default_node = build_int_2 (0, 0);
4758 access_public_node = build_int_2 (1, 0);
4759 access_protected_node = build_int_2 (2, 0);
4760 access_private_node = build_int_2 (3, 0);
4761 access_default_virtual_node = build_int_2 (4, 0);
4762 access_public_virtual_node = build_int_2 (5, 0);
4763 access_protected_virtual_node = build_int_2 (6, 0);
4764 access_private_virtual_node = build_int_2 (7, 0);
4767 /* Set current scope to NAME. CODE tells us if this is a
4768 STRUCT, UNION, or ENUM environment.
4770 NAME may end up being NULL_TREE if this is an anonymous or
4771 late-bound struct (as in "struct { ... } foo;") */
4773 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
4774 appropriate values, found by looking up the type definition of
4775 NAME (as a CODE).
4777 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
4778 which can be seen locally to the class. They are shadowed by
4779 any subsequent local declaration (including parameter names).
4781 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
4782 which have static meaning (i.e., static members, static
4783 member functions, enum declarations, etc).
4785 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
4786 which can be seen locally to the class (as in 1), but
4787 know that we are doing this for declaration purposes
4788 (i.e. friend foo::bar (int)).
4790 So that we may avoid calls to lookup_name, we cache the _TYPE
4791 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
4793 For multiple inheritance, we perform a two-pass depth-first search
4794 of the type lattice. The first pass performs a pre-order search,
4795 marking types after the type has had its fields installed in
4796 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
4797 unmarks the marked types. If a field or member function name
4798 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
4799 that name becomes `error_mark_node'. */
4801 void
4802 pushclass (type, modify)
4803 tree type;
4804 int modify;
4806 type = TYPE_MAIN_VARIANT (type);
4808 /* Make sure there is enough room for the new entry on the stack. */
4809 if (current_class_depth + 1 >= current_class_stack_size)
4811 current_class_stack_size *= 2;
4812 current_class_stack
4813 = (class_stack_node_t) xrealloc (current_class_stack,
4814 current_class_stack_size
4815 * sizeof (struct class_stack_node));
4818 /* Insert a new entry on the class stack. */
4819 current_class_stack[current_class_depth].name = current_class_name;
4820 current_class_stack[current_class_depth].type = current_class_type;
4821 current_class_stack[current_class_depth].access = current_access_specifier;
4822 current_class_stack[current_class_depth].names_used = 0;
4823 current_class_depth++;
4825 /* Now set up the new type. */
4826 current_class_name = TYPE_NAME (type);
4827 if (TREE_CODE (current_class_name) == TYPE_DECL)
4828 current_class_name = DECL_NAME (current_class_name);
4829 current_class_type = type;
4831 /* By default, things in classes are private, while things in
4832 structures or unions are public. */
4833 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
4834 ? access_private_node
4835 : access_public_node);
4837 if (previous_class_type != NULL_TREE
4838 && (type != previous_class_type
4839 || TYPE_SIZE (previous_class_type) == NULL_TREE)
4840 && current_class_depth == 1)
4842 /* Forcibly remove any old class remnants. */
4843 invalidate_class_lookup_cache ();
4846 /* If we're about to enter a nested class, clear
4847 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
4848 if (modify && current_class_depth > 1)
4849 clear_identifier_class_values ();
4851 pushlevel_class ();
4853 #if 0
4854 if (CLASSTYPE_TEMPLATE_INFO (type))
4855 overload_template_name (type);
4856 #endif
4858 if (modify)
4860 if (type != previous_class_type || current_class_depth > 1)
4861 push_class_decls (type);
4862 else
4864 tree item;
4866 /* We are re-entering the same class we just left, so we
4867 don't have to search the whole inheritance matrix to find
4868 all the decls to bind again. Instead, we install the
4869 cached class_shadowed list, and walk through it binding
4870 names and setting up IDENTIFIER_TYPE_VALUEs. */
4871 set_class_shadows (previous_class_values);
4872 for (item = previous_class_values; item; item = TREE_CHAIN (item))
4874 tree id = TREE_PURPOSE (item);
4875 tree decl = TREE_TYPE (item);
4877 push_class_binding (id, decl);
4878 if (TREE_CODE (decl) == TYPE_DECL)
4879 set_identifier_type_value (id, TREE_TYPE (decl));
4881 unuse_fields (type);
4884 storetags (CLASSTYPE_TAGS (type));
4888 /* When we exit a toplevel class scope, we save the
4889 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
4890 reenter the class. Here, we've entered some other class, so we
4891 must invalidate our cache. */
4893 void
4894 invalidate_class_lookup_cache ()
4896 tree t;
4898 /* This code can be seen as a cache miss. When we've cached a
4899 class' scope's bindings and we can't use them, we need to reset
4900 them. This is it! */
4901 for (t = previous_class_values; t; t = TREE_CHAIN (t))
4902 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
4904 previous_class_type = NULL_TREE;
4907 /* Get out of the current class scope. If we were in a class scope
4908 previously, that is the one popped to. */
4910 void
4911 popclass ()
4913 poplevel_class ();
4914 /* Since poplevel_class does the popping of class decls nowadays,
4915 this really only frees the obstack used for these decls. */
4916 pop_class_decls ();
4918 current_class_depth--;
4919 current_class_name = current_class_stack[current_class_depth].name;
4920 current_class_type = current_class_stack[current_class_depth].type;
4921 current_access_specifier = current_class_stack[current_class_depth].access;
4922 if (current_class_stack[current_class_depth].names_used)
4923 splay_tree_delete (current_class_stack[current_class_depth].names_used);
4926 /* Returns 1 if current_class_type is either T or a nested type of T. */
4929 currently_open_class (t)
4930 tree t;
4932 int i;
4933 if (t == current_class_type)
4934 return 1;
4935 for (i = 0; i < current_class_depth; ++i)
4936 if (current_class_stack [i].type == t)
4937 return 1;
4938 return 0;
4941 /* When entering a class scope, all enclosing class scopes' names with
4942 static meaning (static variables, static functions, types and enumerators)
4943 have to be visible. This recursive function calls pushclass for all
4944 enclosing class contexts until global or a local scope is reached.
4945 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
4946 formal of the same name. */
4948 void
4949 push_nested_class (type, modify)
4950 tree type;
4951 int modify;
4953 tree context;
4955 /* A namespace might be passed in error cases, like A::B:C. */
4956 if (type == NULL_TREE || type == error_mark_node || ! IS_AGGR_TYPE (type)
4957 || TREE_CODE (type) == NAMESPACE_DECL
4958 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
4959 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
4960 return;
4962 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
4964 if (context && CLASS_TYPE_P (context))
4965 push_nested_class (context, 2);
4966 pushclass (type, modify);
4969 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
4971 void
4972 pop_nested_class ()
4974 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
4976 popclass ();
4977 if (context && CLASS_TYPE_P (context))
4978 pop_nested_class ();
4981 /* Set global variables CURRENT_LANG_NAME to appropriate value
4982 so that behavior of name-mangling machinery is correct. */
4984 void
4985 push_lang_context (name)
4986 tree name;
4988 *current_lang_stack++ = current_lang_name;
4989 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
4990 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
4992 size_t old_size = VARRAY_SIZE (current_lang_base);
4994 VARRAY_GROW (current_lang_base, old_size + 10);
4995 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
4998 if (name == lang_name_cplusplus)
5000 strict_prototype = strict_prototypes_lang_cplusplus;
5001 current_lang_name = name;
5003 else if (name == lang_name_java)
5005 strict_prototype = strict_prototypes_lang_cplusplus;
5006 current_lang_name = name;
5007 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5008 (See record_builtin_java_type in decl.c.) However, that causes
5009 incorrect debug entries if these types are actually used.
5010 So we re-enable debug output after extern "Java". */
5011 DECL_IGNORED_P (java_byte_type_node) = 0;
5012 DECL_IGNORED_P (java_short_type_node) = 0;
5013 DECL_IGNORED_P (java_int_type_node) = 0;
5014 DECL_IGNORED_P (java_long_type_node) = 0;
5015 DECL_IGNORED_P (java_float_type_node) = 0;
5016 DECL_IGNORED_P (java_double_type_node) = 0;
5017 DECL_IGNORED_P (java_char_type_node) = 0;
5018 DECL_IGNORED_P (java_boolean_type_node) = 0;
5020 else if (name == lang_name_c)
5022 strict_prototype = strict_prototypes_lang_c;
5023 current_lang_name = name;
5025 else
5026 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5029 /* Get out of the current language scope. */
5031 void
5032 pop_lang_context ()
5034 /* Clear the current entry so that garbage collector won't hold on
5035 to it. */
5036 *current_lang_stack = NULL_TREE;
5037 current_lang_name = *--current_lang_stack;
5038 if (current_lang_name == lang_name_cplusplus
5039 || current_lang_name == lang_name_java)
5040 strict_prototype = strict_prototypes_lang_cplusplus;
5041 else if (current_lang_name == lang_name_c)
5042 strict_prototype = strict_prototypes_lang_c;
5045 /* Type instantiation routines. */
5047 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5048 matches the TARGET_TYPE. If there is no satisfactory match, return
5049 error_mark_node, and issue an error message if COMPLAIN is
5050 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
5051 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5052 template arguments. */
5054 static tree
5055 resolve_address_of_overloaded_function (target_type,
5056 overload,
5057 complain,
5058 template_only,
5059 explicit_targs)
5060 tree target_type;
5061 tree overload;
5062 int complain;
5063 int template_only;
5064 tree explicit_targs;
5066 /* Here's what the standard says:
5068 [over.over]
5070 If the name is a function template, template argument deduction
5071 is done, and if the argument deduction succeeds, the deduced
5072 arguments are used to generate a single template function, which
5073 is added to the set of overloaded functions considered.
5075 Non-member functions and static member functions match targets of
5076 type "pointer-to-function" or "reference-to-function." Nonstatic
5077 member functions match targets of type "pointer-to-member
5078 function;" the function type of the pointer to member is used to
5079 select the member function from the set of overloaded member
5080 functions. If a nonstatic member function is selected, the
5081 reference to the overloaded function name is required to have the
5082 form of a pointer to member as described in 5.3.1.
5084 If more than one function is selected, any template functions in
5085 the set are eliminated if the set also contains a non-template
5086 function, and any given template function is eliminated if the
5087 set contains a second template function that is more specialized
5088 than the first according to the partial ordering rules 14.5.5.2.
5089 After such eliminations, if any, there shall remain exactly one
5090 selected function. */
5092 int is_ptrmem = 0;
5093 int is_reference = 0;
5094 /* We store the matches in a TREE_LIST rooted here. The functions
5095 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5096 interoperability with most_specialized_instantiation. */
5097 tree matches = NULL_TREE;
5098 tree fn;
5100 /* By the time we get here, we should be seeing only real
5101 pointer-to-member types, not the internal POINTER_TYPE to
5102 METHOD_TYPE representation. */
5103 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5104 && (TREE_CODE (TREE_TYPE (target_type))
5105 == METHOD_TYPE)), 0);
5107 /* Check that the TARGET_TYPE is reasonable. */
5108 if (TYPE_PTRFN_P (target_type))
5109 /* This is OK. */
5111 else if (TYPE_PTRMEMFUNC_P (target_type))
5112 /* This is OK, too. */
5113 is_ptrmem = 1;
5114 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5116 /* This is OK, too. This comes from a conversion to reference
5117 type. */
5118 target_type = build_reference_type (target_type);
5119 is_reference = 1;
5121 else
5123 if (complain)
5124 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
5125 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5126 return error_mark_node;
5129 /* If we can find a non-template function that matches, we can just
5130 use it. There's no point in generating template instantiations
5131 if we're just going to throw them out anyhow. But, of course, we
5132 can only do this when we don't *need* a template function. */
5133 if (!template_only)
5135 tree fns;
5137 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5139 tree fn = OVL_FUNCTION (fns);
5140 tree fntype;
5142 if (TREE_CODE (fn) == TEMPLATE_DECL)
5143 /* We're not looking for templates just yet. */
5144 continue;
5146 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5147 != is_ptrmem)
5148 /* We're looking for a non-static member, and this isn't
5149 one, or vice versa. */
5150 continue;
5152 /* See if there's a match. */
5153 fntype = TREE_TYPE (fn);
5154 if (is_ptrmem)
5155 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5156 else if (!is_reference)
5157 fntype = build_pointer_type (fntype);
5159 if (can_convert_arg (target_type, fntype, fn))
5160 matches = tree_cons (fn, NULL_TREE, matches);
5164 /* Now, if we've already got a match (or matches), there's no need
5165 to proceed to the template functions. But, if we don't have a
5166 match we need to look at them, too. */
5167 if (!matches)
5169 tree target_fn_type;
5170 tree target_arg_types;
5171 tree fns;
5173 if (is_ptrmem)
5174 target_fn_type
5175 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5176 else
5177 target_fn_type = TREE_TYPE (target_type);
5178 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5180 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5182 tree fn = OVL_FUNCTION (fns);
5183 tree instantiation;
5184 tree instantiation_type;
5185 tree targs;
5187 if (TREE_CODE (fn) != TEMPLATE_DECL)
5188 /* We're only looking for templates. */
5189 continue;
5191 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5192 != is_ptrmem)
5193 /* We're not looking for a non-static member, and this is
5194 one, or vice versa. */
5195 continue;
5197 /* Try to do argument deduction. */
5198 targs = make_tree_vec (DECL_NTPARMS (fn));
5199 if (fn_type_unification (fn, explicit_targs, targs,
5200 target_arg_types, NULL_TREE,
5201 DEDUCE_EXACT) != 0)
5202 /* Argument deduction failed. */
5203 continue;
5205 /* Instantiate the template. */
5206 instantiation = instantiate_template (fn, targs);
5207 if (instantiation == error_mark_node)
5208 /* Instantiation failed. */
5209 continue;
5211 /* See if there's a match. */
5212 instantiation_type = TREE_TYPE (instantiation);
5213 if (is_ptrmem)
5214 instantiation_type =
5215 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5216 else if (!is_reference)
5217 instantiation_type = build_pointer_type (instantiation_type);
5218 if (can_convert_arg (target_type, instantiation_type, instantiation))
5219 matches = tree_cons (instantiation, fn, matches);
5222 /* Now, remove all but the most specialized of the matches. */
5223 if (matches)
5225 tree match = most_specialized_instantiation (matches,
5226 explicit_targs);
5228 if (match != error_mark_node)
5229 matches = tree_cons (match, NULL_TREE, NULL_TREE);
5233 /* Now we should have exactly one function in MATCHES. */
5234 if (matches == NULL_TREE)
5236 /* There were *no* matches. */
5237 if (complain)
5239 cp_error ("no matches converting function `%D' to type `%#T'",
5240 DECL_NAME (OVL_FUNCTION (overload)),
5241 target_type);
5243 /* print_candidates expects a chain with the functions in
5244 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5245 so why be clever?). */
5246 for (; overload; overload = OVL_NEXT (overload))
5247 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
5248 matches);
5250 print_candidates (matches);
5252 return error_mark_node;
5254 else if (TREE_CHAIN (matches))
5256 /* There were too many matches. */
5258 if (complain)
5260 tree match;
5262 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
5263 DECL_NAME (OVL_FUNCTION (overload)),
5264 target_type);
5266 /* Since print_candidates expects the functions in the
5267 TREE_VALUE slot, we flip them here. */
5268 for (match = matches; match; match = TREE_CHAIN (match))
5269 TREE_VALUE (match) = TREE_PURPOSE (match);
5271 print_candidates (matches);
5274 return error_mark_node;
5277 /* Good, exactly one match. Now, convert it to the correct type. */
5278 fn = TREE_PURPOSE (matches);
5280 mark_used (fn);
5282 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
5283 return build_unary_op (ADDR_EXPR, fn, 0);
5284 else
5286 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
5287 will mark the function as addressed, but here we must do it
5288 explicitly. */
5289 mark_addressable (fn);
5291 return fn;
5295 /* This function will instantiate the type of the expression given in
5296 RHS to match the type of LHSTYPE. If errors exist, then return
5297 error_mark_node. We only complain is COMPLAIN is set. If we are
5298 not complaining, never modify rhs, as overload resolution wants to
5299 try many possible instantiations, in hopes that at least one will
5300 work.
5302 FLAGS is a bitmask, as we see at the top of the function.
5304 For non-recursive calls, LHSTYPE should be a function, pointer to
5305 function, or a pointer to member function. */
5307 tree
5308 instantiate_type (lhstype, rhs, flags)
5309 tree lhstype, rhs;
5310 int flags;
5312 int complain = (flags & 1);
5313 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
5315 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
5317 if (complain)
5318 error ("not enough type information");
5319 return error_mark_node;
5322 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
5324 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
5325 return rhs;
5326 if (complain)
5327 cp_error ("argument of type `%T' does not match `%T'",
5328 TREE_TYPE (rhs), lhstype);
5329 return error_mark_node;
5332 /* We don't overwrite rhs if it is an overloaded function.
5333 Copying it would destroy the tree link. */
5334 if (TREE_CODE (rhs) != OVERLOAD)
5335 rhs = copy_node (rhs);
5337 /* This should really only be used when attempting to distinguish
5338 what sort of a pointer to function we have. For now, any
5339 arithmetic operation which is not supported on pointers
5340 is rejected as an error. */
5342 switch (TREE_CODE (rhs))
5344 case TYPE_EXPR:
5345 case CONVERT_EXPR:
5346 case SAVE_EXPR:
5347 case CONSTRUCTOR:
5348 case BUFFER_REF:
5349 my_friendly_abort (177);
5350 return error_mark_node;
5352 case INDIRECT_REF:
5353 case ARRAY_REF:
5355 tree new_rhs;
5357 new_rhs = instantiate_type (build_pointer_type (lhstype),
5358 TREE_OPERAND (rhs, 0), flags);
5359 if (new_rhs == error_mark_node)
5360 return error_mark_node;
5362 TREE_TYPE (rhs) = lhstype;
5363 TREE_OPERAND (rhs, 0) = new_rhs;
5364 return rhs;
5367 case NOP_EXPR:
5368 rhs = copy_node (TREE_OPERAND (rhs, 0));
5369 TREE_TYPE (rhs) = unknown_type_node;
5370 return instantiate_type (lhstype, rhs, flags);
5372 case COMPONENT_REF:
5374 tree r = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5376 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype)
5377 && complain && !flag_ms_extensions)
5379 /* Note: we check this after the recursive call to avoid
5380 complaining about cases where overload resolution fails. */
5382 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
5383 tree fn = PTRMEM_CST_MEMBER (r);
5385 my_friendly_assert (TREE_CODE (r) == PTRMEM_CST, 990811);
5387 cp_pedwarn
5388 ("object-dependent reference to `%E' can only be used in a call",
5389 DECL_NAME (fn));
5390 cp_pedwarn
5391 (" to form a pointer to member function, say `&%T::%E'",
5392 t, DECL_NAME (fn));
5395 return r;
5398 case OFFSET_REF:
5399 rhs = TREE_OPERAND (rhs, 1);
5400 if (BASELINK_P (rhs))
5401 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5403 /* This can happen if we are forming a pointer-to-member for a
5404 member template. */
5405 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
5407 /* Fall through. */
5409 case TEMPLATE_ID_EXPR:
5410 return
5411 resolve_address_of_overloaded_function (lhstype,
5412 TREE_OPERAND (rhs, 0),
5413 complain,
5414 /*template_only=*/1,
5415 TREE_OPERAND (rhs, 1));
5417 case OVERLOAD:
5418 return
5419 resolve_address_of_overloaded_function (lhstype,
5420 rhs,
5421 complain,
5422 /*template_only=*/0,
5423 /*explicit_targs=*/NULL_TREE);
5425 case TREE_LIST:
5426 /* Now we should have a baselink. */
5427 my_friendly_assert (BASELINK_P (rhs), 990412);
5429 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5431 case CALL_EXPR:
5432 /* This is too hard for now. */
5433 my_friendly_abort (183);
5434 return error_mark_node;
5436 case PLUS_EXPR:
5437 case MINUS_EXPR:
5438 case COMPOUND_EXPR:
5439 TREE_OPERAND (rhs, 0)
5440 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5441 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5442 return error_mark_node;
5443 TREE_OPERAND (rhs, 1)
5444 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5445 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5446 return error_mark_node;
5448 TREE_TYPE (rhs) = lhstype;
5449 return rhs;
5451 case MULT_EXPR:
5452 case TRUNC_DIV_EXPR:
5453 case FLOOR_DIV_EXPR:
5454 case CEIL_DIV_EXPR:
5455 case ROUND_DIV_EXPR:
5456 case RDIV_EXPR:
5457 case TRUNC_MOD_EXPR:
5458 case FLOOR_MOD_EXPR:
5459 case CEIL_MOD_EXPR:
5460 case ROUND_MOD_EXPR:
5461 case FIX_ROUND_EXPR:
5462 case FIX_FLOOR_EXPR:
5463 case FIX_CEIL_EXPR:
5464 case FIX_TRUNC_EXPR:
5465 case FLOAT_EXPR:
5466 case NEGATE_EXPR:
5467 case ABS_EXPR:
5468 case MAX_EXPR:
5469 case MIN_EXPR:
5470 case FFS_EXPR:
5472 case BIT_AND_EXPR:
5473 case BIT_IOR_EXPR:
5474 case BIT_XOR_EXPR:
5475 case LSHIFT_EXPR:
5476 case RSHIFT_EXPR:
5477 case LROTATE_EXPR:
5478 case RROTATE_EXPR:
5480 case PREINCREMENT_EXPR:
5481 case PREDECREMENT_EXPR:
5482 case POSTINCREMENT_EXPR:
5483 case POSTDECREMENT_EXPR:
5484 if (complain)
5485 error ("invalid operation on uninstantiated type");
5486 return error_mark_node;
5488 case TRUTH_AND_EXPR:
5489 case TRUTH_OR_EXPR:
5490 case TRUTH_XOR_EXPR:
5491 case LT_EXPR:
5492 case LE_EXPR:
5493 case GT_EXPR:
5494 case GE_EXPR:
5495 case EQ_EXPR:
5496 case NE_EXPR:
5497 case TRUTH_ANDIF_EXPR:
5498 case TRUTH_ORIF_EXPR:
5499 case TRUTH_NOT_EXPR:
5500 if (complain)
5501 error ("not enough type information");
5502 return error_mark_node;
5504 case COND_EXPR:
5505 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
5507 if (complain)
5508 error ("not enough type information");
5509 return error_mark_node;
5511 TREE_OPERAND (rhs, 1)
5512 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5513 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5514 return error_mark_node;
5515 TREE_OPERAND (rhs, 2)
5516 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
5517 if (TREE_OPERAND (rhs, 2) == error_mark_node)
5518 return error_mark_node;
5520 TREE_TYPE (rhs) = lhstype;
5521 return rhs;
5523 case MODIFY_EXPR:
5524 TREE_OPERAND (rhs, 1)
5525 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5526 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5527 return error_mark_node;
5529 TREE_TYPE (rhs) = lhstype;
5530 return rhs;
5532 case ADDR_EXPR:
5533 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5535 case ENTRY_VALUE_EXPR:
5536 my_friendly_abort (184);
5537 return error_mark_node;
5539 case ERROR_MARK:
5540 return error_mark_node;
5542 default:
5543 my_friendly_abort (185);
5544 return error_mark_node;
5548 /* Return the name of the virtual function pointer field
5549 (as an IDENTIFIER_NODE) for the given TYPE. Note that
5550 this may have to look back through base types to find the
5551 ultimate field name. (For single inheritance, these could
5552 all be the same name. Who knows for multiple inheritance). */
5554 static tree
5555 get_vfield_name (type)
5556 tree type;
5558 tree binfo = TYPE_BINFO (type);
5559 char *buf;
5561 while (BINFO_BASETYPES (binfo)
5562 && TYPE_POLYMORPHIC_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
5563 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
5564 binfo = BINFO_BASETYPE (binfo, 0);
5566 type = BINFO_TYPE (binfo);
5567 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
5568 + TYPE_NAME_LENGTH (type) + 2);
5569 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
5570 return get_identifier (buf);
5573 void
5574 print_class_statistics ()
5576 #ifdef GATHER_STATISTICS
5577 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
5578 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
5579 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
5580 n_build_method_call, n_inner_fields_searched);
5581 if (n_vtables)
5583 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
5584 n_vtables, n_vtable_searches);
5585 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
5586 n_vtable_entries, n_vtable_elems);
5588 #endif
5591 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
5592 according to [class]:
5593 The class-name is also inserted
5594 into the scope of the class itself. For purposes of access checking,
5595 the inserted class name is treated as if it were a public member name. */
5597 void
5598 build_self_reference ()
5600 tree name = constructor_name (current_class_type);
5601 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
5602 tree saved_cas;
5604 DECL_NONLOCAL (value) = 1;
5605 DECL_CONTEXT (value) = current_class_type;
5606 DECL_CLASS_CONTEXT (value) = current_class_type;
5607 DECL_ARTIFICIAL (value) = 1;
5609 if (processing_template_decl)
5610 value = push_template_decl (value);
5612 saved_cas = current_access_specifier;
5613 current_access_specifier = access_public_node;
5614 finish_member_declaration (value);
5615 current_access_specifier = saved_cas;
5618 /* Returns 1 if TYPE contains only padding bytes. */
5621 is_empty_class (type)
5622 tree type;
5624 tree t;
5626 if (type == error_mark_node)
5627 return 0;
5629 if (! IS_AGGR_TYPE (type))
5630 return 0;
5632 if (flag_new_abi)
5633 return CLASSTYPE_SIZE (type) == integer_zero_node;
5635 if (TYPE_BINFO_BASETYPES (type))
5636 return 0;
5637 t = TYPE_FIELDS (type);
5638 while (t && TREE_CODE (t) != FIELD_DECL)
5639 t = TREE_CHAIN (t);
5640 return (t == NULL_TREE);
5643 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
5644 a *_TYPE node. NODE can also be a local class. */
5646 tree
5647 get_enclosing_class (type)
5648 tree type;
5650 tree node = type;
5652 while (node && TREE_CODE (node) != NAMESPACE_DECL)
5654 switch (TREE_CODE_CLASS (TREE_CODE (node)))
5656 case 'd':
5657 node = DECL_CONTEXT (node);
5658 break;
5660 case 't':
5661 if (node != type)
5662 return node;
5663 node = TYPE_CONTEXT (node);
5664 break;
5666 default:
5667 my_friendly_abort (0);
5670 return NULL_TREE;
5673 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
5676 is_base_of_enclosing_class (base, type)
5677 tree base, type;
5679 while (type)
5681 if (get_binfo (base, type, 0))
5682 return 1;
5684 type = get_enclosing_class (type);
5686 return 0;
5689 /* Note that NAME was looked up while the current class was being
5690 defined and that the result of that lookup was DECL. */
5692 void
5693 maybe_note_name_used_in_class (name, decl)
5694 tree name;
5695 tree decl;
5697 splay_tree names_used;
5699 /* If we're not defining a class, there's nothing to do. */
5700 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
5701 return;
5703 /* If there's already a binding for this NAME, then we don't have
5704 anything to worry about. */
5705 if (IDENTIFIER_CLASS_VALUE (name))
5706 return;
5708 if (!current_class_stack[current_class_depth - 1].names_used)
5709 current_class_stack[current_class_depth - 1].names_used
5710 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
5711 names_used = current_class_stack[current_class_depth - 1].names_used;
5713 splay_tree_insert (names_used,
5714 (splay_tree_key) name,
5715 (splay_tree_value) decl);
5718 /* Note that NAME was declared (as DECL) in the current class. Check
5719 to see that the declaration is legal. */
5721 void
5722 note_name_declared_in_class (name, decl)
5723 tree name;
5724 tree decl;
5726 splay_tree names_used;
5727 splay_tree_node n;
5729 /* Look to see if we ever used this name. */
5730 names_used
5731 = current_class_stack[current_class_depth - 1].names_used;
5732 if (!names_used)
5733 return;
5735 n = splay_tree_lookup (names_used, (splay_tree_key) name);
5736 if (n)
5738 /* [basic.scope.class]
5740 A name N used in a class S shall refer to the same declaration
5741 in its context and when re-evaluated in the completed scope of
5742 S. */
5743 cp_error ("declaration of `%#D'", decl);
5744 cp_error_at ("changes meaning of `%s' from `%+#D'",
5745 IDENTIFIER_POINTER (DECL_NAME (decl)),
5746 (tree) n->value);