home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cplusplus-8 / cplus-class.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-29  |  187.8 KB  |  6,321 lines

  1. /* Functions related to building and playing with classes.
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.    Contributed by Michael Tiemann (tiemann@mcc.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 1, 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 "rtl.h"
  29. #include "assert.h"
  30. #include <stdio.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 NULL 0
  40. #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
  41. #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
  42.  
  43. /* See cplus-decl.c for comment of this variable.  */
  44. extern int flag_int_enum_equivalence;
  45.  
  46. /* some statistics gathering help.  */
  47. static int n_vtables, n_vtable_entries, n_vtable_searches, n_vtable_elems;
  48. static int n_convert_harshness, n_compute_conversion_costs, n_build_method_call;
  49. static int n_inner_fields_searched;
  50.  
  51. /* Compute the ease with which a conversion can be performed
  52.    between an expected and the given type.  */
  53. static int convert_harshness ();
  54.  
  55. /* in decl.c.  */
  56. extern tree lookup_tag_current_binding_level ();
  57.  
  58. /* in method.c.  */
  59. extern void do_inline_function_hair ();
  60.  
  61. /* Way of stacking class types.  */
  62. static tree *current_class_base, *current_class_stack;
  63. static int current_class_stacksize;
  64.  
  65. struct class_level
  66. {
  67.   /* The previous class level.  */
  68.   struct class_level *level_chain;
  69.  
  70.   /* The class instance variable, as a PARM_DECL.  */
  71.   tree decl;
  72.   /* The class instance variable, as an object.  */
  73.   tree object;
  74.   /* The virtual function table pointer
  75.      for the class instance variable.  */
  76.   tree vtable_decl;
  77.  
  78.   /* Name of the current class.  */
  79.   tree name;
  80.   /* Type of the current class.  */
  81.   tree type;
  82.  
  83.   /* Flags for this class level.  */
  84.   int this_is_variable;
  85.   int memoized_lookups;
  86.   int save_memoized;
  87.   int unused;
  88. };
  89.  
  90. tree current_class_decl, C_C_D;    /* PARM_DECL: the class instance variable */
  91. tree current_vtable_decl;
  92.  
  93. /* The following two can be derived from the previous one */
  94. tree current_class_name;    /* IDENTIFIER_NODE: name of current class */
  95. tree current_class_type;    /* _TYPE: the type of the current class */
  96. tree prev_class_type;        /* _TYPE: the previous type that was a class */
  97.  
  98. static tree get_vtable_name (), get_vfield_name ();
  99. tree the_null_vtable_entry;
  100.  
  101. /* Way of stacking langauge names.  */
  102. static tree *current_lang_base, *current_lang_stack;
  103. static int current_lang_stacksize;
  104.  
  105. /* Names of languages we recognize.  */
  106. tree lang_name_c, lang_name_cplusplus;
  107. #ifdef OBJCPLUS
  108. tree lang_name_objc;
  109. #endif /* OBJCPLUS */
  110. tree current_lang_name;
  111.  
  112. tree minus_one_node;
  113.  
  114. /* When layout out an aggregate type, the size of the
  115.    basetypes (virtual and non-virtual) is passed to layout_record
  116.    via this node.  */
  117. static tree base_layout_decl;
  118.  
  119. #if 0
  120. /* Make sure that the tag NAME is defined *in the current binding level*
  121.    at least as a forward reference.
  122.    CODE says which kind of tag NAME ought to be.
  123.  
  124.    Not used for C++.  Not maintained.  */
  125.  
  126. tree
  127. start_struct (code, name)
  128.      enum tree_code code;
  129.      tree name;
  130. {
  131.   /* If there is already a tag defined at this binding level
  132.      (as a forward reference), just return it.  */
  133.   register tree ref = 0;
  134.  
  135.   if (name != 0)
  136.     ref = lookup_tag (code, name, current_binding_level, 1);
  137.   if (ref && TREE_CODE (ref) == code)
  138.     {
  139.       if (TYPE_FIELDS (ref))
  140.     error ((code == UNION_TYPE ? "redefinition of `union %s'"
  141.         : "redefinition of `struct %s'"),
  142.            IDENTIFIER_POINTER (name));
  143.  
  144.       return ref;
  145.     }
  146.  
  147.   /* Otherwise create a forward-reference just so the tag is in scope.  */
  148.  
  149.   ref = make_lang_type (code);
  150.   /* Must re-synch this with xref_tag if you are going to use it.  */
  151.   assert (0);
  152.   pushtag (name, ref);
  153.   return ref;
  154. }
  155. #endif
  156.  
  157. /* Virtual baseclass things.  */
  158. tree
  159. build_vbase_pointer (exp, type)
  160.      tree exp, type;
  161. {
  162.   char *name;
  163.  
  164.   name = (char *) alloca (TYPE_NAME_LENGTH (type) + sizeof (VBASE_NAME) + 1);
  165.   sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (type));
  166.   return build_component_ref (exp, get_identifier (name), 0, 0);
  167. }
  168.  
  169. /* Build multi-level access to EXPR using hierarchy path PATH.
  170.    CODE is PLUS_EXPR if we are going with the grain,
  171.    and MINUS_EXPR if we are not (in which case, we cannot traverse
  172.    virtual baseclass links).
  173.  
  174.    TYPE is the type we want this path to have on exit.
  175.  
  176.    ALIAS_THIS is non-zero if EXPR in an expression involving `this'.  */
  177. tree
  178. build_vbase_path (code, type, expr, path, alias_this)
  179.      enum tree_code code;
  180.      tree type;
  181.      tree expr;
  182.      tree path;
  183.      int alias_this;
  184. {
  185.   register int changed = 0;
  186.   tree last = NULL_TREE, last_virtual = NULL_TREE;
  187.   int fixed_type_p = 0 && resolves_to_fixed_type_p (expr);
  188.   tree basetype;
  189.   tree offset = integer_zero_node;
  190.  
  191.   if (TREE_CHAIN (path))
  192.     path = nreverse (copy_list (path));
  193.  
  194.   basetype = TREE_VALUE (path);
  195.   while (path)
  196.     {
  197.       if (TREE_VIA_VIRTUAL (path))
  198.     {
  199.       last_virtual = TYPE_MAIN_VARIANT (TREE_VALUE (path));
  200.       if (code == PLUS_EXPR)
  201.         {
  202.           changed = ! fixed_type_p;
  203.  
  204.           if (last)
  205.         expr = convert_pointer_to (TREE_VALUE (last), expr);
  206.           if (changed)
  207.         expr = build_vbase_pointer (build_indirect_ref (expr, 0),
  208.                         last_virtual);
  209.           else
  210.         offset = ASSOC_OFFSET (value_member (last_virtual,
  211.                              CLASSTYPE_VBASECLASSES (basetype)));
  212.           /* Happens in the case of parse errors.  */
  213.           if (expr == error_mark_node)
  214.         return expr;
  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.   /* If we go through any virtual base pointers, make sure that
  229.      casts to BASETYPE from the last virtual base class use
  230.      the right value for BASETYPE.  */
  231.   if (changed)
  232.     {
  233.       tree intype = TREE_TYPE (TREE_TYPE (expr));
  234.       if (TYPE_MAIN_VARIANT (intype) == TYPE_MAIN_VARIANT (last))
  235.     basetype = intype;
  236.       else
  237.     {
  238.       basetype = get_base_type (last, TYPE_MAIN_VARIANT (intype), 0);
  239.       offset = CLASSTYPE_OFFSET (basetype);
  240.     }
  241.     }
  242.   else
  243.     {
  244.       if (last_virtual && last != last_virtual)
  245.     basetype = get_base_type (last, last_virtual, 0);
  246.       else
  247.     basetype = last;
  248.  
  249.       offset = genop (code, offset, CLASSTYPE_OFFSET (basetype));
  250.       /* 900324 JRV: If code was MINUS_EXPR, genop returned a negative
  251.      offset, so shouldn't the code always be PLUS_EXPR now? */
  252.       code = PLUS_EXPR;
  253.     }
  254.  
  255.   if (TREE_INT_CST_LOW (offset))
  256.     {
  257.       /* For multiple inheritance: if `this' can be set by
  258.      any function, then it could be 0 on entry
  259.      to any function.  Preserve such zeroness here.
  260.      Otherwise, only in the case of constructors need
  261.      we worry, and in those cases, it will be zero,
  262.      or initialized to some legal value to which we may
  263.      add.  */
  264.       tree addr = TREE_CODE (expr) == ADDR_EXPR
  265.     ? expr : save_expr (expr);
  266.       expr = build (code, type, addr, offset);
  267.  
  268.       if (alias_this == 0 || flag_this_is_variable)
  269.     return build (COND_EXPR, type,
  270.               build (EQ_EXPR, integer_type_node, addr, integer_zero_node),
  271.               build1 (NOP_EXPR, type, addr),
  272.               expr);
  273.     }
  274.   return build1 (NOP_EXPR, type, expr);
  275. }
  276.  
  277. /* Virtual function things.  */
  278.  
  279. /* Virtual functions to be dealt with after laying out our
  280.    virtual base classes (only if the type has any).  */
  281. static tree pending_hard_virtuals;
  282.  
  283. /* The names of the entries in the virtual table structure.  */
  284. static tree delta_name, pfn_name;
  285.  
  286. /* Temporary assoc list to memoize lookups of the left-most non-virtual
  287.    baseclass B in a lattice topped by T.  B can appear multiple times
  288.    in the lattice.
  289.    TREE_PURPOSE is B's TYPE_MAIN_VARIANT.
  290.    TREE_VALUE is the path by which B is reached from T.
  291.    TREE_TYPE is B's real type.
  292.  
  293.    If TREE_TYPE is NULL_TREE, it means that B was reached via
  294.    a virtual baseclass.
  295.    N.B.: This list consists of nodes on the temporary obstack.  */
  296. static tree leftmost_baseclasses;
  297.  
  298. /* Build an entry in the virtual function table.
  299.    DELTA is the offset for the `this' pointer.
  300.    PFN is an ADDR_EXPR containing a pointer to the virtual function.
  301.    Note that the index (DELTA2) in the virtual function table
  302.    is always 0.  */
  303. tree
  304. build_vtable_entry (delta, pfn)
  305.      tree delta, pfn;
  306. {
  307.   tree elems = tree_cons (NULL_TREE, delta,
  308.               tree_cons (NULL_TREE, integer_zero_node,
  309.                      build_tree_list (NULL_TREE, pfn)));
  310.   tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
  311.   TREE_LITERAL (entry) = 1;
  312.   TREE_STATIC (entry) = 1;
  313.   TREE_READONLY (entry) = 1;
  314.  
  315. #ifdef GATHER_STATISTICS
  316.   n_vtable_entries += 1;
  317. #endif
  318.  
  319.   return entry;
  320. }
  321.  
  322. /* Given an object INSTANCE, return an expression which yields
  323.    the virtual function corresponding to INDEX.  There are many special
  324.    cases for INSTANCE which we take care of here, mainly to avoid
  325.    creating extra tree nodes when we don't have to.  */
  326. tree
  327. build_vfn_ref (ptr_to_instptr, instance, index)
  328.      tree *ptr_to_instptr, instance;
  329.      tree index;
  330. {
  331.   extern int building_cleanup;
  332.   tree vtbl, aref;
  333.   tree basetype = TREE_TYPE (instance);
  334.  
  335.   if (TREE_CODE (basetype) == REFERENCE_TYPE)
  336.     basetype = TREE_TYPE (basetype);
  337.  
  338.   if (instance == C_C_D)
  339.     {
  340.       if (current_vtable_decl == NULL_TREE
  341.       || current_vtable_decl == error_mark_node
  342.       || get_base_type (DECL_FCONTEXT (CLASSTYPE_VFIELD (current_class_type)), basetype, 0) == NULL_TREE)
  343.     vtbl = build_indirect_ref (build_vfield_ref (instance, basetype));
  344.       else
  345.     vtbl = current_vtable_decl;
  346.     }
  347.   else
  348.     {
  349.       if (optimize)
  350.     {
  351.       /* Try to figure out what a reference refers to, and
  352.          access its virtual function table directly.  */
  353.       tree ref = 0;
  354.  
  355.       if (TREE_CODE (instance) == INDIRECT_REF
  356.           && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
  357.         ref = TREE_OPERAND (instance, 0);
  358.       else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
  359.         ref = instance;
  360.  
  361.       if (ref && TREE_CODE (ref) == VAR_DECL
  362.           && DECL_INITIAL (ref))
  363.         {
  364.           tree init = DECL_INITIAL (ref);
  365.  
  366.           while (TREE_CODE (init) == NOP_EXPR
  367.              || TREE_CODE (init) == REFERENCE_EXPR)
  368.         init = TREE_OPERAND (init, 0);
  369.           if (TREE_CODE (init) == ADDR_EXPR)
  370.         {
  371.           init = TREE_OPERAND (init, 0);
  372.           if (IS_AGGR_TYPE (TREE_TYPE (init))
  373.               && (TREE_CODE (init) == PARM_DECL
  374.               || TREE_CODE (init) == VAR_DECL))
  375.             instance = init;
  376.         }
  377.         }
  378.     }
  379.  
  380.       if (IS_AGGR_TYPE (instance)
  381.       && (TREE_CODE (instance) == RESULT_DECL
  382.           || TREE_CODE (instance) == PARM_DECL
  383.           || TREE_CODE (instance) == VAR_DECL))
  384.     vtbl = CLASS_ASSOC_VTABLE (basetype);
  385.       else
  386.     vtbl = build_indirect_ref (build_vfield_ref (instance, basetype), 0);
  387.     }
  388.   aref = build_array_ref (vtbl, index);
  389.   if (!building_cleanup && TREE_CODE (aref) == INDIRECT_REF)
  390.     TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
  391.  
  392.   *ptr_to_instptr = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
  393.                *ptr_to_instptr,
  394.                convert (integer_type_node, build_component_ref (aref, delta_name, 0, 0)));
  395.   return build_component_ref (aref, pfn_name, 0, 0);
  396. }
  397.  
  398. /* Build a virtual function for type TYPE.
  399.    If ASSOC is non-NULL, build the vtable starting with the intial
  400.    approximation that it is the same as the one which is the head of
  401.    the assocation list.  */
  402. static tree
  403. build_vtable (assoc, type)
  404.      tree assoc, type;
  405. {
  406.   tree name = get_vtable_name (type);
  407.   tree virtuals, decl;
  408.  
  409.   if (assoc)
  410.     {
  411.       virtuals = copy_list (ASSOC_VIRTUALS (assoc));
  412.       decl = build_decl (VAR_DECL, name, TREE_TYPE (ASSOC_VTABLE (assoc)));
  413.     }
  414.   else
  415.     {
  416.       virtuals = NULL_TREE;
  417.       decl = build_decl (VAR_DECL, name, void_type_node);
  418.     }
  419.  
  420. #ifdef GATHER_STATISTICS
  421.   n_vtables += 1;
  422.   n_vtable_elems += list_length (virtuals);
  423. #endif
  424.  
  425.   if (write_virtuals >= 2)
  426.     {
  427.       if (CLASSTYPE_INTERFACE_UNKNOWN (type) == 0)
  428.     {
  429.       TREE_PUBLIC (decl) = CLASSTYPE_VTABLE_NEEDS_WRITING (type);
  430.       TREE_EXTERNAL (decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (type);
  431.     }
  432.     }
  433.   else if (write_virtuals > 0)
  434.     TREE_PUBLIC (decl) = 1;
  435.   else if (write_virtuals < 0)
  436.     TREE_EXTERNAL (decl) = 1;
  437.  
  438.   IDENTIFIER_GLOBAL_VALUE (name) = decl = pushdecl_top_level (decl);
  439.   /* Initialize the association list for this type, based
  440.      on our first approximation.  */
  441.   CLASS_ASSOC_VTABLE (type) = decl;
  442.   CLASS_ASSOC_VIRTUALS (type) = virtuals;
  443.  
  444.   TREE_STATIC (decl) = 1;
  445.   DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
  446.                DECL_ALIGN (decl));
  447.  
  448.   if (assoc && write_virtuals >= 0)
  449.     DECL_VIRTUAL_P (decl) = 1;
  450.   /* Remember which class this vtable is really for.  */
  451.   DECL_VPARENT (decl) = type;
  452.   DECL_CONTEXT (decl) = type;
  453.   CLASSTYPE_MARKED3 (type) = 1;
  454.   CLASSTYPE_MARKED4 (type) = 1;
  455.   return decl;
  456. }
  457.  
  458. /* Give TYPE a new virtual function table which is initialized
  459.    with a skeleton-copy of its original initialization.  The only
  460.    entry that changes is the `delta' entry, so we can really
  461.    share a lot of structure.
  462.  
  463.    FOR_TYPE is the derived type which caused this table to
  464.    be needed.
  465.  
  466.    ASSOC is the type association which provided TYPE for FOR_TYPE.
  467.  
  468.    The way we update BASE_ASSOC's vtable information is just to change the
  469.    association information in FOR_TYPE's association list.  */
  470. static void
  471. prepare_fresh_vtable (assoc, base_assoc, for_type)
  472.      tree assoc, base_assoc, for_type;
  473. {
  474.   tree basetype = ASSOC_TYPE (assoc);
  475.   tree orig_decl = ASSOC_VTABLE (assoc);
  476.   tree name = build_type_pathname (VTABLE_NAME_FORMAT, basetype, for_type);
  477.   tree new_decl = build_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
  478.   tree path;
  479.   int result;
  480.  
  481.   assert (TREE_USED (assoc) == 0);
  482.  
  483.   /* Remember which class this vtable is really for.  */
  484.   DECL_VPARENT (new_decl) = ASSOC_TYPE (base_assoc);
  485.   DECL_CONTEXT (new_decl) = for_type;
  486.  
  487.   TREE_STATIC (new_decl) = 1;
  488.   ASSOC_VTABLE (assoc) = pushdecl_top_level (new_decl);
  489.   DECL_VIRTUAL_P (new_decl) = 1;
  490.   DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
  491.  
  492.   /* Make fresh virtual list, so we can smash it later.  */
  493.   assert (ASSOC_VIRTUALS (assoc));
  494.   ASSOC_VIRTUALS (assoc) = copy_list (ASSOC_VIRTUALS (assoc));
  495.  
  496. #ifdef GATHER_STATISTICS
  497.   n_vtables += 1;
  498.   n_vtable_elems += list_length (ASSOC_VIRTUALS (assoc));
  499. #endif
  500.  
  501.   /* Set `new_decl's PUBLIC and EXTERNAL bits.  */
  502.   if (write_virtuals >= 2)
  503.     {
  504.       if (CLASSTYPE_INTERFACE_UNKNOWN (for_type) == 0)
  505.     {
  506.       TREE_PUBLIC (new_decl) = CLASSTYPE_VTABLE_NEEDS_WRITING (for_type);
  507.       TREE_EXTERNAL (new_decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (for_type);
  508.     }
  509.     }
  510.   else if (write_virtuals > 0)
  511.     TREE_PUBLIC (new_decl) = 1;
  512.   else if (write_virtuals < 0)
  513.     TREE_EXTERNAL (new_decl) = 1;
  514.  
  515.   CLASSTYPE_MARKED3 (basetype) = 1;
  516.   CLASSTYPE_MARKED4 (basetype) = 1;
  517.  
  518.   /* Mark all types between FOR_TYPE and TYPE as having been
  519.      touched, so that if we change virtual function table entries,
  520.      new vtables will be initialized.  We may reach the virtual
  521.      baseclass via ambiguous intervening baseclasses.  This
  522.      loop makes sure we get through to the actual baseclass we marked.  */
  523.  
  524.   do
  525.     {
  526.       result = get_base_distance (basetype, for_type, 0, &path);
  527.       for_type = TREE_VALUE (path);
  528.       while (path)
  529.     {
  530.       CLASSTYPE_MARKED3 (TREE_VALUE (path)) = 1;
  531.       path = TREE_CHAIN (path);
  532.     }
  533.     }
  534.   while (result == -2);
  535. }
  536.  
  537. /* Access the virtual function table entry that logically
  538.    contains BASE_FNDECL.  VIRTUALS is the virtual function table's
  539.    initializer.  */
  540. static tree
  541. get_vtable_entry (virtuals, base_fndecl)
  542.      tree virtuals, base_fndecl;
  543. {
  544.   int i = (HOST_BITS_PER_INT >= BITS_PER_WORD
  545. #ifdef VTABLE_USES_MASK
  546.        && 0
  547. #endif
  548.        ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
  549.           & ((1<<(BITS_PER_WORD-1))-1))
  550.        : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
  551.  
  552. #ifdef GATHER_STATISTICS
  553.   n_vtable_searches += i;
  554. #endif
  555.  
  556.   while (i > 0)
  557.     {
  558.       virtuals = TREE_CHAIN (virtuals);
  559.       i -= 1;
  560.     }
  561.   return virtuals;
  562. }
  563.  
  564. /* Put new entry ENTRY into virtual function table initializer
  565.    VIRTUALS.  The virtual function table is for type CONTEXT.
  566.  
  567.    Also update DECL_VINDEX (FNDECL).  */
  568.  
  569. static void
  570. modify_vtable_entry (old_entry_in_list, new_entry, fndecl, context)
  571.      tree old_entry_in_list, new_entry, fndecl, context;
  572. {
  573.   tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list));
  574.   tree vindex;
  575.  
  576.   /* We can't put in the really right offset information
  577.      here, since we have not yet laid out the class to
  578.      take into account virtual base classes.  */
  579.   TREE_VALUE (old_entry_in_list) = new_entry;
  580.   vindex = DECL_VINDEX (TREE_OPERAND (base_pfn, 0));
  581.   if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
  582.     SET_DECL_VINDEX (fndecl, vindex);
  583.   else
  584.     {
  585.       if (! tree_int_cst_equal (DECL_VINDEX (fndecl), vindex))
  586.     {
  587.       tree elts = CONSTRUCTOR_ELTS (new_entry);
  588.       tree vfield = CLASSTYPE_VFIELD (context);
  589.       /* Compute the relative offset of vtable we are really looking for.  */
  590.       TREE_VALUE (elts) = genop (PLUS_EXPR,
  591.                      build_int (DECL_OFFSET (vfield)
  592.                         / DECL_SIZE_UNIT (vfield)),
  593.                      TREE_VALUE (elts));
  594.       /* Say what index to use when we use that vtable.  */
  595. #ifndef VTABLE_USES_MASK
  596.       vindex = build_int_2 (TREE_INT_CST_LOW (vindex) & ~(1 << (BITS_PER_WORD -1)), 0);
  597. #endif
  598.       TREE_VALUE (TREE_CHAIN (elts)) = vindex;
  599.     }
  600.     }
  601. }
  602.  
  603. /* Modify virtual function tables in lattice topped by T to
  604.    place FNDECL in tables which previously held BASE_FNDECL.
  605.    PFN is just FNDECL wrapped in an ADDR_EXPR, so that it
  606.    is suitable for placement directly into an initializer.
  607.  
  608.    All distinct virtual function tables that this type uses
  609.    must be updated.  */
  610. static void
  611. modify_vtable_entries (t, fndecl, base_fndecl, pfn)
  612.      tree t;
  613.      tree fndecl, base_fndecl, pfn;
  614. {
  615.   tree base_offset, offset;
  616.   tree context = DECL_CONTEXT (base_fndecl);
  617.   tree vfield = CLASSTYPE_VFIELD (t);
  618.   tree vfields, vbases;
  619.  
  620.   DECL_VCONTEXT (fndecl) = DECL_VCONTEXT (base_fndecl);
  621.  
  622.   if (DECL_CONTEXT (fndecl) == t
  623.       || !(TYPE_USES_VIRTUAL_BASECLASSES (t)
  624.        || TYPE_USES_MULTIPLE_INHERITANCE (t)))
  625.     offset = integer_zero_node;
  626.   else
  627.     {
  628.       tree assoc = virtual_member (DECL_CONTEXT (fndecl), CLASSTYPE_VBASECLASSES (t));
  629.       if (assoc == NULL_TREE)
  630.     assoc = assoc_value (DECL_CONTEXT (fndecl), t);
  631.       assert (assoc != NULL_TREE);
  632.       offset = ASSOC_OFFSET (assoc);
  633.     }
  634.  
  635.   /* For each layer of base class (i.e., the first base class, and each
  636.      virtual base class from that one), modify the virtual function table
  637.      of the derived class to contain the new virtual function.
  638.      A class has as many vfields as it has virtual base classes (total).  */
  639.   for (vfields = CLASSTYPE_VFIELDS (t); vfields; vfields = TREE_CHAIN (vfields))
  640.     {
  641.       int normal = 1;
  642.       tree assoc, this_offset;
  643.       tree base, path;
  644.  
  645.       /* Find the right base class for this derived class, call it BASE.  */
  646.       base = TREE_VALUE (vfields);
  647.  
  648.       if (base != context)
  649.     {
  650.       /* If BASE_FNDECL is not contained in the vtable accessed by
  651.          the vslot, don't try to modify the vtable.
  652.          
  653.          Virtual functions from virtual baseclasses are not in derived
  654.          virtual function tables.  This is an implementation decision;
  655.          it keeps there from being a combinatorial exposion in the
  656.          number of different vtables which must be maintained.  */
  657.  
  658.       if (get_base_distance (base, context, 0, 0) == -1)
  659.         continue;
  660.  
  661.       /* BASE_FNDECL is defined in a class derived from
  662.          the base class owning this VFIELD.  */
  663.     }
  664.       /* Get the path starting from the deepest base class CONTEXT
  665.      of T (i.e., first defn of BASE_FNDECL).  */
  666.       get_base_distance (context, t, 0, &path);
  667.  
  668.       /* Get our best approximation of what to use for constructing
  669.      the virtual function table for T.  */
  670.       do
  671.     {
  672.       /* Walk from base toward derived, stopping at the
  673.          most derived baseclass that matters.  */
  674.       if (TREE_VIA_VIRTUAL (path))
  675.         {
  676.           base = TREE_VALUE (path);
  677.           assoc = value_member (TYPE_MAIN_VARIANT (base), CLASSTYPE_VBASECLASSES (t));
  678.           break;
  679.         }
  680.       if (TREE_CHAIN (path) == NULL_TREE
  681.           || (CLASSTYPE_BASECLASS (TREE_VALUE (TREE_CHAIN (path)), 1)
  682.           != TREE_VALUE (path))
  683.           || TREE_CHAIN (TREE_CHAIN (path)) == NULL_TREE)
  684.         {
  685.           base = TREE_VALUE (path);
  686.           assoc = assoc_value (TYPE_MAIN_VARIANT (base), t);
  687.           break;
  688.         }
  689.       path = TREE_CHAIN (path);
  690.     }
  691.       while (1);
  692.  
  693.       /* Find the right offset for the this pointer based on the base
  694.      class we just found.  */
  695.       base_offset = ASSOC_OFFSET (assoc);
  696.       if (base_offset == integer_zero_node)
  697.     this_offset = offset;
  698.       else
  699.     this_offset = genop (MINUS_EXPR, offset, base_offset);
  700.  
  701.       /* Make sure we can modify the derived association with immunity.  */
  702.       if (TREE_USED (CLASSTYPE_ASSOC (t)))
  703.     CLASSTYPE_ASSOC (t) = copy_assoc (CLASSTYPE_ASSOC (t));
  704.  
  705.       /* We call this case NORMAL iff this virtual function table
  706.      pointer field has its storage reserved in this class.
  707.      This is normally the case without virtual baseclasses
  708.      or off-center multiple baseclasses.  */
  709.       normal = (vfield != NULL_TREE
  710.         && TREE_VALUE (vfields) == DECL_FCONTEXT (vfield)
  711.         && (TREE_PURPOSE (vfields) == NULL_TREE
  712.             || ! TREE_VIA_VIRTUAL (TREE_PURPOSE (vfields))));
  713.  
  714.       if (normal && TREE_PURPOSE (vfields))
  715.     /* Everything looks normal so far...check that we are really
  716.        working from VFIELD's basetype, and not some other appearance
  717.        of that basetype in the lattice.  */
  718.     normal = (TREE_PURPOSE (vfields) == get_base_type (TREE_VALUE (vfields), t, 0));
  719.  
  720.       if (normal)
  721.     {
  722.       /* In this case, it is *type*'s vtable we are modifying.  */
  723.       context = t;
  724.       if (! CLASSTYPE_MARKED4 (t))
  725.         build_vtable (assoc, t);
  726.       assoc = CLASSTYPE_ASSOC (t);
  727.     }
  728.       else
  729.     {
  730.       /* This is our very own copy of `basetype' to play with.  */
  731.       if (! CLASSTYPE_MARKED4 (ASSOC_TYPE (assoc)))
  732.         prepare_fresh_vtable (assoc, CLASSTYPE_ASSOC (base), t);
  733.     }
  734.  
  735.       modify_vtable_entry (get_vtable_entry (ASSOC_VIRTUALS (assoc), base_fndecl),
  736.                build_vtable_entry (this_offset, pfn),
  737.                fndecl, context);
  738.     }
  739.   for (vbases = CLASSTYPE_VBASECLASSES (t); vbases; vbases = TREE_CHAIN (vbases))
  740.     {
  741.       tree this_offset;
  742.       tree base, path;
  743.  
  744.       if (! ASSOC_VTABLE (vbases))
  745.     /* There are only two ways that a type can fail to have
  746.        virtual functions: neither it nor any of its base
  747.        types define virtual functions (in which case
  748.        no updating need be done), or virtual functions
  749.        accessible to it come from virtual base classes
  750.        (in which case we have or will get them modified
  751.        in other passes of this loop).  */
  752.     continue;
  753.  
  754.       base = TREE_VALUE (vbases);
  755.       path = NULL_TREE;
  756.  
  757.       if (base != context
  758.       && get_base_distance (context, base, 0, &path) == -1)
  759.     continue;
  760.  
  761.       /* Doesn't matter if not actually from this virtual base class,
  762.          but shouldn't come from deeper virtual baseclasses.  The enclosing
  763.      loop should take care of such baseclasses.  */
  764.       while (path)
  765.     {
  766.       if (TREE_VIA_VIRTUAL (path))
  767.         goto skip;
  768.       path = TREE_CHAIN (path);
  769.     }
  770.  
  771.       base_offset = ASSOC_OFFSET (vbases);
  772.       if (base_offset == integer_zero_node)
  773.     this_offset = offset;
  774.       else
  775.     this_offset = genop (MINUS_EXPR, offset, base_offset);
  776.  
  777.       /* Make sure we can modify the derived association with immunity.  */
  778.       if (TREE_USED (CLASSTYPE_ASSOC (t)))
  779.     CLASSTYPE_ASSOC (t) = copy_assoc (CLASSTYPE_ASSOC (t));
  780.  
  781.       /* This is our very own copy of `basetype' to play with.  */
  782.       if (! CLASSTYPE_MARKED4 (ASSOC_TYPE (vbases)))
  783.     {
  784.       tree context_assoc = assoc_value (context, base);
  785.       prepare_fresh_vtable (vbases, context_assoc, t);
  786.     }
  787.       modify_vtable_entry (get_vtable_entry (ASSOC_VIRTUALS (vbases), base_fndecl),
  788.                build_vtable_entry (this_offset, pfn),
  789.                fndecl, context);
  790.     skip: {}
  791.     }
  792. }
  793.  
  794. static tree
  795. add_virtual_function (pending_virtuals, has_virtual, x, first)
  796.      tree pending_virtuals;
  797.      int *has_virtual;
  798.      tree x;
  799.      int first;
  800. {
  801.   int debug_vbase = 1;
  802.  
  803.   /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
  804.      convert to void *.  Make such a conversion here.  */
  805.   tree vfn = build1 (ADDR_EXPR, ptr_type_node, x);
  806.   TREE_LITERAL (vfn) = 1;
  807.   TREE_ADDRESSABLE (x) = CLASSTYPE_VTABLE_NEEDS_WRITING (current_class_type);
  808.  
  809.   /* If the virtual function is a redefinition of a prior one,
  810.      figure out in which base class the new definition goes,
  811.      and if necessary, make a fresh virtual function table
  812.      to hold that entry.  */
  813.   if (DECL_VINDEX (x) == NULL_TREE)
  814.     {
  815.       tree entry = build_vtable_entry (integer_zero_node, vfn);
  816.  
  817.       /* Build a new INT_CST for this DECL_VINDEX.  */
  818. #ifdef VTABLE_USES_MASK
  819.       SET_DECL_VINDEX (x, build_int_2 (++(*has_virtual), 0));
  820. #else
  821.       SET_DECL_VINDEX (x, build_int_2 (((1 << (BITS_PER_WORD - 1)) | ++(*has_virtual)), ~0));
  822. #endif
  823.       pending_virtuals = tree_cons (DECL_VINDEX (x), entry, pending_virtuals);
  824.     }
  825.   /* Happens if declared twice in class.  We will give error
  826.      later.  */
  827.   else if (TREE_CODE (DECL_VINDEX (x)) == INTEGER_CST)
  828.     return pending_virtuals;
  829.   else if (debug_vbase && TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
  830.     {
  831.       /* Need an entry in some other virtual function table.
  832.          Deal with this after we have laid out our virtual base classes.  */
  833.       pending_hard_virtuals = temp_tree_cons (x, vfn, pending_hard_virtuals);
  834.     }
  835.   else
  836.     {
  837.       /* Need an entry in some other virtual function table.
  838.          We can do this now.  */
  839.       tree base_fndecl_list = DECL_VINDEX (x), base_fndecls, prev = 0;
  840.       tree vtable_context = DECL_FCONTEXT (CLASSTYPE_VFIELD (current_class_type));
  841.       tree true_base_fndecl = 0;
  842.  
  843.       /* First assign DECL_VINDEX from the base vfn with which
  844.      we share our vtable.  */
  845.       base_fndecls = base_fndecl_list;
  846.       while (base_fndecls)
  847.     {
  848.       if (TREE_CHAIN (base_fndecls) == NULL_TREE
  849.           || DECL_FCONTEXT (CLASSTYPE_VFIELD (DECL_CONTEXT (TREE_VALUE (base_fndecls)))) == vtable_context)
  850.         {
  851.           true_base_fndecl = TREE_VALUE (base_fndecls);
  852.           modify_vtable_entries (current_class_type, x,
  853.                      true_base_fndecl, vfn);
  854.           if (prev)
  855.         TREE_CHAIN (prev) = TREE_CHAIN (base_fndecls);
  856.           else
  857.         base_fndecl_list = prev;
  858.           break;
  859.         }
  860.       prev = base_fndecls;
  861.       base_fndecls = TREE_CHAIN (base_fndecls);
  862.     }
  863.  
  864.       /* Now fill in the rest of the vtables.  */
  865.       base_fndecls = base_fndecl_list;
  866.       while (base_fndecls)
  867.     {
  868.       /* If we haven't found one we like, first one wins.  */
  869.       if (true_base_fndecl == 0)
  870.         true_base_fndecl = TREE_VALUE (base_fndecls);
  871.  
  872.       modify_vtable_entries (current_class_type, x,
  873.                  TREE_VALUE (base_fndecls), vfn);
  874.       base_fndecls = TREE_CHAIN (base_fndecls);
  875.     }
  876.  
  877.       DECL_VCONTEXT (x) = DECL_VCONTEXT (true_base_fndecl);
  878.     }
  879.   return pending_virtuals;
  880. }
  881.  
  882. /* Obstack on which to build the vector of class methods.  */
  883. struct obstack class_obstack;
  884. extern struct obstack *current_obstack;
  885.  
  886. /* Add method METHOD to class TYPE.  This is used when a method
  887.    has been defined which did not initially appear in the class definition,
  888.    and helps cut down on spurious error messages.
  889.  
  890.    FIELDS is the entry in the METHOD_VEC vector entry of the class type where
  891.    the method should be added.  */
  892. void
  893. add_method (type, fields, method)
  894.      tree type, *fields, method;
  895. {
  896.   /* We must make a copy of METHOD here, since we must be sure that
  897.      we have exclusive title to this method's TREE_CHAIN.  */
  898.   int temp = allocation_temporary_p ();
  899.   tree decl;
  900.  
  901.   if (temp)
  902.     end_temporary_allocation ();
  903.   {
  904.     decl = copy_node (method);
  905.     if (DECL_RTL (decl) == 0)
  906.       make_function_rtl (decl);
  907.   }
  908.  
  909.   if (fields && *fields)
  910.     {
  911.       /* Take care not to hide destructor.  */
  912.       TREE_CHAIN (decl) = TREE_CHAIN (*fields);
  913.       TREE_CHAIN (*fields) = decl;
  914.     }
  915.   else if (CLASSTYPE_METHOD_VEC (type) == 0)
  916.     {
  917.       tree method_vec = make_node (TREE_VEC);
  918.       if (DECL_NAME (TYPE_NAME (type)) == DECL_ORIGINAL_NAME (decl))
  919.     {
  920.       TREE_VEC_ELT (method_vec, 0) = decl;
  921.       TREE_VEC_LENGTH (method_vec) = 1;
  922.     }
  923.       else
  924.     {
  925.       obstack_free (current_obstack, method_vec);
  926.       obstack_blank (current_obstack, sizeof (struct tree_vec) + sizeof (tree *));
  927.       TREE_VEC_ELT (method_vec, 1) = decl;
  928.       TREE_VEC_LENGTH (method_vec) = 2;
  929.       obstack_finish (current_obstack);
  930.     }
  931.       CLASSTYPE_METHOD_VEC (type) = method_vec;
  932.     }
  933.   else
  934.     {
  935.       tree method_vec = CLASSTYPE_METHOD_VEC (type);
  936.       int len = TREE_VEC_LENGTH (method_vec);
  937.  
  938.       /* Adding a new ctor or dtor.  */
  939.       if (DECL_NAME (TYPE_NAME (type)) == DECL_ORIGINAL_NAME (decl))
  940.     TREE_VEC_ELT (method_vec, 0) = decl;
  941.       else
  942.     {
  943.       tree *end = (tree *)obstack_next_free (&class_obstack);
  944.       if (end != TREE_VEC_END (method_vec))
  945.         {
  946.           tree tmp_vec = copy_node (method_vec);
  947.           obstack_copy (current_obstack, &TREE_VEC_ELT (method_vec, 1), len);
  948.           obstack_blank (current_obstack, sizeof (tree *));
  949.           obstack_finish (current_obstack);
  950.           method_vec = tmp_vec;
  951.         }
  952.       else
  953.         {
  954.           /* We can easily extend the last such method_vec created.  */
  955.           obstack_free (&class_obstack, method_vec);
  956.           obstack_blank (&class_obstack,
  957.                  ((char *)end - (char *)method_vec) + sizeof (tree *));
  958.           method_vec = (tree)obstack_base (&class_obstack);
  959.           obstack_finish (&class_obstack);
  960.         }
  961.       TREE_VEC_ELT (method_vec, len) = decl;
  962.       TREE_VEC_LENGTH (method_vec) = len + 1;
  963.       CLASSTYPE_METHOD_VEC (type) = method_vec;
  964.  
  965.       if (CLASSTYPE_BASELINK_VEC (type))
  966.         {
  967.           /* ??? May be better to know whether these can be extended?  */
  968.           tree baselink_vec = copy_node (CLASSTYPE_BASELINK_VEC (type));
  969.  
  970.           obstack_copy (current_obstack, &TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), 1), len);
  971.           TREE_VEC_ELT (baselink_vec, len) = 0;
  972.           TREE_VEC_LENGTH (baselink_vec) = len + 1;
  973.           CLASSTYPE_BASELINK_VEC (type) = baselink_vec;
  974.         }
  975.     }
  976.     }
  977.   DECL_CONTEXT (decl) = type;
  978.   DECL_VCONTEXT (decl) = type;
  979.  
  980.   if (temp)
  981.     resume_temporary_allocation ();
  982. }
  983.  
  984. /* Subroutines of finish_struct.  */
  985.  
  986. /* Look through the list of fields for this struct, deleting
  987.    duplicates as we go.  This must be recursive to handle
  988.    anonymous unions.
  989.  
  990.    FIELD is the field which may not appear anywhere in FIELDS.
  991.    FIELD_PTR, if non-null, is the starting point at which
  992.    chained deletions may take place.
  993.    The value returned is the first acceptable entry found
  994.    in FIELDS.
  995.  
  996.    Note that anonymous fields which are not of UNION_TYPE are
  997.    not duplicates, they are just anonymous fields.  This happens
  998.    when we have unnamed bitfields, for example.  */
  999. static tree
  1000. delete_duplicate_fields_1 (field, field_ptr, fields)
  1001.      tree field, *field_ptr, fields;
  1002. {
  1003.   tree x;
  1004.   tree prev = field_ptr ? *field_ptr : 0;
  1005.   if (DECL_NAME (field) == 0)
  1006.     {
  1007.       if (TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)
  1008.     return fields;
  1009.  
  1010.       for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
  1011.     fields = delete_duplicate_fields_1 (x, field_ptr, fields);
  1012.       if (prev)
  1013.     TREE_CHAIN (prev) = fields;
  1014.       return fields;
  1015.     }
  1016.   else
  1017.     {
  1018.       for (x = fields; x; prev = x, x = TREE_CHAIN (x))
  1019.     {
  1020.       if (DECL_NAME (x) == 0)
  1021.         {
  1022.           if (TREE_CODE (TREE_TYPE (x)) != UNION_TYPE)
  1023.         continue;
  1024.           TYPE_FIELDS (TREE_TYPE (x))
  1025.         = delete_duplicate_fields_1 (field, 0, TYPE_FIELDS (TREE_TYPE (x)));
  1026.           if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
  1027.         {
  1028.           if (prev == 0)
  1029.             fields = TREE_CHAIN (fields);
  1030.           else
  1031.             TREE_CHAIN (prev) = TREE_CHAIN (x);
  1032.         }
  1033.         }
  1034.       else
  1035.         {
  1036.           if (DECL_NAME (field) == DECL_NAME (x))
  1037.         {
  1038.           if (TREE_CODE (field) == CONST_DECL
  1039.               && TREE_CODE (x) == CONST_DECL)
  1040.             error_with_decl (x, "duplicate enum value `%s'");
  1041.           else if (TREE_CODE (field) == CONST_DECL
  1042.                || TREE_CODE (x) == CONST_DECL)
  1043.             error_with_decl (x, "duplicate field `%s' (as enum and non-enum)");
  1044.           else
  1045.             error_with_decl (x, "duplicate member `%s'");
  1046.           if (prev == 0)
  1047.             fields = TREE_CHAIN (fields);
  1048.           else
  1049.             TREE_CHAIN (prev) = TREE_CHAIN (x);
  1050.         }
  1051.         }
  1052.     }
  1053.     }
  1054.   return fields;
  1055. }
  1056.  
  1057. static void
  1058. delete_duplicate_fields (fields)
  1059.      tree fields;
  1060. {
  1061.   tree x;
  1062.   for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
  1063.     TREE_CHAIN (x) = delete_duplicate_fields_1 (x, &x, TREE_CHAIN (x));
  1064. }
  1065.  
  1066. /* Add OFFSET to all child types of T.
  1067.  
  1068.    OFFSET, which is a type offset, is number of bytes.
  1069.  
  1070.    Note that we don't have to worry about having two paths to the
  1071.    same base type, since this type owns its association list.  */
  1072. static void
  1073. propagate_basetype_offsets (for_type, t, offset)
  1074.      tree for_type, t;
  1075.      tree offset;
  1076. {
  1077.   int i, n_baselinks = CLASSTYPE_N_BASECLASSES (t);
  1078.  
  1079.   for (i = 1; i <= n_baselinks; i++)
  1080.     if (! CLASSTYPE_VIA_VIRTUAL (t, i))
  1081.       {
  1082.     int j;
  1083.     tree basetype = CLASSTYPE_BASECLASS (t, i);
  1084.     tree assoc = assoc_value (TYPE_MAIN_VARIANT (basetype), for_type);
  1085.     tree delta;
  1086.  
  1087.     for (j = i+1; j <= n_baselinks; j++)
  1088.       if (! CLASSTYPE_VIA_VIRTUAL (t, j))
  1089.         {
  1090.           /* The next basetype offset must take into account the space
  1091.          between the classes, not just the size of each class.  */
  1092.           delta = genop (MINUS_EXPR,
  1093.                  CLASSTYPE_OFFSET (CLASSTYPE_BASECLASS (t, j)),
  1094.                  CLASSTYPE_OFFSET (basetype));
  1095.           break;
  1096.         }
  1097.  
  1098.     if (CLASSTYPE_OFFSET (basetype) == integer_zero_node)
  1099.       basetype = build_classtype_variant (basetype, offset, 0);
  1100.     else
  1101.       basetype = build_classtype_variant (basetype,
  1102.                           genop (PLUS_EXPR, CLASSTYPE_OFFSET (basetype), offset), 0);
  1103.     /* Now make our own copy of this base type we can munge.  */
  1104.     basetype = copy_node (basetype);
  1105.     copy_type_lang_specific (basetype);
  1106.  
  1107.     CLASSTYPE_BASECLASS (t, i) = basetype;
  1108.     ASSOC_TYPE (assoc) = basetype;
  1109.     ASSOC_OFFSET (assoc) = CLASSTYPE_OFFSET (basetype);
  1110.     TYPE_NAME (basetype) = copy_node (TYPE_NAME (basetype));
  1111.     TREE_TYPE (TYPE_NAME (basetype)) = basetype;
  1112.     DECL_OFFSET (TYPE_NAME (basetype))
  1113.       = TREE_INT_CST_LOW (CLASSTYPE_OFFSET (basetype)) * BITS_PER_UNIT;
  1114.     propagate_basetype_offsets (for_type, basetype, offset);
  1115.  
  1116.     /* Go to our next class that counts for offset propagation.  */
  1117.     i = j;
  1118.     if (i <= n_baselinks)
  1119.       offset = genop (PLUS_EXPR, offset, delta);
  1120.       }
  1121. }
  1122.  
  1123. /* Change the visibility of T::FDECL to VISIBILITY.
  1124.    Return 1 if change was legit, otherwise return 0.  */
  1125. static int
  1126. alter_visibility (t, fdecl, visibility)
  1127.      tree t;
  1128.      tree fdecl;
  1129.      enum visibility_type visibility;
  1130. {
  1131.   tree elem = purpose_member (t, DECL_VISIBILITY (fdecl));
  1132.   if (elem && TREE_VALUE (elem) != (tree)visibility)
  1133.     {
  1134.       if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
  1135.     {
  1136.       error_with_decl (TREE_TYPE (fdecl), "conflicting visibility specifications for method `%s', ignored");
  1137.     }
  1138.       else error ("conflicting visibility specifications for field `%s', ignored", IDENTIFIER_POINTER (DECL_NAME (fdecl)));
  1139.     }
  1140.   else if (TREE_PRIVATE (fdecl) && visibility != visibility_private)
  1141.     error_with_decl (fdecl, "cannot make private %s non-private");
  1142.   else if (TREE_PROTECTED (fdecl) && visibility == visibility_public)
  1143.             
  1144.     error_with_decl (fdecl, "cannot make protected %s public");
  1145.   else if (elem == NULL_TREE)
  1146.     {
  1147.       DECL_VISIBILITY (fdecl) = tree_cons (t, (tree)visibility,
  1148.                        DECL_VISIBILITY (fdecl));
  1149.       return 1;
  1150.     }
  1151.   return 0;
  1152. }
  1153.  
  1154. /* If FOR_TYPE needs to reinitialize virtual function table pointers
  1155.    for TYPE's sub-objects, add such reinitializations to BASE_INIT_LIST.
  1156.    Returns BASE_INIT_LIST appropriately modified.  */
  1157.  
  1158. static tree
  1159. maybe_fixup_vptrs (for_type, type, base_init_list)
  1160.      tree for_type, type, base_init_list;
  1161. {
  1162.   /* Now reinitialize any slots that don't fall under our virtual
  1163.      function table pointer.  */
  1164.   tree vfields = CLASSTYPE_VFIELDS (type);
  1165.   while (vfields)
  1166.     {
  1167.       tree basetype = get_base_type (TREE_VALUE (vfields), for_type, 0);
  1168.       if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (basetype)
  1169.       && ((DECL_OFFSET (CLASSTYPE_VFIELD (basetype))
  1170.            + DECL_OFFSET (TYPE_NAME (basetype)))
  1171.           != DECL_OFFSET (CLASSTYPE_VFIELD (for_type)))
  1172.       && ((DECL_OFFSET (CLASSTYPE_VFIELD (basetype))
  1173.            + DECL_OFFSET (TYPE_NAME (basetype)))
  1174.           != (DECL_OFFSET (CLASSTYPE_VFIELD (type))
  1175.           + DECL_OFFSET (TYPE_NAME (type)))))
  1176.     base_init_list = tree_cons (error_mark_node, basetype,
  1177.                     base_init_list);
  1178.       vfields = TREE_CHAIN (vfields);
  1179.     }
  1180.   return base_init_list;
  1181. }
  1182.  
  1183. /* If TYPE does not have a constructor, then the compiler must
  1184.    manually deal with all of the initialization this type requires.
  1185.  
  1186.    If a base initializer exists only to fill in the virtual function
  1187.    table pointer, then we mark that fact with the TREE_VIRTUAL bit.
  1188.    This way, we avoid multiple initializations of the same field by
  1189.    each virtual function table up the class hierarchy.
  1190.  
  1191.    Virtual base class pointers are not initialized here.  They are
  1192.    initialized only at the "top level" of object creation.  If we
  1193.    initialized them here, we would have to skip a lot of work.  */
  1194.  
  1195. static void
  1196. build_class_init_list (type)
  1197.      tree type;
  1198. {
  1199.   tree base_init_list = NULL_TREE;
  1200.   tree member_init_list = NULL_TREE;
  1201.  
  1202.   /* Since we build member_init_list and base_init_list using
  1203.      tree_cons, backwards fields the all through work.  */
  1204.   tree x;
  1205.   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (type);
  1206.  
  1207.   for (x = TYPE_FIELDS (type); x; x = TREE_CHAIN (x))
  1208.     {
  1209.       if (TREE_CODE (x) != FIELD_DECL)
  1210.     continue;
  1211.  
  1212.       if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (x))
  1213.       || DECL_INITIAL (x) != NULL_TREE)
  1214.     member_init_list = tree_cons (x, type, member_init_list);
  1215.     }
  1216.   member_init_list = nreverse (member_init_list);
  1217.  
  1218.   /* We will end up doing this last.  Need special marker
  1219.      to avoid infinite regress.  */
  1220.   if (TYPE_VIRTUAL_P (type))
  1221.     {
  1222.       base_init_list = build_tree_list (error_mark_node, type);
  1223.       if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (type) == 0)
  1224.     TREE_VALUE (base_init_list) = NULL_TREE;
  1225.       TREE_ADDRESSABLE (base_init_list) = 1;
  1226.     }
  1227.  
  1228.   /* Each base class which needs to have initialization
  1229.      of some kind gets to make such requests known here.  */
  1230.   for (i = n_baseclasses; i > 0; i--)
  1231.     {
  1232.       tree basetype = CLASSTYPE_BASECLASS (type, i);
  1233.       tree blist;
  1234.  
  1235.       /* Don't initialize virtual baseclasses this way.  */
  1236.       if (TREE_VIA_VIRTUAL (basetype))
  1237.     continue;
  1238.  
  1239.       if (TYPE_HAS_CONSTRUCTOR (basetype))
  1240.     {
  1241.       /* ...and the last shall come first...  */
  1242.       base_init_list = maybe_fixup_vptrs (type, basetype, base_init_list);
  1243.       base_init_list = tree_cons (NULL_TREE, basetype,
  1244.                       base_init_list);
  1245.       continue;
  1246.     }
  1247.  
  1248.       if ((blist = CLASSTYPE_BASE_INIT_LIST (basetype)) == NULL_TREE)
  1249.     /* Nothing to initialize.  */
  1250.     continue;
  1251.  
  1252.       /* ...ditto...  */
  1253.       base_init_list = maybe_fixup_vptrs (type, basetype, base_init_list);
  1254.  
  1255.       /* This is normally true for single inheritance.
  1256.      The win is we can shrink the chain of initializations
  1257.      to be done by only converting to the actual type
  1258.      we are interested in.  */
  1259.       if (TREE_VALUE (blist)
  1260.       && TREE_CODE (TREE_VALUE (blist)) == RECORD_TYPE
  1261.       && (DECL_OFFSET (TYPE_NAME (basetype))
  1262.           == DECL_OFFSET (TYPE_NAME (TREE_VALUE (blist)))))
  1263.     {
  1264.       if (base_init_list)
  1265.         {
  1266.           /* Does it do more than just fill in a
  1267.          virtual function table pointer?  */
  1268.           if (! TREE_ADDRESSABLE (blist))
  1269.         base_init_list = build_tree_list (blist, base_init_list);
  1270.           /* Can we get by just with the virtual function table
  1271.          pointer that it fills in?  */
  1272.           else if (TREE_ADDRESSABLE (base_init_list)
  1273.                && TREE_VALUE (base_init_list) == 0)
  1274.         base_init_list = blist;
  1275.           /* Maybe, but it is not obvious as the previous case.  */
  1276.           else if (! CLASSTYPE_NEEDS_VIRTUAL_REINIT (type))
  1277.         {
  1278.           tree last = tree_last (base_init_list);
  1279.           while (TREE_VALUE (last)
  1280.              && TREE_CODE (TREE_VALUE (last)) == TREE_LIST)
  1281.             last = tree_last (TREE_VALUE (last));
  1282.           if (TREE_VALUE (last) == 0)
  1283.             base_init_list = build_tree_list (blist, base_init_list);
  1284.         }
  1285.         }
  1286.       else
  1287.         base_init_list = blist;
  1288.     }
  1289.       else
  1290.     {
  1291.       /* The function expand_aggr_init knows how to do the
  1292.          initialization of `basetype' without getting
  1293.          an explicit `blist'.  */
  1294.       if (base_init_list)
  1295.         base_init_list = tree_cons (NULL_TREE, basetype, base_init_list);
  1296.       else
  1297.         base_init_list = CLASSTYPE_AS_LIST (basetype);
  1298.     }
  1299.     }
  1300.  
  1301.   if (base_init_list)
  1302.     if (member_init_list)
  1303.       CLASSTYPE_BASE_INIT_LIST (type) = build_tree_list (base_init_list, member_init_list);
  1304.     else
  1305.       CLASSTYPE_BASE_INIT_LIST (type) = base_init_list;
  1306.   else if (member_init_list)
  1307.     CLASSTYPE_BASE_INIT_LIST (type) = member_init_list;
  1308. }
  1309.  
  1310. struct base_info
  1311. {
  1312.   int has_virtual;
  1313.   int max_has_virtual;
  1314.   int n_ancestors;
  1315.   tree vfield;
  1316.   tree vfields;
  1317.   char needs_default_ctor;
  1318.   char cant_have_default_ctor;
  1319.   char needs_const_ctor;
  1320.   char cant_have_const_ctor;
  1321. };
  1322.  
  1323. /* Record information about type T derived from its base classes.
  1324.    Store most of that information in T itself, and place the
  1325.    remaining information in the struct BASE_INFO.
  1326.  
  1327.    Returns the index of the first base class to have virtual functions,
  1328.    or zero if no such base class.  */
  1329.  
  1330. static int
  1331. finish_base_struct (t, b)
  1332.      tree t;
  1333.      struct base_info *b;
  1334. {
  1335.   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
  1336.   int first_vfn_base_index = 0;
  1337.  
  1338.   bzero (b, sizeof (struct base_info));
  1339.  
  1340.   for (i = 1; i <= n_baseclasses; i++)
  1341.     {
  1342.       tree basetype = CLASSTYPE_BASECLASS (t, i);
  1343.  
  1344.       /* If the type of basetype is incomplete, then
  1345.      we already complained about that fact
  1346.      (and we should have fixed it up as well).  */
  1347.       if (TYPE_SIZE (basetype) == 0)
  1348.     {
  1349.       int j;
  1350.       /* The base type is of incomplete type.  It is
  1351.          probably best to pretend that it does not
  1352.          exist.  */
  1353.       if (i == n_baseclasses)
  1354.         CLASSTYPE_BASECLASS (t, i) = NULL_TREE;
  1355.       CLASSTYPE_N_BASECLASSES (t) -= 1;
  1356.       n_baseclasses -= 1;
  1357.       for (j = i; j < n_baseclasses; j++)
  1358.         {
  1359.           CLASSTYPE_BASECLASS (t, j) = CLASSTYPE_BASECLASS (t, j+1);
  1360.           SET_CLASSTYPE_VIAS (t, j,
  1361.                   CLASSTYPE_VIA_PUBLIC (t, j+1),
  1362.                   CLASSTYPE_VIA_VIRTUAL (t, j+1));
  1363.         }
  1364.     }
  1365.  
  1366.       if (TYPE_WRAP_TYPE (t) == NULL_TREE)
  1367.     TYPE_WRAP_TYPE (t) = TYPE_WRAP_TYPE (basetype);
  1368.       else if (TYPE_WRAP_TYPE (basetype)
  1369.            && TYPE_WRAP_TYPE (t) != TYPE_WRAP_TYPE (basetype))
  1370.     /* Must have its own.  */
  1371.     TYPE_WRAP_TYPE (t) = error_mark_node;
  1372.  
  1373.       if (TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
  1374.     b->needs_default_ctor = 1;
  1375.       else if (TYPE_HAS_CONSTRUCTOR (basetype))
  1376.     b->cant_have_default_ctor = 1;
  1377.       if (TYPE_GETS_CONST_INIT_REF (basetype))
  1378.     b->needs_const_ctor = 1;
  1379.       else if (TYPE_GETS_INIT_REF (basetype))
  1380.     b->cant_have_const_ctor = 1;
  1381.  
  1382.       CLASSTYPE_ALTERS_VISIBILITIES_P (t)
  1383.     |= CLASSTYPE_ALTERS_VISIBILITIES_P (basetype);
  1384.  
  1385.       b->n_ancestors += CLASSTYPE_N_SUPERCLASSES (basetype);
  1386.       TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
  1387.       TYPE_NEEDS_CONSTRUCTOR (t) |= TYPE_NEEDS_CONSTRUCTOR (basetype);
  1388.       TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
  1389.       TYPE_ANY_ASSIGNS_THIS (t) |= TYPE_ANY_ASSIGNS_THIS (basetype);
  1390.       TYPE_GETS_ASSIGNMENT (t) |= TYPE_GETS_ASSIGNMENT (basetype);
  1391.       TYPE_GETS_INIT_REF (t) |= TYPE_GETS_INIT_REF (basetype);
  1392.  
  1393.       TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
  1394.       TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
  1395.  
  1396.       if (CLASSTYPE_OFFSET (basetype) != integer_zero_node)
  1397.     {
  1398.       /* Completely unshare potentially shared data, and
  1399.          update what is ours.  */
  1400.       tree assoc = assoc_value (TYPE_MAIN_VARIANT (basetype), t);
  1401.       basetype = copy_node (basetype);
  1402.       copy_type_lang_specific (basetype);
  1403.       CLASSTYPE_BASECLASS (t, i) = basetype;
  1404.       ASSOC_TYPE (assoc) = basetype;
  1405.  
  1406.       /* Propagate this offset through all the children.  Do this
  1407.          before uniquizing baseclasses for virtual functions.  */
  1408.       CLASSTYPE_ASSOC (basetype) = copy_assoc (CLASSTYPE_ASSOC (TYPE_MAIN_VARIANT (basetype)));
  1409.  
  1410.       propagate_basetype_offsets (basetype, basetype, CLASSTYPE_OFFSET (basetype));
  1411.     }
  1412.  
  1413.       if (! CLASSTYPE_VIA_VIRTUAL (t, i))
  1414.     CLASSTYPE_N_SUPERCLASSES (t) += 1;
  1415.  
  1416.       if (TYPE_VIRTUAL_P (basetype))
  1417.     {
  1418.       if (CLASSTYPE_VSIZE (basetype) > b->max_has_virtual)
  1419.         b->max_has_virtual = CLASSTYPE_VSIZE (basetype);
  1420.  
  1421.       /* Don't borrow virtuals from virtual baseclasses.  */
  1422.       if (TREE_VIA_VIRTUAL (basetype))
  1423.         continue;
  1424.  
  1425.       if (first_vfn_base_index == 0)
  1426.         {
  1427.           first_vfn_base_index = i;
  1428.  
  1429.           b->has_virtual = CLASSTYPE_VSIZE (basetype);
  1430.           b->vfield = CLASSTYPE_VFIELD (basetype);
  1431.           b->vfields = CLASSTYPE_VFIELDS (basetype);
  1432.           CLASSTYPE_VFIELD (t) = b->vfield;
  1433.         }
  1434.       else
  1435.         {
  1436.           /* Only add unique vfields, and flatten them out as we go.  */
  1437.           tree vfields = CLASSTYPE_VFIELDS (basetype);
  1438.           while (vfields)
  1439.         {
  1440.           if (TREE_PURPOSE (vfields) == NULL_TREE
  1441.               || ! TREE_VIA_VIRTUAL (TREE_PURPOSE (vfields)))
  1442.             {
  1443.               tree value = TREE_VALUE (vfields);
  1444.               if (TYPE_MAIN_VARIANT (basetype) == value)
  1445.             b->vfields = tree_cons (basetype, value, b->vfields);
  1446.               else
  1447.             b->vfields = tree_cons (get_base_type (value, basetype, 0),
  1448.                         value, b->vfields);
  1449.               TREE_TYPE (b->vfields) = basetype;
  1450.             }
  1451.           vfields = TREE_CHAIN (vfields);
  1452.         }
  1453.  
  1454.           if (b->has_virtual == 0)
  1455.         {
  1456.           first_vfn_base_index = i;
  1457.           b->has_virtual = CLASSTYPE_VSIZE (basetype);
  1458.           b->vfield = CLASSTYPE_VFIELD (basetype);
  1459.           CLASSTYPE_VFIELD (t) = b->vfield;
  1460.         }
  1461.         }
  1462.     }
  1463.     }
  1464.   if (b->vfield == 0)
  1465.     /* If all virtual functions come only from virtual baseclasses.  */
  1466.     return 0;
  1467.   return first_vfn_base_index;
  1468. }
  1469.  
  1470. static int
  1471. typecode_p (type, code)
  1472.      tree type;
  1473.      enum tree_code code;
  1474. {
  1475.   return (TREE_CODE (type) == code
  1476.       || (TREE_CODE (type) == REFERENCE_TYPE
  1477.           && TREE_CODE (TREE_TYPE (type)) == code));
  1478. }
  1479.  
  1480. /* Set memoizing fields and bits of T (and its variants) for later use.
  1481.    FIRST_VFN_BASE_INDEX is the first baseclass of T with virtual functions.
  1482.    MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables.  */
  1483. static void
  1484. finish_struct_bits (t, first_vfn_base_index, max_has_virtual)
  1485.      tree t;
  1486.      int first_vfn_base_index, max_has_virtual;
  1487. {
  1488.   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
  1489.   tree method_vec = CLASSTYPE_METHOD_VEC (t);
  1490.  
  1491.   /* Fix up variants (if any).  */
  1492.   tree variants = TYPE_NEXT_VARIANT (t);
  1493.   while (variants)
  1494.     {
  1495.       TYPE_NEEDS_CONSTRUCTOR (variants) = TYPE_NEEDS_CONSTRUCTOR (t);
  1496.       TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
  1497.       TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
  1498.       variants = TYPE_NEXT_VARIANT (variants);
  1499.     }
  1500.  
  1501.   if (n_baseclasses && max_has_virtual)
  1502.     {
  1503.       /* Done by `finish_struct' for classes without baseclasses.  */
  1504.       int has_abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (t) != 0;
  1505.       if (has_abstract_virtuals == 0)
  1506.     for (i = CLASSTYPE_N_BASECLASSES (t); i >= 1; i--)
  1507.       has_abstract_virtuals
  1508.         |= (CLASSTYPE_ABSTRACT_VIRTUALS (CLASSTYPE_BASECLASS (t, i)) != 0);
  1509.       if (has_abstract_virtuals)
  1510.     CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
  1511.     }
  1512.  
  1513.   if (n_baseclasses)
  1514.     {
  1515.       /* Notice whether this class has type conversion functions defined.
  1516.      Also report whether joining two types yields an ambiguity in the
  1517.      virtual function table, e.g.,
  1518.      
  1519.      struct A { virtual int f (); };
  1520.      struct B { virtual int f (); };
  1521.      struct C : A, B { / * no f (); * / };    / / error, ambiguous
  1522.      */
  1523.       tree basetypes = CLASSTYPE_VBASECLASSES (t), basetype;
  1524.       int n_vbases = list_length (basetypes), j;
  1525.  
  1526.       build_mi_virtuals (n_baseclasses + (n_vbases*n_baseclasses), max_has_virtual);
  1527.       /* Fill in virutal function table with values which do not come
  1528.      "normal"ly, i.e., those which come from virtual and/or
  1529.      non-leftmost base classes.  */
  1530.       for (i = n_baseclasses; basetypes; basetypes = TREE_CHAIN (basetypes))
  1531.     {
  1532.       basetype = TREE_VALUE (basetypes);
  1533.       if (CLASSTYPE_VSIZE (basetype))
  1534.         for (j = n_baseclasses; j > 0; j--)
  1535.           {
  1536.         tree this_base = CLASSTYPE_BASECLASS (t, j);
  1537.         if (get_base_distance (basetype, TYPE_MAIN_VARIANT (this_base), 0, 0) != -1)
  1538.           add_mi_virtuals (++i, TREE_CHAIN (ASSOC_VIRTUALS (basetypes)));
  1539.           }
  1540.     }
  1541.       for (i = n_baseclasses; i > 0; i--)
  1542.     {
  1543.       basetype = CLASSTYPE_BASECLASS (t, i);
  1544.  
  1545.       if (TYPE_HAS_CONVERSION (basetype))
  1546.         {
  1547.           TYPE_HAS_CONVERSION (t) = 1;
  1548.           TYPE_HAS_INT_CONVERSION (t) |= TYPE_HAS_INT_CONVERSION (basetype);
  1549.           TYPE_HAS_REAL_CONVERSION (t) |= TYPE_HAS_REAL_CONVERSION (basetype);
  1550.         }
  1551.       if (TREE_VIA_VIRTUAL (basetype))
  1552.         /* Virtual functions from virtual baseclasses are done above.  */;
  1553.       else if (i == first_vfn_base_index)
  1554.         add_mi_virtuals (i, TREE_CHAIN (CLASS_ASSOC_VIRTUALS (t)));
  1555.       else if (CLASSTYPE_VSIZE (basetype) != 0)
  1556.         add_mi_virtuals (i, TREE_CHAIN (CLASS_ASSOC_VIRTUALS (basetype)));
  1557.     }
  1558.       report_ambiguous_mi_virtuals (n_baseclasses + (n_vbases*n_baseclasses), t);
  1559. #if 0
  1560.       /* Now that we know what the virtual functiond table looks like,
  1561.      fix up offsets in the presence of virtual base classes.  */
  1562.       if (n_vbases)
  1563.     fixup_vbase_offsets (t);
  1564. #endif
  1565.     }
  1566.  
  1567.   /* Need to test METHOD_VEC here in case all methods
  1568.      (conversions and otherwise) are inherited.  */
  1569.   if (TYPE_HAS_CONVERSION (t) && method_vec != NULL_TREE)
  1570.     {
  1571.       tree first_conversions[last_conversion_type];
  1572.       tree last_conversions[last_conversion_type];
  1573.       enum conversion_type conv_index;
  1574.       tree *tmp;
  1575.       int i;
  1576.  
  1577.       bzero (first_conversions, sizeof (first_conversions));
  1578.       for (tmp = &TREE_VEC_ELT (method_vec, 1);
  1579.        tmp != TREE_VEC_END (method_vec); tmp += 1)
  1580.     {
  1581.       if (OPERATOR_TYPENAME_P (DECL_ORIGINAL_NAME (*tmp)))
  1582.         {
  1583.           tree fntype = TREE_TYPE (*tmp);
  1584.           tree return_type = TREE_TYPE (fntype);
  1585.           assert (TREE_CODE (fntype) == METHOD_TYPE);
  1586.  
  1587.           if (typecode_p (return_type, POINTER_TYPE))
  1588.         {
  1589.           if (TREE_READONLY (TREE_TYPE (return_type)))
  1590.             conv_index = constptr_conv;
  1591.           else
  1592.             conv_index = ptr_conv;
  1593.         }
  1594.           else if (typecode_p (return_type, INTEGER_TYPE))
  1595.         {
  1596.           TYPE_HAS_INT_CONVERSION (t) = 1;
  1597.           conv_index = int_conv;
  1598.         }
  1599.           else if (typecode_p (return_type, REAL_TYPE))
  1600.         {
  1601.           TYPE_HAS_REAL_CONVERSION (t) = 1;
  1602.           conv_index = real_conv;
  1603.         }
  1604.           else
  1605.         continue;
  1606.  
  1607.           if (first_conversions[(int) conv_index] == NULL_TREE)
  1608.         first_conversions[(int) conv_index] = *tmp;
  1609.           last_conversions[(int) conv_index] = *tmp;
  1610.         }
  1611.     }
  1612.  
  1613.       for (i = 0; i < (int) last_conversion_type; i++)
  1614.     if (first_conversions[i] != last_conversions[i])
  1615.       CLASSTYPE_CONVERSION (t, i) = error_mark_node;
  1616.     else
  1617.       CLASSTYPE_CONVERSION (t, i) = first_conversions[i];
  1618.     }
  1619.  
  1620.   /* If this type has constructors, force its mode to be BLKmode,
  1621.      and force its TREE_ADDRESSABLE bit to be nonzero.  */
  1622.   if (TYPE_NEEDS_CONSTRUCTING (t) || TYPE_NEEDS_DESTRUCTOR (t))
  1623.     {
  1624.       tree variants = t;
  1625.  
  1626.       if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
  1627.     DECL_MODE (TYPE_NAME (t)) = BLKmode;
  1628.       while (variants)
  1629.     {
  1630.       TYPE_MODE (variants) = BLKmode;
  1631.       TREE_ADDRESSABLE (variants) = 1;
  1632.       variants = TYPE_NEXT_VARIANT (variants);
  1633.     }
  1634.     }
  1635. }
  1636.  
  1637. /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
  1638.    (or C++ class declaration).
  1639.  
  1640.    For C++, we must handle the building of derived classes.
  1641.    Also, C++ allows static class members.  The way that this is
  1642.    handled is to keep the field name where it is (as the DECL_NAME
  1643.    of the field), and place the overloaded decl in the DECL_OFFSET
  1644.    of the field.  layout_record and layout_union will know about this.
  1645.  
  1646.    More C++ hair: inline functions have text in their
  1647.    DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
  1648.    meaningful tree structure.  After the struct has been laid out, set
  1649.    things up so that this can happen.
  1650.  
  1651.    And still more: virtual functions.  In the case of single inheritance,
  1652.    when a new virtual function is seen which redefines a virtual function
  1653.    from the base class, the new virtual function is placed into
  1654.    the virtual function table at exactly the same address that
  1655.    it had in the base class.  When this is extended to multiple
  1656.    inheritance, the same thing happens, except that multiple virtual
  1657.    function tables must be maintained.  The first virtual function
  1658.    table is treated in exactly the same way as in the case of single
  1659.    inheritance.  Additional virtual function tables have different
  1660.    DELTAs, which tell how to adjust `this' to point to the right thing.
  1661.  
  1662.    LIST_OF_FIELDLISTS is just that.  The elements of the list are
  1663.    TREE_LIST elements, whose TREE_PURPOSE field tells what visibility
  1664.    the list has, and the TREE_VALUE slot gives the actual fields.
  1665.  
  1666.    EMPTY is non-zero if this structure has no declarations following it.
  1667.  
  1668.    If flag_all_virtual == 1, then we lay all functions into
  1669.    the virtual function table, as though they were declared
  1670.    virtual.  Constructors do not lay down in the virtual function table.
  1671.  
  1672.    If flag_all_virtual == 2, then we lay all functions into
  1673.    the virtual function table, such that virtual functions
  1674.    occupy a space by themselves, and then all functions
  1675.    of the class occupy a space by themselves.  This is illustrated
  1676.    in the following diagram:
  1677.  
  1678.    class A; class B : A;
  1679.  
  1680.     Class A's vtbl:            Class B's vtbl:
  1681.     --------------------------------------------------------------------
  1682.    | A's virtual functions|        | B's virtual funcitions    |
  1683.    |              |        | (may inherit some from A).    |
  1684.     --------------------------------------------------------------------
  1685.    | All of A's functions |        | All of A's functions        |
  1686.    | (such as a->A::f).      |        | (such as b->A::f)        |
  1687.     --------------------------------------------------------------------
  1688.                     | B's new virtual functions    |
  1689.                     | (not defined in A.)        |
  1690.                      -------------------------------
  1691.                     | All of B's functions        |
  1692.                     | (such as b->B::f)        |
  1693.                      -------------------------------
  1694.  
  1695.    this allows the program to make references to any function, virtual
  1696.    or otherwise in a type-consistant manner.  */
  1697.  
  1698. tree
  1699. finish_struct (t, list_of_fieldlists, empty, warn_anon)
  1700.      tree t;
  1701.      tree list_of_fieldlists;
  1702.      int empty;
  1703.      int warn_anon;
  1704. {
  1705.   extern int interface_only, interface_unknown;
  1706.   int old;
  1707.   int round_up_size = 1;
  1708.   /* Set non-zero to debug using default functions.
  1709.      Not set by program.  */
  1710.   static int debug_default_functions = 0;
  1711.  
  1712.   enum tree_code code = TREE_CODE (t);
  1713.   register tree x, y, method_vec;
  1714.   int needs_ctor = 0, needs_dtor = 0;
  1715.   int members_need_dtors = 0;
  1716.   tree name = TYPE_NAME (t), fields, fn_fields, tail;
  1717.   enum visibility_type visibility;
  1718.   int all_virtual;
  1719.   int has_virtual;
  1720.   int max_has_virtual;
  1721.   tree pending_virtuals = NULL_TREE;
  1722.   tree abstract_virtuals = NULL_TREE;
  1723.   tree vfield;
  1724.   tree vfields;
  1725.   int needs_default_ctor;
  1726.   int cant_have_default_ctor;
  1727.   int needs_const_ctor;
  1728.   int cant_have_const_ctor;
  1729.  
  1730.   /* The index of the first base class which has virtual
  1731.      functions.  Only applied to non-virtual baseclasses.  */
  1732.   int first_vfn_base_index;
  1733.  
  1734.   int i, n_baseclasses;
  1735.   int any_default_members = 0;
  1736.   char *err_name;
  1737.   int const_sans_init = 0;
  1738.   int ref_sans_init = 0;
  1739.   int nonprivate_method = 0;
  1740.  
  1741.   if (TREE_CODE (name) == TYPE_DECL)
  1742.     {
  1743.       extern int lineno;
  1744.  
  1745.       DECL_SOURCE_FILE (name) = input_filename;
  1746.       DECL_SOURCE_LINE (name) = lineno;
  1747.       name = DECL_NAME (name);
  1748.     }
  1749.   err_name = IDENTIFIER_POINTER (name);
  1750.  
  1751.   if (warn_anon && code != UNION_TYPE && ANON_AGGRNAME_P (name))
  1752.     {
  1753.       warning ("un-usable class ignored (anonymous classes and unions are useless)");
  1754.       err_name = "(anon)";
  1755.     }
  1756.  
  1757.   leftmost_baseclasses = NULL_TREE;
  1758.   if (TYPE_SIZE (t))
  1759.     {
  1760.       if (TREE_CODE (t) == UNION_TYPE)
  1761.     error ("redefinition of `union %s'", err_name);
  1762.       else if (TREE_CODE (t) == RECORD_TYPE)
  1763.     error ("redefinition of `struct %s'", err_name);
  1764.       else
  1765.     assert (0);
  1766.       popclass (0);
  1767.       return t;
  1768.     }
  1769.  
  1770. #ifdef FIELD_XREF
  1771.   FIELD_xref_decl(current_function_decl,t);
  1772. #endif
  1773.  
  1774.   /* If this type was previously laid out as a forward reference,
  1775.      make sure we lay it out again.  */
  1776.  
  1777.   TYPE_SIZE (t) = 0;
  1778.   CLASSTYPE_GOT_SEMICOLON (t) = 0;
  1779.   CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
  1780.   CLASSTYPE_INTERFACE_UNKNOWN (t) = interface_unknown;
  1781.  
  1782.   old = suspend_momentary ();
  1783.  
  1784.   /* Install struct as DECL_FIELD_CONTEXT of each field decl.
  1785.      Also process specified field sizes.
  1786.      Set DECL_SIZE_UNIT to the specified size, or 0 if none specified.
  1787.      The specified size is found in the DECL_INITIAL.
  1788.      Store 0 there, except for ": 0" fields (so we can find them
  1789.      and delete them, below).  */
  1790.  
  1791.   n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
  1792.   if (n_baseclasses >= 1)
  1793.     {
  1794.       struct base_info base_info;
  1795.  
  1796.       /* If using multiple inheritance, this may cause variants of our
  1797.      basetypes to be used (instead of their canonical forms).  */
  1798.       fields = layout_basetypes (t);
  1799.       y = tree_last (fields);
  1800.  
  1801.       first_vfn_base_index = finish_base_struct (t, &base_info);
  1802.       has_virtual = base_info.has_virtual;
  1803.       max_has_virtual = base_info.max_has_virtual;
  1804.       CLASSTYPE_N_SUPERCLASSES (t) += base_info.n_ancestors;
  1805.       vfield = base_info.vfield;
  1806.       vfields = base_info.vfields;
  1807.       needs_default_ctor = base_info.needs_default_ctor;
  1808.       cant_have_default_ctor = base_info.cant_have_default_ctor;
  1809.       needs_const_ctor = base_info.needs_const_ctor;
  1810.       cant_have_const_ctor = base_info.cant_have_const_ctor;
  1811.       n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
  1812.     }
  1813.   else
  1814.     {
  1815.       first_vfn_base_index = 0;
  1816.       has_virtual = 0;
  1817.       max_has_virtual = 0;
  1818.       vfield = NULL_TREE;
  1819.       vfields = NULL_TREE;
  1820.       fields = NULL_TREE;
  1821.       y = NULL_TREE;
  1822.       needs_default_ctor = 0;
  1823.       cant_have_default_ctor = 0;
  1824.       needs_const_ctor = 0;
  1825.       cant_have_const_ctor = 0;
  1826.     }
  1827.  
  1828.   if (write_virtuals == 3 && ! CLASSTYPE_INTERFACE_UNKNOWN (t))
  1829.     {
  1830.       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
  1831.       CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! interface_only;
  1832.     }
  1833.  
  1834.   /* The three of these are approximations which may later be
  1835.      modified.  Needed at this point to make add_virtual_function
  1836.      and modify_vtable_entries work.  */
  1837.   CLASSTYPE_ASSOC (t) = make_assoc (integer_zero_node, t,
  1838.                     0, 0, CLASSTYPE_ASSOC (t));
  1839.   CLASSTYPE_VFIELDS (t) = vfields;
  1840.   CLASSTYPE_VFIELD (t) = vfield;
  1841.  
  1842.   fn_fields = NULL_TREE;
  1843.   tail = NULL_TREE;
  1844.   if (y && list_of_fieldlists)
  1845.     TREE_CHAIN (y) = TREE_VALUE (list_of_fieldlists);
  1846.  
  1847. #ifdef SOS
  1848.   if (flag_all_virtual == 2)
  1849.     all_virtual = 2;
  1850.   else
  1851. #endif
  1852.     {
  1853.       if (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (t))
  1854.     all_virtual = 1;
  1855.       else
  1856.     all_virtual = 0;
  1857.     }
  1858.  
  1859.   if (CLASSTYPE_DECLARED_CLASS (t) == 0)
  1860.     {
  1861.       nonprivate_method = 1;
  1862.       if (list_of_fieldlists
  1863.       && TREE_PURPOSE (list_of_fieldlists) == (tree)visibility_default)
  1864.     TREE_PURPOSE (list_of_fieldlists) = (tree)visibility_public;
  1865.     }
  1866.   else if (list_of_fieldlists
  1867.        && TREE_PURPOSE (list_of_fieldlists) == (tree)visibility_default)
  1868.     TREE_PURPOSE (list_of_fieldlists) = (tree)visibility_private;
  1869.  
  1870.   while (list_of_fieldlists)
  1871.     {
  1872.       visibility = (enum visibility_type)TREE_PURPOSE (list_of_fieldlists);
  1873.  
  1874.       for (x = TREE_VALUE (list_of_fieldlists); x; x = TREE_CHAIN (x))
  1875.     {
  1876.       TREE_PRIVATE (x) = visibility == visibility_private;
  1877.       TREE_PROTECTED (x) = visibility == visibility_protected;
  1878. #ifdef FIELD_XREF
  1879.       FIELD_xref_member(current_class_name,x);
  1880. #endif
  1881.  
  1882.       if (TREE_CODE (x) == FUNCTION_DECL)
  1883.         {
  1884.           /* Clear out this flag.
  1885.  
  1886.              @@ Doug may figure out how to break
  1887.          @@ this with nested classes and friends.  */
  1888.           DECL_IN_AGGR_P (x) = 0;
  1889.  
  1890.           nonprivate_method |= ! TREE_PRIVATE (x);
  1891.  
  1892.           /* If this was an evil function, don't keep it in class.  */
  1893.           if (IDENTIFIER_ERROR_LOCUS (DECL_NAME (x)))
  1894.         continue;
  1895.  
  1896.           if (y) TREE_CHAIN (y) = TREE_CHAIN (x);
  1897.           if (! fn_fields) fn_fields = x;
  1898.           else TREE_CHAIN (tail) = x;
  1899.           tail = x;
  1900.           if (DECL_CONTEXT (x))
  1901.         continue;
  1902.  
  1903.           DECL_CONTEXT (x) = t;
  1904.           DECL_VCONTEXT (x) = t;
  1905.  
  1906.           DECL_SIZE_UNIT (x) = 0;
  1907.  
  1908.           /* The name of the field is the original field name
  1909.          Save this in auxiliary field for later overloading.  */
  1910.           if (DECL_VIRTUAL_P (x)
  1911.           || (all_virtual == 1 && ! DECL_CONSTRUCTOR_P (x)))
  1912.         {
  1913.           pending_virtuals = add_virtual_function (pending_virtuals,
  1914.                                &has_virtual, x,
  1915.                                first_vfn_base_index);
  1916.           if (DECL_ABSTRACT_VIRTUAL_P (x))
  1917.             abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
  1918.         }
  1919.           continue;
  1920.         }
  1921.  
  1922.       /* Handle visibility declarations.  */
  1923.       if (DECL_NAME (x) && TREE_CODE (DECL_NAME (x)) == SCOPE_REF)
  1924.         {
  1925.           tree fdecl = TREE_OPERAND (DECL_NAME (x), 1);
  1926.  
  1927.           if (y) TREE_CHAIN (y) = TREE_CHAIN (x);
  1928.           /* Make type T see field decl FDECL with
  1929.          the visibility VISIBILITY.  */
  1930.           if (TREE_CODE (fdecl) == TREE_LIST)
  1931.         {
  1932.           fdecl = TREE_VALUE (fdecl);
  1933.           while (fdecl)
  1934.             {
  1935.               if (alter_visibility (t, fdecl, visibility) == 0)
  1936.             break;
  1937.               fdecl = TREE_CHAIN (fdecl);
  1938.             }
  1939.         }
  1940.           else alter_visibility (t, fdecl, visibility);
  1941.           CLASSTYPE_ALTERS_VISIBILITIES_P (t) = 1;
  1942.           continue;
  1943.         }
  1944.  
  1945.       /* Perform error checking that did not get done in grokdeclarator.  */
  1946.       if (TREE_CODE (TREE_TYPE (x)) == FUNCTION_TYPE)
  1947.         {
  1948.           error_with_decl (x, "field `%s' invalidly declared function type");
  1949.           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
  1950.         }
  1951.       else if (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE)
  1952.         {
  1953.           error_with_decl (x, "field `%s' invalidly declared method type");
  1954.           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
  1955.         }
  1956.       else if (TREE_CODE (TREE_TYPE (x)) == OFFSET_TYPE)
  1957.         {
  1958.           error_with_decl (x, "field `%s' invalidly declared offset type");
  1959.           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
  1960.         }
  1961.       /* If this is of reference type, check if it needs an init.  */
  1962.       if (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE
  1963.           && DECL_INITIAL (x) == 0)
  1964.         ref_sans_init = 1;
  1965.  
  1966.       /* When this goes into scope, it will be a non-local reference.  */
  1967.       TREE_NONLOCAL (x) = 1;
  1968.  
  1969.       if (TREE_CODE (x) == FIELD_DECL)
  1970.         {
  1971.           /* Never let anything with uninheritable virutals
  1972.          make it through without complaint.  */
  1973.           if (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
  1974.           && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (x)))
  1975.         abstract_virtuals_error (x, TREE_TYPE (x));
  1976.  
  1977.           if (TYPE_LANG_SPECIFIC (TREE_TYPE (x)))
  1978.         {
  1979.           if (TYPE_HAS_DEFAULT_CONSTRUCTOR (TREE_TYPE (x)))
  1980.             needs_default_ctor = 1;
  1981.           if (TYPE_GETS_CONST_INIT_REF (TREE_TYPE (x)))
  1982.             needs_const_ctor = 1;
  1983.           else if (TYPE_GETS_INIT_REF (TREE_TYPE (x)))
  1984.             cant_have_const_ctor = 1;
  1985.         }
  1986.           else if (DECL_INITIAL (x) == NULL_TREE
  1987.                && (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (x))
  1988.                || TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE))
  1989.         cant_have_default_ctor = 1;
  1990.  
  1991.           /* If any field is const, the structure type is pseudo-const.  */
  1992.           if (TREE_READONLY (x))
  1993.         {
  1994.           C_TYPE_FIELDS_READONLY (t) = 1;
  1995.           if (DECL_INITIAL (x) == 0)
  1996.             const_sans_init = 1;
  1997.         }
  1998.           else 
  1999.         {
  2000.           /* A field that is pseudo-const makes the structure likewise.  */
  2001.           tree t1 = TREE_TYPE (x);
  2002.           while (TREE_CODE (t1) == ARRAY_TYPE)
  2003.             t1 = TREE_TYPE (t1);
  2004.           if (IS_AGGR_TYPE (t1))
  2005.             {
  2006.               if (C_TYPE_FIELDS_READONLY (t1))
  2007.             C_TYPE_FIELDS_READONLY (t) = 1;
  2008.               if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1))
  2009.             const_sans_init = 1;
  2010.             }
  2011.         }
  2012.         }
  2013.       else if (TREE_STATIC (x) && TREE_CODE (t) == UNION_TYPE)
  2014.         /* Unions cannot have static members.  */
  2015.         error_with_decl (x, "field `%s' declared static in union");
  2016.  
  2017.       if (! fields) fields = x;
  2018.       DECL_FIELD_CONTEXT (x) = t;
  2019.       DECL_SIZE_UNIT (x) = 0;
  2020.  
  2021.       if (TREE_PACKED (x))
  2022.         {
  2023.           /* Invalid bit-field size done by grokfield.  */
  2024.           /* Detect invalid bit-field type.  */
  2025.           if (DECL_INITIAL (x)
  2026.           && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
  2027.           && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
  2028.         {
  2029.           error_with_decl (x, "bit-field `%s' has invalid type");
  2030.           DECL_INITIAL (x) = NULL;
  2031.         }
  2032.           if (DECL_INITIAL (x) && pedantic
  2033.           && TREE_TYPE (x) != integer_type_node
  2034.           && TREE_TYPE (x) != unsigned_type_node)
  2035.         warning_with_decl (x, "bit-field `%s' type invalid in ANSI C");
  2036.  
  2037.           /* Detect and ignore out of range field width.  */
  2038.           if (DECL_INITIAL (x))
  2039.         {
  2040.           register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
  2041.  
  2042.           if (width < 0)
  2043.             {
  2044.               DECL_INITIAL (x) = NULL;
  2045.               warning_with_decl (x, "negative width in bit-field `%s'");
  2046.             }
  2047.           else if (width == 0 && DECL_NAME (x) != 0)
  2048.             {
  2049.               error_with_decl (x, "zero width for bit-field `%s'");
  2050.               DECL_INITIAL (x) = NULL;
  2051.             }
  2052.           else if (width > TYPE_PRECISION (TREE_TYPE (x)))
  2053.             {
  2054.               DECL_INITIAL (x) = NULL;
  2055.               warning_with_decl (x, "width of `%s' exceeds its type");
  2056.             }
  2057.         }
  2058.  
  2059.           /* Process valid field width.  */
  2060.           if (DECL_INITIAL (x))
  2061.         {
  2062.           register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
  2063.  
  2064.           if (width == 0)
  2065.             {
  2066.               /* field size 0 => mark following field as "aligned" */
  2067.               if (TREE_CHAIN (x))
  2068.             DECL_ALIGN (TREE_CHAIN (x))
  2069.               = MAX (DECL_ALIGN (TREE_CHAIN (x)), EMPTY_FIELD_BOUNDARY);
  2070.               /* field of size 0 at the end => round up the size.  */
  2071.               else
  2072.             round_up_size = EMPTY_FIELD_BOUNDARY;
  2073.             }
  2074.           else
  2075.             {
  2076.               DECL_INITIAL (x) = NULL_TREE;
  2077.               DECL_SIZE_UNIT (x) = width;
  2078.               TREE_PACKED (x) = 1;
  2079.               /* Traditionally a bit field is unsigned
  2080.              even if declared signed.  */
  2081.               if (flag_traditional
  2082.               && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE)
  2083.             TREE_TYPE (x) = unsigned_type_node;
  2084.             }
  2085.         }
  2086.           else
  2087.         /* Non-bit-fields are aligned for their type.  */
  2088.         DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x)));
  2089.         }
  2090.       else if (TREE_CODE (x) == FIELD_DECL)
  2091.         {
  2092.           tree type = TREE_TYPE (x);
  2093.           if (TREE_CODE (type) == ARRAY_TYPE)
  2094.         type = TREE_TYPE (type);
  2095.           if (code == UNION_TYPE)
  2096.         {
  2097.           if (TYPE_NEEDS_CONSTRUCTING (type))
  2098.             error ("member %s::%s with constructor not allowed in union",
  2099.                IDENTIFIER_POINTER (name), IDENTIFIER_POINTER (DECL_NAME (x)));
  2100.           if (TYPE_NEEDS_DESTRUCTOR (type))
  2101.             error ("member %s::%s with destructor (also) not allowed in union",
  2102.                IDENTIFIER_POINTER (name), IDENTIFIER_POINTER (DECL_NAME (x)));
  2103.         }
  2104.           else if (code == RECORD_TYPE)
  2105.         {
  2106.           /* Array of record type doesn't matter for this bit.  */
  2107.           TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
  2108.           if (IS_AGGR_TYPE (type))
  2109.             {
  2110.               needs_ctor |= TYPE_NEEDS_CONSTRUCTOR (type);
  2111.               needs_dtor |= TYPE_NEEDS_DESTRUCTOR (type);
  2112.               members_need_dtors |= TYPE_NEEDS_DESTRUCTOR (type);
  2113.               TYPE_GETS_ASSIGNMENT (t) |= TYPE_GETS_ASSIGNMENT (type);
  2114.               TYPE_GETS_INIT_REF (t) |= TYPE_GETS_INIT_REF (type);
  2115.               TYPE_GETS_CONST_INIT_REF (t) |= TYPE_GETS_CONST_INIT_REF (type);
  2116.             }
  2117.         }
  2118.           if (DECL_INITIAL (x) != NULL_TREE)
  2119.         {
  2120.           /* `build_class_init_list' does not recognize non-FIELD_DECLs.  */
  2121.           if (code == UNION_TYPE && any_default_members != 0)
  2122.             error ("multiple fields in union initialized");
  2123.           any_default_members = 1;
  2124.         }
  2125.         }
  2126.       y = x;
  2127.     }
  2128.       list_of_fieldlists = TREE_CHAIN (list_of_fieldlists);
  2129.       /* link the tail while we have it! */
  2130.       if (y)
  2131.     {
  2132.       TREE_CHAIN (y) = NULL_TREE;
  2133.  
  2134.       if (list_of_fieldlists
  2135.           && TREE_VALUE (list_of_fieldlists)
  2136.           && TREE_CODE (TREE_VALUE (list_of_fieldlists)) != FUNCTION_DECL)
  2137.         TREE_CHAIN (y) = TREE_VALUE (list_of_fieldlists);
  2138.     }
  2139.     }
  2140.  
  2141.   if (tail) TREE_CHAIN (tail) = NULL_TREE;
  2142.  
  2143.   /* If this type has any constant members which did not come
  2144.      with their own initialization, mark that fact here.  It is
  2145.      not an error here, since such types can be saved either by their
  2146.      constructors, or by fortuitous initialization.  */
  2147.   CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
  2148.   CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
  2149.   CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
  2150.  
  2151.   if (vfield == 0
  2152.       && (has_virtual
  2153. #ifdef SOS
  2154.       || TYPE_DYNAMIC (t)
  2155. #endif
  2156.       ))
  2157.     {
  2158.       /* We build this decl with ptr_type_node, and
  2159.      change the type when we know what it should be.  */
  2160.       vfield = build_decl (FIELD_DECL, get_vfield_name (t), ptr_type_node);
  2161.       CLASSTYPE_VFIELD (t) = vfield;
  2162.       DECL_VIRTUAL_P (vfield) = 1;
  2163.       DECL_FIELD_CONTEXT (vfield) = t;
  2164.       SET_DECL_FCONTEXT (vfield, t);
  2165.       DECL_SIZE_UNIT (vfield) = 0;
  2166.       if (y)
  2167.     {
  2168.       assert (TREE_CHAIN (y) == 0);
  2169.       TREE_CHAIN (y) = vfield;
  2170.       y = vfield;
  2171.     }
  2172.       else fields = vfield;
  2173.       vfields = chainon (vfields, CLASSTYPE_AS_LIST (t));
  2174.     }
  2175.  
  2176.   /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
  2177.      And they have already done their work.
  2178.  
  2179.      C++: maybe we will support default field initialization some day...  */
  2180.  
  2181.   /* Delete all zero-width bit-fields from the front of the fieldlist */
  2182.   while (fields && TREE_PACKED (fields)
  2183.      && DECL_INITIAL (fields))
  2184.     fields = TREE_CHAIN (fields);
  2185.   /* Delete all such fields from the rest of the fields.  */
  2186.   for (x = fields; x;)
  2187.     {
  2188.       if (TREE_CHAIN (x) && TREE_PACKED (TREE_CHAIN (x))
  2189.       && DECL_INITIAL (TREE_CHAIN (x)))
  2190.     TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
  2191.       else x = TREE_CHAIN (x);
  2192.     }
  2193.   /* Delete all duplicate fields from the fields */
  2194.   delete_duplicate_fields (fields);
  2195.  
  2196.   /* Now we have the final fieldlist for the data fields.  Record it,
  2197.      then lay out the structure or union (including the fields).  */
  2198.  
  2199.   TYPE_FIELDS (t) = fields;
  2200.  
  2201.   /* If there's a :0 field at the end, round the size to the
  2202.      EMPTY_FIELD_BOUNDARY.  */
  2203.   TYPE_ALIGN (t) = round_up_size;
  2204.  
  2205.   if (debug_default_functions)
  2206.     {
  2207.       if ((TYPE_NEEDS_CONSTRUCTOR (t) || TYPE_HAS_CONSTRUCTOR (t) || needs_ctor)
  2208.       && ! TYPE_HAS_INIT_REF (t))
  2209.     {
  2210.       tree default_fn = cons_up_default_function (t, name, 1);
  2211.       TREE_CHAIN (default_fn) = fn_fields;
  2212.       DECL_CONTEXT (default_fn) = t;
  2213.       DECL_VCONTEXT (default_fn) = t;
  2214.       fn_fields = default_fn;
  2215.       TYPE_HAS_INIT_REF (t) = 1;
  2216.       default_fn = cons_up_default_function (t, name, 3);
  2217.       TREE_CHAIN (default_fn) = fn_fields;
  2218.       DECL_CONTEXT (default_fn) = t;
  2219.       DECL_VCONTEXT (default_fn) = t;
  2220.       fn_fields = default_fn;
  2221.       nonprivate_method = 1;
  2222.     }
  2223.  
  2224.       if (! TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
  2225.       && needs_default_ctor && ! cant_have_default_ctor)
  2226.     {
  2227.       tree default_fn = cons_up_default_function (t, name, 2);
  2228.       TREE_CHAIN (default_fn) = fn_fields;
  2229.       DECL_CONTEXT (default_fn) = t;
  2230.       DECL_VCONTEXT (default_fn) = t;
  2231.       fn_fields = default_fn;
  2232.       TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
  2233.       nonprivate_method = 1;
  2234.     }
  2235.     }
  2236.   /* Warn about duplicate methods in fn_fields.  Also compact
  2237.      method lists so that lookup can be made faster.
  2238.  
  2239.      Algorithm:  Outer loop builds lists by method name.
  2240.      Inner loop checks for redundant method names within a list.
  2241.  
  2242.      Data Structure:  List of method lists.  The outer list
  2243.      is a TREE_LIST, whose TREE_PURPOSE field is the field name
  2244.      and the TREE_VALUE is the TREE_CHAIN of the FUNCTION_DECLs.
  2245.      Friends are chained in the same way as member functions, but
  2246.      they live in the TREE_TYPE field of the outer list.
  2247.      That allows them to be quicky deleted, and requires
  2248.      no extra storage.
  2249.  
  2250.      If there are any constructors/destructors, they are moved to
  2251.      the front of the list.  This makes pushclass more efficient.
  2252.  
  2253.      We also link each field which has shares a name with its
  2254.      baseclass to the head of the list of fields for that base class.
  2255.      This allows us to reduce search time in places like `build_method_call'
  2256.      to consider only reasonably likely functions.  */
  2257.  
  2258.   if (fn_fields)
  2259.     {
  2260.       /* Now prepare to gather fn_fields into vector.  */
  2261.       struct obstack *ambient_obstack = current_obstack;
  2262.       current_obstack = &class_obstack;
  2263.       method_vec = make_node (TREE_VEC);
  2264.       /* Room has been saved for constructors and destructors.  */
  2265.       current_obstack = ambient_obstack;
  2266.       /* Now make this a live vector.  */
  2267.       obstack_free (&class_obstack, method_vec);
  2268.       obstack_blank (&class_obstack, sizeof (struct tree_vec));
  2269.  
  2270.       while (fn_fields)
  2271.     {
  2272.       /* NEXT Pointer, TEST Pointer, and BASE Pointer.  */
  2273.       tree nextp, *testp;
  2274.  
  2275.       nextp = TREE_CHAIN (fn_fields);
  2276.       TREE_CHAIN (fn_fields) = NULL_TREE;
  2277.       /* Constrcutors are handled easily in search routines.
  2278.          Besides, we know we wont find any, so do not bother looking.  */
  2279.       if (DECL_ORIGINAL_NAME (fn_fields) == name
  2280.           && TREE_VEC_ELT (method_vec, 0) == 0)
  2281.         TREE_VEC_ELT (method_vec, 0) = fn_fields;
  2282.       else
  2283.         {
  2284.           testp = &TREE_VEC_ELT (method_vec, 0);
  2285.           if (*testp == NULL_TREE)
  2286.         testp++;
  2287.           while ((int)testp < (int)obstack_next_free (&class_obstack)
  2288.              && DECL_ORIGINAL_NAME (*testp) != DECL_ORIGINAL_NAME (fn_fields))
  2289.         testp++;
  2290.           if ((int)testp < (int)obstack_next_free (&class_obstack))
  2291.         {
  2292.           for (x = *testp; x; x = TREE_CHAIN (x))
  2293.             {
  2294.               if (DECL_NAME (fn_fields) == DECL_NAME (x))
  2295.             {
  2296.               /* We complain about multiple destructors on sight,
  2297.                  so we do not repeat the warning here.  Friend-friend
  2298.                  ambiguities are warned about outside this loop.  */
  2299.               if (! DESTRUCTOR_NAME_P (DECL_NAME (fn_fields)))
  2300.                 error_with_file_and_line (DECL_SOURCE_FILE (fn_fields),
  2301.                               DECL_SOURCE_LINE (fn_fields),
  2302.                               "ambiguous method `%s' in structure",
  2303.                               lang_printable_name (fn_fields));
  2304.               break;
  2305.             }
  2306.               y = x;
  2307.             }
  2308.           if (x == 0)
  2309.             if (*testp)
  2310.               TREE_CHAIN (y) = fn_fields;
  2311.             else
  2312.               *testp = fn_fields;
  2313.         }
  2314.           else
  2315.         {
  2316.           obstack_ptr_grow (&class_obstack, fn_fields);
  2317.           method_vec = (tree)obstack_base (&class_obstack);
  2318.         }
  2319.         }
  2320.       fn_fields = nextp;
  2321.     }
  2322.  
  2323.       TREE_VEC_LENGTH (method_vec)
  2324.     = (tree *)obstack_next_free (&class_obstack) - (&TREE_VEC_ELT (method_vec, 0));
  2325.       obstack_finish (&class_obstack);
  2326.       CLASSTYPE_METHOD_VEC (t) = method_vec;
  2327.  
  2328.       if (nonprivate_method == 0
  2329.       && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
  2330.       && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
  2331.     {
  2332.       for (i = 0; i <= n_baseclasses; i++)
  2333.         if (CLASSTYPE_VIA_PUBLIC (t, i))
  2334.           {
  2335.         nonprivate_method = 1;
  2336.         break;
  2337.           }
  2338.       if (nonprivate_method == 0)
  2339.         warning ("all class member functions are private");
  2340.     }
  2341.     }
  2342.   else
  2343.     {
  2344.       method_vec = 0;
  2345.  
  2346.       /* Just in case these got accidently
  2347.      filled in by syntax errors.  */
  2348.       TYPE_HAS_CONSTRUCTOR (t) = 0;
  2349.       TYPE_HAS_DESTRUCTOR (t) = 0;
  2350.     }
  2351.  
  2352.   /* If there are constructors (and destructors), they are at the
  2353.      front.  Place destructors at very front.  Also warn if all
  2354.      constructors and/or destructors are private (in which case this
  2355.      class is effectively unusable.  */
  2356.   if (TYPE_HAS_DESTRUCTOR (t))
  2357.     {
  2358.       tree dtor, prev;
  2359.  
  2360.       for (dtor = TREE_VEC_ELT (method_vec, 0); dtor; prev = dtor, dtor = TREE_CHAIN (dtor))
  2361.     {
  2362.       if (DESTRUCTOR_NAME_P (DECL_NAME (dtor)))
  2363.         {
  2364.           if (TREE_PRIVATE (dtor)
  2365.           && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
  2366.           && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
  2367.         warning_with_decl (TYPE_NAME (t), "class `%s' only defines a private destructor and has no friends");
  2368.           break;
  2369.         }
  2370.     }
  2371.       /* Wild parse errors can cause this to happen.  */
  2372.       if (dtor == NULL_TREE)
  2373.     TYPE_HAS_DESTRUCTOR (t) = 0;
  2374.       else if (dtor != TREE_VEC_ELT (method_vec, 0))
  2375.     {
  2376.       TREE_CHAIN (prev) = TREE_CHAIN (dtor);
  2377.       TREE_CHAIN (dtor) = TREE_VEC_ELT (method_vec, 0);
  2378.       TREE_VEC_ELT (method_vec, 0) = dtor;
  2379.     }
  2380.     }
  2381.   else if (members_need_dtors
  2382. #ifdef NeXT
  2383.        /* We must create a destructor to pass the right size to delete. */
  2384.        || TREE_GETS_TWO_ARG_DELETE (t)
  2385. #endif /* NeXT */
  2386.        || TYPE_USES_VIRTUAL_BASECLASSES (t)
  2387.        || TYPE_USES_MULTIPLE_INHERITANCE (t))
  2388.     {
  2389.       /* Here we must cons up a destructor on the fly.  */
  2390.       tree dtor = cons_up_default_function (t, name, 0);
  2391.  
  2392.       /* If we couldn't make it work, then pretend we didn't need it.  */
  2393.       if (dtor == void_type_node)
  2394.     TYPE_NEEDS_DESTRUCTOR (t) = 0;
  2395.       else
  2396.     {
  2397.       DECL_CONTEXT (dtor) = t;
  2398.       DECL_VCONTEXT (dtor) = t;
  2399.       if (DECL_VIRTUAL_P (dtor))
  2400.         pending_virtuals = add_virtual_function (pending_virtuals,
  2401.                              &has_virtual, dtor);
  2402.       if (TYPE_HAS_CONSTRUCTOR (t))
  2403.         TREE_CHAIN (dtor) = TREE_VEC_ELT (method_vec, 0);
  2404.       else if (method_vec == 0)
  2405.         {
  2406.           /* Now prepare to gather fn_fields into vector.  */
  2407.           struct obstack *ambient_obstack = current_obstack;
  2408.           current_obstack = &class_obstack;
  2409.           method_vec = make_node (TREE_VEC);
  2410.           /* Room has been saved for constructors and destructors.  */
  2411.           current_obstack = ambient_obstack;
  2412.           TREE_VEC_LENGTH (method_vec) = 1;
  2413.           CLASSTYPE_METHOD_VEC (t) = method_vec;
  2414.         }
  2415.       TREE_VEC_ELT (method_vec, 0) = dtor;
  2416.       TYPE_HAS_DESTRUCTOR (t) = 1;
  2417.     }
  2418.     }
  2419.   if (TYPE_HAS_CONSTRUCTOR (t)
  2420.       && ! CLASSTYPE_DECLARED_EXCEPTION (t)
  2421.       && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
  2422.       && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
  2423.     {
  2424.       int nonprivate_ctor = 0;
  2425.       tree ctor;
  2426.  
  2427.       for (ctor = TREE_VEC_ELT (method_vec, 0); ctor; ctor = TREE_CHAIN (ctor))
  2428.     if (! TREE_PRIVATE (ctor))
  2429.       {
  2430.         nonprivate_ctor = 1;
  2431.         break;
  2432.       }
  2433.       if (nonprivate_ctor == 0)
  2434.     warning ("class %s only defines private constructors and has no friends",
  2435.          TYPE_NAME_STRING (t));
  2436.     }
  2437.  
  2438.   /* Now for each member function (except for constructors and
  2439.      destructors), compute where member functions of the same
  2440.      name reside in base classes.  */
  2441.   if (n_baseclasses != 0
  2442.       && method_vec != NULL_TREE
  2443.       && TREE_VEC_LENGTH (method_vec) > 1)
  2444.     {
  2445.       int len = TREE_VEC_LENGTH (method_vec);
  2446.       tree baselink_vec = make_tree_vec (len);
  2447.       int any_links = 0;
  2448.  
  2449.       for (i = 1; i < len; i++)
  2450.     {
  2451.       TREE_VEC_ELT (baselink_vec, i)
  2452.         = get_baselinks (t, DECL_ORIGINAL_NAME (TREE_VEC_ELT (method_vec, i)));
  2453.       if (TREE_VEC_ELT (baselink_vec, i) != 0)
  2454.         any_links = 1;
  2455.     }
  2456.       if (any_links != 0)
  2457.     CLASSTYPE_BASELINK_VEC (t) = baselink_vec;
  2458.       else
  2459.     obstack_free (current_obstack, baselink_vec);
  2460.     }
  2461.  
  2462.   /* We can't know this information until we have seen all of the
  2463.      constructors.  */
  2464.   TYPE_NONE_ASSIGN_THIS (t) = 0;
  2465.  
  2466.   /* Pass layout information about base classes to layout_type, if any.  */
  2467.  
  2468.   if (n_baseclasses)
  2469.     {
  2470.       tree pseudo_basetype = TREE_TYPE (base_layout_decl);
  2471.  
  2472.       TREE_CHAIN (base_layout_decl) = TYPE_FIELDS (t);
  2473.       TYPE_FIELDS (t) = base_layout_decl;
  2474.  
  2475.       TYPE_SIZE (pseudo_basetype) = CLASSTYPE_SIZE (t);
  2476.       TYPE_SIZE_UNIT (pseudo_basetype) = TYPE_SIZE_UNIT (t);
  2477.       TYPE_MODE (pseudo_basetype) = TYPE_MODE (t);
  2478.       TYPE_ALIGN (pseudo_basetype) = CLASSTYPE_ALIGN (t);
  2479.       DECL_ALIGN (base_layout_decl) = TYPE_ALIGN (pseudo_basetype);
  2480.     }
  2481.  
  2482.   layout_type (t);
  2483.  
  2484.   if (n_baseclasses)
  2485.     TYPE_FIELDS (t) = TREE_CHAIN (TYPE_FIELDS (t));
  2486.  
  2487.   /* C++: do not let empty structures exist.  */
  2488.   if (integer_zerop (TYPE_SIZE (t)))
  2489.     TYPE_SIZE (t) = TYPE_SIZE (char_type_node);
  2490.  
  2491.   /* Set the TYPE_DECL for this type to contain the right
  2492.      value for DECL_OFFSET, so that we can use it as part
  2493.      of a COMPONENT_REF for multiple inheritance.  */
  2494.  
  2495.   if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
  2496.     layout_decl (TYPE_NAME (t));
  2497.  
  2498.   if (TYPE_USES_VIRTUAL_BASECLASSES (t))
  2499.     {
  2500.       tree vbases;
  2501.  
  2502.       max_has_virtual = layout_vbasetypes (t, max_has_virtual);
  2503.       vbases = CLASSTYPE_VBASECLASSES (t);
  2504.       CLASSTYPE_N_VBASECLASSES (t) = list_length (vbases);
  2505.  
  2506.       /* Now fix up any virtual base class types that we
  2507.      left lying around.  We must get these done
  2508.      before we try to lay out the virtual function table.  */
  2509.       pending_hard_virtuals = nreverse (pending_hard_virtuals);
  2510. #if 1
  2511.       /* This loop makes all the entries in the virtual function tables
  2512.      of interest contain the "latest" version of the functions
  2513.      we have defined.  */
  2514.  
  2515.       while (vbases)
  2516.     {
  2517.       tree virtuals = ASSOC_VIRTUALS (vbases);
  2518.  
  2519.       if (virtuals)
  2520.         virtuals = TREE_CHAIN (virtuals);
  2521.  
  2522.       while (virtuals != NULL_TREE)
  2523.         {
  2524.           tree pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals));
  2525.           tree base_fndecl = TREE_OPERAND (pfn, 0);
  2526.           tree decl = get_first_matching_virtual (t, base_fndecl, 0);
  2527.           tree context = DECL_CONTEXT (decl);
  2528.           if (decl != base_fndecl && context != t)
  2529.         {
  2530.           tree assoc = NULL_TREE, these_virtuals;
  2531.           int i = TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)) & ((1<<(BITS_PER_WORD-1))-1);
  2532.  
  2533.           if (TYPE_USES_VIRTUAL_BASECLASSES (context))
  2534.             assoc = virtual_member (DECL_CONTEXT (base_fndecl),
  2535.                         CLASSTYPE_VBASECLASSES (context));
  2536.           if (assoc == NULL_TREE)
  2537.             assoc = assoc_value (DECL_CONTEXT (base_fndecl), context);
  2538.           if (assoc != NULL_TREE)
  2539.             {
  2540.               these_virtuals = ASSOC_VIRTUALS (assoc);
  2541.  
  2542.               while (i-- > 0)
  2543.             these_virtuals = TREE_CHAIN (these_virtuals);
  2544.               pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (these_virtuals));
  2545.               modify_vtable_entries (t, decl, base_fndecl, pfn);
  2546.             }
  2547.         }
  2548.           virtuals = TREE_CHAIN (virtuals);
  2549.         }
  2550.       vbases = TREE_CHAIN (vbases);
  2551.     }
  2552. #endif /* 1 */
  2553.       while (pending_hard_virtuals)
  2554.     {
  2555.       /* Need an entry in some other virtual function table.  */
  2556.       tree base_fndecls = DECL_VINDEX (TREE_PURPOSE (pending_hard_virtuals));
  2557.       while (base_fndecls)
  2558.         {
  2559.           modify_vtable_entries (t, TREE_PURPOSE (pending_hard_virtuals),
  2560.                      TREE_VALUE (base_fndecls),
  2561.                      TREE_VALUE (pending_hard_virtuals));
  2562.           base_fndecls = TREE_CHAIN (base_fndecls);
  2563.         }
  2564.       pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
  2565.     }
  2566.     }
  2567.   else
  2568.     CLASSTYPE_VBASE_SIZE (t) = integer_zero_node;
  2569.  
  2570.   if (pending_virtuals)
  2571.     {
  2572.       pending_virtuals = nreverse (pending_virtuals);
  2573.       /* We must enter these virtuals into the table.  */
  2574.       if (first_vfn_base_index == 0)
  2575.     {
  2576.       pending_virtuals = tree_cons (NULL_TREE, the_null_vtable_entry,
  2577.                     pending_virtuals);
  2578.       build_vtable (0, t);
  2579.     }
  2580.       else
  2581.     {
  2582.       /* Here we know enough to change the type of our virtual
  2583.          function table, but we will wait until later this function.  */
  2584.       if (! CLASSTYPE_MARKED4 (t))
  2585.         build_vtable (assoc_value (TYPE_MAIN_VARIANT (CLASSTYPE_BASECLASS (t, first_vfn_base_index)), t), t);
  2586.     }
  2587.  
  2588.       /* If this type has basetypes with constructors, then those
  2589.      constructors might clobber the virtual function table.  But
  2590.      they don't if the derived class shares the exact vtable of the base
  2591.      class.  */
  2592.  
  2593.       CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
  2594.     }
  2595.   else if (first_vfn_base_index)
  2596.     {
  2597.       tree basetype = get_base_type (DECL_FIELD_CONTEXT (vfield), t, 0);
  2598.       tree assoc;
  2599.       
  2600.       if (TREE_VIA_VIRTUAL (basetype))
  2601.     assoc = virtual_member (DECL_FIELD_CONTEXT (vfield), CLASSTYPE_VBASECLASSES (t));
  2602.       else
  2603.     assoc = assoc_value (TYPE_MAIN_VARIANT (basetype), t);
  2604.  
  2605.       /* This class contributes nothing new to the virtual function
  2606.      table.  However, it may have declared functions which
  2607.      went into the virtual function table "inherited" from the
  2608.      base class.  If so, we grab a copy of those updated functions,
  2609.      and pretend they are ours.  */
  2610.  
  2611. #ifdef SOS
  2612.       /* Don't define this ahead of time if we have more
  2613.      fields to add later.  */
  2614.       if (all_virtual == 2 && fn_fields != NULL_TREE)
  2615.     ;
  2616.       else
  2617. #endif
  2618.     {
  2619.       /* See if we should steal the virtual info from base class.  */
  2620.       if (CLASS_ASSOC_VTABLE (t) == NULL_TREE)
  2621.         CLASS_ASSOC_VTABLE (t) = ASSOC_VTABLE (assoc);
  2622.       if (CLASS_ASSOC_VIRTUALS (t) == NULL_TREE)
  2623.         CLASS_ASSOC_VIRTUALS (t) = ASSOC_VIRTUALS (assoc);
  2624.     }
  2625.       if (CLASS_ASSOC_VTABLE (t) != ASSOC_VTABLE (assoc))
  2626.     CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
  2627.     }
  2628.  
  2629.   if (has_virtual > max_has_virtual)
  2630.     max_has_virtual = has_virtual;
  2631.   if (max_has_virtual || first_vfn_base_index)
  2632.     {
  2633. #ifdef VTABLE_USES_MASK
  2634.       if (max_has_virtual >= VINDEX_MAX)
  2635.     {
  2636.       error ("too many virtual functions for class `%s' (VINDEX_MAX < %d)", TYPE_NAME_STRING (t), has_virtual);
  2637.     }
  2638. #endif
  2639.       TYPE_VIRTUAL_P (t) = 1;
  2640.       CLASSTYPE_VSIZE (t) = has_virtual;
  2641.       if (first_vfn_base_index)
  2642.     {
  2643.       if (pending_virtuals)
  2644.         CLASS_ASSOC_VIRTUALS (t) = chainon (CLASS_ASSOC_VIRTUALS (t),
  2645.                         pending_virtuals);
  2646.     }
  2647.       else if (has_virtual)
  2648.     {
  2649.       CLASS_ASSOC_VIRTUALS (t) = pending_virtuals;
  2650.       if (write_virtuals >= 0)
  2651.         DECL_VIRTUAL_P (CLASS_ASSOC_VTABLE (t)) = 1;
  2652.     }
  2653.     }
  2654.  
  2655. #ifdef SOS
  2656.   if (all_virtual == 2 && (max_has_virtual || method_vec))
  2657.     {
  2658.       /* Now that we know the size of the virtual table, lay out
  2659.      the absolute table following it.  */
  2660.       int i;
  2661.       tree tmp;
  2662.       tree pending_absolutes = NULL_TREE;
  2663.       int has_absolute = has_virtual;
  2664.  
  2665.       /* Local variables for building a table filled with strings
  2666.      containing the names of interesting things.  */
  2667.       tree decl, init;
  2668.       tree start = NULL_TREE, next = NULL_TREE;
  2669.       tree *outer = &TREE_VEC_ELT (method_vec, 0);
  2670.  
  2671.       while (outer != TREE_VEC_END (method_vec))
  2672.     {
  2673.       tree inner;
  2674.       for (inner = *outer; inner; inner = TREE_CHAIN (inner))
  2675.         {
  2676.           tree entry;
  2677.           tree fn;
  2678.  
  2679.           /* Don't bother with functions which appear
  2680.          for visibility reasons.  */
  2681.           if (DECL_FIELD_CONTEXT (inner) != t)
  2682.         continue;
  2683.  
  2684.           /* Must lay this function into its absolute table as well.
  2685.          This forces an inline function to be written out.  */
  2686.           fn = build1 (ADDR_EXPR, ptr_type_node, inner);
  2687.           TREE_LITERAL (fn) = 1;
  2688.           DECL_DINDEX (inner) = build_int_2 (++has_absolute, 0);
  2689.           entry = build_vtable_entry (integer_zero_node, fn);
  2690.           pending_absolutes = tree_cons (DECL_DINDEX (inner), entry,
  2691.                          pending_absolutes);
  2692.         }
  2693.       outer++;
  2694.     }
  2695.  
  2696.       CLASS_ASSOC_VIRTUALS (t) = chainon (CLASS_ASSOC_VIRTUALS (t),
  2697.                       nreverse (pending_absolutes));
  2698.       if (TYPE_DYNAMIC (t))
  2699.     {
  2700.       for (outer = &TREE_VEC_ELT (method_vec, 0);
  2701.            outer != TREE_VEC_END (method_vec);
  2702.            outer++)
  2703.         {
  2704.           tree inner;
  2705.           for (inner = *outer; inner; inner = TREE_CHAIN (inner))
  2706.         {
  2707.           tree str = make_node (STRING_CST);
  2708.           TREE_STRING_LENGTH (str) = IDENTIFIER_LENGTH (DECL_NAME (inner));
  2709.           TREE_STRING_POINTER (str) = IDENTIFIER_POINTER (DECL_NAME (inner));
  2710.           TREE_LITERAL (str) = 1;
  2711.           TREE_STATIC (str) = 1;
  2712.           TREE_TYPE (str)
  2713.             = build_cplus_array_type (char_type_node,
  2714.                           build_index_type (build_int_2 (TREE_STRING_LENGTH (str) - 1, 0)));
  2715.           
  2716.           if (start)
  2717.             {
  2718.               TREE_CHAIN (next) = build_tree_list (NULL_TREE, str);
  2719.               next = TREE_CHAIN (next);
  2720.             }
  2721.           else
  2722.             {
  2723.               start = build_tree_list (NULL_TREE, str);
  2724.               next = start;
  2725.             }
  2726.         }
  2727.         }
  2728.  
  2729.       /* Lay out dynamic link table for SOS.  */
  2730.  
  2731.       decl = finish_table (get_linktable_name (t),
  2732.                    string_type_node, start, 0);
  2733.     }
  2734.       has_virtual = has_absolute;
  2735.       CLASSTYPE_VSIZE (t) = has_virtual;
  2736.       if (has_virtual > max_has_virtual)
  2737.     max_has_virtual = has_virtual;
  2738.       if (vfield == 0)
  2739.     {
  2740.       /* We build this decl with ptr_type_node, and
  2741.          change the type when we know what it should be.  */
  2742.       vfield = build_decl (FIELD_DECL, get_vfield_name (t), ptr_type_node);
  2743.       CLASSTYPE_VFIELD (t) = vfield;
  2744.       DECL_VIRTUAL_P (vfield) = 1;
  2745.       DECL_FIELD_CONTEXT (vfield) = t;
  2746.       SET_DECL_FCONTEXT (vfield, t);
  2747.       DECL_SIZE_UNIT (vfield) = 0;
  2748.       y = tree_last (fields);
  2749.       if (y)
  2750.         TREE_CHAIN (y) = vfield;
  2751.       else
  2752.         fields = vfield;
  2753.       vfields = chainon (vfields, CLASSTYPE_AS_LIST (t));
  2754.     }
  2755.     }
  2756. #endif
  2757.  
  2758.   /* Now lay out the virtual function table.  */
  2759.   if (has_virtual)
  2760.     {
  2761.       tree atype, itype;
  2762.  
  2763.       if (TREE_TYPE (vfield) == ptr_type_node)
  2764.     {
  2765.       /* We must create a pointer to this table because
  2766.          the one inherited from base class does not exist.
  2767.          We will fill in the type when we know what it
  2768.          should really be.  */
  2769.       itype = build_index_type (build_int_2 (has_virtual, 0));
  2770.       atype = build_array_type (vtable_entry_type, itype);
  2771.       layout_type (atype);
  2772.       TREE_TYPE (vfield) = build_pointer_type (atype);
  2773.     }
  2774.       else
  2775.     {
  2776.       atype = TREE_TYPE (TREE_TYPE (vfield));
  2777.  
  2778.       if (has_virtual != TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))))
  2779.         {
  2780.           /* We must extend (or create) the boundaries on this array,
  2781.          because we picked up virtual functions from multiple
  2782.          base classes.  */
  2783.           itype = build_index_type (build_int_2 (has_virtual, 0));
  2784.           atype = build_array_type (vtable_entry_type, itype);
  2785.           layout_type (atype);
  2786.           vfield = copy_node (vfield);
  2787.           TREE_TYPE (vfield) = build_pointer_type (atype);
  2788. #if 0
  2789.           /* In the case of single inheritance, we can
  2790.          just move up the tree, since we share the
  2791.          same vptr slot.  */
  2792.           if (TREE_CHAIN (vfields) == NULL_TREE)
  2793.         vfields = CLASSTYPE_AS_LIST (t);
  2794. #endif
  2795.         }
  2796.     }
  2797.  
  2798.       CLASSTYPE_VFIELD (t) = vfield;
  2799.       if (TREE_TYPE (CLASS_ASSOC_VTABLE (t)) != atype)
  2800.     {
  2801.       TREE_TYPE (CLASS_ASSOC_VTABLE (t)) = atype;
  2802.       layout_decl (CLASS_ASSOC_VTABLE (t));
  2803.       DECL_ALIGN (CLASS_ASSOC_VTABLE (t))
  2804.         = MAX (TYPE_ALIGN (double_type_node),
  2805.            DECL_ALIGN (CLASS_ASSOC_VTABLE (t)));
  2806.     }
  2807.     }
  2808.   else if (first_vfn_base_index)
  2809.     CLASSTYPE_VFIELD (t) = vfield;
  2810.   CLASSTYPE_VFIELDS (t) = vfields;
  2811.  
  2812.   /* Set all appropriate CLASSTYPE_... flags for this type
  2813.      and its variants.  */
  2814.   TYPE_NEEDS_CONSTRUCTOR (t) |= needs_ctor || TYPE_HAS_CONSTRUCTOR (t);
  2815.   TYPE_NEEDS_CONSTRUCTING (t)
  2816.     |= ((TYPE_NEEDS_CONSTRUCTOR (t)|TYPE_USES_VIRTUAL_BASECLASSES (t))
  2817.     || (has_virtual | first_vfn_base_index)
  2818.     || any_default_members);
  2819.   TYPE_NEEDS_DESTRUCTOR (t) |= needs_dtor || TYPE_HAS_DESTRUCTOR (t);
  2820.   finish_struct_bits (t, first_vfn_base_index, max_has_virtual);
  2821.  
  2822.   /* Promote each bit-field's type to int if it is narrower than that.
  2823.      Also warn (or error) if static members are specified for a class
  2824.      which takes a constructor.  */
  2825.   for (x = fields; x; x = TREE_CHAIN (x))
  2826.     {
  2827.       if (TREE_PACKED (x)
  2828.       && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE
  2829.       && (TREE_INT_CST_LOW (DECL_SIZE (x)) * DECL_SIZE_UNIT (x)
  2830.           < TYPE_PRECISION (integer_type_node)))
  2831.     TREE_TYPE (x) = integer_type_node;
  2832.     }
  2833.  
  2834.   if (TYPE_HAS_CONSTRUCTOR (t))
  2835.     {
  2836.       tree vfields = CLASSTYPE_VFIELDS (t);
  2837.  
  2838.       while (vfields)
  2839.     {
  2840.       /* Mark the fact that constructor for T
  2841.          could affect anybody inheriting from T
  2842.          who wants to initialize vtables for VFIELDS's type.  */
  2843.       if (TREE_TYPE (vfields))
  2844.         TREE_ADDRESSABLE (vfields) = 1;
  2845.       vfields = TREE_CHAIN (vfields);
  2846.     }
  2847.       if (any_default_members != 0)
  2848.     build_class_init_list (t);
  2849.     }
  2850.   else if (TYPE_NEEDS_CONSTRUCTING (t))
  2851.     build_class_init_list (t);
  2852.  
  2853.   if (current_lang_name == lang_name_cplusplus)
  2854.     {
  2855.       if (! CLASSTYPE_DECLARED_EXCEPTION (t))
  2856.     embrace_waiting_friends (t);
  2857.  
  2858.       /* Write out inline function definitions.  */
  2859.       do_inline_function_hair (t, CLASSTYPE_INLINE_FRIENDS (t));
  2860.       CLASSTYPE_INLINE_FRIENDS (t) = 0;
  2861.     }
  2862.  
  2863.   if (CLASSTYPE_VSIZE (t) != 0)
  2864.     {
  2865.       TYPE_NONCOPIED_PARTS (t) = build_tree_list (default_conversion (CLASS_ASSOC_VTABLE (t)), vfield);
  2866.  
  2867.       if ((flag_this_is_variable & 1) == 0)
  2868.     {
  2869.       tree vtbl_ptr = build_decl (VAR_DECL, get_identifier (VPTR_NAME),
  2870.                       TREE_TYPE (vfield));
  2871.       TREE_REGDECL (vtbl_ptr) = 1;
  2872.       CLASSTYPE_VTBL_PTR (t) = vtbl_ptr;
  2873.     }
  2874.       if (DECL_FIELD_CONTEXT (vfield) != t)
  2875.     {
  2876.       tree assoc = assoc_value (DECL_FIELD_CONTEXT (vfield), t);
  2877.       tree offset = ASSOC_OFFSET (assoc);
  2878.  
  2879.       vfield = copy_node (vfield);
  2880.  
  2881.       if (! integer_zerop (offset))
  2882.         offset = convert_units (offset, BITS_PER_UNIT, 1);
  2883.       if (DECL_OFFSET (vfield))
  2884.         offset = genop (PLUS_EXPR, offset, build_int (DECL_OFFSET (vfield)));
  2885.       DECL_FIELD_CONTEXT (vfield) = t;
  2886.       DECL_OFFSET (vfield) = TREE_INT_CST_LOW (offset);
  2887.       CLASSTYPE_VFIELD (t) = vfield;
  2888.     }
  2889.     }
  2890.  
  2891.   /* Make the rtl for any new vtables we have created, and unmark
  2892.      the base types we marked.  */
  2893.   unmark_finished_struct (t);
  2894.  
  2895.   /* Now out of this class's scope.  However, if this class defined
  2896.      any new typedefs, then we must export those to the outer
  2897.      binding level.  This is unpleasant.  */
  2898.   x = gettags ();
  2899.  
  2900.   popclass (0);
  2901.  
  2902. #if 0
  2903.   /* Remove aggregate types from the list of tags,
  2904.      since these appear at global scope.  */
  2905.   while (x && IS_AGGR_TYPE (TREE_VALUE (x)))
  2906.     x = TREE_CHAIN (x);
  2907.   CLASSTYPE_TAGS (t) = x;
  2908.   y = x;
  2909.   while (x)
  2910.     {
  2911.       if (IS_AGGR_TYPE (TREE_VALUE (x)))
  2912.     TREE_CHAIN (y) = TREE_CHAIN (x);
  2913.       x = TREE_CHAIN (x);
  2914.     }
  2915. #endif
  2916.  
  2917.   hack_incomplete_structures (t);
  2918.  
  2919.   resume_momentary (old);
  2920.  
  2921.   if (flag_cadillac)
  2922.     cadillac_finish_struct (t);
  2923.  
  2924.   return t;
  2925. }
  2926.  
  2927. /* Return non-zero if the effective type of INSTANCE is static.
  2928.    Used to determine whether the virtual function table is needed
  2929.    or not.  */
  2930. int
  2931. resolves_to_fixed_type_p (instance)
  2932.      tree instance;
  2933. {
  2934.   switch (TREE_CODE (instance))
  2935.     {
  2936.     case ADDR_EXPR:
  2937.       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0));
  2938.  
  2939.     case COMPONENT_REF:
  2940.       /* Don't let pointers to members look like they hold a fixed type.  */
  2941.       if (TREE_CODE (TREE_OPERAND (instance, 1)) != FIELD_DECL)
  2942.     return 0;
  2943.  
  2944.     case VAR_DECL:
  2945.     case PARM_DECL:
  2946.     case NEW_EXPR:
  2947.       if (IS_AGGR_TYPE (TREE_TYPE (instance)))
  2948.     return 1;
  2949.  
  2950.     default:
  2951.       return 0;
  2952.     }
  2953. }
  2954.  
  2955. /* Ordering function for overload resolution.  */
  2956. int
  2957. rank_for_overload (x, y)
  2958.      struct candidate *x, *y;
  2959. {
  2960.   if (y->evil - x->evil)
  2961.     return y->evil - x->evil;
  2962.   if ((y->harshness[0] & 128) ^ (x->harshness[0] & 128))
  2963.     return y->harshness[0] - x->harshness[0];
  2964.   if (y->user - x->user)
  2965.     return y->user - x->user;
  2966.   if (y->b_or_d - x->b_or_d)
  2967.     return y->b_or_d - x->b_or_d;
  2968.   return y->easy - x->easy;
  2969. }
  2970.  
  2971. /* TYPE is the type we wish to convert to.  PARM is the parameter
  2972.    we have to work with.  We use a somewhat arbitrary cost function
  2973.    to measure this conversion.  */
  2974. static int
  2975. convert_harshness (type, parmtype, parm)
  2976.      register tree type, parmtype;
  2977.      tree parm;
  2978. {
  2979.   register enum tree_code codel = TREE_CODE (type);
  2980.   register enum tree_code coder = TREE_CODE (parmtype);
  2981.  
  2982. #ifdef GATHER_STATISTICS
  2983.   n_convert_harshness++;
  2984. #endif
  2985.  
  2986.   if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
  2987.     return 0;
  2988.  
  2989.   if (coder == ERROR_MARK)
  2990.     return 1;
  2991.  
  2992.   if (codel == POINTER_TYPE
  2993.       && (coder == METHOD_TYPE || coder == FUNCTION_TYPE))
  2994.     {
  2995.       tree p1, p2;
  2996.       int harshness, new_harshness;
  2997.  
  2998.       /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be.  */
  2999.       type = TREE_TYPE (type);
  3000.  
  3001.       if (coder != TREE_CODE (type))
  3002.     return 1;
  3003.  
  3004.       harshness = 0;
  3005.  
  3006.       /* We allow the default conversion between function type
  3007.      and pointer-to-function type for free.  */
  3008.       if (type == parmtype)
  3009.     return 0;
  3010.  
  3011.       /* Compare return types.  */
  3012.       harshness |= convert_harshness (TREE_TYPE (type), TREE_TYPE (parmtype), 0);
  3013.       if (harshness & 1)
  3014.     return 1;
  3015.       p1 = TYPE_ARG_TYPES (type);
  3016.       p2 = TYPE_ARG_TYPES (parmtype);
  3017.       while (p1 && p2)
  3018.     {
  3019.       new_harshness = convert_harshness (TREE_VALUE (p1), TREE_VALUE (p2), 0);
  3020.       if (new_harshness & 1)
  3021.         return 1;
  3022.       if ((new_harshness & 7) == 0)
  3023.         harshness += new_harshness;
  3024.       else
  3025.         harshness |= new_harshness;
  3026.       p1 = TREE_CHAIN (p1);
  3027.       p2 = TREE_CHAIN (p2);
  3028.     }
  3029.       if (p1 == p2)
  3030.     return harshness;
  3031.       if (p2)
  3032.     return 1;
  3033.       if (p1)
  3034.     return harshness | (TREE_PURPOSE (p1) == NULL_TREE);
  3035.     }
  3036.   else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
  3037.     {
  3038.       int harshness;
  3039.  
  3040.       /* Get to the OFFSET_TYPE that this might be.  */
  3041.       type = TREE_TYPE (type);
  3042.  
  3043.       if (coder != TREE_CODE (type))
  3044.     return 1;
  3045.  
  3046.       harshness = 0;
  3047.  
  3048.       if (TYPE_OFFSET_BASETYPE (type) == TYPE_OFFSET_BASETYPE (parmtype))
  3049.     harshness = 0;
  3050.       else if (get_base_type (TYPE_OFFSET_BASETYPE (type),
  3051.                   TYPE_OFFSET_BASETYPE (parmtype), 0))
  3052.     harshness = (1<<3);
  3053.       else
  3054.     return 1;
  3055.       /* Now test the OFFSET_TYPE's target compatability.  */
  3056.       type = TREE_TYPE (type);
  3057.       parmtype = TREE_TYPE (parmtype);
  3058.     }
  3059.  
  3060.   if (coder == UNKNOWN_TYPE)
  3061.     {
  3062.       if (codel == FUNCTION_TYPE
  3063.       || codel == METHOD_TYPE
  3064.       || (codel == POINTER_TYPE
  3065.           && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
  3066.           || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)))
  3067.     return 0;
  3068.       return 1;
  3069.     }
  3070.  
  3071.   if (coder == VOID_TYPE)
  3072.     return 1;
  3073.  
  3074.   if (codel == ENUMERAL_TYPE || codel == INTEGER_TYPE)
  3075.     {
  3076.       /* Control equivalence of ints an enums.  */
  3077.  
  3078.       if (codel == ENUMERAL_TYPE
  3079.       && flag_int_enum_equivalence == 0)
  3080.     {
  3081.       /* Enums can be converted to ints, but not vice-versa.  */
  3082.       if (coder != ENUMERAL_TYPE
  3083.           || TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (parmtype))
  3084.         return 1;
  3085.     }
  3086.  
  3087.       /* else enums and ints (almost) freely interconvert.  */
  3088.  
  3089.       if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
  3090.     {
  3091.       int easy = TREE_UNSIGNED (type) ^ TREE_UNSIGNED (parmtype);
  3092.       if (codel != coder)
  3093.         easy += 1;
  3094.       if (TYPE_MODE (type) != TYPE_MODE (parmtype))
  3095.         easy += 2;
  3096.       return (easy << 4);
  3097.     }
  3098.       else if (coder == REAL_TYPE)
  3099.     return (4<<4);
  3100.     }
  3101.  
  3102.   if (codel == REAL_TYPE)
  3103.     if (coder == REAL_TYPE)
  3104.       /* Shun converting between float and double if a choice exists.  */
  3105.       {
  3106.     if (TYPE_MODE (type) != TYPE_MODE (parmtype))
  3107.       return (2<<4);
  3108.     return 0;
  3109.       }
  3110.     else if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
  3111.       return (4<<4);
  3112.  
  3113.   /* convert arrays which have not previously been converted.  */
  3114.   if (codel == ARRAY_TYPE)
  3115.     codel = POINTER_TYPE;
  3116.   if (coder == ARRAY_TYPE)
  3117.     coder = POINTER_TYPE;
  3118.  
  3119.   /* Conversions among pointers */
  3120.   if (codel == POINTER_TYPE && coder == POINTER_TYPE)
  3121.     {
  3122.       register tree ttl = TYPE_MAIN_VARIANT (TREE_TYPE (type));
  3123.       register tree ttr = TYPE_MAIN_VARIANT (TREE_TYPE (parmtype));
  3124.       int penalty = 4 * (ttl != ttr);
  3125.       /* Anything converts to void *.  void * converts to anything.
  3126.      Since these may be `const void *' (etc.) use VOID_TYPE
  3127.      instead of void_type_node.
  3128.      Otherwise, the targets must be the same,
  3129.      except that we do allow (at some cost) conversion
  3130.      between signed and unsinged pointer types.  */
  3131.  
  3132.       if ((TREE_CODE (ttl) == METHOD_TYPE
  3133.        || TREE_CODE (ttl) == FUNCTION_TYPE)
  3134.       && TREE_CODE (ttl) == TREE_CODE (ttr))
  3135.     {
  3136.       if (comptypes (ttl, ttr, -1))
  3137.         return penalty<<4;
  3138.       return 1;
  3139.     }
  3140.  
  3141.       if (!(TREE_CODE (ttl) == VOID_TYPE
  3142.         || TREE_CODE (ttr) == VOID_TYPE
  3143.         || (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (ttr)
  3144.         && (ttl = unsigned_type (ttl),
  3145.             ttr = unsigned_type (ttr),
  3146.             penalty = 10, 0))
  3147.         || (comp_target_types (ttl, ttr, 0))))
  3148.     return 1;
  3149.  
  3150.       if (ttr == ttl)
  3151.     return 4;
  3152.  
  3153.       if (IS_AGGR_TYPE (ttl) && IS_AGGR_TYPE (ttr))
  3154.     {
  3155.       int b_or_d = get_base_distance (ttl, ttr, 0, 0);
  3156.       if (b_or_d < 0)
  3157.         return 1;
  3158.       return (b_or_d<<3) | 4;
  3159.     }
  3160.  
  3161.       return (penalty<<4);
  3162.     }
  3163.  
  3164.   if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
  3165.     {
  3166.       /* This is not a bad match, but don't let it beat
  3167.      integer-enum combinations.  */
  3168.       if (parm && integer_zerop (parm))
  3169.     return (4<<4);
  3170.     }
  3171.  
  3172.   /* C++: one of the types must be a reference type.  */
  3173.   {
  3174.     tree ttl, ttr;
  3175.     register tree intype = TYPE_MAIN_VARIANT (parmtype);
  3176.     register enum tree_code form = TREE_CODE (intype);
  3177.     int penalty;
  3178.  
  3179.     if (codel == REFERENCE_TYPE || coder == REFERENCE_TYPE)
  3180.       {
  3181.     ttl = TYPE_MAIN_VARIANT (type);
  3182.  
  3183.     if (codel == REFERENCE_TYPE)
  3184.       {
  3185.         ttl = TYPE_MAIN_VARIANT (TREE_TYPE (ttl));
  3186.  
  3187.         if (form == OFFSET_TYPE)
  3188.           {
  3189.         intype = TREE_TYPE (intype);
  3190.         form = TREE_CODE (intype);
  3191.           }
  3192.  
  3193.         if (form == REFERENCE_TYPE)
  3194.           {
  3195.         intype = TYPE_MAIN_VARIANT (TREE_TYPE (intype));
  3196.  
  3197.         if (ttl == intype)
  3198.           return 0;
  3199.         penalty = 2;
  3200.           }
  3201.         else
  3202.           {
  3203.         /* Can reference be built up?  */
  3204.         if (ttl == intype)
  3205.           {
  3206.             return 0;
  3207.           }
  3208.         else
  3209.           penalty = 2;
  3210.           }
  3211.       }
  3212.     else if (form == REFERENCE_TYPE)
  3213.       {
  3214.         if (parm)
  3215.           {
  3216.         tree tmp = convert_from_reference (parm);
  3217.         intype = TYPE_MAIN_VARIANT (TREE_TYPE (tmp));
  3218.           }
  3219.         else
  3220.           {
  3221.         intype = parmtype;
  3222.         do
  3223.           {
  3224.             intype = TREE_TYPE (intype);
  3225.           }
  3226.         while (TREE_CODE (intype) == REFERENCE_TYPE);
  3227.         intype = TYPE_MAIN_VARIANT (intype);
  3228.           }
  3229.  
  3230.         if (ttl == intype)
  3231.           return 0;
  3232.         else
  3233.           penalty = 2;
  3234.       }
  3235.  
  3236.     if (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (intype))
  3237.       {
  3238.         ttl = unsigned_type (ttl);
  3239.         intype = unsigned_type (intype);
  3240.         penalty += 2;
  3241.       }
  3242.  
  3243.     ttr = intype;
  3244.  
  3245.     /* If the initializer is not an lvalue, then it does not
  3246.        matter if we make life easier for the programmer
  3247.        by creating a temporary variable with which to
  3248.        hold the result.  */
  3249.     if (parm && (coder == INTEGER_TYPE
  3250.              || coder == ENUMERAL_TYPE
  3251.              || coder == REAL_TYPE)
  3252.         && ! lvalue_p (parm))
  3253.       return (convert_harshness (ttl, ttr, 0) | (penalty << 4));
  3254.  
  3255.     if (ttl == ttr)
  3256.       return 4;
  3257.  
  3258.     /* Pointers to voids always convert for pointers.  But
  3259.        make them less natural than more specific matches.  */
  3260.     if (TREE_CODE (ttl) == POINTER_TYPE && TREE_CODE (ttr) == POINTER_TYPE)
  3261.       if (TREE_TYPE (ttl) == void_type_node
  3262.           || TREE_TYPE (ttr) == void_type_node)
  3263.         return ((penalty+1)<<4);
  3264.  
  3265.     if (parm && codel != REFERENCE_TYPE)
  3266.       return (convert_harshness (ttl, ttr, 0) | (penalty << 4));
  3267.  
  3268.     /* Here it does matter.  If this conversion is from
  3269.        derived to base, allow it.  Otherwise, types must
  3270.        be compatible in the strong sense.  */
  3271.     if (IS_AGGR_TYPE (ttl) && IS_AGGR_TYPE (ttr))
  3272.       {
  3273.         int b_or_d = get_base_distance (ttl, ttr, 0, 0);
  3274.         if (b_or_d < 0)
  3275.           return 1;
  3276. #if AMBIGUOUS_WORKING
  3277.         if (ttl == TYPE_MAIN_VARIANT (type)
  3278.         && TYPE_GETS_INIT_REF (type))
  3279.           return (b_or_d<<3) | 6;
  3280. #endif
  3281.         return (b_or_d<<3) | 4;
  3282.       }
  3283.  
  3284.     if (comp_target_types (ttl, intype, 1))
  3285.       return (penalty<<4);
  3286.       }
  3287.   }
  3288.   if (codel == RECORD_TYPE && coder == RECORD_TYPE)
  3289.     {
  3290.       int b_or_d = get_base_distance (type, parmtype, 0, 0);
  3291.       if (b_or_d < 0)
  3292.     return 1;
  3293. #if AMBIGUOUS_WORKING
  3294.       if (TYPE_GETS_INIT_REF (type))
  3295.     return (b_or_d<<3) | 6;
  3296. #endif
  3297.       return (b_or_d<<3) | 4;
  3298.     }
  3299.   return 1;
  3300. }
  3301.  
  3302. /* Algorithm: Start out with no stikes against.  For each argument
  3303.    which requires a (subjective) hard conversion (such as between
  3304.    floating point and integer), issue a strike.  If there are the same
  3305.    number of formal and actual parameters in the list, there will be at
  3306.    least on strike, otherwise an exact match would have been found.  If
  3307.    there are not the same number of arguments in the type lists, we are
  3308.    not dead yet: a `...' means that we can have more parms then were
  3309.    declared, and if we wind up in the default argument section of the
  3310.    list those can be used as well.  If an exact match could be found for
  3311.    one of those cases, return it immediately.  Otherwise, Rank the fields
  3312.    so that fields with fewer strikes are tried first.
  3313.  
  3314.    Conversions between builtin and user-defined types are allowed, but
  3315.    no function involving such a conversion is prefered to one which
  3316.    does not require such a conversion.  Furthermore, such conversions
  3317.    must be unique.  */
  3318.  
  3319. void
  3320. compute_conversion_costs (function, tta_in, cp, arglen)
  3321.      tree function;
  3322.      tree tta_in;
  3323.      struct candidate *cp;
  3324.      int arglen;
  3325. {
  3326.   tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
  3327.   tree ttf = ttf_in;
  3328.   tree tta = tta_in;
  3329.  
  3330.   /* Start out with no strikes against.  */
  3331.   int evil_strikes = 0;
  3332.   int user_strikes = 0;
  3333.   int b_or_d_strikes = 0;
  3334.   int easy_strikes = 0;
  3335.  
  3336.   int strike_index = 0, win, lose;
  3337.  
  3338. #ifdef GATHER_STATISTICS
  3339.   n_compute_conversion_costs++;
  3340. #endif
  3341.  
  3342.   cp->function = function;
  3343.   cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
  3344.   cp->u.bad_arg = 0;        /* optimistic!  */
  3345.  
  3346.   bzero (cp->harshness, (arglen+1) * sizeof (short));
  3347.  
  3348.   while (ttf && tta)
  3349.     {
  3350.       int harshness;
  3351.  
  3352.       if (ttf == void_list_node)
  3353.     break;
  3354.  
  3355.       if (type_unknown_p (TREE_VALUE (tta)))
  3356.     {      
  3357.       /* Must perform some instantiation here.  */
  3358.       tree rhs = TREE_VALUE (tta);
  3359.       tree lhstype = TREE_VALUE (ttf);
  3360.  
  3361.       /* @@ This is to undo what `grokdeclarator' does to
  3362.          parameter types.  It really should go through
  3363.          something more general.  */
  3364.  
  3365.       TREE_TYPE (tta) = unknown_type_node;
  3366.       if (TREE_CODE (rhs) == OP_IDENTIFIER)
  3367.         rhs = build_instantiated_decl (lhstype, rhs);
  3368.       else
  3369.         {
  3370.           /* Keep quiet about possible contravariance violations.  */
  3371.           extern int inhibit_warnings;
  3372.           int old_inhibit_warnings = inhibit_warnings;
  3373.           inhibit_warnings = 1;
  3374.  
  3375.           rhs = instantiate_type (lhstype, rhs, 0);
  3376.  
  3377.           inhibit_warnings = old_inhibit_warnings;
  3378.         }
  3379.  
  3380.       if (TREE_CODE (rhs) == ERROR_MARK)
  3381.         harshness = 1;
  3382.       else
  3383.         {
  3384.           harshness = convert_harshness (lhstype, TREE_TYPE (rhs), rhs);
  3385.           /* harshness |= 2; */
  3386.         }
  3387.     }
  3388.       else
  3389.     harshness = convert_harshness (TREE_VALUE (ttf), TREE_TYPE (TREE_VALUE (tta)), TREE_VALUE (tta));
  3390.  
  3391.       cp->harshness[strike_index] = harshness;
  3392.       if (harshness & 1)
  3393.     {
  3394.       cp->u.bad_arg = strike_index;
  3395.       evil_strikes = 1;
  3396.     }
  3397.       else if (harshness & 2)
  3398.     {
  3399.       user_strikes += 1;
  3400.     }
  3401.       else if (harshness & 4)
  3402.     {
  3403.       b_or_d_strikes += (harshness >> 3);
  3404.     }
  3405.       else
  3406.     easy_strikes += harshness >> 4;
  3407.       ttf = TREE_CHAIN (ttf);
  3408.       tta = TREE_CHAIN (tta);
  3409.       strike_index += 1;
  3410.     }
  3411.  
  3412.   if (tta)
  3413.     {
  3414.       /* ran out of formals, and parmlist is fixed size.  */
  3415.       if (ttf /* == void_type_node */)
  3416.     {
  3417.       cp->evil = 1;
  3418.       cp->u.bad_arg = -1;
  3419.       return;
  3420.     }
  3421.     }
  3422.   else if (ttf && ttf != void_list_node)
  3423.     {
  3424.       /* ran out of actuals, and no defaults.  */
  3425.       if (TREE_PURPOSE (ttf) == NULL_TREE)
  3426.     {
  3427.       cp->evil = 1;
  3428.       cp->u.bad_arg = -2;
  3429.       return;
  3430.     }
  3431.       /* Store index of first default.  */
  3432.       cp->harshness[arglen] = strike_index+1;
  3433.     }
  3434.   else cp->harshness[arglen] = 0;
  3435.  
  3436.   /* Argument list lengths work out, so don't need to check them again.  */
  3437.   if (evil_strikes)
  3438.     {
  3439.       /* We do not check for derived->base conversions here, since in
  3440.      no case would they give evil strike counts, unless such conversions
  3441.      are somehow ambiguous.  */
  3442.  
  3443.       /* See if any user-defined conversions apply.
  3444.          But make sure that we do not loop.  */
  3445.       static int dont_convert_types = 0;
  3446.  
  3447.       if (dont_convert_types)
  3448.     {
  3449.       cp->evil = 1;
  3450.       return;
  3451.     }
  3452.  
  3453.       win = 0;            /* Only get one chance to win.  */
  3454.       ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
  3455.       tta = tta_in;
  3456.       strike_index = 0;
  3457.       evil_strikes = 0;
  3458.  
  3459.       while (ttf && tta)
  3460.     {
  3461.       if (ttf == void_list_node)
  3462.         break;
  3463.  
  3464.       lose = cp->harshness[strike_index];
  3465.       if (lose&1)
  3466.         {
  3467.           tree actual_type = TREE_TYPE (TREE_VALUE (tta));
  3468.           tree formal_type = TREE_VALUE (ttf);
  3469.  
  3470.           dont_convert_types = 1;
  3471.  
  3472.           if (TREE_CODE (formal_type) == REFERENCE_TYPE)
  3473.         formal_type = TREE_TYPE (formal_type);
  3474.           if (TREE_CODE (actual_type) == REFERENCE_TYPE)
  3475.         actual_type = TREE_TYPE (actual_type);
  3476.  
  3477.           if (formal_type != error_mark_node
  3478.           && actual_type != error_mark_node)
  3479.         {
  3480.           formal_type = TYPE_MAIN_VARIANT (formal_type);
  3481.           actual_type = TYPE_MAIN_VARIANT (actual_type);
  3482.  
  3483.           if (TYPE_HAS_CONSTRUCTOR (formal_type))
  3484.             {
  3485.               /* If it has a constructor for this type, try to use it.  */
  3486.               if (convert_to_aggr (formal_type, TREE_VALUE (tta), 0, 1)
  3487.               != error_mark_node)
  3488.             {
  3489.               /* @@ There is no way to save this result yet.
  3490.                  @@ So success is NULL_TREE for now.  */
  3491.               win++;
  3492.             }
  3493.             }
  3494.           if (TYPE_LANG_SPECIFIC (actual_type) && TYPE_HAS_CONVERSION (actual_type))
  3495.             {
  3496.               if (TREE_CODE (formal_type) == INTEGER_TYPE
  3497.               && TYPE_HAS_INT_CONVERSION (actual_type))
  3498.             win++;
  3499.               else if (TREE_CODE (formal_type) == REAL_TYPE
  3500.                    && TYPE_HAS_REAL_CONVERSION (actual_type))
  3501.             win++;
  3502.               else
  3503.             {
  3504.               tree conv = build_type_conversion (CALL_EXPR, TREE_VALUE (ttf), TREE_VALUE (tta), 0);
  3505.               if (conv)
  3506.                 {
  3507.                   if (conv == error_mark_node)
  3508.                 win += 2;
  3509.                   else
  3510.                 win++;
  3511.                 }
  3512.               else if (TREE_CODE (TREE_VALUE (ttf)) == REFERENCE_TYPE)
  3513.                 {
  3514.                   conv = build_type_conversion (CALL_EXPR, formal_type, TREE_VALUE (tta), 0);
  3515.                   if (conv)
  3516.                 {
  3517.                   if (conv == error_mark_node)
  3518.                     win += 2;
  3519.                   else
  3520.                     win++;
  3521.                 }
  3522.                 }
  3523.             }
  3524.             }
  3525.         }
  3526.           dont_convert_types = 0;
  3527.  
  3528.           if (win == 1)
  3529.         {
  3530.           user_strikes += 1;
  3531.           cp->harshness[strike_index] = 2;
  3532.           win = 0;
  3533.         }
  3534.           else
  3535.         {
  3536.           if (cp->u.bad_arg > strike_index)
  3537.             cp->u.bad_arg = strike_index;
  3538.  
  3539.           evil_strikes = win ? 2 : 1;
  3540.           break;
  3541.         }
  3542.         }
  3543.  
  3544.       ttf = TREE_CHAIN (ttf);
  3545.       tta = TREE_CHAIN (tta);
  3546.       strike_index += 1;
  3547.     }
  3548.     }
  3549.  
  3550.   /* Calling a non-const member function from a const member function
  3551.      is probably invalid, but for now we let it only draw a warning.
  3552.      We indicate that such a mismatch has occured by setting the
  3553.      harshness to a maximum value.  */
  3554.   if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE
  3555.       && TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in))) == POINTER_TYPE
  3556.       && (TREE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in))))
  3557.       > TREE_READONLY (TREE_TYPE (TREE_VALUE (ttf_in)))))
  3558.     cp->harshness[0] |= 128;
  3559.  
  3560.   cp->evil = evil_strikes;
  3561.   cp->user = user_strikes;
  3562.   cp->b_or_d = b_or_d_strikes;
  3563.   cp->easy = easy_strikes;
  3564. }
  3565.  
  3566. struct candidate *
  3567. ideal_candidate (basetype, candidates, n_candidates, parms, len)
  3568.      tree basetype;
  3569.      struct candidate *candidates;
  3570.      int n_candidates;
  3571.      tree parms;
  3572.      int len;
  3573. {
  3574.   struct candidate *cp = candidates + n_candidates;
  3575.   int index, i;
  3576.   tree ttf;
  3577.  
  3578.   qsort (candidates,        /* char *base */
  3579.      n_candidates,        /* int nel */
  3580.      sizeof (struct candidate), /* int width */
  3581.      rank_for_overload);    /* int (*compar)() */
  3582.  
  3583.   /* If the best candidate requires user-defined conversions,
  3584.      and its user-defined conversions are a strict subset
  3585.      of all other candidates requiring user-defined conversions,
  3586.      then it is, in fact, the best.  */
  3587.   for (i = -1; cp + i != candidates; i--)
  3588.     if (cp[i].user == 0)
  3589.       break;
  3590.  
  3591.   if (i < -1)
  3592.     {
  3593.       tree ttf0;
  3594.  
  3595.       /* Check that every other candidate requires those conversions
  3596.      as a strict subset of their conversions.  */
  3597.       if (cp[i].user == cp[-1].user)
  3598.     goto non_subset;
  3599.  
  3600.       /* Look at subset relationship more closely.  */
  3601.       while (i != -1)
  3602.     {
  3603.       for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[i].function)),
  3604.            ttf0 = TYPE_ARG_TYPES (TREE_TYPE (cp[-1].function)),
  3605.            index = 0;
  3606.            index < len;
  3607.            ttf = TREE_CHAIN (ttf), ttf0 = TREE_CHAIN (ttf0), index++)
  3608.         if (cp[i].harshness[index] & 2)
  3609.           {
  3610.         /* If our "best" candidate also needs a conversion,
  3611.            it must be the same one.  */
  3612.         if ((cp[-1].harshness[index] & 2)
  3613.             && TREE_VALUE (ttf) != TREE_VALUE (ttf0))
  3614.           goto non_subset;
  3615.           }
  3616.       i++;
  3617.     }
  3618.       /* The best was the best.  */
  3619.       return cp - 1;
  3620.     non_subset:
  3621.       /* Use other rules for determining "bestness".  */
  3622.       ;
  3623.     }
  3624.  
  3625.   /* If the best two candidates we find require user-defined
  3626.      conversions, we may need to report and error message.  */
  3627.   if (cp[-1].user && cp[-2].user
  3628.       && (cp[-1].b_or_d || cp[-2].b_or_d == 0))
  3629.     {
  3630.       /* If the best two methods found involved user-defined
  3631.      type conversions, then we must see whether one
  3632.      of them is exactly what we wanted.  If not, then
  3633.      we have an ambiguity.  */
  3634.       int best = 0;
  3635.       tree tta = parms;
  3636.       tree f1, p1;
  3637.  
  3638. #if AMBIGUOUS_WORKING
  3639.       if (cp[-1].b_or_d == 0
  3640.       && cp[-1].easy == 0
  3641.       && (cp[-2].b_or_d | cp[-2].easy) > 0)
  3642.     return cp - 1;
  3643. #endif
  3644.  
  3645.       /* Stash all of our parameters in safe places
  3646.      so that we can perform type conversions in place.  */
  3647.       while (tta)
  3648.     {
  3649.       TREE_PURPOSE (tta) = TREE_VALUE (tta);
  3650.       tta = TREE_CHAIN (tta);
  3651.     }
  3652.  
  3653.       i = 0;
  3654.       do
  3655.     {
  3656.       int exact_conversions = 0;
  3657.  
  3658.       i -= 1;
  3659.       tta = parms;
  3660.       if (DECL_STATIC_FUNCTION_P (cp[i].function))
  3661.         tta = TREE_CHAIN (tta);
  3662.       for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[i].function)), index = 0;
  3663.            index < len;
  3664.            tta = TREE_CHAIN (tta), ttf = TREE_CHAIN (ttf), index++)
  3665.         {
  3666.           if (cp[i].harshness[index] & 2)
  3667.         {
  3668.           TREE_VALUE (tta)
  3669.             = build_type_conversion (CALL_EXPR, TREE_VALUE (ttf), TREE_PURPOSE (tta), 2);
  3670.           if (TREE_VALUE (tta))
  3671.             {
  3672.               if (TREE_CODE (TREE_VALUE (tta)) != CONVERT_EXPR
  3673.               && (TREE_CODE (TREE_VALUE (tta)) != NOP_EXPR
  3674.                   || comp_target_types (TREE_TYPE (TREE_VALUE (tta)),
  3675.                             TREE_TYPE (TREE_OPERAND (TREE_VALUE (tta), 0)), 1)))
  3676.             exact_conversions += 1;
  3677.             }
  3678.           else if (IS_AGGR_TYPE (TREE_VALUE (ttf))
  3679.                || (TREE_CODE (TREE_VALUE (ttf)) == REFERENCE_TYPE
  3680.                    && IS_AGGR_TYPE (TREE_TYPE (TREE_VALUE (ttf)))))
  3681.             {
  3682.               /* To get here we had to have succeeded via
  3683.              a constructor.  */
  3684.               TREE_VALUE (tta) = TREE_PURPOSE (tta);
  3685.               exact_conversions += 1;
  3686.             }
  3687.         }
  3688.         }
  3689.       if (exact_conversions == cp[i].user)
  3690.         {
  3691.           if (best == 0)
  3692.         {
  3693.           best = i;
  3694.           f1 = cp[best].function;
  3695.           p1 = TYPE_ARG_TYPES (TREE_TYPE (f1));
  3696.         }
  3697.           else
  3698.         {
  3699.           /* Don't complain if next best is from base class.  */
  3700.           tree f2 = cp[i].function;
  3701.           tree p2 = TYPE_ARG_TYPES (TREE_TYPE (f2));
  3702.  
  3703.           if (TREE_CODE (TREE_TYPE (f1)) == METHOD_TYPE
  3704.               && TREE_CODE (TREE_TYPE (f2)) == METHOD_TYPE
  3705.               && (cp[i].harshness[0] & 4) != 0
  3706.               && cp[best].harshness[0] < cp[i].harshness[0])
  3707.             {
  3708. #if 0
  3709.               /* For LUCID.  */
  3710.               if (! compparms (TREE_CHAIN (p1), TREE_CHAIN (p2), 1))
  3711.             goto ret0;
  3712.               else
  3713. #endif
  3714.             continue;
  3715.             }
  3716.           else goto ret0;
  3717.         }
  3718.         }
  3719.     } while (cp + i != candidates);
  3720.  
  3721.       if (best)
  3722.     {
  3723.       int exact_conversions = cp[best].user;
  3724.       tta = parms;
  3725.       if (DECL_STATIC_FUNCTION_P (cp[best].function))
  3726.         tta = TREE_CHAIN (parms);
  3727.       for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[best].function)), index = 0;
  3728.            exact_conversions > 0;
  3729.            tta = TREE_CHAIN (tta), ttf = TREE_CHAIN (ttf), index++)
  3730.         {
  3731.           if (cp[best].harshness[index] & 2)
  3732.         {
  3733.           /* We must now fill in the slot we left behind.
  3734.              @@ This could be optimized to use the value previously
  3735.              @@ computed by build_type_conversion in some cases.  */
  3736.           TREE_VALUE (tta) = convert (TREE_VALUE (ttf), TREE_PURPOSE (tta));
  3737.           exact_conversions -= 1;
  3738.         }
  3739.           else TREE_VALUE (tta) = TREE_PURPOSE (tta);
  3740.         }
  3741.       return cp + best;
  3742.     }
  3743.       goto ret0;
  3744.     }
  3745.   /* If the best two candidates we find both use default parameters,
  3746.      we may need to report and error.  Don't need to worry if next-best
  3747.      candidate is forced to use user-defined conversion when best is not.  */
  3748.   if (cp[-2].user == 0
  3749.       && cp[-1].harshness[len] != 0 && cp[-2].harshness[len] != 0)
  3750.     {
  3751.       tree tt1 = TYPE_ARG_TYPES (TREE_TYPE (cp[-1].function));
  3752.       tree tt2 = TYPE_ARG_TYPES (TREE_TYPE (cp[-2].function));
  3753.       int i = cp[-1].harshness[len];
  3754.       if (cp[-2].harshness[len] < i)
  3755.     i = cp[-2].harshness[len];
  3756.       while (--i > 0)
  3757.     {
  3758.       if (TYPE_MAIN_VARIANT (TREE_VALUE (tt1))
  3759.           != TYPE_MAIN_VARIANT (TREE_VALUE (tt2)))
  3760.         /* These lists are not identical, so we can choose our best candidate.  */
  3761.         return cp - 1;
  3762.       tt1 = TREE_CHAIN (tt1);
  3763.       tt2 = TREE_CHAIN (tt2);
  3764.     }
  3765.       /* To get here, both lists had the same parameters up to the defaults
  3766.      which were used.  This is an ambiguous request.  */
  3767.       goto ret0;
  3768.     }
  3769.  
  3770.   /* Otherwise, return our best candidate.  Note that if we get candidates
  3771.      from independent base classes, we have an ambiguity, even if one
  3772.      argument list look a little better than another one.  */
  3773.   if (cp[-1].b_or_d && basetype && TYPE_USES_MULTIPLE_INHERITANCE (basetype))
  3774.     {
  3775.       int i = n_candidates - 1, best;
  3776.       tree base1 = NULL_TREE;
  3777.  
  3778.       if (TREE_CODE (TREE_TYPE (candidates[i].function)) == FUNCTION_TYPE)
  3779.     return cp - 1;
  3780.  
  3781.       for (; i >= 0 && candidates[i].user == 0 && candidates[i].evil == 0; i--)
  3782.     {
  3783.       if (TREE_CODE (TREE_TYPE (candidates[i].function)) == METHOD_TYPE)
  3784.         {
  3785.           tree newbase = TYPE_METHOD_BASETYPE (TREE_TYPE (candidates[i].function));
  3786.  
  3787.           if (base1 != NULL_TREE)
  3788.         {
  3789.           if (newbase != base1
  3790.               && ! get_base_type (newbase, base1, 0))
  3791.             {
  3792.               char *buf = (char *)alloca (8192);
  3793.               error ("ambiguous request for function from distinct base classes of type `%s'", TYPE_NAME_STRING (basetype));
  3794.               error ("first candidate is `%s'", fndecl_as_string (buf, 0, candidates[best].function, 1));
  3795.               error ("second candidates is `%s'", fndecl_as_string (buf, 0, candidates[i].function, 1));
  3796.               return cp - 1;
  3797.             }
  3798.         }
  3799.           else
  3800.         {
  3801.           best = i;
  3802.           base1 = newbase;
  3803.         }
  3804.         }
  3805.       else return cp - 1;
  3806.     }
  3807.     }
  3808.  
  3809. #if AMBIGUOUS_WORKING
  3810.   if (cp[-1].user == cp[-2].user
  3811.       && cp[-1].b_or_d == cp[-2].b_or_d
  3812.       && cp[-1].easy == cp[-2].easy)
  3813.     goto ret0;
  3814. #endif
  3815.  
  3816.   return cp - 1;
  3817.  
  3818.  ret0:
  3819.   /* In the case where there is no ideal candidate, restore
  3820.      TREE_VALUE slots of PARMS from TREE_PURPOSE slots.  */
  3821.   while (parms)
  3822.     {
  3823.       TREE_VALUE (parms) = TREE_PURPOSE (parms);
  3824.       parms = TREE_CHAIN (parms);
  3825.     }
  3826.   return 0;
  3827. }
  3828.  
  3829. /* Assume that if the class referred to is not in the
  3830.    current class hierarchy, that it may be remote.
  3831.    PARENT is assumed to be of aggregate type here.  */
  3832. static int
  3833. may_be_remote (parent)
  3834.      tree parent;
  3835. {
  3836.   if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent) == 0)
  3837.     return 0;
  3838.  
  3839.   if (current_class_type == NULL_TREE)
  3840.     return 0;
  3841.   if (parent == current_class_type)
  3842.     return 0;
  3843.  
  3844.   if (get_base_type (parent, current_class_type, 0))
  3845.     return 0;
  3846.   return 1;
  3847. }
  3848.  
  3849. /* Return the number of bytes that the arglist in PARMS would
  3850.    occupy on the stack.  */
  3851. int
  3852. get_arglist_len_in_bytes (parms)
  3853.      tree parms;
  3854. {
  3855.   register tree parm;
  3856.   register int bytecount = 0;
  3857.  
  3858.   for (parm = parms; parm; parm = TREE_CHAIN (parm))
  3859.     {
  3860.       register tree pval = TREE_VALUE (parm);
  3861.       register int used, size;
  3862.  
  3863.       if (TREE_CODE (pval) == ERROR_MARK)
  3864.     continue;
  3865.       else if (TYPE_MODE (TREE_TYPE (pval)) != BLKmode)
  3866.     {
  3867.       used = size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (pval)));
  3868. #ifdef PUSH_ROUNDING
  3869.       size = PUSH_ROUNDING (size);
  3870. #endif
  3871.       used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
  3872.            / (PARM_BOUNDARY / BITS_PER_UNIT))
  3873.           * (PARM_BOUNDARY / BITS_PER_UNIT));
  3874.     }
  3875.       else
  3876.     {
  3877.       register tree size = size_in_bytes (TREE_TYPE (pval));
  3878.       register tree used_t = convert_units (convert_units (size, BITS_PER_UNIT, PARM_BOUNDARY),
  3879.                         PARM_BOUNDARY, BITS_PER_UNIT);
  3880.       used = TREE_INT_CST_LOW (used_t);
  3881.     }
  3882.       bytecount += used;
  3883.     }
  3884.   return bytecount;
  3885. }
  3886.  
  3887. tree
  3888. build_vfield_ref (datum, type)
  3889.      tree datum, type;
  3890. {
  3891.   if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
  3892.     datum = convert_from_reference (datum);
  3893.  
  3894.   if (! TYPE_USES_VIRTUAL_BASECLASSES (type))
  3895.     return build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
  3896.           datum, CLASSTYPE_VFIELD (type));
  3897.   return build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), 0, 0);
  3898. }
  3899.  
  3900. /* Build a call to a member of an object.  I.e., one that overloads
  3901.    operator ()(), or is a pointer-to-function or pointer-to-method.  */
  3902. static tree
  3903. build_field_call (basetype_path, instance_ptr, name, parms, err_name)
  3904.      tree basetype_path;
  3905.      tree instance_ptr, name, parms;
  3906.      char *err_name;
  3907. {
  3908.   tree field, instance;
  3909.  
  3910.   if (instance_ptr == current_class_decl)
  3911.     {
  3912.       /* Check to see if we really have a reference to an instance variable
  3913.      with `operator()()' overloaded.  */
  3914. #if 1
  3915.       field = IDENTIFIER_CLASS_VALUE (name);
  3916. #else
  3917.       field = identifier_class_value (name);
  3918. #endif
  3919.  
  3920.       if (field == NULL_TREE)
  3921.     {
  3922.       error ("`this' has no member named `%s'", err_name);
  3923.       return error_mark_node;
  3924.     }
  3925.  
  3926.       if (TREE_CODE (field) == FIELD_DECL)
  3927.     {
  3928.       /* If it's a field, try overloading operator (),
  3929.          or calling if the field is a pointer-to-function.  */
  3930.       instance = build_component_ref_1 (C_C_D, field, 0, 1);
  3931.       if (instance == error_mark_node)
  3932.         return error_mark_node;
  3933.  
  3934.       if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
  3935.           && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance)))
  3936.         return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms);
  3937.  
  3938.       if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
  3939.         if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
  3940.           return build_function_call (instance, parms);
  3941.         else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
  3942.           return build_function_call (instance, tree_cons (NULL_TREE, current_class_decl, parms));
  3943.     }
  3944.       return NULL_TREE;
  3945.     }
  3946.  
  3947.   /* Check to see if this is not really a reference to an instance variable
  3948.      with `operator()()' overloaded.  */
  3949.   field = lookup_field (basetype_path, name, 1);
  3950.  
  3951.   /* This can happen if the reference was ambiguous
  3952.      or for visibility violations.  */
  3953.   if (field == error_mark_node)
  3954.     return error_mark_node;
  3955.   if (field)
  3956.     {
  3957.       tree basetype;
  3958.       tree ftype = TREE_TYPE (field);
  3959.  
  3960.       if (TYPE_LANG_SPECIFIC (ftype) && TYPE_OVERLOADS_CALL_EXPR (ftype))
  3961.     {
  3962.       /* Make the next search for this field very short.  */
  3963.       basetype = DECL_FIELD_CONTEXT (field);
  3964.       instance_ptr = convert_pointer_to (basetype, instance_ptr);
  3965.  
  3966.       instance = build_indirect_ref (instance_ptr, 0);
  3967.       return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
  3968.                  build_component_ref_1 (instance, field, 0, 0),
  3969.                  parms);
  3970.     }
  3971.       if (TREE_CODE (ftype) == POINTER_TYPE)
  3972.     {
  3973.       if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
  3974.           || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
  3975.         {
  3976.           /* This is a member which is a pointer to function.  */
  3977.           tree ref = build_component_ref_1 (build_indirect_ref (instance_ptr, 0, 0),
  3978.                         field, LOOKUP_COMPLAIN);
  3979.           if (ref == error_mark_node)
  3980.         return error_mark_node;
  3981.           return build_function_call (ref, parms);
  3982.         }
  3983.     }
  3984.       else if (TREE_CODE (ftype) == METHOD_TYPE)
  3985.     {
  3986.       error ("invalid call via pointer-to-member function");
  3987.       return error_mark_node;
  3988.     }
  3989.       else
  3990.     return NULL_TREE;
  3991.     }
  3992.   return NULL_TREE;
  3993. }
  3994.  
  3995. /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
  3996.    This is how virtual function calls are avoided.  */
  3997. tree
  3998. build_scoped_method_call (exp, scopes, name, parms)
  3999.      tree exp;
  4000.      tree scopes;
  4001.      tree name;
  4002.      tree parms;
  4003. {
  4004.   /* Because this syntactic form does not allow
  4005.      a pointer to a base class to be `stolen',
  4006.      we need not protect the drived->base conversion
  4007.      that happens here.
  4008.      
  4009.      @@ But we do have to check visibility privileges later.  */
  4010.   tree basename = (TREE_CODE (scopes) == SCOPE_REF) ? TREE_OPERAND (scopes, 1) : scopes;
  4011.   tree basetype, decl;
  4012.   tree type = TREE_TYPE (exp);
  4013.  
  4014.   if (type == error_mark_node
  4015.       || ! is_aggr_typedef (basename, 1))
  4016.     return error_mark_node;
  4017.  
  4018.   if (! IS_AGGR_TYPE (type))
  4019.     {
  4020.       error ("base object of scoped method call is not of aggregate type");
  4021.       return error_mark_node;
  4022.     }
  4023.  
  4024.   basetype = TREE_TYPE (TREE_TYPE (basename));
  4025.  
  4026.   if (basetype = basetype_or_else (basetype, type))
  4027.     {
  4028.       if (basetype == error_mark_node)
  4029.     return error_mark_node;
  4030.       if (TREE_CODE (exp) == INDIRECT_REF)
  4031.     decl = build_indirect_ref (convert_pointer_to (basetype,
  4032.                                build_unary_op (ADDR_EXPR, exp, 0)), 0);
  4033.       else
  4034.     decl = build_scoped_ref (exp, scopes);
  4035.  
  4036.       /* Call to a destructor.  */
  4037.       if (TREE_CODE (name) == BIT_NOT_EXPR)
  4038.     {
  4039.       /* Explicit call to destructor.  */
  4040.       name = TREE_OPERAND (name, 0);
  4041.       if (! is_aggr_typedef (name, 1))
  4042.         return error_mark_node;
  4043.       if (TREE_TYPE (decl) != TREE_TYPE (TREE_TYPE (name)))
  4044.         {
  4045.           error_with_aggr_type (TREE_TYPE (decl),
  4046.                     "qualified type `%s' does not match destructor type `%s'",
  4047.                     IDENTIFIER_POINTER (name));
  4048.           return error_mark_node;
  4049.         }
  4050.       if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
  4051.         error_with_aggr_type (TREE_TYPE (decl), "type `%s' has no destructor");
  4052.       return build_delete (TREE_TYPE (decl), decl, integer_two_node,
  4053.                    LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
  4054.     }
  4055.  
  4056.       /* Call to a method.  */
  4057.       return build_method_call (decl, name, parms, NULL_TREE,
  4058.                 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
  4059.     }
  4060.   return error_mark_node;
  4061. }
  4062.  
  4063. /* Build something of the form ptr->method (args)
  4064.    or object.method (args).  This can also build
  4065.    calls to constructors, and find friends.
  4066.  
  4067.    Member functions always take their class variable
  4068.    as a pointer.
  4069.  
  4070.    INSTANCE is a class instance.
  4071.  
  4072.    NAME is the NAME field of the struct, union, or class
  4073.    whose type is that of INSTANCE.
  4074.  
  4075.    PARMS help to figure out what that NAME really refers to.
  4076.  
  4077.    BASETYPE_PATH, if non-NULL, tells which basetypes of INSTANCE
  4078.    we should be traversed before starting our search.  We need
  4079.    this information to get protected accesses correct.
  4080.  
  4081.    FLAGS is the logical disjunction of zero or more LOOKUP_
  4082.    flags.  See cplus-tree.h for more info.
  4083.  
  4084.    If this is all OK, calls build_function_call with the resolved
  4085.    member function.
  4086.  
  4087.    This function must also handle being called to perform
  4088.    initialization, promotion/coercion of arguments, and
  4089.    instantiation of default parameters.
  4090.  
  4091.    Note that NAME may refer to an instance variable name.  If
  4092.    `operator()()' is defined for the type of that field, then we return
  4093.    that result.  */
  4094. tree
  4095. build_method_call (instance, name, parms, basetype_path, flags)
  4096.      tree instance, name, parms, basetype_path;
  4097.      int flags;
  4098. {
  4099.   register tree function, fntype, value_type;
  4100.   register tree basetype, save_basetype;
  4101.   register tree baselink, result, method_name, parmtypes, parm;
  4102.   tree last;
  4103.   int pass;
  4104.   enum visibility_type visibility;
  4105.   int rank_for_overload ();
  4106.  
  4107.   /* Range of cases for vtable optimization.  */
  4108.   enum vtable_needs
  4109.     {
  4110.       not_needed, maybe_needed, unneeded, needed,
  4111.     };
  4112.   enum vtable_needs need_vtbl = not_needed;
  4113.  
  4114.   char *err_name;
  4115.   char *name_kind;
  4116.   int ever_seen = 0;
  4117.   int wrap;
  4118.   tree wrap_type;
  4119.   tree instance_ptr = NULL_TREE;
  4120.   int all_virtual = flag_all_virtual;
  4121.   int static_call_context;
  4122.   tree saw_private = 0;
  4123.   tree saw_protected = 0;
  4124. #ifdef SOS
  4125.   /* If call is a call to a constructor, then `dtbl'
  4126.      will first be initialized with the function table pointer
  4127.      of the appropriate type (calling "sosFindCode" as a last
  4128.      resort), the the call to the constructor will go through there.  */
  4129.   tree dtbl = (flags & LOOKUP_DYNAMIC) ? TREE_VALUE (parms) : NULL_TREE;
  4130.  
  4131.   /* Flag saying whether or not `dtbl' has been inserted into the
  4132.      parameter list.  This is needed because we cannot tell (until
  4133.      we have a match) whether this parameter should go in or not.
  4134.  
  4135.      If 1, then `dtbl' is living naturally.
  4136.      If 0, then `dtbl' is not among the parms that we know about.
  4137.      If -1, the `dtbl' was place into the parms unnaturally.
  4138.  
  4139.      Note that we may side-effect the parameter list, but in such a way
  4140.      that the caller of this function would never know.  */
  4141.   int dtbl_inserted = (flags & LOOKUP_DYNAMIC);
  4142. #endif
  4143.  
  4144.   /* Keep track of `const' and `volatile' objects.  */
  4145.   int constp, volatilep;
  4146.  
  4147.   /* Know if this is explicit destructor call.  */
  4148.   int dtor_specd = 0;
  4149.  
  4150. #ifdef GATHER_STATISTICS
  4151.   n_build_method_call++;
  4152. #endif
  4153.  
  4154.   if (instance == error_mark_node
  4155.       || name == error_mark_node
  4156.       || parms == error_mark_node
  4157.       || (instance != 0 && TREE_TYPE (instance) == error_mark_node))
  4158.     return error_mark_node;
  4159.  
  4160. #if 0
  4161.   /* C++ 2.1 does not allow this, but ANSI probably will.  */
  4162.   if (TREE_CODE (name) == BIT_NOT_EXPR)
  4163.     {
  4164.       error ("invalid call to destructor, use qualified name `%s::~%s'",
  4165.          IDENTIFIER_POINTER (name), IDENTIFIER_POINTER (name));
  4166.       return error_mark_node;
  4167.     }
  4168. #else
  4169.   if (TREE_CODE (name) == BIT_NOT_EXPR)
  4170.     {
  4171.       flags |= LOOKUP_DESTRUCTOR;
  4172.       name = TREE_OPERAND (name, 0);
  4173.       if (! is_aggr_typedef (name, 1))
  4174.     return error_mark_node;
  4175.       if (parms)
  4176.     error ("destructors take no parameters");
  4177.       basetype = TREE_TYPE (TREE_TYPE (name));
  4178.       if (! TYPE_HAS_DESTRUCTOR (basetype))
  4179.     error_with_aggr_type (basetype, "type `%s' has no destructor");
  4180.       instance = default_conversion (instance);
  4181.       if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
  4182.     instance_ptr = instance;
  4183.       else
  4184.     instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
  4185.       return build_delete (basetype, instance_ptr, integer_two_node,
  4186.                LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
  4187.     }
  4188. #endif
  4189.  
  4190.   if (TREE_CODE (name) == WRAPPER_EXPR)
  4191.     {
  4192.       wrap_type = TREE_OPERAND (name, 0);
  4193.       name = TREE_OPERAND (name, 1);
  4194.       wrap = 1;
  4195.     }
  4196.   else if (TREE_CODE (name) == ANTI_WRAPPER_EXPR)
  4197.     {
  4198.       wrap_type = TREE_OPERAND (name, 0);
  4199.       name = TREE_OPERAND (name, 1);
  4200.       wrap = -1;
  4201.     }
  4202.   else
  4203.     {
  4204.       wrap_type = NULL_TREE;
  4205.       wrap = 0;
  4206.     }
  4207.  
  4208.   /* Initialize name for error reporting.  */
  4209.   if (TREE_CODE (name) == OP_IDENTIFIER)
  4210.     name = build_operator_fnname (&name, parms, 1);
  4211.  
  4212.   if (OPERATOR_NAME_P (name))
  4213.     {
  4214.       char *p = operator_name_string (name);
  4215.       err_name = (char *)alloca (strlen (p) + 10);
  4216.       sprintf (err_name, "operator %s", p);
  4217.     }
  4218.   else if (name == wrapper_name)
  4219.     err_name = "wrapper";
  4220.   else if (OPERATOR_TYPENAME_P (name))
  4221.     err_name = "type conversion operator";
  4222.   else if (TREE_CODE (name) == SCOPE_REF)
  4223.     err_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
  4224.   else
  4225.     err_name = IDENTIFIER_POINTER (name);
  4226.  
  4227. #ifdef FIELD_XREF
  4228.   FIELD_xref_call(current_function_decl,err_name);
  4229. #endif
  4230.  
  4231.   if (wrap)
  4232.     {
  4233.       char *p = (char *)alloca (strlen (err_name) + 32);
  4234.       sprintf (p, "%s for `%s'", wrap < 0 ? "anti-wrapper" : "wrapper", err_name);
  4235.       err_name = p;
  4236.     }
  4237.  
  4238.   if (instance == NULL_TREE)
  4239.     {
  4240.       static_call_context = 0;
  4241.  
  4242.       basetype = NULL_TREE;
  4243.       /* Check cases where this is really a call to raise
  4244.      an exception.  */
  4245.       if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
  4246.     {
  4247.       basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
  4248.       if (basetype)
  4249.         basetype = TREE_VALUE (basetype);
  4250.     }
  4251.       else if (TREE_CODE (name) == SCOPE_REF
  4252.            && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
  4253.     {
  4254.       if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
  4255.         return error_mark_node;
  4256.       basetype = purpose_member (TREE_OPERAND (name, 1),
  4257.                      CLASSTYPE_TAGS (TREE_TYPE (TREE_TYPE (TREE_OPERAND (name, 0)))));
  4258.       if (basetype)
  4259.         basetype = TREE_VALUE (basetype);
  4260.     }
  4261.  
  4262.       if (basetype != NULL_TREE)
  4263.     ;
  4264.       /* call to a constructor... */
  4265.       else if (TREE_TYPE (name))
  4266.     basetype = TREE_TYPE (TREE_TYPE (name));
  4267.       else
  4268.     {
  4269.       tree typedef_name = lookup_name (name);
  4270.       if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
  4271.         {
  4272.           /* Cannonicalize the typedef name.  */
  4273.           basetype = TREE_TYPE (typedef_name);
  4274.           name = DECL_NAME (TYPE_NAME (basetype));
  4275.         }
  4276.       else
  4277.         {
  4278.           error ("no constructor named `%s' in visible scope",
  4279.              IDENTIFIER_POINTER (name));
  4280.           return error_mark_node;
  4281.         }
  4282.     }
  4283.       if (wrap_type && wrap_type != basetype)
  4284.     {
  4285.       error_with_aggr_type (wrap_type, "invalid constructor `%s::%s'",
  4286.                 TYPE_NAME_STRING (basetype));
  4287.       return error_mark_node;
  4288.     }
  4289.       if (TYPE_VIRTUAL_P (basetype))
  4290.     {
  4291.       wrap_type = basetype;
  4292.     }
  4293.  
  4294.       if (! IS_AGGR_TYPE (basetype))
  4295.     {
  4296.     non_aggr_error:
  4297.       if ((flags & LOOKUP_COMPLAIN) && TREE_CODE (basetype) != ERROR_MARK)
  4298.         error ("request for member `%s' in something not a structure or union", err_name);
  4299.  
  4300.       return error_mark_node;
  4301.     }
  4302.     }
  4303.   else if (instance == C_C_D || instance == current_class_decl)
  4304.     {
  4305.       extern tree ctor_label, dtor_label;
  4306.  
  4307.       /* When doing initialization, we side-effect the TREE_TYPE of
  4308.      C_C_D, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE.  */
  4309.       basetype = TREE_TYPE (C_C_D);
  4310.  
  4311.       /* Anything manifestly `this' in constructors and destructors
  4312.      has a known type, so virtual function tables are not needed.  */
  4313.       if (TYPE_VIRTUAL_P (basetype)
  4314.       && !(flags & LOOKUP_NONVIRTUAL)
  4315.       && wrap_type == NULL_TREE)
  4316.     need_vtbl = (dtor_label || ctor_label)
  4317.       ? unneeded : maybe_needed;
  4318.  
  4319.       static_call_context = 0;
  4320.       instance = C_C_D;
  4321.       instance_ptr = current_class_decl;
  4322.       result = build_field_call (CLASSTYPE_AS_LIST (current_class_type),
  4323.                  instance_ptr, name, parms, err_name);
  4324.  
  4325.       if (result)
  4326.     return result;
  4327.     }
  4328.   else if (TREE_CODE (instance) == RESULT_DECL)
  4329.     {
  4330.       static_call_context = 0;
  4331.       basetype = TREE_TYPE (instance);
  4332.       if (wrap_type)
  4333.     {
  4334.       if (basetype_or_else (basetype, wrap_type))
  4335.         basetype = wrap_type;
  4336.       else
  4337.         return error_mark_node;
  4338.     }
  4339.       /* Should we ever have to make a virtual function reference
  4340.      from a RESULT_DECL, know that it must be of fixed type
  4341.      within the scope of this function.  */
  4342.       else if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
  4343.     need_vtbl = maybe_needed;
  4344.       instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (basetype), instance);
  4345.     }
  4346.   else if (instance == current_exception_object)
  4347.     {
  4348.       instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (current_exception_type),
  4349.                 TREE_OPERAND (current_exception_object, 0));
  4350.       mark_addressable (TREE_OPERAND (current_exception_object, 0));
  4351.       result = build_field_call (CLASSTYPE_AS_LIST (current_exception_type),
  4352.                  instance_ptr, name, parms, err_name);
  4353.       if (result)
  4354.     return result;
  4355.       error ("exception member `%s' cannot be invoked", err_name);
  4356.       return error_mark_node;
  4357.     }
  4358.   else
  4359.     {
  4360.       /* The MAIN_VARIANT of the type that `instance_ptr' winds up being.  */
  4361.       tree inst_ptr_basetype;
  4362.  
  4363.       /* from the file "cplus-typeck.c".  */
  4364.       extern tree unary_complex_lvalue ();
  4365.  
  4366.       static_call_context = (TREE_CODE (instance) == NOP_EXPR
  4367.                  && TREE_OPERAND (instance, 0) == error_mark_node);
  4368.  
  4369.       /* the base type of an instance variable is pointer to class */
  4370.       basetype = TREE_TYPE (instance);
  4371.  
  4372.       if (TREE_CODE (basetype) == REFERENCE_TYPE)
  4373.     {
  4374.       basetype = TYPE_MAIN_VARIANT (TREE_TYPE (basetype));
  4375.       if (! IS_AGGR_TYPE (basetype))
  4376.         goto non_aggr_error;
  4377.       /* Call to convert not needed because we are remaining
  4378.          within the same type.  */
  4379.       instance_ptr = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), instance);
  4380.       inst_ptr_basetype = basetype;
  4381.     }
  4382.       else
  4383.     {
  4384.       if (TREE_CODE (basetype) == POINTER_TYPE)
  4385.         {
  4386.           basetype = TREE_TYPE (basetype);
  4387.           instance_ptr = instance;
  4388.         }
  4389.  
  4390.       if (! IS_AGGR_TYPE (basetype))
  4391.         goto non_aggr_error;
  4392.  
  4393.       if (! instance_ptr)
  4394.         {
  4395.           if ((lvalue_p (instance)
  4396.            && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
  4397.           || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
  4398.         {
  4399.           if (instance_ptr == error_mark_node)
  4400.             return error_mark_node;
  4401.         }
  4402.           else if (TREE_CODE (instance) == NOP_EXPR
  4403.                || TREE_CODE (instance) == CONSTRUCTOR)
  4404.         {
  4405.           /* A cast is not an lvalue.  Initialize a fresh temp
  4406.              with the value we are casting from, and proceed with
  4407.              that temporary.  We can't cast to a reference type,
  4408.              so that simplifies the initialization to something
  4409.              we can manage.  */
  4410.           tree temp = get_temp_name (TREE_TYPE (instance), 0);
  4411.           if (IS_AGGR_TYPE (TREE_TYPE (instance)))
  4412.             expand_aggr_init (temp, instance, 0);
  4413.           else
  4414.             {
  4415.               store_init_value (temp, instance);
  4416.               expand_decl_init (temp);
  4417.             }
  4418.           instance = temp;
  4419.           instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
  4420.         }
  4421.           else
  4422.         {
  4423.           assert (TREE_CODE (instance) == CALL_EXPR);
  4424.           if (TYPE_NEEDS_CONSTRUCTOR (basetype))
  4425.             instance = build_cplus_new (basetype, instance);
  4426.           else
  4427.             {
  4428.               instance = get_temp_name (basetype, 0);
  4429.               TREE_ADDRESSABLE (instance) = 1;
  4430.             }
  4431.           instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
  4432.         }
  4433.           /* @@ Should we call comp_target_types here?  */
  4434.           inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
  4435.           if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
  4436.         basetype = inst_ptr_basetype;
  4437.           else
  4438.         instance_ptr = convert (TYPE_POINTER_TO (basetype), instance_ptr);
  4439.         }
  4440.       else
  4441.         inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
  4442.     }
  4443.  
  4444.       if (basetype_path == NULL_TREE)
  4445.     basetype_path = CLASSTYPE_AS_LIST (inst_ptr_basetype);
  4446.  
  4447.       result = build_field_call (basetype_path, instance_ptr, name, parms, err_name);
  4448.       if (result)
  4449.     return result;
  4450.  
  4451.       if (wrap_type)
  4452.     {
  4453.       if (basetype_or_else (basetype, wrap_type))
  4454.         basetype = wrap_type;
  4455.       else
  4456.         return error_mark_node;
  4457.     }
  4458.       else if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
  4459.     {
  4460.       if (TREE_VOLATILE (instance_ptr))
  4461.         {
  4462.           /* This action is needed because the instance is needed
  4463.          for providing the base of the virtual function table.
  4464.          Without using a SAVE_EXPR, the function we are building
  4465.          may be called twice, or side effects on the instance
  4466.          variable (such as a post-increment), may happen twice.  */
  4467.           instance_ptr = save_expr (instance_ptr);
  4468.           instance = build_indirect_ref (instance_ptr, 0);
  4469.         }
  4470.       else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
  4471.         {
  4472.           /* This happens when called for operator new ().  */
  4473.           instance = build_indirect_ref (instance, 0);
  4474.         }
  4475.  
  4476.       need_vtbl = maybe_needed;
  4477.     }
  4478.     }
  4479.  
  4480.   if (TYPE_SIZE (basetype) == 0)
  4481.     {
  4482.       /* This is worth complaining about, I think.  */
  4483.       error_with_aggr_type (basetype, "cannot lookup method in incomplete type `%s'");
  4484.       return error_mark_node;
  4485.     }
  4486.  
  4487.   /* Are we building a non-virtual wrapper?  */
  4488.   if (flags & LOOKUP_NONVIRTUAL)
  4489.     {
  4490.       if (all_virtual)
  4491.     sorry ("non-virtual call with -fall-virtual");
  4492.       if (wrap)
  4493.     wrap_type = basetype;
  4494.     }
  4495.  
  4496.   save_basetype = basetype;
  4497.  
  4498.   if (all_virtual == 1
  4499.       && (! strncmp (IDENTIFIER_POINTER (name), OPERATOR_METHOD_FORMAT,
  4500.              OPERATOR_METHOD_LENGTH)
  4501.       || instance_ptr == NULL_TREE
  4502.       || (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype) == 0
  4503.           && TYPE_NEEDS_WRAPPER (basetype) == 0)))
  4504.     all_virtual = 0;
  4505.  
  4506.   last = NULL_TREE;
  4507.   for (parmtypes = 0, parm = parms; parm; parm = TREE_CHAIN (parm))
  4508.     {
  4509.       tree t = TREE_TYPE (TREE_VALUE (parm));
  4510.       if (TREE_CODE (t) == OFFSET_TYPE)
  4511.     {
  4512.       /* Convert OFFSET_TYPE entities to their normal selves.  */
  4513.       TREE_VALUE (parm) = resolve_offset_ref (TREE_VALUE (parm));
  4514.       t = TREE_TYPE (TREE_VALUE (parm));
  4515.     }
  4516.       if (TREE_CODE (t) == ARRAY_TYPE)
  4517.     {
  4518.       /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
  4519.          This eliminates needless calls to `compute_conversion_costs'.  */
  4520.       TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
  4521.       t = TREE_TYPE (TREE_VALUE (parm));
  4522.     }
  4523.       if (t == error_mark_node)
  4524.     return error_mark_node;
  4525.       last = build_tree_list (NULL_TREE, t);
  4526.       parmtypes = chainon (parmtypes, last);
  4527.     }
  4528.  
  4529.   if (instance)
  4530.     {
  4531.       constp = TREE_READONLY (instance);
  4532.       volatilep = TREE_THIS_VOLATILE (instance);
  4533.       parms = tree_cons (NULL_TREE, instance_ptr, parms);
  4534.     }
  4535.   else
  4536.     {
  4537.       /* Raw constructors are always in charge.  */
  4538.       if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
  4539.       && ! (flags & LOOKUP_HAS_IN_CHARGE))
  4540.     {
  4541.       flags |= LOOKUP_HAS_IN_CHARGE;
  4542.       parms = tree_cons (NULL_TREE, integer_one_node, parms);
  4543.       parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
  4544.     }
  4545.  
  4546.       if (flag_this_is_variable)
  4547.     {
  4548.       constp = 0;
  4549.       volatilep = 0;
  4550.       parms = tree_cons (NULL_TREE, build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node), parms);
  4551.     }
  4552.       else
  4553.     {
  4554.       constp = 0;
  4555.       volatilep = 0;
  4556.       instance_ptr = build_new (NULL_TREE, basetype, void_type_node, 0);
  4557.       if (instance_ptr == error_mark_node)
  4558.         return error_mark_node;
  4559.       instance_ptr = save_expr (instance_ptr);
  4560.       TREE_CALLS_NEW (instance_ptr) = 1;
  4561.       instance = build_indirect_ref (instance_ptr, 0);
  4562.       parms = tree_cons (NULL_TREE, instance_ptr, parms);
  4563.     }
  4564.     }
  4565.   parmtypes = tree_cons (NULL_TREE,
  4566.              build_pointer_type (build_type_variant (basetype, constp, volatilep)),
  4567.              parmtypes);
  4568.   if (last == NULL_TREE)
  4569.     last = parmtypes;
  4570.  
  4571.   /* Look up function name in the structure type definition.  */
  4572.  
  4573.   if (wrap)
  4574.     {
  4575.       if (wrap > 0)
  4576.     name_kind = "wrapper";
  4577.       else
  4578.     name_kind = "anti-wrapper";
  4579.       baselink = get_wrapper (basetype);
  4580.     }
  4581.   else
  4582.     {
  4583.       if (TREE_TYPE (name)
  4584.       && TREE_CODE (TREE_TYPE (name)) == TYPE_DECL
  4585.       && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (name))))
  4586.     {
  4587.       tree tmp = NULL_TREE;
  4588.       if (TREE_TYPE (name) == TYPE_NAME (basetype))
  4589.         tmp = basetype;
  4590.       else
  4591.         tmp = get_base_type (TREE_TYPE (TREE_TYPE (name)), basetype, 0);
  4592.       if (tmp != 0)
  4593.         {
  4594.           name_kind = "constructor";
  4595.  
  4596.           if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
  4597.             && ! (flags & LOOKUP_HAS_IN_CHARGE))
  4598.         {
  4599.           /* Constructors called for initialization
  4600.              only are never in charge.  */
  4601.           tree tmplist;
  4602.  
  4603.           flags |= LOOKUP_HAS_IN_CHARGE;
  4604.           tmplist = tree_cons (NULL_TREE, integer_zero_node,
  4605.                        TREE_CHAIN (parms));
  4606.           TREE_CHAIN (parms) = tmplist;
  4607.           tmplist = tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
  4608.           TREE_CHAIN (parmtypes) = tmplist;
  4609.         }
  4610.  
  4611. #ifdef SOS
  4612.           if (TYPE_DYNAMIC (basetype) && dtbl_inserted == 0)
  4613.         {
  4614.           tree parm, parmtype;
  4615.           dtbl = get_sos_dtable (basetype);
  4616.           parm = tree_cons (NULL_TREE, dtbl, TREE_CHAIN (parms));
  4617.           parmtype = tree_cons (NULL_TREE, build_pointer_type (ptr_type_node), TREE_CHAIN (parmtypes));
  4618.           TREE_CHAIN (parms) = parm;
  4619.           TREE_CHAIN (parmtypes) = parmtype;
  4620.           dtbl_inserted = -1;
  4621.         }
  4622. #endif
  4623.           /* constructors are in very specific places.  */
  4624. #ifdef SOS
  4625.           if (dtbl_inserted == -1)
  4626.         {
  4627.           TREE_CHAIN (parmtypes) = TREE_CHAIN (TREE_CHAIN (parmtypes));
  4628.           TREE_CHAIN (parms) = TREE_CHAIN (TREE_CHAIN (parms));
  4629.           dtbl_inserted = 0;
  4630.         }
  4631. #endif
  4632.           basetype = tmp;
  4633.         }
  4634.       else
  4635.         name_kind = "method";
  4636.     }
  4637.       else name_kind = "method";
  4638.  
  4639.       if (basetype_path == NULL_TREE)
  4640.     basetype_path = CLASSTYPE_AS_LIST (basetype);
  4641.       result = lookup_fnfields (basetype_path, name,
  4642.                 (flags & LOOKUP_COMPLAIN));
  4643.       if (result == error_mark_node)
  4644.     return error_mark_node;
  4645.     }
  4646.  
  4647.   /* Now, go look for this method name. We do not find destructors here.
  4648.  
  4649.      Putting `void_list_node' on the end of the parmtypes
  4650.      fakes out `build_decl_overload' into doing the right thing.  */
  4651.   TREE_CHAIN (last) = void_list_node;
  4652.   method_name = build_decl_overload (IDENTIFIER_POINTER (name),
  4653.                      parmtypes,
  4654.                      1 + (name == DECL_NAME (TYPE_NAME (save_basetype))));
  4655.   TREE_CHAIN (last) = NULL_TREE;
  4656.  
  4657.   for (pass = 0; pass < 2; pass++)
  4658.     {
  4659.       struct candidate *candidates;
  4660.       struct candidate *cp;
  4661.       int len, best = 2;
  4662.  
  4663.       /* This increments every time we go up the type hierarchy.
  4664.      The idea is to prefer a function of the derived class if possible.  */
  4665.       int b_or_d;
  4666.  
  4667.       baselink = result;
  4668.  
  4669.       if (pass > 0)
  4670.     {
  4671.       candidates = (struct candidate *) alloca ((ever_seen+1) * sizeof (struct candidate));
  4672.       cp = candidates;
  4673.       len = list_length (parms);
  4674.       b_or_d = 0;
  4675.  
  4676.       /* First see if a global function has a shot at it.  */
  4677.       if (flags & LOOKUP_GLOBAL)
  4678.         {
  4679.           tree friend_parms;
  4680.           tree parm = TREE_VALUE (parms);
  4681.  
  4682.           if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
  4683.         friend_parms = parms;
  4684.           else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
  4685.         {
  4686.           parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
  4687.           parm = convert (build_reference_type (TREE_TYPE (parm)), parm);
  4688.           friend_parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
  4689.         }
  4690.           else
  4691.         assert (0);
  4692.  
  4693.           cp->harshness
  4694.         = (unsigned short *)alloca ((len+1) * sizeof (short));
  4695.           result = build_overload_call (name, friend_parms, 0, cp);
  4696.           /* If it turns out to be the one we were actually looking for
  4697.          (it was probably a friend function), the return the
  4698.          good result.  */
  4699.           if (TREE_CODE (result) == CALL_EXPR)
  4700.         return result;
  4701.  
  4702.           while (cp->evil == 0)
  4703.         {
  4704.           /* non-standard uses: set the field to 0 to indicate
  4705.              we are using a non-member function.  */
  4706.           cp->u.field = 0;
  4707.           if (cp->harshness[len] == 0
  4708.               && cp->harshness[len] == 0
  4709.               && cp->user == 0 && cp->b_or_d == 0
  4710.               && cp->easy < best)
  4711.             best = cp->easy;
  4712.           cp += 1;
  4713.         }
  4714.         }
  4715.     }
  4716.  
  4717.       while (baselink)
  4718.     {
  4719.       /* We have a hit (of sorts). If the parameter list is
  4720.          "error_mark_node", or some variant thereof, it won't
  4721.          match any methods. Since we have verified that the is
  4722.          some method vaguely matching this one (in name at least),
  4723.          silently return.
  4724.          
  4725.          Don't stop for friends, however.  */
  4726.       tree basetypes = TREE_PURPOSE (baselink);
  4727.  
  4728.       function = TREE_VALUE (baselink);
  4729.       basetype = TREE_VALUE (basetypes);
  4730.  
  4731.       /* Cast the instance variable to the approriate type.  */
  4732.       TREE_VALUE (parmtypes) = TYPE_POINTER_TO (basetype);
  4733.  
  4734.       if (DESTRUCTOR_NAME_P (DECL_NAME (function)))
  4735.         function = TREE_CHAIN (function);
  4736.  
  4737.       for (; function; function = TREE_CHAIN (function))
  4738.         {
  4739. #ifdef GATHER_STATISTICS
  4740.           n_inner_fields_searched++;
  4741. #endif
  4742.           ever_seen++;
  4743.  
  4744.           /* Not looking for friends here.  */
  4745.           if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
  4746.           && ! DECL_STATIC_FUNCTION_P (function))
  4747.         continue;
  4748.  
  4749.           if (pass == 0
  4750.           && DECL_NAME (function) == method_name)
  4751.         {
  4752.           if (flags & LOOKUP_PROTECT)
  4753.             {
  4754.               visibility = compute_visibility (basetypes, function);
  4755.               if (visibility == visibility_protected
  4756.               && flags & LOOKUP_PROTECTED_OK)
  4757.             visibility = visibility_public;
  4758.             }
  4759.  
  4760.           if ((flags & LOOKUP_PROTECT) == 0
  4761.               || visibility == visibility_public)
  4762.             goto found_and_ok;
  4763.           else if (visibility == visibility_private)
  4764.             saw_private = function;
  4765.           else if (visibility == visibility_protected)
  4766.             saw_protected = function;
  4767.           /* If we fail on the exact match, we have
  4768.              an immediate failure.  */
  4769.           goto found;
  4770.         }
  4771.           if (pass > 0)
  4772.         {
  4773.           tree these_parms = parms;
  4774.  
  4775. #ifdef GATHER_STATISTICS
  4776.           n_inner_fields_searched++;
  4777. #endif
  4778.           cp->harshness
  4779.             = (unsigned short *)alloca ((len+1) * sizeof (short));
  4780.           if (DECL_STATIC_FUNCTION_P (function))
  4781.             these_parms = TREE_CHAIN (these_parms);
  4782.           compute_conversion_costs (function, these_parms, cp, len);
  4783.           cp->b_or_d += b_or_d;
  4784.           if (cp->evil == 0)
  4785.             {
  4786.               cp->u.field = function;
  4787.               cp->function = function;
  4788.               if (flags & LOOKUP_PROTECT)
  4789.             {
  4790.               enum visibility_type this_v;
  4791.               this_v = compute_visibility (basetypes, function);
  4792.               if (this_v == visibility_protected
  4793.                   && (flags & LOOKUP_PROTECTED_OK))
  4794.                 this_v = visibility_public;
  4795.               if (this_v != visibility_public)
  4796.                 {
  4797.                   if (this_v == visibility_private)
  4798.                 saw_private = function;
  4799.                   else
  4800.                 saw_protected = function;
  4801.                   continue;
  4802.                 }
  4803.             }
  4804.  
  4805.               /* No "two-level" conversions.  */
  4806.               if (flags & LOOKUP_NO_CONVERSION && cp->user != 0)
  4807.             continue;
  4808.  
  4809.               /* If we used default parameters, we must
  4810.              check to see whether anyone else might
  4811.              use them also, and report a possible
  4812.              ambiguity.  */
  4813.               if (! TYPE_USES_MULTIPLE_INHERITANCE (save_basetype)
  4814.               && cp->harshness[len] == 0
  4815.               && (cp->harshness[0] & 128) == 0
  4816.               && cp->user == 0 && cp->b_or_d == 0
  4817.               && cp->easy < best)
  4818.             {
  4819.               if (! DECL_STATIC_FUNCTION_P (function))
  4820.                 TREE_VALUE (parms) = cp->arg;
  4821.               if (best == 2)
  4822.                 goto found_and_maybe_warn;
  4823.             }
  4824.               cp++;
  4825.             }
  4826.         }
  4827.         }
  4828.       /* Now we have run through one link's member functions.
  4829.          arrange to head-insert this link's links.  */
  4830.       baselink = next_baselink (baselink);
  4831.       b_or_d += 1;
  4832.     }
  4833.       if (pass == 0)
  4834.     {
  4835.       /* No exact match could be found.  Now try to find match
  4836.          using default conversions.  */
  4837.       if ((flags & LOOKUP_GLOBAL) && IDENTIFIER_GLOBAL_VALUE (name))
  4838.         if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (name)) == FUNCTION_DECL)
  4839.           ever_seen += 1;
  4840.         else if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (name)) == TREE_LIST)
  4841.           ever_seen += list_length (IDENTIFIER_GLOBAL_VALUE (name));
  4842.  
  4843.       if (ever_seen == 0)
  4844.         {
  4845.           if (flags & LOOKUP_GLOBAL)
  4846.         error ("no global or member function `%s' defined", err_name);
  4847.           else
  4848.         error_with_aggr_type (save_basetype, "no member function `%s::%s'", err_name);
  4849.           return error_mark_node;
  4850.         }
  4851.       continue;
  4852.     }
  4853.  
  4854.       if (cp - candidates != 0)
  4855.     {
  4856.       /* Rank from worst to best.  Then cp will point to best one.
  4857.          Private fields have their bits flipped.  For unsigned
  4858.          numbers, this should make them look very large.
  4859.          If the best alternate has a (signed) negative value,
  4860.          then all we ever saw were private members.  */
  4861.       if (cp - candidates > 1)
  4862.         {
  4863.           cp = ideal_candidate (save_basetype, candidates,
  4864.                     cp - candidates, parms, len);
  4865.           if (cp == 0)
  4866.         {
  4867.           error ("ambiguous type conversion requested for %s `%s'",
  4868.              name_kind, err_name);
  4869.           return error_mark_node;
  4870.         }
  4871.         }
  4872.       else if (cp[-1].evil == 2)
  4873.         {
  4874.           error ("ambiguous type conversion requested for %s `%s'",
  4875.              name_kind, err_name);
  4876.           return error_mark_node;
  4877.         }
  4878.       else cp--;
  4879.  
  4880.       /* The global function was the best, so use it.  */
  4881.       if (cp->u.field == 0)
  4882.         {
  4883.           /* We must convert the instance pointer into a reference type.
  4884.          Global overloaded functions can only either take
  4885.          aggregate objects (which come for free from references)
  4886.          or reference data types anyway.  */
  4887.           TREE_VALUE (parms) = copy_node (instance_ptr);
  4888.           TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
  4889.           return build_function_call (cp->function, parms);
  4890.         }
  4891.  
  4892.       function = cp->function;
  4893.       if (DECL_STATIC_FUNCTION_P (function))
  4894.         basetype = NULL_TREE;
  4895.       else
  4896.         {
  4897.           basetype = TREE_TYPE (TREE_TYPE (cp->arg));
  4898.           TREE_VALUE (parms) = cp->arg;
  4899.         }
  4900.       goto found_and_maybe_warn;
  4901.     }
  4902.  
  4903.       if ((flags & ~LOOKUP_GLOBAL) & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
  4904.     {
  4905.       char *tag_name, *buf;
  4906.  
  4907.       if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
  4908.           == LOOKUP_SPECULATIVELY)
  4909.         return NULL_TREE;
  4910.  
  4911.       if (DECL_STATIC_FUNCTION_P (cp->function))
  4912.         parms = TREE_CHAIN (parms);
  4913.       if (ever_seen)
  4914.         {
  4915.           if (((int)saw_protected|(int)saw_private) == 0)
  4916.         {
  4917.           if (flags & LOOKUP_SPECULATIVELY)
  4918.             return NULL_TREE;
  4919.           if (static_call_context && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
  4920.             error_with_aggr_type (TREE_TYPE (TREE_TYPE (instance_ptr)),
  4921.                       "object missing in call to `%s::%s'",
  4922.                       err_name);
  4923.           else
  4924.             report_type_mismatch (cp, parms, name_kind, err_name);
  4925.         }
  4926.           else
  4927.         {
  4928.           char buf[80];
  4929.           char *msg;
  4930.           tree seen = saw_private;
  4931.  
  4932.           if (saw_private)
  4933.             if (saw_protected)
  4934.               msg = "%s %%s (and the like) are private or protected";
  4935.             else
  4936.               msg = "the %s %%s is private";
  4937.           else
  4938.             {
  4939.               msg = "the %s %%s is protected";
  4940.               seen = saw_protected;
  4941.             }
  4942.           sprintf (buf, msg, name_kind);
  4943.           error_with_decl (seen, buf);
  4944.           error ("within this context");
  4945.         }
  4946.           return error_mark_node;
  4947.         }
  4948.  
  4949.       if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
  4950.           == LOOKUP_COMPLAIN)
  4951.         {
  4952.           if (TREE_CODE (save_basetype) == RECORD_TYPE)
  4953.         tag_name = "structure";
  4954.           else
  4955.         tag_name = "union";
  4956.  
  4957.           if (wrap)
  4958.         buf = "%s has no appropriate wrapper function defined";
  4959.           else
  4960.         {
  4961.           buf = (char *)alloca (30 + strlen (err_name));
  4962.           strcpy (buf, "%s has no method named `%s'");
  4963.         }
  4964.  
  4965.           error (buf, tag_name, err_name);
  4966.           return error_mark_node;
  4967.         }
  4968.       return NULL_TREE;
  4969.     }
  4970.       continue;
  4971.  
  4972.     found_and_maybe_warn:
  4973.       if (cp->harshness[0] & 128)
  4974.     {
  4975.       if (flags & LOOKUP_COMPLAIN)
  4976.         {
  4977.           error_with_decl (cp->function, "non-const member function `%s'");
  4978.           error ("called for const object at this point in file");
  4979.         }
  4980.       /* Not good enough for a match.  */
  4981.       else return error_mark_node;
  4982.     }
  4983.       goto found_and_ok;
  4984.     }
  4985.   /* Silently return error_mark_node.  */
  4986.   return error_mark_node;
  4987.  
  4988.  found:
  4989.   if (visibility == visibility_private)
  4990.     {
  4991.       if (flags & LOOKUP_COMPLAIN)
  4992.     error (TREE_PRIVATE (function)
  4993.            ? "%s `%s' is private"
  4994.            : "%s `%s' is from private base class",
  4995.            name_kind,
  4996.            lang_printable_name (function));
  4997.       return error_mark_node;
  4998.     }
  4999.   else if (visibility == visibility_protected)
  5000.     {
  5001.       if (flags & LOOKUP_COMPLAIN)
  5002.     error (TREE_PROTECTED (function)
  5003.            ? "%s `%s' is protected"
  5004.            : "%s `%s' has protected visibility from this point",
  5005.            name_kind,
  5006.            lang_printable_name (function));
  5007.       return error_mark_node;
  5008.     }
  5009.   abort ();
  5010.  
  5011.  found_and_ok:
  5012.  
  5013.   /* From here on down, BASETYPE is the type that INSTANCE_PTR's
  5014.      type (if it exists) is a pointer to.  */
  5015.   basetype = DECL_CONTEXT (function);
  5016.   fntype = TREE_TYPE (function);
  5017.  
  5018.   if (TREE_CODE (fntype) == POINTER_TYPE)
  5019.     fntype = TREE_TYPE (fntype);
  5020.  
  5021.   /* If we are referencing a virtual function from an object
  5022.      of effectively static type, then there is no need
  5023.      to go through the virtual function table.  */
  5024.   if (need_vtbl == maybe_needed)
  5025.     {
  5026.       int fixed_type = resolves_to_fixed_type_p (instance);
  5027.  
  5028.       if (all_virtual == 1
  5029.       && DECL_VINDEX (function)
  5030.       && may_be_remote (basetype))
  5031.     need_vtbl = needed;
  5032.       else if (DECL_VIRTUAL_P (function))
  5033.     need_vtbl = fixed_type ? unneeded : needed;
  5034.       else
  5035.     need_vtbl = not_needed;
  5036.  
  5037.       if (fixed_type && DECL_ABSTRACT_VIRTUAL_P (function))
  5038.     {
  5039.       error_with_decl (function, "invalid call to abstract function `%s'");
  5040.       return error_mark_node;
  5041.     }
  5042.     }
  5043.  
  5044.   if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context)
  5045.     {
  5046.       /* Let's be nice to the user for now, and give reasonable
  5047.      default behavior.  */
  5048.       instance_ptr = current_class_decl;
  5049.       if (instance_ptr)
  5050.     {
  5051.       if (basetype != current_class_type)
  5052.         {
  5053.           basetype = get_base_type (basetype, current_class_type, 1);
  5054.           if (basetype == 0)
  5055.         {
  5056.           error_not_base_type (DECL_CONTEXT (function), current_class_type);
  5057.           return error_mark_node;
  5058.         }
  5059.           else if (basetype == error_mark_node)
  5060.         return error_mark_node;
  5061.         }
  5062.     }
  5063.       else
  5064.     {
  5065.       error_with_aggr_type (basetype, "cannot call member function `%s::%s' without object",
  5066.                 err_name);
  5067.       return error_mark_node;
  5068.     }
  5069.     }
  5070.  
  5071.   value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
  5072.  
  5073.   if (TYPE_SIZE (value_type) == 0)
  5074.     {
  5075.       if (flags & LOOKUP_COMPLAIN)
  5076.     incomplete_type_error (0, value_type);
  5077.       return error_mark_node;
  5078.     }
  5079.  
  5080.   /* We do not pass FUNCTION into `actualparameterlist', because by
  5081.      now everything should be ok.  If not, then we have a serious error.  */
  5082.   if (DECL_STATIC_FUNCTION_P (function))
  5083.     parms = actualparameterlist (NULL_TREE, TYPE_ARG_TYPES (fntype),
  5084.                  TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL);
  5085.   else if (need_vtbl == unneeded)
  5086.     {
  5087.       int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
  5088.       basetype = TREE_TYPE (instance);
  5089.       if (DECL_CONTEXT (function) != TYPE_MAIN_VARIANT (basetype)
  5090.       && (TYPE_USES_MULTIPLE_INHERITANCE (basetype)
  5091.           || TYPE_USES_VIRTUAL_BASECLASSES (basetype)))
  5092.     {
  5093.       basetype = DECL_CONTEXT (function);
  5094.       instance_ptr = convert_pointer_to (basetype, instance_ptr);
  5095.       instance = build_indirect_ref (instance_ptr, 0);
  5096.     }
  5097.       parms = tree_cons (NULL_TREE, instance_ptr,
  5098.              actualparameterlist (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, sub_flags));
  5099.     }
  5100.   else
  5101.     {
  5102.       if ((flags & LOOKUP_NONVIRTUAL) == 0)
  5103.     basetype = DECL_VCONTEXT (function);
  5104.  
  5105.       /* First parm could be integer_zerop with casts like
  5106.      ((Object*)0)->Object::IsA()  */
  5107.       if (!integer_zerop (TREE_VALUE (parms)))
  5108.     {
  5109.       instance_ptr = convert_pointer_to (build_type_variant (basetype, constp, volatilep),
  5110.                          TREE_VALUE (parms));
  5111.       if (TREE_CODE (instance_ptr) == COND_EXPR)
  5112.         {
  5113.           instance_ptr = save_expr (instance_ptr);
  5114.           instance = build_indirect_ref (instance_ptr);
  5115.         }
  5116.       else if (TREE_CODE (instance_ptr) == NOP_EXPR
  5117.            && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
  5118.            && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
  5119.         ;
  5120.       else if (instance == NULL_TREE
  5121.            || TREE_CODE (instance) != INDIRECT_REF
  5122.            || TREE_OPERAND (instance, 0) != instance_ptr)
  5123.         instance = build_indirect_ref (instance_ptr);
  5124.     }
  5125.       parms = tree_cons (NULL_TREE, instance_ptr,
  5126.              actualparameterlist (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL));
  5127.     }
  5128.  
  5129.   /* See if there is a wrapper for this thing.  */
  5130.   if (wrap < 0
  5131.       || static_call_context
  5132.       || name == wrapper_name
  5133.       || name == DECL_NAME (TYPE_NAME (basetype)))
  5134.     ;
  5135.   else if (wrap > 0 || TYPE_NEEDS_WRAPPER (basetype))
  5136.     {
  5137.       flags &= ~LOOKUP_PROTECT;
  5138.       if (wrap == 0)
  5139.     {
  5140.       wrap = TYPE_NEEDS_WRAPPER (basetype);
  5141.       /* If no wrapper specified, wrapper may be virtual.  */
  5142.       flags &= ~LOOKUP_NONVIRTUAL;
  5143.     }
  5144.  
  5145.       if (wrap)
  5146.     {
  5147.       tree wrapped_result, unwrapped_result;
  5148.       register int bytecount = get_arglist_len_in_bytes (parms);
  5149.  
  5150.       if (!all_virtual && TREE_CODE (function) == FUNCTION_DECL)
  5151.         parm = build_unary_op (ADDR_EXPR, function, 0);
  5152.       else
  5153.         {
  5154.               fntype = build_cplus_method_type (basetype, TREE_TYPE (fntype), TYPE_ARG_TYPES (fntype));
  5155.           parm = build1 (NOP_EXPR, build_pointer_type (fntype), DECL_VINDEX (function));
  5156.         }
  5157.  
  5158.       if (TYPE_HAS_WRAPPER_PRED (basetype))
  5159.         {
  5160.           unwrapped_result = build_nt (CALL_EXPR, default_conversion (function), parms, NULL_TREE);
  5161.  
  5162.           assert (TREE_OPERAND (unwrapped_result, 1) != error_mark_node);
  5163.  
  5164.           TREE_TYPE (unwrapped_result) = value_type;
  5165.           TREE_VOLATILE (unwrapped_result) = 1;
  5166.           TREE_RAISES (unwrapped_result) = !! TYPE_RAISES_EXCEPTIONS (fntype);
  5167.         }
  5168.  
  5169.       /* If this pointer walked as a result of multiple inheritance,
  5170.          keep its displaced value.  */
  5171.       parms = tree_cons (NULL_TREE, build_int_2 (bytecount, 0),
  5172.                  tree_cons (NULL_TREE, parm, TREE_CHAIN (parms)));
  5173.  
  5174.       wrapped_result = get_wrapper (basetype);
  5175.       assert (wrapped_result != NULL_TREE);
  5176.       assert (wrapped_result != error_mark_node);
  5177.  
  5178.       /* @@ Should BASETYPE_PATH get TREE_PURPOSE (wrapped_result) here?  */
  5179.       wrapped_result
  5180.         = build_method_call (instance,
  5181.                  DECL_ORIGINAL_NAME (TREE_VALUE (wrapped_result)),
  5182.                  parms, basetype_path, flags);
  5183. #if 0
  5184.       /* Do this if we want the result of operator->() to inherit
  5185.          the type of the function it is subbing for.  */
  5186.       if (wrapped_result != error_mark_node)
  5187.         TREE_TYPE (wrapped_result) = value_type;
  5188. #endif
  5189.  
  5190.       if (TYPE_HAS_WRAPPER_PRED (basetype))
  5191.         {
  5192.           result = build_conditional_expr
  5193.         (build_method_call (instance, wrapper_pred_name, build_tree_list (NULL_TREE, parm), basetype_path, LOOKUP_NORMAL),
  5194.          wrapped_result,
  5195.          unwrapped_result);
  5196.  
  5197.         }
  5198.       else
  5199.         {
  5200.           result = wrapped_result;
  5201.         }
  5202.  
  5203.       TREE_VOLATILE (result) = 1;
  5204.       return result;
  5205.     }
  5206.     }
  5207.   /* Constructors do not overload method calls.  */
  5208.   else if (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype)
  5209.        && name != DECL_NAME (TYPE_NAME (basetype))
  5210.        && (TREE_CODE (function) != FUNCTION_DECL
  5211.            || strncmp (IDENTIFIER_POINTER (DECL_NAME (function)),
  5212.                OPERATOR_METHOD_FORMAT,
  5213.                OPERATOR_METHOD_LENGTH))
  5214. #if 0
  5215.        && (may_be_remote (basetype)
  5216.            || (C_C_D ? TREE_TYPE (instance) != current_class_type : 1))
  5217. #else
  5218.        /* This change by Larry Ketcham.  */
  5219.          && (may_be_remote (basetype) || instance != C_C_D)
  5220. #endif
  5221.        )
  5222.     {
  5223. #ifdef ESKIT
  5224.       register int bytecount = 0;
  5225. #else
  5226.       register int bytecount = get_arglist_len_in_bytes (parms);
  5227. #endif
  5228.       tree fn_as_int;
  5229.  
  5230.       parms = tree_cons (NULL_TREE, build_int_2 (bytecount, 0),
  5231.              TREE_CHAIN (parms));
  5232.  
  5233.       if (!all_virtual && TREE_CODE (function) == FUNCTION_DECL)
  5234.     fn_as_int = build_unary_op (ADDR_EXPR, function, 0);
  5235.       else
  5236.     fn_as_int = convert (TREE_TYPE (default_conversion (function)), DECL_VINDEX (function));
  5237.       if (all_virtual == 1)
  5238.     fn_as_int = convert (integer_type_node, fn_as_int);
  5239.  
  5240.       result = build_opfncall (METHOD_CALL_EXPR, LOOKUP_NORMAL, instance, fn_as_int, parms);
  5241.  
  5242.       if (result == NULL_TREE)
  5243.     {
  5244.       compiler_error ("could not overload `operator->()(...)'");
  5245.       return error_mark_node;
  5246.     }
  5247.       else if (result == error_mark_node)
  5248.     return error_mark_node;
  5249.  
  5250. #if 0
  5251.       /* Do this if we want the result of operator->() to inherit
  5252.      the type of the function it is subbing for.  */
  5253.       TREE_TYPE (result) = value_type;
  5254. #endif
  5255.  
  5256. #ifdef ESKIT
  5257.       {
  5258.     int used, size;
  5259.  
  5260.     /* Count the number of bytes of arguements to operator->(),
  5261.        not to the method itself.  In the tally, don't count bytes
  5262.        for pointer to member function or for the bytecount.  */
  5263.     parms = TREE_OPERAND (result, 1);
  5264.     bytecount = get_arglist_len_in_bytes (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (parms))));
  5265.     used = size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_VALUE (parms))));
  5266. #ifdef PUSH_ROUNDING
  5267.     size = PUSH_ROUNDING (size);
  5268. #endif
  5269.     used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
  5270.          / (PARM_BOUNDARY / BITS_PER_UNIT))
  5271.         * (PARM_BOUNDARY / BITS_PER_UNIT));
  5272.     bytecount += used;
  5273.     TREE_CHAIN (TREE_CHAIN (parms))
  5274.       = tree_cons (NULL_TREE, build_int_2 (bytecount, 0),
  5275.                TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (parms))));
  5276.       }
  5277. #endif
  5278.  
  5279.       return result;
  5280.     }
  5281.  
  5282.   if (need_vtbl == needed)
  5283.     {
  5284.       function = build_vfn_ref (&TREE_VALUE (parms), instance, DECL_VINDEX (function));
  5285.       TREE_TYPE (function) = build_pointer_type (fntype);
  5286.     }
  5287. #ifdef SOS
  5288.   else if (basetype && TYPE_DYNAMIC (basetype))
  5289.     {
  5290.       function = build_array_ref (dtbl, DECL_DINDEX (function));
  5291.       TREE_TYPE (function) = build_pointer_type (fntype);
  5292.     }
  5293. #endif
  5294.  
  5295.   if (TREE_INLINE (function) && TREE_CODE (function) == FUNCTION_DECL)
  5296.     function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
  5297.   else function = default_conversion (function);
  5298.  
  5299.   result =
  5300.     build_nt (CALL_EXPR, function, parms, NULL_TREE);
  5301.  
  5302.   TREE_TYPE (result) = value_type;
  5303.   TREE_VOLATILE (result) = 1;
  5304.   TREE_RAISES (result)
  5305.     = TYPE_RAISES_EXCEPTIONS (fntype) || (parms && TREE_RAISES (parms));
  5306.   return result;
  5307. }
  5308.  
  5309. /* Similar to `build_method_call', but for overloaded non-member functions.
  5310.    The name of this function comes through NAME.  The name depends
  5311.    on PARMS.
  5312.  
  5313.    Note that this function must handle simple `C' promotions,
  5314.    as well as variable numbers of arguments (...), and
  5315.    default arguments to boot.
  5316.  
  5317.    If the overloading is successful, we return a treenode which
  5318.    contains the call to the function.
  5319.  
  5320.    If overloading produces candidates which are probabe, but not definite,
  5321.    we hold these candidates.  If FINAL_CP is non-zero, then we are free
  5322.    to assume that final_cp points to enough storage for all candidates that
  5323.    this function might generate.  The `harshness' array is preallocated for
  5324.    the first candidate, but not for subsequent ones.
  5325.  
  5326.    Note that the DECL_RTL of FUNCTION must be made to agree with this
  5327.    function's new name.  */
  5328.  
  5329. tree
  5330. build_overload_call (fnname, parms, complain, final_cp)
  5331.      tree fnname, parms;
  5332.      int complain;
  5333.      struct candidate *final_cp;
  5334. {
  5335.   /* must check for overloading here */
  5336.   tree overload_name, functions, function, parm;
  5337.   tree parmtypes = NULL_TREE, last = NULL_TREE;
  5338.   register tree outer;
  5339.   int length;
  5340.   int parmlength = list_length (parms);
  5341.  
  5342.   struct candidate *candidates, *cp;
  5343.   int rank_for_overload ();
  5344.  
  5345.   if (final_cp)
  5346.     {
  5347.       final_cp[0].evil = 0;
  5348.       final_cp[0].user = 0;
  5349.       final_cp[0].b_or_d = 0;
  5350.       final_cp[0].easy = 0;
  5351.       final_cp[0].function = 0;
  5352.       /* end marker.  */
  5353.       final_cp[1].evil = 1;
  5354.     }
  5355.  
  5356.   for (parm = parms; parm; parm = TREE_CHAIN (parm))
  5357.     {
  5358.       register tree t = TREE_TYPE (TREE_VALUE (parm));
  5359.  
  5360.       if (t == error_mark_node)
  5361.     return error_mark_node;
  5362.       if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == OFFSET_TYPE)
  5363.     {
  5364.       /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
  5365.          Also convert OFFSET_TYPE entities to their normal selves.
  5366.          This eliminates needless calls to `compute_conversion_costs'.  */
  5367.       TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
  5368.       t = TREE_TYPE (TREE_VALUE (parm));
  5369.     }
  5370.       last = build_tree_list (NULL_TREE, t);
  5371.       parmtypes = chainon (parmtypes, last);
  5372.     }
  5373.   if (last)
  5374.     TREE_CHAIN (last) = void_list_node;
  5375.   else
  5376.     parmtypes = void_list_node;
  5377.   overload_name = build_decl_overload (IDENTIFIER_POINTER (fnname), parmtypes, 0);
  5378.  
  5379.   /* Now check to see whether or not we can win.
  5380.      Note that if we are called from `build_method_call',
  5381.      then we cannot have a mis-match, because we would have
  5382.      already found such a winning case.  */
  5383.  
  5384.   if (IDENTIFIER_GLOBAL_VALUE (overload_name))
  5385.     if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (overload_name)) != TREE_LIST)
  5386.       return build_function_call (DECL_MAIN_VARIANT (IDENTIFIER_GLOBAL_VALUE (overload_name)), parms);
  5387.  
  5388.   functions = IDENTIFIER_GLOBAL_VALUE (fnname);
  5389.  
  5390.   if (functions == NULL_TREE)
  5391.     {
  5392.       if (complain)
  5393.     error ("only member functions apply");
  5394.       if (final_cp)
  5395.     final_cp->evil = 1;
  5396.       return error_mark_node;
  5397.     }
  5398.  
  5399.   if (TREE_CODE (functions) == FUNCTION_DECL)
  5400.     {
  5401.       functions = DECL_MAIN_VARIANT (functions);
  5402.       if (final_cp)
  5403.     {
  5404.       /* We are just curious whether this is a viable alternative or not.  */
  5405.       compute_conversion_costs (functions, parms, final_cp, parmlength);
  5406.       return functions;
  5407.     }
  5408.       else
  5409.     return build_function_call (functions, parms);
  5410.     }
  5411.  
  5412.   if (TREE_VALUE (functions) == NULL_TREE)
  5413.     {
  5414.       if (complain)
  5415.     error ("function `%s' declared overloaded, but no instances of that function declared",
  5416.            IDENTIFIER_POINTER (TREE_PURPOSE (functions)));
  5417.       return error_mark_node;
  5418.     }
  5419.  
  5420.   if (TREE_CODE (TREE_VALUE (functions)) == TREE_LIST)
  5421.     {
  5422.       register tree outer;
  5423.       length = 0;
  5424.  
  5425.       /* The list-of-lists should only occur for class things.  */
  5426.       assert (functions == IDENTIFIER_CLASS_VALUE (fnname));
  5427.  
  5428.       for (outer = functions; outer; outer = TREE_CHAIN (outer))
  5429.     {
  5430.       /* member functions.  */
  5431.       length += list_length (TREE_VALUE (TREE_VALUE (outer)));
  5432.       /* friend functions.  */
  5433.       length += list_length (TREE_TYPE (TREE_VALUE (outer)));
  5434.     }
  5435.     }
  5436.   else
  5437.     {
  5438.       length = list_length (functions);
  5439.     }
  5440.  
  5441.   if (final_cp)
  5442.     candidates = final_cp;
  5443.   else
  5444.     candidates = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
  5445.  
  5446.   cp = candidates;
  5447.  
  5448.   assert (TREE_CODE (TREE_VALUE (functions)) != TREE_LIST);
  5449.   /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST.  */
  5450.  
  5451.   for (outer = functions; outer; outer = TREE_CHAIN (outer))
  5452.     {
  5453.       function = TREE_VALUE (outer);
  5454.       if (TREE_CODE (function) != FUNCTION_DECL)
  5455.     {
  5456.       if (TREE_CODE (function) == CONST_DECL)
  5457.         error_with_decl (function, "enumeral value `%s' conflicts with function of same name");
  5458.       else if (TREE_CODE (function) == VAR_DECL)
  5459.         if (TREE_STATIC (function))
  5460.           error_with_decl (function, "variable `%s' conflicts with function of same name");
  5461.         else
  5462.           error_with_decl (function, "constant field `%s' conflicts with function of same name");
  5463.       else if (TREE_CODE (function) == TYPE_DECL)
  5464.         continue;
  5465.       else abort ();
  5466.       error ("at this point in file");
  5467.       continue;
  5468.     }
  5469.       function = DECL_MAIN_VARIANT (function);
  5470.       /* Can't use alloca here, since result might be
  5471.      passed to calling function.  */
  5472.       cp->harshness
  5473.     = (unsigned short *)oballoc ((parmlength+1) * sizeof (short));
  5474.       compute_conversion_costs (function, parms, cp, parmlength);
  5475.       if (cp[0].evil == 0)
  5476.     {
  5477.       cp[1].evil = 1;
  5478.       if (final_cp
  5479.           && cp[0].user == 0 && cp[0].b_or_d == 0
  5480.           && cp[0].easy <= 1)
  5481.         {
  5482.           final_cp[0].easy = cp[0].easy;
  5483.           return function;
  5484.         }
  5485.       cp++;
  5486.     }
  5487.     }
  5488.  
  5489.   if (cp - candidates)
  5490.     {
  5491.       tree rval = error_mark_node;
  5492.  
  5493.       /* Leave marker.  */
  5494.       cp[0].evil = 1;
  5495.       if (cp - candidates > 1)
  5496.     {
  5497.       struct candidate *best_cp
  5498.         = ideal_candidate (NULL_TREE, candidates,
  5499.                    cp - candidates, parms, parmlength);
  5500.       if (best_cp == 0)
  5501.         {
  5502.           if (complain)
  5503.         error ("call of overloaded `%s' is ambiguous", IDENTIFIER_POINTER (fnname));
  5504.           return error_mark_node;
  5505.         }
  5506.       else
  5507.         rval = best_cp->function;
  5508.     }
  5509.       else
  5510.     {
  5511.       cp -= 1;
  5512.       if (cp->evil > 1)
  5513.         {
  5514.           if (complain)
  5515.         error ("type conversion ambiguous");
  5516.         }
  5517.       else
  5518.         rval = cp->function;
  5519.     }
  5520.  
  5521.       if (final_cp)
  5522.     return rval;
  5523.  
  5524.       return build_function_call (rval, parms);
  5525.     }
  5526.   else if (complain)
  5527.     {
  5528.       tree name;
  5529.       char *err_name;
  5530.       /* Initialize name for error reporting.  */
  5531.       if (TREE_CODE (functions) == TREE_LIST)
  5532.     name = TREE_PURPOSE (functions);
  5533.       else
  5534.     name = DECL_ORIGINAL_NAME (functions);
  5535.  
  5536.       if (OPERATOR_NAME_P (name))
  5537.     {
  5538.       char *opname = operator_name_string (name);
  5539.       err_name = (char *)alloca (strlen (opname) + 12);
  5540.       sprintf (err_name, "operator %s", opname);
  5541.     }
  5542.       else
  5543.     err_name = IDENTIFIER_POINTER (name);
  5544.  
  5545.       report_type_mismatch (cp, parms, "function", err_name);
  5546.     }
  5547.   return error_mark_node;
  5548. }
  5549.  
  5550. void
  5551. init_class_processing ()
  5552. {
  5553.   current_class_stacksize = 10;
  5554.   current_class_base = (tree *)xmalloc(current_class_stacksize * sizeof (tree));
  5555.   current_class_stack = current_class_base;
  5556.  
  5557.   current_lang_stacksize = 10;
  5558.   current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree));
  5559.   current_lang_stack = current_lang_base;
  5560.  
  5561.   delta_name = get_identifier (VTABLE_DELTA_NAME);
  5562.   pfn_name = get_identifier (VTABLE_PFN_NAME);
  5563.  
  5564.   /* Keep these values lying around.  */
  5565.   minus_one_node = build_int_2 (-1, 0);
  5566.   the_null_vtable_entry = build_vtable_entry (integer_zero_node, integer_zero_node);
  5567.   base_layout_decl = build_lang_field_decl (FIELD_DECL, NULL_TREE, error_mark_node);
  5568.   TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE);
  5569.  
  5570.   obstack_init (&class_obstack);
  5571. }
  5572.  
  5573. /* Set current scope to NAME. CODE tells us if this is a
  5574.    STRUCT, UNION, or ENUM environment.
  5575.  
  5576.    NAME may end up being NULL_TREE if this is an anonymous or
  5577.    late-bound struct (as in "struct { ... } foo;")  */
  5578.  
  5579. /* Here's a subroutine we need because C lacks lambdas.  */
  5580. void
  5581. unuse_fields (type)
  5582.      tree type;
  5583. {
  5584.   tree fields;
  5585.  
  5586.   for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
  5587.     {
  5588.       if (TREE_CODE (fields) != FIELD_DECL)
  5589.     continue;
  5590.  
  5591.       TREE_USED (fields) = 0;
  5592.       if (DECL_ANON_UNION_ELEM (fields))
  5593.     unuse_fields (TREE_TYPE (fields));
  5594.     }
  5595. }
  5596.  
  5597. /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
  5598.    appropriate values, found by looking up the type definition of
  5599.    NAME (as a CODE).
  5600.  
  5601.    If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
  5602.    which can be seen locally to the class. They are shadowed by
  5603.    any subsequent local declaration (including parameter names).
  5604.  
  5605.    If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
  5606.    which have static meaning (i.e., static members, static
  5607.    member functions, enum declarations, etc).
  5608.  
  5609.    So that we may avoid calls to lookup_name, we cache the TYPE_DECL
  5610.    in the TREE_TYPE field of the name.
  5611.  
  5612.    For multiple inheritance, we perform a two-pass depth-first search
  5613.    of the type lattice.  The first pass performs a pre-order search,
  5614.    marking types after the type has had its fields installed in
  5615.    the appropriate IDENTIFIER_CLASS_VALUE slot.  The second pass merely
  5616.    unmarks the marked types.  If a field or member function name
  5617.    appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
  5618.    that name becomes `error_mark_node'.  */
  5619.  
  5620. void
  5621. pushclass (type, modify)
  5622.      tree type;
  5623.      int modify;
  5624. {
  5625.   push_memoized_context (type, modify);
  5626.  
  5627.   *current_class_stack++ = current_class_name;
  5628.   *current_class_stack++ = current_class_type;
  5629.   if (current_class_stack >= current_class_base + current_class_stacksize)
  5630.     {
  5631.       current_class_base =
  5632.     (tree *)xrealloc (current_class_base,
  5633.               sizeof (tree) * (current_class_stacksize + 10));
  5634.       current_class_stack = current_class_base + current_class_stacksize;
  5635.       current_class_stacksize += 10;
  5636.     }
  5637.  
  5638.   type = TYPE_MAIN_VARIANT (type);
  5639.   current_class_name = TYPE_NAME (type);
  5640.   if (TREE_CODE (current_class_name) == TYPE_DECL)
  5641.     current_class_name = DECL_NAME (current_class_name);
  5642.   current_class_type = type;
  5643.  
  5644.   if (type != prev_class_type && prev_class_type != NULL_TREE
  5645.       && current_class_stack == current_class_base + 2)
  5646.     {
  5647.       popclass (-1);
  5648.       prev_class_type = 0;
  5649.     }
  5650.  
  5651.   if (modify)
  5652.     {
  5653.       tree tags;
  5654.  
  5655.       if (type != prev_class_type)
  5656.     {
  5657.       build_mi_matrix (type);
  5658.       push_class_decls (type);
  5659.       free_mi_matrix ();
  5660.       prev_class_type = type;
  5661.     }
  5662.       else
  5663.     unuse_fields (type);
  5664.  
  5665.       tags = CLASSTYPE_TAGS (type);
  5666.       while (tags)
  5667.     {
  5668.       TREE_NONLOCAL (TREE_VALUE (tags)) = 1;
  5669.       pushtag (TREE_PURPOSE (tags), TREE_VALUE (tags));
  5670.       if (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) == NULL_TREE
  5671.           && TREE_CODE (TYPE_NAME (TREE_VALUE (tags))) == TYPE_DECL)
  5672.         IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags))
  5673.           = TYPE_NAME (TREE_VALUE (tags));
  5674.       tags = TREE_CHAIN (tags);
  5675.     }
  5676.     }
  5677.   else
  5678.     pushlevel_class ();
  5679.  
  5680.   if (flag_cadillac)
  5681.     cadillac_push_class (type);
  5682. }
  5683.  
  5684. /* Get out of the current class scope. If we were in a class scope
  5685.    previously, that is the one popped to.  The flag MODIFY tells
  5686.    whether the current scope declarations needs to be modified
  5687.    as a result of popping to the new scope.  */
  5688. void
  5689. popclass (modify)
  5690.      int modify;
  5691. {
  5692.   if (flag_cadillac)
  5693.     cadillac_pop_class ();
  5694.  
  5695.   if (modify < 0)
  5696.     {
  5697.       /* Back this old class out completely.  */
  5698.       tree tags = CLASSTYPE_TAGS (prev_class_type);
  5699.  
  5700.       pop_class_decls (prev_class_type);
  5701.       while (tags)
  5702.     {
  5703.       TREE_NONLOCAL (TREE_VALUE (tags)) = 0;
  5704.       IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) = NULL_TREE;
  5705.       tags = TREE_CHAIN (tags);
  5706.     }
  5707.       return;
  5708.     }
  5709.   if (modify)
  5710.     {
  5711.       /* Just remove from this class what didn't make
  5712.      it into IDENTIFIER_CLASS_VALUE.  */
  5713.       tree tags = CLASSTYPE_TAGS (current_class_type);
  5714.  
  5715.       while (tags)
  5716.     {
  5717.       TREE_NONLOCAL (TREE_VALUE (tags)) = 0;
  5718.       IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) = NULL_TREE;
  5719.       tags = TREE_CHAIN (tags);
  5720.     }
  5721.     }
  5722.   else
  5723.     poplevel_class ();
  5724.  
  5725.   current_class_type = *--current_class_stack;
  5726.   current_class_name = *--current_class_stack;
  5727.  
  5728.   if (current_class_type)
  5729.     {
  5730.       if (CLASSTYPE_VTBL_PTR (current_class_type))
  5731.     {
  5732.       current_vtable_decl = lookup_name (DECL_NAME (CLASSTYPE_VTBL_PTR (current_class_type)));
  5733.       if (current_vtable_decl)
  5734.         current_vtable_decl = build_indirect_ref (current_vtable_decl, 0);
  5735.     }
  5736.       current_class_decl = lookup_name (get_identifier (THIS_NAME));
  5737.       if (current_class_decl)
  5738.     {
  5739.       if (TREE_CODE (TREE_TYPE (current_class_decl)) == POINTER_TYPE)
  5740.         {
  5741.           /* Can't call build_indirect_ref here, because it has special
  5742.          logic to return C_C_D given this argument.  */
  5743.           C_C_D = build1 (INDIRECT_REF, current_class_type, current_class_decl);
  5744.           TREE_READONLY (C_C_D) = TREE_READONLY (TREE_TYPE (TREE_TYPE (current_class_decl)));
  5745.           TREE_VOLATILE (C_C_D) = TREE_VOLATILE (TREE_TYPE (TREE_TYPE (current_class_decl)));
  5746.         }
  5747.       else
  5748.         C_C_D = current_class_decl;
  5749.     }
  5750.       else C_C_D = NULL_TREE;
  5751.     }
  5752.   else
  5753.     {
  5754.       current_class_decl = NULL_TREE;
  5755.       current_vtable_decl = NULL_TREE;
  5756.       C_C_D = NULL_TREE;
  5757.     }
  5758.  
  5759.   pop_memoized_context (modify);
  5760. }
  5761.  
  5762. /* Set global variables CURRENT_LANG_NAME to appropriate value
  5763.    so that behavior of name-mangline machinery is correct.  */
  5764.  
  5765. void
  5766. push_lang_context (name)
  5767.      tree name;
  5768. {
  5769.   *current_lang_stack++ = current_lang_name;
  5770.   if (current_lang_stack >= current_lang_base + current_lang_stacksize)
  5771.     {
  5772.       current_lang_base =
  5773.     (tree *)xrealloc (current_lang_base,
  5774.               sizeof (tree) * (current_lang_stacksize + 10));
  5775.       current_lang_stack = current_lang_base + current_lang_stacksize;
  5776.       current_lang_stacksize += 10;
  5777.     }
  5778.  
  5779.   if (name == lang_name_cplusplus)
  5780.     {
  5781.       strict_prototype = strict_prototypes_lang_cplusplus;
  5782.       current_lang_name = name;
  5783. #ifdef OBJCPLUS
  5784.       install_reserved_words (lang_cplusplus);
  5785. #endif /* OBJCPLUS */
  5786.     }
  5787.   else if (name == lang_name_c)
  5788.     {
  5789.       strict_prototype = strict_prototypes_lang_c;
  5790.       current_lang_name = name;
  5791. #ifdef OBJCPLUS
  5792.       install_reserved_words (lang_c);
  5793. #endif /* OBJCPLUS */
  5794.     }
  5795. #ifdef OBJCPLUS
  5796.   else if (name == lang_name_objc)
  5797.     {
  5798.       /* for now, treat Objective-C like C, other code depends on this */
  5799.       strict_prototype = strict_prototypes_lang_c;
  5800.       current_lang_name = lang_name_c;
  5801.       install_reserved_words (lang_objc);
  5802.     }
  5803. #endif /* OBJCPLUS */
  5804.   else
  5805.     error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
  5806.  
  5807.   if (flag_cadillac)
  5808.     cadillac_push_lang (name);
  5809. }
  5810.   
  5811. /* Get out of the current language scope.  */
  5812. void
  5813. pop_lang_context ()
  5814. {
  5815.   if (flag_cadillac)
  5816.     cadillac_pop_lang ();
  5817.  
  5818.   current_lang_name = *--current_lang_stack;
  5819.   if (current_lang_name == lang_name_cplusplus)
  5820. #ifdef OBJCPLUS
  5821.     {
  5822.       strict_prototype = strict_prototypes_lang_cplusplus;
  5823.       install_reserved_words (lang_cplusplus);
  5824.     }
  5825. #else /* OBJCPLUS */
  5826.     strict_prototype = strict_prototypes_lang_cplusplus;
  5827. #endif /* OBJCPLUS */
  5828.   else if (current_lang_name == lang_name_c)
  5829. #ifdef OBJCPLUS
  5830.     {
  5831.       strict_prototype = strict_prototypes_lang_c;
  5832.       install_reserved_words (lang_c);
  5833.     }
  5834. #else /* OBJCPLUS */
  5835.     strict_prototype = strict_prototypes_lang_c;
  5836. #endif /* OBJCPLUS */
  5837. #ifdef OBJCPLUS
  5838.   else if (current_lang_name == lang_name_objc)
  5839.     {
  5840.       strict_prototype = strict_prototypes_lang_c;
  5841.       install_reserved_words (lang_objc);
  5842.     }
  5843. #endif /* OBJCPLUS */
  5844. }
  5845.  
  5846. int
  5847. root_lang_context_p ()
  5848. {
  5849.   return current_lang_stack == current_lang_base;
  5850. }
  5851.  
  5852. /* Type instantiation routines.  */
  5853.  
  5854. /* This function will instantiate the type of the expression given
  5855.    in RHS to match the type of LHSTYPE.  If LHSTYPE is NULL_TREE,
  5856.    or other errors exist, the TREE_TYPE of RHS will be ERROR_MARK_NODE.
  5857.  
  5858.    This function is used in build_modify_expr, actualparameterlist,
  5859.    build_c_cast, and compute_conversion_costs.  */
  5860. tree
  5861. instantiate_type (lhstype, rhs, complain)
  5862.      tree lhstype, rhs;
  5863.      int complain;
  5864. {
  5865.   if (TREE_CODE (rhs) == OP_IDENTIFIER)
  5866.     return build_instantiated_decl (lhstype, rhs);
  5867.  
  5868.   if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
  5869.     {
  5870.       if (complain)
  5871.     error ("not enough type information");
  5872.       return error_mark_node;
  5873.     }
  5874.  
  5875.   if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
  5876.     return rhs;
  5877.  
  5878.   /* This should really only be used when attempting to distinguish
  5879.      what sort of a pointer to function we have.  For now, any
  5880.      arithmethic operation which is not supported on pointers
  5881.      is rejected as an error.  */
  5882.  
  5883.   switch (TREE_CODE (rhs))
  5884.     {
  5885.     case TYPE_EXPR:
  5886.     case CONVERT_EXPR:
  5887.     case SAVE_EXPR:
  5888.     case CONSTRUCTOR:
  5889.     case BUFFER_REF:
  5890.       assert (0);
  5891.       return error_mark_node;
  5892.  
  5893.     case INDIRECT_REF:
  5894.     case ARRAY_REF:
  5895.       TREE_TYPE (rhs) = lhstype;
  5896.       lhstype = build_pointer_type (lhstype);
  5897.       TREE_OPERAND (rhs, 0)
  5898.     = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
  5899.       if (TREE_OPERAND (rhs, 0) == error_mark_node)
  5900.     return error_mark_node;
  5901.  
  5902.       return rhs;
  5903.  
  5904.     case NOP_EXPR:
  5905.       rhs = copy_node (TREE_OPERAND (rhs, 0));
  5906.       TREE_TYPE (rhs) = unknown_type_node;
  5907.       return instantiate_type (lhstype, rhs, complain);
  5908.  
  5909.     case COMPONENT_REF:
  5910.       {
  5911.     tree field = TREE_OPERAND (rhs, 1);
  5912.     if (TREE_CODE (field) == TREE_LIST)
  5913.       {
  5914.         tree function = instantiate_type (lhstype, field, complain);
  5915.         if (function == error_mark_node)
  5916.           return error_mark_node;
  5917.         assert (TREE_CODE (function) == FUNCTION_DECL);
  5918.         if (DECL_VIRTUAL_P (function))
  5919.           {
  5920.         tree base = TREE_OPERAND (rhs, 0);
  5921.         tree base_ptr = build_unary_op (ADDR_EXPR, base, 0);
  5922.         if (base_ptr == error_mark_node)
  5923.           return error_mark_node;
  5924.         base_ptr = convert_pointer_to (DECL_VCONTEXT (function), base_ptr);
  5925.         if (base_ptr == error_mark_node)
  5926.           return error_mark_node;
  5927.         return build_vfn_ref (&base_ptr, base, DECL_VINDEX (function));
  5928.           }
  5929.         return function;
  5930.       }
  5931.  
  5932.     assert (TREE_CODE (field) == FIELD_DECL);
  5933.     assert (!(TREE_CODE (TREE_TYPE (field)) == FUNCTION_TYPE
  5934.           || TREE_CODE (TREE_TYPE (field)) == METHOD_TYPE));
  5935.  
  5936.     TREE_TYPE (rhs) = lhstype;
  5937.     /* First look for an exact match  */
  5938.  
  5939.     while (field && TREE_TYPE (field) != lhstype)
  5940.       field = TREE_CHAIN (field);
  5941.     if (field)
  5942.       {
  5943.         TREE_OPERAND (rhs, 1) = field;
  5944.         return rhs;
  5945.       }
  5946.  
  5947.     /* No exact match found, look for a compatible function.  */
  5948.     field = TREE_OPERAND (rhs, 1);
  5949.     while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
  5950.       field = TREE_CHAIN (field);
  5951.     if (field)
  5952.       {
  5953.         TREE_OPERAND (rhs, 1) = field;
  5954.         field = TREE_CHAIN (field);
  5955.         while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
  5956.           field = TREE_CHAIN (field);
  5957.         if (field)
  5958.           {
  5959.         if (complain)
  5960.           error ("ambiguous overload for COMPONENT_REF requested");
  5961.         return error_mark_node;
  5962.           }
  5963.       }
  5964.     else
  5965.       {
  5966.         if (complain)
  5967.           error ("no appropriate overload exists for COMPONENT_REF");
  5968.         return error_mark_node;
  5969.       }
  5970.     return rhs;
  5971.       }
  5972.  
  5973.     case TREE_LIST:
  5974.       {
  5975.     tree elem, baselink, name;
  5976.     int globals = overloaded_globals_p (rhs);
  5977.  
  5978.     /* If there's only one function we know about, return that.  */
  5979.     if (globals > 0 && TREE_CHAIN (rhs) == NULL_TREE)
  5980.       return TREE_VALUE (rhs);
  5981.  
  5982.     /* First look for an exact match.  Search either overloaded
  5983.        functions or member functions.  May have to undo what
  5984.        `default_conversion' or `datatype' might do to lhstype.  */
  5985.  
  5986.     if (TREE_CODE (lhstype) == POINTER_TYPE)
  5987.       if (TREE_CODE (TREE_TYPE (lhstype)) == FUNCTION_TYPE
  5988.           || TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE)
  5989.         lhstype = TREE_TYPE (lhstype);
  5990.       else
  5991.         {
  5992.           if (complain)
  5993.         error ("invalid type combination for overload");
  5994.           return error_mark_node;
  5995.         }
  5996.  
  5997.     if (TREE_CODE (lhstype) != FUNCTION_TYPE && globals > 0)
  5998.       {
  5999.         if (complain)
  6000.           error ("cannot resolve overloaded function `%s' based on non-function type",
  6001.              IDENTIFIER_POINTER (TREE_PURPOSE (rhs)));
  6002.         return error_mark_node;
  6003.       }
  6004.  
  6005.     if (globals > 0)
  6006.       {
  6007.         assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL);
  6008.         elem = rhs;
  6009.         while (elem)
  6010.           if (TREE_TYPE (TREE_VALUE (elem)) != lhstype)
  6011.         elem = TREE_CHAIN (elem);
  6012.           else
  6013.         return TREE_VALUE (elem);
  6014.         /* No exact match found, look for a compatible function.  */
  6015.         elem = rhs;
  6016.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (TREE_VALUE (elem)), 1))
  6017.           elem = TREE_CHAIN (elem);
  6018.         if (elem)
  6019.           {
  6020.         tree save_elem = TREE_VALUE (elem);
  6021.         elem = TREE_CHAIN (elem);
  6022.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (TREE_VALUE (elem)), 0))
  6023.           elem = TREE_CHAIN (elem);
  6024.         if (elem)
  6025.           {
  6026.             if (complain)
  6027.               error ("ambiguous overload for overloaded function requested");
  6028.             return error_mark_node;
  6029.           }
  6030.         return save_elem;
  6031.           }
  6032.         if (complain)
  6033.           {
  6034.         if (TREE_CHAIN (rhs))
  6035.           error ("no appropriate overload for overloaded function `%s' exists",
  6036.              IDENTIFIER_POINTER (TREE_PURPOSE (rhs)));
  6037.         else
  6038.           error ("function `%s' has inappropriate type signature",
  6039.              IDENTIFIER_POINTER (TREE_PURPOSE (rhs)));
  6040.           }
  6041.         return error_mark_node;
  6042.       }
  6043.  
  6044.     if (TREE_NONLOCAL (rhs))
  6045.       {
  6046.         /* Got to get it as a baselink.  */
  6047.         rhs = lookup_fnfields (CLASSTYPE_AS_LIST (current_class_type),
  6048.                    TREE_PURPOSE (rhs), 0);
  6049.       }
  6050.     else
  6051.       {
  6052.         assert (TREE_CHAIN (rhs) == NULL_TREE);
  6053.         if (TREE_CODE (TREE_VALUE (rhs)) == TREE_LIST)
  6054.           rhs = TREE_VALUE (rhs);
  6055.         assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL);
  6056.       }
  6057.  
  6058.     for (baselink = rhs; baselink;
  6059.          baselink = next_baselink (baselink))
  6060.       {
  6061.         elem = TREE_VALUE (baselink);
  6062.         while (elem)
  6063.           if (TREE_TYPE (elem) != lhstype)
  6064.         elem = TREE_CHAIN (elem);
  6065.           else
  6066.         return elem;
  6067.       }
  6068.  
  6069.     /* No exact match found, look for a compatible method.  */
  6070.     for (baselink = rhs; baselink;
  6071.          baselink = next_baselink (baselink))
  6072.       {
  6073.         elem = TREE_VALUE (baselink);
  6074.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 1))
  6075.           elem = TREE_CHAIN (elem);
  6076.         if (elem)
  6077.           {
  6078.         tree save_elem = elem;
  6079.         elem = TREE_CHAIN (elem);
  6080.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 0))
  6081.           elem = TREE_CHAIN (elem);
  6082.         if (elem)
  6083.           {
  6084.             if (complain)
  6085.               error ("ambiguous overload for overloaded method requested");
  6086.             return error_mark_node;
  6087.           }
  6088.         return save_elem;
  6089.           }
  6090.         name = DECL_ORIGINAL_NAME (TREE_VALUE (rhs));
  6091.         if (TREE_CODE (lhstype) == FUNCTION_TYPE && globals < 0)
  6092.           {
  6093.         /* Try to instantiate from non-member functions.  */
  6094.         rhs = IDENTIFIER_GLOBAL_VALUE (name);
  6095.         if (rhs && TREE_CODE (rhs) == TREE_LIST)
  6096.           {
  6097.             /* This code seems to be missing a `return'.  */
  6098.             abort ();
  6099.             instantiate_type (lhstype, rhs, complain);
  6100.           }
  6101.           }
  6102.  
  6103.         if (complain)
  6104.           error ("no static member functions named `%s'",
  6105.              IDENTIFIER_POINTER (name));
  6106.         return error_mark_node;
  6107.       }
  6108.       }
  6109.  
  6110.     case CALL_EXPR:
  6111.       /* This is too hard for now.  */
  6112.       assert (0);
  6113.       return error_mark_node;
  6114.  
  6115.     case PLUS_EXPR:
  6116.     case MINUS_EXPR:
  6117.     case COMPOUND_EXPR:
  6118.       TREE_OPERAND (rhs, 0) = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
  6119.       if (TREE_OPERAND (rhs, 0) == error_mark_node)
  6120.     return error_mark_node;
  6121.       TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
  6122.       if (TREE_OPERAND (rhs, 1) == error_mark_node)
  6123.     return error_mark_node;
  6124.  
  6125.       TREE_TYPE (rhs) = lhstype;
  6126.       return rhs;
  6127.  
  6128.     case MULT_EXPR:
  6129.     case TRUNC_DIV_EXPR:
  6130.     case FLOOR_DIV_EXPR:
  6131.     case CEIL_DIV_EXPR:
  6132.     case ROUND_DIV_EXPR:
  6133.     case RDIV_EXPR:
  6134.     case TRUNC_MOD_EXPR:
  6135.     case FLOOR_MOD_EXPR:
  6136.     case CEIL_MOD_EXPR:
  6137.     case ROUND_MOD_EXPR:
  6138.     case FIX_ROUND_EXPR:
  6139.     case FIX_FLOOR_EXPR:
  6140.     case FIX_CEIL_EXPR:
  6141.     case FIX_TRUNC_EXPR:
  6142.     case FLOAT_EXPR:
  6143.     case NEGATE_EXPR:
  6144.     case ABS_EXPR:
  6145.     case MAX_EXPR:
  6146.     case MIN_EXPR:
  6147.     case FFS_EXPR:
  6148.  
  6149.     case BIT_AND_EXPR:
  6150.     case BIT_IOR_EXPR:
  6151.     case BIT_XOR_EXPR:
  6152.     case LSHIFT_EXPR:
  6153.     case RSHIFT_EXPR:
  6154.     case LROTATE_EXPR:
  6155.     case RROTATE_EXPR:
  6156.  
  6157.     case PREINCREMENT_EXPR:
  6158.     case PREDECREMENT_EXPR:
  6159.     case POSTINCREMENT_EXPR:
  6160.     case POSTDECREMENT_EXPR:
  6161.       if (complain)
  6162.     error ("illegal operation on uninstantiated type");
  6163.       return error_mark_node;
  6164.  
  6165.     case TRUTH_AND_EXPR:
  6166.     case TRUTH_OR_EXPR:
  6167.     case LT_EXPR:
  6168.     case LE_EXPR:
  6169.     case GT_EXPR:
  6170.     case GE_EXPR:
  6171.     case EQ_EXPR:
  6172.     case NE_EXPR:
  6173.     case TRUTH_ANDIF_EXPR:
  6174.     case TRUTH_ORIF_EXPR:
  6175.     case TRUTH_NOT_EXPR:
  6176.       if (complain)
  6177.     error ("not enough type information");
  6178.       return error_mark_node;
  6179.  
  6180.     case COND_EXPR:
  6181.       if (type_unknown_p (TREE_OPERAND (rhs, 0)))
  6182.     {
  6183.       if (complain)
  6184.         error ("not enough type information");
  6185.       return error_mark_node;
  6186.     }
  6187.       TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
  6188.       if (TREE_OPERAND (rhs, 1) == error_mark_node)
  6189.     return error_mark_node;
  6190.       TREE_OPERAND (rhs, 2) = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), complain);
  6191.       if (TREE_OPERAND (rhs, 2) == error_mark_node)
  6192.     return error_mark_node;
  6193.  
  6194.       TREE_TYPE (rhs) = lhstype;
  6195.       return rhs;
  6196.  
  6197.     case MODIFY_EXPR:
  6198.       TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
  6199.       if (TREE_OPERAND (rhs, 1) == error_mark_node)
  6200.     return error_mark_node;
  6201.  
  6202.       TREE_TYPE (rhs) = lhstype;
  6203.       return rhs;
  6204.       
  6205.     case ADDR_EXPR:
  6206.       if (TREE_CODE (lhstype) != POINTER_TYPE)
  6207.     {
  6208.       if (complain)
  6209.         error ("type for resolving address of overloaded function must be pointer type");
  6210.       return error_mark_node;
  6211.     }
  6212.       TREE_TYPE (rhs) = lhstype;
  6213.       lhstype = TREE_TYPE (lhstype);
  6214.       TREE_OPERAND (rhs, 0) = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
  6215.       if (TREE_OPERAND (rhs, 0) == error_mark_node)
  6216.     return error_mark_node;
  6217.  
  6218.       mark_addressable (TREE_OPERAND (rhs, 0));
  6219.       return rhs;
  6220.  
  6221.     case ENTRY_VALUE_EXPR:
  6222.       assert (0);
  6223.       return error_mark_node;
  6224.  
  6225.     case ERROR_MARK:
  6226.       return error_mark_node;
  6227.  
  6228.     default:
  6229.       assert (0);
  6230.       return error_mark_node;
  6231.     }
  6232. }
  6233.  
  6234. /* This routine is called when we finally know the type of expression
  6235.    we are looking for.  If the operator encoded by EXP can take an
  6236.    argument of type TYPE, return the FUNCTION_DECL for that operator.  */
  6237. tree
  6238. build_instantiated_decl (type, exp)
  6239.      tree type, exp;
  6240. {
  6241.   tree parmtypes, decl, name;
  6242.  
  6243.   assert (TREE_CODE (exp) == OP_IDENTIFIER);
  6244.   type = datatype (type);
  6245.   if (TREE_CODE (type) != POINTER_TYPE
  6246.       || (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
  6247.       && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE))
  6248.     {
  6249.       error ("invalid type used to resolve overloaded function");
  6250.       return error_mark_node;
  6251.     }
  6252.  
  6253.  
  6254.   /* Now we know the type of this function, so overload it.  */
  6255.   parmtypes = TYPE_ARG_TYPES (TREE_TYPE (type));
  6256.   name = build_operator_fnname (&exp, parmtypes, 0);
  6257.   if (name)
  6258.     {
  6259.       name = build_decl_overload (IDENTIFIER_POINTER (name), parmtypes, 1);
  6260.       decl = lookup_name (name);
  6261.       if (decl)
  6262.     return decl;
  6263.       error ("no suitable declaration of `operator %s' for overloading",
  6264.          operator_name_string (name));
  6265.       return error_mark_node;
  6266.     }
  6267.   return error_mark_node;
  6268. }
  6269.  
  6270. /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
  6271.    for the given TYPE.  */
  6272. static tree
  6273. get_vtable_name (type)
  6274.      tree type;
  6275. {
  6276.   char *buf = (char *)alloca (sizeof (VTABLE_NAME_FORMAT)
  6277.                   + TYPE_NAME_LENGTH (type) + 2);
  6278.   sprintf (buf, VTABLE_NAME_FORMAT, TYPE_NAME_STRING (type));
  6279.   return get_identifier (buf);
  6280. }
  6281.  
  6282. /* Return the name of the virtual function pointer field
  6283.    (as an IDENTIFIER_NODE) for the given TYPE.  Note that
  6284.    this may have to look back through base types to find the
  6285.    ultimate field name.  (For single inheritance, these could
  6286.    all be the same name.  Who knows for multiple inheritance).  */
  6287. static tree
  6288. get_vfield_name (type)
  6289.      tree type;
  6290. {
  6291.   char *buf;
  6292.  
  6293.   while (CLASSTYPE_N_BASECLASSES (type)
  6294.      && TYPE_VIRTUAL_P (CLASSTYPE_BASECLASS (type, 1))
  6295.      && ! CLASSTYPE_VIA_VIRTUAL (type, 1))
  6296.     type = CLASSTYPE_BASECLASS (type, 1);
  6297.  
  6298.   buf = (char *)alloca (sizeof (VFIELD_NAME_FORMAT)
  6299.             + TYPE_NAME_LENGTH (type) + 2);
  6300.   sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
  6301.   return get_identifier (buf);
  6302. }
  6303.  
  6304. void
  6305. print_class_statistics ()
  6306. {
  6307. #ifdef GATHER_STATISTICS
  6308.   fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
  6309.   fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
  6310.   fprintf (stderr, "build_method_call = %d (inner = %d)\n",
  6311.        n_build_method_call, n_inner_fields_searched);
  6312.   if (n_vtables)
  6313.     {
  6314.       fprintf (stderr, "vtables = %d; vtable searches = %d\n",
  6315.            n_vtables, n_vtable_searches);
  6316.       fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
  6317.            n_vtable_entries, n_vtable_elems);
  6318.     }
  6319. #endif
  6320. }
  6321.