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