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