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