* cp-tree.h (make_typename_type): Change prototype.
[official-gcc.git] / gcc / cp / class.c
blob612e0acdef82971d7ed3eb1727ed6be831838c88
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 92-97, 1998, 1999 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* High-level class interface. */
25 #include "config.h"
26 #include "system.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "flags.h"
30 #include "rtl.h"
31 #include "output.h"
32 #include "toplev.h"
33 #include "splay-tree.h"
34 #include "ggc.h"
35 #include "lex.h"
37 #include "obstack.h"
38 #define obstack_chunk_alloc xmalloc
39 #define obstack_chunk_free free
41 /* This is how we tell when two virtual member functions are really the
42 same. */
43 #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
45 extern void set_class_shadows PROTO ((tree));
47 /* The number of nested classes being processed. If we are not in the
48 scope of any class, this is zero. */
50 int current_class_depth;
52 /* In order to deal with nested classes, we keep a stack of classes.
53 The topmost entry is the innermost class, and is the entry at index
54 CURRENT_CLASS_DEPTH */
56 typedef struct class_stack_node {
57 /* The name of the class. */
58 tree name;
60 /* The _TYPE node for the class. */
61 tree type;
63 /* The access specifier pending for new declarations in the scope of
64 this class. */
65 tree access;
67 /* If were defining TYPE, the names used in this class. */
68 splay_tree names_used;
69 }* class_stack_node_t;
71 /* The stack itself. This is an dynamically resized array. The
72 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
73 static int current_class_stack_size;
74 static class_stack_node_t current_class_stack;
76 struct base_info;
78 static tree get_vfield_name PROTO((tree));
79 static void finish_struct_anon PROTO((tree));
80 static tree build_vbase_pointer PROTO((tree, tree));
81 static tree build_vtable_entry PROTO((tree, tree));
82 static tree get_vtable_name PROTO((tree));
83 static tree get_derived_offset PROTO((tree, tree));
84 static tree get_basefndecls PROTO((tree, tree));
85 static void set_rtti_entry PROTO((tree, tree, tree));
86 static tree build_vtable PROTO((tree, tree));
87 static void prepare_fresh_vtable PROTO((tree, tree));
88 static void fixup_vtable_deltas1 PROTO((tree, tree));
89 static void fixup_vtable_deltas PROTO((tree, int, tree));
90 static void finish_vtbls PROTO((tree, int, tree));
91 static void modify_vtable_entry PROTO((tree, tree, tree));
92 static tree get_vtable_entry_n PROTO((tree, unsigned HOST_WIDE_INT));
93 static void add_virtual_function PROTO((tree *, tree *, int *, tree, tree));
94 static tree delete_duplicate_fields_1 PROTO((tree, tree));
95 static void delete_duplicate_fields PROTO((tree));
96 static void finish_struct_bits PROTO((tree, int));
97 static int alter_access PROTO((tree, tree, tree, tree));
98 static void handle_using_decl PROTO((tree, tree, tree, tree));
99 static int overrides PROTO((tree, tree));
100 static int strictly_overrides PROTO((tree, tree));
101 static void merge_overrides PROTO((tree, tree, int, tree));
102 static void override_one_vtable PROTO((tree, tree, tree));
103 static void mark_overriders PROTO((tree, tree));
104 static void check_for_override PROTO((tree, tree));
105 static tree get_class_offset_1 PROTO((tree, tree, tree, tree, tree));
106 static tree get_class_offset PROTO((tree, tree, tree, tree));
107 static void modify_one_vtable PROTO((tree, tree, tree));
108 static void modify_all_vtables PROTO((tree, tree));
109 static void modify_all_direct_vtables PROTO((tree, int, tree, tree));
110 static void modify_all_indirect_vtables PROTO((tree, int, int, tree, tree));
111 static int finish_base_struct PROTO((tree, struct base_info *));
112 static void finish_struct_methods PROTO((tree));
113 static void maybe_warn_about_overly_private_class PROTO ((tree));
114 static int field_decl_cmp PROTO ((const tree *, const tree *));
115 static int method_name_cmp PROTO ((const tree *, const tree *));
116 static tree add_implicitly_declared_members PROTO((tree, int, int, int));
117 static tree fixed_type_or_null PROTO((tree, int *));
118 static tree resolve_address_of_overloaded_function PROTO((tree, tree, int,
119 int, tree));
120 static void build_vtable_entry_ref PROTO((tree, tree, tree));
121 static tree build_vtable_entry_for_fn PROTO((tree, tree));
122 static tree build_vtbl_initializer PROTO((tree));
123 static int count_fields PROTO((tree));
124 static int add_fields_to_vec PROTO((tree, tree, int));
126 /* Variables shared between class.c and call.c. */
128 #ifdef GATHER_STATISTICS
129 int n_vtables = 0;
130 int n_vtable_entries = 0;
131 int n_vtable_searches = 0;
132 int n_vtable_elems = 0;
133 int n_convert_harshness = 0;
134 int n_compute_conversion_costs = 0;
135 int n_build_method_call = 0;
136 int n_inner_fields_searched = 0;
137 #endif
139 /* Virtual baseclass things. */
141 static tree
142 build_vbase_pointer (exp, type)
143 tree exp, type;
145 char *name;
146 FORMAT_VBASE_NAME (name, type);
148 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
151 #if 0
152 /* Is the type of the EXPR, the complete type of the object?
153 If we are going to be wrong, we must be conservative, and return 0. */
155 static int
156 complete_type_p (expr)
157 tree expr;
159 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
160 while (1)
162 switch (TREE_CODE (expr))
164 case SAVE_EXPR:
165 case INDIRECT_REF:
166 case ADDR_EXPR:
167 case NOP_EXPR:
168 case CONVERT_EXPR:
169 expr = TREE_OPERAND (expr, 0);
170 continue;
172 case CALL_EXPR:
173 if (! TREE_HAS_CONSTRUCTOR (expr))
174 break;
175 /* fall through... */
176 case VAR_DECL:
177 case FIELD_DECL:
178 if (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
179 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr)))
180 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
181 return 1;
182 /* fall through... */
183 case TARGET_EXPR:
184 case PARM_DECL:
185 if (IS_AGGR_TYPE (TREE_TYPE (expr))
186 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
187 return 1;
188 /* fall through... */
189 case PLUS_EXPR:
190 default:
191 break;
193 break;
195 return 0;
197 #endif
199 /* Build multi-level access to EXPR using hierarchy path PATH.
200 CODE is PLUS_EXPR if we are going with the grain,
201 and MINUS_EXPR if we are not (in which case, we cannot traverse
202 virtual baseclass links).
204 TYPE is the type we want this path to have on exit.
206 NONNULL is non-zero if we know (for any reason) that EXPR is
207 not, in fact, zero. */
209 tree
210 build_vbase_path (code, type, expr, path, nonnull)
211 enum tree_code code;
212 tree type, expr, path;
213 int nonnull;
215 register int changed = 0;
216 tree last = NULL_TREE, last_virtual = NULL_TREE;
217 int fixed_type_p;
218 tree null_expr = 0, nonnull_expr;
219 tree basetype;
220 tree offset = integer_zero_node;
222 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
223 return build1 (NOP_EXPR, type, expr);
225 /* We could do better if we had additional logic to convert back to the
226 unconverted type (the static type of the complete object), and then
227 convert back to the type we want. Until that is done, we only optimize
228 if the complete type is the same type as expr has. */
229 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
231 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
232 expr = save_expr (expr);
233 nonnull_expr = expr;
235 if (BINFO_INHERITANCE_CHAIN (path))
236 path = reverse_path (path);
238 basetype = BINFO_TYPE (path);
240 while (path)
242 if (TREE_VIA_VIRTUAL (path))
244 last_virtual = BINFO_TYPE (path);
245 if (code == PLUS_EXPR)
247 changed = ! fixed_type_p;
249 if (changed)
251 tree ind;
253 /* We already check for ambiguous things in the caller, just
254 find a path. */
255 if (last)
257 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
258 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
260 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
261 nonnull_expr = build_vbase_pointer (ind, last_virtual);
262 if (nonnull == 0
263 && TREE_CODE (type) == POINTER_TYPE
264 && null_expr == NULL_TREE)
266 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
267 expr = build (COND_EXPR, build_pointer_type (last_virtual),
268 build (EQ_EXPR, boolean_type_node, expr,
269 integer_zero_node),
270 null_expr, nonnull_expr);
273 /* else we'll figure out the offset below. */
275 /* Happens in the case of parse errors. */
276 if (nonnull_expr == error_mark_node)
277 return error_mark_node;
279 else
281 cp_error ("cannot cast up from virtual baseclass `%T'",
282 last_virtual);
283 return error_mark_node;
286 last = path;
287 path = BINFO_INHERITANCE_CHAIN (path);
289 /* LAST is now the last basetype assoc on the path. */
291 /* A pointer to a virtual base member of a non-null object
292 is non-null. Therefore, we only need to test for zeroness once.
293 Make EXPR the canonical expression to deal with here. */
294 if (null_expr)
296 TREE_OPERAND (expr, 2) = nonnull_expr;
297 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
298 = TREE_TYPE (nonnull_expr);
300 else
301 expr = nonnull_expr;
303 /* If we go through any virtual base pointers, make sure that
304 casts to BASETYPE from the last virtual base class use
305 the right value for BASETYPE. */
306 if (changed)
308 tree intype = TREE_TYPE (TREE_TYPE (expr));
309 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
311 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
312 offset = BINFO_OFFSET (binfo);
315 else
317 if (last_virtual)
319 offset = BINFO_OFFSET (binfo_member (last_virtual,
320 CLASSTYPE_VBASECLASSES (basetype)));
321 offset = size_binop (PLUS_EXPR, offset, BINFO_OFFSET (last));
323 else
324 offset = BINFO_OFFSET (last);
327 if (TREE_INT_CST_LOW (offset))
329 /* Bash types to make the backend happy. */
330 offset = cp_convert (type, offset);
331 #if 0
332 /* This shouldn't be necessary. (mrs) */
333 expr = build1 (NOP_EXPR, type, expr);
334 #endif
336 /* If expr might be 0, we need to preserve that zeroness. */
337 if (nonnull == 0)
339 if (null_expr)
340 TREE_TYPE (null_expr) = type;
341 else
342 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
343 if (TREE_SIDE_EFFECTS (expr))
344 expr = save_expr (expr);
346 return build (COND_EXPR, type,
347 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
348 null_expr,
349 build (code, type, expr, offset));
351 else return build (code, type, expr, offset);
354 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
355 be used multiple times in initialization of multiple inheritance. */
356 if (null_expr)
358 TREE_TYPE (expr) = type;
359 return expr;
361 else
362 return build1 (NOP_EXPR, type, expr);
365 /* Virtual function things. */
367 /* Build an entry in the virtual function table.
368 DELTA is the offset for the `this' pointer.
369 PFN is an ADDR_EXPR containing a pointer to the virtual function.
370 Note that the index (DELTA2) in the virtual function table
371 is always 0. */
373 static tree
374 build_vtable_entry (delta, pfn)
375 tree delta, pfn;
377 if (flag_vtable_thunks)
379 HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta);
380 if (idelta && ! DECL_ABSTRACT_VIRTUAL_P (TREE_OPERAND (pfn, 0)))
382 pfn = build1 (ADDR_EXPR, vtable_entry_type,
383 make_thunk (pfn, idelta));
384 TREE_READONLY (pfn) = 1;
385 TREE_CONSTANT (pfn) = 1;
387 #ifdef GATHER_STATISTICS
388 n_vtable_entries += 1;
389 #endif
390 return pfn;
392 else
394 extern int flag_huge_objects;
395 tree elems = tree_cons (NULL_TREE, delta,
396 tree_cons (NULL_TREE, integer_zero_node,
397 build_expr_list (NULL_TREE, pfn)));
398 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
400 /* DELTA used to be constructed by `size_int' and/or size_binop,
401 which caused overflow problems when it was negative. That should
402 be fixed now. */
404 if (! int_fits_type_p (delta, delta_type_node))
406 if (flag_huge_objects)
407 sorry ("object size exceeds built-in limit for virtual function table implementation");
408 else
409 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
412 TREE_CONSTANT (entry) = 1;
413 TREE_STATIC (entry) = 1;
414 TREE_READONLY (entry) = 1;
416 #ifdef GATHER_STATISTICS
417 n_vtable_entries += 1;
418 #endif
420 return entry;
424 /* Build a vtable entry for FNDECL. DELTA is the amount by which we
425 must adjust the this pointer when calling F. */
427 static tree
428 build_vtable_entry_for_fn (delta, fndecl)
429 tree delta;
430 tree fndecl;
432 tree pfn;
434 /* Take the address of the function, considering it to be of an
435 appropriate generic type. */
436 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
437 /* The address of a function can't change. */
438 TREE_CONSTANT (pfn) = 1;
439 /* Now build the vtable entry itself. */
440 return build_vtable_entry (delta, pfn);
443 /* We want to give the assembler the vtable identifier as well as
444 the offset to the function pointer. So we generate
446 __asm__ __volatile__ (".vtable_entry %c0, %c1"
447 : : "s"(&class_vtable),
448 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
450 static void
451 build_vtable_entry_ref (basetype, vtbl, idx)
452 tree basetype, vtbl, idx;
454 static char asm_stmt[] = ".vtable_entry %c0, %c1";
455 tree s, i, i2;
457 s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
458 s = build_tree_list (build_string (1, "s"), s);
460 i = build_array_ref (vtbl, idx);
461 if (!flag_vtable_thunks)
462 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
463 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
464 i2 = build_array_ref (vtbl, build_int_2(0,0));
465 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
466 i = build_binary_op (MINUS_EXPR, i, i2);
467 i = build_tree_list (build_string (1, "i"), i);
469 finish_asm_stmt (ridpointers[RID_VOLATILE],
470 build_string (sizeof(asm_stmt)-1, asm_stmt),
471 NULL_TREE, chainon (s, i), NULL_TREE);
474 /* Given an object INSTANCE, return an expression which yields the
475 virtual function vtable element corresponding to INDEX. There are
476 many special cases for INSTANCE which we take care of here, mainly
477 to avoid creating extra tree nodes when we don't have to. */
479 tree
480 build_vtbl_ref (instance, idx)
481 tree instance, idx;
483 tree vtbl, aref;
484 tree basetype = TREE_TYPE (instance);
486 if (TREE_CODE (basetype) == REFERENCE_TYPE)
487 basetype = TREE_TYPE (basetype);
489 if (instance == current_class_ref)
490 vtbl = build_vfield_ref (instance, basetype);
491 else
493 if (optimize)
495 /* Try to figure out what a reference refers to, and
496 access its virtual function table directly. */
497 tree ref = NULL_TREE;
499 if (TREE_CODE (instance) == INDIRECT_REF
500 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
501 ref = TREE_OPERAND (instance, 0);
502 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
503 ref = instance;
505 if (ref && TREE_CODE (ref) == VAR_DECL
506 && DECL_INITIAL (ref))
508 tree init = DECL_INITIAL (ref);
510 while (TREE_CODE (init) == NOP_EXPR
511 || TREE_CODE (init) == NON_LVALUE_EXPR)
512 init = TREE_OPERAND (init, 0);
513 if (TREE_CODE (init) == ADDR_EXPR)
515 init = TREE_OPERAND (init, 0);
516 if (IS_AGGR_TYPE (TREE_TYPE (init))
517 && (TREE_CODE (init) == PARM_DECL
518 || TREE_CODE (init) == VAR_DECL))
519 instance = init;
524 if (IS_AGGR_TYPE (TREE_TYPE (instance))
525 && (TREE_CODE (instance) == RESULT_DECL
526 || TREE_CODE (instance) == PARM_DECL
527 || TREE_CODE (instance) == VAR_DECL))
528 vtbl = TYPE_BINFO_VTABLE (basetype);
529 else
530 vtbl = build_vfield_ref (instance, basetype);
533 assemble_external (vtbl);
535 if (flag_vtable_gc)
536 build_vtable_entry_ref (basetype, vtbl, idx);
538 aref = build_array_ref (vtbl, idx);
540 return aref;
543 /* Given an object INSTANCE, return an expression which yields the
544 virtual function corresponding to INDEX. There are many special
545 cases for INSTANCE which we take care of here, mainly to avoid
546 creating extra tree nodes when we don't have to. */
548 tree
549 build_vfn_ref (ptr_to_instptr, instance, idx)
550 tree *ptr_to_instptr, instance;
551 tree idx;
553 tree aref = build_vtbl_ref (instance, idx);
555 /* When using thunks, there is no extra delta, and we get the pfn
556 directly. */
557 if (flag_vtable_thunks)
558 return aref;
560 if (ptr_to_instptr)
562 /* Save the intermediate result in a SAVE_EXPR so we don't have to
563 compute each component of the virtual function pointer twice. */
564 if (TREE_CODE (aref) == INDIRECT_REF)
565 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
567 *ptr_to_instptr
568 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
569 *ptr_to_instptr,
570 cp_convert (ptrdiff_type_node,
571 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
574 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
577 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
578 for the given TYPE. */
580 static tree
581 get_vtable_name (type)
582 tree type;
584 tree type_id = build_typename_overload (type);
585 char *buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
586 + IDENTIFIER_LENGTH (type_id) + 2);
587 const char *ptr = IDENTIFIER_POINTER (type_id);
588 int i;
589 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
590 #if 0
591 /* We don't take off the numbers; prepare_fresh_vtable uses the
592 DECL_ASSEMBLER_NAME for the type, which includes the number
593 in `3foo'. If we were to pull them off here, we'd end up with
594 something like `_vt.foo.3bar', instead of a uniform definition. */
595 while (ptr[i] >= '0' && ptr[i] <= '9')
596 i += 1;
597 #endif
598 sprintf (buf, VTABLE_NAME_FORMAT, ptr+i);
599 return get_identifier (buf);
602 /* Return the offset to the main vtable for a given base BINFO. */
604 tree
605 get_vfield_offset (binfo)
606 tree binfo;
608 tree tmp
609 = size_binop (FLOOR_DIV_EXPR,
610 DECL_FIELD_BITPOS (TYPE_VFIELD (BINFO_TYPE (binfo))),
611 size_int (BITS_PER_UNIT));
612 tmp = convert (sizetype, tmp);
613 return size_binop (PLUS_EXPR, tmp, BINFO_OFFSET (binfo));
616 /* Get the offset to the start of the original binfo that we derived
617 this binfo from. If we find TYPE first, return the offset only
618 that far. The shortened search is useful because the this pointer
619 on method calling is expected to point to a DECL_CONTEXT (fndecl)
620 object, and not a baseclass of it. */
622 static tree
623 get_derived_offset (binfo, type)
624 tree binfo, type;
626 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
627 tree offset2;
628 int i;
629 while (BINFO_BASETYPES (binfo)
630 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
632 tree binfos = BINFO_BASETYPES (binfo);
633 if (BINFO_TYPE (binfo) == type)
634 break;
635 binfo = TREE_VEC_ELT (binfos, i);
637 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
638 return size_binop (MINUS_EXPR, offset1, offset2);
641 /* Update the rtti info for this class. */
643 static void
644 set_rtti_entry (virtuals, offset, type)
645 tree virtuals, offset, type;
647 tree fn;
649 if (CLASSTYPE_COM_INTERFACE (type))
650 return;
652 if (flag_rtti)
653 fn = get_tinfo_fn (type);
654 else
655 /* If someone tries to get RTTI information for a type compiled
656 without RTTI, they're out of luck. By calling __pure_virtual
657 in this case, we give a small clue as to what went wrong. We
658 could consider having a __no_typeinfo function as well, for a
659 more specific hint. */
660 fn = abort_fndecl;
662 if (flag_vtable_thunks)
664 /* The first slot holds the offset. */
665 TREE_PURPOSE (virtuals) = offset;
667 /* The next node holds the function. */
668 virtuals = TREE_CHAIN (virtuals);
669 offset = integer_zero_node;
672 /* This slot holds the function to call. */
673 TREE_PURPOSE (virtuals) = offset;
674 TREE_VALUE (virtuals) = fn;
677 /* Build a virtual function for type TYPE.
678 If BINFO is non-NULL, build the vtable starting with the initial
679 approximation that it is the same as the one which is the head of
680 the association list. */
682 static tree
683 build_vtable (binfo, type)
684 tree binfo, type;
686 tree name = get_vtable_name (type);
687 tree virtuals, decl;
689 if (binfo)
691 tree offset;
693 virtuals = copy_list (BINFO_VIRTUALS (binfo));
694 decl = build_lang_decl (VAR_DECL, name,
695 TREE_TYPE (BINFO_VTABLE (binfo)));
697 /* Now do rtti stuff. */
698 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
699 offset = ssize_binop (MINUS_EXPR, integer_zero_node, offset);
700 set_rtti_entry (virtuals, offset, type);
702 else
704 virtuals = NULL_TREE;
705 decl = build_lang_decl (VAR_DECL, name, void_type_node);
708 #ifdef GATHER_STATISTICS
709 n_vtables += 1;
710 n_vtable_elems += list_length (virtuals);
711 #endif
713 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
714 import_export_vtable (decl, type, 0);
716 decl = pushdecl_top_level (decl);
717 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
718 /* Initialize the association list for this type, based
719 on our first approximation. */
720 TYPE_BINFO_VTABLE (type) = decl;
721 TYPE_BINFO_VIRTUALS (type) = virtuals;
723 DECL_ARTIFICIAL (decl) = 1;
724 TREE_STATIC (decl) = 1;
725 #ifndef WRITABLE_VTABLES
726 /* Make them READONLY by default. (mrs) */
727 TREE_READONLY (decl) = 1;
728 #endif
729 /* At one time the vtable info was grabbed 2 words at a time. This
730 fails on sparc unless you have 8-byte alignment. (tiemann) */
731 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
732 DECL_ALIGN (decl));
734 DECL_VIRTUAL_P (decl) = 1;
735 DECL_CONTEXT (decl) = type;
737 binfo = TYPE_BINFO (type);
738 SET_BINFO_NEW_VTABLE_MARKED (binfo);
739 return decl;
742 /* Give TYPE a new virtual function table which is initialized
743 with a skeleton-copy of its original initialization. The only
744 entry that changes is the `delta' entry, so we can really
745 share a lot of structure.
747 FOR_TYPE is the derived type which caused this table to
748 be needed.
750 BINFO is the type association which provided TYPE for FOR_TYPE.
752 The order in which vtables are built (by calling this function) for
753 an object must remain the same, otherwise a binary incompatibility
754 can result. */
756 static void
757 prepare_fresh_vtable (binfo, for_type)
758 tree binfo, for_type;
760 tree basetype;
761 tree orig_decl = BINFO_VTABLE (binfo);
762 tree name;
763 tree new_decl;
764 tree offset;
765 tree path = binfo;
766 char *buf, *buf2;
767 char joiner = '_';
768 int i;
770 #ifdef JOINER
771 joiner = JOINER;
772 #endif
774 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
776 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
777 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
779 /* We know that the vtable that we are going to create doesn't exist
780 yet in the global namespace, and when we finish, it will be
781 pushed into the global namespace. In complex MI hierarchies, we
782 have to loop while the name we are thinking of adding is globally
783 defined, adding more name components to the vtable name as we
784 loop, until the name is unique. This is because in complex MI
785 cases, we might have the same base more than once. This means
786 that the order in which this function is called for vtables must
787 remain the same, otherwise binary compatibility can be
788 compromised. */
790 while (1)
792 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
793 + 1 + i);
794 char *new_buf2;
796 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
797 buf2);
798 buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT) + strlen (buf1) + 1);
799 sprintf (buf, VTABLE_NAME_FORMAT, buf1);
800 name = get_identifier (buf);
802 /* If this name doesn't clash, then we can use it, otherwise
803 we add more to the name until it is unique. */
805 if (! IDENTIFIER_GLOBAL_VALUE (name))
806 break;
808 /* Set values for next loop through, if the name isn't unique. */
810 path = BINFO_INHERITANCE_CHAIN (path);
812 /* We better not run out of stuff to make it unique. */
813 my_friendly_assert (path != NULL_TREE, 368);
815 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
817 if (for_type == basetype)
819 /* If we run out of basetypes in the path, we have already
820 found created a vtable with that name before, we now
821 resort to tacking on _%d to distinguish them. */
822 int j = 2;
823 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
824 buf1 = (char *) alloca (i);
825 do {
826 sprintf (buf1, "%s%c%s%c%d",
827 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
828 buf2, joiner, j);
829 buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
830 + strlen (buf1) + 1);
831 sprintf (buf, VTABLE_NAME_FORMAT, buf1);
832 name = get_identifier (buf);
834 /* If this name doesn't clash, then we can use it,
835 otherwise we add something different to the name until
836 it is unique. */
837 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
839 /* Hey, they really like MI don't they? Increase the 3
840 above to 6, and the 999 to 999999. :-) */
841 my_friendly_assert (j <= 999, 369);
843 break;
846 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
847 new_buf2 = (char *) alloca (i);
848 sprintf (new_buf2, "%s%c%s",
849 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
850 buf2 = new_buf2;
853 new_decl = build_lang_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
854 /* Remember which class this vtable is really for. */
855 DECL_CONTEXT (new_decl) = for_type;
857 DECL_ARTIFICIAL (new_decl) = 1;
858 TREE_STATIC (new_decl) = 1;
859 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
860 DECL_VIRTUAL_P (new_decl) = 1;
861 #ifndef WRITABLE_VTABLES
862 /* Make them READONLY by default. (mrs) */
863 TREE_READONLY (new_decl) = 1;
864 #endif
865 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
867 /* Make fresh virtual list, so we can smash it later. */
868 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
870 if (TREE_VIA_VIRTUAL (binfo))
872 tree binfo1 = binfo_member (BINFO_TYPE (binfo),
873 CLASSTYPE_VBASECLASSES (for_type));
875 /* XXX - This should never happen, if it does, the caller should
876 ensure that the binfo is from for_type's binfos, not from any
877 base type's. We can remove all this code after a while. */
878 if (binfo1 != binfo)
879 warning ("internal inconsistency: binfo offset error for rtti");
881 offset = BINFO_OFFSET (binfo1);
883 else
884 offset = BINFO_OFFSET (binfo);
886 set_rtti_entry (BINFO_VIRTUALS (binfo),
887 ssize_binop (MINUS_EXPR, integer_zero_node, offset),
888 for_type);
890 #ifdef GATHER_STATISTICS
891 n_vtables += 1;
892 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
893 #endif
895 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
896 import_export_vtable (new_decl, for_type, 0);
898 if (TREE_VIA_VIRTUAL (binfo))
899 my_friendly_assert (binfo == binfo_member (BINFO_TYPE (binfo),
900 CLASSTYPE_VBASECLASSES (current_class_type)),
901 170);
902 SET_BINFO_NEW_VTABLE_MARKED (binfo);
905 #if 0
906 /* Access the virtual function table entry that logically
907 contains BASE_FNDECL. VIRTUALS is the virtual function table's
908 initializer. We can run off the end, when dealing with virtual
909 destructors in MI situations, return NULL_TREE in that case. */
911 static tree
912 get_vtable_entry (virtuals, base_fndecl)
913 tree virtuals, base_fndecl;
915 unsigned HOST_WIDE_INT n = (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
916 ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
917 & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1))
918 : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
920 #ifdef GATHER_STATISTICS
921 n_vtable_searches += n;
922 #endif
924 while (n > 0 && virtuals)
926 --n;
927 virtuals = TREE_CHAIN (virtuals);
929 return virtuals;
931 #endif
933 /* Change the offset for the FNDECL entry to NEW_OFFSET. Also update
934 DECL_VINDEX (FNDECL). */
936 static void
937 modify_vtable_entry (old_entry_in_list, new_offset, fndecl)
938 tree old_entry_in_list, new_offset, fndecl;
940 tree base_fndecl = TREE_VALUE (old_entry_in_list);
942 /* Update the entry. */
943 TREE_PURPOSE (old_entry_in_list) = new_offset;
944 TREE_VALUE (old_entry_in_list) = fndecl;
946 /* Now assign virtual dispatch information, if unset. We can
947 dispatch this, through any overridden base function. */
948 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
950 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
951 DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
955 /* Access the virtual function table entry N. VIRTUALS is the virtual
956 function table's initializer. */
958 static tree
959 get_vtable_entry_n (virtuals, n)
960 tree virtuals;
961 unsigned HOST_WIDE_INT n;
963 while (n > 0)
965 --n;
966 virtuals = TREE_CHAIN (virtuals);
968 return virtuals;
971 /* Add a virtual function to all the appropriate vtables for the class
972 T. DECL_VINDEX(X) should be error_mark_node, if we want to
973 allocate a new slot in our table. If it is error_mark_node, we
974 know that no other function from another vtable is overridden by X.
975 HAS_VIRTUAL keeps track of how many virtuals there are in our main
976 vtable for the type, and we build upon the PENDING_VIRTUALS list
977 and return it. */
979 static void
980 add_virtual_function (pv, phv, has_virtual, fndecl, t)
981 tree *pv, *phv;
982 int *has_virtual;
983 tree fndecl;
984 tree t; /* Structure type. */
986 tree pending_virtuals = *pv;
987 tree pending_hard_virtuals = *phv;
989 #ifndef DUMB_USER
990 if (current_class_type == 0)
991 cp_warning ("internal problem, current_class_type is zero when adding `%D', please report",
992 fndecl);
993 if (current_class_type && t != current_class_type)
994 cp_warning ("internal problem, current_class_type differs when adding `%D', please report",
995 fndecl);
996 #endif
998 /* If the virtual function is a redefinition of a prior one,
999 figure out in which base class the new definition goes,
1000 and if necessary, make a fresh virtual function table
1001 to hold that entry. */
1002 if (DECL_VINDEX (fndecl) == error_mark_node)
1004 /* We remember that this was the base sub-object for rtti. */
1005 CLASSTYPE_RTTI (t) = t;
1007 /* If we are using thunks, use two slots at the front, one
1008 for the offset pointer, one for the tdesc pointer.
1009 For ARM-style vtables, use the same slot for both. */
1010 if (*has_virtual == 0 && ! CLASSTYPE_COM_INTERFACE (t))
1012 if (flag_vtable_thunks)
1013 *has_virtual = 2;
1014 else
1015 *has_virtual = 1;
1018 /* Build a new INT_CST for this DECL_VINDEX. */
1020 static tree index_table[256];
1021 tree idx;
1022 /* We skip a slot for the offset/tdesc entry. */
1023 int i = (*has_virtual)++;
1025 if (i >= 256 || index_table[i] == 0)
1027 idx = build_int_2 (i, 0);
1028 if (i < 256)
1029 index_table[i] = idx;
1031 else
1032 idx = index_table[i];
1034 /* Now assign virtual dispatch information. */
1035 DECL_VINDEX (fndecl) = idx;
1036 DECL_CONTEXT (fndecl) = t;
1038 /* Save the state we've computed on the PENDING_VIRTUALS list. */
1039 pending_virtuals = tree_cons (integer_zero_node,
1040 fndecl,
1041 pending_virtuals);
1043 /* Might already be INTEGER_CST if declared twice in class. We will
1044 give error later or we've already given it. */
1045 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1047 /* Need an entry in some other virtual function table.
1048 Deal with this after we have laid out our virtual base classes. */
1049 pending_hard_virtuals = temp_tree_cons (NULL_TREE,
1050 fndecl,
1051 pending_hard_virtuals);
1053 *pv = pending_virtuals;
1054 *phv = pending_hard_virtuals;
1057 extern struct obstack *current_obstack;
1059 /* Add method METHOD to class TYPE.
1061 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1062 the class type where the method should be added. */
1064 void
1065 add_method (type, fields, method)
1066 tree type, *fields, method;
1068 push_permanent_obstack ();
1070 /* Setting the DECL_CONTEXT and DECL_CLASS_CONTEXT here is probably
1071 redundant. */
1072 DECL_CONTEXT (method) = type;
1073 DECL_CLASS_CONTEXT (method) = type;
1075 if (fields && *fields)
1076 *fields = build_overload (method, *fields);
1077 else
1079 int len;
1080 int slot;
1081 tree method_vec;
1083 if (!CLASSTYPE_METHOD_VEC (type))
1084 /* Make a new method vector. We start with 8 entries. We must
1085 allocate at least two (for constructors and destructors), and
1086 we're going to end up with an assignment operator at some
1087 point as well.
1089 We could use a TREE_LIST for now, and convert it to a
1090 TREE_VEC in finish_struct, but we would probably waste more
1091 memory making the links in the list than we would by
1092 over-allocating the size of the vector here. Furthermore,
1093 we would complicate all the code that expects this to be a
1094 vector. */
1095 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1097 method_vec = CLASSTYPE_METHOD_VEC (type);
1098 len = TREE_VEC_LENGTH (method_vec);
1100 if (DECL_NAME (method) == constructor_name (type))
1101 /* A new constructor or destructor. Constructors go in
1102 slot 0; destructors go in slot 1. */
1103 slot = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
1104 else
1106 /* See if we already have an entry with this name. */
1107 for (slot = 2; slot < len; ++slot)
1108 if (!TREE_VEC_ELT (method_vec, slot)
1109 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1110 slot)))
1111 == DECL_NAME (method)))
1112 break;
1114 if (slot == len)
1116 /* We need a bigger method vector. */
1117 tree new_vec = make_tree_vec (2 * len);
1118 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1119 (PTR) &TREE_VEC_ELT (new_vec, 0),
1120 len * sizeof (tree));
1121 len = 2 * len;
1122 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1125 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1127 /* Type conversion operators have to come before
1128 ordinary methods; add_conversions depends on this to
1129 speed up looking for conversion operators. So, if
1130 necessary, we slide some of the vector elements up.
1131 In theory, this makes this algorithm O(N^2) but we
1132 don't expect many conversion operators. */
1133 for (slot = 2; slot < len; ++slot)
1135 tree fn = TREE_VEC_ELT (method_vec, slot);
1137 if (!fn)
1138 /* There are no more entries in the vector, so we
1139 can insert the new conversion operator here. */
1140 break;
1142 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1143 /* We can insert the new function right at the
1144 SLOTth position. */
1145 break;
1148 if (!TREE_VEC_ELT (method_vec, slot))
1149 /* There is nothing in the Ith slot, so we can avoid
1150 moving anything. */
1152 else
1154 /* We know the last slot in the vector is empty
1155 because we know that at this point there's room
1156 for a new function. */
1157 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1158 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1159 (len - slot - 1) * sizeof (tree));
1160 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1165 if (template_class_depth (type))
1166 /* TYPE is a template class. Don't issue any errors now; wait
1167 until instantiation time to complain. */
1169 else
1171 tree fns;
1173 /* Check to see if we've already got this method. */
1174 for (fns = TREE_VEC_ELT (method_vec, slot);
1175 fns;
1176 fns = OVL_NEXT (fns))
1178 tree fn = OVL_CURRENT (fns);
1180 if (TREE_CODE (fn) != TREE_CODE (method))
1181 continue;
1183 if (TREE_CODE (method) != TEMPLATE_DECL)
1185 /* [over.load] Member function declarations with the
1186 same name and the same parameter types cannot be
1187 overloaded if any of them is a static member
1188 function declaration. */
1189 if (DECL_STATIC_FUNCTION_P (fn)
1190 != DECL_STATIC_FUNCTION_P (method))
1192 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1193 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1195 if (! DECL_STATIC_FUNCTION_P (fn))
1196 parms1 = TREE_CHAIN (parms1);
1197 else
1198 parms2 = TREE_CHAIN (parms2);
1200 if (compparms (parms1, parms2))
1201 cp_error ("`%#D' and `%#D' cannot be overloaded",
1202 fn, method);
1205 /* Since this is an ordinary function in a
1206 non-template class, it's mangled name can be used
1207 as a unique identifier. This technique is only
1208 an optimization; we would get the same results if
1209 we just used decls_match here. */
1210 if (DECL_ASSEMBLER_NAME (fn)
1211 != DECL_ASSEMBLER_NAME (method))
1212 continue;
1214 else if (!decls_match (fn, method))
1215 continue;
1217 /* There has already been a declaration of this method
1218 or member template. */
1219 cp_error_at ("`%D' has already been declared in `%T'",
1220 method, type);
1222 /* We don't call duplicate_decls here to merge the
1223 declarations because that will confuse things if the
1224 methods have inline definitions. In particular, we
1225 will crash while processing the definitions. */
1226 return;
1230 /* Actually insert the new method. */
1231 TREE_VEC_ELT (method_vec, slot)
1232 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1234 /* Add the new binding. */
1235 if (!DECL_CONSTRUCTOR_P (method)
1236 && !DECL_DESTRUCTOR_P (method))
1237 push_class_level_binding (DECL_NAME (method),
1238 TREE_VEC_ELT (method_vec, slot));
1240 pop_obstacks ();
1243 /* Subroutines of finish_struct. */
1245 /* Look through the list of fields for this struct, deleting
1246 duplicates as we go. This must be recursive to handle
1247 anonymous unions.
1249 FIELD is the field which may not appear anywhere in FIELDS.
1250 FIELD_PTR, if non-null, is the starting point at which
1251 chained deletions may take place.
1252 The value returned is the first acceptable entry found
1253 in FIELDS.
1255 Note that anonymous fields which are not of UNION_TYPE are
1256 not duplicates, they are just anonymous fields. This happens
1257 when we have unnamed bitfields, for example. */
1259 static tree
1260 delete_duplicate_fields_1 (field, fields)
1261 tree field, fields;
1263 tree x;
1264 tree prev = 0;
1265 if (DECL_NAME (field) == 0)
1267 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1268 return fields;
1270 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1271 fields = delete_duplicate_fields_1 (x, fields);
1272 return fields;
1274 else
1276 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1278 if (DECL_NAME (x) == 0)
1280 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1281 continue;
1282 TYPE_FIELDS (TREE_TYPE (x))
1283 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1284 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1286 if (prev == 0)
1287 fields = TREE_CHAIN (fields);
1288 else
1289 TREE_CHAIN (prev) = TREE_CHAIN (x);
1292 else
1294 if (DECL_NAME (field) == DECL_NAME (x))
1296 if (TREE_CODE (field) == CONST_DECL
1297 && TREE_CODE (x) == CONST_DECL)
1298 cp_error_at ("duplicate enum value `%D'", x);
1299 else if (TREE_CODE (field) == CONST_DECL
1300 || TREE_CODE (x) == CONST_DECL)
1301 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1303 else if (DECL_DECLARES_TYPE_P (field)
1304 && DECL_DECLARES_TYPE_P (x))
1306 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1307 continue;
1308 cp_error_at ("duplicate nested type `%D'", x);
1310 else if (DECL_DECLARES_TYPE_P (field)
1311 || DECL_DECLARES_TYPE_P (x))
1313 /* Hide tag decls. */
1314 if ((TREE_CODE (field) == TYPE_DECL
1315 && DECL_ARTIFICIAL (field))
1316 || (TREE_CODE (x) == TYPE_DECL
1317 && DECL_ARTIFICIAL (x)))
1318 continue;
1319 cp_error_at ("duplicate field `%D' (as type and non-type)",
1322 else
1323 cp_error_at ("duplicate member `%D'", x);
1324 if (prev == 0)
1325 fields = TREE_CHAIN (fields);
1326 else
1327 TREE_CHAIN (prev) = TREE_CHAIN (x);
1332 return fields;
1335 static void
1336 delete_duplicate_fields (fields)
1337 tree fields;
1339 tree x;
1340 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1341 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1344 /* Change the access of FDECL to ACCESS in T. The access to FDECL is
1345 along the path given by BINFO. Return 1 if change was legit,
1346 otherwise return 0. */
1348 static int
1349 alter_access (t, binfo, fdecl, access)
1350 tree t;
1351 tree binfo;
1352 tree fdecl;
1353 tree access;
1355 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1356 if (elem)
1358 if (TREE_VALUE (elem) != access)
1360 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1361 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1362 else
1363 error ("conflicting access specifications for field `%s', ignored",
1364 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1366 else
1368 /* They're changing the access to the same thing they changed
1369 it to before. That's OK. */
1373 else
1375 enforce_access (binfo, fdecl);
1376 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1377 return 1;
1379 return 0;
1382 /* Process the USING_DECL, which is a member of T. The METHOD_VEC, if
1383 non-NULL, is the methods of T. The FIELDS are the fields of T. */
1385 static void
1386 handle_using_decl (using_decl, t, method_vec, fields)
1387 tree using_decl;
1388 tree t;
1389 tree method_vec;
1390 tree fields;
1392 tree ctype = DECL_INITIAL (using_decl);
1393 tree name = DECL_NAME (using_decl);
1394 tree access
1395 = TREE_PRIVATE (using_decl) ? access_private_node
1396 : TREE_PROTECTED (using_decl) ? access_protected_node
1397 : access_public_node;
1398 tree fdecl, binfo;
1399 tree flist = NULL_TREE;
1400 tree tmp;
1401 int i;
1402 int n_methods;
1404 binfo = binfo_or_else (ctype, t);
1405 if (! binfo)
1406 return;
1408 if (name == constructor_name (ctype)
1409 || name == constructor_name_full (ctype))
1411 cp_error_at ("using-declaration for constructor", using_decl);
1412 return;
1415 fdecl = lookup_member (binfo, name, 0, 0);
1417 if (!fdecl)
1419 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1420 return;
1423 /* Functions are represented as TREE_LIST, with the purpose
1424 being the type and the value the functions. Other members
1425 come as themselves. */
1426 if (TREE_CODE (fdecl) == TREE_LIST)
1427 /* Ignore base type this came from. */
1428 fdecl = TREE_VALUE (fdecl);
1430 if (TREE_CODE (fdecl) == OVERLOAD)
1432 /* We later iterate over all functions. */
1433 flist = fdecl;
1434 fdecl = OVL_FUNCTION (flist);
1437 name = DECL_NAME (fdecl);
1438 n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
1439 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); i++)
1440 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)))
1441 == name)
1443 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1444 cp_error_at (" because of local method `%#D' with same name",
1445 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
1446 return;
1449 if (! DECL_LANG_SPECIFIC (fdecl))
1450 /* We don't currently handle DECL_ACCESS for TYPE_DECLs; just return. */
1451 return;
1453 for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
1454 if (DECL_NAME (tmp) == name)
1456 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1457 cp_error_at (" because of local field `%#D' with same name", tmp);
1458 return;
1461 /* Make type T see field decl FDECL with access ACCESS.*/
1462 if (flist)
1464 while (flist)
1466 if (alter_access (t, binfo, OVL_FUNCTION (flist),
1467 access) == 0)
1468 return;
1469 flist = OVL_CHAIN (flist);
1472 else
1473 alter_access (t, binfo, fdecl, access);
1476 struct base_info
1478 int has_virtual;
1479 int max_has_virtual;
1480 tree vfield;
1481 tree vfields;
1482 tree rtti;
1483 char cant_have_default_ctor;
1484 char cant_have_const_ctor;
1485 char no_const_asn_ref;
1488 /* Record information about type T derived from its base classes.
1489 Store most of that information in T itself, and place the
1490 remaining information in the struct BASE_INFO.
1492 Propagate basetype offsets throughout the lattice. Note that the
1493 lattice topped by T is really a pair: it's a DAG that gives the
1494 structure of the derivation hierarchy, and it's a list of the
1495 virtual baseclasses that appear anywhere in the DAG. When a vbase
1496 type appears in the DAG, it's offset is 0, and it's children start
1497 their offsets from that point. When a vbase type appears in the list,
1498 its offset is the offset it has in the hierarchy, and its children's
1499 offsets include that offset in theirs.
1501 Returns the index of the first base class to have virtual functions,
1502 or -1 if no such base class. */
1504 static int
1505 finish_base_struct (t, b)
1506 tree t;
1507 struct base_info *b;
1509 tree binfos = TYPE_BINFO_BASETYPES (t);
1510 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1511 int first_vfn_base_index = -1;
1512 bzero ((char *) b, sizeof (struct base_info));
1514 for (i = 0; i < n_baseclasses; i++)
1516 tree base_binfo = TREE_VEC_ELT (binfos, i);
1517 tree basetype = BINFO_TYPE (base_binfo);
1519 /* Effective C++ rule 14. We only need to check TYPE_VIRTUAL_P
1520 here because the case of virtual functions but non-virtual
1521 dtor is handled in finish_struct_1. */
1522 if (warn_ecpp && ! TYPE_VIRTUAL_P (basetype)
1523 && TYPE_HAS_DESTRUCTOR (basetype))
1524 cp_warning ("base class `%#T' has a non-virtual destructor", basetype);
1526 /* If the type of basetype is incomplete, then
1527 we already complained about that fact
1528 (and we should have fixed it up as well). */
1529 if (TYPE_SIZE (basetype) == 0)
1531 int j;
1532 /* The base type is of incomplete type. It is
1533 probably best to pretend that it does not
1534 exist. */
1535 if (i == n_baseclasses-1)
1536 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1537 TREE_VEC_LENGTH (binfos) -= 1;
1538 n_baseclasses -= 1;
1539 for (j = i; j+1 < n_baseclasses; j++)
1540 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1543 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1544 b->cant_have_const_ctor = 1;
1546 if (TYPE_HAS_CONSTRUCTOR (basetype)
1547 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1549 b->cant_have_default_ctor = 1;
1550 if (! TYPE_HAS_CONSTRUCTOR (t))
1552 cp_pedwarn ("base `%T' with only non-default constructor",
1553 basetype);
1554 cp_pedwarn ("in class without a constructor");
1558 if (TYPE_HAS_ASSIGN_REF (basetype)
1559 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1560 b->no_const_asn_ref = 1;
1562 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1563 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1564 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1565 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1567 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1568 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1569 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1571 if (CLASSTYPE_COM_INTERFACE (basetype))
1573 CLASSTYPE_COM_INTERFACE (t) = 1;
1574 if (i > 0)
1575 cp_error
1576 ("COM interface type `%T' must be the leftmost base class",
1577 basetype);
1579 else if (CLASSTYPE_COM_INTERFACE (t) && i == 0)
1581 cp_error ("COM interface type `%T' with non-COM base class `%T'",
1582 t, basetype);
1583 CLASSTYPE_COM_INTERFACE (t) = 0;
1586 if (TYPE_VIRTUAL_P (basetype))
1588 /* Ensure that this is set from at least a virtual base
1589 class. */
1590 if (b->rtti == NULL_TREE)
1591 b->rtti = CLASSTYPE_RTTI (basetype);
1593 /* Don't borrow virtuals from virtual baseclasses. */
1594 if (TREE_VIA_VIRTUAL (base_binfo))
1595 continue;
1597 if (first_vfn_base_index < 0)
1599 tree vfields;
1600 first_vfn_base_index = i;
1602 /* Update these two, now that we know what vtable we are
1603 going to extend. This is so that we can add virtual
1604 functions, and override them properly. */
1605 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1606 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1607 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1608 b->vfield = TYPE_VFIELD (basetype);
1609 b->vfields = copy_list (CLASSTYPE_VFIELDS (basetype));
1610 vfields = b->vfields;
1611 while (vfields)
1613 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1614 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1616 tree value = VF_BASETYPE_VALUE (vfields);
1617 if (DECL_NAME (TYPE_VFIELD (value))
1618 == DECL_NAME (TYPE_VFIELD (basetype)))
1619 VF_NORMAL_VALUE (b->vfields) = basetype;
1620 else
1621 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1623 vfields = TREE_CHAIN (vfields);
1625 TYPE_VFIELD (t) = b->vfield;
1627 else
1629 /* Only add unique vfields, and flatten them out as we go. */
1630 tree vfields = CLASSTYPE_VFIELDS (basetype);
1631 while (vfields)
1633 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1634 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1636 tree value = VF_BASETYPE_VALUE (vfields);
1637 b->vfields = tree_cons (base_binfo, value, b->vfields);
1638 if (DECL_NAME (TYPE_VFIELD (value))
1639 == DECL_NAME (TYPE_VFIELD (basetype)))
1640 VF_NORMAL_VALUE (b->vfields) = basetype;
1641 else
1642 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1644 vfields = TREE_CHAIN (vfields);
1647 if (b->has_virtual == 0)
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 TYPE_VFIELD (t) = b->vfield;
1659 /* When we install the first one, set the VF_NORMAL_VALUE
1660 to be the current class, as this it is the most derived
1661 class. Hopefully, this is not set to something else
1662 later. (mrs) */
1663 vfields = b->vfields;
1664 while (vfields)
1666 if (DECL_NAME (TYPE_VFIELD (t))
1667 == DECL_NAME (TYPE_VFIELD (basetype)))
1669 VF_NORMAL_VALUE (vfields) = t;
1670 /* There should only be one of them! And it should
1671 always be found, if we get into here. (mrs) */
1672 break;
1674 vfields = TREE_CHAIN (vfields);
1682 tree vfields;
1683 /* Find the base class with the largest number of virtual functions. */
1684 for (vfields = b->vfields; vfields; vfields = TREE_CHAIN (vfields))
1686 if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)) > b->max_has_virtual)
1687 b->max_has_virtual = CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields));
1688 if (VF_DERIVED_VALUE (vfields)
1689 && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)) > b->max_has_virtual)
1690 b->max_has_virtual = CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields));
1694 if (b->vfield == 0)
1695 /* If all virtual functions come only from virtual baseclasses. */
1696 return -1;
1698 /* Update the rtti base if we have a non-virtual base class version
1699 of it. */
1700 b->rtti = CLASSTYPE_RTTI (BINFO_TYPE (TREE_VEC_ELT (binfos, first_vfn_base_index)));
1702 return first_vfn_base_index;
1705 /* Set memoizing fields and bits of T (and its variants) for later use.
1706 MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables. */
1708 static void
1709 finish_struct_bits (t, max_has_virtual)
1710 tree t;
1711 int max_has_virtual;
1713 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1715 /* Fix up variants (if any). */
1716 tree variants = TYPE_NEXT_VARIANT (t);
1717 while (variants)
1719 /* These fields are in the _TYPE part of the node, not in
1720 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1721 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1722 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1723 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1724 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
1726 TYPE_USES_COMPLEX_INHERITANCE (variants) = TYPE_USES_COMPLEX_INHERITANCE (t);
1727 TYPE_VIRTUAL_P (variants) = TYPE_VIRTUAL_P (t);
1728 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1729 /* Copy whatever these are holding today. */
1730 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1731 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1732 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1733 TYPE_SIZE (variants) = TYPE_SIZE (t);
1734 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1735 variants = TYPE_NEXT_VARIANT (variants);
1738 if (n_baseclasses && max_has_virtual)
1740 /* For a class w/o baseclasses, `finish_struct' has set
1741 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by definition). Similarly
1742 for a class who's base classes do not have vtables. When neither
1743 of these is true, we might have removed abstract virtuals (by
1744 providing a definition), added some (by declaring new ones), or
1745 redeclared ones from a base class. We need to recalculate what's
1746 really an abstract virtual at this point (by looking in the
1747 vtables). */
1748 CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
1751 if (n_baseclasses)
1753 /* Notice whether this class has type conversion functions defined. */
1754 tree binfo = TYPE_BINFO (t);
1755 tree binfos = BINFO_BASETYPES (binfo);
1756 tree basetype;
1758 for (i = n_baseclasses-1; i >= 0; i--)
1760 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1762 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1766 /* If this type has a copy constructor, force its mode to be BLKmode, and
1767 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1768 be passed by invisible reference and prevent it from being returned in
1769 a register.
1771 Also do this if the class has BLKmode but can still be returned in
1772 registers, since function_cannot_inline_p won't let us inline
1773 functions returning such a type. This affects the HP-PA. */
1774 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1775 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1776 && CLASSTYPE_NON_AGGREGATE (t)))
1778 tree variants;
1779 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1780 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1782 TYPE_MODE (variants) = BLKmode;
1783 TREE_ADDRESSABLE (variants) = 1;
1788 /* Issue warnings about T having private constructors, but no friends,
1789 and so forth.
1791 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1792 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1793 non-private static member functions. */
1795 static void
1796 maybe_warn_about_overly_private_class (t)
1797 tree t;
1799 int has_member_fn = 0;
1800 int has_nonprivate_method = 0;
1801 tree fn;
1803 if (!warn_ctor_dtor_privacy
1804 /* If the class has friends, those entities might create and
1805 access instances, so we should not warn. */
1806 || (CLASSTYPE_FRIEND_CLASSES (t)
1807 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1808 /* We will have warned when the template was declared; there's
1809 no need to warn on every instantiation. */
1810 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1811 /* There's no reason to even consider warning about this
1812 class. */
1813 return;
1815 /* We only issue one warning, if more than one applies, because
1816 otherwise, on code like:
1818 class A {
1819 // Oops - forgot `public:'
1820 A();
1821 A(const A&);
1822 ~A();
1825 we warn several times about essentially the same problem. */
1827 /* Check to see if all (non-constructor, non-destructor) member
1828 functions are private. (Since there are no friends or
1829 non-private statics, we can't ever call any of the private member
1830 functions.) */
1831 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1832 /* We're not interested in compiler-generated methods; they don't
1833 provide any way to call private members. */
1834 if (!DECL_ARTIFICIAL (fn))
1836 if (!TREE_PRIVATE (fn))
1838 if (DECL_STATIC_FUNCTION_P (fn))
1839 /* A non-private static member function is just like a
1840 friend; it can create and invoke private member
1841 functions, and be accessed without a class
1842 instance. */
1843 return;
1845 has_nonprivate_method = 1;
1846 break;
1848 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1849 has_member_fn = 1;
1852 if (!has_nonprivate_method && has_member_fn)
1854 /* There are no non-private methods, and there's at least one
1855 private member function that isn't a constructor or
1856 destructor. (If all the private members are
1857 constructors/destructors we want to use the code below that
1858 issues error messages specifically referring to
1859 constructors/destructors.) */
1860 int i;
1861 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1862 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1863 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1864 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1866 has_nonprivate_method = 1;
1867 break;
1869 if (!has_nonprivate_method)
1871 cp_warning ("all member functions in class `%T' are private", t);
1872 return;
1876 /* Even if some of the member functions are non-private, the class
1877 won't be useful for much if all the constructors or destructors
1878 are private: such an object can never be created or destroyed. */
1879 if (TYPE_HAS_DESTRUCTOR (t))
1881 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
1883 if (TREE_PRIVATE (dtor))
1885 cp_warning ("`%#T' only defines a private destructor and has no friends",
1887 return;
1891 if (TYPE_HAS_CONSTRUCTOR (t))
1893 int nonprivate_ctor = 0;
1895 /* If a non-template class does not define a copy
1896 constructor, one is defined for it, enabling it to avoid
1897 this warning. For a template class, this does not
1898 happen, and so we would normally get a warning on:
1900 template <class T> class C { private: C(); };
1902 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1903 complete non-template or fully instantiated classes have this
1904 flag set. */
1905 if (!TYPE_HAS_INIT_REF (t))
1906 nonprivate_ctor = 1;
1907 else
1908 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
1910 fn = OVL_NEXT (fn))
1912 tree ctor = OVL_CURRENT (fn);
1913 /* Ideally, we wouldn't count copy constructors (or, in
1914 fact, any constructor that takes an argument of the
1915 class type as a parameter) because such things cannot
1916 be used to construct an instance of the class unless
1917 you already have one. But, for now at least, we're
1918 more generous. */
1919 if (! TREE_PRIVATE (ctor))
1921 nonprivate_ctor = 1;
1922 break;
1926 if (nonprivate_ctor == 0)
1928 cp_warning ("`%#T' only defines private constructors and has no friends",
1930 return;
1935 /* Function to help qsort sort FIELD_DECLs by name order. */
1937 static int
1938 field_decl_cmp (x, y)
1939 const tree *x, *y;
1941 if (DECL_NAME (*x) == DECL_NAME (*y))
1942 return 0;
1943 if (DECL_NAME (*x) == NULL_TREE)
1944 return -1;
1945 if (DECL_NAME (*y) == NULL_TREE)
1946 return 1;
1947 if (DECL_NAME (*x) < DECL_NAME (*y))
1948 return -1;
1949 return 1;
1952 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1954 static int
1955 method_name_cmp (m1, m2)
1956 const tree *m1, *m2;
1958 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1959 return 0;
1960 if (*m1 == NULL_TREE)
1961 return -1;
1962 if (*m2 == NULL_TREE)
1963 return 1;
1964 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1965 return -1;
1966 return 1;
1969 /* Warn about duplicate methods in fn_fields. Also compact method
1970 lists so that lookup can be made faster.
1972 Data Structure: List of method lists. The outer list is a
1973 TREE_LIST, whose TREE_PURPOSE field is the field name and the
1974 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
1975 links the entire list of methods for TYPE_METHODS. Friends are
1976 chained in the same way as member functions (? TREE_CHAIN or
1977 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
1978 list. That allows them to be quickly deleted, and requires no
1979 extra storage.
1981 If there are any constructors/destructors, they are moved to the
1982 front of the list. This makes pushclass more efficient.
1984 @@ The above comment is obsolete. It mostly describes what add_method
1985 @@ and add_implicitly_declared_members do.
1987 Sort methods that are not special (i.e., constructors, destructors, and
1988 type conversion operators) so that we can find them faster in search. */
1990 static void
1991 finish_struct_methods (t)
1992 tree t;
1994 tree fn_fields;
1995 tree method_vec = CLASSTYPE_METHOD_VEC (t);
1996 tree ctor_name = constructor_name (t);
1997 int slot, len = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
1999 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2000 and the next few with type conversion operators (if any). */
2001 for (fn_fields = TYPE_METHODS (t); fn_fields;
2002 fn_fields = TREE_CHAIN (fn_fields))
2004 tree fn_name = DECL_NAME (fn_fields);
2006 /* Clear out this flag.
2008 @@ Doug may figure out how to break
2009 @@ this with nested classes and friends. */
2010 DECL_IN_AGGR_P (fn_fields) = 0;
2012 /* Note here that a copy ctor is private, so we don't dare generate
2013 a default copy constructor for a class that has a member
2014 of this type without making sure they have access to it. */
2015 if (fn_name == ctor_name)
2017 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2018 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2020 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2021 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2023 if (TREE_CHAIN (parmtypes) == NULL_TREE
2024 || TREE_CHAIN (parmtypes) == void_list_node
2025 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2027 if (TREE_PROTECTED (fn_fields))
2028 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2029 else if (TREE_PRIVATE (fn_fields))
2030 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2034 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2036 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2038 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2040 if (TREE_PROTECTED (fn_fields))
2041 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2042 else if (TREE_PRIVATE (fn_fields))
2043 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2048 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2049 /* We thought there was a destructor, but there wasn't. Some
2050 parse errors cause this anomalous situation. */
2051 TYPE_HAS_DESTRUCTOR (t) = 0;
2053 /* Issue warnings about private constructors and such. If there are
2054 no methods, then some public defaults are generated. */
2055 maybe_warn_about_overly_private_class (t);
2057 if (method_vec == NULL_TREE)
2058 return;
2060 /* Now sort the methods. */
2061 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2062 len--;
2063 TREE_VEC_LENGTH (method_vec) = len;
2065 /* The type conversion ops have to live at the front of the vec, so we
2066 can't sort them. */
2067 for (slot = 2; slot < len; ++slot)
2069 tree fn = TREE_VEC_ELT (method_vec, slot);
2071 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2072 break;
2074 if (len - slot > 1)
2075 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2076 (int (*)(const void *, const void *))method_name_cmp);
2079 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2081 void
2082 duplicate_tag_error (t)
2083 tree t;
2085 cp_error ("redefinition of `%#T'", t);
2086 cp_error_at ("previous definition here", t);
2088 /* Pretend we haven't defined this type. */
2090 /* All of the component_decl's were TREE_CHAINed together in the parser.
2091 finish_struct_methods walks these chains and assembles all methods with
2092 the same base name into DECL_CHAINs. Now we don't need the parser chains
2093 anymore, so we unravel them. */
2095 /* This used to be in finish_struct, but it turns out that the
2096 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2097 things... */
2098 if (CLASSTYPE_METHOD_VEC (t))
2100 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2101 int i, len = TREE_VEC_LENGTH (method_vec);
2102 for (i = 0; i < len; i++)
2104 tree unchain = TREE_VEC_ELT (method_vec, i);
2105 while (unchain != NULL_TREE)
2107 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2108 unchain = OVL_NEXT (unchain);
2113 if (TYPE_LANG_SPECIFIC (t))
2115 tree binfo = TYPE_BINFO (t);
2116 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2117 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2118 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2119 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2121 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2122 BINFO_BASETYPES(binfo) = NULL_TREE;
2124 TYPE_BINFO (t) = binfo;
2125 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2126 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2127 TYPE_REDEFINED (t) = 1;
2128 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2129 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2131 TYPE_SIZE (t) = NULL_TREE;
2132 TYPE_MODE (t) = VOIDmode;
2133 TYPE_FIELDS (t) = NULL_TREE;
2134 TYPE_METHODS (t) = NULL_TREE;
2135 TYPE_VFIELD (t) = NULL_TREE;
2136 TYPE_CONTEXT (t) = NULL_TREE;
2137 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2140 /* Construct the initializer for BINFOs virtual function table. */
2142 static tree
2143 build_vtbl_initializer (binfo)
2144 tree binfo;
2146 tree v = BINFO_VIRTUALS (binfo);
2147 tree inits = NULL_TREE;
2149 /* Process the RTTI stuff at the head of the list. If we're not
2150 using vtable thunks, then the RTTI entry is just an ordinary
2151 function, and we can process it just like the other virtual
2152 function entries. */
2153 if (!CLASSTYPE_COM_INTERFACE (BINFO_TYPE (binfo))
2154 && flag_vtable_thunks)
2156 tree offset;
2157 tree init;
2159 /* The first entry is an offset. */
2160 offset = TREE_PURPOSE (v);
2161 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST,
2162 19990727);
2164 /* Convert the offset to look like a function pointer, so that
2165 we can put it in the vtable. */
2166 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
2167 TREE_CONSTANT (init) = 1;
2168 init = build_vtable_entry (integer_zero_node, init);
2169 inits = tree_cons (NULL_TREE, init, inits);
2171 /* Even in this case, the second entry (the tdesc pointer) is
2172 just an ordinary function. */
2173 v = TREE_CHAIN (v);
2176 /* Go through all the ordinary virtual functions, building up
2177 initializers. */
2178 while (v)
2180 tree delta;
2181 tree fn;
2182 tree init;
2184 /* Pull the offset for `this', and the function to call, out of
2185 the list. */
2186 delta = TREE_PURPOSE (v);
2187 fn = TREE_VALUE (v);
2188 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
2189 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
2191 /* You can't call an abstract virtual function; it's abstract.
2192 So, we replace these functions with __pure_virtual. */
2193 if (DECL_ABSTRACT_VIRTUAL_P (fn))
2194 fn = abort_fndecl;
2196 /* Package up that information for the vtable. */
2197 init = build_vtable_entry_for_fn (delta, fn);
2198 /* And add it to the chain of initializers. */
2199 inits = tree_cons (NULL_TREE, init, inits);
2201 /* Keep going. */
2202 v = TREE_CHAIN (v);
2205 /* The initializers were built up in reverse order; straighten them
2206 out now. */
2207 inits = nreverse (inits);
2208 /* Package all the initializers up as an array initializer. */
2209 return build_nt (CONSTRUCTOR, NULL_TREE, inits);
2212 /* finish up all new vtables. */
2214 static void
2215 finish_vtbls (binfo, do_self, t)
2216 tree binfo;
2217 int do_self;
2218 tree t;
2220 tree binfos = BINFO_BASETYPES (binfo);
2221 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2223 /* Should we use something besides CLASSTYPE_VFIELDS? */
2224 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2226 if (BINFO_NEW_VTABLE_MARKED (binfo))
2228 tree decl, context;
2230 decl = BINFO_VTABLE (binfo);
2231 context = DECL_CONTEXT (decl);
2232 DECL_CONTEXT (decl) = 0;
2233 DECL_INITIAL (decl) = build_vtbl_initializer (binfo);
2234 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0);
2235 DECL_CONTEXT (decl) = context;
2237 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2240 for (i = 0; i < n_baselinks; i++)
2242 tree base_binfo = TREE_VEC_ELT (binfos, i);
2243 int is_not_base_vtable
2244 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2245 if (TREE_VIA_VIRTUAL (base_binfo))
2246 base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2247 CLASSTYPE_VBASECLASSES (t));
2248 finish_vtbls (base_binfo, is_not_base_vtable, t);
2252 /* True if we should override the given BASE_FNDECL with the given
2253 FNDECL. */
2255 static int
2256 overrides (fndecl, base_fndecl)
2257 tree fndecl, base_fndecl;
2259 /* Destructors have special names. */
2260 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2261 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2262 return 1;
2263 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2264 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2265 return 0;
2266 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2268 tree types, base_types;
2269 #if 0
2270 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2271 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2272 #endif
2273 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2274 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2275 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2276 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2277 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2278 return 1;
2280 return 0;
2283 static tree
2284 get_class_offset_1 (parent, binfo, context, t, fndecl)
2285 tree parent, binfo, context, t, fndecl;
2287 tree binfos = BINFO_BASETYPES (binfo);
2288 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2289 tree rval = NULL_TREE;
2291 if (binfo == parent)
2292 return error_mark_node;
2294 for (i = 0; i < n_baselinks; i++)
2296 tree base_binfo = TREE_VEC_ELT (binfos, i);
2297 tree nrval;
2299 if (TREE_VIA_VIRTUAL (base_binfo))
2300 base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2301 CLASSTYPE_VBASECLASSES (t));
2302 nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl);
2303 /* See if we have a new value */
2304 if (nrval && (nrval != error_mark_node || rval==0))
2306 /* Only compare if we have two offsets */
2307 if (rval && rval != error_mark_node
2308 && ! tree_int_cst_equal (nrval, rval))
2310 /* Only give error if the two offsets are different */
2311 error ("every virtual function must have a unique final overrider");
2312 cp_error (" found two (or more) `%T' class subobjects in `%T'", context, t);
2313 cp_error (" with virtual `%D' from virtual base class", fndecl);
2314 return rval;
2316 rval = nrval;
2319 if (rval && BINFO_TYPE (binfo) == context)
2321 my_friendly_assert (rval == error_mark_node
2322 || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999);
2323 rval = BINFO_OFFSET (binfo);
2326 return rval;
2329 /* Get the offset to the CONTEXT subobject that is related to the
2330 given BINFO. */
2332 static tree
2333 get_class_offset (context, t, binfo, fndecl)
2334 tree context, t, binfo, fndecl;
2336 tree first_binfo = binfo;
2337 tree offset;
2338 int i;
2340 if (context == t)
2341 return integer_zero_node;
2343 if (BINFO_TYPE (binfo) == context)
2344 return BINFO_OFFSET (binfo);
2346 /* Check less derived binfos first. */
2347 while (BINFO_BASETYPES (binfo)
2348 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
2350 tree binfos = BINFO_BASETYPES (binfo);
2351 binfo = TREE_VEC_ELT (binfos, i);
2352 if (BINFO_TYPE (binfo) == context)
2353 return BINFO_OFFSET (binfo);
2356 /* Ok, not found in the less derived binfos, now check the more
2357 derived binfos. */
2358 offset = get_class_offset_1 (first_binfo, TYPE_BINFO (t), context, t, fndecl);
2359 if (offset==0 || TREE_CODE (offset) != INTEGER_CST)
2360 my_friendly_abort (999); /* we have to find it. */
2361 return offset;
2364 /* Skip RTTI information at the front of the virtual list. */
2366 unsigned HOST_WIDE_INT
2367 skip_rtti_stuff (virtuals, t)
2368 tree *virtuals, t;
2370 int n;
2372 if (CLASSTYPE_COM_INTERFACE (t))
2373 return 0;
2375 n = 0;
2376 if (*virtuals)
2378 /* We always reserve a slot for the offset/tdesc entry. */
2379 ++n;
2380 *virtuals = TREE_CHAIN (*virtuals);
2382 if (flag_vtable_thunks && *virtuals)
2384 /* The second slot is reserved for the tdesc pointer when thunks
2385 are used. */
2386 ++n;
2387 *virtuals = TREE_CHAIN (*virtuals);
2389 return n;
2392 static void
2393 modify_one_vtable (binfo, t, fndecl)
2394 tree binfo, t, fndecl;
2396 tree virtuals = BINFO_VIRTUALS (binfo);
2397 unsigned HOST_WIDE_INT n;
2399 /* update rtti entry */
2400 if (flag_rtti)
2402 if (binfo == TYPE_BINFO (t))
2404 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2405 build_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))), t);
2407 else
2409 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2410 prepare_fresh_vtable (binfo, t);
2413 if (fndecl == NULL_TREE)
2414 return;
2416 n = skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo));
2418 while (virtuals)
2420 tree current_fndecl = TREE_VALUE (virtuals);
2422 /* We should never have an instance of __pure_virtual on the
2423 BINFO_VIRTUALS list. If we do, then we will never notice
2424 that the function that should have been there instead has
2425 been overridden. */
2426 my_friendly_assert (current_fndecl != abort_fndecl,
2427 19990727);
2429 if (current_fndecl && overrides (fndecl, current_fndecl))
2431 tree base_offset, offset;
2432 tree context = DECL_CLASS_CONTEXT (fndecl);
2433 tree vfield = TYPE_VFIELD (t);
2434 tree this_offset;
2436 offset = get_class_offset (context, t, binfo, fndecl);
2438 /* Find the right offset for the this pointer based on the
2439 base class we just found. We have to take into
2440 consideration the virtual base class pointers that we
2441 stick in before the virtual function table pointer.
2443 Also, we want just the delta between the most base class
2444 that we derived this vfield from and us. */
2445 base_offset = size_binop (PLUS_EXPR,
2446 get_derived_offset (binfo, DECL_CONTEXT (current_fndecl)),
2447 BINFO_OFFSET (binfo));
2448 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2450 if (binfo == TYPE_BINFO (t))
2452 /* In this case, it is *type*'s vtable we are modifying.
2453 We start with the approximation that it's vtable is that
2454 of the immediate base class. */
2455 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2456 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2458 else
2460 /* This is our very own copy of `basetype' to play with.
2461 Later, we will fill in all the virtual functions
2462 that override the virtual functions in these base classes
2463 which are not defined by the current type. */
2464 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2465 prepare_fresh_vtable (binfo, t);
2468 #ifdef NOTQUITE
2469 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
2470 #endif
2471 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2472 this_offset,
2473 fndecl);
2475 ++n;
2476 virtuals = TREE_CHAIN (virtuals);
2480 /* These are the ones that are not through virtual base classes. */
2482 static void
2483 modify_all_direct_vtables (binfo, do_self, t, fndecl)
2484 tree binfo;
2485 int do_self;
2486 tree t, fndecl;
2488 tree binfos = BINFO_BASETYPES (binfo);
2489 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2491 /* Should we use something besides CLASSTYPE_VFIELDS? */
2492 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2493 modify_one_vtable (binfo, t, fndecl);
2495 for (i = 0; i < n_baselinks; i++)
2497 tree base_binfo = TREE_VEC_ELT (binfos, i);
2498 int is_not_base_vtable
2499 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2500 if (! TREE_VIA_VIRTUAL (base_binfo))
2501 modify_all_direct_vtables (base_binfo, is_not_base_vtable, t, fndecl);
2505 /* Fixup all the delta entries in this one vtable that need updating. */
2507 static void
2508 fixup_vtable_deltas1 (binfo, t)
2509 tree binfo, t;
2511 tree virtuals = BINFO_VIRTUALS (binfo);
2512 unsigned HOST_WIDE_INT n;
2514 n = skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo));
2516 while (virtuals)
2518 tree fndecl = TREE_VALUE (virtuals);
2519 tree delta = TREE_PURPOSE (virtuals);
2521 if (fndecl)
2523 tree base_offset, offset;
2524 tree context = DECL_CLASS_CONTEXT (fndecl);
2525 tree vfield = TYPE_VFIELD (t);
2526 tree this_offset;
2528 offset = get_class_offset (context, t, binfo, fndecl);
2530 /* Find the right offset for the this pointer based on the
2531 base class we just found. We have to take into
2532 consideration the virtual base class pointers that we
2533 stick in before the virtual function table pointer.
2535 Also, we want just the delta between the most base class
2536 that we derived this vfield from and us. */
2537 base_offset = size_binop (PLUS_EXPR,
2538 get_derived_offset (binfo,
2539 DECL_CONTEXT (fndecl)),
2540 BINFO_OFFSET (binfo));
2541 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2543 if (! tree_int_cst_equal (this_offset, delta))
2545 /* Make sure we can modify the derived association with immunity. */
2546 if (binfo == TYPE_BINFO (t))
2548 /* In this case, it is *type*'s vtable we are modifying.
2549 We start with the approximation that it's vtable is that
2550 of the immediate base class. */
2551 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2552 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2554 else
2556 /* This is our very own copy of `basetype' to play with.
2557 Later, we will fill in all the virtual functions
2558 that override the virtual functions in these base classes
2559 which are not defined by the current type. */
2560 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2561 prepare_fresh_vtable (binfo, t);
2564 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2565 this_offset,
2566 fndecl);
2569 ++n;
2570 virtuals = TREE_CHAIN (virtuals);
2574 /* Fixup all the delta entries in all the direct vtables that need updating.
2575 This happens when we have non-overridden virtual functions from a
2576 virtual base class, that are at a different offset, in the new
2577 hierarchy, because the layout of the virtual bases has changed. */
2579 static void
2580 fixup_vtable_deltas (binfo, init_self, t)
2581 tree binfo;
2582 int init_self;
2583 tree t;
2585 tree binfos = BINFO_BASETYPES (binfo);
2586 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2588 for (i = 0; i < n_baselinks; i++)
2590 tree base_binfo = TREE_VEC_ELT (binfos, i);
2591 int is_not_base_vtable
2592 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2593 if (! TREE_VIA_VIRTUAL (base_binfo))
2594 fixup_vtable_deltas (base_binfo, is_not_base_vtable, t);
2596 /* Should we use something besides CLASSTYPE_VFIELDS? */
2597 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2598 fixup_vtable_deltas1 (binfo, t);
2601 /* These are the ones that are through virtual base classes. */
2603 static void
2604 modify_all_indirect_vtables (binfo, do_self, via_virtual, t, fndecl)
2605 tree binfo;
2606 int do_self, via_virtual;
2607 tree t, fndecl;
2609 tree binfos = BINFO_BASETYPES (binfo);
2610 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2612 /* Should we use something besides CLASSTYPE_VFIELDS? */
2613 if (do_self && via_virtual && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2614 modify_one_vtable (binfo, t, fndecl);
2616 for (i = 0; i < n_baselinks; i++)
2618 tree base_binfo = TREE_VEC_ELT (binfos, i);
2619 int is_not_base_vtable
2620 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2621 if (TREE_VIA_VIRTUAL (base_binfo))
2623 via_virtual = 1;
2624 base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2626 modify_all_indirect_vtables (base_binfo, is_not_base_vtable, via_virtual, t, fndecl);
2630 static void
2631 modify_all_vtables (t, fndecl)
2632 tree t;
2633 tree fndecl;
2635 /* Do these first, so that we will make use of any non-virtual class's
2636 vtable, over a virtual classes vtable. */
2637 modify_all_direct_vtables (TYPE_BINFO (t), 1, t, fndecl);
2638 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
2639 modify_all_indirect_vtables (TYPE_BINFO (t), 1, 0, t, fndecl);
2642 /* Here, we already know that they match in every respect.
2643 All we have to check is where they had their declarations. */
2645 static int
2646 strictly_overrides (fndecl1, fndecl2)
2647 tree fndecl1, fndecl2;
2649 int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
2650 DECL_CLASS_CONTEXT (fndecl1),
2651 0, (tree *)0);
2652 if (distance == -2 || distance > 0)
2653 return 1;
2654 return 0;
2657 /* Merge overrides for one vtable.
2658 If we want to merge in same function, we are fine.
2659 else
2660 if one has a DECL_CLASS_CONTEXT that is a parent of the
2661 other, than choose the more derived one
2662 else
2663 potentially ill-formed (see 10.3 [class.virtual])
2664 we have to check later to see if there was an
2665 override in this class. If there was ok, if not
2666 then it is ill-formed. (mrs)
2668 We take special care to reuse a vtable, if we can. */
2670 static void
2671 override_one_vtable (binfo, old, t)
2672 tree binfo, old, t;
2674 tree virtuals = BINFO_VIRTUALS (binfo);
2675 tree old_virtuals = BINFO_VIRTUALS (old);
2676 enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
2678 /* If we have already committed to modifying it, then don't try and
2679 reuse another vtable. */
2680 if (BINFO_NEW_VTABLE_MARKED (binfo))
2681 choose = NEITHER;
2683 skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo));
2684 skip_rtti_stuff (&old_virtuals, BINFO_TYPE (binfo));
2686 while (virtuals)
2688 tree fndecl = TREE_VALUE (virtuals);
2689 tree old_fndecl = TREE_VALUE (old_virtuals);
2691 /* First check to see if they are the same. */
2692 if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
2694 /* No need to do anything. */
2696 else if (strictly_overrides (fndecl, old_fndecl))
2698 if (choose == UNDECIDED)
2699 choose = REUSE_NEW;
2700 else if (choose == REUSE_OLD)
2702 choose = NEITHER;
2703 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2705 prepare_fresh_vtable (binfo, t);
2706 override_one_vtable (binfo, old, t);
2707 return;
2711 else if (strictly_overrides (old_fndecl, fndecl))
2713 if (choose == UNDECIDED)
2714 choose = REUSE_OLD;
2715 else if (choose == REUSE_NEW)
2717 choose = NEITHER;
2718 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2720 prepare_fresh_vtable (binfo, t);
2721 override_one_vtable (binfo, old, t);
2722 return;
2724 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2726 else if (choose == NEITHER)
2728 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2731 else
2733 choose = NEITHER;
2734 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2736 prepare_fresh_vtable (binfo, t);
2737 override_one_vtable (binfo, old, t);
2738 return;
2741 /* This MUST be overridden, or the class is ill-formed. */
2742 tree fndecl = TREE_VALUE (virtuals);
2744 fndecl = copy_node (fndecl);
2745 copy_lang_decl (fndecl);
2746 DECL_NEEDS_FINAL_OVERRIDER_P (fndecl) = 1;
2747 /* Make sure we search for it later. */
2748 if (! CLASSTYPE_ABSTRACT_VIRTUALS (t))
2749 CLASSTYPE_ABSTRACT_VIRTUALS (t) = error_mark_node;
2751 /* We can use integer_zero_node, as we will core dump
2752 if this is used anyway. */
2753 TREE_PURPOSE (virtuals) = integer_zero_node;
2754 TREE_VALUE (virtuals) = fndecl;
2757 virtuals = TREE_CHAIN (virtuals);
2758 old_virtuals = TREE_CHAIN (old_virtuals);
2761 /* Let's reuse the old vtable. */
2762 if (choose == REUSE_OLD)
2764 BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
2765 BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
2769 /* Merge in overrides for virtual bases.
2770 BINFO is the hierarchy we want to modify, and OLD has the potential
2771 overrides. */
2773 static void
2774 merge_overrides (binfo, old, do_self, t)
2775 tree binfo, old;
2776 int do_self;
2777 tree t;
2779 tree binfos = BINFO_BASETYPES (binfo);
2780 tree old_binfos = BINFO_BASETYPES (old);
2781 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2783 /* Should we use something besides CLASSTYPE_VFIELDS? */
2784 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2786 override_one_vtable (binfo, old, t);
2789 for (i = 0; i < n_baselinks; i++)
2791 tree base_binfo = TREE_VEC_ELT (binfos, i);
2792 tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
2793 int is_not_base_vtable
2794 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2795 if (! TREE_VIA_VIRTUAL (base_binfo))
2796 merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
2800 /* Get the base virtual function declarations in T that are either
2801 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2802 the overrider/hider. */
2804 static tree
2805 get_basefndecls (fndecl, t)
2806 tree fndecl, t;
2808 tree methods = TYPE_METHODS (t);
2809 tree base_fndecls = NULL_TREE;
2810 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2811 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2813 while (methods)
2815 if (TREE_CODE (methods) == FUNCTION_DECL
2816 && DECL_VINDEX (methods) != NULL_TREE
2817 && DECL_NAME (fndecl) == DECL_NAME (methods))
2818 base_fndecls = temp_tree_cons (fndecl, methods, base_fndecls);
2820 methods = TREE_CHAIN (methods);
2823 if (base_fndecls)
2824 return base_fndecls;
2826 for (i = 0; i < n_baseclasses; i++)
2828 tree base_binfo = TREE_VEC_ELT (binfos, i);
2829 tree basetype = BINFO_TYPE (base_binfo);
2831 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2832 base_fndecls);
2835 return base_fndecls;
2838 /* Mark the functions that have been hidden with their overriders.
2839 Since we start out with all functions already marked with a hider,
2840 no need to mark functions that are just hidden.
2842 Subroutine of warn_hidden. */
2844 static void
2845 mark_overriders (fndecl, base_fndecls)
2846 tree fndecl, base_fndecls;
2848 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2850 if (overrides (fndecl, TREE_VALUE (base_fndecls)))
2851 TREE_PURPOSE (base_fndecls) = fndecl;
2855 /* If this declaration supersedes the declaration of
2856 a method declared virtual in the base class, then
2857 mark this field as being virtual as well. */
2859 static void
2860 check_for_override (decl, ctype)
2861 tree decl, ctype;
2863 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
2864 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2865 int virtualp = DECL_VIRTUAL_P (decl);
2866 int found_overriden_fn = 0;
2868 for (i = 0; i < n_baselinks; i++)
2870 tree base_binfo = TREE_VEC_ELT (binfos, i);
2871 if (TYPE_VIRTUAL_P (BINFO_TYPE (base_binfo)))
2873 tree tmp = get_matching_virtual
2874 (base_binfo, decl,
2875 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
2877 if (tmp && !found_overriden_fn)
2879 /* If this function overrides some virtual in some base
2880 class, then the function itself is also necessarily
2881 virtual, even if the user didn't explicitly say so. */
2882 DECL_VIRTUAL_P (decl) = 1;
2884 /* The TMP we really want is the one from the deepest
2885 baseclass on this path, taking care not to
2886 duplicate if we have already found it (via another
2887 path to its virtual baseclass. */
2888 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
2890 cp_error_at ("`static %#D' cannot be declared", decl);
2891 cp_error_at (" since `virtual %#D' declared in base class",
2892 tmp);
2893 break;
2895 virtualp = 1;
2897 DECL_VINDEX (decl)
2898 = tree_cons (NULL_TREE, tmp, DECL_VINDEX (decl));
2900 /* We now know that DECL overrides something,
2901 which is all that is important. But, we must
2902 continue to iterate through all the base-classes
2903 in order to allow get_matching_virtual to check for
2904 various illegal overrides. */
2905 found_overriden_fn = 1;
2909 if (virtualp)
2911 if (DECL_VINDEX (decl) == NULL_TREE)
2912 DECL_VINDEX (decl) = error_mark_node;
2913 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2917 /* Warn about hidden virtual functions that are not overridden in t.
2918 We know that constructors and destructors don't apply. */
2920 void
2921 warn_hidden (t)
2922 tree t;
2924 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2925 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2926 int i;
2928 /* We go through each separately named virtual function. */
2929 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2931 tree fns = TREE_VEC_ELT (method_vec, i);
2932 tree fndecl;
2934 tree base_fndecls = NULL_TREE;
2935 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2936 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2938 /* First see if we have any virtual functions in this batch. */
2939 for (; fns; fns = OVL_NEXT (fns))
2941 fndecl = OVL_CURRENT (fns);
2942 if (DECL_VINDEX (fndecl))
2943 break;
2946 if (fns == NULL_TREE)
2947 continue;
2949 /* First we get a list of all possible functions that might be
2950 hidden from each base class. */
2951 for (i = 0; i < n_baseclasses; i++)
2953 tree base_binfo = TREE_VEC_ELT (binfos, i);
2954 tree basetype = BINFO_TYPE (base_binfo);
2956 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2957 base_fndecls);
2960 fns = OVL_NEXT (fns);
2962 /* ...then mark up all the base functions with overriders, preferring
2963 overriders to hiders. */
2964 if (base_fndecls)
2965 for (; fns; fns = OVL_NEXT (fns))
2967 fndecl = OVL_CURRENT (fns);
2968 if (DECL_VINDEX (fndecl))
2969 mark_overriders (fndecl, base_fndecls);
2972 /* Now give a warning for all base functions without overriders,
2973 as they are hidden. */
2974 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2976 if (! overrides (TREE_PURPOSE (base_fndecls),
2977 TREE_VALUE (base_fndecls)))
2979 /* Here we know it is a hider, and no overrider exists. */
2980 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2981 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
2987 /* Check for things that are invalid. There are probably plenty of other
2988 things we should check for also. */
2990 static void
2991 finish_struct_anon (t)
2992 tree t;
2994 tree field;
2996 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2998 if (TREE_STATIC (field))
2999 continue;
3000 if (TREE_CODE (field) != FIELD_DECL)
3001 continue;
3003 if (DECL_NAME (field) == NULL_TREE
3004 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3006 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3007 for (; elt; elt = TREE_CHAIN (elt))
3009 if (DECL_ARTIFICIAL (elt))
3010 continue;
3012 if (DECL_NAME (elt) == constructor_name (t))
3013 cp_pedwarn_at ("ANSI C++ forbids member `%D' with same name as enclosing class",
3014 elt);
3016 if (TREE_CODE (elt) != FIELD_DECL)
3018 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3019 elt);
3020 continue;
3023 if (TREE_PRIVATE (elt))
3024 cp_pedwarn_at ("private member `%#D' in anonymous union",
3025 elt);
3026 else if (TREE_PROTECTED (elt))
3027 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3028 elt);
3030 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3031 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3037 extern int interface_only, interface_unknown;
3039 /* Create default constructors, assignment operators, and so forth for
3040 the type indicated by T, if they are needed.
3041 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3042 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3043 cannot have a default constructor, copy constructor taking a const
3044 reference argument, or an assignment operator, respectively. If a
3045 virtual destructor is created, its DECL is returned; otherwise the
3046 return value is NULL_TREE. */
3048 static tree
3049 add_implicitly_declared_members (t, cant_have_default_ctor,
3050 cant_have_const_cctor,
3051 cant_have_assignment)
3052 tree t;
3053 int cant_have_default_ctor;
3054 int cant_have_const_cctor;
3055 int cant_have_assignment;
3057 tree default_fn;
3058 tree implicit_fns = NULL_TREE;
3059 tree name = TYPE_IDENTIFIER (t);
3060 tree virtual_dtor = NULL_TREE;
3061 tree *f;
3063 /* Destructor. */
3064 if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
3066 default_fn = cons_up_default_function (t, name, 0);
3067 check_for_override (default_fn, t);
3069 /* If we couldn't make it work, then pretend we didn't need it. */
3070 if (default_fn == void_type_node)
3071 TYPE_NEEDS_DESTRUCTOR (t) = 0;
3072 else
3074 TREE_CHAIN (default_fn) = implicit_fns;
3075 implicit_fns = default_fn;
3077 if (DECL_VINDEX (default_fn))
3078 virtual_dtor = default_fn;
3081 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3083 /* Default constructor. */
3084 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3086 default_fn = cons_up_default_function (t, name, 2);
3087 TREE_CHAIN (default_fn) = implicit_fns;
3088 implicit_fns = default_fn;
3091 /* Copy constructor. */
3092 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3094 /* ARM 12.18: You get either X(X&) or X(const X&), but
3095 not both. --Chip */
3096 default_fn = cons_up_default_function (t, name,
3097 3 + cant_have_const_cctor);
3098 TREE_CHAIN (default_fn) = implicit_fns;
3099 implicit_fns = default_fn;
3102 /* Assignment operator. */
3103 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3105 default_fn = cons_up_default_function (t, name,
3106 5 + cant_have_assignment);
3107 TREE_CHAIN (default_fn) = implicit_fns;
3108 implicit_fns = default_fn;
3111 /* Now, hook all of the new functions on to TYPE_METHODS,
3112 and add them to the CLASSTYPE_METHOD_VEC. */
3113 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3114 add_method (t, 0, *f);
3115 *f = TYPE_METHODS (t);
3116 TYPE_METHODS (t) = implicit_fns;
3118 return virtual_dtor;
3121 /* Subroutine of finish_struct_1. Recursively count the number of fields
3122 in TYPE, including anonymous union members. */
3124 static int
3125 count_fields (fields)
3126 tree fields;
3128 tree x;
3129 int n_fields = 0;
3130 for (x = fields; x; x = TREE_CHAIN (x))
3132 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3133 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3134 else
3135 n_fields += 1;
3137 return n_fields;
3140 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3141 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3143 static int
3144 add_fields_to_vec (fields, field_vec, idx)
3145 tree fields, field_vec;
3146 int idx;
3148 tree x;
3149 for (x = fields; x; x = TREE_CHAIN (x))
3151 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3152 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3153 else
3154 TREE_VEC_ELT (field_vec, idx++) = x;
3156 return idx;
3159 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
3160 (or C++ class declaration).
3162 For C++, we must handle the building of derived classes.
3163 Also, C++ allows static class members. The way that this is
3164 handled is to keep the field name where it is (as the DECL_NAME
3165 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
3166 of the field. layout_record and layout_union will know about this.
3168 More C++ hair: inline functions have text in their
3169 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
3170 meaningful tree structure. After the struct has been laid out, set
3171 things up so that this can happen.
3173 And still more: virtual functions. In the case of single inheritance,
3174 when a new virtual function is seen which redefines a virtual function
3175 from the base class, the new virtual function is placed into
3176 the virtual function table at exactly the same address that
3177 it had in the base class. When this is extended to multiple
3178 inheritance, the same thing happens, except that multiple virtual
3179 function tables must be maintained. The first virtual function
3180 table is treated in exactly the same way as in the case of single
3181 inheritance. Additional virtual function tables have different
3182 DELTAs, which tell how to adjust `this' to point to the right thing.
3184 ATTRIBUTES is the set of decl attributes to be applied, if any. */
3186 void
3187 finish_struct_1 (t)
3188 tree t;
3190 int old;
3191 enum tree_code code = TREE_CODE (t);
3192 tree fields = TYPE_FIELDS (t);
3193 tree x, last_x, method_vec;
3194 int has_virtual;
3195 int max_has_virtual;
3196 tree pending_virtuals = NULL_TREE;
3197 tree pending_hard_virtuals = NULL_TREE;
3198 tree abstract_virtuals = NULL_TREE;
3199 tree vfield;
3200 tree vfields;
3201 tree virtual_dtor;
3202 int cant_have_default_ctor;
3203 int cant_have_const_ctor;
3204 int no_const_asn_ref;
3205 int has_mutable = 0;
3206 int n_fields = 0;
3207 int non_pod_class = 0;
3209 /* The index of the first base class which has virtual
3210 functions. Only applied to non-virtual baseclasses. */
3211 int first_vfn_base_index;
3213 int n_baseclasses;
3214 int any_default_members = 0;
3215 int const_sans_init = 0;
3216 int ref_sans_init = 0;
3217 tree access_decls = NULL_TREE;
3218 int aggregate = 1;
3219 int empty = 1;
3220 int has_pointers = 0;
3221 tree inline_friends;
3223 if (TYPE_SIZE (t))
3225 if (IS_AGGR_TYPE (t))
3226 cp_error ("redefinition of `%#T'", t);
3227 else
3228 my_friendly_abort (172);
3229 popclass ();
3230 return;
3233 GNU_xref_decl (current_function_decl, t);
3235 /* If this type was previously laid out as a forward reference,
3236 make sure we lay it out again. */
3238 TYPE_SIZE (t) = NULL_TREE;
3239 CLASSTYPE_GOT_SEMICOLON (t) = 0;
3241 old = suspend_momentary ();
3243 /* Install struct as DECL_FIELD_CONTEXT of each field decl.
3244 Also process specified field sizes.
3245 Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
3246 The specified size is found in the DECL_INITIAL.
3247 Store 0 there, except for ": 0" fields (so we can find them
3248 and delete them, below). */
3250 if (TYPE_BINFO_BASETYPES (t))
3251 n_baseclasses = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t));
3252 else
3253 n_baseclasses = 0;
3255 if (n_baseclasses > 0)
3257 struct base_info base_info;
3259 first_vfn_base_index = finish_base_struct (t, &base_info);
3260 /* Remember where we got our vfield from. */
3261 CLASSTYPE_VFIELD_PARENT (t) = first_vfn_base_index;
3262 has_virtual = base_info.has_virtual;
3263 max_has_virtual = base_info.max_has_virtual;
3264 vfield = base_info.vfield;
3265 vfields = base_info.vfields;
3266 CLASSTYPE_RTTI (t) = base_info.rtti;
3267 cant_have_default_ctor = base_info.cant_have_default_ctor;
3268 cant_have_const_ctor = base_info.cant_have_const_ctor;
3269 no_const_asn_ref = base_info.no_const_asn_ref;
3270 aggregate = 0;
3272 else
3274 first_vfn_base_index = -1;
3275 has_virtual = 0;
3276 max_has_virtual = has_virtual;
3277 vfield = NULL_TREE;
3278 vfields = NULL_TREE;
3279 CLASSTYPE_RTTI (t) = NULL_TREE;
3280 cant_have_default_ctor = 0;
3281 cant_have_const_ctor = 0;
3282 no_const_asn_ref = 0;
3285 /* The three of these are approximations which may later be
3286 modified. Needed at this point to make add_virtual_function
3287 and modify_vtable_entries work. */
3288 CLASSTYPE_VFIELDS (t) = vfields;
3289 TYPE_VFIELD (t) = vfield;
3291 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3293 GNU_xref_member (current_class_name, x);
3295 /* If this was an evil function, don't keep it in class. */
3296 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3297 continue;
3299 /* Do both of these, even though they're in the same union;
3300 if the insn `r' member and the size `i' member are
3301 different sizes, as on the alpha, the larger of the two
3302 will end up with garbage in it. */
3303 DECL_SAVED_INSNS (x) = 0;
3304 DECL_FIELD_SIZE (x) = 0;
3306 check_for_override (x, t);
3307 if (DECL_ABSTRACT_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3308 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3310 /* The name of the field is the original field name
3311 Save this in auxiliary field for later overloading. */
3312 if (DECL_VINDEX (x))
3314 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3315 &has_virtual, x, t);
3316 if (DECL_ABSTRACT_VIRTUAL_P (x))
3317 abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
3318 #if 0
3319 /* XXX Why did I comment this out? (jason) */
3320 else
3321 TREE_USED (x) = 1;
3322 #endif
3326 if (n_baseclasses)
3327 fields = chainon (build_vbase_pointer_fields (t), fields);
3329 last_x = NULL_TREE;
3330 for (x = fields; x; x = TREE_CHAIN (x))
3332 tree type = TREE_TYPE (x);
3333 GNU_xref_member (current_class_name, x);
3335 if (TREE_CODE (x) == FIELD_DECL)
3337 DECL_PACKED (x) |= TYPE_PACKED (t);
3339 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3340 /* A zero-width bitfield doesn't do the trick. */;
3341 else
3342 empty = 0;
3345 if (TREE_CODE (x) == USING_DECL)
3347 /* Save access declarations for later. */
3348 if (last_x)
3349 TREE_CHAIN (last_x) = TREE_CHAIN (x);
3350 else
3351 fields = TREE_CHAIN (x);
3353 access_decls = tree_cons (NULL_TREE, x, access_decls);
3354 continue;
3357 last_x = x;
3359 if (TREE_CODE (x) == TYPE_DECL
3360 || TREE_CODE (x) == TEMPLATE_DECL)
3361 continue;
3363 /* If we've gotten this far, it's a data member, possibly static,
3364 or an enumerator. */
3366 DECL_FIELD_CONTEXT (x) = t;
3368 /* ``A local class cannot have static data members.'' ARM 9.4 */
3369 if (current_function_decl && TREE_STATIC (x))
3370 cp_error_at ("field `%D' in local class cannot be static", x);
3372 /* Perform error checking that did not get done in
3373 grokdeclarator. */
3374 if (TREE_CODE (type) == FUNCTION_TYPE)
3376 cp_error_at ("field `%D' invalidly declared function type",
3378 type = build_pointer_type (type);
3379 TREE_TYPE (x) = type;
3381 else if (TREE_CODE (type) == METHOD_TYPE)
3383 cp_error_at ("field `%D' invalidly declared method type", x);
3384 type = build_pointer_type (type);
3385 TREE_TYPE (x) = type;
3387 else if (TREE_CODE (type) == OFFSET_TYPE)
3389 cp_error_at ("field `%D' invalidly declared offset type", x);
3390 type = build_pointer_type (type);
3391 TREE_TYPE (x) = type;
3394 #if 0
3395 if (DECL_NAME (x) == constructor_name (t))
3396 cant_have_default_ctor = 1;
3397 #endif
3399 if (type == error_mark_node)
3400 continue;
3402 DECL_SAVED_INSNS (x) = 0;
3403 DECL_FIELD_SIZE (x) = 0;
3405 /* When this goes into scope, it will be a non-local reference. */
3406 DECL_NONLOCAL (x) = 1;
3408 if (TREE_CODE (x) == CONST_DECL)
3409 continue;
3411 if (TREE_CODE (x) == VAR_DECL)
3413 if (TREE_CODE (t) == UNION_TYPE)
3414 /* Unions cannot have static members. */
3415 cp_error_at ("field `%D' declared static in union", x);
3417 continue;
3420 /* Now it can only be a FIELD_DECL. */
3422 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3423 aggregate = 0;
3425 /* If this is of reference type, check if it needs an init.
3426 Also do a little ANSI jig if necessary. */
3427 if (TREE_CODE (type) == REFERENCE_TYPE)
3429 non_pod_class = 1;
3431 if (DECL_INITIAL (x) == NULL_TREE)
3432 ref_sans_init = 1;
3434 /* ARM $12.6.2: [A member initializer list] (or, for an
3435 aggregate, initialization by a brace-enclosed list) is the
3436 only way to initialize nonstatic const and reference
3437 members. */
3438 cant_have_default_ctor = 1;
3439 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3441 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3443 if (DECL_NAME (x))
3444 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3445 else
3446 cp_warning_at ("non-static reference in class without a constructor", x);
3450 while (TREE_CODE (type) == ARRAY_TYPE)
3451 type = TREE_TYPE (type);
3453 if (TREE_CODE (type) == POINTER_TYPE)
3454 has_pointers = 1;
3456 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3457 has_mutable = 1;
3459 if (! pod_type_p (type) || TYPE_PTRMEM_P (type)
3460 || TYPE_PTRMEMFUNC_P (type))
3461 non_pod_class = 1;
3463 /* If any field is const, the structure type is pseudo-const. */
3464 if (CP_TYPE_CONST_P (type))
3466 C_TYPE_FIELDS_READONLY (t) = 1;
3467 if (DECL_INITIAL (x) == NULL_TREE)
3468 const_sans_init = 1;
3470 /* ARM $12.6.2: [A member initializer list] (or, for an
3471 aggregate, initialization by a brace-enclosed list) is the
3472 only way to initialize nonstatic const and reference
3473 members. */
3474 cant_have_default_ctor = 1;
3475 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3477 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3479 if (DECL_NAME (x))
3480 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3481 else
3482 cp_warning_at ("non-static const member in class without a constructor", x);
3485 else
3487 /* A field that is pseudo-const makes the structure
3488 likewise. */
3489 if (IS_AGGR_TYPE (type))
3491 if (C_TYPE_FIELDS_READONLY (type))
3492 C_TYPE_FIELDS_READONLY (t) = 1;
3493 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3494 const_sans_init = 1;
3498 /* We set DECL_C_BIT_FIELD in grokbitfield.
3499 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3500 if (DECL_C_BIT_FIELD (x))
3502 /* Invalid bit-field size done by grokfield. */
3503 /* Detect invalid bit-field type. Simply checking if TYPE is
3504 integral is insufficient, as that is the array core of the
3505 field type. If TREE_TYPE (x) is integral, then TYPE must be
3506 the same. */
3507 if (DECL_INITIAL (x)
3508 && ! INTEGRAL_TYPE_P (TREE_TYPE (x)))
3510 cp_error_at ("bit-field `%#D' with non-integral type", x);
3511 DECL_INITIAL (x) = NULL;
3514 /* Detect and ignore out of range field width. */
3515 if (DECL_INITIAL (x))
3517 tree w = DECL_INITIAL (x);
3518 register int width = 0;
3520 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3521 STRIP_NOPS (w);
3523 /* detect invalid field size. */
3524 if (TREE_CODE (w) == CONST_DECL)
3525 w = DECL_INITIAL (w);
3526 else if (TREE_READONLY_DECL_P (w))
3527 w = decl_constant_value (w);
3529 if (TREE_CODE (w) != INTEGER_CST)
3531 cp_error_at ("bit-field `%D' width not an integer constant",
3533 DECL_INITIAL (x) = NULL_TREE;
3535 else if (width = TREE_INT_CST_LOW (w),
3536 width < 0)
3538 DECL_INITIAL (x) = NULL;
3539 cp_error_at ("negative width in bit-field `%D'", x);
3541 else if (width == 0 && DECL_NAME (x) != 0)
3543 DECL_INITIAL (x) = NULL;
3544 cp_error_at ("zero width for bit-field `%D'", x);
3546 else if (width
3547 > TYPE_PRECISION (long_long_unsigned_type_node))
3549 /* The backend will dump if you try to use something
3550 too big; avoid that. */
3551 DECL_INITIAL (x) = NULL;
3552 sorry ("bit-fields larger than %d bits",
3553 TYPE_PRECISION (long_long_unsigned_type_node));
3554 cp_error_at (" in declaration of `%D'", x);
3556 else if (width > TYPE_PRECISION (type)
3557 && TREE_CODE (type) != ENUMERAL_TYPE
3558 && TREE_CODE (type) != BOOLEAN_TYPE)
3560 cp_warning_at ("width of `%D' exceeds its type", x);
3562 else if (TREE_CODE (type) == ENUMERAL_TYPE
3563 && ((min_precision (TYPE_MIN_VALUE (type),
3564 TREE_UNSIGNED (type)) > width)
3565 || (min_precision (TYPE_MAX_VALUE (type),
3566 TREE_UNSIGNED (type)) > width)))
3568 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3569 x, type);
3572 if (DECL_INITIAL (x))
3574 DECL_INITIAL (x) = NULL_TREE;
3575 DECL_FIELD_SIZE (x) = width;
3576 DECL_BIT_FIELD (x) = 1;
3578 if (width == 0)
3580 #ifdef EMPTY_FIELD_BOUNDARY
3581 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3582 EMPTY_FIELD_BOUNDARY);
3583 #endif
3584 #ifdef PCC_BITFIELD_TYPE_MATTERS
3585 if (PCC_BITFIELD_TYPE_MATTERS)
3586 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3587 TYPE_ALIGN (type));
3588 #endif
3592 else
3593 /* Non-bit-fields are aligned for their type. */
3594 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (type));
3596 else
3598 if (CLASS_TYPE_P (type) && ! ANON_AGGR_TYPE_P (type))
3600 /* Never let anything with uninheritable virtuals
3601 make it through without complaint. */
3602 abstract_virtuals_error (x, type);
3604 if (code == UNION_TYPE)
3606 const char *fie = NULL;
3607 if (TYPE_NEEDS_CONSTRUCTING (type))
3608 fie = "constructor";
3609 else if (TYPE_NEEDS_DESTRUCTOR (type))
3610 fie = "destructor";
3611 else if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3612 fie = "copy assignment operator";
3613 if (fie)
3614 cp_error_at ("member `%#D' with %s not allowed in union", x,
3615 fie);
3617 else
3619 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3620 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3621 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3622 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3625 if (!TYPE_HAS_CONST_INIT_REF (type))
3626 cant_have_const_ctor = 1;
3628 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3629 no_const_asn_ref = 1;
3631 if (TYPE_HAS_CONSTRUCTOR (type)
3632 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3634 cant_have_default_ctor = 1;
3635 #if 0
3636 /* This is wrong for aggregates. */
3637 if (! TYPE_HAS_CONSTRUCTOR (t))
3639 if (DECL_NAME (x))
3640 cp_pedwarn_at ("member `%#D' with only non-default constructor", x);
3641 else
3642 cp_pedwarn_at ("member with only non-default constructor", x);
3643 cp_pedwarn_at ("in class without a constructor",
3646 #endif
3649 if (DECL_INITIAL (x) != NULL_TREE)
3651 /* `build_class_init_list' does not recognize
3652 non-FIELD_DECLs. */
3653 if (code == UNION_TYPE && any_default_members != 0)
3654 cp_error_at ("multiple fields in union `%T' initialized");
3655 any_default_members = 1;
3659 unsigned int min_align = (DECL_PACKED (x) ? BITS_PER_UNIT
3660 : TYPE_ALIGN (TREE_TYPE (x)));
3661 /* Non-bit-fields are aligned for their type, except packed
3662 fields which require only BITS_PER_UNIT alignment. */
3663 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), min_align);
3668 /* If this type has any constant members which did not come
3669 with their own initialization, mark that fact here. It is
3670 not an error here, since such types can be saved either by their
3671 constructors, or by fortuitous initialization. */
3672 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
3673 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
3674 CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
3675 CLASSTYPE_HAS_MUTABLE (t) = has_mutable;
3677 /* Effective C++ rule 11. */
3678 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3679 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3681 cp_warning ("`%#T' has pointer data members", t);
3683 if (! TYPE_HAS_INIT_REF (t))
3685 cp_warning (" but does not override `%T(const %T&)'", t, t);
3686 if (! TYPE_HAS_ASSIGN_REF (t))
3687 cp_warning (" or `operator=(const %T&)'", t);
3689 else if (! TYPE_HAS_ASSIGN_REF (t))
3690 cp_warning (" but does not override `operator=(const %T&)'", t);
3693 /* Do some bookkeeping that will guide the generation of implicitly
3694 declared member functions. */
3695 TYPE_HAS_COMPLEX_INIT_REF (t)
3696 |= (TYPE_HAS_INIT_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3697 || has_virtual || any_default_members);
3698 TYPE_NEEDS_CONSTRUCTING (t)
3699 |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3700 || has_virtual || any_default_members);
3701 CLASSTYPE_NON_AGGREGATE (t)
3702 = ! aggregate || has_virtual || TYPE_HAS_CONSTRUCTOR (t);
3703 CLASSTYPE_NON_POD_P (t)
3704 = non_pod_class || CLASSTYPE_NON_AGGREGATE (t)
3705 || TYPE_HAS_DESTRUCTOR (t) || TYPE_HAS_ASSIGN_REF (t);
3706 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
3707 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
3708 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
3710 /* Synthesize any needed methods. Note that methods will be synthesized
3711 for anonymous unions; grok_x_components undoes that. */
3712 virtual_dtor
3713 = add_implicitly_declared_members (t, cant_have_default_ctor,
3714 cant_have_const_ctor,
3715 no_const_asn_ref);
3716 if (virtual_dtor)
3717 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3718 &has_virtual, virtual_dtor, t);
3720 if (TYPE_METHODS (t))
3722 finish_struct_methods (t);
3723 method_vec = CLASSTYPE_METHOD_VEC (t);
3725 else
3727 method_vec = 0;
3729 /* Just in case these got accidentally
3730 filled in by syntax errors. */
3731 TYPE_HAS_CONSTRUCTOR (t) = 0;
3732 TYPE_HAS_DESTRUCTOR (t) = 0;
3735 for (access_decls = nreverse (access_decls); access_decls;
3736 access_decls = TREE_CHAIN (access_decls))
3737 handle_using_decl (TREE_VALUE (access_decls), t, method_vec, fields);
3739 if (vfield == NULL_TREE && has_virtual)
3741 /* We build this decl with vtbl_ptr_type_node, which is a
3742 `vtable_entry_type*'. It might seem more precise to use
3743 `vtable_entry_type (*)[N]' where N is the number of firtual
3744 functions. However, that would require the vtable pointer in
3745 base classes to have a different type than the vtable pointer
3746 in derived classes. We could make that happen, but that
3747 still wouldn't solve all the problems. In particular, the
3748 type-based alias analysis code would decide that assignments
3749 to the base class vtable pointer can't alias assignments to
3750 the derived class vtable pointer, since they have different
3751 types. Thus, in an derived class destructor, where the base
3752 class constructor was inlined, we could generate bad code for
3753 setting up the vtable pointer.
3755 Therefore, we use one type for all vtable pointers. We still
3756 use a type-correct type; it's just doesn't indicate the array
3757 bounds. That's better than using `void*' or some such; it's
3758 cleaner, and it let's the alias analysis code know that these
3759 stores cannot alias stores to void*! */
3760 vfield = build_lang_decl (FIELD_DECL, get_vfield_name (t),
3761 vtbl_ptr_type_node);
3762 /* If you change any of the below, take a look at all the
3763 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
3764 them too. */
3765 DECL_ASSEMBLER_NAME (vfield) = get_identifier (VFIELD_BASE);
3766 TYPE_VFIELD (t) = vfield;
3767 DECL_VIRTUAL_P (vfield) = 1;
3768 DECL_ARTIFICIAL (vfield) = 1;
3769 DECL_FIELD_CONTEXT (vfield) = t;
3770 DECL_CLASS_CONTEXT (vfield) = t;
3771 DECL_FCONTEXT (vfield) = t;
3772 DECL_SAVED_INSNS (vfield) = 0;
3773 DECL_FIELD_SIZE (vfield) = 0;
3774 DECL_ALIGN (vfield) = TYPE_ALIGN (ptr_type_node);
3775 #if 0
3776 /* This is more efficient, but breaks binary compatibility, turn
3777 it on sometime when we don't care. If we turn it on, we also
3778 have to enable the code in dfs_init_vbase_pointers. */
3779 /* vfield is always first entry in structure. */
3780 TREE_CHAIN (vfield) = fields;
3781 fields = vfield;
3782 #else
3783 if (last_x)
3785 my_friendly_assert (TREE_CHAIN (last_x) == NULL_TREE, 175);
3786 TREE_CHAIN (last_x) = vfield;
3787 last_x = vfield;
3789 else
3790 fields = vfield;
3791 #endif
3792 empty = 0;
3793 vfields = chainon (vfields, build_tree_list (NULL_TREE, t));
3796 /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
3798 C++: maybe we will support default field initialization some day... */
3800 /* Delete all duplicate fields from the fields */
3801 delete_duplicate_fields (fields);
3803 /* Now we have the nearly final fieldlist for the data fields. Record it,
3804 then lay out the structure or union (including the fields). */
3806 TYPE_FIELDS (t) = fields;
3808 if (n_baseclasses)
3810 last_x = build_base_fields (t);
3812 /* If all our bases are empty, we can be empty too. */
3813 for (x = last_x; empty && x; x = TREE_CHAIN (x))
3814 if (DECL_SIZE (x) != integer_zero_node)
3815 empty = 0;
3818 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
3819 we have to save this before we start modifying
3820 TYPE_NONCOPIED_PARTS. */
3821 inline_friends = CLASSTYPE_INLINE_FRIENDS (t);
3822 CLASSTYPE_INLINE_FRIENDS (t) = NULL_TREE;
3824 if (empty)
3826 /* C++: do not let empty structures exist. */
3827 tree decl = build_lang_decl
3828 (FIELD_DECL, NULL_TREE, char_type_node);
3829 TREE_CHAIN (decl) = fields;
3830 TYPE_FIELDS (t) = decl;
3831 TYPE_NONCOPIED_PARTS (t)
3832 = tree_cons (NULL_TREE, decl, TYPE_NONCOPIED_PARTS (t));
3833 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
3836 if (n_baseclasses)
3837 TYPE_FIELDS (t) = chainon (last_x, TYPE_FIELDS (t));
3839 layout_type (t);
3841 /* Remember the size and alignment of the class before adding
3842 the virtual bases. */
3843 if (empty && flag_new_abi)
3844 CLASSTYPE_SIZE (t) = integer_zero_node;
3845 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
3846 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
3847 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
3848 else
3849 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
3850 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
3852 finish_struct_anon (t);
3854 /* Set the TYPE_DECL for this type to contain the right
3855 value for DECL_OFFSET, so that we can use it as part
3856 of a COMPONENT_REF for multiple inheritance. */
3858 layout_decl (TYPE_MAIN_DECL (t), 0);
3860 /* Now fix up any virtual base class types that we left lying
3861 around. We must get these done before we try to lay out the
3862 virtual function table. */
3863 pending_hard_virtuals = nreverse (pending_hard_virtuals);
3865 if (n_baseclasses)
3866 /* layout_basetypes will remove the base subobject fields. */
3867 max_has_virtual = layout_basetypes (t, max_has_virtual);
3868 if (empty)
3869 TYPE_FIELDS (t) = fields;
3871 my_friendly_assert (TYPE_FIELDS (t) == fields, 981117);
3873 /* Delete all zero-width bit-fields from the fieldlist */
3875 tree *fieldsp = &fields;
3876 while (*fieldsp)
3877 if (TREE_CODE (*fieldsp) == FIELD_DECL
3878 && DECL_C_BIT_FIELD (*fieldsp) && DECL_INITIAL (*fieldsp))
3879 *fieldsp = TREE_CHAIN (*fieldsp);
3880 else
3881 fieldsp = &TREE_CHAIN (*fieldsp);
3883 TYPE_FIELDS (t) = fields;
3885 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
3887 tree vbases;
3889 vbases = CLASSTYPE_VBASECLASSES (t);
3892 /* Now fixup overrides of all functions in vtables from all
3893 direct or indirect virtual base classes. */
3894 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3895 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3897 for (i = 0; i < n_baseclasses; i++)
3899 tree base_binfo = TREE_VEC_ELT (binfos, i);
3900 tree basetype = BINFO_TYPE (base_binfo);
3901 tree vbases;
3903 vbases = CLASSTYPE_VBASECLASSES (basetype);
3904 while (vbases)
3906 merge_overrides (binfo_member (BINFO_TYPE (vbases),
3907 CLASSTYPE_VBASECLASSES (t)),
3908 vbases, 1, t);
3909 vbases = TREE_CHAIN (vbases);
3915 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
3916 might need to know it for setting up the offsets in the vtable
3917 (or in thunks) below. */
3918 if (vfield != NULL_TREE
3919 && DECL_FIELD_CONTEXT (vfield) != t)
3921 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
3922 tree offset = BINFO_OFFSET (binfo);
3924 vfield = copy_node (vfield);
3925 copy_lang_decl (vfield);
3927 if (! integer_zerop (offset))
3928 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
3929 DECL_FIELD_CONTEXT (vfield) = t;
3930 DECL_CLASS_CONTEXT (vfield) = t;
3931 DECL_FIELD_BITPOS (vfield)
3932 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
3933 TYPE_VFIELD (t) = vfield;
3936 #ifdef NOTQUITE
3937 cp_warning ("Doing hard virtuals for %T...", t);
3938 #endif
3940 if (has_virtual > max_has_virtual)
3941 max_has_virtual = has_virtual;
3942 if (max_has_virtual > 0)
3943 TYPE_VIRTUAL_P (t) = 1;
3945 if (flag_rtti && TYPE_VIRTUAL_P (t) && !pending_hard_virtuals)
3946 modify_all_vtables (t, NULL_TREE);
3948 while (pending_hard_virtuals)
3950 modify_all_vtables (t,
3951 TREE_VALUE (pending_hard_virtuals));
3952 pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
3955 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
3957 tree vbases;
3958 /* Now fixup any virtual function entries from virtual bases
3959 that have different deltas. This has to come after we do the
3960 pending hard virtuals, as we might have a function that comes
3961 from multiple virtual base instances that is only overridden
3962 by a hard virtual above. */
3963 vbases = CLASSTYPE_VBASECLASSES (t);
3964 while (vbases)
3966 /* We might be able to shorten the amount of work we do by
3967 only doing this for vtables that come from virtual bases
3968 that have differing offsets, but don't want to miss any
3969 entries. */
3970 fixup_vtable_deltas (vbases, 1, t);
3971 vbases = TREE_CHAIN (vbases);
3975 /* Under our model of GC, every C++ class gets its own virtual
3976 function table, at least virtually. */
3977 if (pending_virtuals)
3979 pending_virtuals = nreverse (pending_virtuals);
3980 /* We must enter these virtuals into the table. */
3981 if (first_vfn_base_index < 0)
3983 if (! CLASSTYPE_COM_INTERFACE (t))
3985 /* The second slot is for the tdesc pointer when thunks are used. */
3986 if (flag_vtable_thunks)
3987 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
3989 /* The first slot is for the rtti offset. */
3990 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
3992 set_rtti_entry (pending_virtuals,
3993 convert (ssizetype, integer_zero_node), t);
3995 build_vtable (NULL_TREE, t);
3997 else
3999 /* Here we know enough to change the type of our virtual
4000 function table, but we will wait until later this function. */
4002 if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
4003 build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index), t);
4006 /* If this type has basetypes with constructors, then those
4007 constructors might clobber the virtual function table. But
4008 they don't if the derived class shares the exact vtable of the base
4009 class. */
4011 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4013 else if (first_vfn_base_index >= 0)
4015 tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index);
4016 /* This class contributes nothing new to the virtual function
4017 table. However, it may have declared functions which
4018 went into the virtual function table "inherited" from the
4019 base class. If so, we grab a copy of those updated functions,
4020 and pretend they are ours. */
4022 /* See if we should steal the virtual info from base class. */
4023 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
4024 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
4025 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
4026 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
4027 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
4028 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4031 if (max_has_virtual || first_vfn_base_index >= 0)
4033 CLASSTYPE_VSIZE (t) = has_virtual;
4034 if (first_vfn_base_index >= 0)
4036 if (pending_virtuals)
4037 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t),
4038 pending_virtuals);
4040 else if (has_virtual)
4042 TYPE_BINFO_VIRTUALS (t) = pending_virtuals;
4043 DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1;
4047 /* Now lay out the virtual function table. */
4048 if (has_virtual)
4050 /* Use size_int so values are memoized in common cases. */
4051 tree itype = build_index_type (size_int (has_virtual));
4052 tree atype = build_cplus_array_type (vtable_entry_type, itype);
4054 layout_type (atype);
4056 TYPE_VFIELD (t) = vfield;
4058 /* We may have to grow the vtable. */
4059 if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
4061 TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
4062 DECL_SIZE (TYPE_BINFO_VTABLE (t)) = 0;
4063 layout_decl (TYPE_BINFO_VTABLE (t), 0);
4064 /* At one time the vtable info was grabbed 2 words at a time. This
4065 fails on sparc unless you have 8-byte alignment. (tiemann) */
4066 DECL_ALIGN (TYPE_BINFO_VTABLE (t))
4067 = MAX (TYPE_ALIGN (double_type_node),
4068 DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
4071 else if (first_vfn_base_index >= 0)
4072 TYPE_VFIELD (t) = vfield;
4073 CLASSTYPE_VFIELDS (t) = vfields;
4075 finish_struct_bits (t, max_has_virtual);
4077 /* Complete the rtl for any static member objects of the type we're
4078 working on. */
4079 for (x = fields; x; x = TREE_CHAIN (x))
4081 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4082 && TREE_TYPE (x) == t)
4084 DECL_MODE (x) = TYPE_MODE (t);
4085 make_decl_rtl (x, NULL, 0);
4089 /* Done with FIELDS...now decide whether to sort these for
4090 faster lookups later. Don't worry about optimizing
4091 for structs only declared in inline functions...they're
4092 not going to be referenced anywhere else.
4094 The C front-end only does this when n_fields > 15. We use
4095 a smaller number because most searches fail (succeeding
4096 ultimately as the search bores through the inheritance
4097 hierarchy), and we want this failure to occur quickly. */
4099 n_fields = count_fields (fields);
4100 if (n_fields > 7 && !allocation_temporary_p ())
4102 tree field_vec = make_tree_vec (n_fields);
4103 add_fields_to_vec (fields, field_vec, 0);
4104 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
4105 (int (*)(const void *, const void *))field_decl_cmp);
4106 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
4107 retrofit_lang_decl (TYPE_MAIN_DECL (t));
4108 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
4111 if (TYPE_HAS_CONSTRUCTOR (t))
4113 tree vfields = CLASSTYPE_VFIELDS (t);
4115 while (vfields)
4117 /* Mark the fact that constructor for T
4118 could affect anybody inheriting from T
4119 who wants to initialize vtables for VFIELDS's type. */
4120 if (VF_DERIVED_VALUE (vfields))
4121 TREE_ADDRESSABLE (vfields) = 1;
4122 vfields = TREE_CHAIN (vfields);
4126 /* Write out inline function definitions. */
4127 do_inline_function_hair (t, inline_friends);
4129 if (CLASSTYPE_VSIZE (t) != 0)
4131 /* In addition to this one, all the other vfields should be listed. */
4132 /* Before that can be done, we have to have FIELD_DECLs for them, and
4133 a place to find them. */
4134 TYPE_NONCOPIED_PARTS (t)
4135 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
4136 vfield, TYPE_NONCOPIED_PARTS (t));
4138 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
4139 && DECL_VINDEX (TREE_VEC_ELT (method_vec, 1)) == NULL_TREE)
4140 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
4144 /* Make the rtl for any new vtables we have created, and unmark
4145 the base types we marked. */
4146 finish_vtbls (TYPE_BINFO (t), 1, t);
4147 hack_incomplete_structures (t);
4149 #if 0
4150 if (TYPE_NAME (t) && TYPE_IDENTIFIER (t))
4151 undo_template_name_overload (TYPE_IDENTIFIER (t), 1);
4152 #endif
4154 resume_momentary (old);
4156 if (warn_overloaded_virtual)
4157 warn_hidden (t);
4159 #if 0
4160 /* This has to be done after we have sorted out what to do with
4161 the enclosing type. */
4162 if (write_symbols != DWARF_DEBUG)
4164 /* Be smarter about nested classes here. If a type is nested,
4165 only output it if we would output the enclosing type. */
4166 if (DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (t)))
4167 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = TREE_ASM_WRITTEN (TYPE_MAIN_DECL (t));
4169 #endif
4171 if (write_symbols != DWARF_DEBUG && write_symbols != DWARF2_DEBUG)
4173 /* If the type has methods, we want to think about cutting down
4174 the amount of symbol table stuff we output. The value stored in
4175 the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
4176 For example, if a member function is seen and we decide to
4177 write out that member function, then we can change the value
4178 of the DECL_IGNORED_P slot, and the type will be output when
4179 that member function's debug info is written out.
4181 We can't do this with DWARF, which does not support name
4182 references between translation units. */
4183 if (CLASSTYPE_METHOD_VEC (t))
4185 /* Don't output full info about any type
4186 which does not have its implementation defined here. */
4187 if (CLASSTYPE_INTERFACE_ONLY (t))
4188 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4189 #if 0
4190 /* XXX do something about this. */
4191 else if (CLASSTYPE_INTERFACE_UNKNOWN (t))
4192 /* Only a first approximation! */
4193 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4194 #endif
4196 else if (CLASSTYPE_INTERFACE_ONLY (t))
4197 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4200 /* Finish debugging output for this type. */
4201 rest_of_type_compilation (t, toplevel_bindings_p ());
4203 return;
4206 /* When T was built up, the member declarations were added in reverse
4207 order. Rearrange them to declaration order. */
4209 void
4210 unreverse_member_declarations (t)
4211 tree t;
4213 tree next;
4214 tree prev;
4215 tree x;
4217 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
4218 reverse order. Put them in declaration order now. */
4219 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
4220 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
4222 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
4223 reverse order, so we can't just use nreverse. */
4224 prev = NULL_TREE;
4225 for (x = TYPE_FIELDS (t);
4226 x && TREE_CODE (x) != TYPE_DECL;
4227 x = next)
4229 next = TREE_CHAIN (x);
4230 TREE_CHAIN (x) = prev;
4231 prev = x;
4233 if (prev)
4235 TREE_CHAIN (TYPE_FIELDS (t)) = x;
4236 if (prev)
4237 TYPE_FIELDS (t) = prev;
4241 tree
4242 finish_struct (t, attributes)
4243 tree t, attributes;
4245 /* Now that we've got all the field declarations, reverse everything
4246 as necessary. */
4247 unreverse_member_declarations (t);
4249 cplus_decl_attributes (t, attributes, NULL_TREE);
4251 if (processing_template_decl)
4253 finish_struct_methods (t);
4254 TYPE_SIZE (t) = integer_zero_node;
4256 else
4257 finish_struct_1 (t);
4259 TYPE_BEING_DEFINED (t) = 0;
4261 if (current_class_type)
4262 popclass ();
4263 else
4264 error ("trying to finish struct, but kicked out due to previous parse errors.");
4266 return t;
4269 /* Return the dynamic type of INSTANCE, if known.
4270 Used to determine whether the virtual function table is needed
4271 or not.
4273 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4274 of our knowledge of its type. */
4276 static tree
4277 fixed_type_or_null (instance, nonnull)
4278 tree instance;
4279 int *nonnull;
4281 if (nonnull)
4282 *nonnull = 0;
4284 switch (TREE_CODE (instance))
4286 case INDIRECT_REF:
4287 /* Check that we are not going through a cast of some sort. */
4288 if (TREE_TYPE (instance)
4289 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
4290 instance = TREE_OPERAND (instance, 0);
4291 /* fall through... */
4292 case CALL_EXPR:
4293 /* This is a call to a constructor, hence it's never zero. */
4294 if (TREE_HAS_CONSTRUCTOR (instance))
4296 if (nonnull)
4297 *nonnull = 1;
4298 return TREE_TYPE (instance);
4300 return NULL_TREE;
4302 case SAVE_EXPR:
4303 /* This is a call to a constructor, hence it's never zero. */
4304 if (TREE_HAS_CONSTRUCTOR (instance))
4306 if (nonnull)
4307 *nonnull = 1;
4308 return TREE_TYPE (instance);
4310 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4312 case RTL_EXPR:
4313 return NULL_TREE;
4315 case PLUS_EXPR:
4316 case MINUS_EXPR:
4317 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
4318 /* Propagate nonnull. */
4319 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4320 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
4321 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4322 return NULL_TREE;
4324 case NOP_EXPR:
4325 case CONVERT_EXPR:
4326 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4328 case ADDR_EXPR:
4329 if (nonnull)
4330 *nonnull = 1;
4331 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4333 case COMPONENT_REF:
4334 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
4336 case VAR_DECL:
4337 case FIELD_DECL:
4338 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
4339 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
4341 if (nonnull)
4342 *nonnull = 1;
4343 return TREE_TYPE (TREE_TYPE (instance));
4345 /* fall through... */
4346 case TARGET_EXPR:
4347 case PARM_DECL:
4348 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
4350 if (nonnull)
4351 *nonnull = 1;
4352 return TREE_TYPE (instance);
4354 else if (nonnull)
4356 if (instance == current_class_ptr
4357 && flag_this_is_variable <= 0)
4359 /* Normally, 'this' must be non-null. */
4360 if (flag_this_is_variable == 0)
4361 *nonnull = 1;
4363 /* <0 means we're in a constructor and we know our type. */
4364 if (flag_this_is_variable < 0)
4365 return TREE_TYPE (TREE_TYPE (instance));
4367 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4368 /* Reference variables should be references to objects. */
4369 *nonnull = 1;
4371 return NULL_TREE;
4373 default:
4374 return NULL_TREE;
4378 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
4379 to the static type. We also handle the case where INSTANCE is really
4380 a pointer.
4382 Used to determine whether the virtual function table is needed
4383 or not.
4385 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4386 of our knowledge of its type. */
4389 resolves_to_fixed_type_p (instance, nonnull)
4390 tree instance;
4391 int *nonnull;
4393 tree t = TREE_TYPE (instance);
4394 tree fixed = fixed_type_or_null (instance, nonnull);
4395 if (fixed == NULL_TREE)
4396 return 0;
4397 if (POINTER_TYPE_P (t))
4398 t = TREE_TYPE (t);
4399 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
4403 void
4404 init_class_processing ()
4406 current_class_depth = 0;
4407 current_class_stack_size = 10;
4408 current_class_stack
4409 = (class_stack_node_t) xmalloc (current_class_stack_size
4410 * sizeof (struct class_stack_node));
4412 access_default_node = build_int_2 (0, 0);
4413 access_public_node = build_int_2 (1, 0);
4414 access_protected_node = build_int_2 (2, 0);
4415 access_private_node = build_int_2 (3, 0);
4416 access_default_virtual_node = build_int_2 (4, 0);
4417 access_public_virtual_node = build_int_2 (5, 0);
4418 access_protected_virtual_node = build_int_2 (6, 0);
4419 access_private_virtual_node = build_int_2 (7, 0);
4422 /* Set current scope to NAME. CODE tells us if this is a
4423 STRUCT, UNION, or ENUM environment.
4425 NAME may end up being NULL_TREE if this is an anonymous or
4426 late-bound struct (as in "struct { ... } foo;") */
4428 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
4429 appropriate values, found by looking up the type definition of
4430 NAME (as a CODE).
4432 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
4433 which can be seen locally to the class. They are shadowed by
4434 any subsequent local declaration (including parameter names).
4436 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
4437 which have static meaning (i.e., static members, static
4438 member functions, enum declarations, etc).
4440 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
4441 which can be seen locally to the class (as in 1), but
4442 know that we are doing this for declaration purposes
4443 (i.e. friend foo::bar (int)).
4445 So that we may avoid calls to lookup_name, we cache the _TYPE
4446 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
4448 For multiple inheritance, we perform a two-pass depth-first search
4449 of the type lattice. The first pass performs a pre-order search,
4450 marking types after the type has had its fields installed in
4451 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
4452 unmarks the marked types. If a field or member function name
4453 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
4454 that name becomes `error_mark_node'. */
4456 void
4457 pushclass (type, modify)
4458 tree type;
4459 int modify;
4461 type = TYPE_MAIN_VARIANT (type);
4463 /* Make sure there is enough room for the new entry on the stack. */
4464 if (current_class_depth + 1 >= current_class_stack_size)
4466 current_class_stack_size *= 2;
4467 current_class_stack
4468 = (class_stack_node_t) xrealloc (current_class_stack,
4469 current_class_stack_size
4470 * sizeof (struct class_stack_node));
4473 /* Insert a new entry on the class stack. */
4474 current_class_stack[current_class_depth].name = current_class_name;
4475 current_class_stack[current_class_depth].type = current_class_type;
4476 current_class_stack[current_class_depth].access = current_access_specifier;
4477 current_class_stack[current_class_depth].names_used = 0;
4478 current_class_depth++;
4480 /* Now set up the new type. */
4481 current_class_name = TYPE_NAME (type);
4482 if (TREE_CODE (current_class_name) == TYPE_DECL)
4483 current_class_name = DECL_NAME (current_class_name);
4484 current_class_type = type;
4486 /* By default, things in classes are private, while things in
4487 structures or unions are public. */
4488 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
4489 ? access_private_node
4490 : access_public_node);
4492 if (previous_class_type != NULL_TREE
4493 && (type != previous_class_type
4494 || TYPE_SIZE (previous_class_type) == NULL_TREE)
4495 && current_class_depth == 1)
4497 /* Forcibly remove any old class remnants. */
4498 invalidate_class_lookup_cache ();
4501 /* If we're about to enter a nested class, clear
4502 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
4503 if (modify && current_class_depth > 1)
4504 clear_identifier_class_values ();
4506 pushlevel_class ();
4508 #if 0
4509 if (CLASSTYPE_TEMPLATE_INFO (type))
4510 overload_template_name (type);
4511 #endif
4513 if (modify)
4515 if (type != previous_class_type || current_class_depth > 1)
4516 push_class_decls (type);
4517 else
4519 tree item;
4521 /* We are re-entering the same class we just left, so we
4522 don't have to search the whole inheritance matrix to find
4523 all the decls to bind again. Instead, we install the
4524 cached class_shadowed list, and walk through it binding
4525 names and setting up IDENTIFIER_TYPE_VALUEs. */
4526 set_class_shadows (previous_class_values);
4527 for (item = previous_class_values; item; item = TREE_CHAIN (item))
4529 tree id = TREE_PURPOSE (item);
4530 tree decl = TREE_TYPE (item);
4532 push_class_binding (id, decl);
4533 if (TREE_CODE (decl) == TYPE_DECL)
4534 set_identifier_type_value (id, TREE_TYPE (decl));
4536 unuse_fields (type);
4539 storetags (CLASSTYPE_TAGS (type));
4543 /* When we exit a toplevel class scope, we save the
4544 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
4545 reenter the class. Here, we've entered some other class, so we
4546 must invalidate our cache. */
4548 void
4549 invalidate_class_lookup_cache ()
4551 tree t;
4553 /* This code can be seen as a cache miss. When we've cached a
4554 class' scope's bindings and we can't use them, we need to reset
4555 them. This is it! */
4556 for (t = previous_class_values; t; t = TREE_CHAIN (t))
4557 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
4559 previous_class_type = NULL_TREE;
4562 /* Get out of the current class scope. If we were in a class scope
4563 previously, that is the one popped to. */
4565 void
4566 popclass ()
4568 poplevel_class ();
4569 /* Since poplevel_class does the popping of class decls nowadays,
4570 this really only frees the obstack used for these decls. */
4571 pop_class_decls ();
4573 current_class_depth--;
4574 current_class_name = current_class_stack[current_class_depth].name;
4575 current_class_type = current_class_stack[current_class_depth].type;
4576 current_access_specifier = current_class_stack[current_class_depth].access;
4577 if (current_class_stack[current_class_depth].names_used)
4578 splay_tree_delete (current_class_stack[current_class_depth].names_used);
4581 /* Returns 1 if current_class_type is either T or a nested type of T. */
4584 currently_open_class (t)
4585 tree t;
4587 int i;
4588 if (t == current_class_type)
4589 return 1;
4590 for (i = 0; i < current_class_depth; ++i)
4591 if (current_class_stack [i].type == t)
4592 return 1;
4593 return 0;
4596 /* When entering a class scope, all enclosing class scopes' names with
4597 static meaning (static variables, static functions, types and enumerators)
4598 have to be visible. This recursive function calls pushclass for all
4599 enclosing class contexts until global or a local scope is reached.
4600 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
4601 formal of the same name. */
4603 void
4604 push_nested_class (type, modify)
4605 tree type;
4606 int modify;
4608 tree context;
4610 /* A namespace might be passed in error cases, like A::B:C. */
4611 if (type == NULL_TREE || type == error_mark_node || ! IS_AGGR_TYPE (type)
4612 || TREE_CODE (type) == NAMESPACE_DECL
4613 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
4614 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
4615 return;
4617 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
4619 if (context && CLASS_TYPE_P (context))
4620 push_nested_class (context, 2);
4621 pushclass (type, modify);
4624 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
4626 void
4627 pop_nested_class ()
4629 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
4631 popclass ();
4632 if (context && CLASS_TYPE_P (context))
4633 pop_nested_class ();
4636 /* Set global variables CURRENT_LANG_NAME to appropriate value
4637 so that behavior of name-mangling machinery is correct. */
4639 void
4640 push_lang_context (name)
4641 tree name;
4643 *current_lang_stack++ = current_lang_name;
4644 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
4645 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
4647 size_t old_size = VARRAY_SIZE (current_lang_base);
4649 VARRAY_GROW (current_lang_base, old_size + 10);
4650 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
4653 if (name == lang_name_cplusplus)
4655 strict_prototype = strict_prototypes_lang_cplusplus;
4656 current_lang_name = name;
4658 else if (name == lang_name_java)
4660 strict_prototype = strict_prototypes_lang_cplusplus;
4661 current_lang_name = name;
4662 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
4663 (See record_builtin_java_type in decl.c.) However, that causes
4664 incorrect debug entries if these types are actually used.
4665 So we re-enable debug output after extern "Java". */
4666 DECL_IGNORED_P (java_byte_type_node) = 0;
4667 DECL_IGNORED_P (java_short_type_node) = 0;
4668 DECL_IGNORED_P (java_int_type_node) = 0;
4669 DECL_IGNORED_P (java_long_type_node) = 0;
4670 DECL_IGNORED_P (java_float_type_node) = 0;
4671 DECL_IGNORED_P (java_double_type_node) = 0;
4672 DECL_IGNORED_P (java_char_type_node) = 0;
4673 DECL_IGNORED_P (java_boolean_type_node) = 0;
4675 else if (name == lang_name_c)
4677 strict_prototype = strict_prototypes_lang_c;
4678 current_lang_name = name;
4680 else
4681 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
4684 /* Get out of the current language scope. */
4686 void
4687 pop_lang_context ()
4689 /* Clear the current entry so that garbage collector won't hold on
4690 to it. */
4691 *current_lang_stack = NULL_TREE;
4692 current_lang_name = *--current_lang_stack;
4693 if (current_lang_name == lang_name_cplusplus
4694 || current_lang_name == lang_name_java)
4695 strict_prototype = strict_prototypes_lang_cplusplus;
4696 else if (current_lang_name == lang_name_c)
4697 strict_prototype = strict_prototypes_lang_c;
4700 /* Type instantiation routines. */
4702 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
4703 matches the TARGET_TYPE. If there is no satisfactory match, return
4704 error_mark_node, and issue an error message if COMPLAIN is
4705 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
4706 was a template-id, and EXPLICIT_TARGS are the explicitly provided
4707 template arguments. */
4709 static tree
4710 resolve_address_of_overloaded_function (target_type,
4711 overload,
4712 complain,
4713 template_only,
4714 explicit_targs)
4715 tree target_type;
4716 tree overload;
4717 int complain;
4718 int template_only;
4719 tree explicit_targs;
4721 /* Here's what the standard says:
4723 [over.over]
4725 If the name is a function template, template argument deduction
4726 is done, and if the argument deduction succeeds, the deduced
4727 arguments are used to generate a single template function, which
4728 is added to the set of overloaded functions considered.
4730 Non-member functions and static member functions match targets of
4731 type "pointer-to-function" or "reference-to-function." Nonstatic
4732 member functions match targets of type "pointer-to-member
4733 function;" the function type of the pointer to member is used to
4734 select the member function from the set of overloaded member
4735 functions. If a nonstatic member function is selected, the
4736 reference to the overloaded function name is required to have the
4737 form of a pointer to member as described in 5.3.1.
4739 If more than one function is selected, any template functions in
4740 the set are eliminated if the set also contains a non-template
4741 function, and any given template function is eliminated if the
4742 set contains a second template function that is more specialized
4743 than the first according to the partial ordering rules 14.5.5.2.
4744 After such eliminations, if any, there shall remain exactly one
4745 selected function. */
4747 int is_ptrmem = 0;
4748 int is_reference = 0;
4749 /* We store the matches in a TREE_LIST rooted here. The functions
4750 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
4751 interoperability with most_specialized_instantiation. */
4752 tree matches = NULL_TREE;
4753 tree fn;
4755 /* By the time we get here, we should be seeing only real
4756 pointer-to-member types, not the internal POINTER_TYPE to
4757 METHOD_TYPE representation. */
4758 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
4759 && (TREE_CODE (TREE_TYPE (target_type))
4760 == METHOD_TYPE)), 0);
4762 /* Check that the TARGET_TYPE is reasonable. */
4763 if (TYPE_PTRFN_P (target_type))
4764 /* This is OK. */
4766 else if (TYPE_PTRMEMFUNC_P (target_type))
4767 /* This is OK, too. */
4768 is_ptrmem = 1;
4769 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
4771 /* This is OK, too. This comes from a conversion to reference
4772 type. */
4773 target_type = build_reference_type (target_type);
4774 is_reference = 1;
4776 else
4778 if (complain)
4779 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
4780 DECL_NAME (OVL_FUNCTION (overload)), target_type);
4781 return error_mark_node;
4784 /* If we can find a non-template function that matches, we can just
4785 use it. There's no point in generating template instantiations
4786 if we're just going to throw them out anyhow. But, of course, we
4787 can only do this when we don't *need* a template function. */
4788 if (!template_only)
4790 tree fns;
4792 for (fns = overload; fns; fns = OVL_CHAIN (fns))
4794 tree fn = OVL_FUNCTION (fns);
4795 tree fntype;
4797 if (TREE_CODE (fn) == TEMPLATE_DECL)
4798 /* We're not looking for templates just yet. */
4799 continue;
4801 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4802 != is_ptrmem)
4803 /* We're looking for a non-static member, and this isn't
4804 one, or vice versa. */
4805 continue;
4807 /* See if there's a match. */
4808 fntype = TREE_TYPE (fn);
4809 if (is_ptrmem)
4810 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
4811 else if (!is_reference)
4812 fntype = build_pointer_type (fntype);
4814 if (can_convert_arg (target_type, fntype, fn))
4815 matches = tree_cons (fn, NULL_TREE, matches);
4819 /* Now, if we've already got a match (or matches), there's no need
4820 to proceed to the template functions. But, if we don't have a
4821 match we need to look at them, too. */
4822 if (!matches)
4824 tree target_fn_type;
4825 tree target_arg_types;
4826 tree fns;
4828 if (is_ptrmem)
4829 target_fn_type
4830 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
4831 else
4832 target_fn_type = TREE_TYPE (target_type);
4833 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
4835 for (fns = overload; fns; fns = OVL_CHAIN (fns))
4837 tree fn = OVL_FUNCTION (fns);
4838 tree instantiation;
4839 tree instantiation_type;
4840 tree targs;
4842 if (TREE_CODE (fn) != TEMPLATE_DECL)
4843 /* We're only looking for templates. */
4844 continue;
4846 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4847 != is_ptrmem)
4848 /* We're not looking for a non-static member, and this is
4849 one, or vice versa. */
4850 continue;
4852 /* Try to do argument deduction. */
4853 targs = make_tree_vec (DECL_NTPARMS (fn));
4854 if (fn_type_unification (fn, explicit_targs, targs,
4855 target_arg_types, NULL_TREE,
4856 DEDUCE_EXACT) != 0)
4857 /* Argument deduction failed. */
4858 continue;
4860 /* Instantiate the template. */
4861 instantiation = instantiate_template (fn, targs);
4862 if (instantiation == error_mark_node)
4863 /* Instantiation failed. */
4864 continue;
4866 /* See if there's a match. */
4867 instantiation_type = TREE_TYPE (instantiation);
4868 if (is_ptrmem)
4869 instantiation_type =
4870 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
4871 else if (!is_reference)
4872 instantiation_type = build_pointer_type (instantiation_type);
4873 if (can_convert_arg (target_type, instantiation_type, instantiation))
4874 matches = tree_cons (instantiation, fn, matches);
4877 /* Now, remove all but the most specialized of the matches. */
4878 if (matches)
4880 tree match = most_specialized_instantiation (matches,
4881 explicit_targs);
4883 if (match != error_mark_node)
4884 matches = tree_cons (match, NULL_TREE, NULL_TREE);
4888 /* Now we should have exactly one function in MATCHES. */
4889 if (matches == NULL_TREE)
4891 /* There were *no* matches. */
4892 if (complain)
4894 cp_error ("no matches converting function `%D' to type `%#T'",
4895 DECL_NAME (OVL_FUNCTION (overload)),
4896 target_type);
4898 /* print_candidates expects a chain with the functions in
4899 TREE_VALUE slots, so we cons one up here (we're losing anyway,
4900 so why be clever?). */
4901 for (; overload; overload = OVL_NEXT (overload))
4902 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
4903 matches);
4905 print_candidates (matches);
4907 return error_mark_node;
4909 else if (TREE_CHAIN (matches))
4911 /* There were too many matches. */
4913 if (complain)
4915 tree match;
4917 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
4918 DECL_NAME (OVL_FUNCTION (overload)),
4919 target_type);
4921 /* Since print_candidates expects the functions in the
4922 TREE_VALUE slot, we flip them here. */
4923 for (match = matches; match; match = TREE_CHAIN (match))
4924 TREE_VALUE (match) = TREE_PURPOSE (match);
4926 print_candidates (matches);
4929 return error_mark_node;
4932 /* Good, exactly one match. Now, convert it to the correct type. */
4933 fn = TREE_PURPOSE (matches);
4935 mark_used (fn);
4937 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
4938 return build_unary_op (ADDR_EXPR, fn, 0);
4939 else
4941 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
4942 will mark the function as addressed, but here we must do it
4943 explicitly. */
4944 mark_addressable (fn);
4946 return fn;
4950 /* This function will instantiate the type of the expression given in
4951 RHS to match the type of LHSTYPE. If errors exist, then return
4952 error_mark_node. We only complain is COMPLAIN is set. If we are
4953 not complaining, never modify rhs, as overload resolution wants to
4954 try many possible instantiations, in hopes that at least one will
4955 work.
4957 FLAGS is a bitmask, as we see at the top of the function.
4959 For non-recursive calls, LHSTYPE should be a function, pointer to
4960 function, or a pointer to member function. */
4962 tree
4963 instantiate_type (lhstype, rhs, flags)
4964 tree lhstype, rhs;
4965 int flags;
4967 int complain = (flags & 1);
4968 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
4970 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
4972 if (complain)
4973 error ("not enough type information");
4974 return error_mark_node;
4977 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
4979 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
4980 return rhs;
4981 if (complain)
4982 cp_error ("argument of type `%T' does not match `%T'",
4983 TREE_TYPE (rhs), lhstype);
4984 return error_mark_node;
4987 /* We don't overwrite rhs if it is an overloaded function.
4988 Copying it would destroy the tree link. */
4989 if (TREE_CODE (rhs) != OVERLOAD)
4990 rhs = copy_node (rhs);
4992 /* This should really only be used when attempting to distinguish
4993 what sort of a pointer to function we have. For now, any
4994 arithmetic operation which is not supported on pointers
4995 is rejected as an error. */
4997 switch (TREE_CODE (rhs))
4999 case TYPE_EXPR:
5000 case CONVERT_EXPR:
5001 case SAVE_EXPR:
5002 case CONSTRUCTOR:
5003 case BUFFER_REF:
5004 my_friendly_abort (177);
5005 return error_mark_node;
5007 case INDIRECT_REF:
5008 case ARRAY_REF:
5010 tree new_rhs;
5012 new_rhs = instantiate_type (build_pointer_type (lhstype),
5013 TREE_OPERAND (rhs, 0), flags);
5014 if (new_rhs == error_mark_node)
5015 return error_mark_node;
5017 TREE_TYPE (rhs) = lhstype;
5018 TREE_OPERAND (rhs, 0) = new_rhs;
5019 return rhs;
5022 case NOP_EXPR:
5023 rhs = copy_node (TREE_OPERAND (rhs, 0));
5024 TREE_TYPE (rhs) = unknown_type_node;
5025 return instantiate_type (lhstype, rhs, flags);
5027 case COMPONENT_REF:
5029 tree r = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5031 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype)
5032 && complain && !flag_ms_extensions)
5034 /* Note: we check this after the recursive call to avoid
5035 complaining about cases where overload resolution fails. */
5037 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
5038 tree fn = PTRMEM_CST_MEMBER (r);
5040 my_friendly_assert (TREE_CODE (r) == PTRMEM_CST, 990811);
5042 cp_pedwarn
5043 ("object-dependent reference to `%E' can only be used in a call",
5044 DECL_NAME (fn));
5045 cp_pedwarn
5046 (" to form a pointer to member function, say `&%T::%E'",
5047 t, DECL_NAME (fn));
5050 return r;
5053 case OFFSET_REF:
5054 rhs = TREE_OPERAND (rhs, 1);
5055 if (BASELINK_P (rhs))
5056 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5058 /* This can happen if we are forming a pointer-to-member for a
5059 member template. */
5060 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
5062 /* Fall through. */
5064 case TEMPLATE_ID_EXPR:
5065 return
5066 resolve_address_of_overloaded_function (lhstype,
5067 TREE_OPERAND (rhs, 0),
5068 complain,
5069 /*template_only=*/1,
5070 TREE_OPERAND (rhs, 1));
5072 case OVERLOAD:
5073 return
5074 resolve_address_of_overloaded_function (lhstype,
5075 rhs,
5076 complain,
5077 /*template_only=*/0,
5078 /*explicit_targs=*/NULL_TREE);
5080 case TREE_LIST:
5081 /* Now we should have a baselink. */
5082 my_friendly_assert (BASELINK_P (rhs), 990412);
5084 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5086 case CALL_EXPR:
5087 /* This is too hard for now. */
5088 my_friendly_abort (183);
5089 return error_mark_node;
5091 case PLUS_EXPR:
5092 case MINUS_EXPR:
5093 case COMPOUND_EXPR:
5094 TREE_OPERAND (rhs, 0)
5095 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5096 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5097 return error_mark_node;
5098 TREE_OPERAND (rhs, 1)
5099 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5100 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5101 return error_mark_node;
5103 TREE_TYPE (rhs) = lhstype;
5104 return rhs;
5106 case MULT_EXPR:
5107 case TRUNC_DIV_EXPR:
5108 case FLOOR_DIV_EXPR:
5109 case CEIL_DIV_EXPR:
5110 case ROUND_DIV_EXPR:
5111 case RDIV_EXPR:
5112 case TRUNC_MOD_EXPR:
5113 case FLOOR_MOD_EXPR:
5114 case CEIL_MOD_EXPR:
5115 case ROUND_MOD_EXPR:
5116 case FIX_ROUND_EXPR:
5117 case FIX_FLOOR_EXPR:
5118 case FIX_CEIL_EXPR:
5119 case FIX_TRUNC_EXPR:
5120 case FLOAT_EXPR:
5121 case NEGATE_EXPR:
5122 case ABS_EXPR:
5123 case MAX_EXPR:
5124 case MIN_EXPR:
5125 case FFS_EXPR:
5127 case BIT_AND_EXPR:
5128 case BIT_IOR_EXPR:
5129 case BIT_XOR_EXPR:
5130 case LSHIFT_EXPR:
5131 case RSHIFT_EXPR:
5132 case LROTATE_EXPR:
5133 case RROTATE_EXPR:
5135 case PREINCREMENT_EXPR:
5136 case PREDECREMENT_EXPR:
5137 case POSTINCREMENT_EXPR:
5138 case POSTDECREMENT_EXPR:
5139 if (complain)
5140 error ("invalid operation on uninstantiated type");
5141 return error_mark_node;
5143 case TRUTH_AND_EXPR:
5144 case TRUTH_OR_EXPR:
5145 case TRUTH_XOR_EXPR:
5146 case LT_EXPR:
5147 case LE_EXPR:
5148 case GT_EXPR:
5149 case GE_EXPR:
5150 case EQ_EXPR:
5151 case NE_EXPR:
5152 case TRUTH_ANDIF_EXPR:
5153 case TRUTH_ORIF_EXPR:
5154 case TRUTH_NOT_EXPR:
5155 if (complain)
5156 error ("not enough type information");
5157 return error_mark_node;
5159 case COND_EXPR:
5160 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
5162 if (complain)
5163 error ("not enough type information");
5164 return error_mark_node;
5166 TREE_OPERAND (rhs, 1)
5167 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5168 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5169 return error_mark_node;
5170 TREE_OPERAND (rhs, 2)
5171 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
5172 if (TREE_OPERAND (rhs, 2) == error_mark_node)
5173 return error_mark_node;
5175 TREE_TYPE (rhs) = lhstype;
5176 return rhs;
5178 case MODIFY_EXPR:
5179 TREE_OPERAND (rhs, 1)
5180 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5181 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5182 return error_mark_node;
5184 TREE_TYPE (rhs) = lhstype;
5185 return rhs;
5187 case ADDR_EXPR:
5188 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5190 case ENTRY_VALUE_EXPR:
5191 my_friendly_abort (184);
5192 return error_mark_node;
5194 case ERROR_MARK:
5195 return error_mark_node;
5197 default:
5198 my_friendly_abort (185);
5199 return error_mark_node;
5203 /* Return the name of the virtual function pointer field
5204 (as an IDENTIFIER_NODE) for the given TYPE. Note that
5205 this may have to look back through base types to find the
5206 ultimate field name. (For single inheritance, these could
5207 all be the same name. Who knows for multiple inheritance). */
5209 static tree
5210 get_vfield_name (type)
5211 tree type;
5213 tree binfo = TYPE_BINFO (type);
5214 char *buf;
5216 while (BINFO_BASETYPES (binfo)
5217 && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
5218 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
5219 binfo = BINFO_BASETYPE (binfo, 0);
5221 type = BINFO_TYPE (binfo);
5222 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
5223 + TYPE_NAME_LENGTH (type) + 2);
5224 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
5225 return get_identifier (buf);
5228 void
5229 print_class_statistics ()
5231 #ifdef GATHER_STATISTICS
5232 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
5233 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
5234 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
5235 n_build_method_call, n_inner_fields_searched);
5236 if (n_vtables)
5238 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
5239 n_vtables, n_vtable_searches);
5240 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
5241 n_vtable_entries, n_vtable_elems);
5243 #endif
5246 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
5247 according to [class]:
5248 The class-name is also inserted
5249 into the scope of the class itself. For purposes of access checking,
5250 the inserted class name is treated as if it were a public member name. */
5252 void
5253 build_self_reference ()
5255 tree name = constructor_name (current_class_type);
5256 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
5257 tree saved_cas;
5259 DECL_NONLOCAL (value) = 1;
5260 DECL_CONTEXT (value) = current_class_type;
5261 DECL_CLASS_CONTEXT (value) = current_class_type;
5262 DECL_ARTIFICIAL (value) = 1;
5264 if (processing_template_decl)
5265 value = push_template_decl (value);
5267 saved_cas = current_access_specifier;
5268 current_access_specifier = access_public_node;
5269 finish_member_declaration (value);
5270 current_access_specifier = saved_cas;
5273 /* Returns 1 if TYPE contains only padding bytes. */
5276 is_empty_class (type)
5277 tree type;
5279 tree t;
5281 if (type == error_mark_node)
5282 return 0;
5284 if (! IS_AGGR_TYPE (type))
5285 return 0;
5287 if (flag_new_abi)
5288 return CLASSTYPE_SIZE (type) == integer_zero_node;
5290 if (TYPE_BINFO_BASETYPES (type))
5291 return 0;
5292 t = TYPE_FIELDS (type);
5293 while (t && TREE_CODE (t) != FIELD_DECL)
5294 t = TREE_CHAIN (t);
5295 return (t == NULL_TREE);
5298 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
5299 a *_TYPE node. NODE can also be a local class. */
5301 tree
5302 get_enclosing_class (type)
5303 tree type;
5305 tree node = type;
5307 while (node && TREE_CODE (node) != NAMESPACE_DECL)
5309 switch (TREE_CODE_CLASS (TREE_CODE (node)))
5311 case 'd':
5312 node = DECL_CONTEXT (node);
5313 break;
5315 case 't':
5316 if (node != type)
5317 return node;
5318 node = TYPE_CONTEXT (node);
5319 break;
5321 default:
5322 my_friendly_abort (0);
5325 return NULL_TREE;
5328 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
5331 is_base_of_enclosing_class (base, type)
5332 tree base, type;
5334 while (type)
5336 if (get_binfo (base, type, 0))
5337 return 1;
5339 type = get_enclosing_class (type);
5341 return 0;
5344 /* Note that NAME was looked up while the current class was being
5345 defined and that the result of that lookup was DECL. */
5347 void
5348 maybe_note_name_used_in_class (name, decl)
5349 tree name;
5350 tree decl;
5352 splay_tree names_used;
5354 /* If we're not defining a class, there's nothing to do. */
5355 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
5356 return;
5358 /* If there's already a binding for this NAME, then we don't have
5359 anything to worry about. */
5360 if (IDENTIFIER_CLASS_VALUE (name))
5361 return;
5363 if (!current_class_stack[current_class_depth - 1].names_used)
5364 current_class_stack[current_class_depth - 1].names_used
5365 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
5366 names_used = current_class_stack[current_class_depth - 1].names_used;
5368 splay_tree_insert (names_used,
5369 (splay_tree_key) name,
5370 (splay_tree_value) decl);
5373 /* Note that NAME was declared (as DECL) in the current class. Check
5374 to see that the declaration is legal. */
5376 void
5377 note_name_declared_in_class (name, decl)
5378 tree name;
5379 tree decl;
5381 splay_tree names_used;
5382 splay_tree_node n;
5384 /* Look to see if we ever used this name. */
5385 names_used
5386 = current_class_stack[current_class_depth - 1].names_used;
5387 if (!names_used)
5388 return;
5390 n = splay_tree_lookup (names_used, (splay_tree_key) name);
5391 if (n)
5393 /* [basic.scope.class]
5395 A name N used in a class S shall refer to the same declaration
5396 in its context and when re-evaluated in the completed scope of
5397 S. */
5398 cp_error ("declaration of `%#D'", decl);
5399 cp_error_at ("changes meaning of `%s' from `%+#D'",
5400 IDENTIFIER_POINTER (DECL_NAME (decl)),
5401 (tree) n->value);