OSDN Git Service

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