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