class.c (class_cache_obstack, [...]): Remove.
[gcc.git] / gcc / cp / class.c
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)
4
5 This file is part of GNU CC.
6
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.
11
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.
16
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. */
21
22
23 /* High-level class interface. */
24
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
36 #include "obstack.h"
37 #define obstack_chunk_alloc xmalloc
38 #define obstack_chunk_free free
39
40 /* This is how we tell when two virtual member functions are really the
41 same. */
42 #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
43
44 extern void set_class_shadows PROTO ((tree));
45
46 /* The number of nested classes being processed. If we are not in the
47 scope of any class, this is zero. */
48
49 int current_class_depth;
50
51 /* In order to deal with nested classes, we keep a stack of classes.
52 The topmost entry is the innermost class, and is the entry at index
53 CURRENT_CLASS_DEPTH */
54
55 typedef struct class_stack_node {
56 /* The name of the class. */
57 tree name;
58
59 /* The _TYPE node for the class. */
60 tree type;
61
62 /* The access specifier pending for new declarations in the scope of
63 this class. */
64 tree access;
65
66 /* If were defining TYPE, the names used in this class. */
67 splay_tree names_used;
68 }* class_stack_node_t;
69
70 /* The stack itself. This is an dynamically resized array. The
71 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
72 static int current_class_stack_size;
73 static class_stack_node_t current_class_stack;
74
75 struct base_info;
76
77 static tree get_vfield_name PROTO((tree));
78 static void finish_struct_anon PROTO((tree));
79 static tree build_vbase_pointer PROTO((tree, tree));
80 static tree build_vtable_entry PROTO((tree, tree));
81 static tree get_vtable_name PROTO((tree));
82 static tree get_derived_offset PROTO((tree, tree));
83 static tree get_basefndecls PROTO((tree, tree));
84 static void set_rtti_entry PROTO((tree, tree, tree));
85 static tree build_vtable PROTO((tree, tree));
86 static void prepare_fresh_vtable PROTO((tree, tree));
87 static void fixup_vtable_deltas1 PROTO((tree, tree));
88 static void fixup_vtable_deltas PROTO((tree, int, tree));
89 static void finish_vtbls PROTO((tree, int, tree));
90 static void modify_vtable_entry PROTO((tree, tree, tree));
91 static tree get_vtable_entry_n PROTO((tree, unsigned HOST_WIDE_INT));
92 static void add_virtual_function PROTO((tree *, tree *, int *, tree, tree));
93 static tree delete_duplicate_fields_1 PROTO((tree, tree));
94 static void delete_duplicate_fields PROTO((tree));
95 static void finish_struct_bits PROTO((tree, int));
96 static int alter_access PROTO((tree, tree, tree, tree));
97 static void handle_using_decl PROTO((tree, tree, tree, tree));
98 static int overrides PROTO((tree, tree));
99 static int strictly_overrides PROTO((tree, tree));
100 static void merge_overrides PROTO((tree, tree, int, tree));
101 static void override_one_vtable PROTO((tree, tree, tree));
102 static void mark_overriders PROTO((tree, tree));
103 static void check_for_override PROTO((tree, tree));
104 static tree get_class_offset_1 PROTO((tree, tree, tree, tree, tree));
105 static tree get_class_offset PROTO((tree, tree, tree, tree));
106 static void modify_one_vtable PROTO((tree, tree, tree));
107 static void modify_all_vtables PROTO((tree, tree));
108 static void modify_all_direct_vtables PROTO((tree, int, tree, tree));
109 static void modify_all_indirect_vtables PROTO((tree, int, int, tree, tree));
110 static int finish_base_struct PROTO((tree, struct base_info *));
111 static void finish_struct_methods PROTO((tree));
112 static void maybe_warn_about_overly_private_class PROTO ((tree));
113 static int field_decl_cmp PROTO ((const tree *, const tree *));
114 static int method_name_cmp PROTO ((const tree *, const tree *));
115 static tree add_implicitly_declared_members PROTO((tree, int, int, int));
116 static tree fixed_type_or_null PROTO((tree, int *));
117 static tree resolve_address_of_overloaded_function PROTO((tree, tree, int,
118 int, tree));
119 static void build_vtable_entry_ref PROTO((tree, tree, tree));
120 static tree build_vtable_entry_for_fn PROTO((tree, tree));
121 static tree build_vtbl_initializer PROTO((tree));
122 static int count_fields PROTO((tree));
123 static int add_fields_to_vec PROTO((tree, tree, int));
124
125 /* Variables shared between class.c and call.c. */
126
127 #ifdef GATHER_STATISTICS
128 int n_vtables = 0;
129 int n_vtable_entries = 0;
130 int n_vtable_searches = 0;
131 int n_vtable_elems = 0;
132 int n_convert_harshness = 0;
133 int n_compute_conversion_costs = 0;
134 int n_build_method_call = 0;
135 int n_inner_fields_searched = 0;
136 #endif
137
138 /* Virtual baseclass things. */
139
140 static tree
141 build_vbase_pointer (exp, type)
142 tree exp, type;
143 {
144 char *name;
145 FORMAT_VBASE_NAME (name, type);
146
147 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
148 }
149
150 #if 0
151 /* Is the type of the EXPR, the complete type of the object?
152 If we are going to be wrong, we must be conservative, and return 0. */
153
154 static int
155 complete_type_p (expr)
156 tree expr;
157 {
158 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
159 while (1)
160 {
161 switch (TREE_CODE (expr))
162 {
163 case SAVE_EXPR:
164 case INDIRECT_REF:
165 case ADDR_EXPR:
166 case NOP_EXPR:
167 case CONVERT_EXPR:
168 expr = TREE_OPERAND (expr, 0);
169 continue;
170
171 case CALL_EXPR:
172 if (! TREE_HAS_CONSTRUCTOR (expr))
173 break;
174 /* fall through... */
175 case VAR_DECL:
176 case FIELD_DECL:
177 if (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
178 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr)))
179 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
180 return 1;
181 /* fall through... */
182 case TARGET_EXPR:
183 case PARM_DECL:
184 if (IS_AGGR_TYPE (TREE_TYPE (expr))
185 && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
186 return 1;
187 /* fall through... */
188 case PLUS_EXPR:
189 default:
190 break;
191 }
192 break;
193 }
194 return 0;
195 }
196 #endif
197
198 /* Build multi-level access to EXPR using hierarchy path PATH.
199 CODE is PLUS_EXPR if we are going with the grain,
200 and MINUS_EXPR if we are not (in which case, we cannot traverse
201 virtual baseclass links).
202
203 TYPE is the type we want this path to have on exit.
204
205 NONNULL is non-zero if we know (for any reason) that EXPR is
206 not, in fact, zero. */
207
208 tree
209 build_vbase_path (code, type, expr, path, nonnull)
210 enum tree_code code;
211 tree type, expr, path;
212 int nonnull;
213 {
214 register int changed = 0;
215 tree last = NULL_TREE, last_virtual = NULL_TREE;
216 int fixed_type_p;
217 tree null_expr = 0, nonnull_expr;
218 tree basetype;
219 tree offset = integer_zero_node;
220
221 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
222 return build1 (NOP_EXPR, type, expr);
223
224 /* We could do better if we had additional logic to convert back to the
225 unconverted type (the static type of the complete object), and then
226 convert back to the type we want. Until that is done, we only optimize
227 if the complete type is the same type as expr has. */
228 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
229
230 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
231 expr = save_expr (expr);
232 nonnull_expr = expr;
233
234 if (BINFO_INHERITANCE_CHAIN (path))
235 path = reverse_path (path);
236
237 basetype = BINFO_TYPE (path);
238
239 while (path)
240 {
241 if (TREE_VIA_VIRTUAL (path))
242 {
243 last_virtual = BINFO_TYPE (path);
244 if (code == PLUS_EXPR)
245 {
246 changed = ! fixed_type_p;
247
248 if (changed)
249 {
250 tree ind;
251
252 /* We already check for ambiguous things in the caller, just
253 find a path. */
254 if (last)
255 {
256 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
257 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
258 }
259 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
260 nonnull_expr = build_vbase_pointer (ind, last_virtual);
261 if (nonnull == 0
262 && TREE_CODE (type) == POINTER_TYPE
263 && null_expr == NULL_TREE)
264 {
265 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
266 expr = build (COND_EXPR, build_pointer_type (last_virtual),
267 build (EQ_EXPR, boolean_type_node, expr,
268 integer_zero_node),
269 null_expr, nonnull_expr);
270 }
271 }
272 /* else we'll figure out the offset below. */
273
274 /* Happens in the case of parse errors. */
275 if (nonnull_expr == error_mark_node)
276 return error_mark_node;
277 }
278 else
279 {
280 cp_error ("cannot cast up from virtual baseclass `%T'",
281 last_virtual);
282 return error_mark_node;
283 }
284 }
285 last = path;
286 path = BINFO_INHERITANCE_CHAIN (path);
287 }
288 /* LAST is now the last basetype assoc on the path. */
289
290 /* A pointer to a virtual base member of a non-null object
291 is non-null. Therefore, we only need to test for zeroness once.
292 Make EXPR the canonical expression to deal with here. */
293 if (null_expr)
294 {
295 TREE_OPERAND (expr, 2) = nonnull_expr;
296 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
297 = TREE_TYPE (nonnull_expr);
298 }
299 else
300 expr = nonnull_expr;
301
302 /* If we go through any virtual base pointers, make sure that
303 casts to BASETYPE from the last virtual base class use
304 the right value for BASETYPE. */
305 if (changed)
306 {
307 tree intype = TREE_TYPE (TREE_TYPE (expr));
308 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
309 {
310 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
311 offset = BINFO_OFFSET (binfo);
312 }
313 }
314 else
315 {
316 if (last_virtual)
317 {
318 offset = BINFO_OFFSET (binfo_member (last_virtual,
319 CLASSTYPE_VBASECLASSES (basetype)));
320 offset = size_binop (PLUS_EXPR, offset, BINFO_OFFSET (last));
321 }
322 else
323 offset = BINFO_OFFSET (last);
324 }
325
326 if (TREE_INT_CST_LOW (offset))
327 {
328 /* Bash types to make the backend happy. */
329 offset = cp_convert (type, offset);
330 #if 0
331 /* This shouldn't be necessary. (mrs) */
332 expr = build1 (NOP_EXPR, type, expr);
333 #endif
334
335 /* If expr might be 0, we need to preserve that zeroness. */
336 if (nonnull == 0)
337 {
338 if (null_expr)
339 TREE_TYPE (null_expr) = type;
340 else
341 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
342 if (TREE_SIDE_EFFECTS (expr))
343 expr = save_expr (expr);
344
345 return build (COND_EXPR, type,
346 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
347 null_expr,
348 build (code, type, expr, offset));
349 }
350 else return build (code, type, expr, offset);
351 }
352
353 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
354 be used multiple times in initialization of multiple inheritance. */
355 if (null_expr)
356 {
357 TREE_TYPE (expr) = type;
358 return expr;
359 }
360 else
361 return build1 (NOP_EXPR, type, expr);
362 }
363
364 /* Virtual function things. */
365
366 /* Build an entry in the virtual function table.
367 DELTA is the offset for the `this' pointer.
368 PFN is an ADDR_EXPR containing a pointer to the virtual function.
369 Note that the index (DELTA2) in the virtual function table
370 is always 0. */
371
372 static tree
373 build_vtable_entry (delta, pfn)
374 tree delta, pfn;
375 {
376 if (flag_vtable_thunks)
377 {
378 HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta);
379 if (idelta && ! DECL_ABSTRACT_VIRTUAL_P (TREE_OPERAND (pfn, 0)))
380 {
381 pfn = build1 (ADDR_EXPR, vtable_entry_type,
382 make_thunk (pfn, idelta));
383 TREE_READONLY (pfn) = 1;
384 TREE_CONSTANT (pfn) = 1;
385 }
386 #ifdef GATHER_STATISTICS
387 n_vtable_entries += 1;
388 #endif
389 return pfn;
390 }
391 else
392 {
393 extern int flag_huge_objects;
394 tree elems = tree_cons (NULL_TREE, delta,
395 tree_cons (NULL_TREE, integer_zero_node,
396 build_expr_list (NULL_TREE, pfn)));
397 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
398
399 /* DELTA used to be constructed by `size_int' and/or size_binop,
400 which caused overflow problems when it was negative. That should
401 be fixed now. */
402
403 if (! int_fits_type_p (delta, delta_type_node))
404 {
405 if (flag_huge_objects)
406 sorry ("object size exceeds built-in limit for virtual function table implementation");
407 else
408 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
409 }
410
411 TREE_CONSTANT (entry) = 1;
412 TREE_STATIC (entry) = 1;
413 TREE_READONLY (entry) = 1;
414
415 #ifdef GATHER_STATISTICS
416 n_vtable_entries += 1;
417 #endif
418
419 return entry;
420 }
421 }
422
423 /* Build a vtable entry for FNDECL. DELTA is the amount by which we
424 must adjust the this pointer when calling F. */
425
426 static tree
427 build_vtable_entry_for_fn (delta, fndecl)
428 tree delta;
429 tree fndecl;
430 {
431 tree pfn;
432
433 /* Take the address of the function, considering it to be of an
434 appropriate generic type. */
435 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
436 /* The address of a function can't change. */
437 TREE_CONSTANT (pfn) = 1;
438 /* Now build the vtable entry itself. */
439 return build_vtable_entry (delta, pfn);
440 }
441
442 /* We want to give the assembler the vtable identifier as well as
443 the offset to the function pointer. So we generate
444
445 __asm__ __volatile__ (".vtable_entry %c0, %c1"
446 : : "s"(&class_vtable),
447 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
448
449 static void
450 build_vtable_entry_ref (basetype, vtbl, idx)
451 tree basetype, vtbl, idx;
452 {
453 static char asm_stmt[] = ".vtable_entry %c0, %c1";
454 tree s, i, i2;
455
456 s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
457 s = build_tree_list (build_string (1, "s"), s);
458
459 i = build_array_ref (vtbl, idx);
460 if (!flag_vtable_thunks)
461 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
462 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
463 i2 = build_array_ref (vtbl, build_int_2(0,0));
464 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
465 i = build_binary_op (MINUS_EXPR, i, i2);
466 i = build_tree_list (build_string (1, "i"), i);
467
468 expand_asm_operands (build_string (sizeof(asm_stmt)-1, asm_stmt),
469 NULL_TREE, chainon (s, i), NULL_TREE, 1, NULL, 0);
470 }
471
472 /* Given an object INSTANCE, return an expression which yields the
473 virtual function vtable element corresponding to INDEX. There are
474 many special cases for INSTANCE which we take care of here, mainly
475 to avoid creating extra tree nodes when we don't have to. */
476
477 tree
478 build_vtbl_ref (instance, idx)
479 tree instance, idx;
480 {
481 tree vtbl, aref;
482 tree basetype = TREE_TYPE (instance);
483
484 if (TREE_CODE (basetype) == REFERENCE_TYPE)
485 basetype = TREE_TYPE (basetype);
486
487 if (instance == current_class_ref)
488 vtbl = build_vfield_ref (instance, basetype);
489 else
490 {
491 if (optimize)
492 {
493 /* Try to figure out what a reference refers to, and
494 access its virtual function table directly. */
495 tree ref = NULL_TREE;
496
497 if (TREE_CODE (instance) == INDIRECT_REF
498 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
499 ref = TREE_OPERAND (instance, 0);
500 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
501 ref = instance;
502
503 if (ref && TREE_CODE (ref) == VAR_DECL
504 && DECL_INITIAL (ref))
505 {
506 tree init = DECL_INITIAL (ref);
507
508 while (TREE_CODE (init) == NOP_EXPR
509 || TREE_CODE (init) == NON_LVALUE_EXPR)
510 init = TREE_OPERAND (init, 0);
511 if (TREE_CODE (init) == ADDR_EXPR)
512 {
513 init = TREE_OPERAND (init, 0);
514 if (IS_AGGR_TYPE (TREE_TYPE (init))
515 && (TREE_CODE (init) == PARM_DECL
516 || TREE_CODE (init) == VAR_DECL))
517 instance = init;
518 }
519 }
520 }
521
522 if (IS_AGGR_TYPE (TREE_TYPE (instance))
523 && (TREE_CODE (instance) == RESULT_DECL
524 || TREE_CODE (instance) == PARM_DECL
525 || TREE_CODE (instance) == VAR_DECL))
526 vtbl = TYPE_BINFO_VTABLE (basetype);
527 else
528 vtbl = build_vfield_ref (instance, basetype);
529 }
530
531 assemble_external (vtbl);
532
533 if (flag_vtable_gc)
534 build_vtable_entry_ref (basetype, vtbl, idx);
535
536 aref = build_array_ref (vtbl, idx);
537
538 return aref;
539 }
540
541 /* Given an object INSTANCE, return an expression which yields the
542 virtual function corresponding to INDEX. There are many special
543 cases for INSTANCE which we take care of here, mainly to avoid
544 creating extra tree nodes when we don't have to. */
545
546 tree
547 build_vfn_ref (ptr_to_instptr, instance, idx)
548 tree *ptr_to_instptr, instance;
549 tree idx;
550 {
551 tree aref = build_vtbl_ref (instance, idx);
552
553 /* When using thunks, there is no extra delta, and we get the pfn
554 directly. */
555 if (flag_vtable_thunks)
556 return aref;
557
558 if (ptr_to_instptr)
559 {
560 /* Save the intermediate result in a SAVE_EXPR so we don't have to
561 compute each component of the virtual function pointer twice. */
562 if (TREE_CODE (aref) == INDIRECT_REF)
563 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
564
565 *ptr_to_instptr
566 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
567 *ptr_to_instptr,
568 cp_convert (ptrdiff_type_node,
569 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
570 }
571
572 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
573 }
574
575 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
576 for the given TYPE. */
577
578 static tree
579 get_vtable_name (type)
580 tree type;
581 {
582 tree type_id = build_typename_overload (type);
583 char *buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
584 + IDENTIFIER_LENGTH (type_id) + 2);
585 const char *ptr = IDENTIFIER_POINTER (type_id);
586 int i;
587 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
588 #if 0
589 /* We don't take off the numbers; prepare_fresh_vtable uses the
590 DECL_ASSEMBLER_NAME for the type, which includes the number
591 in `3foo'. If we were to pull them off here, we'd end up with
592 something like `_vt.foo.3bar', instead of a uniform definition. */
593 while (ptr[i] >= '0' && ptr[i] <= '9')
594 i += 1;
595 #endif
596 sprintf (buf, VTABLE_NAME_FORMAT, ptr+i);
597 return get_identifier (buf);
598 }
599
600 /* Return the offset to the main vtable for a given base BINFO. */
601
602 tree
603 get_vfield_offset (binfo)
604 tree binfo;
605 {
606 tree tmp
607 = size_binop (FLOOR_DIV_EXPR,
608 DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))),
609 size_int (BITS_PER_UNIT));
610 tmp = convert (sizetype, tmp);
611 return size_binop (PLUS_EXPR, tmp, BINFO_OFFSET (binfo));
612 }
613
614 /* Get the offset to the start of the original binfo that we derived
615 this binfo from. If we find TYPE first, return the offset only
616 that far. The shortened search is useful because the this pointer
617 on method calling is expected to point to a DECL_CONTEXT (fndecl)
618 object, and not a baseclass of it. */
619
620 static tree
621 get_derived_offset (binfo, type)
622 tree binfo, type;
623 {
624 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
625 tree offset2;
626 int i;
627 while (BINFO_BASETYPES (binfo)
628 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
629 {
630 tree binfos = BINFO_BASETYPES (binfo);
631 if (BINFO_TYPE (binfo) == type)
632 break;
633 binfo = TREE_VEC_ELT (binfos, i);
634 }
635 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
636 return size_binop (MINUS_EXPR, offset1, offset2);
637 }
638
639 /* Update the rtti info for this class. */
640
641 static void
642 set_rtti_entry (virtuals, offset, type)
643 tree virtuals, offset, type;
644 {
645 tree fn;
646
647 if (CLASSTYPE_COM_INTERFACE (type))
648 return;
649
650 if (flag_rtti)
651 fn = get_tinfo_fn (type);
652 else
653 /* If someone tries to get RTTI information for a type compiled
654 without RTTI, they're out of luck. By calling __pure_virtual
655 in this case, we give a small clue as to what went wrong. We
656 could consider having a __no_typeinfo function as well, for a
657 more specific hint. */
658 fn = abort_fndecl;
659
660 if (flag_vtable_thunks)
661 {
662 /* The first slot holds the offset. */
663 TREE_PURPOSE (virtuals) = offset;
664
665 /* The next node holds the function. */
666 virtuals = TREE_CHAIN (virtuals);
667 offset = integer_zero_node;
668 }
669
670 /* This slot holds the function to call. */
671 TREE_PURPOSE (virtuals) = offset;
672 TREE_VALUE (virtuals) = fn;
673 }
674
675 /* Build a virtual function for type TYPE.
676 If BINFO is non-NULL, build the vtable starting with the initial
677 approximation that it is the same as the one which is the head of
678 the association list. */
679
680 static tree
681 build_vtable (binfo, type)
682 tree binfo, type;
683 {
684 tree name = get_vtable_name (type);
685 tree virtuals, decl;
686
687 if (binfo)
688 {
689 tree offset;
690
691 virtuals = copy_list (BINFO_VIRTUALS (binfo));
692 decl = build_lang_decl (VAR_DECL, name,
693 TREE_TYPE (BINFO_VTABLE (binfo)));
694
695 /* Now do rtti stuff. */
696 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
697 offset = ssize_binop (MINUS_EXPR, integer_zero_node, offset);
698 set_rtti_entry (virtuals, offset, type);
699 }
700 else
701 {
702 virtuals = NULL_TREE;
703 decl = build_lang_decl (VAR_DECL, name, void_type_node);
704 }
705
706 #ifdef GATHER_STATISTICS
707 n_vtables += 1;
708 n_vtable_elems += list_length (virtuals);
709 #endif
710
711 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
712 import_export_vtable (decl, type, 0);
713
714 decl = pushdecl_top_level (decl);
715 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
716 /* Initialize the association list for this type, based
717 on our first approximation. */
718 TYPE_BINFO_VTABLE (type) = decl;
719 TYPE_BINFO_VIRTUALS (type) = virtuals;
720
721 DECL_ARTIFICIAL (decl) = 1;
722 TREE_STATIC (decl) = 1;
723 #ifndef WRITABLE_VTABLES
724 /* Make them READONLY by default. (mrs) */
725 TREE_READONLY (decl) = 1;
726 #endif
727 /* At one time the vtable info was grabbed 2 words at a time. This
728 fails on sparc unless you have 8-byte alignment. (tiemann) */
729 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
730 DECL_ALIGN (decl));
731
732 DECL_VIRTUAL_P (decl) = 1;
733 DECL_CONTEXT (decl) = type;
734
735 binfo = TYPE_BINFO (type);
736 SET_BINFO_NEW_VTABLE_MARKED (binfo);
737 return decl;
738 }
739
740 /* Give TYPE a new virtual function table which is initialized
741 with a skeleton-copy of its original initialization. The only
742 entry that changes is the `delta' entry, so we can really
743 share a lot of structure.
744
745 FOR_TYPE is the derived type which caused this table to
746 be needed.
747
748 BINFO is the type association which provided TYPE for FOR_TYPE.
749
750 The order in which vtables are built (by calling this function) for
751 an object must remain the same, otherwise a binary incompatibility
752 can result. */
753
754 static void
755 prepare_fresh_vtable (binfo, for_type)
756 tree binfo, for_type;
757 {
758 tree basetype;
759 tree orig_decl = BINFO_VTABLE (binfo);
760 tree name;
761 tree new_decl;
762 tree offset;
763 tree path = binfo;
764 char *buf, *buf2;
765 char joiner = '_';
766 int i;
767
768 #ifdef JOINER
769 joiner = JOINER;
770 #endif
771
772 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
773
774 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
775 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
776
777 /* We know that the vtable that we are going to create doesn't exist
778 yet in the global namespace, and when we finish, it will be
779 pushed into the global namespace. In complex MI hierarchies, we
780 have to loop while the name we are thinking of adding is globally
781 defined, adding more name components to the vtable name as we
782 loop, until the name is unique. This is because in complex MI
783 cases, we might have the same base more than once. This means
784 that the order in which this function is called for vtables must
785 remain the same, otherwise binary compatibility can be
786 compromised. */
787
788 while (1)
789 {
790 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
791 + 1 + i);
792 char *new_buf2;
793
794 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
795 buf2);
796 buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT) + strlen (buf1) + 1);
797 sprintf (buf, VTABLE_NAME_FORMAT, buf1);
798 name = get_identifier (buf);
799
800 /* If this name doesn't clash, then we can use it, otherwise
801 we add more to the name until it is unique. */
802
803 if (! IDENTIFIER_GLOBAL_VALUE (name))
804 break;
805
806 /* Set values for next loop through, if the name isn't unique. */
807
808 path = BINFO_INHERITANCE_CHAIN (path);
809
810 /* We better not run out of stuff to make it unique. */
811 my_friendly_assert (path != NULL_TREE, 368);
812
813 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
814
815 if (for_type == basetype)
816 {
817 /* If we run out of basetypes in the path, we have already
818 found created a vtable with that name before, we now
819 resort to tacking on _%d to distinguish them. */
820 int j = 2;
821 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
822 buf1 = (char *) alloca (i);
823 do {
824 sprintf (buf1, "%s%c%s%c%d",
825 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
826 buf2, joiner, j);
827 buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
828 + strlen (buf1) + 1);
829 sprintf (buf, VTABLE_NAME_FORMAT, buf1);
830 name = get_identifier (buf);
831
832 /* If this name doesn't clash, then we can use it,
833 otherwise we add something different to the name until
834 it is unique. */
835 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
836
837 /* Hey, they really like MI don't they? Increase the 3
838 above to 6, and the 999 to 999999. :-) */
839 my_friendly_assert (j <= 999, 369);
840
841 break;
842 }
843
844 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
845 new_buf2 = (char *) alloca (i);
846 sprintf (new_buf2, "%s%c%s",
847 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
848 buf2 = new_buf2;
849 }
850
851 new_decl = build_lang_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
852 /* Remember which class this vtable is really for. */
853 DECL_CONTEXT (new_decl) = for_type;
854
855 DECL_ARTIFICIAL (new_decl) = 1;
856 TREE_STATIC (new_decl) = 1;
857 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
858 DECL_VIRTUAL_P (new_decl) = 1;
859 #ifndef WRITABLE_VTABLES
860 /* Make them READONLY by default. (mrs) */
861 TREE_READONLY (new_decl) = 1;
862 #endif
863 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
864
865 /* Make fresh virtual list, so we can smash it later. */
866 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
867
868 if (TREE_VIA_VIRTUAL (binfo))
869 {
870 tree binfo1 = binfo_member (BINFO_TYPE (binfo),
871 CLASSTYPE_VBASECLASSES (for_type));
872
873 /* XXX - This should never happen, if it does, the caller should
874 ensure that the binfo is from for_type's binfos, not from any
875 base type's. We can remove all this code after a while. */
876 if (binfo1 != binfo)
877 warning ("internal inconsistency: binfo offset error for rtti");
878
879 offset = BINFO_OFFSET (binfo1);
880 }
881 else
882 offset = BINFO_OFFSET (binfo);
883
884 set_rtti_entry (BINFO_VIRTUALS (binfo),
885 ssize_binop (MINUS_EXPR, integer_zero_node, offset),
886 for_type);
887
888 #ifdef GATHER_STATISTICS
889 n_vtables += 1;
890 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
891 #endif
892
893 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
894 import_export_vtable (new_decl, for_type, 0);
895
896 if (TREE_VIA_VIRTUAL (binfo))
897 my_friendly_assert (binfo == binfo_member (BINFO_TYPE (binfo),
898 CLASSTYPE_VBASECLASSES (current_class_type)),
899 170);
900 SET_BINFO_NEW_VTABLE_MARKED (binfo);
901 }
902
903 #if 0
904 /* Access the virtual function table entry that logically
905 contains BASE_FNDECL. VIRTUALS is the virtual function table's
906 initializer. We can run off the end, when dealing with virtual
907 destructors in MI situations, return NULL_TREE in that case. */
908
909 static tree
910 get_vtable_entry (virtuals, base_fndecl)
911 tree virtuals, base_fndecl;
912 {
913 unsigned HOST_WIDE_INT n = (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
914 ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
915 & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1))
916 : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
917
918 #ifdef GATHER_STATISTICS
919 n_vtable_searches += n;
920 #endif
921
922 while (n > 0 && virtuals)
923 {
924 --n;
925 virtuals = TREE_CHAIN (virtuals);
926 }
927 return virtuals;
928 }
929 #endif
930
931 /* Change the offset for the FNDECL entry to NEW_OFFSET. Also update
932 DECL_VINDEX (FNDECL). */
933
934 static void
935 modify_vtable_entry (old_entry_in_list, new_offset, fndecl)
936 tree old_entry_in_list, new_offset, fndecl;
937 {
938 tree base_fndecl = TREE_VALUE (old_entry_in_list);
939
940 /* Update the entry. */
941 TREE_PURPOSE (old_entry_in_list) = new_offset;
942 TREE_VALUE (old_entry_in_list) = fndecl;
943
944 /* Now assign virtual dispatch information, if unset. We can
945 dispatch this, through any overridden base function. */
946 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
947 {
948 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
949 DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
950 }
951 }
952
953 /* Access the virtual function table entry N. VIRTUALS is the virtual
954 function table's initializer. */
955
956 static tree
957 get_vtable_entry_n (virtuals, n)
958 tree virtuals;
959 unsigned HOST_WIDE_INT n;
960 {
961 while (n > 0)
962 {
963 --n;
964 virtuals = TREE_CHAIN (virtuals);
965 }
966 return virtuals;
967 }
968
969 /* Add a virtual function to all the appropriate vtables for the class
970 T. DECL_VINDEX(X) should be error_mark_node, if we want to
971 allocate a new slot in our table. If it is error_mark_node, we
972 know that no other function from another vtable is overridden by X.
973 HAS_VIRTUAL keeps track of how many virtuals there are in our main
974 vtable for the type, and we build upon the PENDING_VIRTUALS list
975 and return it. */
976
977 static void
978 add_virtual_function (pv, phv, has_virtual, fndecl, t)
979 tree *pv, *phv;
980 int *has_virtual;
981 tree fndecl;
982 tree t; /* Structure type. */
983 {
984 tree pending_virtuals = *pv;
985 tree pending_hard_virtuals = *phv;
986
987 #ifndef DUMB_USER
988 if (current_class_type == 0)
989 cp_warning ("internal problem, current_class_type is zero when adding `%D', please report",
990 fndecl);
991 if (current_class_type && t != current_class_type)
992 cp_warning ("internal problem, current_class_type differs when adding `%D', please report",
993 fndecl);
994 #endif
995
996 /* If the virtual function is a redefinition of a prior one,
997 figure out in which base class the new definition goes,
998 and if necessary, make a fresh virtual function table
999 to hold that entry. */
1000 if (DECL_VINDEX (fndecl) == error_mark_node)
1001 {
1002 /* We remember that this was the base sub-object for rtti. */
1003 CLASSTYPE_RTTI (t) = t;
1004
1005 /* If we are using thunks, use two slots at the front, one
1006 for the offset pointer, one for the tdesc pointer.
1007 For ARM-style vtables, use the same slot for both. */
1008 if (*has_virtual == 0 && ! CLASSTYPE_COM_INTERFACE (t))
1009 {
1010 if (flag_vtable_thunks)
1011 *has_virtual = 2;
1012 else
1013 *has_virtual = 1;
1014 }
1015
1016 /* Build a new INT_CST for this DECL_VINDEX. */
1017 {
1018 static tree index_table[256];
1019 tree idx;
1020 /* We skip a slot for the offset/tdesc entry. */
1021 int i = (*has_virtual)++;
1022
1023 if (i >= 256 || index_table[i] == 0)
1024 {
1025 idx = build_int_2 (i, 0);
1026 if (i < 256)
1027 index_table[i] = idx;
1028 }
1029 else
1030 idx = index_table[i];
1031
1032 /* Now assign virtual dispatch information. */
1033 DECL_VINDEX (fndecl) = idx;
1034 DECL_CONTEXT (fndecl) = t;
1035 }
1036 /* Save the state we've computed on the PENDING_VIRTUALS list. */
1037 pending_virtuals = tree_cons (integer_zero_node,
1038 fndecl,
1039 pending_virtuals);
1040 }
1041 /* Might already be INTEGER_CST if declared twice in class. We will
1042 give error later or we've already given it. */
1043 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1044 {
1045 /* Need an entry in some other virtual function table.
1046 Deal with this after we have laid out our virtual base classes. */
1047 pending_hard_virtuals = temp_tree_cons (NULL_TREE,
1048 fndecl,
1049 pending_hard_virtuals);
1050 }
1051 *pv = pending_virtuals;
1052 *phv = pending_hard_virtuals;
1053 }
1054 \f
1055 extern struct obstack *current_obstack;
1056
1057 /* Add method METHOD to class TYPE.
1058
1059 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1060 the class type where the method should be added. */
1061
1062 void
1063 add_method (type, fields, method)
1064 tree type, *fields, method;
1065 {
1066 push_permanent_obstack ();
1067
1068 /* Setting the DECL_CONTEXT and DECL_CLASS_CONTEXT here is probably
1069 redundant. */
1070 DECL_CONTEXT (method) = type;
1071 DECL_CLASS_CONTEXT (method) = type;
1072
1073 if (fields && *fields)
1074 *fields = build_overload (method, *fields);
1075 else
1076 {
1077 int len;
1078 int slot;
1079 tree method_vec;
1080
1081 if (!CLASSTYPE_METHOD_VEC (type))
1082 /* Make a new method vector. We start with 8 entries. We must
1083 allocate at least two (for constructors and destructors), and
1084 we're going to end up with an assignment operator at some
1085 point as well.
1086
1087 We could use a TREE_LIST for now, and convert it to a
1088 TREE_VEC in finish_struct, but we would probably waste more
1089 memory making the links in the list than we would by
1090 over-allocating the size of the vector here. Furthermore,
1091 we would complicate all the code that expects this to be a
1092 vector. */
1093 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1094
1095 method_vec = CLASSTYPE_METHOD_VEC (type);
1096 len = TREE_VEC_LENGTH (method_vec);
1097
1098 if (DECL_NAME (method) == constructor_name (type))
1099 /* A new constructor or destructor. Constructors go in
1100 slot 0; destructors go in slot 1. */
1101 slot = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
1102 else
1103 {
1104 /* See if we already have an entry with this name. */
1105 for (slot = 2; slot < len; ++slot)
1106 if (!TREE_VEC_ELT (method_vec, slot)
1107 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1108 slot)))
1109 == DECL_NAME (method)))
1110 break;
1111
1112 if (slot == len)
1113 {
1114 /* We need a bigger method vector. */
1115 tree new_vec = make_tree_vec (2 * len);
1116 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1117 (PTR) &TREE_VEC_ELT (new_vec, 0),
1118 len * sizeof (tree));
1119 len = 2 * len;
1120 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1121 }
1122
1123 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1124 {
1125 /* Type conversion operators have to come before
1126 ordinary methods; add_conversions depends on this to
1127 speed up looking for conversion operators. So, if
1128 necessary, we slide some of the vector elements up.
1129 In theory, this makes this algorithm O(N^2) but we
1130 don't expect many conversion operators. */
1131 for (slot = 2; slot < len; ++slot)
1132 {
1133 tree fn = TREE_VEC_ELT (method_vec, slot);
1134
1135 if (!fn)
1136 /* There are no more entries in the vector, so we
1137 can insert the new conversion operator here. */
1138 break;
1139
1140 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1141 /* We can insert the new function right at the
1142 SLOTth position. */
1143 break;
1144 }
1145
1146 if (!TREE_VEC_ELT (method_vec, slot))
1147 /* There is nothing in the Ith slot, so we can avoid
1148 moving anything. */
1149 ;
1150 else
1151 {
1152 /* We know the last slot in the vector is empty
1153 because we know that at this point there's room
1154 for a new function. */
1155 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1156 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1157 (len - slot - 1) * sizeof (tree));
1158 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1159 }
1160 }
1161 }
1162
1163 if (template_class_depth (type))
1164 /* TYPE is a template class. Don't issue any errors now; wait
1165 until instantiation time to complain. */
1166 ;
1167 else
1168 {
1169 tree fns;
1170
1171 /* Check to see if we've already got this method. */
1172 for (fns = TREE_VEC_ELT (method_vec, slot);
1173 fns;
1174 fns = OVL_NEXT (fns))
1175 {
1176 tree fn = OVL_CURRENT (fns);
1177
1178 if (TREE_CODE (fn) != TREE_CODE (method))
1179 continue;
1180
1181 if (TREE_CODE (method) != TEMPLATE_DECL)
1182 {
1183 /* [over.load] Member function declarations with the
1184 same name and the same parameter types cannot be
1185 overloaded if any of them is a static member
1186 function declaration. */
1187 if (DECL_STATIC_FUNCTION_P (fn)
1188 != DECL_STATIC_FUNCTION_P (method))
1189 {
1190 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1191 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1192
1193 if (! DECL_STATIC_FUNCTION_P (fn))
1194 parms1 = TREE_CHAIN (parms1);
1195 else
1196 parms2 = TREE_CHAIN (parms2);
1197
1198 if (compparms (parms1, parms2))
1199 cp_error ("`%#D' and `%#D' cannot be overloaded",
1200 fn, method);
1201 }
1202
1203 /* Since this is an ordinary function in a
1204 non-template class, it's mangled name can be used
1205 as a unique identifier. This technique is only
1206 an optimization; we would get the same results if
1207 we just used decls_match here. */
1208 if (DECL_ASSEMBLER_NAME (fn)
1209 != DECL_ASSEMBLER_NAME (method))
1210 continue;
1211 }
1212 else if (!decls_match (fn, method))
1213 continue;
1214
1215 /* There has already been a declaration of this method
1216 or member template. */
1217 cp_error_at ("`%D' has already been declared in `%T'",
1218 method, type);
1219
1220 /* We don't call duplicate_decls here to merge the
1221 declarations because that will confuse things if the
1222 methods have inline definitions. In particular, we
1223 will crash while processing the definitions. */
1224 return;
1225 }
1226 }
1227
1228 /* Actually insert the new method. */
1229 TREE_VEC_ELT (method_vec, slot)
1230 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1231
1232 /* Add the new binding. */
1233 if (!DECL_CONSTRUCTOR_P (method)
1234 && !DECL_DESTRUCTOR_P (method))
1235 push_class_level_binding (DECL_NAME (method),
1236 TREE_VEC_ELT (method_vec, slot));
1237 }
1238 pop_obstacks ();
1239 }
1240
1241 /* Subroutines of finish_struct. */
1242
1243 /* Look through the list of fields for this struct, deleting
1244 duplicates as we go. This must be recursive to handle
1245 anonymous unions.
1246
1247 FIELD is the field which may not appear anywhere in FIELDS.
1248 FIELD_PTR, if non-null, is the starting point at which
1249 chained deletions may take place.
1250 The value returned is the first acceptable entry found
1251 in FIELDS.
1252
1253 Note that anonymous fields which are not of UNION_TYPE are
1254 not duplicates, they are just anonymous fields. This happens
1255 when we have unnamed bitfields, for example. */
1256
1257 static tree
1258 delete_duplicate_fields_1 (field, fields)
1259 tree field, fields;
1260 {
1261 tree x;
1262 tree prev = 0;
1263 if (DECL_NAME (field) == 0)
1264 {
1265 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1266 return fields;
1267
1268 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1269 fields = delete_duplicate_fields_1 (x, fields);
1270 return fields;
1271 }
1272 else
1273 {
1274 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1275 {
1276 if (DECL_NAME (x) == 0)
1277 {
1278 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1279 continue;
1280 TYPE_FIELDS (TREE_TYPE (x))
1281 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1282 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1283 {
1284 if (prev == 0)
1285 fields = TREE_CHAIN (fields);
1286 else
1287 TREE_CHAIN (prev) = TREE_CHAIN (x);
1288 }
1289 }
1290 else
1291 {
1292 if (DECL_NAME (field) == DECL_NAME (x))
1293 {
1294 if (TREE_CODE (field) == CONST_DECL
1295 && TREE_CODE (x) == CONST_DECL)
1296 cp_error_at ("duplicate enum value `%D'", x);
1297 else if (TREE_CODE (field) == CONST_DECL
1298 || TREE_CODE (x) == CONST_DECL)
1299 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1300 x);
1301 else if (DECL_DECLARES_TYPE_P (field)
1302 && DECL_DECLARES_TYPE_P (x))
1303 {
1304 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1305 continue;
1306 cp_error_at ("duplicate nested type `%D'", x);
1307 }
1308 else if (DECL_DECLARES_TYPE_P (field)
1309 || DECL_DECLARES_TYPE_P (x))
1310 {
1311 /* Hide tag decls. */
1312 if ((TREE_CODE (field) == TYPE_DECL
1313 && DECL_ARTIFICIAL (field))
1314 || (TREE_CODE (x) == TYPE_DECL
1315 && DECL_ARTIFICIAL (x)))
1316 continue;
1317 cp_error_at ("duplicate field `%D' (as type and non-type)",
1318 x);
1319 }
1320 else
1321 cp_error_at ("duplicate member `%D'", x);
1322 if (prev == 0)
1323 fields = TREE_CHAIN (fields);
1324 else
1325 TREE_CHAIN (prev) = TREE_CHAIN (x);
1326 }
1327 }
1328 }
1329 }
1330 return fields;
1331 }
1332
1333 static void
1334 delete_duplicate_fields (fields)
1335 tree fields;
1336 {
1337 tree x;
1338 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1339 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1340 }
1341
1342 /* Change the access of FDECL to ACCESS in T. The access to FDECL is
1343 along the path given by BINFO. Return 1 if change was legit,
1344 otherwise return 0. */
1345
1346 static int
1347 alter_access (t, binfo, fdecl, access)
1348 tree t;
1349 tree binfo;
1350 tree fdecl;
1351 tree access;
1352 {
1353 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1354 if (elem)
1355 {
1356 if (TREE_VALUE (elem) != access)
1357 {
1358 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1359 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1360 else
1361 error ("conflicting access specifications for field `%s', ignored",
1362 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1363 }
1364 else
1365 {
1366 /* They're changing the access to the same thing they changed
1367 it to before. That's OK. */
1368 ;
1369 }
1370 }
1371 else
1372 {
1373 enforce_access (binfo, fdecl);
1374 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1375 return 1;
1376 }
1377 return 0;
1378 }
1379
1380 /* Process the USING_DECL, which is a member of T. The METHOD_VEC, if
1381 non-NULL, is the methods of T. The FIELDS are the fields of T. */
1382
1383 static void
1384 handle_using_decl (using_decl, t, method_vec, fields)
1385 tree using_decl;
1386 tree t;
1387 tree method_vec;
1388 tree fields;
1389 {
1390 tree ctype = DECL_INITIAL (using_decl);
1391 tree name = DECL_NAME (using_decl);
1392 tree access
1393 = TREE_PRIVATE (using_decl) ? access_private_node
1394 : TREE_PROTECTED (using_decl) ? access_protected_node
1395 : access_public_node;
1396 tree fdecl, binfo;
1397 tree flist = NULL_TREE;
1398 tree tmp;
1399 int i;
1400 int n_methods;
1401
1402 binfo = binfo_or_else (ctype, t);
1403 if (! binfo)
1404 return;
1405
1406 if (name == constructor_name (ctype)
1407 || name == constructor_name_full (ctype))
1408 {
1409 cp_error_at ("using-declaration for constructor", using_decl);
1410 return;
1411 }
1412
1413 fdecl = lookup_member (binfo, name, 0, 0);
1414
1415 if (!fdecl)
1416 {
1417 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1418 return;
1419 }
1420
1421 /* Functions are represented as TREE_LIST, with the purpose
1422 being the type and the value the functions. Other members
1423 come as themselves. */
1424 if (TREE_CODE (fdecl) == TREE_LIST)
1425 /* Ignore base type this came from. */
1426 fdecl = TREE_VALUE (fdecl);
1427
1428 if (TREE_CODE (fdecl) == OVERLOAD)
1429 {
1430 /* We later iterate over all functions. */
1431 flist = fdecl;
1432 fdecl = OVL_FUNCTION (flist);
1433 }
1434
1435 name = DECL_NAME (fdecl);
1436 n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
1437 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); i++)
1438 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)))
1439 == name)
1440 {
1441 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1442 cp_error_at (" because of local method `%#D' with same name",
1443 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
1444 return;
1445 }
1446
1447 if (! DECL_LANG_SPECIFIC (fdecl))
1448 /* We don't currently handle DECL_ACCESS for TYPE_DECLs; just return. */
1449 return;
1450
1451 for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
1452 if (DECL_NAME (tmp) == name)
1453 {
1454 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1455 cp_error_at (" because of local field `%#D' with same name", tmp);
1456 return;
1457 }
1458
1459 /* Make type T see field decl FDECL with access ACCESS.*/
1460 if (flist)
1461 {
1462 while (flist)
1463 {
1464 if (alter_access (t, binfo, OVL_FUNCTION (flist),
1465 access) == 0)
1466 return;
1467 flist = OVL_CHAIN (flist);
1468 }
1469 }
1470 else
1471 alter_access (t, binfo, fdecl, access);
1472 }
1473 \f
1474 struct base_info
1475 {
1476 int has_virtual;
1477 int max_has_virtual;
1478 tree vfield;
1479 tree vfields;
1480 tree rtti;
1481 char cant_have_default_ctor;
1482 char cant_have_const_ctor;
1483 char no_const_asn_ref;
1484 };
1485
1486 /* Record information about type T derived from its base classes.
1487 Store most of that information in T itself, and place the
1488 remaining information in the struct BASE_INFO.
1489
1490 Propagate basetype offsets throughout the lattice. Note that the
1491 lattice topped by T is really a pair: it's a DAG that gives the
1492 structure of the derivation hierarchy, and it's a list of the
1493 virtual baseclasses that appear anywhere in the DAG. When a vbase
1494 type appears in the DAG, it's offset is 0, and it's children start
1495 their offsets from that point. When a vbase type appears in the list,
1496 its offset is the offset it has in the hierarchy, and its children's
1497 offsets include that offset in theirs.
1498
1499 Returns the index of the first base class to have virtual functions,
1500 or -1 if no such base class. */
1501
1502 static int
1503 finish_base_struct (t, b)
1504 tree t;
1505 struct base_info *b;
1506 {
1507 tree binfos = TYPE_BINFO_BASETYPES (t);
1508 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1509 int first_vfn_base_index = -1;
1510 bzero ((char *) b, sizeof (struct base_info));
1511
1512 for (i = 0; i < n_baseclasses; i++)
1513 {
1514 tree base_binfo = TREE_VEC_ELT (binfos, i);
1515 tree basetype = BINFO_TYPE (base_binfo);
1516
1517 /* Effective C++ rule 14. We only need to check TYPE_VIRTUAL_P
1518 here because the case of virtual functions but non-virtual
1519 dtor is handled in finish_struct_1. */
1520 if (warn_ecpp && ! TYPE_VIRTUAL_P (basetype)
1521 && TYPE_HAS_DESTRUCTOR (basetype))
1522 cp_warning ("base class `%#T' has a non-virtual destructor", basetype);
1523
1524 /* If the type of basetype is incomplete, then
1525 we already complained about that fact
1526 (and we should have fixed it up as well). */
1527 if (TYPE_SIZE (basetype) == 0)
1528 {
1529 int j;
1530 /* The base type is of incomplete type. It is
1531 probably best to pretend that it does not
1532 exist. */
1533 if (i == n_baseclasses-1)
1534 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1535 TREE_VEC_LENGTH (binfos) -= 1;
1536 n_baseclasses -= 1;
1537 for (j = i; j+1 < n_baseclasses; j++)
1538 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1539 }
1540
1541 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1542 b->cant_have_const_ctor = 1;
1543
1544 if (TYPE_HAS_CONSTRUCTOR (basetype)
1545 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1546 {
1547 b->cant_have_default_ctor = 1;
1548 if (! TYPE_HAS_CONSTRUCTOR (t))
1549 {
1550 cp_pedwarn ("base `%T' with only non-default constructor",
1551 basetype);
1552 cp_pedwarn ("in class without a constructor");
1553 }
1554 }
1555
1556 if (TYPE_HAS_ASSIGN_REF (basetype)
1557 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1558 b->no_const_asn_ref = 1;
1559
1560 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1561 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1562 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1563 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1564
1565 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1566 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1567 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1568
1569 if (CLASSTYPE_COM_INTERFACE (basetype))
1570 {
1571 CLASSTYPE_COM_INTERFACE (t) = 1;
1572 if (i > 0)
1573 cp_error
1574 ("COM interface type `%T' must be the leftmost base class",
1575 basetype);
1576 }
1577 else if (CLASSTYPE_COM_INTERFACE (t) && i == 0)
1578 {
1579 cp_error ("COM interface type `%T' with non-COM base class `%T'",
1580 t, basetype);
1581 CLASSTYPE_COM_INTERFACE (t) = 0;
1582 }
1583
1584 if (TYPE_VIRTUAL_P (basetype))
1585 {
1586 /* Ensure that this is set from at least a virtual base
1587 class. */
1588 if (b->rtti == NULL_TREE)
1589 b->rtti = CLASSTYPE_RTTI (basetype);
1590
1591 /* Don't borrow virtuals from virtual baseclasses. */
1592 if (TREE_VIA_VIRTUAL (base_binfo))
1593 continue;
1594
1595 if (first_vfn_base_index < 0)
1596 {
1597 tree vfields;
1598 first_vfn_base_index = i;
1599
1600 /* Update these two, now that we know what vtable we are
1601 going to extend. This is so that we can add virtual
1602 functions, and override them properly. */
1603 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1604 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1605 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1606 b->vfield = CLASSTYPE_VFIELD (basetype);
1607 b->vfields = copy_list (CLASSTYPE_VFIELDS (basetype));
1608 vfields = b->vfields;
1609 while (vfields)
1610 {
1611 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1612 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1613 {
1614 tree value = VF_BASETYPE_VALUE (vfields);
1615 if (DECL_NAME (CLASSTYPE_VFIELD (value))
1616 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1617 VF_NORMAL_VALUE (b->vfields) = basetype;
1618 else
1619 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1620 }
1621 vfields = TREE_CHAIN (vfields);
1622 }
1623 CLASSTYPE_VFIELD (t) = b->vfield;
1624 }
1625 else
1626 {
1627 /* Only add unique vfields, and flatten them out as we go. */
1628 tree vfields = CLASSTYPE_VFIELDS (basetype);
1629 while (vfields)
1630 {
1631 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1632 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1633 {
1634 tree value = VF_BASETYPE_VALUE (vfields);
1635 b->vfields = tree_cons (base_binfo, value, b->vfields);
1636 if (DECL_NAME (CLASSTYPE_VFIELD (value))
1637 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1638 VF_NORMAL_VALUE (b->vfields) = basetype;
1639 else
1640 VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1641 }
1642 vfields = TREE_CHAIN (vfields);
1643 }
1644
1645 if (b->has_virtual == 0)
1646 {
1647 first_vfn_base_index = i;
1648
1649 /* Update these two, now that we know what vtable we are
1650 going to extend. This is so that we can add virtual
1651 functions, and override them properly. */
1652 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1653 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1654 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1655 b->vfield = CLASSTYPE_VFIELD (basetype);
1656 CLASSTYPE_VFIELD (t) = b->vfield;
1657 /* When we install the first one, set the VF_NORMAL_VALUE
1658 to be the current class, as this it is the most derived
1659 class. Hopefully, this is not set to something else
1660 later. (mrs) */
1661 vfields = b->vfields;
1662 while (vfields)
1663 {
1664 if (DECL_NAME (CLASSTYPE_VFIELD (t))
1665 == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1666 {
1667 VF_NORMAL_VALUE (vfields) = t;
1668 /* There should only be one of them! And it should
1669 always be found, if we get into here. (mrs) */
1670 break;
1671 }
1672 vfields = TREE_CHAIN (vfields);
1673 }
1674 }
1675 }
1676 }
1677 }
1678
1679 {
1680 tree vfields;
1681 /* Find the base class with the largest number of virtual functions. */
1682 for (vfields = b->vfields; vfields; vfields = TREE_CHAIN (vfields))
1683 {
1684 if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)) > b->max_has_virtual)
1685 b->max_has_virtual = CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields));
1686 if (VF_DERIVED_VALUE (vfields)
1687 && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)) > b->max_has_virtual)
1688 b->max_has_virtual = CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields));
1689 }
1690 }
1691
1692 if (b->vfield == 0)
1693 /* If all virtual functions come only from virtual baseclasses. */
1694 return -1;
1695
1696 /* Update the rtti base if we have a non-virtual base class version
1697 of it. */
1698 b->rtti = CLASSTYPE_RTTI (BINFO_TYPE (TREE_VEC_ELT (binfos, first_vfn_base_index)));
1699
1700 return first_vfn_base_index;
1701 }
1702 \f
1703 /* Set memoizing fields and bits of T (and its variants) for later use.
1704 MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables. */
1705
1706 static void
1707 finish_struct_bits (t, max_has_virtual)
1708 tree t;
1709 int max_has_virtual;
1710 {
1711 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1712
1713 /* Fix up variants (if any). */
1714 tree variants = TYPE_NEXT_VARIANT (t);
1715 while (variants)
1716 {
1717 /* These fields are in the _TYPE part of the node, not in
1718 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1719 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1720 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1721 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1722 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
1723
1724 TYPE_USES_COMPLEX_INHERITANCE (variants) = TYPE_USES_COMPLEX_INHERITANCE (t);
1725 TYPE_VIRTUAL_P (variants) = TYPE_VIRTUAL_P (t);
1726 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1727 /* Copy whatever these are holding today. */
1728 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1729 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1730 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1731 TYPE_SIZE (variants) = TYPE_SIZE (t);
1732 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1733 variants = TYPE_NEXT_VARIANT (variants);
1734 }
1735
1736 if (n_baseclasses && max_has_virtual)
1737 {
1738 /* For a class w/o baseclasses, `finish_struct' has set
1739 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by definition). Similarly
1740 for a class who's base classes do not have vtables. When neither
1741 of these is true, we might have removed abstract virtuals (by
1742 providing a definition), added some (by declaring new ones), or
1743 redeclared ones from a base class. We need to recalculate what's
1744 really an abstract virtual at this point (by looking in the
1745 vtables). */
1746 CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
1747 }
1748
1749 if (n_baseclasses)
1750 {
1751 /* Notice whether this class has type conversion functions defined. */
1752 tree binfo = TYPE_BINFO (t);
1753 tree binfos = BINFO_BASETYPES (binfo);
1754 tree basetype;
1755
1756 for (i = n_baseclasses-1; i >= 0; i--)
1757 {
1758 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1759
1760 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1761 }
1762 }
1763
1764 /* If this type has a copy constructor, force its mode to be BLKmode, and
1765 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1766 be passed by invisible reference and prevent it from being returned in
1767 a register.
1768
1769 Also do this if the class has BLKmode but can still be returned in
1770 registers, since function_cannot_inline_p won't let us inline
1771 functions returning such a type. This affects the HP-PA. */
1772 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1773 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1774 && CLASSTYPE_NON_AGGREGATE (t)))
1775 {
1776 tree variants;
1777 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1778 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1779 {
1780 TYPE_MODE (variants) = BLKmode;
1781 TREE_ADDRESSABLE (variants) = 1;
1782 }
1783 }
1784 }
1785
1786 /* Issue warnings about T having private constructors, but no friends,
1787 and so forth.
1788
1789 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1790 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1791 non-private static member functions. */
1792
1793 static void
1794 maybe_warn_about_overly_private_class (t)
1795 tree t;
1796 {
1797 int has_member_fn = 0;
1798 int has_nonprivate_method = 0;
1799 tree fn;
1800
1801 if (!warn_ctor_dtor_privacy
1802 /* If the class has friends, those entities might create and
1803 access instances, so we should not warn. */
1804 || (CLASSTYPE_FRIEND_CLASSES (t)
1805 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1806 /* We will have warned when the template was declared; there's
1807 no need to warn on every instantiation. */
1808 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1809 /* There's no reason to even consider warning about this
1810 class. */
1811 return;
1812
1813 /* We only issue one warning, if more than one applies, because
1814 otherwise, on code like:
1815
1816 class A {
1817 // Oops - forgot `public:'
1818 A();
1819 A(const A&);
1820 ~A();
1821 };
1822
1823 we warn several times about essentially the same problem. */
1824
1825 /* Check to see if all (non-constructor, non-destructor) member
1826 functions are private. (Since there are no friends or
1827 non-private statics, we can't ever call any of the private member
1828 functions.) */
1829 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1830 /* We're not interested in compiler-generated methods; they don't
1831 provide any way to call private members. */
1832 if (!DECL_ARTIFICIAL (fn))
1833 {
1834 if (!TREE_PRIVATE (fn))
1835 {
1836 if (DECL_STATIC_FUNCTION_P (fn))
1837 /* A non-private static member function is just like a
1838 friend; it can create and invoke private member
1839 functions, and be accessed without a class
1840 instance. */
1841 return;
1842
1843 has_nonprivate_method = 1;
1844 break;
1845 }
1846 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1847 has_member_fn = 1;
1848 }
1849
1850 if (!has_nonprivate_method && has_member_fn)
1851 {
1852 /* There are no non-private methods, and there's at least one
1853 private member function that isn't a constructor or
1854 destructor. (If all the private members are
1855 constructors/destructors we want to use the code below that
1856 issues error messages specifically referring to
1857 constructors/destructors.) */
1858 int i;
1859 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1860 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1861 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1862 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1863 {
1864 has_nonprivate_method = 1;
1865 break;
1866 }
1867 if (!has_nonprivate_method)
1868 {
1869 cp_warning ("all member functions in class `%T' are private", t);
1870 return;
1871 }
1872 }
1873
1874 /* Even if some of the member functions are non-private, the class
1875 won't be useful for much if all the constructors or destructors
1876 are private: such an object can never be created or destroyed. */
1877 if (TYPE_HAS_DESTRUCTOR (t))
1878 {
1879 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
1880
1881 if (TREE_PRIVATE (dtor))
1882 {
1883 cp_warning ("`%#T' only defines a private destructor and has no friends",
1884 t);
1885 return;
1886 }
1887 }
1888
1889 if (TYPE_HAS_CONSTRUCTOR (t))
1890 {
1891 int nonprivate_ctor = 0;
1892
1893 /* If a non-template class does not define a copy
1894 constructor, one is defined for it, enabling it to avoid
1895 this warning. For a template class, this does not
1896 happen, and so we would normally get a warning on:
1897
1898 template <class T> class C { private: C(); };
1899
1900 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1901 complete non-template or fully instantiated classes have this
1902 flag set. */
1903 if (!TYPE_HAS_INIT_REF (t))
1904 nonprivate_ctor = 1;
1905 else
1906 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
1907 fn;
1908 fn = OVL_NEXT (fn))
1909 {
1910 tree ctor = OVL_CURRENT (fn);
1911 /* Ideally, we wouldn't count copy constructors (or, in
1912 fact, any constructor that takes an argument of the
1913 class type as a parameter) because such things cannot
1914 be used to construct an instance of the class unless
1915 you already have one. But, for now at least, we're
1916 more generous. */
1917 if (! TREE_PRIVATE (ctor))
1918 {
1919 nonprivate_ctor = 1;
1920 break;
1921 }
1922 }
1923
1924 if (nonprivate_ctor == 0)
1925 {
1926 cp_warning ("`%#T' only defines private constructors and has no friends",
1927 t);
1928 return;
1929 }
1930 }
1931 }
1932
1933 /* Function to help qsort sort FIELD_DECLs by name order. */
1934
1935 static int
1936 field_decl_cmp (x, y)
1937 const tree *x, *y;
1938 {
1939 if (DECL_NAME (*x) == DECL_NAME (*y))
1940 return 0;
1941 if (DECL_NAME (*x) == NULL_TREE)
1942 return -1;
1943 if (DECL_NAME (*y) == NULL_TREE)
1944 return 1;
1945 if (DECL_NAME (*x) < DECL_NAME (*y))
1946 return -1;
1947 return 1;
1948 }
1949
1950 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1951
1952 static int
1953 method_name_cmp (m1, m2)
1954 const tree *m1, *m2;
1955 {
1956 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1957 return 0;
1958 if (*m1 == NULL_TREE)
1959 return -1;
1960 if (*m2 == NULL_TREE)
1961 return 1;
1962 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1963 return -1;
1964 return 1;
1965 }
1966
1967 /* Warn about duplicate methods in fn_fields. Also compact method
1968 lists so that lookup can be made faster.
1969
1970 Data Structure: List of method lists. The outer list is a
1971 TREE_LIST, whose TREE_PURPOSE field is the field name and the
1972 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
1973 links the entire list of methods for TYPE_METHODS. Friends are
1974 chained in the same way as member functions (? TREE_CHAIN or
1975 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
1976 list. That allows them to be quickly deleted, and requires no
1977 extra storage.
1978
1979 If there are any constructors/destructors, they are moved to the
1980 front of the list. This makes pushclass more efficient.
1981
1982 @@ The above comment is obsolete. It mostly describes what add_method
1983 @@ and add_implicitly_declared_members do.
1984
1985 Sort methods that are not special (i.e., constructors, destructors, and
1986 type conversion operators) so that we can find them faster in search. */
1987
1988 static void
1989 finish_struct_methods (t)
1990 tree t;
1991 {
1992 tree fn_fields;
1993 tree method_vec = CLASSTYPE_METHOD_VEC (t);
1994 tree ctor_name = constructor_name (t);
1995 int slot, len = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
1996
1997 /* First fill in entry 0 with the constructors, entry 1 with destructors,
1998 and the next few with type conversion operators (if any). */
1999 for (fn_fields = TYPE_METHODS (t); fn_fields;
2000 fn_fields = TREE_CHAIN (fn_fields))
2001 {
2002 tree fn_name = DECL_NAME (fn_fields);
2003
2004 /* Clear out this flag.
2005
2006 @@ Doug may figure out how to break
2007 @@ this with nested classes and friends. */
2008 DECL_IN_AGGR_P (fn_fields) = 0;
2009
2010 /* Note here that a copy ctor is private, so we don't dare generate
2011 a default copy constructor for a class that has a member
2012 of this type without making sure they have access to it. */
2013 if (fn_name == ctor_name)
2014 {
2015 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2016 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2017
2018 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2019 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2020 {
2021 if (TREE_CHAIN (parmtypes) == NULL_TREE
2022 || TREE_CHAIN (parmtypes) == void_list_node
2023 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2024 {
2025 if (TREE_PROTECTED (fn_fields))
2026 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2027 else if (TREE_PRIVATE (fn_fields))
2028 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2029 }
2030 }
2031 }
2032 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2033 {
2034 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2035
2036 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2037 {
2038 if (TREE_PROTECTED (fn_fields))
2039 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2040 else if (TREE_PRIVATE (fn_fields))
2041 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2042 }
2043 }
2044 }
2045
2046 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2047 /* We thought there was a destructor, but there wasn't. Some
2048 parse errors cause this anomalous situation. */
2049 TYPE_HAS_DESTRUCTOR (t) = 0;
2050
2051 /* Issue warnings about private constructors and such. If there are
2052 no methods, then some public defaults are generated. */
2053 maybe_warn_about_overly_private_class (t);
2054
2055 if (method_vec == NULL_TREE)
2056 return;
2057
2058 /* Now sort the methods. */
2059 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2060 len--;
2061 TREE_VEC_LENGTH (method_vec) = len;
2062
2063 /* The type conversion ops have to live at the front of the vec, so we
2064 can't sort them. */
2065 for (slot = 2; slot < len; ++slot)
2066 {
2067 tree fn = TREE_VEC_ELT (method_vec, slot);
2068
2069 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2070 break;
2071 }
2072 if (len - slot > 1)
2073 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2074 (int (*)(const void *, const void *))method_name_cmp);
2075 }
2076
2077 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2078
2079 void
2080 duplicate_tag_error (t)
2081 tree t;
2082 {
2083 cp_error ("redefinition of `%#T'", t);
2084 cp_error_at ("previous definition here", t);
2085
2086 /* Pretend we haven't defined this type. */
2087
2088 /* All of the component_decl's were TREE_CHAINed together in the parser.
2089 finish_struct_methods walks these chains and assembles all methods with
2090 the same base name into DECL_CHAINs. Now we don't need the parser chains
2091 anymore, so we unravel them. */
2092
2093 /* This used to be in finish_struct, but it turns out that the
2094 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2095 things... */
2096 if (CLASSTYPE_METHOD_VEC (t))
2097 {
2098 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2099 int i, len = TREE_VEC_LENGTH (method_vec);
2100 for (i = 0; i < len; i++)
2101 {
2102 tree unchain = TREE_VEC_ELT (method_vec, i);
2103 while (unchain != NULL_TREE)
2104 {
2105 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2106 unchain = OVL_NEXT (unchain);
2107 }
2108 }
2109 }
2110
2111 if (TYPE_LANG_SPECIFIC (t))
2112 {
2113 tree binfo = TYPE_BINFO (t);
2114 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2115 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2116 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2117 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2118
2119 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2120 BINFO_BASETYPES(binfo) = NULL_TREE;
2121
2122 TYPE_BINFO (t) = binfo;
2123 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2124 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2125 TYPE_REDEFINED (t) = 1;
2126 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2127 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2128 }
2129 TYPE_SIZE (t) = NULL_TREE;
2130 TYPE_MODE (t) = VOIDmode;
2131 TYPE_FIELDS (t) = NULL_TREE;
2132 TYPE_METHODS (t) = NULL_TREE;
2133 TYPE_VFIELD (t) = NULL_TREE;
2134 TYPE_CONTEXT (t) = NULL_TREE;
2135 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2136 }
2137
2138 /* Construct the initializer for BINFOs virtual function table. */
2139
2140 static tree
2141 build_vtbl_initializer (binfo)
2142 tree binfo;
2143 {
2144 tree v = BINFO_VIRTUALS (binfo);
2145 tree inits = NULL_TREE;
2146
2147 /* Process the RTTI stuff at the head of the list. If we're not
2148 using vtable thunks, then the RTTI entry is just an ordinary
2149 function, and we can process it just like the other virtual
2150 function entries. */
2151 if (!CLASSTYPE_COM_INTERFACE (BINFO_TYPE (binfo))
2152 && flag_vtable_thunks)
2153 {
2154 tree offset;
2155 tree init;
2156
2157 /* The first entry is an offset. */
2158 offset = TREE_PURPOSE (v);
2159 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST,
2160 19990727);
2161
2162 /* Convert the offset to look like a function pointer, so that
2163 we can put it in the vtable. */
2164 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
2165 TREE_CONSTANT (init) = 1;
2166 init = build_vtable_entry (integer_zero_node, init);
2167 inits = tree_cons (NULL_TREE, init, inits);
2168
2169 /* Even in this case, the second entry (the tdesc pointer) is
2170 just an ordinary function. */
2171 v = TREE_CHAIN (v);
2172 }
2173
2174 /* Go through all the ordinary virtual functions, building up
2175 initializers. */
2176 while (v)
2177 {
2178 tree delta;
2179 tree fn;
2180 tree init;
2181
2182 /* Pull the offset for `this', and the function to call, out of
2183 the list. */
2184 delta = TREE_PURPOSE (v);
2185 fn = TREE_VALUE (v);
2186 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
2187 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
2188
2189 /* You can't call an abstract virtual function; it's abstract.
2190 So, we replace these functions with __pure_virtual. */
2191 if (DECL_ABSTRACT_VIRTUAL_P (fn))
2192 fn = abort_fndecl;
2193
2194 /* Package up that information for the vtable. */
2195 init = build_vtable_entry_for_fn (delta, fn);
2196 /* And add it to the chain of initializers. */
2197 inits = tree_cons (NULL_TREE, init, inits);
2198
2199 /* Keep going. */
2200 v = TREE_CHAIN (v);
2201 }
2202
2203 /* The initializers were built up in reverse order; straighten them
2204 out now. */
2205 inits = nreverse (inits);
2206 /* Package all the initializers up as an array initializer. */
2207 return build_nt (CONSTRUCTOR, NULL_TREE, inits);
2208 }
2209
2210 /* finish up all new vtables. */
2211
2212 static void
2213 finish_vtbls (binfo, do_self, t)
2214 tree binfo;
2215 int do_self;
2216 tree t;
2217 {
2218 tree binfos = BINFO_BASETYPES (binfo);
2219 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2220
2221 /* Should we use something besides CLASSTYPE_VFIELDS? */
2222 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2223 {
2224 if (BINFO_NEW_VTABLE_MARKED (binfo))
2225 {
2226 tree decl, context;
2227
2228 decl = BINFO_VTABLE (binfo);
2229 context = DECL_CONTEXT (decl);
2230 DECL_CONTEXT (decl) = 0;
2231 DECL_INITIAL (decl) = build_vtbl_initializer (binfo);
2232 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0);
2233 DECL_CONTEXT (decl) = context;
2234 }
2235 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2236 }
2237
2238 for (i = 0; i < n_baselinks; i++)
2239 {
2240 tree base_binfo = TREE_VEC_ELT (binfos, i);
2241 int is_not_base_vtable
2242 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2243 if (TREE_VIA_VIRTUAL (base_binfo))
2244 base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2245 CLASSTYPE_VBASECLASSES (t));
2246 finish_vtbls (base_binfo, is_not_base_vtable, t);
2247 }
2248 }
2249
2250 /* True if we should override the given BASE_FNDECL with the given
2251 FNDECL. */
2252
2253 static int
2254 overrides (fndecl, base_fndecl)
2255 tree fndecl, base_fndecl;
2256 {
2257 /* Destructors have special names. */
2258 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2259 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2260 return 1;
2261 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2262 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2263 return 0;
2264 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2265 {
2266 tree types, base_types;
2267 #if 0
2268 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2269 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2270 #endif
2271 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2272 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2273 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2274 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2275 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2276 return 1;
2277 }
2278 return 0;
2279 }
2280
2281 static tree
2282 get_class_offset_1 (parent, binfo, context, t, fndecl)
2283 tree parent, binfo, context, t, fndecl;
2284 {
2285 tree binfos = BINFO_BASETYPES (binfo);
2286 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2287 tree rval = NULL_TREE;
2288
2289 if (binfo == parent)
2290 return error_mark_node;
2291
2292 for (i = 0; i < n_baselinks; i++)
2293 {
2294 tree base_binfo = TREE_VEC_ELT (binfos, i);
2295 tree nrval;
2296
2297 if (TREE_VIA_VIRTUAL (base_binfo))
2298 base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2299 CLASSTYPE_VBASECLASSES (t));
2300 nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl);
2301 /* See if we have a new value */
2302 if (nrval && (nrval != error_mark_node || rval==0))
2303 {
2304 /* Only compare if we have two offsets */
2305 if (rval && rval != error_mark_node
2306 && ! tree_int_cst_equal (nrval, rval))
2307 {
2308 /* Only give error if the two offsets are different */
2309 error ("every virtual function must have a unique final overrider");
2310 cp_error (" found two (or more) `%T' class subobjects in `%T'", context, t);
2311 cp_error (" with virtual `%D' from virtual base class", fndecl);
2312 return rval;
2313 }
2314 rval = nrval;
2315 }
2316
2317 if (rval && BINFO_TYPE (binfo) == context)
2318 {
2319 my_friendly_assert (rval == error_mark_node
2320 || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999);
2321 rval = BINFO_OFFSET (binfo);
2322 }
2323 }
2324 return rval;
2325 }
2326
2327 /* Get the offset to the CONTEXT subobject that is related to the
2328 given BINFO. */
2329
2330 static tree
2331 get_class_offset (context, t, binfo, fndecl)
2332 tree context, t, binfo, fndecl;
2333 {
2334 tree first_binfo = binfo;
2335 tree offset;
2336 int i;
2337
2338 if (context == t)
2339 return integer_zero_node;
2340
2341 if (BINFO_TYPE (binfo) == context)
2342 return BINFO_OFFSET (binfo);
2343
2344 /* Check less derived binfos first. */
2345 while (BINFO_BASETYPES (binfo)
2346 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
2347 {
2348 tree binfos = BINFO_BASETYPES (binfo);
2349 binfo = TREE_VEC_ELT (binfos, i);
2350 if (BINFO_TYPE (binfo) == context)
2351 return BINFO_OFFSET (binfo);
2352 }
2353
2354 /* Ok, not found in the less derived binfos, now check the more
2355 derived binfos. */
2356 offset = get_class_offset_1 (first_binfo, TYPE_BINFO (t), context, t, fndecl);
2357 if (offset==0 || TREE_CODE (offset) != INTEGER_CST)
2358 my_friendly_abort (999); /* we have to find it. */
2359 return offset;
2360 }
2361
2362 /* Skip RTTI information at the front of the virtual list. */
2363
2364 unsigned HOST_WIDE_INT
2365 skip_rtti_stuff (virtuals, t)
2366 tree *virtuals, t;
2367 {
2368 int n;
2369
2370 if (CLASSTYPE_COM_INTERFACE (t))
2371 return 0;
2372
2373 n = 0;
2374 if (*virtuals)
2375 {
2376 /* We always reserve a slot for the offset/tdesc entry. */
2377 ++n;
2378 *virtuals = TREE_CHAIN (*virtuals);
2379 }
2380 if (flag_vtable_thunks && *virtuals)
2381 {
2382 /* The second slot is reserved for the tdesc pointer when thunks
2383 are used. */
2384 ++n;
2385 *virtuals = TREE_CHAIN (*virtuals);
2386 }
2387 return n;
2388 }
2389
2390 static void
2391 modify_one_vtable (binfo, t, fndecl)
2392 tree binfo, t, fndecl;
2393 {
2394 tree virtuals = BINFO_VIRTUALS (binfo);
2395 unsigned HOST_WIDE_INT n;
2396
2397 /* update rtti entry */
2398 if (flag_rtti)
2399 {
2400 if (binfo == TYPE_BINFO (t))
2401 {
2402 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2403 build_vtable (TYPE_BINFO (DECL_CONTEXT (CLASSTYPE_VFIELD (t))), t);
2404 }
2405 else
2406 {
2407 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2408 prepare_fresh_vtable (binfo, t);
2409 }
2410 }
2411 if (fndecl == NULL_TREE)
2412 return;
2413
2414 n = skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo));
2415
2416 while (virtuals)
2417 {
2418 tree current_fndecl = TREE_VALUE (virtuals);
2419
2420 /* We should never have an instance of __pure_virtual on the
2421 BINFO_VIRTUALS list. If we do, then we will never notice
2422 that the function that should have been there instead has
2423 been overridden. */
2424 my_friendly_assert (current_fndecl != abort_fndecl,
2425 19990727);
2426
2427 if (current_fndecl && overrides (fndecl, current_fndecl))
2428 {
2429 tree base_offset, offset;
2430 tree context = DECL_CLASS_CONTEXT (fndecl);
2431 tree vfield = CLASSTYPE_VFIELD (t);
2432 tree this_offset;
2433
2434 offset = get_class_offset (context, t, binfo, fndecl);
2435
2436 /* Find the right offset for the this pointer based on the
2437 base class we just found. We have to take into
2438 consideration the virtual base class pointers that we
2439 stick in before the virtual function table pointer.
2440
2441 Also, we want just the delta between the most base class
2442 that we derived this vfield from and us. */
2443 base_offset = size_binop (PLUS_EXPR,
2444 get_derived_offset (binfo, DECL_CONTEXT (current_fndecl)),
2445 BINFO_OFFSET (binfo));
2446 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2447
2448 if (binfo == TYPE_BINFO (t))
2449 {
2450 /* In this case, it is *type*'s vtable we are modifying.
2451 We start with the approximation that it's vtable is that
2452 of the immediate base class. */
2453 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2454 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2455 }
2456 else
2457 {
2458 /* This is our very own copy of `basetype' to play with.
2459 Later, we will fill in all the virtual functions
2460 that override the virtual functions in these base classes
2461 which are not defined by the current type. */
2462 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2463 prepare_fresh_vtable (binfo, t);
2464 }
2465
2466 #ifdef NOTQUITE
2467 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
2468 #endif
2469 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2470 this_offset,
2471 fndecl);
2472 }
2473 ++n;
2474 virtuals = TREE_CHAIN (virtuals);
2475 }
2476 }
2477
2478 /* These are the ones that are not through virtual base classes. */
2479
2480 static void
2481 modify_all_direct_vtables (binfo, do_self, t, fndecl)
2482 tree binfo;
2483 int do_self;
2484 tree t, fndecl;
2485 {
2486 tree binfos = BINFO_BASETYPES (binfo);
2487 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2488
2489 /* Should we use something besides CLASSTYPE_VFIELDS? */
2490 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2491 modify_one_vtable (binfo, t, fndecl);
2492
2493 for (i = 0; i < n_baselinks; i++)
2494 {
2495 tree base_binfo = TREE_VEC_ELT (binfos, i);
2496 int is_not_base_vtable
2497 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2498 if (! TREE_VIA_VIRTUAL (base_binfo))
2499 modify_all_direct_vtables (base_binfo, is_not_base_vtable, t, fndecl);
2500 }
2501 }
2502
2503 /* Fixup all the delta entries in this one vtable that need updating. */
2504
2505 static void
2506 fixup_vtable_deltas1 (binfo, t)
2507 tree binfo, t;
2508 {
2509 tree virtuals = BINFO_VIRTUALS (binfo);
2510 unsigned HOST_WIDE_INT n;
2511
2512 n = skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo));
2513
2514 while (virtuals)
2515 {
2516 tree fndecl = TREE_VALUE (virtuals);
2517 tree delta = TREE_PURPOSE (virtuals);
2518
2519 if (fndecl)
2520 {
2521 tree base_offset, offset;
2522 tree context = DECL_CLASS_CONTEXT (fndecl);
2523 tree vfield = CLASSTYPE_VFIELD (t);
2524 tree this_offset;
2525
2526 offset = get_class_offset (context, t, binfo, fndecl);
2527
2528 /* Find the right offset for the this pointer based on the
2529 base class we just found. We have to take into
2530 consideration the virtual base class pointers that we
2531 stick in before the virtual function table pointer.
2532
2533 Also, we want just the delta between the most base class
2534 that we derived this vfield from and us. */
2535 base_offset = size_binop (PLUS_EXPR,
2536 get_derived_offset (binfo,
2537 DECL_CONTEXT (fndecl)),
2538 BINFO_OFFSET (binfo));
2539 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2540
2541 if (! tree_int_cst_equal (this_offset, delta))
2542 {
2543 /* Make sure we can modify the derived association with immunity. */
2544 if (binfo == TYPE_BINFO (t))
2545 {
2546 /* In this case, it is *type*'s vtable we are modifying.
2547 We start with the approximation that it's vtable is that
2548 of the immediate base class. */
2549 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2550 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2551 }
2552 else
2553 {
2554 /* This is our very own copy of `basetype' to play with.
2555 Later, we will fill in all the virtual functions
2556 that override the virtual functions in these base classes
2557 which are not defined by the current type. */
2558 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2559 prepare_fresh_vtable (binfo, t);
2560 }
2561
2562 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2563 this_offset,
2564 fndecl);
2565 }
2566 }
2567 ++n;
2568 virtuals = TREE_CHAIN (virtuals);
2569 }
2570 }
2571
2572 /* Fixup all the delta entries in all the direct vtables that need updating.
2573 This happens when we have non-overridden virtual functions from a
2574 virtual base class, that are at a different offset, in the new
2575 hierarchy, because the layout of the virtual bases has changed. */
2576
2577 static void
2578 fixup_vtable_deltas (binfo, init_self, t)
2579 tree binfo;
2580 int init_self;
2581 tree t;
2582 {
2583 tree binfos = BINFO_BASETYPES (binfo);
2584 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2585
2586 for (i = 0; i < n_baselinks; i++)
2587 {
2588 tree base_binfo = TREE_VEC_ELT (binfos, i);
2589 int is_not_base_vtable
2590 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2591 if (! TREE_VIA_VIRTUAL (base_binfo))
2592 fixup_vtable_deltas (base_binfo, is_not_base_vtable, t);
2593 }
2594 /* Should we use something besides CLASSTYPE_VFIELDS? */
2595 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2596 fixup_vtable_deltas1 (binfo, t);
2597 }
2598
2599 /* These are the ones that are through virtual base classes. */
2600
2601 static void
2602 modify_all_indirect_vtables (binfo, do_self, via_virtual, t, fndecl)
2603 tree binfo;
2604 int do_self, via_virtual;
2605 tree t, fndecl;
2606 {
2607 tree binfos = BINFO_BASETYPES (binfo);
2608 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2609
2610 /* Should we use something besides CLASSTYPE_VFIELDS? */
2611 if (do_self && via_virtual && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2612 modify_one_vtable (binfo, t, fndecl);
2613
2614 for (i = 0; i < n_baselinks; i++)
2615 {
2616 tree base_binfo = TREE_VEC_ELT (binfos, i);
2617 int is_not_base_vtable
2618 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2619 if (TREE_VIA_VIRTUAL (base_binfo))
2620 {
2621 via_virtual = 1;
2622 base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2623 }
2624 modify_all_indirect_vtables (base_binfo, is_not_base_vtable, via_virtual, t, fndecl);
2625 }
2626 }
2627
2628 static void
2629 modify_all_vtables (t, fndecl)
2630 tree t;
2631 tree fndecl;
2632 {
2633 /* Do these first, so that we will make use of any non-virtual class's
2634 vtable, over a virtual classes vtable. */
2635 modify_all_direct_vtables (TYPE_BINFO (t), 1, t, fndecl);
2636 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
2637 modify_all_indirect_vtables (TYPE_BINFO (t), 1, 0, t, fndecl);
2638 }
2639
2640 /* Here, we already know that they match in every respect.
2641 All we have to check is where they had their declarations. */
2642
2643 static int
2644 strictly_overrides (fndecl1, fndecl2)
2645 tree fndecl1, fndecl2;
2646 {
2647 int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
2648 DECL_CLASS_CONTEXT (fndecl1),
2649 0, (tree *)0);
2650 if (distance == -2 || distance > 0)
2651 return 1;
2652 return 0;
2653 }
2654
2655 /* Merge overrides for one vtable.
2656 If we want to merge in same function, we are fine.
2657 else
2658 if one has a DECL_CLASS_CONTEXT that is a parent of the
2659 other, than choose the more derived one
2660 else
2661 potentially ill-formed (see 10.3 [class.virtual])
2662 we have to check later to see if there was an
2663 override in this class. If there was ok, if not
2664 then it is ill-formed. (mrs)
2665
2666 We take special care to reuse a vtable, if we can. */
2667
2668 static void
2669 override_one_vtable (binfo, old, t)
2670 tree binfo, old, t;
2671 {
2672 tree virtuals = BINFO_VIRTUALS (binfo);
2673 tree old_virtuals = BINFO_VIRTUALS (old);
2674 enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
2675
2676 /* If we have already committed to modifying it, then don't try and
2677 reuse another vtable. */
2678 if (BINFO_NEW_VTABLE_MARKED (binfo))
2679 choose = NEITHER;
2680
2681 skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo));
2682 skip_rtti_stuff (&old_virtuals, BINFO_TYPE (binfo));
2683
2684 while (virtuals)
2685 {
2686 tree fndecl = TREE_VALUE (virtuals);
2687 tree old_fndecl = TREE_VALUE (old_virtuals);
2688
2689 /* First check to see if they are the same. */
2690 if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
2691 {
2692 /* No need to do anything. */
2693 }
2694 else if (strictly_overrides (fndecl, old_fndecl))
2695 {
2696 if (choose == UNDECIDED)
2697 choose = REUSE_NEW;
2698 else if (choose == REUSE_OLD)
2699 {
2700 choose = NEITHER;
2701 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2702 {
2703 prepare_fresh_vtable (binfo, t);
2704 override_one_vtable (binfo, old, t);
2705 return;
2706 }
2707 }
2708 }
2709 else if (strictly_overrides (old_fndecl, fndecl))
2710 {
2711 if (choose == UNDECIDED)
2712 choose = REUSE_OLD;
2713 else if (choose == REUSE_NEW)
2714 {
2715 choose = NEITHER;
2716 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2717 {
2718 prepare_fresh_vtable (binfo, t);
2719 override_one_vtable (binfo, old, t);
2720 return;
2721 }
2722 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2723 }
2724 else if (choose == NEITHER)
2725 {
2726 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2727 }
2728 }
2729 else
2730 {
2731 choose = NEITHER;
2732 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2733 {
2734 prepare_fresh_vtable (binfo, t);
2735 override_one_vtable (binfo, old, t);
2736 return;
2737 }
2738 {
2739 /* This MUST be overridden, or the class is ill-formed. */
2740 tree fndecl = TREE_VALUE (virtuals);
2741
2742 fndecl = copy_node (fndecl);
2743 copy_lang_decl (fndecl);
2744 DECL_NEEDS_FINAL_OVERRIDER_P (fndecl) = 1;
2745 /* Make sure we search for it later. */
2746 if (! CLASSTYPE_ABSTRACT_VIRTUALS (t))
2747 CLASSTYPE_ABSTRACT_VIRTUALS (t) = error_mark_node;
2748
2749 /* We can use integer_zero_node, as we will core dump
2750 if this is used anyway. */
2751 TREE_PURPOSE (virtuals) = integer_zero_node;
2752 TREE_VALUE (virtuals) = fndecl;
2753 }
2754 }
2755 virtuals = TREE_CHAIN (virtuals);
2756 old_virtuals = TREE_CHAIN (old_virtuals);
2757 }
2758
2759 /* Let's reuse the old vtable. */
2760 if (choose == REUSE_OLD)
2761 {
2762 BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
2763 BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
2764 }
2765 }
2766
2767 /* Merge in overrides for virtual bases.
2768 BINFO is the hierarchy we want to modify, and OLD has the potential
2769 overrides. */
2770
2771 static void
2772 merge_overrides (binfo, old, do_self, t)
2773 tree binfo, old;
2774 int do_self;
2775 tree t;
2776 {
2777 tree binfos = BINFO_BASETYPES (binfo);
2778 tree old_binfos = BINFO_BASETYPES (old);
2779 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2780
2781 /* Should we use something besides CLASSTYPE_VFIELDS? */
2782 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2783 {
2784 override_one_vtable (binfo, old, t);
2785 }
2786
2787 for (i = 0; i < n_baselinks; i++)
2788 {
2789 tree base_binfo = TREE_VEC_ELT (binfos, i);
2790 tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
2791 int is_not_base_vtable
2792 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2793 if (! TREE_VIA_VIRTUAL (base_binfo))
2794 merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
2795 }
2796 }
2797
2798 /* Get the base virtual function declarations in T that are either
2799 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2800 the overrider/hider. */
2801
2802 static tree
2803 get_basefndecls (fndecl, t)
2804 tree fndecl, t;
2805 {
2806 tree methods = TYPE_METHODS (t);
2807 tree base_fndecls = NULL_TREE;
2808 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2809 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2810
2811 while (methods)
2812 {
2813 if (TREE_CODE (methods) == FUNCTION_DECL
2814 && DECL_VINDEX (methods) != NULL_TREE
2815 && DECL_NAME (fndecl) == DECL_NAME (methods))
2816 base_fndecls = temp_tree_cons (fndecl, methods, base_fndecls);
2817
2818 methods = TREE_CHAIN (methods);
2819 }
2820
2821 if (base_fndecls)
2822 return base_fndecls;
2823
2824 for (i = 0; i < n_baseclasses; i++)
2825 {
2826 tree base_binfo = TREE_VEC_ELT (binfos, i);
2827 tree basetype = BINFO_TYPE (base_binfo);
2828
2829 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2830 base_fndecls);
2831 }
2832
2833 return base_fndecls;
2834 }
2835
2836 /* Mark the functions that have been hidden with their overriders.
2837 Since we start out with all functions already marked with a hider,
2838 no need to mark functions that are just hidden.
2839
2840 Subroutine of warn_hidden. */
2841
2842 static void
2843 mark_overriders (fndecl, base_fndecls)
2844 tree fndecl, base_fndecls;
2845 {
2846 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2847 {
2848 if (overrides (fndecl, TREE_VALUE (base_fndecls)))
2849 TREE_PURPOSE (base_fndecls) = fndecl;
2850 }
2851 }
2852
2853 /* If this declaration supersedes the declaration of
2854 a method declared virtual in the base class, then
2855 mark this field as being virtual as well. */
2856
2857 static void
2858 check_for_override (decl, ctype)
2859 tree decl, ctype;
2860 {
2861 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
2862 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2863 int virtualp = DECL_VIRTUAL_P (decl);
2864 int found_overriden_fn = 0;
2865
2866 for (i = 0; i < n_baselinks; i++)
2867 {
2868 tree base_binfo = TREE_VEC_ELT (binfos, i);
2869 if (TYPE_VIRTUAL_P (BINFO_TYPE (base_binfo)))
2870 {
2871 tree tmp = get_matching_virtual
2872 (base_binfo, decl,
2873 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
2874
2875 if (tmp && !found_overriden_fn)
2876 {
2877 /* If this function overrides some virtual in some base
2878 class, then the function itself is also necessarily
2879 virtual, even if the user didn't explicitly say so. */
2880 DECL_VIRTUAL_P (decl) = 1;
2881
2882 /* The TMP we really want is the one from the deepest
2883 baseclass on this path, taking care not to
2884 duplicate if we have already found it (via another
2885 path to its virtual baseclass. */
2886 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
2887 {
2888 cp_error_at ("`static %#D' cannot be declared", decl);
2889 cp_error_at (" since `virtual %#D' declared in base class",
2890 tmp);
2891 break;
2892 }
2893 virtualp = 1;
2894
2895 DECL_VINDEX (decl)
2896 = tree_cons (NULL_TREE, tmp, DECL_VINDEX (decl));
2897
2898 /* We now know that DECL overrides something,
2899 which is all that is important. But, we must
2900 continue to iterate through all the base-classes
2901 in order to allow get_matching_virtual to check for
2902 various illegal overrides. */
2903 found_overriden_fn = 1;
2904 }
2905 }
2906 }
2907 if (virtualp)
2908 {
2909 if (DECL_VINDEX (decl) == NULL_TREE)
2910 DECL_VINDEX (decl) = error_mark_node;
2911 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2912 }
2913 }
2914
2915 /* Warn about hidden virtual functions that are not overridden in t.
2916 We know that constructors and destructors don't apply. */
2917
2918 void
2919 warn_hidden (t)
2920 tree t;
2921 {
2922 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2923 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2924 int i;
2925
2926 /* We go through each separately named virtual function. */
2927 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2928 {
2929 tree fns = TREE_VEC_ELT (method_vec, i);
2930 tree fndecl;
2931
2932 tree base_fndecls = NULL_TREE;
2933 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2934 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2935
2936 /* First see if we have any virtual functions in this batch. */
2937 for (; fns; fns = OVL_NEXT (fns))
2938 {
2939 fndecl = OVL_CURRENT (fns);
2940 if (DECL_VINDEX (fndecl))
2941 break;
2942 }
2943
2944 if (fns == NULL_TREE)
2945 continue;
2946
2947 /* First we get a list of all possible functions that might be
2948 hidden from each base class. */
2949 for (i = 0; i < n_baseclasses; i++)
2950 {
2951 tree base_binfo = TREE_VEC_ELT (binfos, i);
2952 tree basetype = BINFO_TYPE (base_binfo);
2953
2954 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2955 base_fndecls);
2956 }
2957
2958 fns = OVL_NEXT (fns);
2959
2960 /* ...then mark up all the base functions with overriders, preferring
2961 overriders to hiders. */
2962 if (base_fndecls)
2963 for (; fns; fns = OVL_NEXT (fns))
2964 {
2965 fndecl = OVL_CURRENT (fns);
2966 if (DECL_VINDEX (fndecl))
2967 mark_overriders (fndecl, base_fndecls);
2968 }
2969
2970 /* Now give a warning for all base functions without overriders,
2971 as they are hidden. */
2972 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2973 {
2974 if (! overrides (TREE_PURPOSE (base_fndecls),
2975 TREE_VALUE (base_fndecls)))
2976 {
2977 /* Here we know it is a hider, and no overrider exists. */
2978 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2979 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
2980 }
2981 }
2982 }
2983 }
2984
2985 /* Check for things that are invalid. There are probably plenty of other
2986 things we should check for also. */
2987
2988 static void
2989 finish_struct_anon (t)
2990 tree t;
2991 {
2992 tree field;
2993
2994 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2995 {
2996 if (TREE_STATIC (field))
2997 continue;
2998 if (TREE_CODE (field) != FIELD_DECL)
2999 continue;
3000
3001 if (DECL_NAME (field) == NULL_TREE
3002 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3003 {
3004 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3005 for (; elt; elt = TREE_CHAIN (elt))
3006 {
3007 if (DECL_ARTIFICIAL (elt))
3008 continue;
3009
3010 if (DECL_NAME (elt) == constructor_name (t))
3011 cp_pedwarn_at ("ANSI C++ forbids member `%D' with same name as enclosing class",
3012 elt);
3013
3014 if (TREE_CODE (elt) != FIELD_DECL)
3015 {
3016 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3017 elt);
3018 continue;
3019 }
3020
3021 if (TREE_PRIVATE (elt))
3022 cp_pedwarn_at ("private member `%#D' in anonymous union",
3023 elt);
3024 else if (TREE_PROTECTED (elt))
3025 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3026 elt);
3027
3028 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3029 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3030 }
3031 }
3032 }
3033 }
3034
3035 extern int interface_only, interface_unknown;
3036
3037 /* Create default constructors, assignment operators, and so forth for
3038 the type indicated by T, if they are needed.
3039 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3040 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3041 cannot have a default constructor, copy constructor taking a const
3042 reference argument, or an assignment operator, respectively. If a
3043 virtual destructor is created, its DECL is returned; otherwise the
3044 return value is NULL_TREE. */
3045
3046 static tree
3047 add_implicitly_declared_members (t, cant_have_default_ctor,
3048 cant_have_const_cctor,
3049 cant_have_assignment)
3050 tree t;
3051 int cant_have_default_ctor;
3052 int cant_have_const_cctor;
3053 int cant_have_assignment;
3054 {
3055 tree default_fn;
3056 tree implicit_fns = NULL_TREE;
3057 tree name = TYPE_IDENTIFIER (t);
3058 tree virtual_dtor = NULL_TREE;
3059 tree *f;
3060
3061 /* Destructor. */
3062 if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
3063 {
3064 default_fn = cons_up_default_function (t, name, 0);
3065 check_for_override (default_fn, t);
3066
3067 /* If we couldn't make it work, then pretend we didn't need it. */
3068 if (default_fn == void_type_node)
3069 TYPE_NEEDS_DESTRUCTOR (t) = 0;
3070 else
3071 {
3072 TREE_CHAIN (default_fn) = implicit_fns;
3073 implicit_fns = default_fn;
3074
3075 if (DECL_VINDEX (default_fn))
3076 virtual_dtor = default_fn;
3077 }
3078 }
3079 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3080
3081 /* Default constructor. */
3082 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3083 {
3084 default_fn = cons_up_default_function (t, name, 2);
3085 TREE_CHAIN (default_fn) = implicit_fns;
3086 implicit_fns = default_fn;
3087 }
3088
3089 /* Copy constructor. */
3090 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3091 {
3092 /* ARM 12.18: You get either X(X&) or X(const X&), but
3093 not both. --Chip */
3094 default_fn = cons_up_default_function (t, name,
3095 3 + cant_have_const_cctor);
3096 TREE_CHAIN (default_fn) = implicit_fns;
3097 implicit_fns = default_fn;
3098 }
3099
3100 /* Assignment operator. */
3101 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3102 {
3103 default_fn = cons_up_default_function (t, name,
3104 5 + cant_have_assignment);
3105 TREE_CHAIN (default_fn) = implicit_fns;
3106 implicit_fns = default_fn;
3107 }
3108
3109 /* Now, hook all of the new functions on to TYPE_METHODS,
3110 and add them to the CLASSTYPE_METHOD_VEC. */
3111 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3112 add_method (t, 0, *f);
3113 *f = TYPE_METHODS (t);
3114 TYPE_METHODS (t) = implicit_fns;
3115
3116 return virtual_dtor;
3117 }
3118
3119 /* Subroutine of finish_struct_1. Recursively count the number of fields
3120 in TYPE, including anonymous union members. */
3121
3122 static int
3123 count_fields (fields)
3124 tree fields;
3125 {
3126 tree x;
3127 int n_fields = 0;
3128 for (x = fields; x; x = TREE_CHAIN (x))
3129 {
3130 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3131 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3132 else
3133 n_fields += 1;
3134 }
3135 return n_fields;
3136 }
3137
3138 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3139 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3140
3141 static int
3142 add_fields_to_vec (fields, field_vec, idx)
3143 tree fields, field_vec;
3144 int idx;
3145 {
3146 tree x;
3147 for (x = fields; x; x = TREE_CHAIN (x))
3148 {
3149 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3150 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3151 else
3152 TREE_VEC_ELT (field_vec, idx++) = x;
3153 }
3154 return idx;
3155 }
3156
3157 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
3158 (or C++ class declaration).
3159
3160 For C++, we must handle the building of derived classes.
3161 Also, C++ allows static class members. The way that this is
3162 handled is to keep the field name where it is (as the DECL_NAME
3163 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
3164 of the field. layout_record and layout_union will know about this.
3165
3166 More C++ hair: inline functions have text in their
3167 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
3168 meaningful tree structure. After the struct has been laid out, set
3169 things up so that this can happen.
3170
3171 And still more: virtual functions. In the case of single inheritance,
3172 when a new virtual function is seen which redefines a virtual function
3173 from the base class, the new virtual function is placed into
3174 the virtual function table at exactly the same address that
3175 it had in the base class. When this is extended to multiple
3176 inheritance, the same thing happens, except that multiple virtual
3177 function tables must be maintained. The first virtual function
3178 table is treated in exactly the same way as in the case of single
3179 inheritance. Additional virtual function tables have different
3180 DELTAs, which tell how to adjust `this' to point to the right thing.
3181
3182 ATTRIBUTES is the set of decl attributes to be applied, if any. */
3183
3184 void
3185 finish_struct_1 (t)
3186 tree t;
3187 {
3188 int old;
3189 enum tree_code code = TREE_CODE (t);
3190 tree fields = TYPE_FIELDS (t);
3191 tree x, last_x, method_vec;
3192 int has_virtual;
3193 int max_has_virtual;
3194 tree pending_virtuals = NULL_TREE;
3195 tree pending_hard_virtuals = NULL_TREE;
3196 tree abstract_virtuals = NULL_TREE;
3197 tree vfield;
3198 tree vfields;
3199 tree virtual_dtor;
3200 int cant_have_default_ctor;
3201 int cant_have_const_ctor;
3202 int no_const_asn_ref;
3203 int has_mutable = 0;
3204 int n_fields = 0;
3205 int non_pod_class = 0;
3206
3207 /* The index of the first base class which has virtual
3208 functions. Only applied to non-virtual baseclasses. */
3209 int first_vfn_base_index;
3210
3211 int n_baseclasses;
3212 int any_default_members = 0;
3213 int const_sans_init = 0;
3214 int ref_sans_init = 0;
3215 tree access_decls = NULL_TREE;
3216 int aggregate = 1;
3217 int empty = 1;
3218 int has_pointers = 0;
3219 tree inline_friends;
3220
3221 if (TYPE_SIZE (t))
3222 {
3223 if (IS_AGGR_TYPE (t))
3224 cp_error ("redefinition of `%#T'", t);
3225 else
3226 my_friendly_abort (172);
3227 popclass ();
3228 return;
3229 }
3230
3231 GNU_xref_decl (current_function_decl, t);
3232
3233 /* If this type was previously laid out as a forward reference,
3234 make sure we lay it out again. */
3235
3236 TYPE_SIZE (t) = NULL_TREE;
3237 CLASSTYPE_GOT_SEMICOLON (t) = 0;
3238
3239 old = suspend_momentary ();
3240
3241 /* Install struct as DECL_FIELD_CONTEXT of each field decl.
3242 Also process specified field sizes.
3243 Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
3244 The specified size is found in the DECL_INITIAL.
3245 Store 0 there, except for ": 0" fields (so we can find them
3246 and delete them, below). */
3247
3248 if (TYPE_BINFO_BASETYPES (t))
3249 n_baseclasses = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t));
3250 else
3251 n_baseclasses = 0;
3252
3253 if (n_baseclasses > 0)
3254 {
3255 struct base_info base_info;
3256
3257 first_vfn_base_index = finish_base_struct (t, &base_info);
3258 /* Remember where we got our vfield from. */
3259 CLASSTYPE_VFIELD_PARENT (t) = first_vfn_base_index;
3260 has_virtual = base_info.has_virtual;
3261 max_has_virtual = base_info.max_has_virtual;
3262 vfield = base_info.vfield;
3263 vfields = base_info.vfields;
3264 CLASSTYPE_RTTI (t) = base_info.rtti;
3265 cant_have_default_ctor = base_info.cant_have_default_ctor;
3266 cant_have_const_ctor = base_info.cant_have_const_ctor;
3267 no_const_asn_ref = base_info.no_const_asn_ref;
3268 aggregate = 0;
3269 }
3270 else
3271 {
3272 first_vfn_base_index = -1;
3273 has_virtual = 0;
3274 max_has_virtual = has_virtual;
3275 vfield = NULL_TREE;
3276 vfields = NULL_TREE;
3277 CLASSTYPE_RTTI (t) = NULL_TREE;
3278 cant_have_default_ctor = 0;
3279 cant_have_const_ctor = 0;
3280 no_const_asn_ref = 0;
3281 }
3282
3283 /* The three of these are approximations which may later be
3284 modified. Needed at this point to make add_virtual_function
3285 and modify_vtable_entries work. */
3286 CLASSTYPE_VFIELDS (t) = vfields;
3287 CLASSTYPE_VFIELD (t) = vfield;
3288
3289 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3290 {
3291 GNU_xref_member (current_class_name, x);
3292
3293 /* If this was an evil function, don't keep it in class. */
3294 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3295 continue;
3296
3297 /* Do both of these, even though they're in the same union;
3298 if the insn `r' member and the size `i' member are
3299 different sizes, as on the alpha, the larger of the two
3300 will end up with garbage in it. */
3301 DECL_SAVED_INSNS (x) = 0;
3302 DECL_FIELD_SIZE (x) = 0;
3303
3304 check_for_override (x, t);
3305 if (DECL_ABSTRACT_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3306 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3307
3308 /* The name of the field is the original field name
3309 Save this in auxiliary field for later overloading. */
3310 if (DECL_VINDEX (x))
3311 {
3312 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3313 &has_virtual, x, t);
3314 if (DECL_ABSTRACT_VIRTUAL_P (x))
3315 abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
3316 #if 0
3317 /* XXX Why did I comment this out? (jason) */
3318 else
3319 TREE_USED (x) = 1;
3320 #endif
3321 }
3322 }
3323
3324 if (n_baseclasses)
3325 fields = chainon (build_vbase_pointer_fields (t), fields);
3326
3327 last_x = NULL_TREE;
3328 for (x = fields; x; x = TREE_CHAIN (x))
3329 {
3330 tree type = TREE_TYPE (x);
3331 GNU_xref_member (current_class_name, x);
3332
3333 if (TREE_CODE (x) == FIELD_DECL)
3334 {
3335 DECL_PACKED (x) |= TYPE_PACKED (t);
3336
3337 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3338 /* A zero-width bitfield doesn't do the trick. */;
3339 else
3340 empty = 0;
3341 }
3342
3343 if (TREE_CODE (x) == USING_DECL)
3344 {
3345 /* Save access declarations for later. */
3346 if (last_x)
3347 TREE_CHAIN (last_x) = TREE_CHAIN (x);
3348 else
3349 fields = TREE_CHAIN (x);
3350
3351 access_decls = tree_cons (NULL_TREE, x, access_decls);
3352 continue;
3353 }
3354
3355 last_x = x;
3356
3357 if (TREE_CODE (x) == TYPE_DECL
3358 || TREE_CODE (x) == TEMPLATE_DECL)
3359 continue;
3360
3361 /* If we've gotten this far, it's a data member, possibly static,
3362 or an enumerator. */
3363
3364 DECL_FIELD_CONTEXT (x) = t;
3365
3366 /* ``A local class cannot have static data members.'' ARM 9.4 */
3367 if (current_function_decl && TREE_STATIC (x))
3368 cp_error_at ("field `%D' in local class cannot be static", x);
3369
3370 /* Perform error checking that did not get done in
3371 grokdeclarator. */
3372 if (TREE_CODE (type) == FUNCTION_TYPE)
3373 {
3374 cp_error_at ("field `%D' invalidly declared function type",
3375 x);
3376 type = build_pointer_type (type);
3377 TREE_TYPE (x) = type;
3378 }
3379 else if (TREE_CODE (type) == METHOD_TYPE)
3380 {
3381 cp_error_at ("field `%D' invalidly declared method type", x);
3382 type = build_pointer_type (type);
3383 TREE_TYPE (x) = type;
3384 }
3385 else if (TREE_CODE (type) == OFFSET_TYPE)
3386 {
3387 cp_error_at ("field `%D' invalidly declared offset type", x);
3388 type = build_pointer_type (type);
3389 TREE_TYPE (x) = type;
3390 }
3391
3392 #if 0
3393 if (DECL_NAME (x) == constructor_name (t))
3394 cant_have_default_ctor = 1;
3395 #endif
3396
3397 if (type == error_mark_node)
3398 continue;
3399
3400 DECL_SAVED_INSNS (x) = 0;
3401 DECL_FIELD_SIZE (x) = 0;
3402
3403 /* When this goes into scope, it will be a non-local reference. */
3404 DECL_NONLOCAL (x) = 1;
3405
3406 if (TREE_CODE (x) == CONST_DECL)
3407 continue;
3408
3409 if (TREE_CODE (x) == VAR_DECL)
3410 {
3411 if (TREE_CODE (t) == UNION_TYPE)
3412 /* Unions cannot have static members. */
3413 cp_error_at ("field `%D' declared static in union", x);
3414
3415 continue;
3416 }
3417
3418 /* Now it can only be a FIELD_DECL. */
3419
3420 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3421 aggregate = 0;
3422
3423 /* If this is of reference type, check if it needs an init.
3424 Also do a little ANSI jig if necessary. */
3425 if (TREE_CODE (type) == REFERENCE_TYPE)
3426 {
3427 non_pod_class = 1;
3428
3429 if (DECL_INITIAL (x) == NULL_TREE)
3430 ref_sans_init = 1;
3431
3432 /* ARM $12.6.2: [A member initializer list] (or, for an
3433 aggregate, initialization by a brace-enclosed list) is the
3434 only way to initialize nonstatic const and reference
3435 members. */
3436 cant_have_default_ctor = 1;
3437 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3438
3439 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3440 {
3441 if (DECL_NAME (x))
3442 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3443 else
3444 cp_warning_at ("non-static reference in class without a constructor", x);
3445 }
3446 }
3447
3448 while (TREE_CODE (type) == ARRAY_TYPE)
3449 type = TREE_TYPE (type);
3450
3451 if (TREE_CODE (type) == POINTER_TYPE)
3452 has_pointers = 1;
3453
3454 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3455 has_mutable = 1;
3456
3457 if (! pod_type_p (type) || TYPE_PTRMEM_P (type)
3458 || TYPE_PTRMEMFUNC_P (type))
3459 non_pod_class = 1;
3460
3461 /* If any field is const, the structure type is pseudo-const. */
3462 if (CP_TYPE_CONST_P (type))
3463 {
3464 C_TYPE_FIELDS_READONLY (t) = 1;
3465 if (DECL_INITIAL (x) == NULL_TREE)
3466 const_sans_init = 1;
3467
3468 /* ARM $12.6.2: [A member initializer list] (or, for an
3469 aggregate, initialization by a brace-enclosed list) is the
3470 only way to initialize nonstatic const and reference
3471 members. */
3472 cant_have_default_ctor = 1;
3473 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3474
3475 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3476 {
3477 if (DECL_NAME (x))
3478 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3479 else
3480 cp_warning_at ("non-static const member in class without a constructor", x);
3481 }
3482 }
3483 else
3484 {
3485 /* A field that is pseudo-const makes the structure
3486 likewise. */
3487 if (IS_AGGR_TYPE (type))
3488 {
3489 if (C_TYPE_FIELDS_READONLY (type))
3490 C_TYPE_FIELDS_READONLY (t) = 1;
3491 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3492 const_sans_init = 1;
3493 }
3494 }
3495
3496 /* We set DECL_C_BIT_FIELD in grokbitfield.
3497 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3498 if (DECL_C_BIT_FIELD (x))
3499 {
3500 /* Invalid bit-field size done by grokfield. */
3501 /* Detect invalid bit-field type. Simply checking if TYPE is
3502 integral is insufficient, as that is the array core of the
3503 field type. If TREE_TYPE (x) is integral, then TYPE must be
3504 the same. */
3505 if (DECL_INITIAL (x)
3506 && ! INTEGRAL_TYPE_P (TREE_TYPE (x)))
3507 {
3508 cp_error_at ("bit-field `%#D' with non-integral type", x);
3509 DECL_INITIAL (x) = NULL;
3510 }
3511
3512 /* Detect and ignore out of range field width. */
3513 if (DECL_INITIAL (x))
3514 {
3515 tree w = DECL_INITIAL (x);
3516 register int width = 0;
3517
3518 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3519 STRIP_NOPS (w);
3520
3521 /* detect invalid field size. */
3522 if (TREE_CODE (w) == CONST_DECL)
3523 w = DECL_INITIAL (w);
3524 else if (TREE_READONLY_DECL_P (w))
3525 w = decl_constant_value (w);
3526
3527 if (TREE_CODE (w) != INTEGER_CST)
3528 {
3529 cp_error_at ("bit-field `%D' width not an integer constant",
3530 x);
3531 DECL_INITIAL (x) = NULL_TREE;
3532 }
3533 else if (width = TREE_INT_CST_LOW (w),
3534 width < 0)
3535 {
3536 DECL_INITIAL (x) = NULL;
3537 cp_error_at ("negative width in bit-field `%D'", x);
3538 }
3539 else if (width == 0 && DECL_NAME (x) != 0)
3540 {
3541 DECL_INITIAL (x) = NULL;
3542 cp_error_at ("zero width for bit-field `%D'", x);
3543 }
3544 else if (width
3545 > TYPE_PRECISION (long_long_unsigned_type_node))
3546 {
3547 /* The backend will dump if you try to use something
3548 too big; avoid that. */
3549 DECL_INITIAL (x) = NULL;
3550 sorry ("bit-fields larger than %d bits",
3551 TYPE_PRECISION (long_long_unsigned_type_node));
3552 cp_error_at (" in declaration of `%D'", x);
3553 }
3554 else if (width > TYPE_PRECISION (type)
3555 && TREE_CODE (type) != ENUMERAL_TYPE
3556 && TREE_CODE (type) != BOOLEAN_TYPE)
3557 {
3558 cp_warning_at ("width of `%D' exceeds its type", x);
3559 }
3560 else if (TREE_CODE (type) == ENUMERAL_TYPE
3561 && ((min_precision (TYPE_MIN_VALUE (type),
3562 TREE_UNSIGNED (type)) > width)
3563 || (min_precision (TYPE_MAX_VALUE (type),
3564 TREE_UNSIGNED (type)) > width)))
3565 {
3566 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3567 x, type);
3568 }
3569
3570 if (DECL_INITIAL (x))
3571 {
3572 DECL_INITIAL (x) = NULL_TREE;
3573 DECL_FIELD_SIZE (x) = width;
3574 DECL_BIT_FIELD (x) = 1;
3575
3576 if (width == 0)
3577 {
3578 #ifdef EMPTY_FIELD_BOUNDARY
3579 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3580 EMPTY_FIELD_BOUNDARY);
3581 #endif
3582 #ifdef PCC_BITFIELD_TYPE_MATTERS
3583 if (PCC_BITFIELD_TYPE_MATTERS)
3584 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3585 TYPE_ALIGN (type));
3586 #endif
3587 }
3588 }
3589 }
3590 else
3591 /* Non-bit-fields are aligned for their type. */
3592 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (type));
3593 }
3594 else
3595 {
3596 if (CLASS_TYPE_P (type) && ! ANON_AGGR_TYPE_P (type))
3597 {
3598 /* Never let anything with uninheritable virtuals
3599 make it through without complaint. */
3600 abstract_virtuals_error (x, type);
3601
3602 if (code == UNION_TYPE)
3603 {
3604 const char *fie = NULL;
3605 if (TYPE_NEEDS_CONSTRUCTING (type))
3606 fie = "constructor";
3607 else if (TYPE_NEEDS_DESTRUCTOR (type))
3608 fie = "destructor";
3609 else if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3610 fie = "copy assignment operator";
3611 if (fie)
3612 cp_error_at ("member `%#D' with %s not allowed in union", x,
3613 fie);
3614 }
3615 else
3616 {
3617 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3618 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3619 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3620 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3621 }
3622
3623 if (!TYPE_HAS_CONST_INIT_REF (type))
3624 cant_have_const_ctor = 1;
3625
3626 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3627 no_const_asn_ref = 1;
3628
3629 if (TYPE_HAS_CONSTRUCTOR (type)
3630 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3631 {
3632 cant_have_default_ctor = 1;
3633 #if 0
3634 /* This is wrong for aggregates. */
3635 if (! TYPE_HAS_CONSTRUCTOR (t))
3636 {
3637 if (DECL_NAME (x))
3638 cp_pedwarn_at ("member `%#D' with only non-default constructor", x);
3639 else
3640 cp_pedwarn_at ("member with only non-default constructor", x);
3641 cp_pedwarn_at ("in class without a constructor",
3642 x);
3643 }
3644 #endif
3645 }
3646 }
3647 if (DECL_INITIAL (x) != NULL_TREE)
3648 {
3649 /* `build_class_init_list' does not recognize
3650 non-FIELD_DECLs. */
3651 if (code == UNION_TYPE && any_default_members != 0)
3652 cp_error_at ("multiple fields in union `%T' initialized");
3653 any_default_members = 1;
3654 }
3655 }
3656 }
3657
3658 /* If this type has any constant members which did not come
3659 with their own initialization, mark that fact here. It is
3660 not an error here, since such types can be saved either by their
3661 constructors, or by fortuitous initialization. */
3662 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
3663 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
3664 CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
3665 CLASSTYPE_HAS_MUTABLE (t) = has_mutable;
3666
3667 /* Effective C++ rule 11. */
3668 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3669 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3670 {
3671 cp_warning ("`%#T' has pointer data members", t);
3672
3673 if (! TYPE_HAS_INIT_REF (t))
3674 {
3675 cp_warning (" but does not override `%T(const %T&)'", t, t);
3676 if (! TYPE_HAS_ASSIGN_REF (t))
3677 cp_warning (" or `operator=(const %T&)'", t);
3678 }
3679 else if (! TYPE_HAS_ASSIGN_REF (t))
3680 cp_warning (" but does not override `operator=(const %T&)'", t);
3681 }
3682
3683 /* Do some bookkeeping that will guide the generation of implicitly
3684 declared member functions. */
3685 TYPE_HAS_COMPLEX_INIT_REF (t)
3686 |= (TYPE_HAS_INIT_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3687 || has_virtual || any_default_members);
3688 TYPE_NEEDS_CONSTRUCTING (t)
3689 |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3690 || has_virtual || any_default_members);
3691 CLASSTYPE_NON_AGGREGATE (t)
3692 = ! aggregate || has_virtual || TYPE_HAS_CONSTRUCTOR (t);
3693 CLASSTYPE_NON_POD_P (t)
3694 = non_pod_class || CLASSTYPE_NON_AGGREGATE (t)
3695 || TYPE_HAS_DESTRUCTOR (t) || TYPE_HAS_ASSIGN_REF (t);
3696 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
3697 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
3698 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
3699
3700 /* Synthesize any needed methods. Note that methods will be synthesized
3701 for anonymous unions; grok_x_components undoes that. */
3702 virtual_dtor
3703 = add_implicitly_declared_members (t, cant_have_default_ctor,
3704 cant_have_const_ctor,
3705 no_const_asn_ref);
3706 if (virtual_dtor)
3707 add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3708 &has_virtual, virtual_dtor, t);
3709
3710 if (TYPE_METHODS (t))
3711 {
3712 finish_struct_methods (t);
3713 method_vec = CLASSTYPE_METHOD_VEC (t);
3714 }
3715 else
3716 {
3717 method_vec = 0;
3718
3719 /* Just in case these got accidentally
3720 filled in by syntax errors. */
3721 TYPE_HAS_CONSTRUCTOR (t) = 0;
3722 TYPE_HAS_DESTRUCTOR (t) = 0;
3723 }
3724
3725 for (access_decls = nreverse (access_decls); access_decls;
3726 access_decls = TREE_CHAIN (access_decls))
3727 handle_using_decl (TREE_VALUE (access_decls), t, method_vec, fields);
3728
3729 if (vfield == NULL_TREE && has_virtual)
3730 {
3731 /* We build this decl with vtbl_ptr_type_node, which is a
3732 `vtable_entry_type*'. It might seem more precise to use
3733 `vtable_entry_type (*)[N]' where N is the number of firtual
3734 functions. However, that would require the vtable pointer in
3735 base classes to have a different type than the vtable pointer
3736 in derived classes. We could make that happen, but that
3737 still wouldn't solve all the problems. In particular, the
3738 type-based alias analysis code would decide that assignments
3739 to the base class vtable pointer can't alias assignments to
3740 the derived class vtable pointer, since they have different
3741 types. Thus, in an derived class destructor, where the base
3742 class constructor was inlined, we could generate bad code for
3743 setting up the vtable pointer.
3744
3745 Therefore, we use one type for all vtable pointers. We still
3746 use a type-correct type; it's just doesn't indicate the array
3747 bounds. That's better than using `void*' or some such; it's
3748 cleaner, and it let's the alias analysis code know that these
3749 stores cannot alias stores to void*! */
3750 vfield = build_lang_decl (FIELD_DECL, get_vfield_name (t),
3751 vtbl_ptr_type_node);
3752 /* If you change any of the below, take a look at all the
3753 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
3754 them too. */
3755 DECL_ASSEMBLER_NAME (vfield) = get_identifier (VFIELD_BASE);
3756 CLASSTYPE_VFIELD (t) = vfield;
3757 DECL_VIRTUAL_P (vfield) = 1;
3758 DECL_ARTIFICIAL (vfield) = 1;
3759 DECL_FIELD_CONTEXT (vfield) = t;
3760 DECL_CLASS_CONTEXT (vfield) = t;
3761 DECL_FCONTEXT (vfield) = t;
3762 DECL_SAVED_INSNS (vfield) = 0;
3763 DECL_FIELD_SIZE (vfield) = 0;
3764 DECL_ALIGN (vfield) = TYPE_ALIGN (ptr_type_node);
3765 #if 0
3766 /* This is more efficient, but breaks binary compatibility, turn
3767 it on sometime when we don't care. If we turn it on, we also
3768 have to enable the code in dfs_init_vbase_pointers. */
3769 /* vfield is always first entry in structure. */
3770 TREE_CHAIN (vfield) = fields;
3771 fields = vfield;
3772 #else
3773 if (last_x)
3774 {
3775 my_friendly_assert (TREE_CHAIN (last_x) == NULL_TREE, 175);
3776 TREE_CHAIN (last_x) = vfield;
3777 last_x = vfield;
3778 }
3779 else
3780 fields = vfield;
3781 #endif
3782 empty = 0;
3783 vfields = chainon (vfields, build_tree_list (NULL_TREE, t));
3784 }
3785
3786 /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
3787
3788 C++: maybe we will support default field initialization some day... */
3789
3790 /* Delete all duplicate fields from the fields */
3791 delete_duplicate_fields (fields);
3792
3793 /* Now we have the nearly final fieldlist for the data fields. Record it,
3794 then lay out the structure or union (including the fields). */
3795
3796 TYPE_FIELDS (t) = fields;
3797
3798 if (n_baseclasses)
3799 {
3800 last_x = build_base_fields (t);
3801
3802 /* If all our bases are empty, we can be empty too. */
3803 for (x = last_x; empty && x; x = TREE_CHAIN (x))
3804 if (DECL_SIZE (x) != integer_zero_node)
3805 empty = 0;
3806 }
3807
3808 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
3809 we have to save this before we start modifying
3810 TYPE_NONCOPIED_PARTS. */
3811 inline_friends = CLASSTYPE_INLINE_FRIENDS (t);
3812 CLASSTYPE_INLINE_FRIENDS (t) = NULL_TREE;
3813
3814 if (empty)
3815 {
3816 /* C++: do not let empty structures exist. */
3817 tree decl = build_lang_decl
3818 (FIELD_DECL, NULL_TREE, char_type_node);
3819 TREE_CHAIN (decl) = fields;
3820 TYPE_FIELDS (t) = decl;
3821 TYPE_NONCOPIED_PARTS (t)
3822 = tree_cons (NULL_TREE, decl, TYPE_NONCOPIED_PARTS (t));
3823 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
3824 }
3825
3826 if (n_baseclasses)
3827 TYPE_FIELDS (t) = chainon (last_x, TYPE_FIELDS (t));
3828
3829 layout_type (t);
3830
3831 /* Remember the size and alignment of the class before adding
3832 the virtual bases. */
3833 if (empty && flag_new_abi)
3834 CLASSTYPE_SIZE (t) = integer_zero_node;
3835 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
3836 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
3837 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
3838 else
3839 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
3840 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
3841
3842 finish_struct_anon (t);
3843
3844 /* Set the TYPE_DECL for this type to contain the right
3845 value for DECL_OFFSET, so that we can use it as part
3846 of a COMPONENT_REF for multiple inheritance. */
3847
3848 layout_decl (TYPE_MAIN_DECL (t), 0);
3849
3850 /* Now fix up any virtual base class types that we left lying
3851 around. We must get these done before we try to lay out the
3852 virtual function table. */
3853 pending_hard_virtuals = nreverse (pending_hard_virtuals);
3854
3855 if (n_baseclasses)
3856 /* layout_basetypes will remove the base subobject fields. */
3857 max_has_virtual = layout_basetypes (t, max_has_virtual);
3858 if (empty)
3859 TYPE_FIELDS (t) = fields;
3860
3861 my_friendly_assert (TYPE_FIELDS (t) == fields, 981117);
3862
3863 /* Delete all zero-width bit-fields from the front of the fieldlist */
3864 while (fields && DECL_C_BIT_FIELD (fields)
3865 && DECL_INITIAL (fields))
3866 fields = TREE_CHAIN (fields);
3867 /* Delete all such fields from the rest of the fields. */
3868 for (x = fields; x;)
3869 {
3870 if (TREE_CHAIN (x) && DECL_C_BIT_FIELD (TREE_CHAIN (x))
3871 && DECL_INITIAL (TREE_CHAIN (x)))
3872 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
3873 else
3874 x = TREE_CHAIN (x);
3875 }
3876 TYPE_FIELDS (t) = fields;
3877
3878 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
3879 {
3880 tree vbases;
3881
3882 vbases = CLASSTYPE_VBASECLASSES (t);
3883
3884 {
3885 /* Now fixup overrides of all functions in vtables from all
3886 direct or indirect virtual base classes. */
3887 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3888 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3889
3890 for (i = 0; i < n_baseclasses; i++)
3891 {
3892 tree base_binfo = TREE_VEC_ELT (binfos, i);
3893 tree basetype = BINFO_TYPE (base_binfo);
3894 tree vbases;
3895
3896 vbases = CLASSTYPE_VBASECLASSES (basetype);
3897 while (vbases)
3898 {
3899 merge_overrides (binfo_member (BINFO_TYPE (vbases),
3900 CLASSTYPE_VBASECLASSES (t)),
3901 vbases, 1, t);
3902 vbases = TREE_CHAIN (vbases);
3903 }
3904 }
3905 }
3906 }
3907
3908 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
3909 might need to know it for setting up the offsets in the vtable
3910 (or in thunks) below. */
3911 if (vfield != NULL_TREE
3912 && DECL_FIELD_CONTEXT (vfield) != t)
3913 {
3914 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
3915 tree offset = BINFO_OFFSET (binfo);
3916
3917 vfield = copy_node (vfield);
3918 copy_lang_decl (vfield);
3919
3920 if (! integer_zerop (offset))
3921 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
3922 DECL_FIELD_CONTEXT (vfield) = t;
3923 DECL_CLASS_CONTEXT (vfield) = t;
3924 DECL_FIELD_BITPOS (vfield)
3925 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
3926 CLASSTYPE_VFIELD (t) = vfield;
3927 }
3928
3929 #ifdef NOTQUITE
3930 cp_warning ("Doing hard virtuals for %T...", t);
3931 #endif
3932
3933 if (has_virtual > max_has_virtual)
3934 max_has_virtual = has_virtual;
3935 if (max_has_virtual > 0)
3936 TYPE_VIRTUAL_P (t) = 1;
3937
3938 if (flag_rtti && TYPE_VIRTUAL_P (t) && !pending_hard_virtuals)
3939 modify_all_vtables (t, NULL_TREE);
3940
3941 while (pending_hard_virtuals)
3942 {
3943 modify_all_vtables (t,
3944 TREE_VALUE (pending_hard_virtuals));
3945 pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
3946 }
3947
3948 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
3949 {
3950 tree vbases;
3951 /* Now fixup any virtual function entries from virtual bases
3952 that have different deltas. This has to come after we do the
3953 pending hard virtuals, as we might have a function that comes
3954 from multiple virtual base instances that is only overridden
3955 by a hard virtual above. */
3956 vbases = CLASSTYPE_VBASECLASSES (t);
3957 while (vbases)
3958 {
3959 /* We might be able to shorten the amount of work we do by
3960 only doing this for vtables that come from virtual bases
3961 that have differing offsets, but don't want to miss any
3962 entries. */
3963 fixup_vtable_deltas (vbases, 1, t);
3964 vbases = TREE_CHAIN (vbases);
3965 }
3966 }
3967
3968 /* Under our model of GC, every C++ class gets its own virtual
3969 function table, at least virtually. */
3970 if (pending_virtuals)
3971 {
3972 pending_virtuals = nreverse (pending_virtuals);
3973 /* We must enter these virtuals into the table. */
3974 if (first_vfn_base_index < 0)
3975 {
3976 if (! CLASSTYPE_COM_INTERFACE (t))
3977 {
3978 /* The second slot is for the tdesc pointer when thunks are used. */
3979 if (flag_vtable_thunks)
3980 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
3981
3982 /* The first slot is for the rtti offset. */
3983 pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
3984
3985 set_rtti_entry (pending_virtuals,
3986 convert (ssizetype, integer_zero_node), t);
3987 }
3988 build_vtable (NULL_TREE, t);
3989 }
3990 else
3991 {
3992 /* Here we know enough to change the type of our virtual
3993 function table, but we will wait until later this function. */
3994
3995 if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
3996 build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index), t);
3997 }
3998
3999 /* If this type has basetypes with constructors, then those
4000 constructors might clobber the virtual function table. But
4001 they don't if the derived class shares the exact vtable of the base
4002 class. */
4003
4004 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4005 }
4006 else if (first_vfn_base_index >= 0)
4007 {
4008 tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index);
4009 /* This class contributes nothing new to the virtual function
4010 table. However, it may have declared functions which
4011 went into the virtual function table "inherited" from the
4012 base class. If so, we grab a copy of those updated functions,
4013 and pretend they are ours. */
4014
4015 /* See if we should steal the virtual info from base class. */
4016 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
4017 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
4018 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
4019 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
4020 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
4021 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4022 }
4023
4024 if (max_has_virtual || first_vfn_base_index >= 0)
4025 {
4026 CLASSTYPE_VSIZE (t) = has_virtual;
4027 if (first_vfn_base_index >= 0)
4028 {
4029 if (pending_virtuals)
4030 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t),
4031 pending_virtuals);
4032 }
4033 else if (has_virtual)
4034 {
4035 TYPE_BINFO_VIRTUALS (t) = pending_virtuals;
4036 DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1;
4037 }
4038 }
4039
4040 /* Now lay out the virtual function table. */
4041 if (has_virtual)
4042 {
4043 /* Use size_int so values are memoized in common cases. */
4044 tree itype = build_index_type (size_int (has_virtual));
4045 tree atype = build_cplus_array_type (vtable_entry_type, itype);
4046
4047 layout_type (atype);
4048
4049 CLASSTYPE_VFIELD (t) = vfield;
4050
4051 /* We may have to grow the vtable. */
4052 if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
4053 {
4054 TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
4055 DECL_SIZE (TYPE_BINFO_VTABLE (t)) = 0;
4056 layout_decl (TYPE_BINFO_VTABLE (t), 0);
4057 /* At one time the vtable info was grabbed 2 words at a time. This
4058 fails on sparc unless you have 8-byte alignment. (tiemann) */
4059 DECL_ALIGN (TYPE_BINFO_VTABLE (t))
4060 = MAX (TYPE_ALIGN (double_type_node),
4061 DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
4062 }
4063 }
4064 else if (first_vfn_base_index >= 0)
4065 CLASSTYPE_VFIELD (t) = vfield;
4066 CLASSTYPE_VFIELDS (t) = vfields;
4067
4068 finish_struct_bits (t, max_has_virtual);
4069
4070 /* Complete the rtl for any static member objects of the type we're
4071 working on. */
4072 for (x = fields; x; x = TREE_CHAIN (x))
4073 {
4074 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4075 && TREE_TYPE (x) == t)
4076 {
4077 DECL_MODE (x) = TYPE_MODE (t);
4078 make_decl_rtl (x, NULL, 0);
4079 }
4080 }
4081
4082 /* Done with FIELDS...now decide whether to sort these for
4083 faster lookups later. Don't worry about optimizing
4084 for structs only declared in inline functions...they're
4085 not going to be referenced anywhere else.
4086
4087 The C front-end only does this when n_fields > 15. We use
4088 a smaller number because most searches fail (succeeding
4089 ultimately as the search bores through the inheritance
4090 hierarchy), and we want this failure to occur quickly. */
4091
4092 n_fields = count_fields (fields);
4093 if (n_fields > 7 && !allocation_temporary_p ())
4094 {
4095 tree field_vec = make_tree_vec (n_fields);
4096 add_fields_to_vec (fields, field_vec, 0);
4097 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
4098 (int (*)(const void *, const void *))field_decl_cmp);
4099 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
4100 retrofit_lang_decl (TYPE_MAIN_DECL (t));
4101 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
4102 }
4103
4104 if (TYPE_HAS_CONSTRUCTOR (t))
4105 {
4106 tree vfields = CLASSTYPE_VFIELDS (t);
4107
4108 while (vfields)
4109 {
4110 /* Mark the fact that constructor for T
4111 could affect anybody inheriting from T
4112 who wants to initialize vtables for VFIELDS's type. */
4113 if (VF_DERIVED_VALUE (vfields))
4114 TREE_ADDRESSABLE (vfields) = 1;
4115 vfields = TREE_CHAIN (vfields);
4116 }
4117 }
4118
4119 /* Write out inline function definitions. */
4120 do_inline_function_hair (t, inline_friends);
4121
4122 if (CLASSTYPE_VSIZE (t) != 0)
4123 {
4124 /* In addition to this one, all the other vfields should be listed. */
4125 /* Before that can be done, we have to have FIELD_DECLs for them, and
4126 a place to find them. */
4127 TYPE_NONCOPIED_PARTS (t)
4128 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
4129 vfield, TYPE_NONCOPIED_PARTS (t));
4130
4131 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
4132 && DECL_VINDEX (TREE_VEC_ELT (method_vec, 1)) == NULL_TREE)
4133 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
4134 t);
4135 }
4136
4137 /* Make the rtl for any new vtables we have created, and unmark
4138 the base types we marked. */
4139 finish_vtbls (TYPE_BINFO (t), 1, t);
4140 hack_incomplete_structures (t);
4141
4142 #if 0
4143 if (TYPE_NAME (t) && TYPE_IDENTIFIER (t))
4144 undo_template_name_overload (TYPE_IDENTIFIER (t), 1);
4145 #endif
4146
4147 resume_momentary (old);
4148
4149 if (warn_overloaded_virtual)
4150 warn_hidden (t);
4151
4152 #if 0
4153 /* This has to be done after we have sorted out what to do with
4154 the enclosing type. */
4155 if (write_symbols != DWARF_DEBUG)
4156 {
4157 /* Be smarter about nested classes here. If a type is nested,
4158 only output it if we would output the enclosing type. */
4159 if (DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (t)))
4160 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = TREE_ASM_WRITTEN (TYPE_MAIN_DECL (t));
4161 }
4162 #endif
4163
4164 if (write_symbols != DWARF_DEBUG && write_symbols != DWARF2_DEBUG)
4165 {
4166 /* If the type has methods, we want to think about cutting down
4167 the amount of symbol table stuff we output. The value stored in
4168 the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
4169 For example, if a member function is seen and we decide to
4170 write out that member function, then we can change the value
4171 of the DECL_IGNORED_P slot, and the type will be output when
4172 that member function's debug info is written out.
4173
4174 We can't do this with DWARF, which does not support name
4175 references between translation units. */
4176 if (CLASSTYPE_METHOD_VEC (t))
4177 {
4178 /* Don't output full info about any type
4179 which does not have its implementation defined here. */
4180 if (CLASSTYPE_INTERFACE_ONLY (t))
4181 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4182 #if 0
4183 /* XXX do something about this. */
4184 else if (CLASSTYPE_INTERFACE_UNKNOWN (t))
4185 /* Only a first approximation! */
4186 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4187 #endif
4188 }
4189 else if (CLASSTYPE_INTERFACE_ONLY (t))
4190 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4191 }
4192
4193 /* Finish debugging output for this type. */
4194 rest_of_type_compilation (t, toplevel_bindings_p ());
4195
4196 return;
4197 }
4198
4199 /* When T was built up, the member declarations were added in reverse
4200 order. Rearrange them to declaration order. */
4201
4202 void
4203 unreverse_member_declarations (t)
4204 tree t;
4205 {
4206 tree next;
4207 tree prev;
4208 tree x;
4209
4210 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
4211 reverse order. Put them in declaration order now. */
4212 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
4213 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
4214
4215 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
4216 reverse order, so we can't just use nreverse. */
4217 prev = NULL_TREE;
4218 for (x = TYPE_FIELDS (t);
4219 x && TREE_CODE (x) != TYPE_DECL;
4220 x = next)
4221 {
4222 next = TREE_CHAIN (x);
4223 TREE_CHAIN (x) = prev;
4224 prev = x;
4225 }
4226 if (prev)
4227 {
4228 TREE_CHAIN (TYPE_FIELDS (t)) = x;
4229 if (prev)
4230 TYPE_FIELDS (t) = prev;
4231 }
4232 }
4233
4234 tree
4235 finish_struct (t, attributes)
4236 tree t, attributes;
4237 {
4238 /* Now that we've got all the field declarations, reverse everything
4239 as necessary. */
4240 unreverse_member_declarations (t);
4241
4242 cplus_decl_attributes (t, attributes, NULL_TREE);
4243
4244 if (processing_template_decl)
4245 {
4246 finish_struct_methods (t);
4247 TYPE_SIZE (t) = integer_zero_node;
4248 }
4249 else
4250 finish_struct_1 (t);
4251
4252 TYPE_BEING_DEFINED (t) = 0;
4253
4254 if (current_class_type)
4255 popclass ();
4256 else
4257 error ("trying to finish struct, but kicked out due to previous parse errors.");
4258
4259 return t;
4260 }
4261 \f
4262 /* Return the dynamic type of INSTANCE, if known.
4263 Used to determine whether the virtual function table is needed
4264 or not.
4265
4266 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4267 of our knowledge of its type. */
4268
4269 static tree
4270 fixed_type_or_null (instance, nonnull)
4271 tree instance;
4272 int *nonnull;
4273 {
4274 switch (TREE_CODE (instance))
4275 {
4276 case INDIRECT_REF:
4277 /* Check that we are not going through a cast of some sort. */
4278 if (TREE_TYPE (instance)
4279 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
4280 instance = TREE_OPERAND (instance, 0);
4281 /* fall through... */
4282 case CALL_EXPR:
4283 /* This is a call to a constructor, hence it's never zero. */
4284 if (TREE_HAS_CONSTRUCTOR (instance))
4285 {
4286 if (nonnull)
4287 *nonnull = 1;
4288 return TREE_TYPE (instance);
4289 }
4290 return NULL_TREE;
4291
4292 case SAVE_EXPR:
4293 /* This is a call to a constructor, hence it's never zero. */
4294 if (TREE_HAS_CONSTRUCTOR (instance))
4295 {
4296 if (nonnull)
4297 *nonnull = 1;
4298 return TREE_TYPE (instance);
4299 }
4300 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4301
4302 case RTL_EXPR:
4303 return NULL_TREE;
4304
4305 case PLUS_EXPR:
4306 case MINUS_EXPR:
4307 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
4308 /* Propagate nonnull. */
4309 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4310 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
4311 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4312 return NULL_TREE;
4313
4314 case NOP_EXPR:
4315 case CONVERT_EXPR:
4316 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4317
4318 case ADDR_EXPR:
4319 if (nonnull)
4320 *nonnull = 1;
4321 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4322
4323 case COMPONENT_REF:
4324 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
4325
4326 case VAR_DECL:
4327 case FIELD_DECL:
4328 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
4329 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
4330 {
4331 if (nonnull)
4332 *nonnull = 1;
4333 return TREE_TYPE (TREE_TYPE (instance));
4334 }
4335 /* fall through... */
4336 case TARGET_EXPR:
4337 case PARM_DECL:
4338 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
4339 {
4340 if (nonnull)
4341 *nonnull = 1;
4342 return TREE_TYPE (instance);
4343 }
4344 else if (nonnull)
4345 {
4346 if (instance == current_class_ptr
4347 && flag_this_is_variable <= 0)
4348 {
4349 /* Normally, 'this' must be non-null. */
4350 if (flag_this_is_variable == 0)
4351 *nonnull = 1;
4352
4353 /* <0 means we're in a constructor and we know our type. */
4354 if (flag_this_is_variable < 0)
4355 return TREE_TYPE (TREE_TYPE (instance));
4356 }
4357 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4358 /* Reference variables should be references to objects. */
4359 *nonnull = 1;
4360 }
4361 return NULL_TREE;
4362
4363 default:
4364 return NULL_TREE;
4365 }
4366 }
4367
4368 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
4369 to the static type. We also handle the case where INSTANCE is really
4370 a pointer.
4371
4372 Used to determine whether the virtual function table is needed
4373 or not.
4374
4375 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4376 of our knowledge of its type. */
4377
4378 int
4379 resolves_to_fixed_type_p (instance, nonnull)
4380 tree instance;
4381 int *nonnull;
4382 {
4383 tree t = TREE_TYPE (instance);
4384 tree fixed = fixed_type_or_null (instance, nonnull);
4385 if (fixed == NULL_TREE)
4386 return 0;
4387 if (POINTER_TYPE_P (t))
4388 t = TREE_TYPE (t);
4389 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
4390 }
4391
4392 \f
4393 void
4394 init_class_processing ()
4395 {
4396 current_class_depth = 0;
4397 current_class_stack_size = 10;
4398 current_class_stack
4399 = (class_stack_node_t) xmalloc (current_class_stack_size
4400 * sizeof (struct class_stack_node));
4401
4402 access_default_node = build_int_2 (0, 0);
4403 access_public_node = build_int_2 (1, 0);
4404 access_protected_node = build_int_2 (2, 0);
4405 access_private_node = build_int_2 (3, 0);
4406 access_default_virtual_node = build_int_2 (4, 0);
4407 access_public_virtual_node = build_int_2 (5, 0);
4408 access_protected_virtual_node = build_int_2 (6, 0);
4409 access_private_virtual_node = build_int_2 (7, 0);
4410 }
4411
4412 /* Set current scope to NAME. CODE tells us if this is a
4413 STRUCT, UNION, or ENUM environment.
4414
4415 NAME may end up being NULL_TREE if this is an anonymous or
4416 late-bound struct (as in "struct { ... } foo;") */
4417
4418 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
4419 appropriate values, found by looking up the type definition of
4420 NAME (as a CODE).
4421
4422 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
4423 which can be seen locally to the class. They are shadowed by
4424 any subsequent local declaration (including parameter names).
4425
4426 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
4427 which have static meaning (i.e., static members, static
4428 member functions, enum declarations, etc).
4429
4430 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
4431 which can be seen locally to the class (as in 1), but
4432 know that we are doing this for declaration purposes
4433 (i.e. friend foo::bar (int)).
4434
4435 So that we may avoid calls to lookup_name, we cache the _TYPE
4436 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
4437
4438 For multiple inheritance, we perform a two-pass depth-first search
4439 of the type lattice. The first pass performs a pre-order search,
4440 marking types after the type has had its fields installed in
4441 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
4442 unmarks the marked types. If a field or member function name
4443 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
4444 that name becomes `error_mark_node'. */
4445
4446 void
4447 pushclass (type, modify)
4448 tree type;
4449 int modify;
4450 {
4451 type = TYPE_MAIN_VARIANT (type);
4452
4453 /* Make sure there is enough room for the new entry on the stack. */
4454 if (current_class_depth + 1 >= current_class_stack_size)
4455 {
4456 current_class_stack_size *= 2;
4457 current_class_stack
4458 = (class_stack_node_t) xrealloc (current_class_stack,
4459 current_class_stack_size
4460 * sizeof (struct class_stack_node));
4461 }
4462
4463 /* Insert a new entry on the class stack. */
4464 current_class_stack[current_class_depth].name = current_class_name;
4465 current_class_stack[current_class_depth].type = current_class_type;
4466 current_class_stack[current_class_depth].access = current_access_specifier;
4467 current_class_stack[current_class_depth].names_used = 0;
4468 current_class_depth++;
4469
4470 /* Now set up the new type. */
4471 current_class_name = TYPE_NAME (type);
4472 if (TREE_CODE (current_class_name) == TYPE_DECL)
4473 current_class_name = DECL_NAME (current_class_name);
4474 current_class_type = type;
4475
4476 /* By default, things in classes are private, while things in
4477 structures or unions are public. */
4478 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
4479 ? access_private_node
4480 : access_public_node);
4481
4482 if (previous_class_type != NULL_TREE
4483 && (type != previous_class_type
4484 || TYPE_SIZE (previous_class_type) == NULL_TREE)
4485 && current_class_depth == 1)
4486 {
4487 /* Forcibly remove any old class remnants. */
4488 invalidate_class_lookup_cache ();
4489 }
4490
4491 /* If we're about to enter a nested class, clear
4492 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
4493 if (modify && current_class_depth > 1)
4494 clear_identifier_class_values ();
4495
4496 pushlevel_class ();
4497
4498 #if 0
4499 if (CLASSTYPE_TEMPLATE_INFO (type))
4500 overload_template_name (type);
4501 #endif
4502
4503 if (modify)
4504 {
4505 if (type != previous_class_type || current_class_depth > 1)
4506 push_class_decls (type);
4507 else
4508 {
4509 tree item;
4510
4511 /* We are re-entering the same class we just left, so we
4512 don't have to search the whole inheritance matrix to find
4513 all the decls to bind again. Instead, we install the
4514 cached class_shadowed list, and walk through it binding
4515 names and setting up IDENTIFIER_TYPE_VALUEs. */
4516 set_class_shadows (previous_class_values);
4517 for (item = previous_class_values; item; item = TREE_CHAIN (item))
4518 {
4519 tree id = TREE_PURPOSE (item);
4520 tree decl = TREE_TYPE (item);
4521
4522 push_class_binding (id, decl);
4523 if (TREE_CODE (decl) == TYPE_DECL)
4524 set_identifier_type_value (id, TREE_TYPE (decl));
4525 }
4526 unuse_fields (type);
4527 }
4528
4529 storetags (CLASSTYPE_TAGS (type));
4530 }
4531 }
4532
4533 /* When we exit a toplevel class scope, we save the
4534 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
4535 reenter the class. Here, we've entered some other class, so we
4536 must invalidate our cache. */
4537
4538 void
4539 invalidate_class_lookup_cache ()
4540 {
4541 tree t;
4542
4543 /* This code can be seen as a cache miss. When we've cached a
4544 class' scope's bindings and we can't use them, we need to reset
4545 them. This is it! */
4546 for (t = previous_class_values; t; t = TREE_CHAIN (t))
4547 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
4548
4549 previous_class_type = NULL_TREE;
4550 }
4551
4552 /* Get out of the current class scope. If we were in a class scope
4553 previously, that is the one popped to. */
4554
4555 void
4556 popclass ()
4557 {
4558 poplevel_class ();
4559 /* Since poplevel_class does the popping of class decls nowadays,
4560 this really only frees the obstack used for these decls. */
4561 pop_class_decls ();
4562
4563 current_class_depth--;
4564 current_class_name = current_class_stack[current_class_depth].name;
4565 current_class_type = current_class_stack[current_class_depth].type;
4566 current_access_specifier = current_class_stack[current_class_depth].access;
4567 if (current_class_stack[current_class_depth].names_used)
4568 splay_tree_delete (current_class_stack[current_class_depth].names_used);
4569 }
4570
4571 /* Returns 1 if current_class_type is either T or a nested type of T. */
4572
4573 int
4574 currently_open_class (t)
4575 tree t;
4576 {
4577 int i;
4578 if (t == current_class_type)
4579 return 1;
4580 for (i = 0; i < current_class_depth; ++i)
4581 if (current_class_stack [i].type == t)
4582 return 1;
4583 return 0;
4584 }
4585
4586 /* When entering a class scope, all enclosing class scopes' names with
4587 static meaning (static variables, static functions, types and enumerators)
4588 have to be visible. This recursive function calls pushclass for all
4589 enclosing class contexts until global or a local scope is reached.
4590 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
4591 formal of the same name. */
4592
4593 void
4594 push_nested_class (type, modify)
4595 tree type;
4596 int modify;
4597 {
4598 tree context;
4599
4600 /* A namespace might be passed in error cases, like A::B:C. */
4601 if (type == NULL_TREE || type == error_mark_node || ! IS_AGGR_TYPE (type)
4602 || TREE_CODE (type) == NAMESPACE_DECL
4603 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
4604 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
4605 return;
4606
4607 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
4608
4609 if (context && CLASS_TYPE_P (context))
4610 push_nested_class (context, 2);
4611 pushclass (type, modify);
4612 }
4613
4614 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
4615
4616 void
4617 pop_nested_class ()
4618 {
4619 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
4620
4621 popclass ();
4622 if (context && CLASS_TYPE_P (context))
4623 pop_nested_class ();
4624 }
4625
4626 /* Set global variables CURRENT_LANG_NAME to appropriate value
4627 so that behavior of name-mangling machinery is correct. */
4628
4629 void
4630 push_lang_context (name)
4631 tree name;
4632 {
4633 *current_lang_stack++ = current_lang_name;
4634 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
4635 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
4636 {
4637 size_t old_size = VARRAY_SIZE (current_lang_base);
4638
4639 VARRAY_GROW (current_lang_base, old_size + 10);
4640 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
4641 }
4642
4643 if (name == lang_name_cplusplus)
4644 {
4645 strict_prototype = strict_prototypes_lang_cplusplus;
4646 current_lang_name = name;
4647 }
4648 else if (name == lang_name_java)
4649 {
4650 strict_prototype = strict_prototypes_lang_cplusplus;
4651 current_lang_name = name;
4652 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
4653 (See record_builtin_java_type in decl.c.) However, that causes
4654 incorrect debug entries if these types are actually used.
4655 So we re-enable debug output after extern "Java". */
4656 DECL_IGNORED_P (java_byte_type_node) = 0;
4657 DECL_IGNORED_P (java_short_type_node) = 0;
4658 DECL_IGNORED_P (java_int_type_node) = 0;
4659 DECL_IGNORED_P (java_long_type_node) = 0;
4660 DECL_IGNORED_P (java_float_type_node) = 0;
4661 DECL_IGNORED_P (java_double_type_node) = 0;
4662 DECL_IGNORED_P (java_char_type_node) = 0;
4663 DECL_IGNORED_P (java_boolean_type_node) = 0;
4664 }
4665 else if (name == lang_name_c)
4666 {
4667 strict_prototype = strict_prototypes_lang_c;
4668 current_lang_name = name;
4669 }
4670 else
4671 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
4672 }
4673
4674 /* Get out of the current language scope. */
4675
4676 void
4677 pop_lang_context ()
4678 {
4679 /* Clear the current entry so that garbage collector won't hold on
4680 to it. */
4681 *current_lang_stack = NULL_TREE;
4682 current_lang_name = *--current_lang_stack;
4683 if (current_lang_name == lang_name_cplusplus
4684 || current_lang_name == lang_name_java)
4685 strict_prototype = strict_prototypes_lang_cplusplus;
4686 else if (current_lang_name == lang_name_c)
4687 strict_prototype = strict_prototypes_lang_c;
4688 }
4689 \f
4690 /* Type instantiation routines. */
4691
4692 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
4693 matches the TARGET_TYPE. If there is no satisfactory match, return
4694 error_mark_node, and issue an error message if COMPLAIN is
4695 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
4696 was a template-id, and EXPLICIT_TARGS are the explicitly provided
4697 template arguments. */
4698
4699 static tree
4700 resolve_address_of_overloaded_function (target_type,
4701 overload,
4702 complain,
4703 template_only,
4704 explicit_targs)
4705 tree target_type;
4706 tree overload;
4707 int complain;
4708 int template_only;
4709 tree explicit_targs;
4710 {
4711 /* Here's what the standard says:
4712
4713 [over.over]
4714
4715 If the name is a function template, template argument deduction
4716 is done, and if the argument deduction succeeds, the deduced
4717 arguments are used to generate a single template function, which
4718 is added to the set of overloaded functions considered.
4719
4720 Non-member functions and static member functions match targets of
4721 type "pointer-to-function" or "reference-to-function." Nonstatic
4722 member functions match targets of type "pointer-to-member
4723 function;" the function type of the pointer to member is used to
4724 select the member function from the set of overloaded member
4725 functions. If a nonstatic member function is selected, the
4726 reference to the overloaded function name is required to have the
4727 form of a pointer to member as described in 5.3.1.
4728
4729 If more than one function is selected, any template functions in
4730 the set are eliminated if the set also contains a non-template
4731 function, and any given template function is eliminated if the
4732 set contains a second template function that is more specialized
4733 than the first according to the partial ordering rules 14.5.5.2.
4734 After such eliminations, if any, there shall remain exactly one
4735 selected function. */
4736
4737 int is_ptrmem = 0;
4738 int is_reference = 0;
4739 /* We store the matches in a TREE_LIST rooted here. The functions
4740 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
4741 interoperability with most_specialized_instantiation. */
4742 tree matches = NULL_TREE;
4743 tree fn;
4744
4745 /* By the time we get here, we should be seeing only real
4746 pointer-to-member types, not the internal POINTER_TYPE to
4747 METHOD_TYPE representation. */
4748 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
4749 && (TREE_CODE (TREE_TYPE (target_type))
4750 == METHOD_TYPE)), 0);
4751
4752 /* Check that the TARGET_TYPE is reasonable. */
4753 if (TYPE_PTRFN_P (target_type))
4754 /* This is OK. */
4755 ;
4756 else if (TYPE_PTRMEMFUNC_P (target_type))
4757 /* This is OK, too. */
4758 is_ptrmem = 1;
4759 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
4760 {
4761 /* This is OK, too. This comes from a conversion to reference
4762 type. */
4763 target_type = build_reference_type (target_type);
4764 is_reference = 1;
4765 }
4766 else
4767 {
4768 if (complain)
4769 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
4770 DECL_NAME (OVL_FUNCTION (overload)), target_type);
4771 return error_mark_node;
4772 }
4773
4774 /* If we can find a non-template function that matches, we can just
4775 use it. There's no point in generating template instantiations
4776 if we're just going to throw them out anyhow. But, of course, we
4777 can only do this when we don't *need* a template function. */
4778 if (!template_only)
4779 {
4780 tree fns;
4781
4782 for (fns = overload; fns; fns = OVL_CHAIN (fns))
4783 {
4784 tree fn = OVL_FUNCTION (fns);
4785 tree fntype;
4786
4787 if (TREE_CODE (fn) == TEMPLATE_DECL)
4788 /* We're not looking for templates just yet. */
4789 continue;
4790
4791 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4792 != is_ptrmem)
4793 /* We're looking for a non-static member, and this isn't
4794 one, or vice versa. */
4795 continue;
4796
4797 /* See if there's a match. */
4798 fntype = TREE_TYPE (fn);
4799 if (is_ptrmem)
4800 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
4801 else if (!is_reference)
4802 fntype = build_pointer_type (fntype);
4803
4804 if (can_convert_arg (target_type, fntype, fn))
4805 matches = tree_cons (fn, NULL_TREE, matches);
4806 }
4807 }
4808
4809 /* Now, if we've already got a match (or matches), there's no need
4810 to proceed to the template functions. But, if we don't have a
4811 match we need to look at them, too. */
4812 if (!matches)
4813 {
4814 tree target_fn_type;
4815 tree target_arg_types;
4816 tree fns;
4817
4818 if (is_ptrmem)
4819 target_fn_type
4820 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
4821 else
4822 target_fn_type = TREE_TYPE (target_type);
4823 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
4824
4825 for (fns = overload; fns; fns = OVL_CHAIN (fns))
4826 {
4827 tree fn = OVL_FUNCTION (fns);
4828 tree instantiation;
4829 tree instantiation_type;
4830 tree targs;
4831
4832 if (TREE_CODE (fn) != TEMPLATE_DECL)
4833 /* We're only looking for templates. */
4834 continue;
4835
4836 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4837 != is_ptrmem)
4838 /* We're not looking for a non-static member, and this is
4839 one, or vice versa. */
4840 continue;
4841
4842 /* Try to do argument deduction. */
4843 targs = make_scratch_vec (DECL_NTPARMS (fn));
4844 if (fn_type_unification (fn, explicit_targs, targs,
4845 target_arg_types, NULL_TREE,
4846 DEDUCE_EXACT) != 0)
4847 /* Argument deduction failed. */
4848 continue;
4849
4850 /* Instantiate the template. */
4851 instantiation = instantiate_template (fn, targs);
4852 if (instantiation == error_mark_node)
4853 /* Instantiation failed. */
4854 continue;
4855
4856 /* See if there's a match. */
4857 instantiation_type = TREE_TYPE (instantiation);
4858 if (is_ptrmem)
4859 instantiation_type =
4860 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
4861 else if (!is_reference)
4862 instantiation_type = build_pointer_type (instantiation_type);
4863 if (can_convert_arg (target_type, instantiation_type, instantiation))
4864 matches = tree_cons (instantiation, fn, matches);
4865 }
4866
4867 /* Now, remove all but the most specialized of the matches. */
4868 if (matches)
4869 {
4870 tree match = most_specialized_instantiation (matches,
4871 explicit_targs);
4872
4873 if (match != error_mark_node)
4874 matches = tree_cons (match, NULL_TREE, NULL_TREE);
4875 }
4876 }
4877
4878 /* Now we should have exactly one function in MATCHES. */
4879 if (matches == NULL_TREE)
4880 {
4881 /* There were *no* matches. */
4882 if (complain)
4883 {
4884 cp_error ("no matches converting function `%D' to type `%#T'",
4885 DECL_NAME (OVL_FUNCTION (overload)),
4886 target_type);
4887
4888 /* print_candidates expects a chain with the functions in
4889 TREE_VALUE slots, so we cons one up here (we're losing anyway,
4890 so why be clever?). */
4891 for (; overload; overload = OVL_NEXT (overload))
4892 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
4893 matches);
4894
4895 print_candidates (matches);
4896 }
4897 return error_mark_node;
4898 }
4899 else if (TREE_CHAIN (matches))
4900 {
4901 /* There were too many matches. */
4902
4903 if (complain)
4904 {
4905 tree match;
4906
4907 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
4908 DECL_NAME (OVL_FUNCTION (overload)),
4909 target_type);
4910
4911 /* Since print_candidates expects the functions in the
4912 TREE_VALUE slot, we flip them here. */
4913 for (match = matches; match; match = TREE_CHAIN (match))
4914 TREE_VALUE (match) = TREE_PURPOSE (match);
4915
4916 print_candidates (matches);
4917 }
4918
4919 return error_mark_node;
4920 }
4921
4922 /* Good, exactly one match. Now, convert it to the correct type. */
4923 fn = TREE_PURPOSE (matches);
4924
4925 mark_used (fn);
4926
4927 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
4928 return build_unary_op (ADDR_EXPR, fn, 0);
4929 else
4930 {
4931 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
4932 will mark the function as addressed, but here we must do it
4933 explicitly. */
4934 mark_addressable (fn);
4935
4936 return fn;
4937 }
4938 }
4939
4940 /* This function will instantiate the type of the expression given in
4941 RHS to match the type of LHSTYPE. If errors exist, then return
4942 error_mark_node. We only complain is COMPLAIN is set. If we are
4943 not complaining, never modify rhs, as overload resolution wants to
4944 try many possible instantiations, in hopes that at least one will
4945 work.
4946
4947 FLAGS is a bitmask, as we see at the top of the function.
4948
4949 For non-recursive calls, LHSTYPE should be a function, pointer to
4950 function, or a pointer to member function. */
4951
4952 tree
4953 instantiate_type (lhstype, rhs, flags)
4954 tree lhstype, rhs;
4955 int flags;
4956 {
4957 int complain = (flags & 1);
4958 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
4959
4960 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
4961 {
4962 if (complain)
4963 error ("not enough type information");
4964 return error_mark_node;
4965 }
4966
4967 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
4968 {
4969 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
4970 return rhs;
4971 if (complain)
4972 cp_error ("argument of type `%T' does not match `%T'",
4973 TREE_TYPE (rhs), lhstype);
4974 return error_mark_node;
4975 }
4976
4977 /* We don't overwrite rhs if it is an overloaded function.
4978 Copying it would destroy the tree link. */
4979 if (TREE_CODE (rhs) != OVERLOAD)
4980 rhs = copy_node (rhs);
4981
4982 /* This should really only be used when attempting to distinguish
4983 what sort of a pointer to function we have. For now, any
4984 arithmetic operation which is not supported on pointers
4985 is rejected as an error. */
4986
4987 switch (TREE_CODE (rhs))
4988 {
4989 case TYPE_EXPR:
4990 case CONVERT_EXPR:
4991 case SAVE_EXPR:
4992 case CONSTRUCTOR:
4993 case BUFFER_REF:
4994 my_friendly_abort (177);
4995 return error_mark_node;
4996
4997 case INDIRECT_REF:
4998 case ARRAY_REF:
4999 {
5000 tree new_rhs;
5001
5002 new_rhs = instantiate_type (build_pointer_type (lhstype),
5003 TREE_OPERAND (rhs, 0), flags);
5004 if (new_rhs == error_mark_node)
5005 return error_mark_node;
5006
5007 TREE_TYPE (rhs) = lhstype;
5008 TREE_OPERAND (rhs, 0) = new_rhs;
5009 return rhs;
5010 }
5011
5012 case NOP_EXPR:
5013 rhs = copy_node (TREE_OPERAND (rhs, 0));
5014 TREE_TYPE (rhs) = unknown_type_node;
5015 return instantiate_type (lhstype, rhs, flags);
5016
5017 case COMPONENT_REF:
5018 {
5019 tree r = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5020
5021 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype)
5022 && complain && !flag_ms_extensions)
5023 {
5024 /* Note: we check this after the recursive call to avoid
5025 complaining about cases where overload resolution fails. */
5026
5027 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
5028 tree fn = PTRMEM_CST_MEMBER (r);
5029
5030 my_friendly_assert (TREE_CODE (r) == PTRMEM_CST, 990811);
5031
5032 cp_pedwarn
5033 ("object-dependent reference to `%E' can only be used in a call",
5034 DECL_NAME (fn));
5035 cp_pedwarn
5036 (" to form a pointer to member function, say `&%T::%E'",
5037 t, DECL_NAME (fn));
5038 }
5039
5040 return r;
5041 }
5042
5043 case OFFSET_REF:
5044 rhs = TREE_OPERAND (rhs, 1);
5045 if (BASELINK_P (rhs))
5046 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5047
5048 /* This can happen if we are forming a pointer-to-member for a
5049 member template. */
5050 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
5051
5052 /* Fall through. */
5053
5054 case TEMPLATE_ID_EXPR:
5055 return
5056 resolve_address_of_overloaded_function (lhstype,
5057 TREE_OPERAND (rhs, 0),
5058 complain,
5059 /*template_only=*/1,
5060 TREE_OPERAND (rhs, 1));
5061
5062 case OVERLOAD:
5063 return
5064 resolve_address_of_overloaded_function (lhstype,
5065 rhs,
5066 complain,
5067 /*template_only=*/0,
5068 /*explicit_targs=*/NULL_TREE);
5069
5070 case TREE_LIST:
5071 /* Now we should have a baselink. */
5072 my_friendly_assert (BASELINK_P (rhs), 990412);
5073
5074 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5075
5076 case CALL_EXPR:
5077 /* This is too hard for now. */
5078 my_friendly_abort (183);
5079 return error_mark_node;
5080
5081 case PLUS_EXPR:
5082 case MINUS_EXPR:
5083 case COMPOUND_EXPR:
5084 TREE_OPERAND (rhs, 0)
5085 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5086 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5087 return error_mark_node;
5088 TREE_OPERAND (rhs, 1)
5089 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5090 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5091 return error_mark_node;
5092
5093 TREE_TYPE (rhs) = lhstype;
5094 return rhs;
5095
5096 case MULT_EXPR:
5097 case TRUNC_DIV_EXPR:
5098 case FLOOR_DIV_EXPR:
5099 case CEIL_DIV_EXPR:
5100 case ROUND_DIV_EXPR:
5101 case RDIV_EXPR:
5102 case TRUNC_MOD_EXPR:
5103 case FLOOR_MOD_EXPR:
5104 case CEIL_MOD_EXPR:
5105 case ROUND_MOD_EXPR:
5106 case FIX_ROUND_EXPR:
5107 case FIX_FLOOR_EXPR:
5108 case FIX_CEIL_EXPR:
5109 case FIX_TRUNC_EXPR:
5110 case FLOAT_EXPR:
5111 case NEGATE_EXPR:
5112 case ABS_EXPR:
5113 case MAX_EXPR:
5114 case MIN_EXPR:
5115 case FFS_EXPR:
5116
5117 case BIT_AND_EXPR:
5118 case BIT_IOR_EXPR:
5119 case BIT_XOR_EXPR:
5120 case LSHIFT_EXPR:
5121 case RSHIFT_EXPR:
5122 case LROTATE_EXPR:
5123 case RROTATE_EXPR:
5124
5125 case PREINCREMENT_EXPR:
5126 case PREDECREMENT_EXPR:
5127 case POSTINCREMENT_EXPR:
5128 case POSTDECREMENT_EXPR:
5129 if (complain)
5130 error ("invalid operation on uninstantiated type");
5131 return error_mark_node;
5132
5133 case TRUTH_AND_EXPR:
5134 case TRUTH_OR_EXPR:
5135 case TRUTH_XOR_EXPR:
5136 case LT_EXPR:
5137 case LE_EXPR:
5138 case GT_EXPR:
5139 case GE_EXPR:
5140 case EQ_EXPR:
5141 case NE_EXPR:
5142 case TRUTH_ANDIF_EXPR:
5143 case TRUTH_ORIF_EXPR:
5144 case TRUTH_NOT_EXPR:
5145 if (complain)
5146 error ("not enough type information");
5147 return error_mark_node;
5148
5149 case COND_EXPR:
5150 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
5151 {
5152 if (complain)
5153 error ("not enough type information");
5154 return error_mark_node;
5155 }
5156 TREE_OPERAND (rhs, 1)
5157 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5158 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5159 return error_mark_node;
5160 TREE_OPERAND (rhs, 2)
5161 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
5162 if (TREE_OPERAND (rhs, 2) == error_mark_node)
5163 return error_mark_node;
5164
5165 TREE_TYPE (rhs) = lhstype;
5166 return rhs;
5167
5168 case MODIFY_EXPR:
5169 TREE_OPERAND (rhs, 1)
5170 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5171 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5172 return error_mark_node;
5173
5174 TREE_TYPE (rhs) = lhstype;
5175 return rhs;
5176
5177 case ADDR_EXPR:
5178 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5179
5180 case ENTRY_VALUE_EXPR:
5181 my_friendly_abort (184);
5182 return error_mark_node;
5183
5184 case ERROR_MARK:
5185 return error_mark_node;
5186
5187 default:
5188 my_friendly_abort (185);
5189 return error_mark_node;
5190 }
5191 }
5192 \f
5193 /* Return the name of the virtual function pointer field
5194 (as an IDENTIFIER_NODE) for the given TYPE. Note that
5195 this may have to look back through base types to find the
5196 ultimate field name. (For single inheritance, these could
5197 all be the same name. Who knows for multiple inheritance). */
5198
5199 static tree
5200 get_vfield_name (type)
5201 tree type;
5202 {
5203 tree binfo = TYPE_BINFO (type);
5204 char *buf;
5205
5206 while (BINFO_BASETYPES (binfo)
5207 && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
5208 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
5209 binfo = BINFO_BASETYPE (binfo, 0);
5210
5211 type = BINFO_TYPE (binfo);
5212 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
5213 + TYPE_NAME_LENGTH (type) + 2);
5214 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
5215 return get_identifier (buf);
5216 }
5217
5218 void
5219 print_class_statistics ()
5220 {
5221 #ifdef GATHER_STATISTICS
5222 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
5223 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
5224 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
5225 n_build_method_call, n_inner_fields_searched);
5226 if (n_vtables)
5227 {
5228 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
5229 n_vtables, n_vtable_searches);
5230 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
5231 n_vtable_entries, n_vtable_elems);
5232 }
5233 #endif
5234 }
5235
5236 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
5237 according to [class]:
5238 The class-name is also inserted
5239 into the scope of the class itself. For purposes of access checking,
5240 the inserted class name is treated as if it were a public member name. */
5241
5242 void
5243 build_self_reference ()
5244 {
5245 tree name = constructor_name (current_class_type);
5246 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
5247 tree saved_cas;
5248
5249 DECL_NONLOCAL (value) = 1;
5250 DECL_CONTEXT (value) = current_class_type;
5251 DECL_CLASS_CONTEXT (value) = current_class_type;
5252 DECL_ARTIFICIAL (value) = 1;
5253
5254 if (processing_template_decl)
5255 value = push_template_decl (value);
5256
5257 saved_cas = current_access_specifier;
5258 current_access_specifier = access_public_node;
5259 finish_member_declaration (value);
5260 current_access_specifier = saved_cas;
5261 }
5262
5263 /* Returns 1 if TYPE contains only padding bytes. */
5264
5265 int
5266 is_empty_class (type)
5267 tree type;
5268 {
5269 tree t;
5270
5271 if (type == error_mark_node)
5272 return 0;
5273
5274 if (! IS_AGGR_TYPE (type))
5275 return 0;
5276
5277 if (flag_new_abi)
5278 return CLASSTYPE_SIZE (type) == integer_zero_node;
5279
5280 if (TYPE_BINFO_BASETYPES (type))
5281 return 0;
5282 t = TYPE_FIELDS (type);
5283 while (t && TREE_CODE (t) != FIELD_DECL)
5284 t = TREE_CHAIN (t);
5285 return (t == NULL_TREE);
5286 }
5287
5288 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
5289 a *_TYPE node. NODE can also be a local class. */
5290
5291 tree
5292 get_enclosing_class (type)
5293 tree type;
5294 {
5295 tree node = type;
5296
5297 while (node && TREE_CODE (node) != NAMESPACE_DECL)
5298 {
5299 switch (TREE_CODE_CLASS (TREE_CODE (node)))
5300 {
5301 case 'd':
5302 node = DECL_CONTEXT (node);
5303 break;
5304
5305 case 't':
5306 if (node != type)
5307 return node;
5308 node = TYPE_CONTEXT (node);
5309 break;
5310
5311 default:
5312 my_friendly_abort (0);
5313 }
5314 }
5315 return NULL_TREE;
5316 }
5317
5318 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
5319
5320 int
5321 is_base_of_enclosing_class (base, type)
5322 tree base, type;
5323 {
5324 while (type)
5325 {
5326 if (get_binfo (base, type, 0))
5327 return 1;
5328
5329 type = get_enclosing_class (type);
5330 }
5331 return 0;
5332 }
5333
5334 /* Note that NAME was looked up while the current class was being
5335 defined and that the result of that lookup was DECL. */
5336
5337 void
5338 maybe_note_name_used_in_class (name, decl)
5339 tree name;
5340 tree decl;
5341 {
5342 splay_tree names_used;
5343
5344 /* If we're not defining a class, there's nothing to do. */
5345 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
5346 return;
5347
5348 /* If there's already a binding for this NAME, then we don't have
5349 anything to worry about. */
5350 if (IDENTIFIER_CLASS_VALUE (name))
5351 return;
5352
5353 if (!current_class_stack[current_class_depth - 1].names_used)
5354 current_class_stack[current_class_depth - 1].names_used
5355 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
5356 names_used = current_class_stack[current_class_depth - 1].names_used;
5357
5358 splay_tree_insert (names_used,
5359 (splay_tree_key) name,
5360 (splay_tree_value) decl);
5361 }
5362
5363 /* Note that NAME was declared (as DECL) in the current class. Check
5364 to see that the declaration is legal. */
5365
5366 void
5367 note_name_declared_in_class (name, decl)
5368 tree name;
5369 tree decl;
5370 {
5371 splay_tree names_used;
5372 splay_tree_node n;
5373
5374 /* Look to see if we ever used this name. */
5375 names_used
5376 = current_class_stack[current_class_depth - 1].names_used;
5377 if (!names_used)
5378 return;
5379
5380 n = splay_tree_lookup (names_used, (splay_tree_key) name);
5381 if (n)
5382 {
5383 /* [basic.scope.class]
5384
5385 A name N used in a class S shall refer to the same declaration
5386 in its context and when re-evaluated in the completed scope of
5387 S. */
5388 cp_error ("declaration of `%#D'", decl);
5389 cp_error_at ("changes meaning of `%s' from `%+#D'",
5390 IDENTIFIER_POINTER (DECL_NAME (decl)),
5391 (tree) n->value);
5392 }
5393 }