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