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

  1. /* Functions related to invoking methods and overloaded functions.
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.    Contributed by Michael Tiemann (tiemann@cygnus.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* High-level class interface. */
  23.  
  24. #include "config.h"
  25. #include "tree.h"
  26. #include "cplus-tree.h"
  27. #include "flags.h"
  28. #include "assert.h"
  29. #include <stdio.h>
  30. #include "cplus-class.h"
  31.  
  32. #include "obstack.h"
  33. #define obstack_chunk_alloc xmalloc
  34. #define obstack_chunk_free free
  35.  
  36. extern int xmalloc ();
  37. extern void free ();
  38.  
  39. /* See cplus-decl.c for comment of this variable.  */
  40. extern int flag_int_enum_equivalence;
  41.  
  42. /* Compute the ease with which a conversion can be performed
  43.    between an expected and the given type.  */
  44. static int convert_harshness ();
  45.  
  46. /* Ordering function for overload resolution.  */
  47. int
  48. rank_for_overload (x, y)
  49.      struct candidate *x, *y;
  50. {
  51.   if (y->evil - x->evil)
  52.     return y->evil - x->evil;
  53.   if ((y->harshness[0] & 128) ^ (x->harshness[0] & 128))
  54.     return y->harshness[0] - x->harshness[0];
  55.   if (y->user - x->user)
  56.     return y->user - x->user;
  57.   if (y->b_or_d - x->b_or_d)
  58.     return y->b_or_d - x->b_or_d;
  59.   return y->easy - x->easy;
  60. }
  61.  
  62. /* TYPE is the type we wish to convert to.  PARM is the parameter
  63.    we have to work with.  We use a somewhat arbitrary cost function
  64.    to measure this conversion.  */
  65. static int
  66. convert_harshness (type, parmtype, parm)
  67.      register tree type, parmtype;
  68.      tree parm;
  69. {
  70.   register enum tree_code codel = TREE_CODE (type);
  71.   register enum tree_code coder = TREE_CODE (parmtype);
  72.  
  73. #ifdef GATHER_STATISTICS
  74.   n_convert_harshness++;
  75. #endif
  76.  
  77.   if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
  78.     return 0;
  79.  
  80.   if (coder == ERROR_MARK)
  81.     return 1;
  82.  
  83.   if (codel == POINTER_TYPE
  84.       && (coder == METHOD_TYPE || coder == FUNCTION_TYPE))
  85.     {
  86.       tree p1, p2;
  87.       int harshness, new_harshness;
  88.  
  89.       /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be.  */
  90.       type = TREE_TYPE (type);
  91.  
  92.       if (coder != TREE_CODE (type))
  93.     return 1;
  94.  
  95.       harshness = 0;
  96.  
  97.       /* We allow the default conversion between function type
  98.      and pointer-to-function type for free.  */
  99.       if (type == parmtype)
  100.     return 0;
  101.  
  102.       /* Compare return types.  */
  103.       harshness |= convert_harshness (TREE_TYPE (type), TREE_TYPE (parmtype), 0);
  104.       if (harshness & 1)
  105.     return 1;
  106.       p1 = TYPE_ARG_TYPES (type);
  107.       p2 = TYPE_ARG_TYPES (parmtype);
  108.       while (p1 && p2)
  109.     {
  110.       new_harshness = convert_harshness (TREE_VALUE (p1), TREE_VALUE (p2), 0);
  111.       if (new_harshness & 1)
  112.         return 1;
  113.       if ((new_harshness & 7) == 0)
  114.         harshness += new_harshness;
  115.       else
  116.         harshness |= new_harshness;
  117.       p1 = TREE_CHAIN (p1);
  118.       p2 = TREE_CHAIN (p2);
  119.     }
  120.       if (p1 == p2)
  121.     return harshness;
  122.       if (p2)
  123.     return 1;
  124.       if (p1)
  125.     return harshness | (TREE_PURPOSE (p1) == NULL_TREE);
  126.     }
  127.   else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
  128.     {
  129.       int harshness;
  130.  
  131.       /* Get to the OFFSET_TYPE that this might be.  */
  132.       type = TREE_TYPE (type);
  133.  
  134.       if (coder != TREE_CODE (type))
  135.     return 1;
  136.  
  137.       harshness = 0;
  138.  
  139.       if (TYPE_OFFSET_BASETYPE (type) == TYPE_OFFSET_BASETYPE (parmtype))
  140.     harshness = 0;
  141.       else if (get_base_type (TYPE_OFFSET_BASETYPE (type),
  142.                   TYPE_OFFSET_BASETYPE (parmtype), 0))
  143.     harshness = (1<<3);
  144.       else
  145.     return 1;
  146.       /* Now test the OFFSET_TYPE's target compatability.  */
  147.       type = TREE_TYPE (type);
  148.       parmtype = TREE_TYPE (parmtype);
  149.     }
  150.  
  151.   if (coder == UNKNOWN_TYPE)
  152.     {
  153.       if (codel == FUNCTION_TYPE
  154.       || codel == METHOD_TYPE
  155.       || (codel == POINTER_TYPE
  156.           && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
  157.           || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)))
  158.     return 0;
  159.       return 1;
  160.     }
  161.  
  162.   if (coder == VOID_TYPE)
  163.     return 1;
  164.  
  165.   if (codel == ENUMERAL_TYPE || codel == INTEGER_TYPE)
  166.     {
  167.       /* Control equivalence of ints an enums.  */
  168.  
  169.       if (codel == ENUMERAL_TYPE
  170.       && flag_int_enum_equivalence == 0)
  171.     {
  172.       /* Enums can be converted to ints, but not vice-versa.  */
  173.       if (coder != ENUMERAL_TYPE
  174.           || TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (parmtype))
  175.         return 1;
  176.     }
  177.  
  178.       /* else enums and ints (almost) freely interconvert.  */
  179.  
  180.       if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
  181.     {
  182.       int easy = TREE_UNSIGNED (type) ^ TREE_UNSIGNED (parmtype);
  183.       if (codel != coder)
  184.         easy += 1;
  185.       if (TYPE_MODE (type) != TYPE_MODE (parmtype))
  186.         easy += 2;
  187.       return (easy << 4);
  188.     }
  189.       else if (coder == REAL_TYPE)
  190.     return (4<<4);
  191.     }
  192.  
  193.   if (codel == REAL_TYPE)
  194.     if (coder == REAL_TYPE)
  195.       /* Shun converting between float and double if a choice exists.  */
  196.       {
  197.     if (TYPE_MODE (type) != TYPE_MODE (parmtype))
  198.       return (2<<4);
  199.     return 0;
  200.       }
  201.     else if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
  202.       return (4<<4);
  203.  
  204.   /* convert arrays which have not previously been converted.  */
  205.   if (codel == ARRAY_TYPE)
  206.     codel = POINTER_TYPE;
  207.   if (coder == ARRAY_TYPE)
  208.     coder = POINTER_TYPE;
  209.  
  210.   /* Conversions among pointers */
  211.   if (codel == POINTER_TYPE && coder == POINTER_TYPE)
  212.     {
  213.       register tree ttl = TYPE_MAIN_VARIANT (TREE_TYPE (type));
  214.       register tree ttr = TYPE_MAIN_VARIANT (TREE_TYPE (parmtype));
  215.       int penalty = 4 * (ttl != ttr);
  216.       /* Anything converts to void *.  void * converts to anything.
  217.      Since these may be `const void *' (etc.) use VOID_TYPE
  218.      instead of void_type_node.
  219.      Otherwise, the targets must be the same,
  220.      except that we do allow (at some cost) conversion
  221.      between signed and unsinged pointer types.  */
  222.  
  223.       if ((TREE_CODE (ttl) == METHOD_TYPE
  224.        || TREE_CODE (ttl) == FUNCTION_TYPE)
  225.       && TREE_CODE (ttl) == TREE_CODE (ttr))
  226.     {
  227.       if (comptypes (ttl, ttr, -1))
  228.         return penalty<<4;
  229.       return 1;
  230.     }
  231.  
  232.       if (!(TREE_CODE (ttl) == VOID_TYPE
  233.         || TREE_CODE (ttr) == VOID_TYPE
  234.         || (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (ttr)
  235.         && (ttl = unsigned_type (ttl),
  236.             ttr = unsigned_type (ttr),
  237.             penalty = 10, 0))
  238.         || (comp_target_types (ttl, ttr, 0))))
  239.     return 1;
  240.  
  241.       if (ttr == ttl)
  242.     return 4;
  243.  
  244.       if (IS_AGGR_TYPE_2 (ttl, ttr))
  245.     {
  246.       int b_or_d = get_base_distance (ttl, ttr, 0, 0);
  247.       if (b_or_d < 0)
  248.         return 1;
  249.       return (b_or_d<<3) | 4;
  250.     }
  251.  
  252.       return (penalty<<4);
  253.     }
  254.  
  255.   if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
  256.     {
  257.       /* This is not a bad match, but don't let it beat
  258.      integer-enum combinations.  */
  259.       if (parm && integer_zerop (parm))
  260.     return (4<<4);
  261.     }
  262.  
  263.   /* C++: one of the types must be a reference type.  */
  264.   {
  265.     tree ttl, ttr;
  266.     register tree intype = TYPE_MAIN_VARIANT (parmtype);
  267.     register enum tree_code form = TREE_CODE (intype);
  268.     int penalty;
  269.  
  270.     if (codel == REFERENCE_TYPE || coder == REFERENCE_TYPE)
  271.       {
  272.     ttl = TYPE_MAIN_VARIANT (type);
  273.  
  274.     if (codel == REFERENCE_TYPE)
  275.       {
  276.         ttl = TYPE_MAIN_VARIANT (TREE_TYPE (ttl));
  277.  
  278.         if (form == OFFSET_TYPE)
  279.           {
  280.         intype = TREE_TYPE (intype);
  281.         form = TREE_CODE (intype);
  282.           }
  283.  
  284.         if (form == REFERENCE_TYPE)
  285.           {
  286.         intype = TYPE_MAIN_VARIANT (TREE_TYPE (intype));
  287.  
  288.         if (ttl == intype)
  289.           return 0;
  290.         penalty = 2;
  291.           }
  292.         else
  293.           {
  294.         /* Can reference be built up?  */
  295.         if (ttl == intype)
  296.           {
  297.             return 0;
  298.           }
  299.         else
  300.           penalty = 2;
  301.           }
  302.       }
  303.     else if (form == REFERENCE_TYPE)
  304.       {
  305.         if (parm)
  306.           {
  307.         tree tmp = convert_from_reference (parm);
  308.         intype = TYPE_MAIN_VARIANT (TREE_TYPE (tmp));
  309.           }
  310.         else
  311.           {
  312.         intype = parmtype;
  313.         do
  314.           {
  315.             intype = TREE_TYPE (intype);
  316.           }
  317.         while (TREE_CODE (intype) == REFERENCE_TYPE);
  318.         intype = TYPE_MAIN_VARIANT (intype);
  319.           }
  320.  
  321.         if (ttl == intype)
  322.           return 0;
  323.         else
  324.           penalty = 2;
  325.       }
  326.  
  327.     if (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (intype))
  328.       {
  329.         ttl = unsigned_type (ttl);
  330.         intype = unsigned_type (intype);
  331.         penalty += 2;
  332.       }
  333.  
  334.     ttr = intype;
  335.  
  336.     /* If the initializer is not an lvalue, then it does not
  337.        matter if we make life easier for the programmer
  338.        by creating a temporary variable with which to
  339.        hold the result.  */
  340.     if (parm && (coder == INTEGER_TYPE
  341.              || coder == ENUMERAL_TYPE
  342.              || coder == REAL_TYPE)
  343.         && ! lvalue_p (parm))
  344.       return (convert_harshness (ttl, ttr, 0) | (penalty << 4));
  345.  
  346.     if (ttl == ttr)
  347.       return 4;
  348.  
  349.     /* Pointers to voids always convert for pointers.  But
  350.        make them less natural than more specific matches.  */
  351.     if (TREE_CODE (ttl) == POINTER_TYPE && TREE_CODE (ttr) == POINTER_TYPE)
  352.       if (TREE_TYPE (ttl) == void_type_node
  353.           || TREE_TYPE (ttr) == void_type_node)
  354.         return ((penalty+1)<<4);
  355.  
  356.     if (parm && codel != REFERENCE_TYPE)
  357.       return (convert_harshness (ttl, ttr, 0) | (penalty << 4));
  358.  
  359.     /* Here it does matter.  If this conversion is from
  360.        derived to base, allow it.  Otherwise, types must
  361.        be compatible in the strong sense.  */
  362.     if (IS_AGGR_TYPE_2 (ttl, ttr))
  363.       {
  364.         int b_or_d = get_base_distance (ttl, ttr, 0, 0);
  365.         if (b_or_d < 0)
  366.           return 1;
  367. #if AMBIGUOUS_WORKING
  368.         if (ttl == TYPE_MAIN_VARIANT (type)
  369.         && TYPE_GETS_INIT_REF (type))
  370.           return (b_or_d<<3) | 6;
  371. #endif
  372.         return (b_or_d<<3) | 4;
  373.       }
  374.  
  375.     if (comp_target_types (ttl, intype, 1))
  376.       return (penalty<<4);
  377.       }
  378.   }
  379.   if (codel == RECORD_TYPE && coder == RECORD_TYPE)
  380.     {
  381.       int b_or_d = get_base_distance (type, parmtype, 0, 0);
  382.       if (b_or_d < 0)
  383.     return 1;
  384. #if AMBIGUOUS_WORKING
  385.       if (TYPE_GETS_INIT_REF (type))
  386.     return (b_or_d<<3) | 6;
  387. #endif
  388.       return (b_or_d<<3) | 4;
  389.     }
  390.   return 1;
  391. }
  392.  
  393. /* Algorithm: Start out with no stikes against.  For each argument
  394.    which requires a (subjective) hard conversion (such as between
  395.    floating point and integer), issue a strike.  If there are the same
  396.    number of formal and actual parameters in the list, there will be at
  397.    least on strike, otherwise an exact match would have been found.  If
  398.    there are not the same number of arguments in the type lists, we are
  399.    not dead yet: a `...' means that we can have more parms then were
  400.    declared, and if we wind up in the default argument section of the
  401.    list those can be used as well.  If an exact match could be found for
  402.    one of those cases, return it immediately.  Otherwise, Rank the fields
  403.    so that fields with fewer strikes are tried first.
  404.  
  405.    Conversions between builtin and user-defined types are allowed, but
  406.    no function involving such a conversion is prefered to one which
  407.    does not require such a conversion.  Furthermore, such conversions
  408.    must be unique.  */
  409.  
  410. void
  411. compute_conversion_costs (function, tta_in, cp, arglen)
  412.      tree function;
  413.      tree tta_in;
  414.      struct candidate *cp;
  415.      int arglen;
  416. {
  417.   tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
  418.   tree ttf = ttf_in;
  419.   tree tta = tta_in;
  420.  
  421.   /* Start out with no strikes against.  */
  422.   int evil_strikes = 0;
  423.   int user_strikes = 0;
  424.   int b_or_d_strikes = 0;
  425.   int easy_strikes = 0;
  426.  
  427.   int strike_index = 0, win, lose;
  428.  
  429. #ifdef GATHER_STATISTICS
  430.   n_compute_conversion_costs++;
  431. #endif
  432.  
  433.   cp->function = function;
  434.   cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
  435.   cp->u.bad_arg = 0;        /* optimistic!  */
  436.  
  437.   bzero (cp->harshness, (arglen+1) * sizeof (short));
  438.  
  439.   while (ttf && tta)
  440.     {
  441.       int harshness;
  442.  
  443.       if (ttf == void_list_node)
  444.     break;
  445.  
  446.       if (type_unknown_p (TREE_VALUE (tta)))
  447.     {      
  448.       /* Must perform some instantiation here.  */
  449.       tree rhs = TREE_VALUE (tta);
  450.       tree lhstype = TREE_VALUE (ttf);
  451.  
  452.       /* @@ This is to undo what `grokdeclarator' does to
  453.          parameter types.  It really should go through
  454.          something more general.  */
  455.  
  456.       TREE_TYPE (tta) = unknown_type_node;
  457.       if (TREE_CODE (rhs) == OP_IDENTIFIER)
  458.         rhs = build_instantiated_decl (lhstype, rhs);
  459.       else
  460.         {
  461.           /* Keep quiet about possible contravariance violations.  */
  462.           extern int inhibit_warnings;
  463.           int old_inhibit_warnings = inhibit_warnings;
  464.           inhibit_warnings = 1;
  465.  
  466.           rhs = instantiate_type (lhstype, rhs, 0);
  467.  
  468.           inhibit_warnings = old_inhibit_warnings;
  469.         }
  470.  
  471.       if (TREE_CODE (rhs) == ERROR_MARK)
  472.         harshness = 1;
  473.       else
  474.         {
  475.           harshness = convert_harshness (lhstype, TREE_TYPE (rhs), rhs);
  476.           /* harshness |= 2; */
  477.         }
  478.     }
  479.       else
  480.     harshness = convert_harshness (TREE_VALUE (ttf), TREE_TYPE (TREE_VALUE (tta)), TREE_VALUE (tta));
  481.  
  482.       cp->harshness[strike_index] = harshness;
  483.       if (harshness & 1)
  484.     {
  485.       cp->u.bad_arg = strike_index;
  486.       evil_strikes = 1;
  487.     }
  488.       else if (harshness & 2)
  489.     {
  490.       user_strikes += 1;
  491.     }
  492.       else if (harshness & 4)
  493.     {
  494.       b_or_d_strikes += (harshness >> 3);
  495.     }
  496.       else
  497.     easy_strikes += harshness >> 4;
  498.       ttf = TREE_CHAIN (ttf);
  499.       tta = TREE_CHAIN (tta);
  500.       strike_index += 1;
  501.     }
  502.  
  503.   if (tta)
  504.     {
  505.       /* ran out of formals, and parmlist is fixed size.  */
  506.       if (ttf /* == void_type_node */)
  507.     {
  508.       cp->evil = 1;
  509.       cp->u.bad_arg = -1;
  510.       return;
  511.     }
  512.     }
  513.   else if (ttf && ttf != void_list_node)
  514.     {
  515.       /* ran out of actuals, and no defaults.  */
  516.       if (TREE_PURPOSE (ttf) == NULL_TREE)
  517.     {
  518.       cp->evil = 1;
  519.       cp->u.bad_arg = -2;
  520.       return;
  521.     }
  522.       /* Store index of first default.  */
  523.       cp->harshness[arglen] = strike_index+1;
  524.     }
  525.   else cp->harshness[arglen] = 0;
  526.  
  527.   /* Argument list lengths work out, so don't need to check them again.  */
  528.   if (evil_strikes)
  529.     {
  530.       /* We do not check for derived->base conversions here, since in
  531.      no case would they give evil strike counts, unless such conversions
  532.      are somehow ambiguous.  */
  533.  
  534.       /* See if any user-defined conversions apply.
  535.          But make sure that we do not loop.  */
  536.       static int dont_convert_types = 0;
  537.  
  538.       if (dont_convert_types)
  539.     {
  540.       cp->evil = 1;
  541.       return;
  542.     }
  543.  
  544.       win = 0;            /* Only get one chance to win.  */
  545.       ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
  546.       tta = tta_in;
  547.       strike_index = 0;
  548.       evil_strikes = 0;
  549.  
  550.       while (ttf && tta)
  551.     {
  552.       if (ttf == void_list_node)
  553.         break;
  554.  
  555.       lose = cp->harshness[strike_index];
  556.       if (lose&1)
  557.         {
  558.           tree actual_type = TREE_TYPE (TREE_VALUE (tta));
  559.           tree formal_type = TREE_VALUE (ttf);
  560.  
  561.           dont_convert_types = 1;
  562.  
  563.           if (TREE_CODE (formal_type) == REFERENCE_TYPE)
  564.         formal_type = TREE_TYPE (formal_type);
  565.           if (TREE_CODE (actual_type) == REFERENCE_TYPE)
  566.         actual_type = TREE_TYPE (actual_type);
  567.  
  568.           if (formal_type != error_mark_node
  569.           && actual_type != error_mark_node)
  570.         {
  571.           formal_type = TYPE_MAIN_VARIANT (formal_type);
  572.           actual_type = TYPE_MAIN_VARIANT (actual_type);
  573.  
  574.           if (TYPE_HAS_CONSTRUCTOR (formal_type))
  575.             {
  576.               /* If it has a constructor for this type, try to use it.  */
  577.               if (convert_to_aggr (formal_type, TREE_VALUE (tta), 0, 1)
  578.               != error_mark_node)
  579.             {
  580.               /* @@ There is no way to save this result yet.
  581.                  @@ So success is NULL_TREE for now.  */
  582.               win++;
  583.             }
  584.             }
  585.           if (TYPE_LANG_SPECIFIC (actual_type) && TYPE_HAS_CONVERSION (actual_type))
  586.             {
  587.               if (TREE_CODE (formal_type) == INTEGER_TYPE
  588.               && TYPE_HAS_INT_CONVERSION (actual_type))
  589.             win++;
  590.               else if (TREE_CODE (formal_type) == REAL_TYPE
  591.                    && TYPE_HAS_REAL_CONVERSION (actual_type))
  592.             win++;
  593.               else
  594.             {
  595.               tree conv = build_type_conversion (CALL_EXPR, TREE_VALUE (ttf), TREE_VALUE (tta), 0);
  596.               if (conv)
  597.                 {
  598.                   if (conv == error_mark_node)
  599.                 win += 2;
  600.                   else
  601.                 win++;
  602.                 }
  603.               else if (TREE_CODE (TREE_VALUE (ttf)) == REFERENCE_TYPE)
  604.                 {
  605.                   conv = build_type_conversion (CALL_EXPR, formal_type, TREE_VALUE (tta), 0);
  606.                   if (conv)
  607.                 {
  608.                   if (conv == error_mark_node)
  609.                     win += 2;
  610.                   else
  611.                     win++;
  612.                 }
  613.                 }
  614.             }
  615.             }
  616.         }
  617.           dont_convert_types = 0;
  618.  
  619.           if (win == 1)
  620.         {
  621.           user_strikes += 1;
  622.           cp->harshness[strike_index] = 2;
  623.           win = 0;
  624.         }
  625.           else
  626.         {
  627.           if (cp->u.bad_arg > strike_index)
  628.             cp->u.bad_arg = strike_index;
  629.  
  630.           evil_strikes = win ? 2 : 1;
  631.           break;
  632.         }
  633.         }
  634.  
  635.       ttf = TREE_CHAIN (ttf);
  636.       tta = TREE_CHAIN (tta);
  637.       strike_index += 1;
  638.     }
  639.     }
  640.  
  641.   /* Calling a non-const member function from a const member function
  642.      is probably invalid, but for now we let it only draw a warning.
  643.      We indicate that such a mismatch has occured by setting the
  644.      harshness to a maximum value.  */
  645.   if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE
  646.       && TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in))) == POINTER_TYPE
  647.       && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in))))
  648.       > TYPE_READONLY (TREE_TYPE (TREE_VALUE (ttf_in)))))
  649.     cp->harshness[0] |= 128;
  650.  
  651.   cp->evil = evil_strikes;
  652.   cp->user = user_strikes;
  653.   cp->b_or_d = b_or_d_strikes;
  654.   cp->easy = easy_strikes;
  655. }
  656.  
  657. struct candidate *
  658. ideal_candidate (basetype, candidates, n_candidates, parms, len)
  659.      tree basetype;
  660.      struct candidate *candidates;
  661.      int n_candidates;
  662.      tree parms;
  663.      int len;
  664. {
  665.   struct candidate *cp = candidates + n_candidates;
  666.   int index, i;
  667.   tree ttf;
  668.  
  669.   qsort (candidates,        /* char *base */
  670.      n_candidates,        /* int nel */
  671.      sizeof (struct candidate), /* int width */
  672.      rank_for_overload);    /* int (*compar)() */
  673.  
  674.   /* If the best candidate requires user-defined conversions,
  675.      and its user-defined conversions are a strict subset
  676.      of all other candidates requiring user-defined conversions,
  677.      then it is, in fact, the best.  */
  678.   for (i = -1; cp + i != candidates; i--)
  679.     if (cp[i].user == 0)
  680.       break;
  681.  
  682.   if (i < -1)
  683.     {
  684.       tree ttf0;
  685.  
  686.       /* Check that every other candidate requires those conversions
  687.      as a strict subset of their conversions.  */
  688.       if (cp[i].user == cp[-1].user)
  689.     goto non_subset;
  690.  
  691.       /* Look at subset relationship more closely.  */
  692.       while (i != -1)
  693.     {
  694.       for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[i].function)),
  695.            ttf0 = TYPE_ARG_TYPES (TREE_TYPE (cp[-1].function)),
  696.            index = 0;
  697.            index < len;
  698.            ttf = TREE_CHAIN (ttf), ttf0 = TREE_CHAIN (ttf0), index++)
  699.         if (cp[i].harshness[index] & 2)
  700.           {
  701.         /* If our "best" candidate also needs a conversion,
  702.            it must be the same one.  */
  703.         if ((cp[-1].harshness[index] & 2)
  704.             && TREE_VALUE (ttf) != TREE_VALUE (ttf0))
  705.           goto non_subset;
  706.           }
  707.       i++;
  708.     }
  709.       /* The best was the best.  */
  710.       return cp - 1;
  711.     non_subset:
  712.       /* Use other rules for determining "bestness".  */
  713.       ;
  714.     }
  715.  
  716.   /* If the best two candidates we find require user-defined
  717.      conversions, we may need to report and error message.  */
  718.   if (cp[-1].user && cp[-2].user
  719.       && (cp[-1].b_or_d || cp[-2].b_or_d == 0))
  720.     {
  721.       /* If the best two methods found involved user-defined
  722.      type conversions, then we must see whether one
  723.      of them is exactly what we wanted.  If not, then
  724.      we have an ambiguity.  */
  725.       int best = 0;
  726.       tree tta = parms;
  727.       tree f1, p1;
  728.  
  729. #if AMBIGUOUS_WORKING
  730.       if (cp[-1].b_or_d == 0
  731.       && cp[-1].easy == 0
  732.       && (cp[-2].b_or_d | cp[-2].easy) > 0)
  733.     return cp - 1;
  734. #endif
  735.  
  736.       /* Stash all of our parameters in safe places
  737.      so that we can perform type conversions in place.  */
  738.       while (tta)
  739.     {
  740.       TREE_PURPOSE (tta) = TREE_VALUE (tta);
  741.       tta = TREE_CHAIN (tta);
  742.     }
  743.  
  744.       i = 0;
  745.       do
  746.     {
  747.       int exact_conversions = 0;
  748.  
  749.       i -= 1;
  750.       tta = parms;
  751.       if (DECL_STATIC_FUNCTION_P (cp[i].function))
  752.         tta = TREE_CHAIN (tta);
  753.       for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[i].function)), index = 0;
  754.            index < len;
  755.            tta = TREE_CHAIN (tta), ttf = TREE_CHAIN (ttf), index++)
  756.         {
  757.           if (cp[i].harshness[index] & 2)
  758.         {
  759.           TREE_VALUE (tta)
  760.             = build_type_conversion (CALL_EXPR, TREE_VALUE (ttf), TREE_PURPOSE (tta), 2);
  761.           if (TREE_VALUE (tta))
  762.             {
  763.               if (TREE_CODE (TREE_VALUE (tta)) != CONVERT_EXPR
  764.               && (TREE_CODE (TREE_VALUE (tta)) != NOP_EXPR
  765.                   || comp_target_types (TREE_TYPE (TREE_VALUE (tta)),
  766.                             TREE_TYPE (TREE_OPERAND (TREE_VALUE (tta), 0)), 1)))
  767.             exact_conversions += 1;
  768.             }
  769.           else if (PROMOTES_TO_AGGR_TYPE (TREE_VALUE (ttf), REFERENCE_TYPE))
  770.             {
  771.               /* To get here we had to have succeeded via
  772.              a constructor.  */
  773.               TREE_VALUE (tta) = TREE_PURPOSE (tta);
  774.               exact_conversions += 1;
  775.             }
  776.         }
  777.         }
  778.       if (exact_conversions == cp[i].user)
  779.         {
  780.           if (best == 0)
  781.         {
  782.           best = i;
  783.           f1 = cp[best].function;
  784.           p1 = TYPE_ARG_TYPES (TREE_TYPE (f1));
  785.         }
  786.           else
  787.         {
  788.           /* Don't complain if next best is from base class.  */
  789.           tree f2 = cp[i].function;
  790.           tree p2 = TYPE_ARG_TYPES (TREE_TYPE (f2));
  791.  
  792.           if (TREE_CODE (TREE_TYPE (f1)) == METHOD_TYPE
  793.               && TREE_CODE (TREE_TYPE (f2)) == METHOD_TYPE
  794.               && (cp[i].harshness[0] & 4) != 0
  795.               && cp[best].harshness[0] < cp[i].harshness[0])
  796.             {
  797. #if 0
  798.               /* For LUCID.  */
  799.               if (! compparms (TREE_CHAIN (p1), TREE_CHAIN (p2), 1))
  800.             goto ret0;
  801.               else
  802. #endif
  803.             continue;
  804.             }
  805.           else goto ret0;
  806.         }
  807.         }
  808.     } while (cp + i != candidates);
  809.  
  810.       if (best)
  811.     {
  812.       int exact_conversions = cp[best].user;
  813.       tta = parms;
  814.       if (DECL_STATIC_FUNCTION_P (cp[best].function))
  815.         tta = TREE_CHAIN (parms);
  816.       for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[best].function)), index = 0;
  817.            exact_conversions > 0;
  818.            tta = TREE_CHAIN (tta), ttf = TREE_CHAIN (ttf), index++)
  819.         {
  820.           if (cp[best].harshness[index] & 2)
  821.         {
  822.           /* We must now fill in the slot we left behind.
  823.              @@ This could be optimized to use the value previously
  824.              @@ computed by build_type_conversion in some cases.  */
  825.           TREE_VALUE (tta) = convert (TREE_VALUE (ttf), TREE_PURPOSE (tta));
  826.           exact_conversions -= 1;
  827.         }
  828.           else TREE_VALUE (tta) = TREE_PURPOSE (tta);
  829.         }
  830.       return cp + best;
  831.     }
  832.       goto ret0;
  833.     }
  834.   /* If the best two candidates we find both use default parameters,
  835.      we may need to report and error.  Don't need to worry if next-best
  836.      candidate is forced to use user-defined conversion when best is not.  */
  837.   if (cp[-2].user == 0
  838.       && cp[-1].harshness[len] != 0 && cp[-2].harshness[len] != 0)
  839.     {
  840.       tree tt1 = TYPE_ARG_TYPES (TREE_TYPE (cp[-1].function));
  841.       tree tt2 = TYPE_ARG_TYPES (TREE_TYPE (cp[-2].function));
  842.       int i = cp[-1].harshness[len];
  843.       if (cp[-2].harshness[len] < i)
  844.     i = cp[-2].harshness[len];
  845.       while (--i > 0)
  846.     {
  847.       if (TYPE_MAIN_VARIANT (TREE_VALUE (tt1))
  848.           != TYPE_MAIN_VARIANT (TREE_VALUE (tt2)))
  849.         /* These lists are not identical, so we can choose our best candidate.  */
  850.         return cp - 1;
  851.       tt1 = TREE_CHAIN (tt1);
  852.       tt2 = TREE_CHAIN (tt2);
  853.     }
  854.       /* To get here, both lists had the same parameters up to the defaults
  855.      which were used.  This is an ambiguous request.  */
  856.       goto ret0;
  857.     }
  858.  
  859.   /* Otherwise, return our best candidate.  Note that if we get candidates
  860.      from independent base classes, we have an ambiguity, even if one
  861.      argument list look a little better than another one.  */
  862.   if (cp[-1].b_or_d && basetype && TYPE_USES_MULTIPLE_INHERITANCE (basetype))
  863.     {
  864.       int i = n_candidates - 1, best;
  865.       tree base1 = NULL_TREE;
  866.  
  867.       if (TREE_CODE (TREE_TYPE (candidates[i].function)) == FUNCTION_TYPE)
  868.     return cp - 1;
  869.  
  870.       for (; i >= 0 && candidates[i].user == 0 && candidates[i].evil == 0; i--)
  871.     {
  872.       if (TREE_CODE (TREE_TYPE (candidates[i].function)) == METHOD_TYPE)
  873.         {
  874.           tree newbase = TYPE_METHOD_BASETYPE (TREE_TYPE (candidates[i].function));
  875.  
  876.           if (base1 != NULL_TREE)
  877.         {
  878.           if (newbase != base1
  879.               && ! get_base_type (newbase, base1, 0))
  880.             {
  881.               char *buf = (char *)alloca (8192);
  882.               error ("ambiguous request for function from distinct base classes of type `%s'", TYPE_NAME_STRING (basetype));
  883.               error ("first candidate is `%s'", fndecl_as_string (buf, 0, candidates[best].function, 1));
  884.               error ("second candidates is `%s'", fndecl_as_string (buf, 0, candidates[i].function, 1));
  885.               return cp - 1;
  886.             }
  887.         }
  888.           else
  889.         {
  890.           best = i;
  891.           base1 = newbase;
  892.         }
  893.         }
  894.       else return cp - 1;
  895.     }
  896.     }
  897.  
  898. #if AMBIGUOUS_WORKING
  899.   if (cp[-1].user == cp[-2].user
  900.       && cp[-1].b_or_d == cp[-2].b_or_d
  901.       && cp[-1].easy == cp[-2].easy)
  902.     goto ret0;
  903. #endif
  904.  
  905.   return cp - 1;
  906.  
  907.  ret0:
  908.   /* In the case where there is no ideal candidate, restore
  909.      TREE_VALUE slots of PARMS from TREE_PURPOSE slots.  */
  910.   while (parms)
  911.     {
  912.       TREE_VALUE (parms) = TREE_PURPOSE (parms);
  913.       parms = TREE_CHAIN (parms);
  914.     }
  915.   return 0;
  916. }
  917.  
  918. /* Assume that if the class referred to is not in the
  919.    current class hierarchy, that it may be remote.
  920.    PARENT is assumed to be of aggregate type here.  */
  921. static int
  922. may_be_remote (parent)
  923.      tree parent;
  924. {
  925.   if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent) == 0)
  926.     return 0;
  927.  
  928.   if (current_class_type == NULL_TREE)
  929.     return 0;
  930.   if (parent == current_class_type)
  931.     return 0;
  932.  
  933.   if (get_base_type (parent, current_class_type, 0))
  934.     return 0;
  935.   return 1;
  936. }
  937.  
  938. #ifdef ESKIT
  939. /* Return the number of bytes that the arglist in PARMS would
  940.    occupy on the stack.  */
  941. int
  942. get_arglist_len_in_bytes (parms)
  943.      tree parms;
  944. {
  945.   register tree parm;
  946.   register int bytecount = 0;
  947.  
  948.   for (parm = parms; parm; parm = TREE_CHAIN (parm))
  949.     {
  950.       register tree pval = TREE_VALUE (parm);
  951.       register int used, size;
  952.  
  953.       if (TREE_CODE (pval) == ERROR_MARK)
  954.     continue;
  955.       else if (TYPE_MODE (TREE_TYPE (pval)) != BLKmode)
  956.     {
  957.       used = size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (pval)));
  958. #ifdef PUSH_ROUNDING
  959.       size = PUSH_ROUNDING (size);
  960. #endif
  961.       used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
  962.            / (PARM_BOUNDARY / BITS_PER_UNIT))
  963.           * (PARM_BOUNDARY / BITS_PER_UNIT));
  964.     }
  965.       else
  966.     {
  967.       register tree size = size_in_bytes (TREE_TYPE (pval));
  968.       register tree used_t
  969.         = round_up (size, PARM_BOUNDARY / BITS_PER_UNIT);
  970.       used = TREE_INT_CST_LOW (used_t);
  971.     }
  972.       bytecount += used;
  973.     }
  974.   return bytecount;
  975. }
  976. #endif
  977.  
  978. tree
  979. build_vfield_ref (datum, type)
  980.      tree datum, type;
  981. {
  982.   tree rval;
  983.   extern int flag_assume_nonnull_objects;
  984.   int old_assume_nonnull_objects = flag_assume_nonnull_objects;
  985.  
  986.   /* Vtable references are always made from non-null objects.  */
  987.   flag_assume_nonnull_objects = 1;
  988.   if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
  989.     datum = convert_from_reference (datum);
  990.  
  991.   if (! TYPE_USES_VIRTUAL_BASECLASSES (type))
  992.     rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
  993.           datum, CLASSTYPE_VFIELD (type));
  994.   else
  995.     rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), 0, 0);
  996.   flag_assume_nonnull_objects = old_assume_nonnull_objects;
  997.  
  998.   return rval;
  999. }
  1000.  
  1001. /* Build a call to a member of an object.  I.e., one that overloads
  1002.    operator ()(), or is a pointer-to-function or pointer-to-method.  */
  1003. static tree
  1004. build_field_call (basetype_path, instance_ptr, name, parms, err_name)
  1005.      tree basetype_path;
  1006.      tree instance_ptr, name, parms;
  1007.      char *err_name;
  1008. {
  1009.   tree field, instance;
  1010.  
  1011.   if (instance_ptr == current_class_decl)
  1012.     {
  1013.       /* Check to see if we really have a reference to an instance variable
  1014.      with `operator()()' overloaded.  */
  1015. #if 1
  1016.       field = IDENTIFIER_CLASS_VALUE (name);
  1017. #else
  1018.       field = identifier_class_value (name);
  1019. #endif
  1020.  
  1021.       if (field == NULL_TREE)
  1022.     {
  1023.       error ("`this' has no member named `%s'", err_name);
  1024.       return error_mark_node;
  1025.     }
  1026.  
  1027.       if (TREE_CODE (field) == FIELD_DECL)
  1028.     {
  1029.       /* If it's a field, try overloading operator (),
  1030.          or calling if the field is a pointer-to-function.  */
  1031.       instance = build_component_ref_1 (C_C_D, field, 0, 1);
  1032.       if (instance == error_mark_node)
  1033.         return error_mark_node;
  1034.  
  1035.       if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
  1036.           && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance)))
  1037.         return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms);
  1038.  
  1039.       if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
  1040.         if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
  1041.           return build_function_call (instance, parms);
  1042.         else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
  1043.           return build_function_call (instance, tree_cons (NULL_TREE, current_class_decl, parms));
  1044.     }
  1045.       return NULL_TREE;
  1046.     }
  1047.  
  1048.   /* Check to see if this is not really a reference to an instance variable
  1049.      with `operator()()' overloaded.  */
  1050.   field = lookup_field (basetype_path, name, 1);
  1051.  
  1052.   /* This can happen if the reference was ambiguous
  1053.      or for visibility violations.  */
  1054.   if (field == error_mark_node)
  1055.     return error_mark_node;
  1056.   if (field)
  1057.     {
  1058.       tree basetype;
  1059.       tree ftype = TREE_TYPE (field);
  1060.  
  1061.       if (TYPE_LANG_SPECIFIC (ftype) && TYPE_OVERLOADS_CALL_EXPR (ftype))
  1062.     {
  1063.       /* Make the next search for this field very short.  */
  1064.       basetype = DECL_FIELD_CONTEXT (field);
  1065.       instance_ptr = convert_pointer_to (basetype, instance_ptr);
  1066.  
  1067.       instance = build_indirect_ref (instance_ptr, 0);
  1068.       return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
  1069.                  build_component_ref_1 (instance, field, 0, 0),
  1070.                  parms);
  1071.     }
  1072.       if (TREE_CODE (ftype) == POINTER_TYPE)
  1073.     {
  1074.       if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
  1075.           || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
  1076.         {
  1077.           /* This is a member which is a pointer to function.  */
  1078.           tree ref = build_component_ref_1 (build_indirect_ref (instance_ptr, 0, 0),
  1079.                         field, LOOKUP_COMPLAIN);
  1080.           if (ref == error_mark_node)
  1081.         return error_mark_node;
  1082.           return build_function_call (ref, parms);
  1083.         }
  1084.     }
  1085.       else if (TREE_CODE (ftype) == METHOD_TYPE)
  1086.     {
  1087.       error ("invalid call via pointer-to-member function");
  1088.       return error_mark_node;
  1089.     }
  1090.       else
  1091.     return NULL_TREE;
  1092.     }
  1093.   return NULL_TREE;
  1094. }
  1095.  
  1096. /* Resolve an expression NAME1::NAME2::...::NAMEn to
  1097.    the name that names the above nested type.  INNER_TYPES
  1098.    is a chain of nested type names (held together by SCOPE_REFs);
  1099.    OUTER_TYPE is the type we know to enclose INNER_TYPES.
  1100.    Returns NULL_TREE if there is an error.  */
  1101. tree
  1102. resolve_scope_to_name (outer_type, inner_types)
  1103.      tree outer_type, inner_types;
  1104. {
  1105.   tree tags;
  1106.   tree inner_name;
  1107.  
  1108.   if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
  1109.     {
  1110.       /* We first try to look for a nesting in our current class context.  */
  1111.       tree rval = resolve_scope_to_name (current_class_type, inner_types);
  1112.       if (rval != NULL_TREE)
  1113.     return rval;
  1114.     }
  1115.  
  1116.   if (TREE_CODE (inner_types) == SCOPE_REF)
  1117.     {
  1118.       inner_name = TREE_OPERAND (inner_types, 0);
  1119.       inner_types = TREE_OPERAND (inner_types, 1);
  1120.     }
  1121.   else
  1122.     {
  1123.       inner_name = inner_types;
  1124.       inner_types = 0;
  1125.     }
  1126.  
  1127.   if (outer_type == NULL_TREE)
  1128.     {
  1129.       /* If we have something that's already a type by itself,
  1130.      use that.  */
  1131.       if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
  1132.     {
  1133.       if (inner_types)
  1134.         return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
  1135.                       inner_types);
  1136.       return inner_name;
  1137.     }
  1138.       return NULL_TREE;
  1139.     }
  1140.  
  1141.   if (! IS_AGGR_TYPE (outer_type))
  1142.     return NULL_TREE;
  1143.  
  1144.   /* Look for member classes.  */
  1145.   tags = CLASSTYPE_TAGS (outer_type);
  1146.  
  1147.   while (tags)
  1148.     {
  1149.       if (TREE_PURPOSE (tags) == inner_name)
  1150.     {
  1151.       if (inner_types == NULL_TREE)
  1152.         return DECL_NESTED_TYPENAME (TYPE_NAME (TREE_VALUE (tags)));
  1153.       return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
  1154.     }
  1155.       tags = TREE_CHAIN (tags);
  1156.     }
  1157.  
  1158.   /* Look for a TYPE_DECL.  */
  1159.   for (tags = TYPE_FIELDS (outer_type); tags; tags = TREE_CHAIN (tags))
  1160.     if (TREE_CODE (tags) == TYPE_DECL
  1161.     && DECL_NAME (tags) == inner_name)
  1162.       {
  1163. #if 0
  1164.     /* Code by tiemann.  */
  1165.     tree type = TREE_TYPE (tags);
  1166.     /* With luck, this will name a visible type.  */
  1167.     inner_name = TYPE_NAME (type);
  1168.     if (TREE_CODE (inner_name) == TYPE_DECL)
  1169.       inner_name = DECL_NAME (inner_name);
  1170.     return inner_name;
  1171. #else
  1172.     /* Code by raeburn.  */
  1173.     if (inner_types == NULL_TREE)
  1174.       return DECL_NESTED_TYPENAME (tags);
  1175.     return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
  1176. #endif
  1177.       }
  1178.  
  1179.   return NULL_TREE;
  1180. }
  1181.  
  1182. /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
  1183.    This is how virtual function calls are avoided.  */
  1184. tree
  1185. build_scoped_method_call (exp, scopes, name, parms)
  1186.      tree exp;
  1187.      tree scopes;
  1188.      tree name;
  1189.      tree parms;
  1190. {
  1191.   /* Because this syntactic form does not allow
  1192.      a pointer to a base class to be `stolen',
  1193.      we need not protect the drived->base conversion
  1194.      that happens here.
  1195.      
  1196.      @@ But we do have to check visibility privileges later.  */
  1197.   tree basename = resolve_scope_to_name (NULL_TREE, scopes);
  1198.   tree basetype, decl;
  1199.   tree type = TREE_TYPE (exp);
  1200.  
  1201.   if (type == error_mark_node
  1202.       || basename == NULL_TREE
  1203.       || ! is_aggr_typedef (basename, 1))
  1204.     return error_mark_node;
  1205.  
  1206.   if (! IS_AGGR_TYPE (type))
  1207.     {
  1208.       error ("base object of scoped method call is not of aggregate type");
  1209.       return error_mark_node;
  1210.     }
  1211.  
  1212.   basetype = IDENTIFIER_TYPE_VALUE (basename);
  1213.  
  1214.   if (basetype = basetype_or_else (basetype, type))
  1215.     {
  1216.       if (basetype == error_mark_node)
  1217.     return error_mark_node;
  1218.       if (TREE_CODE (exp) == INDIRECT_REF)
  1219.     decl = build_indirect_ref (convert_pointer_to (basetype,
  1220.                                build_unary_op (ADDR_EXPR, exp, 0)), 0);
  1221.       else
  1222.     decl = build_scoped_ref (exp, scopes);
  1223.  
  1224.       /* Call to a destructor.  */
  1225.       if (TREE_CODE (name) == BIT_NOT_EXPR)
  1226.     {
  1227.       /* Explicit call to destructor.  */
  1228.       name = TREE_OPERAND (name, 0);
  1229.       if (! is_aggr_typedef (name, 1))
  1230.         return error_mark_node;
  1231.       if (TREE_TYPE (decl) != IDENTIFIER_TYPE_VALUE (name))
  1232.         {
  1233.           error_with_aggr_type (TREE_TYPE (decl),
  1234.                     "qualified type `%s' does not match destructor type `%s'",
  1235.                     IDENTIFIER_POINTER (name));
  1236.           return error_mark_node;
  1237.         }
  1238.       if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
  1239.         error_with_aggr_type (TREE_TYPE (decl), "type `%s' has no destructor");
  1240.       return build_delete (TREE_TYPE (decl), decl, integer_two_node,
  1241.                    LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
  1242.     }
  1243.  
  1244.       /* Call to a method.  */
  1245.       return build_method_call (decl, name, parms, NULL_TREE,
  1246.                 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
  1247.     }
  1248.   return error_mark_node;
  1249. }
  1250.  
  1251. /* Build something of the form ptr->method (args)
  1252.    or object.method (args).  This can also build
  1253.    calls to constructors, and find friends.
  1254.  
  1255.    Member functions always take their class variable
  1256.    as a pointer.
  1257.  
  1258.    INSTANCE is a class instance.
  1259.  
  1260.    NAME is the NAME field of the struct, union, or class
  1261.    whose type is that of INSTANCE.
  1262.  
  1263.    PARMS help to figure out what that NAME really refers to.
  1264.  
  1265.    BASETYPE_PATH, if non-NULL, tells which basetypes of INSTANCE
  1266.    we should be traversed before starting our search.  We need
  1267.    this information to get protected accesses correct.
  1268.  
  1269.    FLAGS is the logical disjunction of zero or more LOOKUP_
  1270.    flags.  See cplus-tree.h for more info.
  1271.  
  1272.    If this is all OK, calls build_function_call with the resolved
  1273.    member function.
  1274.  
  1275.    This function must also handle being called to perform
  1276.    initialization, promotion/coercion of arguments, and
  1277.    instantiation of default parameters.
  1278.  
  1279.    Note that NAME may refer to an instance variable name.  If
  1280.    `operator()()' is defined for the type of that field, then we return
  1281.    that result.  */
  1282. tree
  1283. build_method_call (instance, name, parms, basetype_path, flags)
  1284.      tree instance, name, parms, basetype_path;
  1285.      int flags;
  1286. {
  1287.   register tree function, fntype, value_type;
  1288.   register tree basetype, save_basetype;
  1289.   register tree baselink, result, method_name, parmtypes, parm;
  1290.   tree last;
  1291.   int pass;
  1292.   enum visibility_type visibility;
  1293.   int rank_for_overload ();
  1294.  
  1295.   /* Range of cases for vtable optimization.  */
  1296.   enum vtable_needs
  1297.     {
  1298.       not_needed, maybe_needed, unneeded, needed,
  1299.     };
  1300.   enum vtable_needs need_vtbl = not_needed;
  1301.  
  1302.   char *err_name;
  1303.   char *name_kind;
  1304.   int ever_seen = 0;
  1305.   int wrap;
  1306.   tree wrap_type;
  1307.   tree instance_ptr = NULL_TREE;
  1308.   int all_virtual = flag_all_virtual;
  1309.   int static_call_context;
  1310.   tree saw_private = 0;
  1311.   tree saw_protected = 0;
  1312. #ifdef SOS
  1313.   /* If call is a call to a constructor, then `dtbl'
  1314.      will first be initialized with the function table pointer
  1315.      of the appropriate type (calling "sosFindCode" as a last
  1316.      resort), the the call to the constructor will go through there.  */
  1317.   tree dtbl = (flags & LOOKUP_DYNAMIC) ? TREE_VALUE (parms) : NULL_TREE;
  1318.  
  1319.   /* Flag saying whether or not `dtbl' has been inserted into the
  1320.      parameter list.  This is needed because we cannot tell (until
  1321.      we have a match) whether this parameter should go in or not.
  1322.  
  1323.      If 1, then `dtbl' is living naturally.
  1324.      If 0, then `dtbl' is not among the parms that we know about.
  1325.      If -1, the `dtbl' was place into the parms unnaturally.
  1326.  
  1327.      Note that we may side-effect the parameter list, but in such a way
  1328.      that the caller of this function would never know.  */
  1329.   int dtbl_inserted = (flags & LOOKUP_DYNAMIC);
  1330. #endif
  1331.  
  1332.   /* Keep track of `const' and `volatile' objects.  */
  1333.   int constp, volatilep;
  1334.  
  1335.   /* Know if this is explicit destructor call.  */
  1336.   int dtor_specd = 0;
  1337.  
  1338. #ifdef GATHER_STATISTICS
  1339.   n_build_method_call++;
  1340. #endif
  1341.  
  1342.   if (instance == error_mark_node
  1343.       || name == error_mark_node
  1344.       || parms == error_mark_node
  1345.       || (instance != 0 && TREE_TYPE (instance) == error_mark_node))
  1346.     return error_mark_node;
  1347.  
  1348. #if 0
  1349.   /* C++ 2.1 does not allow this, but ANSI probably will.  */
  1350.   if (TREE_CODE (name) == BIT_NOT_EXPR)
  1351.     {
  1352.       error ("invalid call to destructor, use qualified name `%s::~%s'",
  1353.          IDENTIFIER_POINTER (name), IDENTIFIER_POINTER (name));
  1354.       return error_mark_node;
  1355.     }
  1356. #else
  1357.   if (TREE_CODE (name) == BIT_NOT_EXPR)
  1358.     {
  1359.       flags |= LOOKUP_DESTRUCTOR;
  1360.       name = TREE_OPERAND (name, 0);
  1361.       if (! is_aggr_typedef (name, 1))
  1362.     return error_mark_node;
  1363.       if (parms)
  1364.     error ("destructors take no parameters");
  1365.       basetype = IDENTIFIER_TYPE_VALUE (name);
  1366.       if (! TYPE_HAS_DESTRUCTOR (basetype))
  1367.     {
  1368. #if 0 /* ARM says tp->~T() without T::~T() is valid.  */
  1369.       error_with_aggr_type (basetype, "type `%s' has no destructor");
  1370. #endif
  1371.       /* A destructive destructor wouldn't be a bad idea, but let's
  1372.          not bother for now.  */
  1373.       return build_c_cast (void_type_node, instance);
  1374.     }
  1375.       instance = default_conversion (instance);
  1376.       if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
  1377.     instance_ptr = instance;
  1378.       else
  1379.     instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
  1380.       return build_delete (basetype, instance_ptr, integer_two_node,
  1381.                LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
  1382.     }
  1383. #endif
  1384.  
  1385.   if (TREE_CODE (name) == WRAPPER_EXPR)
  1386.     {
  1387.       wrap_type = TREE_OPERAND (name, 0);
  1388.       name = TREE_OPERAND (name, 1);
  1389.       wrap = 1;
  1390.     }
  1391.   else if (TREE_CODE (name) == ANTI_WRAPPER_EXPR)
  1392.     {
  1393.       wrap_type = TREE_OPERAND (name, 0);
  1394.       name = TREE_OPERAND (name, 1);
  1395.       wrap = -1;
  1396.     }
  1397.   else
  1398.     {
  1399.       wrap_type = NULL_TREE;
  1400.       wrap = 0;
  1401.     }
  1402.  
  1403.   /* Initialize name for error reporting.  */
  1404.   if (TREE_CODE (name) == OP_IDENTIFIER)
  1405.     name = build_operator_fnname (&name, parms, 1);
  1406.  
  1407.   if (OPERATOR_NAME_P (name))
  1408.     {
  1409.       char *p = operator_name_string (name);
  1410.       err_name = (char *)alloca (strlen (p) + 10);
  1411.       sprintf (err_name, "operator %s", p);
  1412.     }
  1413.   else if (name == wrapper_name)
  1414.     err_name = "wrapper";
  1415.   else if (OPERATOR_TYPENAME_P (name))
  1416.     err_name = "type conversion operator";
  1417.   else if (TREE_CODE (name) == SCOPE_REF)
  1418.     err_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
  1419.   else
  1420.     err_name = IDENTIFIER_POINTER (name);
  1421.  
  1422.   if (OPERATOR_TYPENAME_P (name) || OPERATOR_NAME_P (name))
  1423.     GNU_xref_call (current_function_decl,  IDENTIFIER_POINTER (name));
  1424.   else
  1425.     GNU_xref_call (current_function_decl, err_name);
  1426.  
  1427.   if (wrap)
  1428.     {
  1429.       char *p = (char *)alloca (strlen (err_name) + 32);
  1430.       sprintf (p, "%s for `%s'", wrap < 0 ? "anti-wrapper" : "wrapper", err_name);
  1431.       err_name = p;
  1432.     }
  1433.  
  1434.   if (instance == NULL_TREE)
  1435.     {
  1436.       static_call_context = 0;
  1437.  
  1438.       basetype = NULL_TREE;
  1439.       /* Check cases where this is really a call to raise
  1440.      an exception.  */
  1441.       if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
  1442.     {
  1443.       basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
  1444.       if (basetype)
  1445.         basetype = TREE_VALUE (basetype);
  1446.     }
  1447.       else if (TREE_CODE (name) == SCOPE_REF
  1448.            && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
  1449.     {
  1450.       if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
  1451.         return error_mark_node;
  1452.       basetype = purpose_member (TREE_OPERAND (name, 1),
  1453.                      CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0))));
  1454.       if (basetype)
  1455.         basetype = TREE_VALUE (basetype);
  1456.     }
  1457.  
  1458.       if (basetype != NULL_TREE)
  1459.     ;
  1460.       /* call to a constructor... */
  1461.       else if (IDENTIFIER_HAS_TYPE_VALUE (name))
  1462.     {
  1463.       basetype = IDENTIFIER_TYPE_VALUE (name);
  1464.       name = constructor_name (basetype);
  1465.     }
  1466.       else
  1467.     {
  1468.       tree typedef_name = lookup_name (name);
  1469.       if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
  1470.         {
  1471.           /* Cannonicalize the typedef name.  */
  1472.           basetype = TREE_TYPE (typedef_name);
  1473.           name = TYPE_IDENTIFIER (basetype);
  1474.         }
  1475.       else
  1476.         {
  1477.           error ("no constructor named `%s' in visible scope",
  1478.              IDENTIFIER_POINTER (name));
  1479.           return error_mark_node;
  1480.         }
  1481.     }
  1482.       if (wrap_type && wrap_type != basetype)
  1483.     {
  1484.       error_with_aggr_type (wrap_type, "invalid constructor `%s::%s'",
  1485.                 TYPE_NAME_STRING (basetype));
  1486.       return error_mark_node;
  1487.     }
  1488.       if (TYPE_VIRTUAL_P (basetype))
  1489.     {
  1490.       wrap_type = basetype;
  1491.     }
  1492.  
  1493.       if (! IS_AGGR_TYPE (basetype))
  1494.     {
  1495.     non_aggr_error:
  1496.       if ((flags & LOOKUP_COMPLAIN) && TREE_CODE (basetype) != ERROR_MARK)
  1497.         error ("request for member `%s' in something not a structure or union", err_name);
  1498.  
  1499.       return error_mark_node;
  1500.     }
  1501.     }
  1502.   else if (instance == C_C_D || instance == current_class_decl)
  1503.     {
  1504.       extern tree ctor_label, dtor_label;
  1505.  
  1506.       /* When doing initialization, we side-effect the TREE_TYPE of
  1507.      C_C_D, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE.  */
  1508.       basetype = TREE_TYPE (C_C_D);
  1509.  
  1510.       /* Anything manifestly `this' in constructors and destructors
  1511.      has a known type, so virtual function tables are not needed.  */
  1512.       if (TYPE_VIRTUAL_P (basetype)
  1513.       && !(flags & LOOKUP_NONVIRTUAL)
  1514.       && wrap_type == NULL_TREE)
  1515.     need_vtbl = (dtor_label || ctor_label)
  1516.       ? unneeded : maybe_needed;
  1517.  
  1518.       static_call_context = 0;
  1519.       instance = C_C_D;
  1520.       instance_ptr = current_class_decl;
  1521.       result = build_field_call (CLASSTYPE_AS_LIST (current_class_type),
  1522.                  instance_ptr, name, parms, err_name);
  1523.  
  1524.       if (result)
  1525.     return result;
  1526.     }
  1527.   else if (TREE_CODE (instance) == RESULT_DECL)
  1528.     {
  1529.       static_call_context = 0;
  1530.       basetype = TREE_TYPE (instance);
  1531.       if (wrap_type)
  1532.     {
  1533.       if (basetype_or_else (basetype, wrap_type))
  1534.         basetype = wrap_type;
  1535.       else
  1536.         return error_mark_node;
  1537.     }
  1538.       /* Should we ever have to make a virtual function reference
  1539.      from a RESULT_DECL, know that it must be of fixed type
  1540.      within the scope of this function.  */
  1541.       else if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
  1542.     need_vtbl = maybe_needed;
  1543.       instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (basetype), instance);
  1544.     }
  1545.   else if (instance == current_exception_object)
  1546.     {
  1547.       instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (current_exception_type),
  1548.                 TREE_OPERAND (current_exception_object, 0));
  1549.       mark_addressable (TREE_OPERAND (current_exception_object, 0));
  1550.       result = build_field_call (CLASSTYPE_AS_LIST (current_exception_type),
  1551.                  instance_ptr, name, parms, err_name);
  1552.       if (result)
  1553.     return result;
  1554.       error ("exception member `%s' cannot be invoked", err_name);
  1555.       return error_mark_node;
  1556.     }
  1557.   else
  1558.     {
  1559.       /* The MAIN_VARIANT of the type that `instance_ptr' winds up being.  */
  1560.       tree inst_ptr_basetype;
  1561.  
  1562.       /* from the file "cplus-typeck.c".  */
  1563.       extern tree unary_complex_lvalue ();
  1564.  
  1565.       static_call_context = (TREE_CODE (instance) == NOP_EXPR
  1566.                  && TREE_OPERAND (instance, 0) == error_mark_node);
  1567.  
  1568.       /* the base type of an instance variable is pointer to class */
  1569.       basetype = TREE_TYPE (instance);
  1570.  
  1571.       if (TREE_CODE (basetype) == REFERENCE_TYPE)
  1572.     {
  1573.       basetype = TYPE_MAIN_VARIANT (TREE_TYPE (basetype));
  1574.       if (! IS_AGGR_TYPE (basetype))
  1575.         goto non_aggr_error;
  1576.       /* Call to convert not needed because we are remaining
  1577.          within the same type.  */
  1578.       instance_ptr = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), instance);
  1579.       inst_ptr_basetype = basetype;
  1580.     }
  1581.       else
  1582.     {
  1583.       if (TREE_CODE (basetype) == POINTER_TYPE)
  1584.         {
  1585.           basetype = TREE_TYPE (basetype);
  1586.           instance_ptr = instance;
  1587.         }
  1588.  
  1589.       if (! IS_AGGR_TYPE (basetype))
  1590.         goto non_aggr_error;
  1591.  
  1592.       if (! instance_ptr)
  1593.         {
  1594.           if ((lvalue_p (instance)
  1595.            && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
  1596.           || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
  1597.         {
  1598.           if (instance_ptr == error_mark_node)
  1599.             return error_mark_node;
  1600.         }
  1601.           else if (TREE_CODE (instance) == NOP_EXPR
  1602.                || TREE_CODE (instance) == CONSTRUCTOR)
  1603.         {
  1604.           /* A cast is not an lvalue.  Initialize a fresh temp
  1605.              with the value we are casting from, and proceed with
  1606.              that temporary.  We can't cast to a reference type,
  1607.              so that simplifies the initialization to something
  1608.              we can manage.  */
  1609.           tree temp = get_temp_name (TREE_TYPE (instance), 0);
  1610.           if (IS_AGGR_TYPE (TREE_TYPE (instance)))
  1611.             expand_aggr_init (temp, instance, 0);
  1612.           else
  1613.             {
  1614.               store_init_value (temp, instance);
  1615.               expand_decl_init (temp);
  1616.             }
  1617.           instance = temp;
  1618.           instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
  1619.         }
  1620.           else
  1621.         {
  1622.           assert (TREE_CODE (instance) == CALL_EXPR);
  1623.           if (TYPE_NEEDS_CONSTRUCTOR (basetype))
  1624.             instance = build_cplus_new (basetype, instance, 0);
  1625.           else
  1626.             {
  1627.               instance = get_temp_name (basetype, 0);
  1628.               TREE_ADDRESSABLE (instance) = 1;
  1629.             }
  1630.           instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
  1631.         }
  1632.           /* @@ Should we call comp_target_types here?  */
  1633.           inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
  1634.           if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
  1635.         basetype = inst_ptr_basetype;
  1636.           else
  1637.         instance_ptr = convert (TYPE_POINTER_TO (basetype), instance_ptr);
  1638.         }
  1639.       else
  1640.         inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
  1641.     }
  1642.  
  1643.       if (basetype_path == NULL_TREE)
  1644.     basetype_path = CLASSTYPE_AS_LIST (inst_ptr_basetype);
  1645.  
  1646.       result = build_field_call (basetype_path, instance_ptr, name, parms, err_name);
  1647.       if (result)
  1648.     return result;
  1649.  
  1650.       if (wrap_type)
  1651.     {
  1652.       if (basetype_or_else (basetype, wrap_type))
  1653.         basetype = wrap_type;
  1654.       else
  1655.         return error_mark_node;
  1656.     }
  1657.       else if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
  1658.     {
  1659.       if (TREE_SIDE_EFFECTS (instance_ptr))
  1660.         {
  1661.           /* This action is needed because the instance is needed
  1662.          for providing the base of the virtual function table.
  1663.          Without using a SAVE_EXPR, the function we are building
  1664.          may be called twice, or side effects on the instance
  1665.          variable (such as a post-increment), may happen twice.  */
  1666.           instance_ptr = save_expr (instance_ptr);
  1667.           instance = build_indirect_ref (instance_ptr, 0);
  1668.         }
  1669.       else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
  1670.         {
  1671.           /* This happens when called for operator new ().  */
  1672.           instance = build_indirect_ref (instance, 0);
  1673.         }
  1674.  
  1675.       need_vtbl = maybe_needed;
  1676.     }
  1677.     }
  1678.  
  1679.   if (TYPE_SIZE (basetype) == 0)
  1680.     {
  1681.       /* This is worth complaining about, I think.  */
  1682.       error_with_aggr_type (basetype, "cannot lookup method in incomplete type `%s'");
  1683.       return error_mark_node;
  1684.     }
  1685.  
  1686.   /* Are we building a non-virtual wrapper?  */
  1687.   if (flags & LOOKUP_NONVIRTUAL)
  1688.     {
  1689.       if (all_virtual)
  1690.     sorry ("non-virtual call with -fall-virtual");
  1691.       if (wrap)
  1692.     wrap_type = basetype;
  1693.     }
  1694.  
  1695.   save_basetype = basetype;
  1696.  
  1697.   if (all_virtual == 1
  1698.       && (! strncmp (IDENTIFIER_POINTER (name), OPERATOR_METHOD_FORMAT,
  1699.              OPERATOR_METHOD_LENGTH)
  1700.       || instance_ptr == NULL_TREE
  1701.       || (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype) == 0
  1702.           && TYPE_NEEDS_WRAPPER (basetype) == 0)))
  1703.     all_virtual = 0;
  1704.  
  1705.   last = NULL_TREE;
  1706.   for (parmtypes = 0, parm = parms; parm; parm = TREE_CHAIN (parm))
  1707.     {
  1708.       tree t = TREE_TYPE (TREE_VALUE (parm));
  1709.       if (TREE_CODE (t) == OFFSET_TYPE)
  1710.     {
  1711.       /* Convert OFFSET_TYPE entities to their normal selves.  */
  1712.       TREE_VALUE (parm) = resolve_offset_ref (TREE_VALUE (parm));
  1713.       t = TREE_TYPE (TREE_VALUE (parm));
  1714.     }
  1715.       if (TREE_CODE (t) == ARRAY_TYPE)
  1716.     {
  1717.       /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
  1718.          This eliminates needless calls to `compute_conversion_costs'.  */
  1719.       TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
  1720.       t = TREE_TYPE (TREE_VALUE (parm));
  1721.     }
  1722.       if (t == error_mark_node)
  1723.     return error_mark_node;
  1724.       last = build_tree_list (NULL_TREE, t);
  1725.       parmtypes = chainon (parmtypes, last);
  1726.     }
  1727.  
  1728.   if (instance)
  1729.     {
  1730.       constp = TREE_READONLY (instance);
  1731.       volatilep = TREE_THIS_VOLATILE (instance);
  1732.       parms = tree_cons (NULL_TREE, instance_ptr, parms);
  1733.     }
  1734.   else
  1735.     {
  1736.       /* Raw constructors are always in charge.  */
  1737.       if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
  1738.       && ! (flags & LOOKUP_HAS_IN_CHARGE))
  1739.     {
  1740.       flags |= LOOKUP_HAS_IN_CHARGE;
  1741.       parms = tree_cons (NULL_TREE, integer_one_node, parms);
  1742.       parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
  1743.     }
  1744.  
  1745.       if (flag_this_is_variable)
  1746.     {
  1747.       constp = 0;
  1748.       volatilep = 0;
  1749.       parms = tree_cons (NULL_TREE, build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node), parms);
  1750.     }
  1751.       else
  1752.     {
  1753.       constp = 0;
  1754.       volatilep = 0;
  1755.       instance_ptr = build_new (NULL_TREE, basetype, void_type_node, 0);
  1756.       if (instance_ptr == error_mark_node)
  1757.         return error_mark_node;
  1758.       instance_ptr = save_expr (instance_ptr);
  1759.       TREE_CALLS_NEW (instance_ptr) = 1;
  1760.       instance = build_indirect_ref (instance_ptr, 0);
  1761.       parms = tree_cons (NULL_TREE, instance_ptr, parms);
  1762.     }
  1763.     }
  1764.   parmtypes = tree_cons (NULL_TREE,
  1765.              build_pointer_type (build_type_variant (basetype, constp, volatilep)),
  1766.              parmtypes);
  1767.   if (last == NULL_TREE)
  1768.     last = parmtypes;
  1769.  
  1770.   /* Look up function name in the structure type definition.  */
  1771.  
  1772.   if (wrap)
  1773.     {
  1774.       if (wrap > 0)
  1775.     name_kind = "wrapper";
  1776.       else
  1777.     name_kind = "anti-wrapper";
  1778.       baselink = get_wrapper (basetype);
  1779.     }
  1780.   else
  1781.     {
  1782.       if (IDENTIFIER_HAS_TYPE_VALUE (name)
  1783.       && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name)))
  1784.     {
  1785.       tree tmp = NULL_TREE;
  1786.       if (IDENTIFIER_TYPE_VALUE (name) == basetype)
  1787.         tmp = basetype;
  1788.       else
  1789.         tmp = get_base_type (IDENTIFIER_TYPE_VALUE (name), basetype, 0);
  1790.       if (tmp != 0)
  1791.         {
  1792.           name_kind = "constructor";
  1793.  
  1794.           if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
  1795.             && ! (flags & LOOKUP_HAS_IN_CHARGE))
  1796.         {
  1797.           /* Constructors called for initialization
  1798.              only are never in charge.  */
  1799.           tree tmplist;
  1800.  
  1801.           flags |= LOOKUP_HAS_IN_CHARGE;
  1802.           tmplist = tree_cons (NULL_TREE, integer_zero_node,
  1803.                        TREE_CHAIN (parms));
  1804.           TREE_CHAIN (parms) = tmplist;
  1805.           tmplist = tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
  1806.           TREE_CHAIN (parmtypes) = tmplist;
  1807.         }
  1808.  
  1809. #ifdef SOS
  1810.           if (TYPE_DYNAMIC (basetype) && dtbl_inserted == 0)
  1811.         {
  1812.           tree parm, parmtype;
  1813.           dtbl = get_sos_dtable (basetype);
  1814.           parm = tree_cons (NULL_TREE, dtbl, TREE_CHAIN (parms));
  1815.           parmtype = tree_cons (NULL_TREE, build_pointer_type (ptr_type_node), TREE_CHAIN (parmtypes));
  1816.           TREE_CHAIN (parms) = parm;
  1817.           TREE_CHAIN (parmtypes) = parmtype;
  1818.           dtbl_inserted = -1;
  1819.         }
  1820. #endif
  1821.           /* constructors are in very specific places.  */
  1822. #ifdef SOS
  1823.           if (dtbl_inserted == -1)
  1824.         {
  1825.           TREE_CHAIN (parmtypes) = TREE_CHAIN (TREE_CHAIN (parmtypes));
  1826.           TREE_CHAIN (parms) = TREE_CHAIN (TREE_CHAIN (parms));
  1827.           dtbl_inserted = 0;
  1828.         }
  1829. #endif
  1830.           basetype = tmp;
  1831.         }
  1832.       else
  1833.         name_kind = "method";
  1834.     }
  1835.       else name_kind = "method";
  1836.  
  1837.       if (basetype_path == NULL_TREE)
  1838.     basetype_path = CLASSTYPE_AS_LIST (basetype);
  1839.       result = lookup_fnfields (basetype_path, name,
  1840.                 (flags & LOOKUP_COMPLAIN));
  1841.       if (result == error_mark_node)
  1842.     return error_mark_node;
  1843.     }
  1844.  
  1845.   /* Now, go look for this method name. We do not find destructors here.
  1846.  
  1847.      Putting `void_list_node' on the end of the parmtypes
  1848.      fakes out `build_decl_overload' into doing the right thing.  */
  1849.   TREE_CHAIN (last) = void_list_node;
  1850.   method_name = build_decl_overload (IDENTIFIER_POINTER (name),
  1851.                      parmtypes,
  1852.                      1 + (name == constructor_name (save_basetype)));
  1853.   TREE_CHAIN (last) = NULL_TREE;
  1854.  
  1855.   for (pass = 0; pass < 2; pass++)
  1856.     {
  1857.       struct candidate *candidates;
  1858.       struct candidate *cp;
  1859.       int len, best = 2;
  1860.  
  1861.       /* This increments every time we go up the type hierarchy.
  1862.      The idea is to prefer a function of the derived class if possible.  */
  1863.       int b_or_d;
  1864.  
  1865.       baselink = result;
  1866.  
  1867.       if (pass > 0)
  1868.     {
  1869.       candidates = (struct candidate *) alloca ((ever_seen+1) * sizeof (struct candidate));
  1870.       cp = candidates;
  1871.       len = list_length (parms);
  1872.       b_or_d = 0;
  1873.  
  1874.       /* First see if a global function has a shot at it.  */
  1875.       if (flags & LOOKUP_GLOBAL)
  1876.         {
  1877.           tree friend_parms;
  1878.           tree parm = TREE_VALUE (parms);
  1879.  
  1880.           if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
  1881.         friend_parms = parms;
  1882.           else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
  1883.         {
  1884.           parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
  1885.           parm = convert (build_reference_type (TREE_TYPE (parm)), parm);
  1886.           friend_parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
  1887.         }
  1888.           else
  1889.         assert (0);
  1890.  
  1891.           cp->harshness
  1892.         = (unsigned short *)alloca ((len+1) * sizeof (short));
  1893.           result = build_overload_call (name, friend_parms, 0, cp);
  1894.           /* If it turns out to be the one we were actually looking for
  1895.          (it was probably a friend function), the return the
  1896.          good result.  */
  1897.           if (TREE_CODE (result) == CALL_EXPR)
  1898.         return result;
  1899.  
  1900.           while (cp->evil == 0)
  1901.         {
  1902.           /* non-standard uses: set the field to 0 to indicate
  1903.              we are using a non-member function.  */
  1904.           cp->u.field = 0;
  1905.           if (cp->harshness[len] == 0
  1906.               && cp->harshness[len] == 0
  1907.               && cp->user == 0 && cp->b_or_d == 0
  1908.               && cp->easy < best)
  1909.             best = cp->easy;
  1910.           cp += 1;
  1911.         }
  1912.         }
  1913.     }
  1914.  
  1915.       while (baselink)
  1916.     {
  1917.       /* We have a hit (of sorts). If the parameter list is
  1918.          "error_mark_node", or some variant thereof, it won't
  1919.          match any methods. Since we have verified that the is
  1920.          some method vaguely matching this one (in name at least),
  1921.          silently return.
  1922.          
  1923.          Don't stop for friends, however.  */
  1924.       tree basetypes = TREE_PURPOSE (baselink);
  1925.  
  1926.       function = TREE_VALUE (baselink);
  1927.       basetype = TREE_VALUE (basetypes);
  1928.  
  1929.       /* Cast the instance variable to the approriate type.  */
  1930.       TREE_VALUE (parmtypes) = TYPE_POINTER_TO (basetype);
  1931.  
  1932.       if (DESTRUCTOR_NAME_P (DECL_NAME (function)))
  1933.         function = DECL_CHAIN (function);
  1934.  
  1935.       for (; function; function = DECL_CHAIN (function))
  1936.         {
  1937. #ifdef GATHER_STATISTICS
  1938.           n_inner_fields_searched++;
  1939. #endif
  1940.           ever_seen++;
  1941.  
  1942.           /* Not looking for friends here.  */
  1943.           if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
  1944.           && ! DECL_STATIC_FUNCTION_P (function))
  1945.         continue;
  1946.  
  1947.           if (pass == 0
  1948.           && DECL_NAME (function) == method_name)
  1949.         {
  1950.           if (flags & LOOKUP_PROTECT)
  1951.             {
  1952.               visibility = compute_visibility (basetypes, function);
  1953.               if (visibility == visibility_protected
  1954.               && flags & LOOKUP_PROTECTED_OK)
  1955.             visibility = visibility_public;
  1956.             }
  1957.  
  1958.           if ((flags & LOOKUP_PROTECT) == 0
  1959.               || visibility == visibility_public)
  1960.             goto found_and_ok;
  1961.           else if (visibility == visibility_private)
  1962.             saw_private = function;
  1963.           else if (visibility == visibility_protected)
  1964.             saw_protected = function;
  1965.           /* If we fail on the exact match, we have
  1966.              an immediate failure.  */
  1967.           goto found;
  1968.         }
  1969.           if (pass > 0)
  1970.         {
  1971.           tree these_parms = parms;
  1972.  
  1973. #ifdef GATHER_STATISTICS
  1974.           n_inner_fields_searched++;
  1975. #endif
  1976.           cp->harshness
  1977.             = (unsigned short *)alloca ((len+1) * sizeof (short));
  1978.           if (DECL_STATIC_FUNCTION_P (function))
  1979.             these_parms = TREE_CHAIN (these_parms);
  1980.           compute_conversion_costs (function, these_parms, cp, len);
  1981.           cp->b_or_d += b_or_d;
  1982.           if (cp->evil == 0)
  1983.             {
  1984.               cp->u.field = function;
  1985.               cp->function = function;
  1986.               if (flags & LOOKUP_PROTECT)
  1987.             {
  1988.               enum visibility_type this_v;
  1989.               this_v = compute_visibility (basetypes, function);
  1990.               if (this_v == visibility_protected
  1991.                   && (flags & LOOKUP_PROTECTED_OK))
  1992.                 this_v = visibility_public;
  1993.               if (this_v != visibility_public)
  1994.                 {
  1995.                   if (this_v == visibility_private)
  1996.                 saw_private = function;
  1997.                   else
  1998.                 saw_protected = function;
  1999.                   continue;
  2000.                 }
  2001.             }
  2002.  
  2003.               /* No "two-level" conversions.  */
  2004.               if (flags & LOOKUP_NO_CONVERSION && cp->user != 0)
  2005.             continue;
  2006.  
  2007.               /* If we used default parameters, we must
  2008.              check to see whether anyone else might
  2009.              use them also, and report a possible
  2010.              ambiguity.  */
  2011.               if (! TYPE_USES_MULTIPLE_INHERITANCE (save_basetype)
  2012.               && cp->harshness[len] == 0
  2013.               && (cp->harshness[0] & 128) == 0
  2014.               && cp->user == 0 && cp->b_or_d == 0
  2015.               && cp->easy < best)
  2016.             {
  2017.               if (! DECL_STATIC_FUNCTION_P (function))
  2018.                 TREE_VALUE (parms) = cp->arg;
  2019.               if (best == 2)
  2020.                 goto found_and_maybe_warn;
  2021.             }
  2022.               cp++;
  2023.             }
  2024.         }
  2025.         }
  2026.       /* Now we have run through one link's member functions.
  2027.          arrange to head-insert this link's links.  */
  2028.       baselink = next_baselink (baselink);
  2029.       b_or_d += 1;
  2030.     }
  2031.       if (pass == 0)
  2032.     {
  2033.       /* No exact match could be found.  Now try to find match
  2034.          using default conversions.  */
  2035.       if ((flags & LOOKUP_GLOBAL) && IDENTIFIER_GLOBAL_VALUE (name))
  2036.         if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (name)) == FUNCTION_DECL)
  2037.           ever_seen += 1;
  2038.         else if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (name)) == TREE_LIST)
  2039.           ever_seen += list_length (IDENTIFIER_GLOBAL_VALUE (name));
  2040.  
  2041.       if (ever_seen == 0)
  2042.         {
  2043.           if (flags & LOOKUP_GLOBAL)
  2044.         error ("no global or member function `%s' defined", err_name);
  2045.           else
  2046.         error_with_aggr_type (save_basetype, "no member function `%s::%s'", err_name);
  2047.           return error_mark_node;
  2048.         }
  2049.       continue;
  2050.     }
  2051.  
  2052.       if (cp - candidates != 0)
  2053.     {
  2054.       /* Rank from worst to best.  Then cp will point to best one.
  2055.          Private fields have their bits flipped.  For unsigned
  2056.          numbers, this should make them look very large.
  2057.          If the best alternate has a (signed) negative value,
  2058.          then all we ever saw were private members.  */
  2059.       if (cp - candidates > 1)
  2060.         {
  2061.           cp = ideal_candidate (save_basetype, candidates,
  2062.                     cp - candidates, parms, len);
  2063.           if (cp == 0)
  2064.         {
  2065.           error ("ambiguous type conversion requested for %s `%s'",
  2066.              name_kind, err_name);
  2067.           return error_mark_node;
  2068.         }
  2069.         }
  2070.       else if (cp[-1].evil == 2)
  2071.         {
  2072.           error ("ambiguous type conversion requested for %s `%s'",
  2073.              name_kind, err_name);
  2074.           return error_mark_node;
  2075.         }
  2076.       else cp--;
  2077.  
  2078.       /* The global function was the best, so use it.  */
  2079.       if (cp->u.field == 0)
  2080.         {
  2081.           /* We must convert the instance pointer into a reference type.
  2082.          Global overloaded functions can only either take
  2083.          aggregate objects (which come for free from references)
  2084.          or reference data types anyway.  */
  2085.           TREE_VALUE (parms) = copy_node (instance_ptr);
  2086.           TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
  2087.           return build_function_call (cp->function, parms);
  2088.         }
  2089.  
  2090.       function = cp->function;
  2091.       if (DECL_STATIC_FUNCTION_P (function))
  2092.         basetype = NULL_TREE;
  2093.       else
  2094.         {
  2095.           basetype = TREE_TYPE (TREE_TYPE (cp->arg));
  2096.           TREE_VALUE (parms) = cp->arg;
  2097.         }
  2098.       goto found_and_maybe_warn;
  2099.     }
  2100.  
  2101.       if ((flags & ~LOOKUP_GLOBAL) & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
  2102.     {
  2103.       char *tag_name, *buf;
  2104.  
  2105.       if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
  2106.           == LOOKUP_SPECULATIVELY)
  2107.         return NULL_TREE;
  2108.  
  2109.       if (DECL_STATIC_FUNCTION_P (cp->function))
  2110.         parms = TREE_CHAIN (parms);
  2111.       if (ever_seen)
  2112.         {
  2113.           if (((int)saw_protected|(int)saw_private) == 0)
  2114.         {
  2115.           if (flags & LOOKUP_SPECULATIVELY)
  2116.             return NULL_TREE;
  2117.           if (static_call_context && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
  2118.             error_with_aggr_type (TREE_TYPE (TREE_TYPE (instance_ptr)),
  2119.                       "object missing in call to `%s::%s'",
  2120.                       err_name);
  2121.           else
  2122.             report_type_mismatch (cp, parms, name_kind, err_name);
  2123.         }
  2124.           else
  2125.         {
  2126.           char buf[80];
  2127.           char *msg;
  2128.           tree seen = saw_private;
  2129.  
  2130.           if (saw_private)
  2131.             if (saw_protected)
  2132.               msg = "%s %%s (and the like) are private or protected";
  2133.             else
  2134.               msg = "the %s %%s is private";
  2135.           else
  2136.             {
  2137.               msg = "the %s %%s is protected";
  2138.               seen = saw_protected;
  2139.             }
  2140.           sprintf (buf, msg, name_kind);
  2141.           error_with_decl (seen, buf);
  2142.           error ("within this context");
  2143.         }
  2144.           return error_mark_node;
  2145.         }
  2146.  
  2147.       if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
  2148.           == LOOKUP_COMPLAIN)
  2149.         {
  2150.           if (TREE_CODE (save_basetype) == RECORD_TYPE)
  2151.         tag_name = "structure";
  2152.           else
  2153.         tag_name = "union";
  2154.  
  2155.           if (wrap)
  2156.         buf = "%s has no appropriate wrapper function defined";
  2157.           else
  2158.         {
  2159.           buf = (char *)alloca (30 + strlen (err_name));
  2160.           strcpy (buf, "%s has no method named `%s'");
  2161.         }
  2162.  
  2163.           error (buf, tag_name, err_name);
  2164.           return error_mark_node;
  2165.         }
  2166.       return NULL_TREE;
  2167.     }
  2168.       continue;
  2169.  
  2170.     found_and_maybe_warn:
  2171.       if (cp->harshness[0] & 128)
  2172.     {
  2173.       if (flags & LOOKUP_COMPLAIN)
  2174.         {
  2175.           error_with_decl (cp->function, "non-const member function `%s'");
  2176.           error ("called for const object at this point in file");
  2177.         }
  2178.       /* Not good enough for a match.  */
  2179.       else return error_mark_node;
  2180.     }
  2181.       goto found_and_ok;
  2182.     }
  2183.   /* Silently return error_mark_node.  */
  2184.   return error_mark_node;
  2185.  
  2186.  found:
  2187.   if (visibility == visibility_private)
  2188.     {
  2189.       if (flags & LOOKUP_COMPLAIN)
  2190.     error (TREE_PRIVATE (function)
  2191.            ? "%s `%s' is private"
  2192.            : "%s `%s' is from private base class",
  2193.            name_kind,
  2194.            lang_printable_name (function));
  2195.       return error_mark_node;
  2196.     }
  2197.   else if (visibility == visibility_protected)
  2198.     {
  2199.       if (flags & LOOKUP_COMPLAIN)
  2200.     error (TREE_PROTECTED (function)
  2201.            ? "%s `%s' is protected"
  2202.            : "%s `%s' has protected visibility from this point",
  2203.            name_kind,
  2204.            lang_printable_name (function));
  2205.       return error_mark_node;
  2206.     }
  2207.   abort ();
  2208.  
  2209.  found_and_ok:
  2210.  
  2211.   /* From here on down, BASETYPE is the type that INSTANCE_PTR's
  2212.      type (if it exists) is a pointer to.  */
  2213.   basetype = DECL_CONTEXT (function);
  2214.   fntype = TREE_TYPE (function);
  2215.  
  2216.   if (TREE_CODE (fntype) == POINTER_TYPE)
  2217.     fntype = TREE_TYPE (fntype);
  2218.  
  2219.   /* If we are referencing a virtual function from an object
  2220.      of effectively static type, then there is no need
  2221.      to go through the virtual function table.  */
  2222.   if (need_vtbl == maybe_needed)
  2223.     {
  2224.       int fixed_type = resolves_to_fixed_type_p (instance, 0);
  2225.  
  2226.       if (all_virtual == 1
  2227.       && DECL_VINDEX (function)
  2228.       && may_be_remote (basetype))
  2229.     need_vtbl = needed;
  2230.       else if (DECL_VIRTUAL_P (function))
  2231.     need_vtbl = fixed_type ? unneeded : needed;
  2232.       else
  2233.     need_vtbl = not_needed;
  2234.  
  2235.       if (fixed_type && DECL_ABSTRACT_VIRTUAL_P (function))
  2236.     {
  2237.       error_with_decl (function, "invalid call to abstract function `%s'");
  2238.       return error_mark_node;
  2239.     }
  2240.     }
  2241.  
  2242.   if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context)
  2243.     {
  2244.       /* Let's be nice to the user for now, and give reasonable
  2245.      default behavior.  */
  2246.       instance_ptr = current_class_decl;
  2247.       if (instance_ptr)
  2248.     {
  2249.       if (basetype != current_class_type)
  2250.         {
  2251.           basetype = get_base_type (basetype, current_class_type, 1);
  2252.           if (basetype == 0)
  2253.         {
  2254.           error_not_base_type (DECL_CONTEXT (function), current_class_type);
  2255.           return error_mark_node;
  2256.         }
  2257.           else if (basetype == error_mark_node)
  2258.         return error_mark_node;
  2259.         }
  2260.     }
  2261.       else
  2262.     {
  2263.       error_with_aggr_type (basetype, "cannot call member function `%s::%s' without object",
  2264.                 err_name);
  2265.       return error_mark_node;
  2266.     }
  2267.     }
  2268.  
  2269.   value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
  2270.  
  2271.   if (TYPE_SIZE (value_type) == 0)
  2272.     {
  2273.       if (flags & LOOKUP_COMPLAIN)
  2274.     incomplete_type_error (0, value_type);
  2275.       return error_mark_node;
  2276.     }
  2277.  
  2278.   /* We do not pass FUNCTION into `convert_arguments', because by
  2279.      now everything should be ok.  If not, then we have a serious error.  */
  2280.   if (DECL_STATIC_FUNCTION_P (function))
  2281.     parms = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
  2282.                    TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL);
  2283.   else if (need_vtbl == unneeded)
  2284.     {
  2285.       int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
  2286.       basetype = TREE_TYPE (instance);
  2287.       if (DECL_CONTEXT (function) != TYPE_MAIN_VARIANT (basetype)
  2288.       && TYPE_USES_COMPLEX_INHERITANCE (basetype))
  2289.     {
  2290.       basetype = DECL_CONTEXT (function);
  2291.       instance_ptr = convert_pointer_to (basetype, instance_ptr);
  2292.       instance = build_indirect_ref (instance_ptr, 0);
  2293.     }
  2294.       parms = tree_cons (NULL_TREE, instance_ptr,
  2295.              convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, sub_flags));
  2296.     }
  2297.   else
  2298.     {
  2299.       if ((flags & LOOKUP_NONVIRTUAL) == 0)
  2300.     basetype = DECL_VCONTEXT (function);
  2301.  
  2302.       /* First parm could be integer_zerop with casts like
  2303.      ((Object*)0)->Object::IsA()  */
  2304.       if (!integer_zerop (TREE_VALUE (parms)))
  2305.     {
  2306.       instance_ptr = convert_pointer_to (build_type_variant (basetype, constp, volatilep),
  2307.                          TREE_VALUE (parms));
  2308.       if (TREE_CODE (instance_ptr) == COND_EXPR)
  2309.         {
  2310.           instance_ptr = save_expr (instance_ptr);
  2311.           instance = build_indirect_ref (instance_ptr);
  2312.         }
  2313.       else if (TREE_CODE (instance_ptr) == NOP_EXPR
  2314.            && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
  2315.            && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
  2316.         ;
  2317.       else if (instance == NULL_TREE
  2318.            || TREE_CODE (instance) != INDIRECT_REF
  2319.            || TREE_OPERAND (instance, 0) != instance_ptr)
  2320.         instance = build_indirect_ref (instance_ptr);
  2321.     }
  2322.       parms = tree_cons (NULL_TREE, instance_ptr,
  2323.              convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL));
  2324.     }
  2325.  
  2326.   /* See if there is a wrapper for this thing.  */
  2327.   if (wrap < 0
  2328.       || static_call_context
  2329.       || name == wrapper_name
  2330.       || name == TYPE_IDENTIFIER (basetype))
  2331.     ;
  2332.   else if (wrap > 0 || TYPE_NEEDS_WRAPPER (basetype))
  2333.     {
  2334.       flags &= ~LOOKUP_PROTECT;
  2335.       if (wrap == 0)
  2336.     {
  2337.       wrap = TYPE_NEEDS_WRAPPER (basetype);
  2338.       /* If no wrapper specified, wrapper may be virtual.  */
  2339.       flags &= ~LOOKUP_NONVIRTUAL;
  2340.     }
  2341.  
  2342.       if (wrap)
  2343.     {
  2344.       tree wrapped_result, unwrapped_result;
  2345.  
  2346.       if (!all_virtual && TREE_CODE (function) == FUNCTION_DECL)
  2347.         parm = build_unary_op (ADDR_EXPR, function, 0);
  2348.       else
  2349.         {
  2350.               fntype = build_cplus_method_type (basetype, TREE_TYPE (fntype), TYPE_ARG_TYPES (fntype));
  2351.           parm = build1 (NOP_EXPR, build_pointer_type (fntype), DECL_VINDEX (function));
  2352.         }
  2353.  
  2354.       if (TYPE_HAS_WRAPPER_PRED (basetype))
  2355.         {
  2356.           unwrapped_result = build_nt (CALL_EXPR, default_conversion (function), parms, NULL_TREE);
  2357.  
  2358.           assert (TREE_OPERAND (unwrapped_result, 1) != error_mark_node);
  2359.  
  2360.           TREE_TYPE (unwrapped_result) = value_type;
  2361.           TREE_SIDE_EFFECTS (unwrapped_result) = 1;
  2362.           TREE_RAISES (unwrapped_result) = !! TYPE_RAISES_EXCEPTIONS (fntype);
  2363.         }
  2364.  
  2365.       /* If this pointer walked as a result of multiple inheritance,
  2366.          keep its displaced value.  */
  2367.       parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
  2368.  
  2369.       wrapped_result = get_wrapper (basetype);
  2370.       assert (wrapped_result != NULL_TREE);
  2371.       assert (wrapped_result != error_mark_node);
  2372.  
  2373.       /* @@ Should BASETYPE_PATH get TREE_PURPOSE (wrapped_result) here?  */
  2374.       wrapped_result
  2375.         = build_method_call (instance,
  2376.                  DECL_ORIGINAL_NAME (TREE_VALUE (wrapped_result)),
  2377.                  parms, basetype_path, flags);
  2378. #if 0
  2379.       /* Do this if we want the result of operator->() to inherit
  2380.          the type of the function it is subbing for.  */
  2381.       if (wrapped_result != error_mark_node)
  2382.         TREE_TYPE (wrapped_result) = value_type;
  2383. #endif
  2384.  
  2385.       if (TYPE_HAS_WRAPPER_PRED (basetype))
  2386.         {
  2387.           result = build_conditional_expr
  2388.         (build_method_call (instance, wrapper_pred_name, build_tree_list (NULL_TREE, parm), basetype_path, LOOKUP_NORMAL),
  2389.          wrapped_result,
  2390.          unwrapped_result);
  2391.  
  2392.         }
  2393.       else
  2394.         {
  2395.           result = wrapped_result;
  2396.         }
  2397.  
  2398.       TREE_SIDE_EFFECTS (result) = 1;
  2399.       return result;
  2400.     }
  2401.     }
  2402.   /* Constructors do not overload method calls.  */
  2403.   else if (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype)
  2404.        && name != TYPE_IDENTIFIER (basetype)
  2405.        && (TREE_CODE (function) != FUNCTION_DECL
  2406.            || strncmp (IDENTIFIER_POINTER (DECL_NAME (function)),
  2407.                OPERATOR_METHOD_FORMAT,
  2408.                OPERATOR_METHOD_LENGTH))
  2409. #if 0
  2410.        && (may_be_remote (basetype)
  2411.            || (C_C_D ? TREE_TYPE (instance) != current_class_type : 1))
  2412. #else
  2413.        /* This change by Larry Ketcham.  */
  2414.          && (may_be_remote (basetype) || instance != C_C_D)
  2415. #endif
  2416.        )
  2417.     {
  2418.       tree fn_as_int;
  2419. #ifdef ESKIT
  2420.       register int bytecount = 0;
  2421.  
  2422.       parms = tree_cons (NULL_TREE, build_int_2 (bytecount, 0),
  2423.              TREE_CHAIN (parms));
  2424. #else
  2425.       parms = TREE_CHAIN (parms);
  2426. #endif
  2427.  
  2428.       if (!all_virtual && TREE_CODE (function) == FUNCTION_DECL)
  2429.     fn_as_int = build_unary_op (ADDR_EXPR, function, 0);
  2430.       else
  2431.     fn_as_int = convert (TREE_TYPE (default_conversion (function)), DECL_VINDEX (function));
  2432.       if (all_virtual == 1)
  2433.     fn_as_int = convert (integer_type_node, fn_as_int);
  2434.  
  2435.       result = build_opfncall (METHOD_CALL_EXPR, LOOKUP_NORMAL, instance, fn_as_int, parms);
  2436.  
  2437.       if (result == NULL_TREE)
  2438.     {
  2439.       compiler_error ("could not overload `operator->()(...)'");
  2440.       return error_mark_node;
  2441.     }
  2442.       else if (result == error_mark_node)
  2443.     return error_mark_node;
  2444.  
  2445. #if 0
  2446.       /* Do this if we want the result of operator->() to inherit
  2447.      the type of the function it is subbing for.  */
  2448.       TREE_TYPE (result) = value_type;
  2449. #endif
  2450.  
  2451. #ifdef ESKIT
  2452.       {
  2453.     int used, size;
  2454.  
  2455.     /* Count the number of bytes of arguements to operator->(),
  2456.        not to the method itself.  In the tally, don't count bytes
  2457.        for pointer to member function or for the bytecount.  */
  2458.     parms = TREE_OPERAND (result, 1);
  2459.     bytecount = get_arglist_len_in_bytes (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (parms))));
  2460.     used = size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_VALUE (parms))));
  2461. #ifdef PUSH_ROUNDING
  2462.     size = PUSH_ROUNDING (size);
  2463. #endif
  2464.     used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
  2465.          / (PARM_BOUNDARY / BITS_PER_UNIT))
  2466.         * (PARM_BOUNDARY / BITS_PER_UNIT));
  2467.     bytecount += used;
  2468.     TREE_CHAIN (TREE_CHAIN (parms))
  2469.       = tree_cons (NULL_TREE, build_int_2 (bytecount, 0),
  2470.                TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (parms))));
  2471.       }
  2472. #endif
  2473.  
  2474.       return result;
  2475.     }
  2476.  
  2477.   if (need_vtbl == needed)
  2478.     {
  2479.       function = build_vfn_ref (&TREE_VALUE (parms), instance, DECL_VINDEX (function));
  2480.       TREE_TYPE (function) = build_pointer_type (fntype);
  2481.     }
  2482. #ifdef SOS
  2483.   else if (basetype && TYPE_DYNAMIC (basetype))
  2484.     {
  2485.       function = build_array_ref (dtbl, DECL_DINDEX (function));
  2486.       TREE_TYPE (function) = build_pointer_type (fntype);
  2487.     }
  2488. #endif
  2489.  
  2490.    if (TREE_CODE(function) == FUNCTION_DECL)
  2491.      GNU_xref_call (current_function_decl,
  2492.             IDENTIFIER_POINTER (DECL_NAME (function)));
  2493.  
  2494.   if (TREE_CODE (function) == FUNCTION_DECL && TREE_INLINE (function))
  2495.     function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
  2496.   else function = default_conversion (function);
  2497.  
  2498.   result =
  2499.     build_nt (CALL_EXPR, function, parms, NULL_TREE);
  2500.  
  2501.   TREE_TYPE (result) = value_type;
  2502.   TREE_SIDE_EFFECTS (result) = 1;
  2503.   TREE_RAISES (result)
  2504.     = TYPE_RAISES_EXCEPTIONS (fntype) || (parms && TREE_RAISES (parms));
  2505.   return result;
  2506. }
  2507.  
  2508. /* Similar to `build_method_call', but for overloaded non-member functions.
  2509.    The name of this function comes through NAME.  The name depends
  2510.    on PARMS.
  2511.  
  2512.    Note that this function must handle simple `C' promotions,
  2513.    as well as variable numbers of arguments (...), and
  2514.    default arguments to boot.
  2515.  
  2516.    If the overloading is successful, we return a treenode which
  2517.    contains the call to the function.
  2518.  
  2519.    If overloading produces candidates which are probabe, but not definite,
  2520.    we hold these candidates.  If FINAL_CP is non-zero, then we are free
  2521.    to assume that final_cp points to enough storage for all candidates that
  2522.    this function might generate.  The `harshness' array is preallocated for
  2523.    the first candidate, but not for subsequent ones.
  2524.  
  2525.    Note that the DECL_RTL of FUNCTION must be made to agree with this
  2526.    function's new name.  */
  2527.  
  2528. tree
  2529. build_overload_call (fnname, parms, complain, final_cp)
  2530.      tree fnname, parms;
  2531.      int complain;
  2532.      struct candidate *final_cp;
  2533. {
  2534.   /* must check for overloading here */
  2535.   tree overload_name, functions, function, parm;
  2536.   tree parmtypes = NULL_TREE, last = NULL_TREE;
  2537.   register tree outer;
  2538.   int length;
  2539.   int parmlength = list_length (parms);
  2540.  
  2541.   struct candidate *candidates, *cp;
  2542.   int rank_for_overload ();
  2543.  
  2544.   if (final_cp)
  2545.     {
  2546.       final_cp[0].evil = 0;
  2547.       final_cp[0].user = 0;
  2548.       final_cp[0].b_or_d = 0;
  2549.       final_cp[0].easy = 0;
  2550.       final_cp[0].function = 0;
  2551.       /* end marker.  */
  2552.       final_cp[1].evil = 1;
  2553.     }
  2554.  
  2555.   for (parm = parms; parm; parm = TREE_CHAIN (parm))
  2556.     {
  2557.       register tree t = TREE_TYPE (TREE_VALUE (parm));
  2558.  
  2559.       if (t == error_mark_node)
  2560.     return error_mark_node;
  2561.       if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == OFFSET_TYPE)
  2562.     {
  2563.       /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
  2564.          Also convert OFFSET_TYPE entities to their normal selves.
  2565.          This eliminates needless calls to `compute_conversion_costs'.  */
  2566.       TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
  2567.       t = TREE_TYPE (TREE_VALUE (parm));
  2568.     }
  2569.       last = build_tree_list (NULL_TREE, t);
  2570.       parmtypes = chainon (parmtypes, last);
  2571.     }
  2572.   if (last)
  2573.     TREE_CHAIN (last) = void_list_node;
  2574.   else
  2575.     parmtypes = void_list_node;
  2576.   overload_name = build_decl_overload (IDENTIFIER_POINTER (fnname), parmtypes, 0);
  2577.  
  2578.   /* Now check to see whether or not we can win.
  2579.      Note that if we are called from `build_method_call',
  2580.      then we cannot have a mis-match, because we would have
  2581.      already found such a winning case.  */
  2582.  
  2583.   if (IDENTIFIER_GLOBAL_VALUE (overload_name))
  2584.     if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (overload_name)) != TREE_LIST)
  2585.       return build_function_call (DECL_MAIN_VARIANT (IDENTIFIER_GLOBAL_VALUE (overload_name)), parms);
  2586.  
  2587.   functions = IDENTIFIER_GLOBAL_VALUE (fnname);
  2588.  
  2589.   if (functions == NULL_TREE)
  2590.     {
  2591.       if (complain)
  2592.     error ("only member functions apply");
  2593.       if (final_cp)
  2594.     final_cp->evil = 1;
  2595.       return error_mark_node;
  2596.     }
  2597.  
  2598.   if (TREE_CODE (functions) == FUNCTION_DECL)
  2599.     {
  2600.       functions = DECL_MAIN_VARIANT (functions);
  2601.       if (final_cp)
  2602.     {
  2603.       /* We are just curious whether this is a viable alternative or not.  */
  2604.       compute_conversion_costs (functions, parms, final_cp, parmlength);
  2605.       return functions;
  2606.     }
  2607.       else
  2608.     return build_function_call (functions, parms);
  2609.     }
  2610.  
  2611.   if (TREE_VALUE (functions) == NULL_TREE)
  2612.     {
  2613.       if (complain)
  2614.     error ("function `%s' declared overloaded, but no instances of that function declared",
  2615.            IDENTIFIER_POINTER (TREE_PURPOSE (functions)));
  2616.       return error_mark_node;
  2617.     }
  2618.  
  2619.   if (TREE_CODE (TREE_VALUE (functions)) == TREE_LIST)
  2620.     {
  2621.       register tree outer;
  2622.       length = 0;
  2623.  
  2624.       /* The list-of-lists should only occur for class things.  */
  2625.       assert (functions == IDENTIFIER_CLASS_VALUE (fnname));
  2626.  
  2627.       for (outer = functions; outer; outer = TREE_CHAIN (outer))
  2628.     {
  2629.       /* member functions.  */
  2630.       length += decl_list_length (TREE_VALUE (TREE_VALUE (outer)));
  2631.       /* friend functions.  */
  2632.       length += list_length (TREE_TYPE (TREE_VALUE (outer)));
  2633.     }
  2634.     }
  2635.   else
  2636.     {
  2637.       length = list_length (functions);
  2638.     }
  2639.  
  2640.   if (final_cp)
  2641.     candidates = final_cp;
  2642.   else
  2643.     candidates = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
  2644.  
  2645.   cp = candidates;
  2646.  
  2647.   assert (TREE_CODE (TREE_VALUE (functions)) != TREE_LIST);
  2648.   /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST.  */
  2649.  
  2650.   for (outer = functions; outer; outer = TREE_CHAIN (outer))
  2651.     {
  2652.       function = TREE_VALUE (outer);
  2653.       if (TREE_CODE (function) != FUNCTION_DECL)
  2654.     {
  2655.       if (TREE_CODE (function) == CONST_DECL)
  2656.         error_with_decl (function, "enumeral value `%s' conflicts with function of same name");
  2657.       else if (TREE_CODE (function) == VAR_DECL)
  2658.         if (TREE_STATIC (function))
  2659.           error_with_decl (function, "variable `%s' conflicts with function of same name");
  2660.         else
  2661.           error_with_decl (function, "constant field `%s' conflicts with function of same name");
  2662.       else if (TREE_CODE (function) == TYPE_DECL)
  2663.         continue;
  2664.       else abort ();
  2665.       error ("at this point in file");
  2666.       continue;
  2667.     }
  2668.       function = DECL_MAIN_VARIANT (function);
  2669.       /* Can't use alloca here, since result might be
  2670.      passed to calling function.  */
  2671.       cp->harshness
  2672.     = (unsigned short *)oballoc ((parmlength+1) * sizeof (short));
  2673.       compute_conversion_costs (function, parms, cp, parmlength);
  2674.       if (cp[0].evil == 0)
  2675.     {
  2676.       cp[1].evil = 1;
  2677.       if (final_cp
  2678.           && cp[0].user == 0 && cp[0].b_or_d == 0
  2679.           && cp[0].easy <= 1)
  2680.         {
  2681.           final_cp[0].easy = cp[0].easy;
  2682.           return function;
  2683.         }
  2684.       cp++;
  2685.     }
  2686.     }
  2687.  
  2688.   if (cp - candidates)
  2689.     {
  2690.       tree rval = error_mark_node;
  2691.  
  2692.       /* Leave marker.  */
  2693.       cp[0].evil = 1;
  2694.       if (cp - candidates > 1)
  2695.     {
  2696.       struct candidate *best_cp
  2697.         = ideal_candidate (NULL_TREE, candidates,
  2698.                    cp - candidates, parms, parmlength);
  2699.       if (best_cp == 0)
  2700.         {
  2701.           if (complain)
  2702.         error ("call of overloaded `%s' is ambiguous", IDENTIFIER_POINTER (fnname));
  2703.           return error_mark_node;
  2704.         }
  2705.       else
  2706.         rval = best_cp->function;
  2707.     }
  2708.       else
  2709.     {
  2710.       cp -= 1;
  2711.       if (cp->evil > 1)
  2712.         {
  2713.           if (complain)
  2714.         error ("type conversion ambiguous");
  2715.         }
  2716.       else
  2717.         rval = cp->function;
  2718.     }
  2719.  
  2720.       if (final_cp)
  2721.     return rval;
  2722.  
  2723.       return build_function_call (rval, parms);
  2724.     }
  2725.   else if (complain)
  2726.     {
  2727.       tree name;
  2728.       char *err_name;
  2729.       /* Initialize name for error reporting.  */
  2730.       if (TREE_CODE (functions) == TREE_LIST)
  2731.     name = TREE_PURPOSE (functions);
  2732.       else
  2733.     name = DECL_ORIGINAL_NAME (functions);
  2734.  
  2735.       if (OPERATOR_NAME_P (name))
  2736.     {
  2737.       char *opname = operator_name_string (name);
  2738.       err_name = (char *)alloca (strlen (opname) + 12);
  2739.       sprintf (err_name, "operator %s", opname);
  2740.     }
  2741.       else
  2742.     err_name = IDENTIFIER_POINTER (name);
  2743.  
  2744.       report_type_mismatch (cp, parms, "function", err_name);
  2745.     }
  2746.   return error_mark_node;
  2747. }
  2748.