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