OSDN Git Service

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