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