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