xref: /386bsd/usr/src/usr.bin/g++/cc1plus/cp-class.c (revision a2142627)
1 /* Functions related to building classes and their related objects.
2    Copyright (C) 1987, 1992, 1993 Free Software Foundation, Inc.
3    Contributed by Michael Tiemann (tiemann@cygnus.com)
4 
5 This file is part of GNU CC.
6 
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11 
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
20 
21 
22 /* High-level class interface. */
23 
24 #include "config.h"
25 #include "tree.h"
26 #include <stdio.h>
27 #include "cp-tree.h"
28 #include "flags.h"
29 
30 #ifdef DEBUG_CP_BINDING_LEVELS
31 #include "cp-decl.h"
32 #endif
33 
34 #include "obstack.h"
35 #define obstack_chunk_alloc xmalloc
36 #define obstack_chunk_free free
37 
38 extern struct obstack permanent_obstack;
39 
40 /* Way of stacking class types.  */
41 static tree *current_class_base, *current_class_stack;
42 static int current_class_stacksize;
43 int current_class_depth;
44 
45 struct class_level
46 {
47   /* The previous class level.  */
48   struct class_level *level_chain;
49 
50   /* The class instance variable, as a PARM_DECL.  */
51   tree decl;
52   /* The class instance variable, as an object.  */
53   tree object;
54   /* The virtual function table pointer
55      for the class instance variable.  */
56   tree vtable_decl;
57 
58   /* Name of the current class.  */
59   tree name;
60   /* Type of the current class.  */
61   tree type;
62 
63   /* Flags for this class level.  */
64   int this_is_variable;
65   int memoized_lookups;
66   int save_memoized;
67   int unused;
68 };
69 
70 tree current_class_decl, C_C_D;	/* PARM_DECL: the class instance variable */
71 tree current_vtable_decl;
72 
73 /* The following two can be derived from the previous one */
74 tree current_class_name;	/* IDENTIFIER_NODE: name of current class */
75 tree current_class_type;	/* _TYPE: the type of the current class */
76 static tree prev_class_type;	/* _TYPE: the previous type that was a class */
77 
78 static tree get_vfield_name PROTO((tree));
79 tree the_null_vtable_entry;
80 
81 /* Way of stacking language names.  */
82 tree *current_lang_base, *current_lang_stack;
83 static int current_lang_stacksize;
84 
85 /* Names of languages we recognize.  */
86 tree lang_name_c, lang_name_cplusplus;
87 tree current_lang_name;
88 
89 /* When layout out an aggregate type, the size of the
90    basetypes (virtual and non-virtual) is passed to layout_record
91    via this node.  */
92 static tree base_layout_decl;
93 
94 /* Variables shared between cp-class.c and cp-call.c.  */
95 
96 int n_vtables = 0;
97 int n_vtable_entries = 0;
98 int n_vtable_searches = 0;
99 int n_vtable_elems = 0;
100 int n_convert_harshness = 0;
101 int n_compute_conversion_costs = 0;
102 int n_build_method_call = 0;
103 int n_inner_fields_searched = 0;
104 
105 /* Virtual baseclass things.  */
106 tree
build_vbase_pointer(exp,type)107 build_vbase_pointer (exp, type)
108      tree exp, type;
109 {
110   char *name;
111 
112   name = (char *) alloca (TYPE_NAME_LENGTH (type) + sizeof (VBASE_NAME) + 1);
113   sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (type));
114   return build_component_ref (exp, get_identifier (name), 0, 0);
115 }
116 
117 /* Build multi-level access to EXPR using hierarchy path PATH.
118    CODE is PLUS_EXPR if we are going with the grain,
119    and MINUS_EXPR if we are not (in which case, we cannot traverse
120    virtual baseclass links).
121 
122    TYPE is the type we want this path to have on exit.
123 
124    ALIAS_THIS is non-zero if EXPR in an expression involving `this'.  */
125 tree
build_vbase_path(code,type,expr,path,alias_this)126 build_vbase_path (code, type, expr, path, alias_this)
127      enum tree_code code;
128      tree type, expr, path;
129      int alias_this;
130 {
131   register int changed = 0;
132   tree last = NULL_TREE, last_virtual = NULL_TREE;
133   int nonnull = 0;
134   int fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
135   tree null_expr = 0, nonnull_expr;
136   tree basetype;
137   tree offset = integer_zero_node;
138 
139   if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
140     expr = save_expr (expr);
141   nonnull_expr = expr;
142 
143   if (BINFO_INHERITANCE_CHAIN (path))
144     {
145       tree reverse_path = NULL_TREE;
146 
147       while (path)
148 	{
149 	  tree r = copy_node (path);
150 	  BINFO_INHERITANCE_CHAIN (r) = reverse_path;
151 	  reverse_path = r;
152 	  path = BINFO_INHERITANCE_CHAIN (path);
153 	}
154       path = reverse_path;
155     }
156 
157   basetype = BINFO_TYPE (path);
158 
159   while (path)
160     {
161       if (TREE_VIA_VIRTUAL (path))
162 	{
163 	  last_virtual = BINFO_TYPE (path);
164 	  if (code == PLUS_EXPR)
165 	    {
166 	      changed = ! fixed_type_p;
167 
168 	      if (changed)
169 		{
170 		  extern int flag_assume_nonnull_objects;
171 		  tree ind;
172 
173 		  if (last)
174 		    nonnull_expr = convert_pointer_to (last, nonnull_expr);
175 		  ind = build_indirect_ref (nonnull_expr, NULL);
176 		  nonnull_expr = build_vbase_pointer (ind, last_virtual);
177 		  if (nonnull == 0 && !flag_assume_nonnull_objects
178 		      && null_expr == NULL_TREE)
179 		    {
180 		      null_expr = build1 (NOP_EXPR, TYPE_POINTER_TO (last_virtual), integer_zero_node);
181 		      expr = build (COND_EXPR, TYPE_POINTER_TO (last_virtual),
182 				    build (EQ_EXPR, integer_type_node, expr,
183 					   integer_zero_node),
184 				    null_expr, nonnull_expr);
185 		    }
186 		}
187 	      /* else we'll figure out the offset below.  */
188 
189 	      /* Happens in the case of parse errors.  */
190 	      if (nonnull_expr == error_mark_node)
191 		return error_mark_node;
192 	    }
193 	  else
194 	    {
195 	      error_with_aggr_type (last_virtual, "cannot cast up from virtual baseclass `%s'");
196 	      return error_mark_node;
197 	    }
198 	}
199       last = path;
200       path = BINFO_INHERITANCE_CHAIN (path);
201     }
202   /* LAST is now the last basetype assoc on the path.  */
203 
204   /* A pointer to a virtual base member of a non-null object
205      is non-null.  Therefore, we only need to test for zeroness once.
206      Make EXPR the canonical expression to deal with here.  */
207   if (null_expr)
208     {
209       TREE_OPERAND (expr, 2) = nonnull_expr;
210       TREE_TYPE (TREE_OPERAND (expr, 1)) = TREE_TYPE (nonnull_expr);
211     }
212   else
213     expr = nonnull_expr;
214 
215   /* If we go through any virtual base pointers, make sure that
216      casts to BASETYPE from the last virtual base class use
217      the right value for BASETYPE.  */
218   if (changed)
219     {
220       tree intype = TREE_TYPE (TREE_TYPE (expr));
221       if (TYPE_MAIN_VARIANT (intype) == BINFO_TYPE (last))
222 	basetype = intype;
223       else
224 	{
225 	  tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
226 	  basetype = last;
227 	  offset = BINFO_OFFSET (binfo);
228 	}
229     }
230   else
231     {
232       if (last_virtual)
233 	{
234 	  offset = BINFO_OFFSET (binfo_member (last_virtual,
235 					       CLASSTYPE_VBASECLASSES (basetype)));
236 	  offset = size_binop (PLUS_EXPR, offset, BINFO_OFFSET (last));
237 	}
238       else
239 	offset = BINFO_OFFSET (last);
240 
241 #if 0
242       /* why unconditionally set this? (mrs) see deja-gnu/g++.mike/net15.C
243 	 for a test case. */
244       code = PLUS_EXPR;
245 #endif
246     }
247 
248   if (TREE_INT_CST_LOW (offset))
249     {
250       /* For multiple inheritance: if `this' can be set by any
251 	 function, then it could be 0 on entry to any function.
252 	 Preserve such zeroness here.  Otherwise, only in the
253 	 case of constructors need we worry, and in those cases,
254 	 it will be zero, or initialized to some legal value to
255 	 which we may add.  */
256       if (nonnull == 0 && (alias_this == 0 || flag_this_is_variable > 0))
257 	{
258 	  if (null_expr)
259 	    TREE_TYPE (null_expr) = type;
260 	  else
261 	    null_expr = build1 (NOP_EXPR, type, integer_zero_node);
262 	  if (TREE_SIDE_EFFECTS (expr))
263 	    expr = save_expr (expr);
264 
265 	  return build (COND_EXPR, type,
266 			build (EQ_EXPR, integer_type_node, expr, integer_zero_node),
267 			null_expr,
268 			build (code, type, expr, offset));
269 	}
270       else return build (code, type, expr, offset);
271     }
272 
273   /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
274      be used multiple times in initialization of multiple inheritance.  */
275   if (null_expr)
276     {
277       TREE_TYPE (expr) = type;
278       return expr;
279     }
280   else
281     return build1 (NOP_EXPR, type, expr);
282 }
283 
284 /* Virtual function things.  */
285 
286 /* Virtual functions to be dealt with after laying out our
287    base classes.  Usually this is used only when classes have virtual
288    baseclasses, but it can happen also when classes have non-virtual
289    baseclasses if the derived class overrides baseclass functions
290    at different offsets.  */
291 static tree pending_hard_virtuals;
292 static int doing_hard_virtuals;
293 
294 /* The names of the entries in the virtual table structure.  */
295 static tree delta_name, pfn_name;
296 
297 /* XXX This is set but never used.  (bpk) */
298 #if 0
299 /* Temporary binfo list to memoize lookups of the left-most non-virtual
300    baseclass B in a lattice topped by T.  B can appear multiple times
301    in the lattice.
302    TREE_PURPOSE is B's TYPE_MAIN_VARIANT.
303    TREE_VALUE is the path by which B is reached from T.
304    TREE_TYPE is B's real type.
305 
306    If TREE_TYPE is NULL_TREE, it means that B was reached via
307    a virtual baseclass.
308    N.B.: This list consists of nodes on the temporary obstack.  */
309 static tree leftmost_baseclasses;
310 #endif
311 
312 /* Build an entry in the virtual function table.
313    DELTA is the offset for the `this' pointer.
314    PFN is an ADDR_EXPR containing a pointer to the virtual function.
315    Note that the index (DELTA2) in the virtual function table
316    is always 0.  */
317 tree
build_vtable_entry(delta,pfn)318 build_vtable_entry (delta, pfn)
319      tree delta, pfn;
320 {
321   tree elems = tree_cons (NULL_TREE, delta,
322 			  tree_cons (NULL_TREE, integer_zero_node,
323 				     build_tree_list (NULL_TREE, pfn)));
324   tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
325 
326   /* DELTA is constructed by `size_int', which means it may be an
327      unsigned quantity on some platforms.  Therefore, we cannot use
328      `int_fits_type_p', because when DELTA is really negative,
329      `force_fit_type' will make it look like a very large number.  */
330 
331   if ((TREE_INT_CST_LOW (TYPE_MAX_VALUE (short_integer_type_node))
332        < TREE_INT_CST_LOW (delta))
333       || (TREE_INT_CST_LOW (delta)
334 	  < TREE_INT_CST_LOW (TYPE_MIN_VALUE (short_integer_type_node))))
335     sorry ("object size exceeds built-in limit for virtual function table implementation");
336 
337   TREE_CONSTANT (entry) = 1;
338   TREE_STATIC (entry) = 1;
339   TREE_READONLY (entry) = 1;
340 
341 #ifdef GATHER_STATISTICS
342   n_vtable_entries += 1;
343 #endif
344 
345   return entry;
346 }
347 
348 /* Given an object INSTANCE, return an expression which yields
349    the virtual function corresponding to INDEX.  There are many special
350    cases for INSTANCE which we take care of here, mainly to avoid
351    creating extra tree nodes when we don't have to.  */
352 tree
build_vfn_ref(ptr_to_instptr,instance,index)353 build_vfn_ref (ptr_to_instptr, instance, index)
354      tree *ptr_to_instptr, instance;
355      tree index;
356 {
357   extern int building_cleanup;
358   tree vtbl, aref;
359   tree basetype = TREE_TYPE (instance);
360 
361   if (TREE_CODE (basetype) == REFERENCE_TYPE)
362     basetype = TREE_TYPE (basetype);
363 
364   if (instance == C_C_D)
365     {
366       if (current_vtable_decl == NULL_TREE
367 	  || current_vtable_decl == error_mark_node
368 	  || !UNIQUELY_DERIVED_FROM_P (DECL_FCONTEXT (CLASSTYPE_VFIELD (current_class_type)), basetype))
369 	vtbl = build_indirect_ref (build_vfield_ref (instance, basetype), NULL);
370       else
371 	vtbl = current_vtable_decl;
372     }
373   else
374     {
375       if (optimize)
376 	{
377 	  /* Try to figure out what a reference refers to, and
378 	     access its virtual function table directly.  */
379 	  tree ref = NULL_TREE;
380 
381 	  if (TREE_CODE (instance) == INDIRECT_REF
382 	      && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
383 	    ref = TREE_OPERAND (instance, 0);
384 	  else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
385 	    ref = instance;
386 
387 	  if (ref && TREE_CODE (ref) == VAR_DECL
388 	      && DECL_INITIAL (ref))
389 	    {
390 	      tree init = DECL_INITIAL (ref);
391 
392 	      while (TREE_CODE (init) == NOP_EXPR
393 		     || TREE_CODE (init) == NON_LVALUE_EXPR)
394 		init = TREE_OPERAND (init, 0);
395 	      if (TREE_CODE (init) == ADDR_EXPR)
396 		{
397 		  init = TREE_OPERAND (init, 0);
398 		  if (IS_AGGR_TYPE (TREE_TYPE (init))
399 		      && (TREE_CODE (init) == PARM_DECL
400 			  || TREE_CODE (init) == VAR_DECL))
401 		    instance = init;
402 		}
403 	    }
404 	}
405 
406       if (IS_AGGR_TYPE (TREE_TYPE (instance))
407 	  && (TREE_CODE (instance) == RESULT_DECL
408 	      || TREE_CODE (instance) == PARM_DECL
409 	      || TREE_CODE (instance) == VAR_DECL))
410 	vtbl = TYPE_BINFO_VTABLE (basetype);
411       else
412 	vtbl = build_indirect_ref (build_vfield_ref (instance, basetype),
413 				   NULL);
414     }
415   assemble_external (vtbl);
416   aref = build_array_ref (vtbl, index);
417   if (!building_cleanup && TREE_CODE (aref) == INDIRECT_REF)
418     TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
419 
420   *ptr_to_instptr = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
421 			   *ptr_to_instptr,
422 			   convert (integer_type_node, build_component_ref (aref, delta_name, 0, 0)));
423   return build_component_ref (aref, pfn_name, 0, 0);
424 }
425 
426 /* Set TREE_PUBLIC and/or TREE_EXTERN on the vtable DECL,
427    based on TYPE and other static flags.
428 
429    Note that anything public is tagged TREE_PUBLIC, whether
430    it's public in this file or in another one.  */
431 
432 static void
import_export_vtable(decl,type)433 import_export_vtable (decl, type)
434   tree decl, type;
435 {
436   if (write_virtuals >= 2)
437     {
438       if (CLASSTYPE_INTERFACE_UNKNOWN (type) == 0)
439 	{
440 	  TREE_PUBLIC (decl) = 1;
441 	  DECL_EXTERNAL (decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (type);
442 	}
443     }
444   else if (write_virtuals != 0)
445     {
446       TREE_PUBLIC (decl) = 1;
447       if (write_virtuals < 0)
448 	DECL_EXTERNAL (decl) = 1;
449     }
450 }
451 
452 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
453    for the given TYPE.  */
454 static tree
get_vtable_name(type)455 get_vtable_name (type)
456      tree type;
457 {
458   tree type_id = build_typename_overload (type);
459   char *buf = (char *)alloca (sizeof (VTABLE_NAME_FORMAT)
460 			      + IDENTIFIER_LENGTH (type_id) + 2);
461   char *ptr = IDENTIFIER_POINTER (type_id);
462   int i;
463   for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
464   while (ptr[i] >= '0' && ptr[i] <= '9')
465     i += 1;
466   sprintf (buf, VTABLE_NAME_FORMAT, ptr+i);
467   return get_identifier (buf);
468 }
469 
470 /* Build a virtual function for type TYPE.
471    If BINFO is non-NULL, build the vtable starting with the initial
472    approximation that it is the same as the one which is the head of
473    the association list.  */
474 static tree
build_vtable(binfo,type)475 build_vtable (binfo, type)
476      tree binfo, type;
477 {
478   tree name = get_vtable_name (type);
479   tree virtuals, decl;
480 
481   if (binfo)
482     {
483       virtuals = copy_list (BINFO_VIRTUALS (binfo));
484       decl = build_decl (VAR_DECL, name, TREE_TYPE (BINFO_VTABLE (binfo)));
485     }
486   else
487     {
488       virtuals = NULL_TREE;
489       decl = build_decl (VAR_DECL, name, void_type_node);
490     }
491 
492 #ifdef GATHER_STATISTICS
493   n_vtables += 1;
494   n_vtable_elems += list_length (virtuals);
495 #endif
496 
497   /* Set TREE_PUBLIC and TREE_EXTERN as appropriate.  */
498   import_export_vtable (decl, type);
499 
500   IDENTIFIER_GLOBAL_VALUE (name) = decl = pushdecl_top_level (decl);
501   /* Initialize the association list for this type, based
502      on our first approximation.  */
503   TYPE_BINFO_VTABLE (type) = decl;
504   TYPE_BINFO_VIRTUALS (type) = virtuals;
505 
506   TREE_STATIC (decl) = 1;
507   DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
508 			   DECL_ALIGN (decl));
509 
510   if (binfo && write_virtuals >= 0)
511     DECL_VIRTUAL_P (decl) = 1;
512 #if 0
513   /* Remember which class this vtable is really for.  */
514   if (binfo)
515     DECL_VPARENT (decl) = BINFO_TYPE (binfo);
516   else
517     DECL_VPARENT (decl) = type;
518 #endif
519   DECL_CONTEXT (decl) = type;
520 
521   binfo = TYPE_BINFO (type);
522   SET_BINFO_VTABLE_PATH_MARKED (binfo);
523   SET_BINFO_NEW_VTABLE_MARKED (binfo);
524   return decl;
525 }
526 
527 /* Give TYPE a new virtual function table which is initialized
528    with a skeleton-copy of its original initialization.  The only
529    entry that changes is the `delta' entry, so we can really
530    share a lot of structure.
531 
532    FOR_TYPE is the derived type which caused this table to
533    be needed.
534 
535    BINFO is the type association which provided TYPE for FOR_TYPE.
536 
537    The way we update BASE_BINFO's vtable information is just to change the
538    association information in FOR_TYPE's association list.  */
539 static void
prepare_fresh_vtable(binfo,base_binfo,for_type)540 prepare_fresh_vtable (binfo, base_binfo, for_type)
541      tree binfo, base_binfo, for_type;
542 {
543   tree basetype = BINFO_TYPE (binfo);
544   tree orig_decl = BINFO_VTABLE (binfo);
545   tree name = build_type_pathname (VTABLE_NAME_FORMAT, basetype, for_type);
546   tree new_decl = build_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
547   tree path;
548   int result;
549 
550   /* Remember which class this vtable is really for.  */
551 #if 0
552   DECL_VPARENT (new_decl) = BINFO_TYPE (base_binfo);
553 #endif
554   DECL_CONTEXT (new_decl) = for_type;
555 
556   TREE_STATIC (new_decl) = 1;
557   BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
558   DECL_VIRTUAL_P (new_decl) = 1;
559   DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
560 
561   /* Make fresh virtual list, so we can smash it later.  */
562   BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
563   /* Install the value for `headof' if that's what we're doing.  */
564   if (flag_dossier)
565     TREE_VALUE (TREE_CHAIN (BINFO_VIRTUALS (binfo)))
566       = build_vtable_entry (size_binop (MINUS_EXPR, integer_zero_node, BINFO_OFFSET (binfo)),
567 			    FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (TREE_CHAIN (BINFO_VIRTUALS (binfo)))));
568 
569 #ifdef GATHER_STATISTICS
570   n_vtables += 1;
571   n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
572 #endif
573 
574   /* Set TREE_PUBLIC and TREE_EXTERN as appropriate.  */
575   import_export_vtable (new_decl, for_type);
576 
577   if (TREE_VIA_VIRTUAL (binfo))
578     my_friendly_assert (binfo == binfo_member (BINFO_TYPE (binfo),
579 				   CLASSTYPE_VBASECLASSES (current_class_type)),
580 			170);
581   SET_BINFO_NEW_VTABLE_MARKED (binfo);
582   SET_BINFO_VTABLE_PATH_MARKED (binfo);
583 
584   /* Mark all types between FOR_TYPE and TYPE as having been
585      touched, so that if we change virtual function table entries,
586      new vtables will be initialized.  We may reach the virtual
587      baseclass via ambiguous intervening baseclasses.  This
588      loop makes sure we get through to the actual baseclass we marked.
589 
590      Also, update the vtable entries to reflect the overrides
591      of the top-most class (short of the top type).  */
592 
593   do
594     {
595       result = get_base_distance (basetype, for_type, 0, &path);
596       for_type = path;
597       while (path)
598 	{
599 	  tree path_binfo = path;
600 	  tree path_type = BINFO_TYPE (path);
601 
602 	  if (TREE_VIA_VIRTUAL (path))
603 	    path_binfo = binfo_member (path_type,
604 				       CLASSTYPE_VBASECLASSES (current_class_type));
605 
606 	  SET_BINFO_VTABLE_PATH_MARKED (path_binfo);
607 	  if (BINFO_INHERITANCE_CHAIN (path)
608 	      && CLASSTYPE_VFIELD (path_type) != NULL_TREE
609 	      && (DECL_NAME (CLASSTYPE_VFIELD (BINFO_TYPE (binfo)))
610 		  == DECL_NAME (CLASSTYPE_VFIELD (path_type)))
611 	      /* This is the baseclass just before the original FOR_TYPE.  */
612 	      && BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (path)) == NULL_TREE)
613 	    {
614 	      tree old_virtuals = TREE_CHAIN (BINFO_VIRTUALS (binfo));
615 	      tree new_virtuals = TREE_CHAIN (BINFO_VIRTUALS (path_binfo));
616 	      if (flag_dossier)
617 		{
618 		  old_virtuals = TREE_CHAIN (old_virtuals);
619 		  new_virtuals = TREE_CHAIN (new_virtuals);
620 		}
621 	      while (old_virtuals)
622 		{
623 		  TREE_VALUE (old_virtuals) = TREE_VALUE (new_virtuals);
624 		  old_virtuals = TREE_CHAIN (old_virtuals);
625 		  new_virtuals = TREE_CHAIN (new_virtuals);
626 		}
627 	    }
628 	  path = BINFO_INHERITANCE_CHAIN (path);
629 	}
630     }
631   while (result == -2);
632 }
633 
634 /* Access the virtual function table entry that logically
635    contains BASE_FNDECL.  VIRTUALS is the virtual function table's
636    initializer.  */
637 static tree
get_vtable_entry(virtuals,base_fndecl)638 get_vtable_entry (virtuals, base_fndecl)
639      tree virtuals, base_fndecl;
640 {
641   unsigned HOST_WIDE_INT i = (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
642 #ifdef VTABLE_USES_MASK
643 	   && 0
644 #endif
645 	   ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
646 	      & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1))
647 	   : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
648 
649 #ifdef GATHER_STATISTICS
650   n_vtable_searches += i;
651 #endif
652 
653   while (i > 0)
654     {
655       virtuals = TREE_CHAIN (virtuals);
656       i -= 1;
657     }
658   return virtuals;
659 }
660 
661 /* Put new entry ENTRY into virtual function table initializer
662    VIRTUALS.  The virtual function table is for type CONTEXT.
663 
664    Also update DECL_VINDEX (FNDECL).  */
665 
666 static void
modify_vtable_entry(old_entry_in_list,new_entry,fndecl,context)667 modify_vtable_entry (old_entry_in_list, new_entry, fndecl, context)
668      tree old_entry_in_list, new_entry, fndecl, context;
669 {
670   tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list));
671   tree vindex;
672 
673   /* We can't put in the really right offset information
674      here, since we have not yet laid out the class to
675      take into account virtual base classes.  */
676   TREE_VALUE (old_entry_in_list) = new_entry;
677   vindex = DECL_VINDEX (TREE_OPERAND (base_pfn, 0));
678   if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
679     DECL_VINDEX (fndecl) = vindex;
680   else
681     {
682       if (! tree_int_cst_equal (DECL_VINDEX (fndecl), vindex))
683 	{
684 	  tree elts = CONSTRUCTOR_ELTS (new_entry);
685 	  tree vfield = CLASSTYPE_VFIELD (context);
686 
687 	  if (! doing_hard_virtuals)
688 	    {
689 	      pending_hard_virtuals
690 		= tree_cons (fndecl, FNADDR_FROM_VTABLE_ENTRY (new_entry),
691 			     pending_hard_virtuals);
692 	      TREE_TYPE (pending_hard_virtuals) = TREE_OPERAND (base_pfn, 0);
693 	      return;
694 	    }
695 
696 #if 0
697 	  my_friendly_abort (3);
698 
699 	  /* Compute the relative offset of vtable we are really looking for.  */
700 	  TREE_VALUE (elts) = size_binop (PLUS_EXPR,
701 					  size_int (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (vfield))
702 /* ??? This may be wrong. */
703 						    / BITS_PER_UNIT),
704 					  TREE_VALUE (elts));
705 	  /* Say what index to use when we use that vtable.  */
706 #ifndef VTABLE_USES_MASK
707 	  vindex = build_int_2 (TREE_INT_CST_LOW (vindex)
708 				& ~((unsigned HOST_WIDE_INT) 1
709 				    << (BITS_PER_WORD -1)), 0);
710 #endif
711 	  TREE_VALUE (TREE_CHAIN (elts)) = vindex;
712 #endif
713 	}
714     }
715 }
716 
717 /* Check to ensure that the virtual function table slot in VFIELD,
718    found by DECL_VINDEX of the BASE_FNDECL is in fact from a parent
719    virtual function table that is the same parent as for the
720    BASE_FNDECL given to us.  */
721 
722 static int
related_vslot(base_fndecl,vfields,type)723 related_vslot (base_fndecl, vfields, type)
724      tree base_fndecl, vfields, type;
725 {
726   tree base_context = TYPE_MAIN_VARIANT (DECL_CONTEXT (base_fndecl));
727   tree base;
728   tree path;
729   int distance;
730 
731   if (TREE_CODE (vfields) != TREE_LIST)
732     abort ();
733   base = VF_NORMAL_VALUE (vfields);
734   if (base == NULL_TREE)
735     base = VF_BASETYPE_VALUE (vfields);
736 
737   /* The simple right way to do this is to ensure that the context of
738      the base virtual function is found along the leftmost path
739      between the most derived type associated with the vfield and the
740      current type.  */
741   distance = get_base_distance (base, type, 0, &path);
742   if (distance == -1)
743     abort ();
744   while (path)
745     {
746       if (BINFO_TYPE (path) == base_context)
747 	return 1;
748       path = BINFO_INHERITANCE_CHAIN (path);
749     }
750 
751   /* given:
752 		Rr
753 	       / \
754 	      Mm  Hh
755 	       \ /
756 	        P
757 
758      make sure we fill in P's vtable for H with overrides of r,
759      but be cautious of virtual base classes.  */
760   /* Combine the two below after debugging. */
761   if (get_base_distance (base_context, base, 0, &path) != -1)
762     {
763       while (path)
764 	{
765 	  if (TREE_VIA_VIRTUAL (path))
766 	    return 0;
767 	  path = BINFO_INHERITANCE_CHAIN (path);
768 	}
769       return 1;
770     }
771   return 0;
772 }
773 
774 /* Modify virtual function tables in lattice topped by T to
775    place FNDECL in tables which previously held BASE_FNDECL.
776    PFN is just FNDECL wrapped in an ADDR_EXPR, so that it
777    is suitable for placement directly into an initializer.
778 
779    All distinct virtual function tables that this type uses
780    must be updated.  */
781 static void
modify_vtable_entries(t,fndecl,base_fndecl,pfn)782 modify_vtable_entries (t, fndecl, base_fndecl, pfn)
783      tree t;
784      tree fndecl, base_fndecl, pfn;
785 {
786   tree base_offset, offset;
787   tree base_context = DECL_CLASS_CONTEXT (base_fndecl);
788   tree context = DECL_CLASS_CONTEXT (fndecl);
789   tree vfield = CLASSTYPE_VFIELD (t);
790   tree vfields, vbases;
791 
792   DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
793 
794   offset = integer_zero_node;
795   if (context != t && TYPE_USES_COMPLEX_INHERITANCE (t))
796     {
797       offset = virtual_offset (context, CLASSTYPE_VBASECLASSES (t), offset);
798       if (offset == NULL_TREE)
799 	{
800 	  tree binfo = binfo_value (context, t);
801 	  offset = BINFO_OFFSET (binfo);
802 	}
803     }
804 
805   /* For each layer of base class (i.e., the first base class, and each
806      virtual base class from that one), modify the virtual function table
807      of the derived class to contain the new virtual function.
808      A class has as many vfields as it has virtual base classes (total).  */
809   for (vfields = CLASSTYPE_VFIELDS (t); vfields; vfields = TREE_CHAIN (vfields))
810     {
811       int normal = 1;
812       tree binfo, this_offset;
813       tree base, path;
814 
815 /* This can go away when the new searching strategy as a little mileage on it. */
816 #define NEW_SEARCH 1
817 #if NEW_SEARCH
818       if (!related_vslot (base_fndecl, vfields, t))
819 	  continue;
820 #endif
821 
822       /* Find the right base class for this derived class, call it BASE.  */
823       base = VF_BASETYPE_VALUE (vfields);
824 
825 #if NEW_SEARCH == 0
826       if (base != base_context)
827 	{
828 	  /* If BASE_FNDECL is not contained in the vtable accessed by
829 	     the vslot, don't try to modify the vtable.
830 
831 	     Virtual functions from virtual baseclasses are not in derived
832 	     virtual function tables.  This is an implementation decision;
833 	     it keeps there from being a combinatorial explosion in the
834 	     number of different vtables which must be maintained.  */
835 
836 	  /* In this case, we need to know whether BASE is derived
837 	     from BASE_CONTEXT in any case, even the case where the
838 	     derivation is ambiguous.  */
839 	  int distance = get_base_distance (base, base_context, 0, (tree *)0);
840 	  if (distance < 0 && distance != -2)
841 	    continue;
842 
843 	  /* BASE_FNDECL is defined in a class derived from
844 	     the base class owning this VFIELD.  */
845 	}
846 #endif
847 
848       /* Get the path starting from the deepest base class CONTEXT
849 	 of T (i.e., first defn of BASE_FNDECL).  */
850       get_base_distance (base_context, t, 0, &path);
851 
852       /* Get our best approximation of what to use for constructing
853 	 the virtual function table for T.  */
854       do
855 	{
856 	  /* Walk from base toward derived, stopping at the
857 	     most derived baseclass that matters.  That baseclass
858 	     is exactly the one which provides the vtable along
859 	     the VFIELD spine, but no more.  */
860 	  if (TREE_VIA_VIRTUAL (path))
861 	    {
862 	      base = path;
863 	      binfo = binfo_member (BINFO_TYPE (base), CLASSTYPE_VBASECLASSES (t));
864 	      break;
865 	    }
866 	  if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE
867 	      || (BINFO_TYPE (BINFO_BASETYPE (BINFO_INHERITANCE_CHAIN (path), 0))
868 		  != BINFO_TYPE (path))
869 	      || BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (path)) == NULL_TREE)
870 	    {
871 	      base = path;
872 	      binfo = base;
873 	      break;
874 	    }
875 	  path = BINFO_INHERITANCE_CHAIN (path);
876 	}
877       while (1);
878 
879       /* Find the right offset for the this pointer based on the base
880 	 class we just found.  */
881       base_offset = BINFO_OFFSET (binfo);
882       this_offset = size_binop (MINUS_EXPR, offset, base_offset);
883 
884       /* Make sure we can modify the derived association with immunity.  */
885       if (TREE_USED (TYPE_BINFO (t)))
886 	TYPE_BINFO (t) = copy_binfo (TYPE_BINFO (t));
887 
888       /* We call this case NORMAL iff this virtual function table
889 	 pointer field has its storage reserved in this class.
890 	 This is normally the case without virtual baseclasses
891 	 or off-center multiple baseclasses.  */
892       normal = (vfield != NULL_TREE
893 		&& VF_BASETYPE_VALUE (vfields) == DECL_FCONTEXT (vfield)
894 		&& (VF_BINFO_VALUE (vfields) == NULL_TREE
895 		    || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields))));
896 
897       if (normal && VF_BINFO_VALUE (vfields))
898 	/* Everything looks normal so far...check that we are really
899 	   working from VFIELD's basetype, and not some other appearance
900 	   of that basetype in the lattice.  */
901 	normal = (VF_BINFO_VALUE (vfields)
902 		  == get_binfo (VF_BASETYPE_VALUE (vfields), t, 0));
903 
904       if (normal)
905 	{
906 	  /* In this case, it is *type*'s vtable we are modifying.
907 	     We start with the approximation that it's vtable is that
908 	     of the immediate base class.  */
909 	  base_context = t;
910 	  binfo = TYPE_BINFO (t);
911 	  if (! BINFO_NEW_VTABLE_MARKED (binfo))
912 	    build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
913 	}
914       else
915 	{
916 	  /* This is our very own copy of `basetype' to play with.
917 	     Later, we will fill in all the virtual functions
918 	     that override the virtual functions in these base classes
919 	     which are not defined by the current type.  */
920 	  if (! BINFO_NEW_VTABLE_MARKED (binfo))
921 	    prepare_fresh_vtable (binfo, base, t);
922 	}
923 
924       modify_vtable_entry (get_vtable_entry (BINFO_VIRTUALS (binfo), base_fndecl),
925 			   build_vtable_entry (this_offset, pfn),
926 			   fndecl, base_context);
927     }
928   for (vbases = CLASSTYPE_VBASECLASSES (t); vbases; vbases = TREE_CHAIN (vbases))
929     {
930       tree this_offset;
931       tree base, path;
932 
933       if (! BINFO_VTABLE (vbases))
934 	/* There are only two ways that a type can fail to have
935 	   virtual functions: neither it nor any of its base
936 	   types define virtual functions (in which case
937 	   no updating need be done), or virtual functions
938 	   accessible to it come from virtual base classes
939 	   (in which case we have or will get them modified
940 	   in other passes of this loop).  */
941 	continue;
942 
943       base = BINFO_TYPE (vbases);
944       path = NULL_TREE;
945 
946       if (base != base_context
947 	  && get_base_distance (base_context, base, 0, &path) == -1)
948 	continue;
949 
950       if (path)
951 	this_offset = size_binop (MINUS_EXPR, offset, BINFO_OFFSET (path));
952       else
953 	this_offset = offset;
954 
955       /* Doesn't matter if not actually from this virtual base class,
956          but shouldn't come from deeper virtual baseclasses.  The enclosing
957 	 loop should take care of such baseclasses.  */
958       while (path)
959 	{
960 	  if (TREE_VIA_VIRTUAL (path))
961 	    goto skip;
962 	  path = BINFO_INHERITANCE_CHAIN (path);
963 	}
964 
965       base_offset = BINFO_OFFSET (vbases);
966       this_offset = size_binop (MINUS_EXPR, this_offset, base_offset);
967 
968       /* Make sure we can modify the derived association with immunity.  */
969       if (TREE_USED (TYPE_BINFO (t)))
970 	TYPE_BINFO (t) = copy_binfo (TYPE_BINFO (t));
971 
972       /* This is our very own copy of `basetype' to play with.  */
973       if (! BINFO_NEW_VTABLE_MARKED (vbases))
974 	{
975 	  tree context_binfo = binfo_value (base_context, base);
976 	  prepare_fresh_vtable (vbases, context_binfo, t);
977 	}
978       modify_vtable_entry (get_vtable_entry (BINFO_VIRTUALS (vbases), base_fndecl),
979 			   build_vtable_entry (this_offset, pfn),
980 			   fndecl, base_context);
981     skip: {}
982     }
983 }
984 
985 static tree
add_virtual_function(pending_virtuals,has_virtual,x,t)986 add_virtual_function (pending_virtuals, has_virtual, x, t)
987      tree pending_virtuals;
988      int *has_virtual;
989      tree x;
990      tree t; /* Structure type. */
991 {
992   int debug_vbase = 1;
993 
994   /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
995      convert to void *.  Make such a conversion here.  */
996   tree vfn = build1 (ADDR_EXPR, ptr_type_node, x);
997   TREE_CONSTANT (vfn) = 1;
998 
999   /* current_class_type may be NULL_TREE in case of error.  */
1000   if (current_class_type)
1001     TREE_ADDRESSABLE (x) = CLASSTYPE_VTABLE_NEEDS_WRITING (current_class_type);
1002 
1003   /* If the virtual function is a redefinition of a prior one,
1004      figure out in which base class the new definition goes,
1005      and if necessary, make a fresh virtual function table
1006      to hold that entry.  */
1007   if (DECL_VINDEX (x) == error_mark_node)
1008     {
1009       tree entry = build_vtable_entry (integer_zero_node, vfn);
1010 
1011       if (flag_dossier && *has_virtual == 0)
1012 	{
1013 	  /* CLASSTYPE_DOSSIER is only used as a Boolean (NULL or not). */
1014 	  CLASSTYPE_DOSSIER (t) = integer_one_node;
1015 	  *has_virtual = 1;
1016         }
1017 
1018       /* Build a new INT_CST for this DECL_VINDEX.  */
1019 #ifdef VTABLE_USES_MASK
1020       SET_DECL_VINDEX (x, build_int_2 (++(*has_virtual), 0));
1021 #else
1022       {
1023 	static tree index_table[256];
1024 	tree index;
1025 	int i = ++(*has_virtual);
1026 
1027 	if (i >= 256 || index_table[i] == 0)
1028 	  {
1029 	    index = build_int_2 (((unsigned HOST_WIDE_INT) 1
1030 				  << (BITS_PER_WORD - 1)) | i, ~0);
1031 	    if (i < 256)
1032 	      index_table[i] = index;
1033 	  }
1034 	else
1035 	  index = index_table[i];
1036 
1037 	DECL_VINDEX (x) = index;
1038       }
1039 #endif
1040       pending_virtuals = tree_cons (DECL_VINDEX (x), entry, pending_virtuals);
1041     }
1042   /* Happens if declared twice in class or we're not in a class definition.
1043      We will give error later or we've already given it.  */
1044   else if (TREE_CODE (DECL_VINDEX (x)) == INTEGER_CST
1045 	   || current_class_type == NULL_TREE)
1046     return pending_virtuals;
1047   else if (debug_vbase && TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
1048     {
1049       /* Need an entry in some other virtual function table.
1050          Deal with this after we have laid out our virtual base classes.  */
1051       pending_hard_virtuals = temp_tree_cons (x, vfn, pending_hard_virtuals);
1052     }
1053   else
1054     {
1055       /* Need an entry in some other virtual function table.
1056          We can do this now.  */
1057       tree base_fndecl_list = DECL_VINDEX (x), base_fndecls, prev = 0;
1058       tree vtable_context = DECL_FCONTEXT (CLASSTYPE_VFIELD (current_class_type));
1059       tree true_base_fndecl = 0;
1060 
1061       /* First assign DECL_VINDEX from the base vfn with which
1062 	 we share our vtable.  */
1063       base_fndecls = base_fndecl_list;
1064       while (base_fndecls)
1065 	{
1066 	  if (TREE_CHAIN (base_fndecls) == NULL_TREE
1067 	      || DECL_FCONTEXT (CLASSTYPE_VFIELD (DECL_CLASS_CONTEXT (TREE_VALUE (base_fndecls)))) == vtable_context)
1068 	    {
1069 	      true_base_fndecl = TREE_VALUE (base_fndecls);
1070 	      modify_vtable_entries (current_class_type, x,
1071 				     true_base_fndecl, vfn);
1072 	      if (prev)
1073 		TREE_CHAIN (prev) = TREE_CHAIN (base_fndecls);
1074 	      else
1075 		base_fndecl_list = prev;
1076 	      break;
1077 	    }
1078 	  prev = base_fndecls;
1079 	  base_fndecls = TREE_CHAIN (base_fndecls);
1080 	}
1081 
1082       /* Now fill in the rest of the vtables.  */
1083       base_fndecls = base_fndecl_list;
1084       while (base_fndecls)
1085 	{
1086 	  /* If we haven't found one we like, first one wins.  */
1087 	  if (true_base_fndecl == 0)
1088 	    true_base_fndecl = TREE_VALUE (base_fndecls);
1089 
1090 	  modify_vtable_entries (current_class_type, x,
1091 				 TREE_VALUE (base_fndecls), vfn);
1092 	  base_fndecls = TREE_CHAIN (base_fndecls);
1093 	}
1094 
1095       DECL_CONTEXT (x) = DECL_CONTEXT (true_base_fndecl);
1096     }
1097   return pending_virtuals;
1098 }
1099 
1100 /* Obstack on which to build the vector of class methods.  */
1101 struct obstack class_obstack;
1102 extern struct obstack *current_obstack;
1103 
1104 /* Add method METHOD to class TYPE.  This is used when a method
1105    has been defined which did not initially appear in the class definition,
1106    and helps cut down on spurious error messages.
1107 
1108    FIELDS is the entry in the METHOD_VEC vector entry of the class type where
1109    the method should be added.  */
1110 void
add_method(type,fields,method)1111 add_method (type, fields, method)
1112      tree type, *fields, method;
1113 {
1114   /* We must make a copy of METHOD here, since we must be sure that
1115      we have exclusive title to this method's DECL_CHAIN.  */
1116   tree decl;
1117 
1118   push_obstacks (&permanent_obstack, &permanent_obstack);
1119   {
1120     decl = copy_node (method);
1121     if (DECL_RTL (decl) == 0
1122         && (!processing_template_decl
1123             || !uses_template_parms (decl)))
1124       {
1125 	make_function_rtl (decl);
1126 	DECL_RTL (method) = DECL_RTL (decl);
1127       }
1128   }
1129 
1130   if (fields && *fields)
1131     {
1132       /* Take care not to hide destructor.  */
1133       DECL_CHAIN (decl) = DECL_CHAIN (*fields);
1134       DECL_CHAIN (*fields) = decl;
1135     }
1136   else if (CLASSTYPE_METHOD_VEC (type) == 0)
1137     {
1138       tree method_vec = make_node (TREE_VEC);
1139       if (TYPE_IDENTIFIER (type) == DECL_NAME (decl))
1140 	{
1141 	  TREE_VEC_ELT (method_vec, 0) = decl;
1142 	  TREE_VEC_LENGTH (method_vec) = 1;
1143 	}
1144       else
1145 	{
1146 	  /* ??? Is it possible for there to have been enough room in the
1147 	     current chunk for the tree_vec structure but not a tree_vec
1148 	     plus a tree*?  Will this work in that case?  */
1149 	  obstack_free (current_obstack, method_vec);
1150 	  obstack_blank (current_obstack, sizeof (struct tree_vec) + sizeof (tree *));
1151 	  TREE_VEC_ELT (method_vec, 1) = decl;
1152 	  TREE_VEC_LENGTH (method_vec) = 2;
1153 	  obstack_finish (current_obstack);
1154 	}
1155       CLASSTYPE_METHOD_VEC (type) = method_vec;
1156     }
1157   else
1158     {
1159       tree method_vec = CLASSTYPE_METHOD_VEC (type);
1160       int len = TREE_VEC_LENGTH (method_vec);
1161 
1162       /* Adding a new ctor or dtor.  This is easy because our
1163          METHOD_VEC always has a slot for such entries.  */
1164       if (TYPE_IDENTIFIER (type) == DECL_NAME (decl))
1165 	{
1166 	  /* TREE_VEC_ELT (method_vec, 0) = decl; */
1167 	  if (decl != TREE_VEC_ELT (method_vec, 0))
1168 	    {
1169 	      DECL_CHAIN (decl) = TREE_VEC_ELT (method_vec, 0);
1170 	      TREE_VEC_ELT (method_vec, 0) = decl;
1171 	    }
1172 	}
1173       else
1174 	{
1175 	  /* This is trickier.  We try to extend the TREE_VEC in-place,
1176 	     but if that does not work, we copy all its data to a new
1177 	     TREE_VEC that's large enough.  */
1178 	  struct obstack *ob = &class_obstack;
1179 	  tree *end = (tree *)obstack_next_free (ob);
1180 
1181 	  if (end != TREE_VEC_END (method_vec))
1182 	    {
1183 	      ob = current_obstack;
1184 	      TREE_VEC_LENGTH (method_vec) += 1;
1185 	      TREE_VEC_ELT (method_vec, len) = NULL_TREE;
1186 	      method_vec = copy_node (method_vec);
1187 	      TREE_VEC_LENGTH (method_vec) -= 1;
1188 	    }
1189 	  else
1190 	    {
1191 	      tree tmp_vec = (tree) obstack_base (ob);
1192 	      if (obstack_room (ob) < sizeof (tree))
1193 		{
1194 		  obstack_blank (ob, sizeof (struct tree_common)
1195 				 + tree_code_length[(int) TREE_VEC]
1196 				   * sizeof (char *)
1197 				 + len * sizeof (tree));
1198 		  tmp_vec = (tree) obstack_base (ob);
1199 		  bcopy (method_vec, tmp_vec,
1200 			 (sizeof (struct tree_common)
1201 			  + tree_code_length[(int) TREE_VEC] * sizeof (char *)
1202 			  + (len-1) * sizeof (tree)));
1203 		  method_vec = tmp_vec;
1204 		}
1205 	      else
1206 		obstack_blank (ob, sizeof (tree));
1207 	    }
1208 
1209 	  obstack_finish (ob);
1210 	  TREE_VEC_ELT (method_vec, len) = decl;
1211 	  TREE_VEC_LENGTH (method_vec) = len + 1;
1212 	  CLASSTYPE_METHOD_VEC (type) = method_vec;
1213 
1214 	  if (TYPE_BINFO_BASETYPES (type) && CLASSTYPE_BASELINK_VEC (type))
1215 	    {
1216 	      /* ??? May be better to know whether these can be extended?  */
1217 	      tree baselink_vec = CLASSTYPE_BASELINK_VEC (type);
1218 
1219 	      TREE_VEC_LENGTH (baselink_vec) += 1;
1220 	      CLASSTYPE_BASELINK_VEC (type) = copy_node (baselink_vec);
1221 	      TREE_VEC_LENGTH (baselink_vec) -= 1;
1222 
1223 	      TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), len) = 0;
1224 	    }
1225 	}
1226     }
1227   DECL_CONTEXT (decl) = type;
1228   DECL_CLASS_CONTEXT (decl) = type;
1229 
1230   pop_obstacks ();
1231 }
1232 
1233 /* Subroutines of finish_struct.  */
1234 
1235 /* Look through the list of fields for this struct, deleting
1236    duplicates as we go.  This must be recursive to handle
1237    anonymous unions.
1238 
1239    FIELD is the field which may not appear anywhere in FIELDS.
1240    FIELD_PTR, if non-null, is the starting point at which
1241    chained deletions may take place.
1242    The value returned is the first acceptable entry found
1243    in FIELDS.
1244 
1245    Note that anonymous fields which are not of UNION_TYPE are
1246    not duplicates, they are just anonymous fields.  This happens
1247    when we have unnamed bitfields, for example.  */
1248 static tree
delete_duplicate_fields_1(field,field_ptr,fields)1249 delete_duplicate_fields_1 (field, field_ptr, fields)
1250      tree field, *field_ptr, fields;
1251 {
1252   tree x;
1253   tree prev = field_ptr ? *field_ptr : 0;
1254   if (DECL_NAME (field) == 0)
1255     {
1256       if (TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)
1257 	return fields;
1258 
1259       for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1260 	fields = delete_duplicate_fields_1 (x, field_ptr, fields);
1261       if (prev)
1262 	TREE_CHAIN (prev) = fields;
1263       return fields;
1264     }
1265   else
1266     {
1267       for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1268 	{
1269 	  if (DECL_NAME (x) == 0)
1270 	    {
1271 	      if (TREE_CODE (TREE_TYPE (x)) != UNION_TYPE)
1272 		continue;
1273 	      TYPE_FIELDS (TREE_TYPE (x))
1274 		= delete_duplicate_fields_1 (field, (tree *)0, TYPE_FIELDS (TREE_TYPE (x)));
1275 	      if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1276 		{
1277 		  if (prev == 0)
1278 		    fields = TREE_CHAIN (fields);
1279 		  else
1280 		    TREE_CHAIN (prev) = TREE_CHAIN (x);
1281 		}
1282 	    }
1283 	  else
1284 	    {
1285 	      if (DECL_NAME (field) == DECL_NAME (x))
1286 		{
1287 		  if (TREE_CODE (field) == CONST_DECL
1288 		      && TREE_CODE (x) == CONST_DECL)
1289 		    error_with_decl (x, "duplicate enum value `%s'");
1290 		  else if (TREE_CODE (field) == CONST_DECL
1291 			   || TREE_CODE (x) == CONST_DECL)
1292 		    error_with_decl (x, "duplicate field `%s' (as enum and non-enum)");
1293 		  else if (TREE_CODE (field) == TYPE_DECL
1294 			   && TREE_CODE (x) == TYPE_DECL)
1295 		    error_with_decl (x, "duplicate class scope type `%s'");
1296 		  else if (TREE_CODE (field) == TYPE_DECL
1297 			   || TREE_CODE (x) == TYPE_DECL)
1298 		    error_with_decl (x, "duplicate field `%s' (as type and non-type)");
1299 		  else
1300 		    error_with_decl (x, "duplicate member `%s'");
1301 		  if (prev == 0)
1302 		    fields = TREE_CHAIN (fields);
1303 		  else
1304 		    TREE_CHAIN (prev) = TREE_CHAIN (x);
1305 		}
1306 	    }
1307 	}
1308     }
1309   return fields;
1310 }
1311 
1312 static void
delete_duplicate_fields(fields)1313 delete_duplicate_fields (fields)
1314      tree fields;
1315 {
1316   tree x;
1317   for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1318     TREE_CHAIN (x) = delete_duplicate_fields_1 (x, &x, TREE_CHAIN (x));
1319 }
1320 
1321 /* Change the visibility of T::FDECL to VISIBILITY.
1322    Return 1 if change was legit, otherwise return 0.  */
1323 static int
alter_visibility(t,fdecl,visibility)1324 alter_visibility (t, fdecl, visibility)
1325      tree t;
1326      tree fdecl;
1327      enum visibility_type visibility;
1328 {
1329   tree elem = purpose_member (t, DECL_VISIBILITY (fdecl));
1330   if (elem && TREE_VALUE (elem) != (tree)visibility)
1331     {
1332       if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1333 	{
1334 	  error_with_decl (TREE_TYPE (fdecl), "conflicting visibility specifications for method `%s', ignored");
1335 	}
1336       else error ("conflicting visibility specifications for field `%s', ignored", IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1337     }
1338   else if (TREE_PRIVATE (fdecl) && visibility != visibility_private)
1339     error_with_decl (fdecl, "cannot make private `%s' non-private");
1340   else if (TREE_PROTECTED (fdecl) && visibility == visibility_public)
1341     error_with_decl (fdecl, "cannot make protected `%s' public");
1342   /* ARM 11.3: an access declaration may not be used to restrict access
1343      to a member that is accessible in the base class.  */
1344   else if (TREE_PUBLIC (fdecl)
1345 	   && (visibility == visibility_private
1346 	       || visibility == visibility_protected))
1347     error_with_decl (fdecl, "cannot reduce visibility of public member `%s'");
1348   else if (elem == NULL_TREE)
1349     {
1350       DECL_VISIBILITY (fdecl) = tree_cons (t, (tree)visibility,
1351 					   DECL_VISIBILITY (fdecl));
1352       return 1;
1353     }
1354   return 0;
1355 }
1356 
1357 static tree
get_vfield_offset(binfo)1358 get_vfield_offset (binfo)
1359      tree binfo;
1360 {
1361   return size_binop (PLUS_EXPR,
1362 		     DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))),
1363 		     BINFO_OFFSET (binfo));
1364 }
1365 
1366 /* If FOR_TYPE needs to reinitialize virtual function table pointers
1367    for TYPE's sub-objects, add such reinitializations to BASE_INIT_LIST.
1368    Returns BASE_INIT_LIST appropriately modified.  */
1369 
1370 static tree
maybe_fixup_vptrs(for_type,binfo,base_init_list)1371 maybe_fixup_vptrs (for_type, binfo, base_init_list)
1372      tree for_type, binfo, base_init_list;
1373 {
1374   /* Now reinitialize any slots that don't fall under our virtual
1375      function table pointer.  */
1376   tree vfields = CLASSTYPE_VFIELDS (BINFO_TYPE (binfo));
1377   while (vfields)
1378     {
1379       tree base_binfo = get_binfo (VF_BASETYPE_VALUE (vfields), for_type, 0);
1380       if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (VF_BASETYPE_VALUE (vfields)))
1381 	{
1382 	  tree base_offset = get_vfield_offset (base_binfo);
1383 	  if (! tree_int_cst_equal (base_offset, get_vfield_offset (TYPE_BINFO (for_type)))
1384 	      && ! tree_int_cst_equal (base_offset, get_vfield_offset (binfo)))
1385 	    base_init_list = tree_cons (error_mark_node, base_binfo,
1386 					base_init_list);
1387 	}
1388       vfields = TREE_CHAIN (vfields);
1389     }
1390   return base_init_list;
1391 }
1392 
1393 /* If TYPE does not have a constructor, then the compiler must
1394    manually deal with all of the initialization this type requires.
1395 
1396    If a base initializer exists only to fill in the virtual function
1397    table pointer, then we mark that fact with the TREE_VIRTUAL bit.
1398    This way, we avoid multiple initializations of the same field by
1399    each virtual function table up the class hierarchy.
1400 
1401    Virtual base class pointers are not initialized here.  They are
1402    initialized only at the "top level" of object creation.  If we
1403    initialized them here, we would have to skip a lot of work.  */
1404 
1405 static void
build_class_init_list(type)1406 build_class_init_list (type)
1407      tree type;
1408 {
1409   tree base_init_list = NULL_TREE;
1410   tree member_init_list = NULL_TREE;
1411 
1412   /* Since we build member_init_list and base_init_list using
1413      tree_cons, backwards fields the all through work.  */
1414   tree x;
1415   tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
1416   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1417 
1418   for (x = TYPE_FIELDS (type); x; x = TREE_CHAIN (x))
1419     {
1420       if (TREE_CODE (x) != FIELD_DECL)
1421 	continue;
1422 
1423       if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (x))
1424 	  || DECL_INITIAL (x) != NULL_TREE)
1425 	member_init_list = tree_cons (x, type, member_init_list);
1426     }
1427   member_init_list = nreverse (member_init_list);
1428 
1429   /* We will end up doing this last.  Need special marker
1430      to avoid infinite regress.  */
1431   if (TYPE_VIRTUAL_P (type))
1432     {
1433       base_init_list = build_tree_list (error_mark_node, TYPE_BINFO (type));
1434       if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (type) == 0)
1435 	TREE_VALUE (base_init_list) = NULL_TREE;
1436       TREE_ADDRESSABLE (base_init_list) = 1;
1437     }
1438 
1439   /* Each base class which needs to have initialization
1440      of some kind gets to make such requests known here.  */
1441   for (i = n_baseclasses-1; i >= 0; i--)
1442     {
1443       tree base_binfo = TREE_VEC_ELT (binfos, i);
1444       tree blist;
1445 
1446       /* Don't initialize virtual baseclasses this way.  */
1447       if (TREE_VIA_VIRTUAL (base_binfo))
1448 	continue;
1449 
1450       if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (base_binfo)))
1451 	{
1452 	  /* ...and the last shall come first...  */
1453 	  base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
1454 	  base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
1455 	  continue;
1456 	}
1457 
1458       if ((blist = CLASSTYPE_BASE_INIT_LIST (BINFO_TYPE (base_binfo))) == NULL_TREE)
1459 	/* Nothing to initialize.  */
1460 	continue;
1461 
1462       /* ...ditto...  */
1463       base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
1464 
1465       /* This is normally true for single inheritance.
1466 	 The win is we can shrink the chain of initializations
1467 	 to be done by only converting to the actual type
1468 	 we are interested in.  */
1469       if (TREE_VALUE (blist)
1470 	  && TREE_CODE (TREE_VALUE (blist)) == TREE_VEC
1471 	  && tree_int_cst_equal (BINFO_OFFSET (base_binfo),
1472 				 BINFO_OFFSET (TREE_VALUE (blist))))
1473 	{
1474 	  if (base_init_list)
1475 	    {
1476 	      /* Does it do more than just fill in a
1477 		 virtual function table pointer?  */
1478 	      if (! TREE_ADDRESSABLE (blist))
1479 		base_init_list = build_tree_list (blist, base_init_list);
1480 	      /* Can we get by just with the virtual function table
1481 		 pointer that it fills in?  */
1482 	      else if (TREE_ADDRESSABLE (base_init_list)
1483 		       && TREE_VALUE (base_init_list) == 0)
1484 		base_init_list = blist;
1485 	      /* Maybe, but it is not obvious as the previous case.  */
1486 	      else if (! CLASSTYPE_NEEDS_VIRTUAL_REINIT (type))
1487 		{
1488 		  tree last = tree_last (base_init_list);
1489 		  while (TREE_VALUE (last)
1490 			 && TREE_CODE (TREE_VALUE (last)) == TREE_LIST)
1491 		    last = tree_last (TREE_VALUE (last));
1492 		  if (TREE_VALUE (last) == 0)
1493 		    base_init_list = build_tree_list (blist, base_init_list);
1494 		}
1495 	    }
1496 	  else
1497 	    base_init_list = blist;
1498 	}
1499       else
1500 	{
1501 	  /* The function expand_aggr_init knows how to do the
1502 	     initialization of `basetype' without getting
1503 	     an explicit `blist'.  */
1504 	  if (base_init_list)
1505 	    base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
1506 	  else
1507 	    base_init_list = CLASSTYPE_BINFO_AS_LIST (BINFO_TYPE (base_binfo));
1508 	}
1509     }
1510 
1511   if (base_init_list)
1512     if (member_init_list)
1513       CLASSTYPE_BASE_INIT_LIST (type) = build_tree_list (base_init_list, member_init_list);
1514     else
1515       CLASSTYPE_BASE_INIT_LIST (type) = base_init_list;
1516   else if (member_init_list)
1517     CLASSTYPE_BASE_INIT_LIST (type) = member_init_list;
1518 }
1519 
1520 struct base_info
1521 {
1522   int has_virtual;
1523   int max_has_virtual;
1524   int n_ancestors;
1525   tree vfield;
1526   tree vfields;
1527   char needs_default_ctor;
1528   char cant_have_default_ctor;
1529   char needs_const_ctor;
1530   char cant_have_const_ctor;
1531   char members_need_dtors;
1532   char needs_virtual_dtor;
1533 };
1534 
1535 /* Record information about type T derived from its base classes.
1536    Store most of that information in T itself, and place the
1537    remaining information in the struct BASE_INFO.
1538 
1539    Propagate basetype offsets throughout the lattice.  Note that the
1540    lattice topped by T is really a pair: it's a DAG that gives the
1541    structure of the derivation hierarchy, and it's a list of the
1542    virtual baseclasses that appear anywhere in the DAG.  When a vbase
1543    type appears in the DAG, it's offset is 0, and it's children start
1544    their offsets from that point.  When a vbase type appears in the list,
1545    its offset is the offset it has in the hierarchy, and its children's
1546    offsets include that offset in theirs.
1547 
1548    Returns the index of the first base class to have virtual functions,
1549    or zero if no such base class.  */
1550 
1551 static int
finish_base_struct(t,b,binfos)1552 finish_base_struct (t, b, binfos)
1553      tree t;
1554      struct base_info *b;
1555      tree binfos;
1556 {
1557   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1558   int first_vfn_base_index = -1;
1559   bzero (b, sizeof (struct base_info));
1560 
1561   for (i = 0; i < n_baseclasses; i++)
1562     {
1563       tree base_binfo = TREE_VEC_ELT (binfos, i);
1564       tree basetype = BINFO_TYPE (base_binfo);
1565 
1566       /* If the type of basetype is incomplete, then
1567 	 we already complained about that fact
1568 	 (and we should have fixed it up as well).  */
1569       if (TYPE_SIZE (basetype) == 0)
1570 	{
1571 	  int j;
1572 	  /* The base type is of incomplete type.  It is
1573 	     probably best to pretend that it does not
1574 	     exist.  */
1575 	  if (i == n_baseclasses-1)
1576 	    TREE_VEC_ELT (binfos, i) = NULL_TREE;
1577 	  TREE_VEC_LENGTH (binfos) -= 1;
1578 	  n_baseclasses -= 1;
1579 	  for (j = i; j+1 < n_baseclasses; j++)
1580 	    TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1581 	}
1582 
1583       if (TYPE_NEEDS_DESTRUCTOR (basetype))
1584 	b->members_need_dtors = 1;
1585       if (TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1586 	b->needs_default_ctor = 1;
1587       else if (TYPE_HAS_CONSTRUCTOR (basetype))
1588 	b->cant_have_default_ctor = 1;
1589       if (TYPE_GETS_CONST_INIT_REF (basetype))
1590 	b->needs_const_ctor = 1;
1591       else if (TYPE_GETS_INIT_REF (basetype))
1592 	b->cant_have_const_ctor = 1;
1593 
1594       CLASSTYPE_ALTERS_VISIBILITIES_P (t)
1595 	|= CLASSTYPE_ALTERS_VISIBILITIES_P (basetype);
1596 
1597       b->n_ancestors += CLASSTYPE_N_SUPERCLASSES (basetype);
1598       TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1599       TYPE_NEEDS_CONSTRUCTOR (t) |= TYPE_NEEDS_CONSTRUCTOR (basetype);
1600       TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1601       TYPE_GETS_ASSIGNMENT (t) |= TYPE_GETS_ASSIGNMENT (basetype);
1602       TYPE_GETS_INIT_REF (t) |= TYPE_GETS_INIT_REF (basetype);
1603 
1604       TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1605       TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1606       TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1607 
1608       if (! TREE_VIA_VIRTUAL (base_binfo)
1609 #if 0
1610 	  /* This cannot be done, as prepare_fresh_vtable wants to modify
1611 	     binfos associated with vfields anywhere in the hierarchy, not
1612 	     just immediate base classes.  Due to unsharing, the compiler
1613 	     might consume 3% more memory on a real program.
1614 	     */
1615 	  && ! BINFO_OFFSET_ZEROP (base_binfo)
1616 #endif
1617 	  && BINFO_BASETYPES (base_binfo))
1618 	{
1619 	  tree base_binfos = BINFO_BASETYPES (base_binfo);
1620 	  tree chain = NULL_TREE;
1621 	  int j;
1622 
1623 	  /* Now unshare the structure beneath BASE_BINFO.  */
1624 	  for (j = TREE_VEC_LENGTH (base_binfos)-1;
1625 	       j >= 0; j--)
1626 	    {
1627 	      tree base_base_binfo = TREE_VEC_ELT (base_binfos, j);
1628 	      if (! TREE_VIA_VIRTUAL (base_base_binfo))
1629 		TREE_VEC_ELT (base_binfos, j)
1630 		  = make_binfo (BINFO_OFFSET (base_base_binfo),
1631 				BINFO_TYPE (base_base_binfo),
1632 				BINFO_VTABLE (base_base_binfo),
1633 				BINFO_VIRTUALS (base_base_binfo),
1634 				chain);
1635 	      chain = TREE_VEC_ELT (base_binfos, j);
1636 	      TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo);
1637 	      TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo);
1638 	    }
1639 
1640 	  /* Completely unshare potentially shared data, and
1641 	     update what is ours.  */
1642 	  propagate_binfo_offsets (base_binfo, BINFO_OFFSET (base_binfo));
1643 	}
1644 
1645       if (! TREE_VIA_VIRTUAL (base_binfo))
1646 	CLASSTYPE_N_SUPERCLASSES (t) += 1;
1647 
1648       if (TYPE_VIRTUAL_P (basetype))
1649 	{
1650 	  /* If there's going to be a destructor needed, make
1651 	     sure it will be virtual.  */
1652 	  b->needs_virtual_dtor = 1;
1653 
1654 	  /* Don't borrow virtuals from virtual baseclasses.  */
1655 	  if (TREE_VIA_VIRTUAL (base_binfo))
1656 	    continue;
1657 
1658 	  if (first_vfn_base_index < 0)
1659 	    {
1660 	      first_vfn_base_index = i;
1661 
1662 	      b->has_virtual = CLASSTYPE_VSIZE (basetype);
1663 	      b->vfield = CLASSTYPE_VFIELD (basetype);
1664 	      b->vfields = CLASSTYPE_VFIELDS (basetype);
1665 	      CLASSTYPE_VFIELD (t) = b->vfield;
1666 	    }
1667 	  else
1668 	    {
1669 	      /* Only add unique vfields, and flatten them out as we go.  */
1670 	      tree vfields = CLASSTYPE_VFIELDS (basetype);
1671 	      while (vfields)
1672 		{
1673 		  if (VF_BINFO_VALUE (vfields) == NULL_TREE
1674 		      || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1675 		    {
1676 		      tree value = VF_BASETYPE_VALUE (vfields);
1677 		      b->vfields = tree_cons (base_binfo, value, b->vfields);
1678 		      if (DECL_NAME (CLASSTYPE_VFIELD (value))
1679 			  == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1680 			VF_NORMAL_VALUE (b->vfields) = basetype;
1681 		      else
1682 			VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1683 		    }
1684 		  vfields = TREE_CHAIN (vfields);
1685 		}
1686 
1687 	      if (b->has_virtual == 0)
1688 		{
1689 		  first_vfn_base_index = i;
1690 		  b->has_virtual = CLASSTYPE_VSIZE (basetype);
1691 		  b->vfield = CLASSTYPE_VFIELD (basetype);
1692 		  CLASSTYPE_VFIELD (t) = b->vfield;
1693 		}
1694 	    }
1695 	}
1696     }
1697 
1698   {
1699     tree vfields;
1700     /* Find the base class with the largest number of virtual functions.  */
1701     for (vfields = b->vfields; vfields; vfields = TREE_CHAIN (vfields))
1702       {
1703 	if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)) > b->max_has_virtual)
1704 	  b->max_has_virtual = CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields));
1705 	if (VF_DERIVED_VALUE (vfields)
1706 	    && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)) > b->max_has_virtual)
1707 	  b->max_has_virtual = CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields));
1708       }
1709   }
1710 
1711   if (b->vfield == 0)
1712     /* If all virtual functions come only from virtual baseclasses.  */
1713     return -1;
1714   return first_vfn_base_index;
1715 }
1716 
1717 static int
typecode_p(type,code)1718 typecode_p (type, code)
1719      tree type;
1720      enum tree_code code;
1721 {
1722   return (TREE_CODE (type) == code
1723 	  || (TREE_CODE (type) == REFERENCE_TYPE
1724 	      && TREE_CODE (TREE_TYPE (type)) == code));
1725 }
1726 
1727 /* Set memoizing fields and bits of T (and its variants) for later use.
1728    MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables.  */
1729 static void
finish_struct_bits(t,max_has_virtual)1730 finish_struct_bits (t, max_has_virtual)
1731      tree t;
1732      int max_has_virtual;
1733 {
1734   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1735   tree method_vec = CLASSTYPE_METHOD_VEC (t);
1736 
1737   /* Fix up variants (if any).  */
1738   tree variants = TYPE_NEXT_VARIANT (t);
1739   while (variants)
1740     {
1741       /* These fields are in the _TYPE part of the node, not in
1742 	 the TYPE_LANG_SPECIFIC component, so they are not shared.  */
1743       TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1744       TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1745       TYPE_NEEDS_CONSTRUCTOR (variants) = TYPE_NEEDS_CONSTRUCTOR (t);
1746       TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1747       TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
1748 
1749       TYPE_USES_COMPLEX_INHERITANCE (variants) = TYPE_USES_COMPLEX_INHERITANCE (t);
1750       TYPE_VIRTUAL_P (variants) = TYPE_VIRTUAL_P (t);
1751       TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1752       /* Copy whatever these are holding today.  */
1753       TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1754       TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1755       variants = TYPE_NEXT_VARIANT (variants);
1756     }
1757 
1758   if (n_baseclasses && max_has_virtual)
1759     {
1760       /* Done by `finish_struct' for classes without baseclasses.  */
1761       int has_abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (t) != 0;
1762       tree binfos = TYPE_BINFO_BASETYPES (t);
1763       for (i = n_baseclasses-1; i >= 0; i--)
1764 	{
1765 	  has_abstract_virtuals
1766 	    |= (CLASSTYPE_ABSTRACT_VIRTUALS (BINFO_TYPE (TREE_VEC_ELT (binfos, i))) != 0);
1767 	  if (has_abstract_virtuals)
1768 	    break;
1769 	}
1770       if (has_abstract_virtuals)
1771 	CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
1772     }
1773 
1774   if (n_baseclasses)
1775     {
1776       /* Notice whether this class has type conversion functions defined.
1777 	 Also report whether joining two types yields an ambiguity in the
1778 	 virtual function table, e.g.,
1779 
1780 	 struct A { virtual int f (); };
1781 	 struct B { virtual int f (); };
1782 	 struct C : A, B { / * no f (); * / };	/ / error, ambiguous
1783 	 */
1784       tree binfo = TYPE_BINFO (t);
1785       tree binfos = BINFO_BASETYPES (binfo);
1786       int n_binfos = list_length (binfo);
1787       tree vbases = CLASSTYPE_VBASECLASSES (t), basetype;
1788       int n_vbases = list_length (vbases), j;
1789 
1790       build_mi_virtuals (n_binfos+n_vbases*n_baseclasses, max_has_virtual);
1791       /* Fill in virtual function table with values which do not come
1792 	 "normal"ly, i.e., those which come from virtual and/or
1793 	 non-leftmost base classes.  */
1794       for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
1795 	{
1796 	  if (TREE_VIA_VIRTUAL (binfo))
1797 	    /* Virtual functions from virtual baseclasses are done below.  */;
1798 	  else if (CLASSTYPE_VSIZE (BINFO_TYPE (binfo)))
1799 	    {
1800 	      tree virtuals = TREE_CHAIN (BINFO_VIRTUALS (binfo));
1801 	      if (flag_dossier)
1802 		virtuals = TREE_CHAIN (virtuals);
1803 	      add_mi_virtuals (++i, virtuals);
1804 	    }
1805 	}
1806       for (; vbases; vbases = TREE_CHAIN (vbases))
1807 	{
1808 	  basetype = BINFO_TYPE (vbases);
1809 	  if (CLASSTYPE_VSIZE (basetype))
1810 	    for (j = n_baseclasses-1; j >= 0; j--)
1811 	      {
1812 		tree this_binfo = TREE_VEC_ELT (binfos, j);
1813 		if (UNIQUELY_DERIVED_FROM_P (basetype, this_binfo))
1814 		  {
1815 		    tree virtuals = TREE_CHAIN (BINFO_VIRTUALS (vbases));
1816 		    if (flag_dossier)
1817 		      virtuals = TREE_CHAIN (virtuals);
1818 		    add_mi_virtuals (++i, virtuals);
1819 		  }
1820 	      }
1821 	}
1822       for (i = n_baseclasses-1; i >= 0; i--)
1823 	{
1824 	  basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1825 
1826 	  if (TYPE_HAS_CONVERSION (basetype))
1827 	    {
1828 	      TYPE_HAS_CONVERSION (t) = 1;
1829 	      TYPE_HAS_INT_CONVERSION (t) |= TYPE_HAS_INT_CONVERSION (basetype);
1830 	      TYPE_HAS_REAL_CONVERSION (t) |= TYPE_HAS_REAL_CONVERSION (basetype);
1831 	    }
1832 	  if (CLASSTYPE_MAX_DEPTH (basetype) >= CLASSTYPE_MAX_DEPTH (t))
1833 	    CLASSTYPE_MAX_DEPTH (t) = CLASSTYPE_MAX_DEPTH (basetype) + 1;
1834 	}
1835       report_ambiguous_mi_virtuals (n_binfos+n_vbases*n_baseclasses, t);
1836 #if 0
1837       /* Now that we know what the virtual function table looks like,
1838 	 fix up offsets in the presence of virtual base classes.  */
1839       if (n_vbases)
1840 	fixup_vbase_offsets (t);
1841 #endif
1842     }
1843 
1844   /* Need to test METHOD_VEC here in case all methods
1845      (conversions and otherwise) are inherited.  */
1846   if (TYPE_HAS_CONVERSION (t) && method_vec != NULL_TREE)
1847     {
1848       tree first_conversions[last_conversion_type];
1849       tree last_conversions[last_conversion_type];
1850       enum conversion_type conv_index;
1851       tree *tmp;
1852       int i;
1853 
1854       bzero (first_conversions, sizeof (first_conversions));
1855       bzero (last_conversions, sizeof (last_conversions));
1856       for (tmp = &TREE_VEC_ELT (method_vec, 1);
1857 	   tmp != TREE_VEC_END (method_vec); tmp += 1)
1858 	{
1859 	  /* ??? This should compare DECL_NAME (*tmp) == ansi_opname[TYPE_EXPR].  */
1860 	  if (IDENTIFIER_TYPENAME_P (DECL_ASSEMBLER_NAME (*tmp)))
1861 	    {
1862 	      tree fntype = TREE_TYPE (*tmp);
1863 	      tree return_type = TREE_TYPE (fntype);
1864 	      my_friendly_assert (TREE_CODE (fntype) == METHOD_TYPE, 171);
1865 
1866 	      if (typecode_p (return_type, POINTER_TYPE))
1867 		{
1868 		  if (TYPE_READONLY (TREE_TYPE (return_type)))
1869 		    conv_index = constptr_conv;
1870 		  else
1871 		    conv_index = ptr_conv;
1872 		}
1873 	      else if (typecode_p (return_type, INTEGER_TYPE))
1874 		{
1875 		  TYPE_HAS_INT_CONVERSION (t) = 1;
1876 		  conv_index = int_conv;
1877 		}
1878 	      else if (typecode_p (return_type, REAL_TYPE))
1879 		{
1880 		  TYPE_HAS_REAL_CONVERSION (t) = 1;
1881 		  conv_index = real_conv;
1882 		}
1883 	      else
1884 		continue;
1885 
1886 	      if (first_conversions[(int) conv_index] == NULL_TREE)
1887 		first_conversions[(int) conv_index] = *tmp;
1888 	      last_conversions[(int) conv_index] = *tmp;
1889 	    }
1890 	}
1891 
1892       for (i = 0; i < (int) last_conversion_type; i++)
1893 	if (first_conversions[i] != last_conversions[i])
1894 	  CLASSTYPE_CONVERSION (t, i) = error_mark_node;
1895 	else
1896 	  CLASSTYPE_CONVERSION (t, i) = first_conversions[i];
1897     }
1898 
1899   /* If this type has constructors, force its mode to be BLKmode,
1900      and force its TREE_ADDRESSABLE bit to be nonzero.  */
1901   if (TYPE_NEEDS_CONSTRUCTING (t) || TYPE_NEEDS_DESTRUCTOR (t))
1902     {
1903       tree variants = t;
1904 
1905       if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
1906 	DECL_MODE (TYPE_NAME (t)) = BLKmode;
1907       while (variants)
1908 	{
1909 	  TYPE_MODE (variants) = BLKmode;
1910 	  TREE_ADDRESSABLE (variants) = 1;
1911 	  variants = TYPE_NEXT_VARIANT (variants);
1912 	}
1913     }
1914 }
1915 
1916 /* Warn about duplicate methods in fn_fields.  Also compact method
1917    lists so that lookup can be made faster.
1918 
1919    Algorithm: Outer loop builds lists by method name.  Inner loop
1920    checks for redundant method names within a list.
1921 
1922    Data Structure: List of method lists.  The outer list is a
1923    TREE_LIST, whose TREE_PURPOSE field is the field name and the
1924    TREE_VALUE is the TREE_CHAIN of the FUNCTION_DECLs.  Friends are
1925    chained in the same way as member functions, but they live in the
1926    TREE_TYPE field of the outer list.  That allows them to be quickly
1927    deleted, and requires no extra storage.
1928 
1929    If there are any constructors/destructors, they are moved to the
1930    front of the list.  This makes pushclass more efficient.
1931 
1932    We also link each field which has shares a name with its baseclass
1933    to the head of the list of fields for that base class.  This allows
1934    us to reduce search time in places like `build_method_call' to
1935    consider only reasonably likely functions.  */
1936 
1937 static tree
finish_struct_methods(t,fn_fields,nonprivate_method)1938 finish_struct_methods (t, fn_fields, nonprivate_method)
1939      tree t;
1940      tree fn_fields;
1941      int nonprivate_method;
1942 {
1943   tree method_vec;
1944   tree name = constructor_name (t);
1945   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1946 
1947   /* Now prepare to gather fn_fields into vector.  */
1948   struct obstack *ambient_obstack = current_obstack;
1949   current_obstack = &class_obstack;
1950   method_vec = make_node (TREE_VEC);
1951   /* Room has been saved for constructors and destructors.  */
1952   current_obstack = ambient_obstack;
1953   /* Now make this a live vector.  */
1954   obstack_free (&class_obstack, method_vec);
1955   obstack_blank (&class_obstack, sizeof (struct tree_vec));
1956 
1957   while (fn_fields)
1958     {
1959       /* NEXT Pointer, TEST Pointer, and BASE Pointer.  */
1960       tree nextp, *testp;
1961       tree fn_name = DECL_NAME (fn_fields);
1962       if (fn_name == NULL_TREE)
1963 	fn_name = name;
1964 
1965       nextp = TREE_CHAIN (fn_fields);
1966       TREE_CHAIN (fn_fields) = NULL_TREE;
1967       /* Constructors are handled easily in search routines.
1968 	 Besides, we know we won't find any, so do not bother looking.  */
1969       if (fn_name == name && TREE_VEC_ELT (method_vec, 0) == 0)
1970 	TREE_VEC_ELT (method_vec, 0) = fn_fields;
1971       else
1972 	{
1973 	  testp = &TREE_VEC_ELT (method_vec, 0);
1974 	  if (*testp == NULL_TREE)
1975 	    testp++;
1976 	  while (((HOST_WIDE_INT) testp
1977 		  < (HOST_WIDE_INT) obstack_next_free (&class_obstack))
1978 		 && DECL_NAME (*testp) != fn_name)
1979 	    testp++;
1980 	  if ((HOST_WIDE_INT) testp
1981 	      < (HOST_WIDE_INT) obstack_next_free (&class_obstack))
1982 	    {
1983 	      tree x, prev_x;
1984 
1985 	      for (x = *testp; x; x = DECL_CHAIN (x))
1986 		{
1987 		  if (DECL_NAME (fn_fields) == ansi_opname[(int) DELETE_EXPR])
1988 		    {
1989 		      /* ANSI C++ June 5 1992 WP 12.5.5.1 */
1990 		      error_with_decl (fn_fields, "operator delete cannot be overloaded");
1991 		      error_with_decl (x, "previous declaration here");
1992 		    }
1993 		  if (DECL_ASSEMBLER_NAME (fn_fields) == DECL_ASSEMBLER_NAME (x))
1994 		    {
1995 		      /* We complain about multiple destructors on sight,
1996 			 so we do not repeat the warning here.  Friend-friend
1997 			 ambiguities are warned about outside this loop.  */
1998 		      if (! DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fn_fields)))
1999 			error_with_file_and_line (DECL_SOURCE_FILE (fn_fields),
2000 						  DECL_SOURCE_LINE (fn_fields),
2001 						  "ambiguous method `%s' in structure",
2002 						  lang_printable_name (fn_fields));
2003 		      break;
2004 		    }
2005 		  prev_x = x;
2006 		}
2007 	      if (x == 0)
2008 		if (*testp)
2009 		  DECL_CHAIN (prev_x) = fn_fields;
2010 		else
2011 		  *testp = fn_fields;
2012 	    }
2013 	  else
2014 	    {
2015 	      obstack_ptr_grow (&class_obstack, fn_fields);
2016 	      method_vec = (tree)obstack_base (&class_obstack);
2017 	    }
2018 	}
2019       fn_fields = nextp;
2020     }
2021 
2022   TREE_VEC_LENGTH (method_vec)
2023     = (tree *)obstack_next_free (&class_obstack) - (&TREE_VEC_ELT (method_vec, 0));
2024   obstack_finish (&class_obstack);
2025   CLASSTYPE_METHOD_VEC (t) = method_vec;
2026 
2027   if (nonprivate_method == 0
2028       && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
2029       && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
2030     {
2031       tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2032       for (i = 0; i < n_baseclasses; i++)
2033 	if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2034 	    || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2035 	  {
2036 	    nonprivate_method = 1;
2037 	    break;
2038 	  }
2039       if (nonprivate_method == 0)
2040 	warning ("all member functions in class `%s' are private",
2041 		 TYPE_NAME_STRING (t));
2042     }
2043 
2044   /* If there are constructors (and destructors), they are at the
2045      front.  Place destructors at very front.  Also warn if all
2046      constructors and/or destructors are private (in which case this
2047      class is effectively unusable.  */
2048   if (TYPE_HAS_DESTRUCTOR (t))
2049     {
2050       tree dtor, prev;
2051 
2052       for (dtor = TREE_VEC_ELT (method_vec, 0); dtor; prev = dtor, dtor = DECL_CHAIN (dtor))
2053 	{
2054 	  if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (dtor)))
2055 	    {
2056 	      if (TREE_PRIVATE (dtor)
2057 		  && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
2058 		  && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
2059 		warning_with_decl (TYPE_NAME (t), "class `%s' only defines a private destructor and has no friends");
2060 	      break;
2061 	    }
2062 	}
2063       /* Wild parse errors can cause this to happen.  */
2064       if (dtor == NULL_TREE)
2065 	TYPE_HAS_DESTRUCTOR (t) = 0;
2066       else if (dtor != TREE_VEC_ELT (method_vec, 0))
2067 	{
2068 	  DECL_CHAIN (prev) = DECL_CHAIN (dtor);
2069 	  DECL_CHAIN (dtor) = TREE_VEC_ELT (method_vec, 0);
2070 	  TREE_VEC_ELT (method_vec, 0) = dtor;
2071 	}
2072     }
2073 
2074   /* Now for each member function (except for constructors and
2075      destructors), compute where member functions of the same
2076      name reside in base classes.  */
2077   if (n_baseclasses != 0
2078       && TREE_VEC_LENGTH (method_vec) > 1)
2079     {
2080       int len = TREE_VEC_LENGTH (method_vec);
2081       tree baselink_vec = make_tree_vec (len);
2082       int any_links = 0;
2083       tree baselink_binfo = build_tree_list (NULL_TREE, TYPE_BINFO (t));
2084 
2085       for (i = 1; i < len; i++)
2086 	{
2087 	  TREE_VEC_ELT (baselink_vec, i)
2088 	    = get_baselinks (baselink_binfo, t, DECL_NAME (TREE_VEC_ELT (method_vec, i)));
2089 	  if (TREE_VEC_ELT (baselink_vec, i) != 0)
2090 	    any_links = 1;
2091 	}
2092       if (any_links != 0)
2093 	CLASSTYPE_BASELINK_VEC (t) = baselink_vec;
2094       else
2095 	obstack_free (current_obstack, baselink_vec);
2096     }
2097 
2098   /* Now add the methods to the TYPE_METHODS of T, arranged in a chain.  */
2099   {
2100     tree x, last_x = NULL_TREE;
2101     int limit = TREE_VEC_LENGTH (method_vec);
2102 
2103     for (i = 1; i < limit; i++)
2104       {
2105 	for (x = TREE_VEC_ELT (method_vec, i); x; x = DECL_CHAIN (x))
2106 	  {
2107 	    if (last_x != NULL_TREE)
2108 	      TREE_CHAIN (last_x) = x;
2109 	    last_x = x;
2110 	  }
2111       }
2112 
2113     /* Put ctors and dtors at the front of the list.  */
2114     x = TREE_VEC_ELT (method_vec, 0);
2115     if (x)
2116       {
2117 	while (DECL_CHAIN (x))
2118 	  {
2119 	    /* Let's avoid being circular about this.  */
2120 	    if (x == DECL_CHAIN (x))
2121 	      break;
2122 	    TREE_CHAIN (x) = DECL_CHAIN (x);
2123 	    x = DECL_CHAIN (x);
2124 	  }
2125 	if (TREE_VEC_LENGTH (method_vec) > 1)
2126 	  TREE_CHAIN (x) = TREE_VEC_ELT (method_vec, 1);
2127 	else
2128 	  TREE_CHAIN (x) = NULL_TREE;
2129       }
2130   }
2131 
2132 #if 0
2133   TYPE_METHODS (t) = TREE_VEC_ELT (method_vec, 0)
2134     ? TREE_VEC_ELT (method_vec, 0) : TREE_VEC_ELT (method_vec, 1);
2135 #else
2136   TYPE_METHODS (t) = method_vec;
2137 #endif
2138 
2139   return method_vec;
2140 }
2141 
2142 /* Emit error when a duplicate definition of a type is seen.  Patch up. */
2143 
2144 void
duplicate_tag_error(t)2145 duplicate_tag_error (t)
2146      tree t;
2147 {
2148   char *err_name;
2149   tree name = TYPE_NAME (t);
2150   if (TREE_CODE (name) == TYPE_DECL)
2151     name = DECL_NAME (name);
2152   err_name = IDENTIFIER_POINTER (name);
2153   if (TREE_CODE (t) == UNION_TYPE)
2154     error ("redefinition of `union %s'", err_name);
2155   else if (TREE_CODE (t) == RECORD_TYPE)
2156     error ("redefinition of `struct %s'", err_name);
2157   else
2158     error ("redefinition of tag %s", err_name);
2159 
2160   /* Pretend we haven't defined this type.  */
2161 
2162   /* All of the component_decl's were TREE_CHAINed together in the parser.
2163      finish_struct_methods walks these chains and assembles all methods with
2164      the same base name into DECL_CHAINs. Now we don't need the parser chains
2165      anymore, so we unravel them.
2166    */
2167   /*
2168    * This used to be in finish_struct, but it turns out that the
2169    * TREE_CHAIN is used by dbxout_type_methods and perhaps some other things...
2170    */
2171   if (CLASSTYPE_METHOD_VEC(t))
2172     {
2173       tree tv = CLASSTYPE_METHOD_VEC(t);
2174       int i, len  = TREE_VEC_LENGTH (tv);
2175       for (i = 0; i < len; i++)
2176 	{
2177 	  tree unchain = TREE_VEC_ELT (tv, i);
2178 	  while(unchain != NULL_TREE)
2179 	    {
2180 	      TREE_CHAIN (unchain) = NULL_TREE;
2181 	      unchain = DECL_CHAIN(unchain);
2182 	    }
2183 	}
2184     }
2185 
2186   if (TYPE_LANG_SPECIFIC (t))
2187     {
2188       tree as_list = CLASSTYPE_AS_LIST (t);
2189       tree binfo = TYPE_BINFO (t);
2190       tree binfo_as_list = CLASSTYPE_BINFO_AS_LIST (t);
2191       int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2192       int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2193 
2194       bzero (TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2195       BINFO_BASETYPES(binfo) = NULL_TREE;
2196 
2197       CLASSTYPE_AS_LIST (t) = as_list;
2198       TYPE_BINFO (t) = binfo;
2199       CLASSTYPE_BINFO_AS_LIST (t) = binfo_as_list;
2200       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2201       CLASSTYPE_INTERFACE_UNKNOWN (t) = interface_unknown;
2202       CLASSTYPE_VBASE_SIZE (t) = integer_zero_node;
2203       TYPE_REDEFINED (t) = 1;
2204     }
2205   TYPE_SIZE (t) = NULL_TREE;
2206   TYPE_MODE (t) = VOIDmode;
2207   TYPE_FIELDS (t) = NULL_TREE;
2208   TYPE_METHODS (t) = NULL_TREE;
2209   TYPE_VFIELD (t) = NULL_TREE;
2210   TYPE_CONTEXT (t) = NULL_TREE;
2211 }
2212 
2213 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
2214    (or C++ class declaration).
2215 
2216    For C++, we must handle the building of derived classes.
2217    Also, C++ allows static class members.  The way that this is
2218    handled is to keep the field name where it is (as the DECL_NAME
2219    of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
2220    of the field.  layout_record and layout_union will know about this.
2221 
2222    More C++ hair: inline functions have text in their
2223    DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
2224    meaningful tree structure.  After the struct has been laid out, set
2225    things up so that this can happen.
2226 
2227    And still more: virtual functions.  In the case of single inheritance,
2228    when a new virtual function is seen which redefines a virtual function
2229    from the base class, the new virtual function is placed into
2230    the virtual function table at exactly the same address that
2231    it had in the base class.  When this is extended to multiple
2232    inheritance, the same thing happens, except that multiple virtual
2233    function tables must be maintained.  The first virtual function
2234    table is treated in exactly the same way as in the case of single
2235    inheritance.  Additional virtual function tables have different
2236    DELTAs, which tell how to adjust `this' to point to the right thing.
2237 
2238    LIST_OF_FIELDLISTS is just that.  The elements of the list are
2239    TREE_LIST elements, whose TREE_PURPOSE field tells what visibility
2240    the list has, and the TREE_VALUE slot gives the actual fields.
2241 
2242    If flag_all_virtual == 1, then we lay all functions into
2243    the virtual function table, as though they were declared
2244    virtual.  Constructors do not lay down in the virtual function table.
2245 
2246    If flag_all_virtual == 2, then we lay all functions into
2247    the virtual function table, such that virtual functions
2248    occupy a space by themselves, and then all functions
2249    of the class occupy a space by themselves.  This is illustrated
2250    in the following diagram:
2251 
2252    class A; class B : A;
2253 
2254 	Class A's vtbl:			Class B's vtbl:
2255     --------------------------------------------------------------------
2256    | A's virtual functions|		| B's virtual functions		|
2257    |			  |		| (may inherit some from A).	|
2258     --------------------------------------------------------------------
2259    | All of A's functions |		| All of A's functions		|
2260    | (such as a->A::f).	  |		| (such as b->A::f)		|
2261     --------------------------------------------------------------------
2262 					| B's new virtual functions	|
2263 					| (not defined in A.)		|
2264 					 -------------------------------
2265 					| All of B's functions		|
2266 					| (such as b->B::f)		|
2267 					 -------------------------------
2268 
2269    this allows the program to make references to any function, virtual
2270    or otherwise in a type-consistent manner.  */
2271 
2272 tree
finish_struct(t,list_of_fieldlists,warn_anon)2273 finish_struct (t, list_of_fieldlists, warn_anon)
2274      tree t;
2275      tree list_of_fieldlists;
2276      int warn_anon;
2277 {
2278   extern int interface_only, interface_unknown;
2279   int old;
2280   int round_up_size = 1;
2281   /* Set non-zero to debug using default functions.
2282      Not set by program.  */
2283   static int debug_default_functions = 0;
2284 
2285   enum tree_code code = TREE_CODE (t);
2286   register tree x, last_x, method_vec;
2287   int needs_ctor = 0, needs_dtor = 0;
2288   int members_need_dtors, needs_virtual_dtor;
2289   tree name = TYPE_NAME (t), fields, fn_fields, tail;
2290   enum visibility_type visibility;
2291   int all_virtual;
2292   int has_virtual;
2293   int max_has_virtual;
2294   tree pending_virtuals = NULL_TREE;
2295   tree abstract_virtuals = NULL_TREE;
2296   tree vfield;
2297   tree vfields;
2298   int needs_default_ctor;
2299   int cant_have_default_ctor;
2300   int needs_const_ctor;
2301   int cant_have_const_ctor;
2302 
2303   /* The index of the first base class which has virtual
2304      functions.  Only applied to non-virtual baseclasses.  */
2305   int first_vfn_base_index;
2306 
2307   int n_baseclasses;
2308   int any_default_members = 0;
2309   char *err_name;
2310   int const_sans_init = 0;
2311   int ref_sans_init = 0;
2312   int nonprivate_method = 0;
2313   tree t_binfo = TYPE_BINFO (t);
2314 
2315   if (TREE_CODE (name) == TYPE_DECL)
2316     {
2317       extern int lineno;
2318 
2319       DECL_SOURCE_FILE (name) = input_filename;
2320       /* For TYPE_DECL that are not typedefs (those marked with a line number
2321 	 of zero, we don't want to mark them as real typedefs.  If this fails
2322 	 one needs to make sure real typedefs have a previous line number,
2323 	 even if it is wrong, that way the below will fill in the right line
2324 	 number.  (mrs) */
2325       if (DECL_SOURCE_LINE (name))
2326 	DECL_SOURCE_LINE (name) = lineno;
2327       name = DECL_NAME (name);
2328     }
2329   err_name = IDENTIFIER_POINTER (name);
2330 
2331   if (warn_anon && code != UNION_TYPE && ANON_AGGRNAME_P (name))
2332     {
2333       warning ("un-usable class ignored (anonymous classes and unions are useless)");
2334       err_name = "(anon)";
2335     }
2336 
2337 #if 0
2338   /* This is set here, but it's never actually used anywhere.  (bpk) */
2339   leftmost_baseclasses = NULL_TREE;
2340 #endif
2341   if (TYPE_SIZE (t))
2342     {
2343       if (TREE_CODE (t) == UNION_TYPE)
2344 	error ("redefinition of `union %s'", err_name);
2345       else if (TREE_CODE (t) == RECORD_TYPE)
2346 	error ("redefinition of `struct %s'", err_name);
2347       else
2348 	my_friendly_abort (172);
2349       popclass (0);
2350       return t;
2351     }
2352 
2353   GNU_xref_decl (current_function_decl, t);
2354 
2355   /* If this type was previously laid out as a forward reference,
2356      make sure we lay it out again.  */
2357 
2358   TYPE_SIZE (t) = 0;
2359   CLASSTYPE_GOT_SEMICOLON (t) = 0;
2360   CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2361   CLASSTYPE_INTERFACE_UNKNOWN (t) = interface_unknown;
2362 
2363   if (flag_dossier)
2364     build_t_desc (t, 0);
2365 
2366   TYPE_BINFO (t) = NULL_TREE;
2367 
2368   old = suspend_momentary ();
2369 
2370   /* Install struct as DECL_FIELD_CONTEXT of each field decl.
2371      Also process specified field sizes.
2372      Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
2373      The specified size is found in the DECL_INITIAL.
2374      Store 0 there, except for ": 0" fields (so we can find them
2375      and delete them, below).  */
2376 
2377   if (t_binfo && BINFO_BASETYPES (t_binfo))
2378     n_baseclasses = TREE_VEC_LENGTH (BINFO_BASETYPES (t_binfo));
2379   else
2380     n_baseclasses = 0;
2381 
2382   if (n_baseclasses > 0)
2383     {
2384       struct base_info base_info;
2385 
2386       /* If using multiple inheritance, this may cause variants of our
2387 	 basetypes to be used (instead of their canonical forms).  */
2388       fields = layout_basetypes (t, BINFO_BASETYPES (t_binfo));
2389       last_x = tree_last (fields);
2390 
2391       first_vfn_base_index = finish_base_struct (t, &base_info,
2392 						 BINFO_BASETYPES (t_binfo));
2393       has_virtual = base_info.has_virtual;
2394       max_has_virtual = base_info.max_has_virtual;
2395       CLASSTYPE_N_SUPERCLASSES (t) += base_info.n_ancestors;
2396       vfield = base_info.vfield;
2397       vfields = base_info.vfields;
2398       needs_default_ctor = base_info.needs_default_ctor;
2399       cant_have_default_ctor = base_info.cant_have_default_ctor;
2400       needs_const_ctor = base_info.needs_const_ctor;
2401       cant_have_const_ctor = base_info.cant_have_const_ctor;
2402       members_need_dtors = base_info.members_need_dtors;
2403       needs_virtual_dtor = base_info.needs_virtual_dtor;
2404       n_baseclasses = TREE_VEC_LENGTH (BINFO_BASETYPES (t_binfo));
2405     }
2406   else
2407     {
2408       first_vfn_base_index = -1;
2409       has_virtual = 0;
2410       max_has_virtual = has_virtual;
2411       vfield = NULL_TREE;
2412       vfields = NULL_TREE;
2413       fields = NULL_TREE;
2414       last_x = NULL_TREE;
2415       needs_default_ctor = 0;
2416       cant_have_default_ctor = 0;
2417       needs_const_ctor = 0;
2418       cant_have_const_ctor = 0;
2419       members_need_dtors = 0;
2420       needs_virtual_dtor = 0;
2421     }
2422 
2423   if (write_virtuals == 3 && ! CLASSTYPE_INTERFACE_UNKNOWN (t)
2424       && current_lang_name == lang_name_cplusplus)
2425     {
2426       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2427       CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! interface_only;
2428     }
2429 
2430   /* The three of these are approximations which may later be
2431      modified.  Needed at this point to make add_virtual_function
2432      and modify_vtable_entries work.  */
2433   TREE_CHAIN (t_binfo) = TYPE_BINFO (t);
2434   TYPE_BINFO (t) = t_binfo;
2435   CLASSTYPE_VFIELDS (t) = vfields;
2436   CLASSTYPE_VFIELD (t) = vfield;
2437 
2438   fn_fields = NULL_TREE;
2439   tail = NULL_TREE;
2440   if (last_x && list_of_fieldlists)
2441     TREE_CHAIN (last_x) = TREE_VALUE (list_of_fieldlists);
2442 
2443   if (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (t))
2444     all_virtual = 1;
2445   else
2446     all_virtual = 0;
2447 
2448   if (CLASSTYPE_DECLARED_CLASS (t) == 0)
2449     {
2450       nonprivate_method = 1;
2451       if (list_of_fieldlists
2452 	  && TREE_PURPOSE (list_of_fieldlists) == (tree)visibility_default)
2453 	TREE_PURPOSE (list_of_fieldlists) = (tree)visibility_public;
2454     }
2455   else if (list_of_fieldlists
2456 	   && TREE_PURPOSE (list_of_fieldlists) == (tree)visibility_default)
2457     TREE_PURPOSE (list_of_fieldlists) = (tree)visibility_private;
2458 
2459   while (list_of_fieldlists)
2460     {
2461       visibility = (enum visibility_type)TREE_PURPOSE (list_of_fieldlists);
2462 
2463       for (x = TREE_VALUE (list_of_fieldlists); x; x = TREE_CHAIN (x))
2464 	{
2465 	  TREE_PRIVATE (x) = visibility == visibility_private;
2466 	  TREE_PROTECTED (x) = visibility == visibility_protected;
2467 	  GNU_xref_member (current_class_name, x);
2468 
2469           if (TREE_CODE (x) == TYPE_DECL
2470               && TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE)
2471             {
2472 #if 0
2473 	      /* @@ Um.  This doesn't seem to be handled properly, at
2474 		 least in my PT test cases.  Not sure if it's really
2475 		 supposed to work for non-PT cases.  Let's find out.  */
2476               static tree t, d;
2477               d = DECL_NAME (x);
2478               t = TYPE_IDENTIFIER (TREE_TYPE (x));
2479 	      if (d == t) continue;
2480 	      if (IDENTIFIER_TEMPLATE (t))
2481 		{
2482 		  t = DECL_NAME (TREE_PURPOSE (IDENTIFIER_TEMPLATE (t)));
2483 		  my_friendly_assert (t == d, 173);
2484 		  continue;
2485 		}
2486 	      else if (IDENTIFIER_CLASS_VALUE (t))
2487 		my_friendly_assert (TREE_TYPE (DECL_NAME (d))
2488 				    == TREE_TYPE (DECL_NAME (TREE_TYPE (t))),
2489 				    174);
2490 	      else
2491 		abort ();
2492 #endif
2493 	      continue;
2494 	    }
2495 
2496 
2497 	  if (TREE_CODE (x) == FUNCTION_DECL)
2498 	    {
2499 	      /* Clear out this flag.
2500 
2501 	         @@ Doug may figure out how to break
2502 		 @@ this with nested classes and friends.  */
2503 	      DECL_IN_AGGR_P (x) = 0;
2504 
2505 	      nonprivate_method |= ! TREE_PRIVATE (x);
2506 
2507 	      /* If this was an evil function, don't keep it in class.  */
2508 	      if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
2509 		continue;
2510 
2511 	      if (last_x) TREE_CHAIN (last_x) = TREE_CHAIN (x);
2512 	      if (! fn_fields) fn_fields = x;
2513 	      else TREE_CHAIN (tail) = x;
2514 	      tail = x;
2515 
2516 #if 0
2517 	      /* ??? What if we have duplicate declarations
2518 		 in T's definition?  */
2519 	      if (DECL_CLASS_CONTEXT (x))
2520 		continue;
2521 #endif
2522 	      DECL_CLASS_CONTEXT (x) = t;
2523 
2524 	      DECL_FIELD_SIZE (x) = 0;
2525 
2526 	      /* The name of the field is the original field name
2527 		 Save this in auxiliary field for later overloading.  */
2528 	      if (DECL_VINDEX (x)
2529 		  || (all_virtual == 1 && ! DECL_CONSTRUCTOR_P (x)))
2530 		{
2531 		  pending_virtuals = add_virtual_function (pending_virtuals,
2532 							   &has_virtual, x, t);
2533 		  if (DECL_ABSTRACT_VIRTUAL_P (x))
2534 		    abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
2535 		}
2536 	      continue;
2537 	    }
2538 
2539 	  /* Handle visibility declarations.  */
2540 	  if (DECL_NAME (x) && TREE_CODE (DECL_NAME (x)) == SCOPE_REF)
2541 	    {
2542 	      tree fdecl = TREE_OPERAND (DECL_NAME (x), 1);
2543 
2544 	      if (last_x) TREE_CHAIN (last_x) = TREE_CHAIN (x);
2545 	      /* Make type T see field decl FDECL with
2546 		 the visibility VISIBILITY.  */
2547 	      if (TREE_CODE (fdecl) == TREE_LIST)
2548 		{
2549 		  fdecl = TREE_VALUE (fdecl);
2550 		  while (fdecl)
2551 		    {
2552 		      if (alter_visibility (t, fdecl, visibility) == 0)
2553 			break;
2554 		      fdecl = DECL_CHAIN (fdecl);
2555 		    }
2556 		}
2557 	      else alter_visibility (t, fdecl, visibility);
2558 	      CLASSTYPE_ALTERS_VISIBILITIES_P (t) = 1;
2559 	      continue;
2560 	    }
2561 
2562 	  /* If this is of reference type, check if it needs an init.  */
2563 	  if (TREE_CODE (x) != TYPE_DECL
2564 	      && TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE
2565 	      && DECL_INITIAL (x) == 0)
2566 	    ref_sans_init = 1;
2567 
2568 	  /* ``A local class cannot have static data members.'' ARM 9.4 */
2569 	  if (current_function_decl && TREE_STATIC (x))
2570 	    error_with_decl (x, "field `%s' in local class cannot be static");
2571 
2572 	  /* When this goes into scope, it will be a non-local reference.  */
2573 	  DECL_NONLOCAL (x) = 1;
2574 
2575 	  /* Perform error checking that did not get done in grokdeclarator.  */
2576 	  if (TREE_CODE (x) == FIELD_DECL || TREE_CODE (x) == VAR_DECL)
2577 	    {
2578 	      if (TREE_CODE (TREE_TYPE (x)) == FUNCTION_TYPE)
2579 		{
2580 		  error_with_decl (x, "field `%s' invalidly declared function type");
2581 		  TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
2582 		}
2583 	      else if (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE)
2584 		{
2585 		  error_with_decl (x, "field `%s' invalidly declared method type");
2586 		  TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
2587 		}
2588 	      else if (TREE_CODE (TREE_TYPE (x)) == OFFSET_TYPE)
2589 		{
2590 		  error_with_decl (x, "field `%s' invalidly declared offset type");
2591 		  TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
2592 		}
2593 	    }
2594 
2595 	  if (TREE_CODE (x) == FIELD_DECL)
2596 	    {
2597 	      /* If the field has a bogus type, don't bother with it.  */
2598 	      if (TREE_TYPE (x) != error_mark_node)
2599 		{
2600 		  /* Never let anything with uninheritable virtuals
2601 		     make it through without complaint.  */
2602 		  if (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
2603 		      && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (x)))
2604 		    abstract_virtuals_error (x, TREE_TYPE (x));
2605 
2606 		  if (TYPE_LANG_SPECIFIC (TREE_TYPE (x)))
2607 		    {
2608 		      if (TYPE_HAS_DEFAULT_CONSTRUCTOR (TREE_TYPE (x)))
2609 			needs_default_ctor = 1;
2610 		      if (TYPE_GETS_CONST_INIT_REF (TREE_TYPE (x)))
2611 			needs_const_ctor = 1;
2612 		      else if (TYPE_GETS_INIT_REF (TREE_TYPE (x)))
2613 			cant_have_const_ctor = 1;
2614 		    }
2615 		  else if (DECL_INITIAL (x) == NULL_TREE
2616 			   && (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (x))
2617 			       || TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE))
2618 		    cant_have_default_ctor = 1;
2619 		}
2620 
2621 	      /* If any field is const, the structure type is pseudo-const.  */
2622 	      if (TREE_READONLY (x))
2623 		{
2624 		  C_TYPE_FIELDS_READONLY (t) = 1;
2625 		  if (DECL_INITIAL (x) == 0)
2626 		    const_sans_init = 1;
2627 		}
2628 	      else
2629 		{
2630 		  /* A field that is pseudo-const makes the structure likewise.  */
2631 		  tree t1 = TREE_TYPE (x);
2632 		  while (TREE_CODE (t1) == ARRAY_TYPE)
2633 		    t1 = TREE_TYPE (t1);
2634 		  if (IS_AGGR_TYPE (t1))
2635 		    {
2636 		      if (C_TYPE_FIELDS_READONLY (t1))
2637 			C_TYPE_FIELDS_READONLY (t) = 1;
2638 		      if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1))
2639 			const_sans_init = 1;
2640 		    }
2641 		}
2642 	    }
2643 	  else if (TREE_CODE (x) == VAR_DECL && TREE_CODE (t) == UNION_TYPE)
2644 	    /* Unions cannot have static members.  */
2645 	    error_with_decl (x, "field `%s' declared static in union");
2646 
2647 	  if (! fields) fields = x;
2648 	  DECL_FIELD_CONTEXT (x) = t;
2649 	  /* We could be making an extern "C" function a friend. */
2650 	  if (DECL_LANG_SPECIFIC (x))
2651 	    DECL_CLASS_CONTEXT (x) = t;
2652 	  DECL_FIELD_SIZE (x) = 0;
2653 
2654 	  /* We set DECL_BIT_FIELD tentatively in grokbitfield.
2655 	     If the type and width are valid, we'll keep it set.
2656 	     Otherwise, the flag is cleared.  */
2657 	  if (DECL_BIT_FIELD (x))
2658 	    {
2659 	      DECL_BIT_FIELD (x) = 0;
2660 	      /* Invalid bit-field size done by grokfield.  */
2661 	      /* Detect invalid bit-field type.  */
2662 	      if (DECL_INITIAL (x)
2663 		  && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
2664 		  && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
2665 		{
2666 		  error_with_decl (x, "bit-field `%s' has invalid type");
2667 		  DECL_INITIAL (x) = NULL;
2668 		}
2669 	      if (DECL_INITIAL (x) && pedantic
2670 		  && TREE_TYPE (x) != integer_type_node
2671 		  && TREE_TYPE (x) != unsigned_type_node
2672 		  && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
2673 		warning_with_decl (x, "bit-field `%s' type invalid in ANSI C++");
2674 
2675 	      /* Detect and ignore out of range field width.  */
2676 	      if (DECL_INITIAL (x))
2677 		{
2678 		  register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
2679 
2680 		  if (width < 0)
2681 		    {
2682 		      DECL_INITIAL (x) = NULL;
2683 		      warning_with_decl (x, "negative width in bit-field `%s'");
2684 		    }
2685 		  else if (width == 0 && DECL_NAME (x) != 0)
2686 		    {
2687 		      error_with_decl (x, "zero width for bit-field `%s'");
2688 		      DECL_INITIAL (x) = NULL;
2689 		    }
2690 		  else if ((unsigned)width > TYPE_PRECISION (TREE_TYPE (x)))
2691 		    {
2692 		      DECL_INITIAL (x) = NULL;
2693 		      warning_with_decl (x, "width of `%s' exceeds its type");
2694 		    }
2695 		}
2696 
2697 	      /* Process valid field width.  */
2698 	      if (DECL_INITIAL (x))
2699 		{
2700 		  register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
2701 
2702 		  if (width == 0)
2703 		    {
2704 #ifdef EMPTY_FIELD_BOUNDARY
2705 		      /* field size 0 => mark following field as "aligned" */
2706 		      if (TREE_CHAIN (x))
2707 			DECL_ALIGN (TREE_CHAIN (x))
2708 			  = MAX (DECL_ALIGN (TREE_CHAIN (x)), EMPTY_FIELD_BOUNDARY);
2709 		      /* field of size 0 at the end => round up the size.  */
2710 		      else
2711 			round_up_size = EMPTY_FIELD_BOUNDARY;
2712 #endif
2713 #ifdef PCC_BITFIELD_TYPE_MATTERS
2714 		      DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
2715 					    TYPE_ALIGN (TREE_TYPE (x)));
2716 #endif
2717 		    }
2718 		  else
2719 		    {
2720 		      DECL_INITIAL (x) = NULL_TREE;
2721 		      DECL_FIELD_SIZE (x) = width;
2722 		      DECL_BIT_FIELD (x) = 1;
2723 		      /* Traditionally a bit field is unsigned
2724 			 even if declared signed.  */
2725 		      if (flag_traditional
2726 			  && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE)
2727 			TREE_TYPE (x) = unsigned_type_node;
2728 		    }
2729 		}
2730 	      else
2731 		/* Non-bit-fields are aligned for their type.  */
2732 		DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x)));
2733 	    }
2734 	  else if (TREE_CODE (x) == FIELD_DECL)
2735 	    {
2736 	      tree type = TREE_TYPE (x);
2737 	      if (TREE_CODE (type) == ARRAY_TYPE)
2738 		type = TREE_TYPE (type);
2739 	      if (code == UNION_TYPE && IS_AGGR_TYPE (type))
2740 		{
2741 		  if (TYPE_NEEDS_CONSTRUCTING (type)
2742 		      || TYPE_NEEDS_DESTRUCTOR (type))
2743 		    error_with_decl (x, "member `%s' with constructor or destructor not allowed in union");
2744 		  TYPE_GETS_ASSIGNMENT (t) |= TYPE_GETS_ASSIGNMENT (type);
2745 		  TYPE_GETS_INIT_REF (t) |= TYPE_GETS_INIT_REF (type);
2746 		}
2747 	      else if (code == RECORD_TYPE)
2748 		{
2749 		  /* Array of record type doesn't matter for this bit.  */
2750 		  TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
2751 		  if (IS_AGGR_TYPE (type))
2752 		    {
2753 		      needs_ctor |= TYPE_NEEDS_CONSTRUCTOR (type);
2754 		      needs_dtor |= TYPE_NEEDS_DESTRUCTOR (type);
2755 		      members_need_dtors |= TYPE_NEEDS_DESTRUCTOR (type);
2756 		      TYPE_GETS_CONST_INIT_REF (t) |= TYPE_GETS_CONST_INIT_REF (type);
2757 		      TYPE_GETS_ASSIGNMENT (t) |= TYPE_GETS_ASSIGNMENT (type);
2758 		      TYPE_GETS_INIT_REF (t) |= TYPE_GETS_INIT_REF (type);
2759 		    }
2760 		}
2761 	      if (DECL_INITIAL (x) != NULL_TREE)
2762 		{
2763 		  /* `build_class_init_list' does not recognize non-FIELD_DECLs.  */
2764 		  if (code == UNION_TYPE && any_default_members != 0)
2765 		    error ("multiple fields in union initialized");
2766 		  any_default_members = 1;
2767 		}
2768 	    }
2769 	  last_x = x;
2770 	}
2771       list_of_fieldlists = TREE_CHAIN (list_of_fieldlists);
2772       /* link the tail while we have it! */
2773       if (last_x)
2774 	{
2775 	  TREE_CHAIN (last_x) = NULL_TREE;
2776 
2777 	  if (list_of_fieldlists
2778 	      && TREE_VALUE (list_of_fieldlists)
2779 	      && TREE_CODE (TREE_VALUE (list_of_fieldlists)) != FUNCTION_DECL)
2780 	    TREE_CHAIN (last_x) = TREE_VALUE (list_of_fieldlists);
2781 	}
2782     }
2783 
2784   if (tail) TREE_CHAIN (tail) = NULL_TREE;
2785 
2786   /* If this type has any constant members which did not come
2787      with their own initialization, mark that fact here.  It is
2788      not an error here, since such types can be saved either by their
2789      constructors, or by fortuitous initialization.  */
2790   CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
2791   CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
2792   CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
2793 
2794   if (members_need_dtors && !TYPE_HAS_DESTRUCTOR (t))
2795     {
2796       /* Here we must cons up a destructor on the fly.  */
2797       tree dtor = cons_up_default_function (t, name,
2798 					    needs_virtual_dtor != 0);
2799 
2800       /* If we couldn't make it work, then pretend we didn't need it.  */
2801       if (dtor == void_type_node)
2802 	TYPE_NEEDS_DESTRUCTOR (t) = 0;
2803       else
2804 	{
2805 	  if (! fn_fields) fn_fields = dtor;
2806 	  else TREE_CHAIN (tail) = dtor;
2807 	  tail = dtor;
2808 
2809 	  if (DECL_VINDEX (dtor) == NULL_TREE
2810 	      && ! CLASSTYPE_DECLARED_EXCEPTION (t)
2811 	      && (needs_virtual_dtor
2812 		  || pending_virtuals != NULL_TREE
2813 		  || pending_hard_virtuals != NULL_TREE))
2814 	    DECL_VINDEX (dtor) = error_mark_node;
2815 	  if (DECL_VINDEX (dtor))
2816 	    pending_virtuals = add_virtual_function (pending_virtuals,
2817 						     &has_virtual, dtor, NULL_TREE);
2818 	  nonprivate_method = 1;
2819 	  TYPE_HAS_DESTRUCTOR (t) = 1;
2820 	}
2821     }
2822 
2823   if (debug_default_functions)
2824     {
2825       if ((TYPE_NEEDS_CONSTRUCTOR (t) || TYPE_HAS_CONSTRUCTOR (t) || needs_ctor)
2826 	  && ! TYPE_HAS_INIT_REF (t))
2827 	{
2828 	  tree default_fn = cons_up_default_function (t, name, 4);
2829 	  TREE_CHAIN (default_fn) = fn_fields;
2830 	  fn_fields = default_fn;
2831 	  TYPE_HAS_INIT_REF (t) = 1;
2832 	  default_fn = cons_up_default_function (t, name, 3);
2833 	  TREE_CHAIN (default_fn) = fn_fields;
2834 	  fn_fields = default_fn;
2835 	  nonprivate_method = 1;
2836 	}
2837 
2838       if (! TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
2839 	  && needs_default_ctor && ! cant_have_default_ctor)
2840 	{
2841 	  tree default_fn = cons_up_default_function (t, name, 2);
2842 	  TREE_CHAIN (default_fn) = fn_fields;
2843 	  fn_fields = default_fn;
2844 	  TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
2845 	  nonprivate_method = 1;
2846 	}
2847     }
2848 
2849   if (fn_fields)
2850     {
2851       method_vec = finish_struct_methods (t, fn_fields, nonprivate_method);
2852 
2853       if (TYPE_HAS_CONSTRUCTOR (t)
2854 	  && ! CLASSTYPE_DECLARED_EXCEPTION (t)
2855 	  && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
2856 	  && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
2857 	{
2858 	  int nonprivate_ctor = 0;
2859 	  tree ctor;
2860 
2861 	  for (ctor = TREE_VEC_ELT (method_vec, 0);
2862 	       ctor;
2863 	       ctor = DECL_CHAIN (ctor))
2864 	    if (! TREE_PRIVATE (ctor))
2865 	      {
2866 		nonprivate_ctor = 1;
2867 		break;
2868 	      }
2869 	  if (nonprivate_ctor == 0)
2870 	    warning ("class `%s' only defines private constructors and has no friends",
2871 		     err_name);
2872 	}
2873     }
2874   else
2875     {
2876       method_vec = 0;
2877 
2878       /* Just in case these got accidentally
2879 	 filled in by syntax errors.  */
2880       TYPE_HAS_CONSTRUCTOR (t) = 0;
2881       TYPE_HAS_DESTRUCTOR (t) = 0;
2882     }
2883 
2884   if (vfield == NULL_TREE && has_virtual)
2885     {
2886       /* We build this decl with ptr_type_node, and
2887 	 change the type when we know what it should be.  */
2888       vfield = build_lang_field_decl (FIELD_DECL, get_vfield_name (t), ptr_type_node);
2889       /* If you change any of the below, take a look at all the
2890 	 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
2891 	 them too. */
2892       DECL_ASSEMBLER_NAME (vfield) = get_identifier (VFIELD_BASE);
2893       CLASSTYPE_VFIELD (t) = vfield;
2894       DECL_VIRTUAL_P (vfield) = 1;
2895       DECL_FIELD_CONTEXT (vfield) = t;
2896       DECL_CLASS_CONTEXT (vfield) = t;
2897       DECL_FCONTEXT (vfield) = t;
2898       DECL_FIELD_SIZE (vfield) = 0;
2899       DECL_ALIGN (vfield) = TYPE_ALIGN (ptr_type_node);
2900       if (CLASSTYPE_DOSSIER (t))
2901 	{
2902 	  /* vfield is always first entry in structure.  */
2903 	  TREE_CHAIN (vfield) = fields;
2904 	  fields = vfield;
2905 	}
2906       else if (last_x)
2907 	{
2908 	  my_friendly_assert (TREE_CHAIN (last_x) == 0, 175);
2909 	  TREE_CHAIN (last_x) = vfield;
2910 	  last_x = vfield;
2911 	}
2912       else fields = vfield;
2913       vfields = chainon (vfields, CLASSTYPE_AS_LIST (t));
2914     }
2915 
2916   /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
2917      And they have already done their work.
2918 
2919      C++: maybe we will support default field initialization some day...  */
2920 
2921   /* Delete all zero-width bit-fields from the front of the fieldlist */
2922   while (fields && DECL_BIT_FIELD (fields)
2923 	 && DECL_INITIAL (fields))
2924     fields = TREE_CHAIN (fields);
2925   /* Delete all such fields from the rest of the fields.  */
2926   for (x = fields; x;)
2927     {
2928       if (TREE_CHAIN (x) && DECL_BIT_FIELD (TREE_CHAIN (x))
2929 	  && DECL_INITIAL (TREE_CHAIN (x)))
2930 	TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
2931       else x = TREE_CHAIN (x);
2932     }
2933   /* Delete all duplicate fields from the fields */
2934   delete_duplicate_fields (fields);
2935 
2936   /* Now we have the final fieldlist for the data fields.  Record it,
2937      then lay out the structure or union (including the fields).  */
2938 
2939   TYPE_FIELDS (t) = fields;
2940 
2941   /* If there's a :0 field at the end, round the size to the
2942      EMPTY_FIELD_BOUNDARY.  */
2943   TYPE_ALIGN (t) = round_up_size;
2944 
2945   /* Pass layout information about base classes to layout_type, if any.  */
2946 
2947   if (n_baseclasses)
2948     {
2949       tree pseudo_basetype = TREE_TYPE (base_layout_decl);
2950 
2951       TREE_CHAIN (base_layout_decl) = TYPE_FIELDS (t);
2952       TYPE_FIELDS (t) = base_layout_decl;
2953 
2954       TYPE_SIZE (pseudo_basetype) = CLASSTYPE_SIZE (t);
2955       TYPE_MODE (pseudo_basetype) = TYPE_MODE (t);
2956       TYPE_ALIGN (pseudo_basetype) = CLASSTYPE_ALIGN (t);
2957       DECL_ALIGN (base_layout_decl) = TYPE_ALIGN (pseudo_basetype);
2958     }
2959 
2960   layout_type (t);
2961 
2962   if (n_baseclasses)
2963     TYPE_FIELDS (t) = TREE_CHAIN (TYPE_FIELDS (t));
2964 
2965   /* C++: do not let empty structures exist.  */
2966   if (integer_zerop (TYPE_SIZE (t)))
2967     TYPE_SIZE (t) = TYPE_SIZE (char_type_node);
2968 
2969   /* Set the TYPE_DECL for this type to contain the right
2970      value for DECL_OFFSET, so that we can use it as part
2971      of a COMPONENT_REF for multiple inheritance.  */
2972 
2973   if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
2974     layout_decl (TYPE_NAME (t), 0);
2975 
2976   /* Now fix up any virtual base class types that we
2977      left lying around.  We must get these done
2978      before we try to lay out the virtual function table.  */
2979   doing_hard_virtuals = 1;
2980   pending_hard_virtuals = nreverse (pending_hard_virtuals);
2981 
2982   if (TYPE_USES_VIRTUAL_BASECLASSES (t))
2983     {
2984       tree vbases;
2985 
2986       max_has_virtual = layout_vbasetypes (t, max_has_virtual);
2987       vbases = CLASSTYPE_VBASECLASSES (t);
2988       CLASSTYPE_N_VBASECLASSES (t) = list_length (vbases);
2989 
2990       /* This loop makes all the entries in the virtual function tables
2991 	 of interest contain the "latest" version of the functions
2992 	 we have defined.  */
2993 
2994       while (vbases)
2995 	{
2996 	  tree virtuals = BINFO_VIRTUALS (vbases);
2997 
2998 	  if (virtuals)
2999 	    {
3000 	      /* Get past the `null' vtable entry...  */
3001 	      virtuals = TREE_CHAIN (virtuals);
3002 	      /* and the `dossier' vtable entry if we're doing dossiers.  */
3003 	      if (flag_dossier)
3004 		virtuals = TREE_CHAIN (virtuals);
3005 	    }
3006 
3007 	  while (virtuals != NULL_TREE)
3008 	    {
3009 	      tree pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals));
3010 	      tree base_fndecl = TREE_OPERAND (pfn, 0);
3011 	      tree decl = get_first_matching_virtual (TYPE_BINFO (t), base_fndecl,
3012 						      DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl)));
3013 	      tree context = DECL_CLASS_CONTEXT (decl);
3014 	      if (decl != base_fndecl && context != t)
3015 		{
3016 		  tree base_context = DECL_CLASS_CONTEXT (base_fndecl);
3017 		  tree binfo = NULL_TREE, these_virtuals;
3018 #if 0
3019 		  unsigned HOST_WIDE_INT i
3020 		    = (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
3021 		       & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1));
3022 #endif
3023 
3024 		  if (TYPE_USES_VIRTUAL_BASECLASSES (context))
3025 		    binfo = virtual_member (base_context,
3026 					    CLASSTYPE_VBASECLASSES (context));
3027 		  if (binfo == NULL_TREE)
3028 		    binfo = binfo_value (base_context, context);
3029 		  if (binfo != NULL_TREE)
3030 		    {
3031 #if 1
3032 		      pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (get_vtable_entry (BINFO_VIRTUALS (binfo), base_fndecl)));
3033 #else
3034 		      these_virtuals = BINFO_VIRTUALS (binfo);
3035 
3036 		      while (i-- > 0)
3037 			these_virtuals = TREE_CHAIN (these_virtuals);
3038 		      pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (these_virtuals));
3039 #endif
3040 		      modify_vtable_entries (t, decl, base_fndecl, pfn);
3041 		    }
3042 		}
3043 	      virtuals = TREE_CHAIN (virtuals);
3044 	    }
3045 	  /* Update dossier info with offsets for virtual baseclasses.  */
3046 	  if (flag_dossier && ! BINFO_NEW_VTABLE_MARKED (vbases))
3047 	    prepare_fresh_vtable (vbases, vbases, t);
3048 
3049 	  vbases = TREE_CHAIN (vbases);
3050 	}
3051     }
3052 
3053   while (pending_hard_virtuals)
3054     {
3055       /* Need an entry in some other virtual function table.  */
3056       if (TREE_TYPE (pending_hard_virtuals))
3057 	{
3058 	  /* This is how we modify entries when a vfn's index changes
3059 	     between derived and base type.  */
3060 	  modify_vtable_entries (t, TREE_PURPOSE (pending_hard_virtuals),
3061 				 TREE_TYPE (pending_hard_virtuals),
3062 				 TREE_VALUE (pending_hard_virtuals));
3063 	}
3064       else
3065 	{
3066 	  /* This is how we modify entries when a vfn comes from
3067 	     a virtual baseclass.  */
3068 	  tree base_fndecls = DECL_VINDEX (TREE_PURPOSE (pending_hard_virtuals));
3069 	  my_friendly_assert (base_fndecls != error_mark_node, 176);
3070 	  while (base_fndecls)
3071 	    {
3072 	      modify_vtable_entries (t, TREE_PURPOSE (pending_hard_virtuals),
3073 				     TREE_VALUE (base_fndecls),
3074 				     TREE_VALUE (pending_hard_virtuals));
3075 	      base_fndecls = TREE_CHAIN (base_fndecls);
3076 	    }
3077 	}
3078       pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
3079     }
3080   doing_hard_virtuals = 0;
3081 
3082   /* Under our model of GC, every C++ class gets its own virtual
3083      function table, at least virtually.  */
3084   if (pending_virtuals || CLASSTYPE_DOSSIER (t))
3085     {
3086       pending_virtuals = nreverse (pending_virtuals);
3087       /* We must enter these virtuals into the table.  */
3088       if (first_vfn_base_index < 0)
3089 	{
3090 	  if (flag_dossier)
3091 	    pending_virtuals = tree_cons (NULL_TREE,
3092 					  build_vtable_entry (integer_zero_node,
3093 							      build_t_desc (t, 0)),
3094 					  pending_virtuals);
3095 	  pending_virtuals = tree_cons (NULL_TREE, the_null_vtable_entry,
3096 					pending_virtuals);
3097 	  build_vtable (NULL_TREE, t);
3098 	}
3099       else
3100 	{
3101 	  /* Here we know enough to change the type of our virtual
3102 	     function table, but we will wait until later this function.  */
3103 
3104 	  if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
3105 	    build_vtable (binfo_value (TYPE_BINFO_BASETYPE (t, first_vfn_base_index), t), t);
3106 
3107 	  /* Update the dossier pointer for this class.  */
3108 	  if (flag_dossier)
3109 	    TREE_VALUE (TREE_CHAIN (TYPE_BINFO_VIRTUALS (t)))
3110 	      = build_vtable_entry (integer_zero_node, build_t_desc (t, 0));
3111 	}
3112 
3113       /* If this type has basetypes with constructors, then those
3114 	 constructors might clobber the virtual function table.  But
3115 	 they don't if the derived class shares the exact vtable of the base
3116 	 class.  */
3117 
3118       CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
3119     }
3120   else if (first_vfn_base_index >= 0)
3121     {
3122       tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
3123 
3124       /* This class contributes nothing new to the virtual function
3125 	 table.  However, it may have declared functions which
3126 	 went into the virtual function table "inherited" from the
3127 	 base class.  If so, we grab a copy of those updated functions,
3128 	 and pretend they are ours.  */
3129 
3130       /* See if we should steal the virtual info from base class.  */
3131       if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
3132 	TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
3133       if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
3134 	TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
3135       if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
3136 	CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
3137     }
3138 
3139   if (has_virtual > max_has_virtual)
3140     max_has_virtual = has_virtual;
3141   if (max_has_virtual || first_vfn_base_index >= 0)
3142     {
3143 #ifdef VTABLE_USES_MASK
3144       if (max_has_virtual >= VINDEX_MAX)
3145 	{
3146 	  error ("too many virtual functions for class `%s' (VINDEX_MAX < %d)",
3147 		 err_name, has_virtual);
3148 	}
3149 #endif
3150       TYPE_VIRTUAL_P (t) = 1;
3151       CLASSTYPE_VSIZE (t) = has_virtual;
3152       if (first_vfn_base_index >= 0)
3153 	{
3154 	  if (pending_virtuals)
3155 	    TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t),
3156 						pending_virtuals);
3157 	}
3158       else if (has_virtual)
3159 	{
3160 	  TYPE_BINFO_VIRTUALS (t) = pending_virtuals;
3161 	  if (write_virtuals >= 0)
3162 	    DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1;
3163 	}
3164     }
3165 
3166   /* Now lay out the virtual function table.  */
3167   if (has_virtual)
3168     {
3169       tree atype, itype;
3170 
3171       if (TREE_TYPE (vfield) == ptr_type_node)
3172 	{
3173 	  /* We must create a pointer to this table because
3174 	     the one inherited from base class does not exist.
3175 	     We will fill in the type when we know what it
3176 	     should really be.  Use `size_int' so values are memoized
3177 	     in common cases.  */
3178 	  itype = build_index_type (size_int (has_virtual));
3179 	  atype = build_array_type (vtable_entry_type, itype);
3180 	  layout_type (atype);
3181 	  TREE_TYPE (vfield) = build_pointer_type (atype);
3182 	}
3183       else
3184 	{
3185 	  atype = TREE_TYPE (TREE_TYPE (vfield));
3186 
3187 	  if (has_virtual != TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))))
3188 	    {
3189 	      /* We must extend (or create) the boundaries on this array,
3190 		 because we picked up virtual functions from multiple
3191 		 base classes.  */
3192 	      itype = build_index_type (size_int (has_virtual));
3193 	      atype = build_array_type (vtable_entry_type, itype);
3194 	      layout_type (atype);
3195 	      vfield = copy_node (vfield);
3196 	      TREE_TYPE (vfield) = build_pointer_type (atype);
3197 	    }
3198 	}
3199 
3200       CLASSTYPE_VFIELD (t) = vfield;
3201       if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
3202 	{
3203 	  TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
3204 	  layout_decl (TYPE_BINFO_VTABLE (t), 0);
3205 	  DECL_ALIGN (TYPE_BINFO_VTABLE (t))
3206 	    = MAX (TYPE_ALIGN (double_type_node),
3207 		   DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
3208 	}
3209     }
3210   else if (first_vfn_base_index >= 0)
3211     CLASSTYPE_VFIELD (t) = vfield;
3212   CLASSTYPE_VFIELDS (t) = vfields;
3213 
3214   /* Set all appropriate CLASSTYPE_... flags for this type
3215      and its variants.  */
3216   TYPE_NEEDS_CONSTRUCTOR (t) |= needs_ctor || TYPE_HAS_CONSTRUCTOR (t);
3217   TYPE_NEEDS_CONSTRUCTING (t)
3218     |= ((TYPE_NEEDS_CONSTRUCTOR (t)|TYPE_USES_VIRTUAL_BASECLASSES (t))
3219 	|| has_virtual || any_default_members
3220 	|| first_vfn_base_index >= 0);
3221   TYPE_NEEDS_DESTRUCTOR (t) |= needs_dtor || TYPE_HAS_DESTRUCTOR (t);
3222   finish_struct_bits (t, max_has_virtual);
3223 
3224   /* Promote each bit-field's type to int if it is narrower than that.
3225      There's more: complete the rtl for any static member objects which
3226      is of the same type we're working on.
3227   */
3228   for (x = fields; x; x = TREE_CHAIN (x))
3229     {
3230       if (DECL_BIT_FIELD (x)
3231 	  && C_PROMOTING_INTEGER_TYPE_P (TREE_TYPE (x)))
3232 	TREE_TYPE (x) = TREE_UNSIGNED (TREE_TYPE (x))
3233 	  ? unsigned_type_node : integer_type_node;
3234       if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
3235 	  && TREE_TYPE (x) == t)
3236 	{
3237 	  DECL_MODE (x) = TYPE_MODE (t);
3238 	  make_decl_rtl (x, NULL, 0);
3239 	}
3240     }
3241 
3242   /* Now add the tags, if any, to the list of TYPE_DECLs
3243      defined for this type.  */
3244   if (CLASSTYPE_TAGS (t))
3245     {
3246       x = CLASSTYPE_TAGS (t);
3247       last_x = tree_last (TYPE_FIELDS (t));
3248       while (x)
3249 	{
3250 	  tree tag = build_lang_decl (TYPE_DECL, TREE_PURPOSE (x), TREE_VALUE (x));
3251 	  DECL_CONTEXT (tag) = t;
3252 	  DECL_CLASS_CONTEXT (tag) = t;
3253 	  x = TREE_CHAIN (x);
3254 	  last_x = chainon (last_x, tag);
3255 	}
3256       if (TYPE_FIELDS (t) == 0)
3257 	TYPE_FIELDS (t) = last_x;
3258       CLASSTYPE_LOCAL_TYPEDECLS (t) = 1;
3259     }
3260 
3261   if (TYPE_HAS_CONSTRUCTOR (t))
3262     {
3263       tree vfields = CLASSTYPE_VFIELDS (t);
3264 
3265       while (vfields)
3266 	{
3267 	  /* Mark the fact that constructor for T
3268 	     could affect anybody inheriting from T
3269 	     who wants to initialize vtables for VFIELDS's type.  */
3270 	  if (VF_DERIVED_VALUE (vfields))
3271 	    TREE_ADDRESSABLE (vfields) = 1;
3272 	  vfields = TREE_CHAIN (vfields);
3273 	}
3274       if (any_default_members != 0)
3275 	build_class_init_list (t);
3276     }
3277   else if (TYPE_NEEDS_CONSTRUCTING (t))
3278     build_class_init_list (t);
3279 
3280   if (current_lang_name == lang_name_cplusplus)
3281     {
3282       if (! CLASSTYPE_DECLARED_EXCEPTION (t))
3283 	embrace_waiting_friends (t);
3284 
3285       /* Write out inline function definitions.  */
3286       do_inline_function_hair (t, CLASSTYPE_INLINE_FRIENDS (t));
3287       CLASSTYPE_INLINE_FRIENDS (t) = 0;
3288     }
3289 
3290   if (CLASSTYPE_VSIZE (t) != 0)
3291     {
3292 #if 0
3293       if (!TYPE_USES_COMPLEX_INHERITANCE (t))
3294 	TYPE_NONCOPIED_PARTS (t) = build_tree_list (default_conversion (TYPE_BINFO_VTABLE (t)), vfield);
3295 #endif
3296 
3297       if ((flag_this_is_variable & 1) == 0)
3298 	{
3299 	  tree vtbl_ptr = build_decl (VAR_DECL, get_identifier (VPTR_NAME),
3300 				      TREE_TYPE (vfield));
3301 	  DECL_REGISTER (vtbl_ptr) = 1;
3302 	  CLASSTYPE_VTBL_PTR (t) = vtbl_ptr;
3303 	}
3304       if (DECL_FIELD_CONTEXT (vfield) != t)
3305 	{
3306 	  tree binfo = binfo_value (DECL_FIELD_CONTEXT (vfield), t);
3307 	  tree offset = BINFO_OFFSET (binfo);
3308 
3309 	  vfield = copy_node (vfield);
3310 	  copy_lang_decl (vfield);
3311 
3312 	  if (! integer_zerop (offset))
3313 	    offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
3314 	  DECL_FIELD_CONTEXT (vfield) = t;
3315 	  DECL_CLASS_CONTEXT (vfield) = t;
3316 	  DECL_FIELD_BITPOS (vfield)
3317 	    = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
3318 	  CLASSTYPE_VFIELD (t) = vfield;
3319 	}
3320       if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
3321 	  && DECL_VINDEX (TREE_VEC_ELT (method_vec, 0)) == NULL_TREE)
3322 	warning ("class `%s' has virtual functions but non-virtual destructor",
3323 		 err_name);
3324     }
3325 
3326   /* Make the rtl for any new vtables we have created, and unmark
3327      the base types we marked.  */
3328   unmark_finished_struct (t);
3329   TYPE_BEING_DEFINED (t) = 0;
3330 
3331   if (flag_dossier && CLASSTYPE_VTABLE_NEEDS_WRITING (t))
3332     {
3333       tree variants;
3334       tree tdecl;
3335 
3336       /* Now instantiate its type descriptors.  */
3337       tdecl = TREE_OPERAND (build_t_desc (t, 1), 0);
3338       variants = TYPE_POINTER_TO (t);
3339       build_type_variant (variants, 1, 0);
3340       while (variants)
3341 	{
3342 	  build_t_desc (variants, 1);
3343 	  variants = TYPE_NEXT_VARIANT (variants);
3344 	}
3345       variants = build_reference_type (t);
3346       build_type_variant (variants, 1, 0);
3347       while (variants)
3348 	{
3349 	  build_t_desc (variants, 1);
3350 	  variants = TYPE_NEXT_VARIANT (variants);
3351 	}
3352 #if 0
3353       DECL_VPARENT (tdecl) = t;
3354 #endif
3355       DECL_CONTEXT (tdecl) = t;
3356     }
3357   /* Still need to instantiate this C struct's type descriptor.  */
3358   else if (flag_dossier && ! CLASSTYPE_DOSSIER (t))
3359     build_t_desc (t, 1);
3360 
3361   if (TYPE_NAME (t) && TYPE_IDENTIFIER (t))
3362     undo_template_name_overload (TYPE_IDENTIFIER (t), 1);
3363   if (current_class_type)
3364     popclass (0);
3365   else
3366     error ("trying to finish struct, but kicked out due to previous parse errors.");
3367 
3368   hack_incomplete_structures (t);
3369 
3370   resume_momentary (old);
3371 
3372   if (flag_cadillac)
3373     cadillac_finish_struct (t);
3374 
3375 #if 0
3376   /* This has to be done after we have sorted out what to do with
3377      the enclosing type.  */
3378   /* Be smarter about nested classes here.  If a type is nested,
3379      only output it if we would output the enclosing type.  */
3380   if (DECL_CONTEXT (TYPE_NAME (t))
3381       && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (TYPE_NAME (t)))) == 't')
3382     DECL_IGNORED_P (TYPE_NAME (t)) = TREE_ASM_WRITTEN (TYPE_NAME (t));
3383 #endif
3384 
3385   /* If the type has methods, we want to think about cutting down
3386      the amount of symbol table stuff we output.  The value stored in
3387      the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
3388      For example, if a member function is seen and we decide to
3389      write out that member function, then we can change the value
3390      of the DECL_IGNORED_P slot, and the type will be output when
3391      that member function's debug info is written out.  */
3392   if (CLASSTYPE_METHOD_VEC (t))
3393     {
3394       extern tree pending_vtables;
3395 
3396       /* Don't output full info about any type
3397 	 which does not have its implementation defined here.  */
3398       if (TYPE_VIRTUAL_P (t) && write_virtuals == 2)
3399 	DECL_IGNORED_P (TYPE_NAME (t))
3400 	  = (value_member (TYPE_IDENTIFIER (t), pending_vtables) == 0);
3401       else if (CLASSTYPE_INTERFACE_ONLY (t))
3402 	DECL_IGNORED_P (TYPE_NAME (t)) = 1;
3403       else if (CLASSTYPE_INTERFACE_UNKNOWN (t))
3404 	/* Only a first approximation!  */
3405 	DECL_IGNORED_P (TYPE_NAME (t)) = 1;
3406     }
3407   else if (CLASSTYPE_INTERFACE_ONLY (t))
3408     DECL_IGNORED_P (TYPE_NAME (t)) = 1;
3409 
3410   /* Finish debugging output for this type.  */
3411   rest_of_type_compilation (t, global_bindings_p ());
3412 
3413   return t;
3414 }
3415 
3416 /* Return non-zero if the effective type of INSTANCE is static.
3417    Used to determine whether the virtual function table is needed
3418    or not.
3419 
3420    *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
3421    of our knowledge of its type.  */
3422 int
resolves_to_fixed_type_p(instance,nonnull)3423 resolves_to_fixed_type_p (instance, nonnull)
3424      tree instance;
3425      int *nonnull;
3426 {
3427   switch (TREE_CODE (instance))
3428     {
3429     case INDIRECT_REF:
3430       /* Check that we are not going through a cast of some sort.  */
3431       if (TREE_TYPE (instance)
3432 	  == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
3433 	instance = TREE_OPERAND (instance, 0);
3434       /* fall through...  */
3435     case CALL_EXPR:
3436       /* This is a call to a constructor, hence it's never zero.  */
3437       if (TREE_HAS_CONSTRUCTOR (instance))
3438 	{
3439 	  if (nonnull)
3440 	    *nonnull = 1;
3441 	  return 1;
3442 	}
3443       return 0;
3444 
3445     case SAVE_EXPR:
3446       /* This is a call to a constructor, hence it's never zero.  */
3447       if (TREE_HAS_CONSTRUCTOR (instance))
3448 	{
3449 	  if (nonnull)
3450 	    *nonnull = 1;
3451 	  return 1;
3452 	}
3453       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
3454 
3455     case RTL_EXPR:
3456       /* This is a call to `new', hence it's never zero.  */
3457       if (TREE_CALLS_NEW (instance))
3458 	{
3459 	  if (nonnull)
3460 	    *nonnull = 1;
3461 	  return 1;
3462 	}
3463       return 0;
3464 
3465     case PLUS_EXPR:
3466     case MINUS_EXPR:
3467       if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
3468 	/* Propagate nonnull.  */
3469 	resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
3470       if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
3471 	return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
3472       return 0;
3473 
3474     case NOP_EXPR:
3475     case CONVERT_EXPR:
3476       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
3477 
3478     case ADDR_EXPR:
3479       if (nonnull)
3480 	*nonnull = 1;
3481       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
3482 
3483     case COMPONENT_REF:
3484       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 1), nonnull);
3485 
3486     case WITH_CLEANUP_EXPR:
3487       if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
3488 	return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
3489       /* fall through... */
3490     case VAR_DECL:
3491     case FIELD_DECL:
3492       if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
3493 	  && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
3494 	{
3495 	  if (nonnull)
3496 	    *nonnull = 1;
3497 	  return 1;
3498 	}
3499       /* fall through... */
3500     case TARGET_EXPR:
3501     case PARM_DECL:
3502       if (IS_AGGR_TYPE (TREE_TYPE (instance)))
3503 	{
3504 	  if (nonnull)
3505 	    *nonnull = 1;
3506 	  return 1;
3507 	}
3508       else if (nonnull)
3509 	{
3510 	  if (instance == current_class_decl
3511 	      && flag_this_is_variable <= 0)
3512 	    {
3513 	      /* Some people still use `this = 0' inside destructors.  */
3514 	      *nonnull = ! DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (current_function_decl));
3515 	      /* In a constructor, we know our type.  */
3516 	      if (flag_this_is_variable < 0)
3517 		return 1;
3518 	    }
3519 	  else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
3520 	    /* Reference variables should be references to objects.  */
3521 	    *nonnull = 1;
3522 	}
3523       return 0;
3524 
3525     default:
3526       return 0;
3527     }
3528 }
3529 
3530 void
init_class_processing()3531 init_class_processing ()
3532 {
3533   current_class_depth = 0;
3534   current_class_stacksize = 10;
3535   current_class_base = (tree *)xmalloc(current_class_stacksize * sizeof (tree));
3536   current_class_stack = current_class_base;
3537 
3538   current_lang_stacksize = 10;
3539   current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree));
3540   current_lang_stack = current_lang_base;
3541 
3542   delta_name = get_identifier (VTABLE_DELTA_NAME);
3543   pfn_name = get_identifier (VTABLE_PFN_NAME);
3544 
3545   /* Keep these values lying around.  */
3546   the_null_vtable_entry = build_vtable_entry (integer_zero_node, integer_zero_node);
3547   base_layout_decl = build_lang_field_decl (FIELD_DECL, NULL_TREE, error_mark_node);
3548   TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE);
3549 
3550   gcc_obstack_init (&class_obstack);
3551 }
3552 
3553 /* Set current scope to NAME. CODE tells us if this is a
3554    STRUCT, UNION, or ENUM environment.
3555 
3556    NAME may end up being NULL_TREE if this is an anonymous or
3557    late-bound struct (as in "struct { ... } foo;")  */
3558 
3559 /* Here's a subroutine we need because C lacks lambdas.  */
3560 static void
unuse_fields(type)3561 unuse_fields (type)
3562      tree type;
3563 {
3564   tree fields;
3565 
3566   for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3567     {
3568       if (TREE_CODE (fields) != FIELD_DECL)
3569 	continue;
3570 
3571       TREE_USED (fields) = 0;
3572       if (DECL_NAME (fields) == NULL_TREE
3573 	  && TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
3574 	unuse_fields (TREE_TYPE (fields));
3575     }
3576 }
3577 
3578 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
3579    appropriate values, found by looking up the type definition of
3580    NAME (as a CODE).
3581 
3582    If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
3583    which can be seen locally to the class.  They are shadowed by
3584    any subsequent local declaration (including parameter names).
3585 
3586    If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
3587    which have static meaning (i.e., static members, static
3588    member functions, enum declarations, etc).
3589 
3590    If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
3591    which can be seen locally to the class (as in 1), but
3592    know that we are doing this for declaration purposes
3593    (i.e. friend foo::bar (int)).
3594 
3595    So that we may avoid calls to lookup_name, we cache the _TYPE
3596    nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
3597 
3598    For multiple inheritance, we perform a two-pass depth-first search
3599    of the type lattice.  The first pass performs a pre-order search,
3600    marking types after the type has had its fields installed in
3601    the appropriate IDENTIFIER_CLASS_VALUE slot.  The second pass merely
3602    unmarks the marked types.  If a field or member function name
3603    appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
3604    that name becomes `error_mark_node'.  */
3605 
3606 void
pushclass(type,modify)3607 pushclass (type, modify)
3608      tree type;
3609      int modify;
3610 {
3611 #ifdef DEBUG_CP_BINDING_LEVELS
3612   indent_to (stderr, debug_bindings_indentation);
3613   fprintf (stderr, "pushclass");
3614   debug_bindings_indentation += 4;
3615 #endif
3616 
3617   push_memoized_context (type, modify);
3618 
3619   current_class_depth++;
3620   *current_class_stack++ = current_class_name;
3621   *current_class_stack++ = current_class_type;
3622   if (current_class_stack >= current_class_base + current_class_stacksize)
3623     {
3624       current_class_base =
3625 	(tree *)xrealloc (current_class_base,
3626 			  sizeof (tree) * (current_class_stacksize + 10));
3627       current_class_stack = current_class_base + current_class_stacksize;
3628       current_class_stacksize += 10;
3629     }
3630 
3631   current_class_name = TYPE_NAME (type);
3632   if (TREE_CODE (current_class_name) == TYPE_DECL)
3633     current_class_name = DECL_NAME (current_class_name);
3634   current_class_type = type;
3635 
3636   if (prev_class_type != NULL_TREE
3637       && (type != prev_class_type
3638 	  || TYPE_SIZE (prev_class_type) == NULL_TREE
3639 	  /* ??? Is this necessary any more?  */
3640 	  || IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (prev_class_type)))
3641       && (current_class_depth == 1 || modify == 3))
3642     {
3643       /* Forcibly remove any old class remnants.  */
3644       popclass (-1);
3645       prev_class_type = 0;
3646     }
3647 
3648   pushlevel_class ();
3649 
3650   if (modify)
3651     {
3652       tree tags;
3653       tree this_fndecl = current_function_decl;
3654 
3655       if (current_function_decl
3656 	  && DECL_CONTEXT (current_function_decl)
3657 	  && TREE_CODE (DECL_CONTEXT (current_function_decl)) == FUNCTION_DECL)
3658 	current_function_decl = DECL_CONTEXT (current_function_decl);
3659       else
3660 	current_function_decl = NULL_TREE;
3661 
3662       if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
3663         {
3664           declare_uninstantiated_type_level ();
3665 	  overload_template_name (current_class_name, 0);
3666         }
3667       else if (type != prev_class_type)
3668 	{
3669 	  build_mi_matrix (type);
3670 	  push_class_decls (type);
3671 	  free_mi_matrix ();
3672 	  prev_class_type = type;
3673 	}
3674       else
3675 	unuse_fields (type);
3676 
3677       for (tags = CLASSTYPE_TAGS (type); tags; tags = TREE_CHAIN (tags))
3678 	{
3679 	  TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 1;
3680 	  if (! TREE_PURPOSE (tags))
3681 	    continue;
3682 	  pushtag (TREE_PURPOSE (tags), TREE_VALUE (tags));
3683 	  if (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) == NULL_TREE
3684 	      && TREE_CODE (TYPE_NAME (TREE_VALUE (tags))) == TYPE_DECL)
3685 	    IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags))
3686 	      = TYPE_NAME (TREE_VALUE (tags));
3687 	}
3688 
3689       current_function_decl = this_fndecl;
3690     }
3691 
3692   if (flag_cadillac)
3693     cadillac_push_class (type);
3694 
3695 #ifdef DEBUG_CP_BINDING_LEVELS
3696   debug_bindings_indentation -= 4;
3697 #endif
3698 }
3699 
3700 /* Get out of the current class scope. If we were in a class scope
3701    previously, that is the one popped to.  The flag MODIFY tells
3702    whether the current scope declarations needs to be modified
3703    as a result of popping to the previous scope.  */
3704 void
popclass(modify)3705 popclass (modify)
3706      int modify;
3707 {
3708 #ifdef DEBUG_CP_BINDING_LEVELS
3709   indent_to (stderr, debug_bindings_indentation);
3710   fprintf (stderr, "popclass");
3711   debug_bindings_indentation += 4;
3712 #endif
3713 
3714   if (flag_cadillac)
3715     cadillac_pop_class ();
3716 
3717   if (modify < 0)
3718     {
3719       /* Back this old class out completely.  */
3720       tree tags = CLASSTYPE_TAGS (prev_class_type);
3721 
3722       pop_class_decls (prev_class_type);
3723       while (tags)
3724 	{
3725 	  TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
3726 	  IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) = NULL_TREE;
3727 	  tags = TREE_CHAIN (tags);
3728 	}
3729       goto ret;
3730     }
3731 
3732   if (modify)
3733     {
3734       /* Just remove from this class what didn't make
3735 	 it into IDENTIFIER_CLASS_VALUE.  */
3736       tree tags = CLASSTYPE_TAGS (current_class_type);
3737 
3738       while (tags)
3739 	{
3740 	  TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
3741 	  if (TREE_PURPOSE (tags))
3742 	    IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) = NULL_TREE;
3743 	  tags = TREE_CHAIN (tags);
3744 	}
3745     }
3746   if (TREE_CODE (current_class_type) == UNINSTANTIATED_P_TYPE)
3747     undo_template_name_overload (current_class_name, 0);
3748   poplevel_class ();
3749 
3750   current_class_depth--;
3751   current_class_type = *--current_class_stack;
3752   current_class_name = *--current_class_stack;
3753 
3754   if (current_class_type)
3755     {
3756       if (CLASSTYPE_VTBL_PTR (current_class_type))
3757 	{
3758 	  current_vtable_decl = lookup_name (DECL_NAME (CLASSTYPE_VTBL_PTR (current_class_type)), 0);
3759 	  if (current_vtable_decl)
3760 	    current_vtable_decl = build_indirect_ref (current_vtable_decl,
3761 						      NULL);
3762 	}
3763       current_class_decl = lookup_name (get_identifier (THIS_NAME), 0);
3764       if (current_class_decl)
3765 	{
3766 	  if (TREE_CODE (TREE_TYPE (current_class_decl)) == POINTER_TYPE)
3767 	    {
3768 	      tree temp;
3769 	      /* Can't call build_indirect_ref here, because it has special
3770 		 logic to return C_C_D given this argument.  */
3771 	      C_C_D = build1 (INDIRECT_REF, current_class_type, current_class_decl);
3772 	      temp = TREE_TYPE (TREE_TYPE (current_class_decl));
3773 	      TREE_READONLY (C_C_D) = TYPE_READONLY (temp);
3774 	      TREE_SIDE_EFFECTS (C_C_D) = TYPE_VOLATILE (temp);
3775 	      TREE_THIS_VOLATILE (C_C_D) = TYPE_VOLATILE (temp);
3776 	    }
3777 	  else
3778 	    C_C_D = current_class_decl;
3779 	}
3780       else C_C_D = NULL_TREE;
3781     }
3782   else
3783     {
3784       current_class_decl = NULL_TREE;
3785       current_vtable_decl = NULL_TREE;
3786       C_C_D = NULL_TREE;
3787     }
3788 
3789   pop_memoized_context (modify);
3790 
3791  ret:
3792   ;
3793 #ifdef DEBUG_CP_BINDING_LEVELS
3794   debug_bindings_indentation -= 4;
3795 #endif
3796 }
3797 
3798 /* Set global variables CURRENT_LANG_NAME to appropriate value
3799    so that behavior of name-mangling machinery is correct.  */
3800 
3801 void
push_lang_context(name)3802 push_lang_context (name)
3803      tree name;
3804 {
3805   *current_lang_stack++ = current_lang_name;
3806   if (current_lang_stack >= current_lang_base + current_lang_stacksize)
3807     {
3808       current_lang_base =
3809 	(tree *)xrealloc (current_lang_base,
3810 			  sizeof (tree) * (current_lang_stacksize + 10));
3811       current_lang_stack = current_lang_base + current_lang_stacksize;
3812       current_lang_stacksize += 10;
3813     }
3814 
3815   if (name == lang_name_cplusplus)
3816     {
3817       strict_prototype = strict_prototypes_lang_cplusplus;
3818       current_lang_name = name;
3819     }
3820   else if (name == lang_name_c)
3821     {
3822       strict_prototype = strict_prototypes_lang_c;
3823       current_lang_name = name;
3824     }
3825   else
3826     error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
3827 
3828   if (flag_cadillac)
3829     cadillac_push_lang (name);
3830 }
3831 
3832 /* Get out of the current language scope.  */
3833 void
pop_lang_context()3834 pop_lang_context ()
3835 {
3836   if (flag_cadillac)
3837     cadillac_pop_lang ();
3838 
3839   current_lang_name = *--current_lang_stack;
3840   if (current_lang_name == lang_name_cplusplus)
3841     strict_prototype = strict_prototypes_lang_cplusplus;
3842   else if (current_lang_name == lang_name_c)
3843     strict_prototype = strict_prototypes_lang_c;
3844 }
3845 
3846 int
root_lang_context_p()3847 root_lang_context_p ()
3848 {
3849   return current_lang_stack == current_lang_base;
3850 }
3851 
3852 /* Type instantiation routines.  */
3853 
3854 /* This function will instantiate the type of the expression given
3855    in RHS to match the type of LHSTYPE.  If LHSTYPE is NULL_TREE,
3856    or other errors exist, the TREE_TYPE of RHS will be ERROR_MARK_NODE.
3857 
3858    This function is used in build_modify_expr, convert_arguments,
3859    build_c_cast, and compute_conversion_costs.  */
3860 tree
instantiate_type(lhstype,rhs,complain)3861 instantiate_type (lhstype, rhs, complain)
3862      tree lhstype, rhs;
3863      int complain;
3864 {
3865   if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
3866     {
3867       if (complain)
3868 	error ("not enough type information");
3869       return error_mark_node;
3870     }
3871 
3872   if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
3873     return rhs;
3874 
3875   /* This should really only be used when attempting to distinguish
3876      what sort of a pointer to function we have.  For now, any
3877      arithmetic operation which is not supported on pointers
3878      is rejected as an error.  */
3879 
3880   switch (TREE_CODE (rhs))
3881     {
3882     case TYPE_EXPR:
3883     case CONVERT_EXPR:
3884     case SAVE_EXPR:
3885     case CONSTRUCTOR:
3886     case BUFFER_REF:
3887       my_friendly_abort (177);
3888       return error_mark_node;
3889 
3890     case INDIRECT_REF:
3891     case ARRAY_REF:
3892       TREE_TYPE (rhs) = lhstype;
3893       lhstype = build_pointer_type (lhstype);
3894       TREE_OPERAND (rhs, 0)
3895 	= instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
3896       if (TREE_OPERAND (rhs, 0) == error_mark_node)
3897 	return error_mark_node;
3898 
3899       return rhs;
3900 
3901     case NOP_EXPR:
3902       rhs = copy_node (TREE_OPERAND (rhs, 0));
3903       TREE_TYPE (rhs) = unknown_type_node;
3904       return instantiate_type (lhstype, rhs, complain);
3905 
3906     case COMPONENT_REF:
3907       {
3908 	tree field = TREE_OPERAND (rhs, 1);
3909 	if (TREE_CODE (field) == TREE_LIST)
3910 	  {
3911 	    tree function = instantiate_type (lhstype, field, complain);
3912 	    if (function == error_mark_node)
3913 	      return error_mark_node;
3914 	    my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 185);
3915 	    if (DECL_VINDEX (function))
3916 	      {
3917 		tree base = TREE_OPERAND (rhs, 0);
3918 		tree base_ptr = build_unary_op (ADDR_EXPR, base, 0);
3919 		if (base_ptr == error_mark_node)
3920 		  return error_mark_node;
3921 		base_ptr = convert_pointer_to (DECL_CONTEXT (function), base_ptr);
3922 		if (base_ptr == error_mark_node)
3923 		  return error_mark_node;
3924 		return build_vfn_ref (&base_ptr, base, DECL_VINDEX (function));
3925 	      }
3926 	    return function;
3927 	  }
3928 
3929 	my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 178);
3930 	my_friendly_assert (!(TREE_CODE (TREE_TYPE (field)) == FUNCTION_TYPE
3931 			      || TREE_CODE (TREE_TYPE (field)) == METHOD_TYPE),
3932 			    179);
3933 
3934 	TREE_TYPE (rhs) = lhstype;
3935 	/* First look for an exact match  */
3936 
3937 	while (field && TREE_TYPE (field) != lhstype)
3938 	  field = TREE_CHAIN (field);
3939 	if (field)
3940 	  {
3941 	    TREE_OPERAND (rhs, 1) = field;
3942 	    return rhs;
3943 	  }
3944 
3945 	/* No exact match found, look for a compatible function.  */
3946 	field = TREE_OPERAND (rhs, 1);
3947 	while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
3948 	  field = TREE_CHAIN (field);
3949 	if (field)
3950 	  {
3951 	    TREE_OPERAND (rhs, 1) = field;
3952 	    field = TREE_CHAIN (field);
3953 	    while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
3954 	      field = TREE_CHAIN (field);
3955 	    if (field)
3956 	      {
3957 		if (complain)
3958 		  error ("ambiguous overload for COMPONENT_REF requested");
3959 		return error_mark_node;
3960 	      }
3961 	  }
3962 	else
3963 	  {
3964 	    if (complain)
3965 	      error ("no appropriate overload exists for COMPONENT_REF");
3966 	    return error_mark_node;
3967 	  }
3968 	return rhs;
3969       }
3970 
3971     case TREE_LIST:
3972       {
3973 	tree elem, baselink, name;
3974 	int globals = overloaded_globals_p (rhs);
3975 
3976 	/* If there's only one function we know about, return that.  */
3977 	if (globals > 0 && TREE_CHAIN (rhs) == NULL_TREE)
3978 	  return TREE_VALUE (rhs);
3979 
3980 	/* First look for an exact match.  Search either overloaded
3981 	   functions or member functions.  May have to undo what
3982 	   `default_conversion' might do to lhstype.  */
3983 
3984 	if (TREE_CODE (lhstype) == POINTER_TYPE)
3985 	  if (TREE_CODE (TREE_TYPE (lhstype)) == FUNCTION_TYPE
3986 	      || TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE)
3987 	    lhstype = TREE_TYPE (lhstype);
3988 	  else
3989 	    {
3990 	      if (complain)
3991 		error ("invalid type combination for overload");
3992 	      return error_mark_node;
3993 	    }
3994 
3995 	if (TREE_CODE (lhstype) != FUNCTION_TYPE && globals > 0)
3996 	  {
3997 	    if (complain)
3998 	      error ("cannot resolve overloaded function `%s' based on non-function type",
3999 		     IDENTIFIER_POINTER (TREE_PURPOSE (rhs)));
4000 	    return error_mark_node;
4001 	  }
4002 
4003 	if (globals > 0)
4004 	  {
4005 	    my_friendly_assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL,
4006 				180);
4007 	    elem = rhs;
4008 	    while (elem)
4009 	      if (TREE_TYPE (TREE_VALUE (elem)) != lhstype)
4010 		elem = TREE_CHAIN (elem);
4011 	      else
4012 		return TREE_VALUE (elem);
4013 	    /* No exact match found, look for a compatible function.  */
4014 	    elem = rhs;
4015 	    while (elem && ! comp_target_types (lhstype, TREE_TYPE (TREE_VALUE (elem)), 1))
4016 	      elem = TREE_CHAIN (elem);
4017 	    if (elem)
4018 	      {
4019 		tree save_elem = TREE_VALUE (elem);
4020 		elem = TREE_CHAIN (elem);
4021 		while (elem && ! comp_target_types (lhstype, TREE_TYPE (TREE_VALUE (elem)), 0))
4022 		  elem = TREE_CHAIN (elem);
4023 		if (elem)
4024 		  {
4025 		    if (complain)
4026 		      error ("ambiguous overload for overloaded function requested");
4027 		    return error_mark_node;
4028 		  }
4029 		return save_elem;
4030 	      }
4031 	    if (complain)
4032 	      {
4033 		if (TREE_CHAIN (rhs))
4034 		  error ("no appropriate overload for overloaded function `%s' exists",
4035 			 IDENTIFIER_POINTER (TREE_PURPOSE (rhs)));
4036 		else
4037 		  error ("function `%s' has inappropriate type signature",
4038 			 IDENTIFIER_POINTER (TREE_PURPOSE (rhs)));
4039 	      }
4040 	    return error_mark_node;
4041 	  }
4042 
4043 	if (TREE_NONLOCAL_FLAG (rhs))
4044 	  {
4045 	    /* Got to get it as a baselink.  */
4046 	    rhs = lookup_fnfields (TYPE_BINFO (current_class_type),
4047 				   TREE_PURPOSE (rhs), 0);
4048 	  }
4049 	else
4050 	  {
4051 	    my_friendly_assert (TREE_CHAIN (rhs) == NULL_TREE, 181);
4052 	    if (TREE_CODE (TREE_VALUE (rhs)) == TREE_LIST)
4053 	      rhs = TREE_VALUE (rhs);
4054 	    my_friendly_assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL,
4055 				182);
4056 	  }
4057 
4058 	for (baselink = rhs; baselink;
4059 	     baselink = next_baselink (baselink))
4060 	  {
4061 	    elem = TREE_VALUE (baselink);
4062 	    while (elem)
4063 	      if (TREE_TYPE (elem) != lhstype)
4064 		elem = TREE_CHAIN (elem);
4065 	      else
4066 		return elem;
4067 	  }
4068 
4069 	/* No exact match found, look for a compatible method.  */
4070 	for (baselink = rhs; baselink;
4071 	     baselink = next_baselink (baselink))
4072 	  {
4073 	    elem = TREE_VALUE (baselink);
4074 	    while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 1))
4075 	      elem = TREE_CHAIN (elem);
4076 	    if (elem)
4077 	      {
4078 		tree save_elem = elem;
4079 		elem = TREE_CHAIN (elem);
4080 		while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 0))
4081 		  elem = TREE_CHAIN (elem);
4082 		if (elem)
4083 		  {
4084 		    if (complain)
4085 		      error ("ambiguous overload for overloaded method requested");
4086 		    return error_mark_node;
4087 		  }
4088 		return save_elem;
4089 	      }
4090 	    name = DECL_NAME (TREE_VALUE (rhs));
4091 	    if (TREE_CODE (lhstype) == FUNCTION_TYPE && globals < 0)
4092 	      {
4093 		/* Try to instantiate from non-member functions.  */
4094 		rhs = IDENTIFIER_GLOBAL_VALUE (name);
4095 		if (rhs && TREE_CODE (rhs) == TREE_LIST)
4096 		  {
4097 		    /* This code seems to be missing a `return'.  */
4098 		    my_friendly_abort (4);
4099 		    instantiate_type (lhstype, rhs, complain);
4100 		  }
4101 	      }
4102 	  }
4103 	if (complain)
4104 	  error ("no static member functions named `%s'",
4105 		 IDENTIFIER_POINTER (name));
4106 	return error_mark_node;
4107       }
4108 
4109     case CALL_EXPR:
4110       /* This is too hard for now.  */
4111       my_friendly_abort (183);
4112       return error_mark_node;
4113 
4114     case PLUS_EXPR:
4115     case MINUS_EXPR:
4116     case COMPOUND_EXPR:
4117       TREE_OPERAND (rhs, 0) = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
4118       if (TREE_OPERAND (rhs, 0) == error_mark_node)
4119 	return error_mark_node;
4120       TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
4121       if (TREE_OPERAND (rhs, 1) == error_mark_node)
4122 	return error_mark_node;
4123 
4124       TREE_TYPE (rhs) = lhstype;
4125       return rhs;
4126 
4127     case MULT_EXPR:
4128     case TRUNC_DIV_EXPR:
4129     case FLOOR_DIV_EXPR:
4130     case CEIL_DIV_EXPR:
4131     case ROUND_DIV_EXPR:
4132     case RDIV_EXPR:
4133     case TRUNC_MOD_EXPR:
4134     case FLOOR_MOD_EXPR:
4135     case CEIL_MOD_EXPR:
4136     case ROUND_MOD_EXPR:
4137     case FIX_ROUND_EXPR:
4138     case FIX_FLOOR_EXPR:
4139     case FIX_CEIL_EXPR:
4140     case FIX_TRUNC_EXPR:
4141     case FLOAT_EXPR:
4142     case NEGATE_EXPR:
4143     case ABS_EXPR:
4144     case MAX_EXPR:
4145     case MIN_EXPR:
4146     case FFS_EXPR:
4147 
4148     case BIT_AND_EXPR:
4149     case BIT_IOR_EXPR:
4150     case BIT_XOR_EXPR:
4151     case LSHIFT_EXPR:
4152     case RSHIFT_EXPR:
4153     case LROTATE_EXPR:
4154     case RROTATE_EXPR:
4155 
4156     case PREINCREMENT_EXPR:
4157     case PREDECREMENT_EXPR:
4158     case POSTINCREMENT_EXPR:
4159     case POSTDECREMENT_EXPR:
4160       if (complain)
4161 	error ("illegal operation on uninstantiated type");
4162       return error_mark_node;
4163 
4164     case TRUTH_AND_EXPR:
4165     case TRUTH_OR_EXPR:
4166     case TRUTH_XOR_EXPR:
4167     case LT_EXPR:
4168     case LE_EXPR:
4169     case GT_EXPR:
4170     case GE_EXPR:
4171     case EQ_EXPR:
4172     case NE_EXPR:
4173     case TRUTH_ANDIF_EXPR:
4174     case TRUTH_ORIF_EXPR:
4175     case TRUTH_NOT_EXPR:
4176       if (complain)
4177 	error ("not enough type information");
4178       return error_mark_node;
4179 
4180     case COND_EXPR:
4181       if (type_unknown_p (TREE_OPERAND (rhs, 0)))
4182 	{
4183 	  if (complain)
4184 	    error ("not enough type information");
4185 	  return error_mark_node;
4186 	}
4187       TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
4188       if (TREE_OPERAND (rhs, 1) == error_mark_node)
4189 	return error_mark_node;
4190       TREE_OPERAND (rhs, 2) = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), complain);
4191       if (TREE_OPERAND (rhs, 2) == error_mark_node)
4192 	return error_mark_node;
4193 
4194       TREE_TYPE (rhs) = lhstype;
4195       return rhs;
4196 
4197     case MODIFY_EXPR:
4198       TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
4199       if (TREE_OPERAND (rhs, 1) == error_mark_node)
4200 	return error_mark_node;
4201 
4202       TREE_TYPE (rhs) = lhstype;
4203       return rhs;
4204 
4205     case ADDR_EXPR:
4206       if (TREE_CODE (lhstype) != POINTER_TYPE)
4207 	{
4208 	  if (complain)
4209 	    error ("type for resolving address of overloaded function must be pointer type");
4210 	  return error_mark_node;
4211 	}
4212       TREE_TYPE (rhs) = lhstype;
4213       lhstype = TREE_TYPE (lhstype);
4214       TREE_OPERAND (rhs, 0) = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
4215       if (TREE_OPERAND (rhs, 0) == error_mark_node)
4216 	return error_mark_node;
4217 
4218       mark_addressable (TREE_OPERAND (rhs, 0));
4219       return rhs;
4220 
4221     case ENTRY_VALUE_EXPR:
4222       my_friendly_abort (184);
4223       return error_mark_node;
4224 
4225     case ERROR_MARK:
4226       return error_mark_node;
4227 
4228     default:
4229       my_friendly_abort (185);
4230       return error_mark_node;
4231     }
4232 }
4233 
4234 /* Return the name of the virtual function pointer field
4235    (as an IDENTIFIER_NODE) for the given TYPE.  Note that
4236    this may have to look back through base types to find the
4237    ultimate field name.  (For single inheritance, these could
4238    all be the same name.  Who knows for multiple inheritance).  */
4239 static tree
get_vfield_name(type)4240 get_vfield_name (type)
4241      tree type;
4242 {
4243   tree binfo = TYPE_BINFO (type);
4244   char *buf;
4245 
4246   while (BINFO_BASETYPES (binfo)
4247 	 && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
4248 	 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
4249     binfo = BINFO_BASETYPE (binfo, 0);
4250 
4251   type = BINFO_TYPE (binfo);
4252   buf = (char *)alloca (sizeof (VFIELD_NAME_FORMAT)
4253 			+ TYPE_NAME_LENGTH (type) + 2);
4254   sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
4255   return get_identifier (buf);
4256 }
4257 
4258 void
print_class_statistics()4259 print_class_statistics ()
4260 {
4261 #ifdef GATHER_STATISTICS
4262   fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
4263   fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
4264   fprintf (stderr, "build_method_call = %d (inner = %d)\n",
4265 	   n_build_method_call, n_inner_fields_searched);
4266   if (n_vtables)
4267     {
4268       fprintf (stderr, "vtables = %d; vtable searches = %d\n",
4269 	       n_vtables, n_vtable_searches);
4270       fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
4271 	       n_vtable_entries, n_vtable_elems);
4272     }
4273 #endif
4274 }
4275