home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / cplus-class.c < prev    next >
C/C++ Source or Header  |  1991-06-04  |  125KB  |  3,996 lines

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