home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / gcc / cp / call.c next >
C/C++ Source or Header  |  1996-08-23  |  87KB  |  3,000 lines

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