OSDN Git Service

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