"Fossies" - the Fresh Open Source Software Archive

Member "ponyc-0.33.2/src/libponyc/expr/call.c" (3 Feb 2020, 25790 Bytes) of package /linux/misc/ponyc-0.33.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "call.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.33.1_vs_0.33.2.

    1 #include "call.h"
    2 #include "postfix.h"
    3 #include "control.h"
    4 #include "literal.h"
    5 #include "reference.h"
    6 #include "../ast/astbuild.h"
    7 #include "../ast/lexer.h"
    8 #include "../pkg/package.h"
    9 #include "../pass/expr.h"
   10 #include "../pass/sugar.h"
   11 #include "../type/alias.h"
   12 #include "../type/cap.h"
   13 #include "../type/assemble.h"
   14 #include "../type/lookup.h"
   15 #include "../type/reify.h"
   16 #include "../type/safeto.h"
   17 #include "../type/sanitise.h"
   18 #include "../type/subtype.h"
   19 #include "../type/viewpoint.h"
   20 #include "ponyassert.h"
   21 
   22 static bool insert_apply(pass_opt_t* opt, ast_t** astp)
   23 {
   24   // Sugar .apply()
   25   ast_t* ast = *astp;
   26   AST_GET_CHILDREN(ast, lhs, positional, namedargs, question);
   27 
   28   ast_t* dot = ast_from(ast, TK_DOT);
   29   ast_add(dot, ast_from_string(ast, "apply"));
   30   ast_swap(lhs, dot);
   31   ast_add(dot, lhs);
   32 
   33   if(!expr_dot(opt, &dot))
   34     return false;
   35 
   36   return expr_call(opt, astp);
   37 }
   38 
   39 bool method_check_type_params(pass_opt_t* opt, ast_t** astp)
   40 {
   41   ast_t* lhs = *astp;
   42   ast_t* type = ast_type(lhs);
   43 
   44   if(is_typecheck_error(type))
   45     return false;
   46 
   47   ast_t* typeparams = ast_childidx(type, 1);
   48   pony_assert(ast_id(type) == TK_FUNTYPE);
   49 
   50   if(ast_id(typeparams) == TK_NONE)
   51     return true;
   52 
   53   BUILD(typeargs, ast_parent(lhs), NODE(TK_TYPEARGS));
   54 
   55   if(!reify_defaults(typeparams, typeargs, true, opt))
   56   {
   57     ast_free_unattached(typeargs);
   58     return false;
   59   }
   60 
   61   if(!check_constraints(lhs, typeparams, typeargs, true, opt))
   62   {
   63     ast_free_unattached(typeargs);
   64     return false;
   65   }
   66 
   67   type = reify(type, typeparams, typeargs, opt, true);
   68   typeparams = ast_childidx(type, 1);
   69   ast_replace(&typeparams, ast_from(typeparams, TK_NONE));
   70 
   71   REPLACE(astp, NODE(ast_id(lhs), TREE(lhs) TREE(typeargs)));
   72   ast_settype(*astp, type);
   73 
   74   return true;
   75 }
   76 
   77 static bool extend_positional_args(pass_opt_t* opt, ast_t* params,
   78   ast_t* positional)
   79 {
   80   // Fill out the positional args to be as long as the param list.
   81   size_t param_len = ast_childcount(params);
   82   size_t arg_len = ast_childcount(positional);
   83 
   84   if(arg_len > param_len)
   85   {
   86     ast_error(opt->check.errors, positional, "too many arguments");
   87     ast_error_continue(opt->check.errors, params, "definition is here");
   88     return false;
   89   }
   90 
   91   while(arg_len < param_len)
   92   {
   93     ast_setid(positional, TK_POSITIONALARGS);
   94     ast_append(positional, ast_from(positional, TK_NONE));
   95     arg_len++;
   96   }
   97 
   98   return true;
   99 }
  100 
  101 static bool apply_named_args(pass_opt_t* opt, ast_t* params, ast_t* positional,
  102   ast_t* namedargs)
  103 {
  104   ast_t* namedarg = ast_pop(namedargs);
  105 
  106   while(namedarg != NULL)
  107   {
  108     AST_GET_CHILDREN(namedarg, arg_id, arg);
  109 
  110     ast_t* param = ast_child(params);
  111     size_t param_index = 0;
  112 
  113     while(param != NULL)
  114     {
  115       AST_GET_CHILDREN(param, param_id);
  116 
  117       if(ast_name(arg_id) == ast_name(param_id))
  118         break;
  119 
  120       param = ast_sibling(param);
  121       param_index++;
  122     }
  123 
  124     if(param == NULL)
  125     {
  126       if(ast_id(namedarg) == TK_UPDATEARG)
  127       {
  128         ast_error(opt->check.errors, arg_id,
  129           "cannot use sugar, update() has no parameter named \"value\"");
  130         return false;
  131       }
  132 
  133       ast_error(opt->check.errors, arg_id, "not a parameter name");
  134       return false;
  135     }
  136 
  137     ast_t* arg_replace = ast_childidx(positional, param_index);
  138 
  139     if(ast_id(arg_replace) != TK_NONE)
  140     {
  141       ast_error(opt->check.errors, arg_id,
  142         "named argument is already supplied");
  143       ast_error_continue(opt->check.errors, arg_replace,
  144         "supplied argument is here");
  145       return false;
  146     }
  147 
  148     // Extract named argument expression to avoid copying it
  149     ast_free(ast_pop(namedarg));  // ID
  150     arg = ast_pop(namedarg);  // Expression
  151 
  152     ast_replace(&arg_replace, arg);
  153     namedarg = ast_pop(namedargs);
  154   }
  155 
  156   ast_setid(namedargs, TK_NONE);
  157   return true;
  158 }
  159 
  160 static bool apply_default_arg(pass_opt_t* opt, ast_t* param, ast_t** argp)
  161 {
  162   // Pick up a default argument.
  163   AST_GET_CHILDREN(param, id, type, def_arg);
  164 
  165   if(ast_id(def_arg) == TK_NONE)
  166   {
  167     ast_error(opt->check.errors, *argp, "not enough arguments");
  168     ast_error_continue(opt->check.errors, param, "definition is here");
  169     return false;
  170   }
  171 
  172   pony_assert(ast_id(def_arg) == TK_SEQ);
  173 
  174   if(ast_id(ast_child(def_arg)) == TK_LOCATION)
  175   {
  176     // Default argument is __loc. Expand call location.
  177     ast_t* arg = *argp;
  178     ast_t* location = expand_location(arg);
  179     ast_add(arg, location);
  180     ast_setid(arg, TK_SEQ);
  181 
  182     if(!ast_passes_subtree(&location, opt, PASS_EXPR))
  183       return false;
  184   }
  185   else
  186   {
  187     // Just use default argument.
  188     ast_replace(argp, def_arg);
  189   }
  190 
  191   if(!ast_passes_subtree(argp, opt, PASS_EXPR))
  192     return false;
  193 
  194   return true;
  195 }
  196 
  197 static bool check_arg_types(pass_opt_t* opt, ast_t* params, ast_t* positional,
  198   bool partial)
  199 {
  200   // Check positional args vs params.
  201   ast_t* param = ast_child(params);
  202   ast_t* arg = ast_child(positional);
  203 
  204   while(arg != NULL)
  205   {
  206     if(ast_id(arg) == TK_NONE)
  207     {
  208       if(partial)
  209       {
  210         // Don't check missing arguments for partial application.
  211         arg = ast_sibling(arg);
  212         param = ast_sibling(param);
  213         continue;
  214       } else {
  215         // Pick up a default argument if we can.
  216         if(!apply_default_arg(opt, param, &arg))
  217           return false;
  218       }
  219     }
  220 
  221     ast_t* p_type = ast_childidx(param, 1);
  222 
  223     if(!coerce_literals(&arg, p_type, opt))
  224       return false;
  225 
  226     ast_t* arg_type = ast_type(arg);
  227 
  228     if(is_typecheck_error(arg_type))
  229       return false;
  230 
  231     if(ast_checkflag(arg, AST_FLAG_JUMPS_AWAY))
  232     {
  233       ast_error(opt->check.errors, arg,
  234         "can't use a control expression in an argument");
  235       return false;
  236     }
  237 
  238     ast_t* a_type = alias(arg_type);
  239     errorframe_t info = NULL;
  240 
  241     if(!is_subtype(a_type, p_type, &info, opt))
  242     {
  243       errorframe_t frame = NULL;
  244       ast_error_frame(&frame, arg, "argument not a subtype of parameter");
  245       ast_error_frame(&frame, arg, "argument type is %s",
  246                       ast_print_type(a_type));
  247       ast_error_frame(&frame, param, "parameter type is %s",
  248                       ast_print_type(p_type));
  249       errorframe_append(&frame, &info);
  250 
  251       if (ast_childcount(arg) > 1)
  252         ast_error_frame(&frame, arg,
  253           "note that arguments must be separated by a comma");
  254 
  255       if(ast_checkflag(ast_type(arg), AST_FLAG_INCOMPLETE))
  256         ast_error_frame(&frame, arg,
  257           "this might be possible if all fields were already defined");
  258 
  259       errorframe_report(&frame, opt->check.errors);
  260       ast_free_unattached(a_type);
  261       return false;
  262     }
  263 
  264     ast_free_unattached(a_type);
  265     arg = ast_sibling(arg);
  266     param = ast_sibling(param);
  267   }
  268 
  269   return true;
  270 }
  271 
  272 static bool auto_recover_call(ast_t* ast, ast_t* receiver_type,
  273   ast_t* positional, ast_t* result)
  274 {
  275   switch(ast_id(ast))
  276   {
  277     case TK_FUNREF:
  278     case TK_FUNAPP:
  279     case TK_FUNCHAIN:
  280       break;
  281 
  282     default:
  283       pony_assert(0);
  284       break;
  285   }
  286 
  287   // We can recover the receiver (ie not alias the receiver type) if all
  288   // arguments are safe and the result is either safe or unused.
  289   // The result of a chained method is always unused.
  290   ast_t* call = ast_parent(ast);
  291   if(is_result_needed(call) && !safe_to_autorecover(receiver_type, result))
  292     return false;
  293 
  294   ast_t* arg = ast_child(positional);
  295 
  296   while(arg != NULL)
  297   {
  298     if(ast_id(arg) != TK_NONE)
  299     {
  300       ast_t* arg_type = ast_type(arg);
  301 
  302       if(is_typecheck_error(arg_type))
  303         return false;
  304 
  305       ast_t* a_type = alias(arg_type);
  306       bool ok = safe_to_autorecover(receiver_type, a_type);
  307       ast_free_unattached(a_type);
  308 
  309       if(!ok)
  310         return false;
  311     }
  312 
  313     arg = ast_sibling(arg);
  314   }
  315 
  316   return true;
  317 }
  318 
  319 static ast_t* method_receiver(ast_t* method)
  320 {
  321   ast_t* receiver = ast_child(method);
  322 
  323   // Dig through function qualification.
  324   if((ast_id(receiver) == TK_FUNREF) || (ast_id(receiver) == TK_FUNAPP) ||
  325      (ast_id(receiver) == TK_FUNCHAIN))
  326     receiver = ast_child(receiver);
  327 
  328   return receiver;
  329 }
  330 
  331 static ast_t* method_receiver_type(ast_t* method)
  332 {
  333   ast_t* receiver = ast_child(method);
  334 
  335   // Dig through function qualification.
  336   if((ast_id(receiver) == TK_FUNREF) || (ast_id(receiver) == TK_FUNAPP) ||
  337      (ast_id(receiver) == TK_FUNCHAIN))
  338     receiver = ast_child(receiver);
  339 
  340   ast_t* r_type = ast_type(receiver);
  341 
  342   return r_type;
  343 }
  344 
  345 static bool check_receiver_cap(pass_opt_t* opt, ast_t* ast, bool* recovered)
  346 {
  347   AST_GET_CHILDREN(ast, lhs, positional, namedargs, question);
  348 
  349   ast_t* type = ast_type(lhs);
  350 
  351   if(is_typecheck_error(type))
  352     return false;
  353 
  354   AST_GET_CHILDREN(type, cap, typeparams, params, result);
  355 
  356   // Receiver type, alias of receiver type, and target type.
  357   ast_t* r_type = method_receiver_type(lhs);
  358 
  359   if(is_typecheck_error(r_type))
  360     return false;
  361 
  362   ast_t* t_type = set_cap_and_ephemeral(r_type, ast_id(cap), TK_NONE);
  363   ast_t* a_type;
  364 
  365   // If we can recover the receiver, we don't alias it here.
  366   bool can_recover = auto_recover_call(lhs, r_type, positional, result);
  367   bool cap_recover = false;
  368 
  369   switch(ast_id(cap))
  370   {
  371     case TK_ISO:
  372     case TK_TRN:
  373     case TK_VAL:
  374     case TK_TAG:
  375       break;
  376 
  377     case TK_REF:
  378     case TK_BOX:
  379       cap_recover = true;
  380       break;
  381 
  382     default:
  383       pony_assert(0);
  384   }
  385 
  386   if(can_recover && cap_recover)
  387   {
  388     a_type = r_type;
  389     if(recovered != NULL)
  390       *recovered = true;
  391   }
  392   else
  393   {
  394     a_type = alias(r_type);
  395     if(recovered != NULL)
  396       *recovered = false;
  397   }
  398 
  399   errorframe_t info = NULL;
  400   bool ok = is_subtype(a_type, t_type, &info, opt);
  401 
  402   if(!ok)
  403   {
  404     errorframe_t frame = NULL;
  405 
  406     ast_error_frame(&frame, ast,
  407       "receiver type is not a subtype of target type");
  408 
  409     switch (ast_id(a_type)) { // provide better information if the refcap is `this->*`
  410       case TK_ARROW:
  411         ast_error_frame(&frame, ast_child(lhs),
  412           "receiver type: %s (which becomes '%s' in this context)", ast_print_type(a_type), ast_print_type(viewpoint_upper(a_type)));
  413         break;
  414 
  415       default:
  416         ast_error_frame(&frame, ast_child(lhs),
  417           "receiver type: %s", ast_print_type(a_type));
  418     }
  419 
  420     ast_error_frame(&frame, cap,
  421       "target type: %s", ast_print_type(t_type));
  422     errorframe_append(&frame, &info);
  423 
  424     if(ast_checkflag(ast_type(method_receiver(lhs)), AST_FLAG_INCOMPLETE))
  425       ast_error_frame(&frame, method_receiver(lhs),
  426         "this might be possible if all fields were already defined");
  427 
  428     if(!can_recover && cap_recover && is_subtype(r_type, t_type, NULL, opt))
  429     {
  430       ast_error_frame(&frame, ast,
  431         "this would be possible if the arguments and return value "
  432         "were all sendable");
  433     }
  434 
  435     ast_t* fn = ast_nearest(lhs, TK_FUN);
  436     if (fn != NULL && ast_id(a_type) == TK_ARROW)
  437     {
  438       ast_t* iso = ast_child(fn);
  439       pony_assert(iso != NULL);
  440       token_id iso_id = ast_id(iso);
  441 
  442       ast_t* t_cap = cap_fetch(t_type);
  443       pony_assert(t_cap != NULL);
  444 
  445       if (ast_id(t_cap) == TK_REF && (iso_id == TK_BOX || iso_id == TK_VAL || iso_id == TK_TAG))
  446       {
  447         ast_error_frame(&frame, iso, "you are trying to change state in a %s function; this would be possible in a ref function", lexer_print(iso_id));
  448       }
  449     }
  450 
  451     errorframe_report(&frame, opt->check.errors);
  452   }
  453 
  454   if(a_type != r_type)
  455     ast_free_unattached(a_type);
  456 
  457   ast_free_unattached(r_type);
  458   ast_free_unattached(t_type);
  459   return ok;
  460 }
  461 
  462 static bool is_receiver_safe(typecheck_t* t, ast_t* ast)
  463 {
  464   switch(ast_id(ast))
  465   {
  466      case TK_THIS:
  467      case TK_FLETREF:
  468      case TK_FVARREF:
  469      case TK_EMBEDREF:
  470      case TK_PARAMREF:
  471      case TK_TUPLEELEMREF:
  472      {
  473        ast_t* type = ast_type(ast);
  474        return sendable(type);
  475      }
  476 
  477      case TK_LETREF:
  478      case TK_VARREF:
  479      {
  480        ast_t* def = (ast_t*)ast_data(ast);
  481        pony_assert(def != NULL);
  482        ast_t* def_recover = ast_nearest(def, TK_RECOVER);
  483        if(t->frame->recover == def_recover)
  484          return true;
  485        ast_t* type = ast_type(ast);
  486        return sendable(type);
  487      }
  488 
  489      default:
  490        // Unsafe receivers inside expressions are catched before we get there.
  491        return true;
  492   }
  493 }
  494 
  495 static bool check_nonsendable_recover(pass_opt_t* opt, ast_t* ast)
  496 {
  497   if(opt->check.frame->recover != NULL)
  498   {
  499     AST_GET_CHILDREN(ast, lhs, positional, namedargs, question);
  500 
  501     ast_t* type = ast_type(lhs);
  502 
  503     AST_GET_CHILDREN(type, cap, typeparams, params, result);
  504 
  505     // If the method is tag, the call is always safe.
  506     if(ast_id(cap) == TK_TAG)
  507       return true;
  508 
  509     ast_t* receiver = ast_child(lhs);
  510 
  511     // Dig through function qualification.
  512     if((ast_id(receiver) == TK_FUNREF) || (ast_id(receiver) == TK_FUNAPP) ||
  513        (ast_id(receiver) == TK_FUNCHAIN))
  514       receiver = ast_child(receiver);
  515 
  516     if(!is_receiver_safe(&opt->check, receiver))
  517     {
  518       ast_t* arg = ast_child(positional);
  519       bool args_sendable = true;
  520       while(arg != NULL)
  521       {
  522         if(ast_id(arg) != TK_NONE)
  523         {
  524           // Don't typecheck arg_type, this was already done in
  525           // auto_recover_call.
  526           ast_t* arg_type = ast_type(arg);
  527           if(!sendable(arg_type))
  528           {
  529             args_sendable = false;
  530             break;
  531           }
  532         }
  533         arg = ast_sibling(arg);
  534       }
  535       if(!args_sendable || !sendable(result))
  536       {
  537         ast_error(opt->check.errors, ast, "can't call method on non-sendable "
  538           "object inside of a recover expression");
  539         ast_error_continue(opt->check.errors, ast, "this would be possible if "
  540           "the arguments and return value were all sendable");
  541         return false;
  542       }
  543     }
  544   }
  545   return true;
  546 }
  547 
  548 static bool method_application(pass_opt_t* opt, ast_t* ast, bool partial)
  549 {
  550   AST_GET_CHILDREN(ast, lhs, positional, namedargs, question);
  551 
  552   if(!method_check_type_params(opt, &lhs))
  553     return false;
  554 
  555   ast_t* type = ast_type(lhs);
  556 
  557   if(is_typecheck_error(type))
  558     return false;
  559 
  560   AST_GET_CHILDREN(type, cap, typeparams, params, result);
  561 
  562   if(!extend_positional_args(opt, params, positional))
  563     return false;
  564 
  565   if(!apply_named_args(opt, params, positional, namedargs))
  566     return false;
  567 
  568   if(!check_arg_types(opt, params, positional, partial))
  569     return false;
  570 
  571   switch(ast_id(lhs))
  572   {
  573     case TK_FUNREF:
  574     case TK_FUNAPP:
  575       if(ast_id(ast_child(type)) != TK_AT)
  576       {
  577         if(!check_receiver_cap(opt, ast, NULL))
  578           return false;
  579 
  580         if(!check_nonsendable_recover(opt, ast))
  581           return false;
  582       } else {
  583         ast_t* receiver = ast_child(lhs);
  584 
  585         // Dig through function qualification.
  586         if((ast_id(receiver) == TK_FUNREF) || (ast_id(receiver) == TK_FUNAPP) ||
  587            (ast_id(receiver) == TK_FUNCHAIN))
  588           receiver = ast_child(receiver);
  589 
  590         ast_t* recv_type = ast_type(receiver);
  591         if(!is_known(recv_type) && (ast_id(receiver) == TK_TYPEREF))
  592         {
  593           ast_error(opt->check.errors, lhs, "a bare method cannot be called on "
  594             "an abstract type reference");
  595           return false;
  596         }
  597       }
  598 
  599       break;
  600 
  601     default: {}
  602   }
  603 
  604   return true;
  605 }
  606 
  607 static bool method_call(pass_opt_t* opt, ast_t* ast)
  608 {
  609   if(!method_application(opt, ast, false))
  610     return false;
  611 
  612   AST_GET_CHILDREN(ast, lhs, positional, namedargs, question);
  613   ast_t* type = ast_type(lhs);
  614 
  615   if(is_typecheck_error(type))
  616     return false;
  617 
  618   AST_GET_CHILDREN(type, cap, typeparams, params, result);
  619   ast_settype(ast, result);
  620 
  621   return true;
  622 }
  623 
  624 static token_id partial_application_cap(pass_opt_t* opt, ast_t* ftype,
  625   ast_t* receiver, ast_t* positional)
  626 {
  627   // Check if the apply method in the generated object literal can accept a box
  628   // receiver. If not, it must be a ref receiver. It can accept a box receiver
  629   // if box->receiver <: lhs->receiver and box->arg <: lhs->param.
  630   AST_GET_CHILDREN(ftype, cap, typeparams, params, result);
  631 
  632   ast_t* type = ast_type(receiver);
  633   ast_t* view_type = viewpoint_type(ast_from(type, TK_BOX), type);
  634   ast_t* need_type = set_cap_and_ephemeral(type, ast_id(cap), TK_NONE);
  635 
  636   bool ok = is_subtype(view_type, need_type, NULL, opt);
  637   ast_free_unattached(view_type);
  638   ast_free_unattached(need_type);
  639 
  640   if(!ok)
  641     return TK_REF;
  642 
  643   ast_t* param = ast_child(params);
  644   ast_t* arg = ast_child(positional);
  645 
  646   while(arg != NULL)
  647   {
  648     if(ast_id(arg) != TK_NONE)
  649     {
  650       type = ast_type(arg);
  651       view_type = viewpoint_type(ast_from(type, TK_BOX), type);
  652       need_type = ast_childidx(param, 1);
  653 
  654       ok = is_subtype(view_type, need_type, NULL, opt);
  655       ast_free_unattached(view_type);
  656       ast_free_unattached(need_type);
  657 
  658       if(!ok)
  659         return TK_REF;
  660     }
  661 
  662     arg = ast_sibling(arg);
  663     param = ast_sibling(param);
  664   }
  665 
  666   return TK_BOX;
  667 }
  668 
  669 // Sugar for partial application, which we convert to a lambda.
  670 static bool partial_application(pass_opt_t* opt, ast_t** astp)
  671 {
  672   /* Example that we refer to throughout this function.
  673    * ```pony
  674    * class C
  675    *   fun f[T](a: A, b: B = b_default): R
  676    *
  677    * let recv: T = ...
  678    * recv~f[T2](foo)
  679    * ```
  680    *
  681    * Partial call is converted to:
  682    * ```pony
  683    * {(b: B = b_default)($0 = recv, a = foo): R => $0.f[T2](a, consume b) }
  684    * ```
  685    */
  686 
  687   ast_t* ast = *astp;
  688   typecheck_t* t = &opt->check;
  689 
  690   if(!method_application(opt, ast, true))
  691     return false;
  692 
  693   AST_GET_CHILDREN(ast, lhs, positional, namedargs, question);
  694 
  695   // LHS must be an application, possibly wrapped in another application
  696   // if the method had type parameters for qualification.
  697   pony_assert(ast_id(lhs) == TK_FUNAPP || ast_id(lhs) == TK_BEAPP ||
  698     ast_id(lhs) == TK_NEWAPP);
  699   AST_GET_CHILDREN(lhs, receiver, method);
  700   ast_t* type_args = NULL;
  701 
  702   if(ast_id(receiver) == ast_id(lhs))
  703   {
  704     type_args = method;
  705     AST_GET_CHILDREN_NO_DECL(receiver, receiver, method);
  706   }
  707 
  708   // Look up the original method definition for this method call.
  709   deferred_reification_t* method_def = lookup(opt, lhs, ast_type(receiver),
  710     ast_name(method));
  711   ast_t* method_ast = method_def->ast;
  712 
  713   // The deferred reification doesn't own the underlying AST so we can free it
  714   // safely.
  715   deferred_reify_free(method_def);
  716 
  717   pony_assert(ast_id(method_ast) == TK_FUN || ast_id(method_ast) == TK_BE ||
  718     ast_id(method_ast) == TK_NEW);
  719 
  720   // The TK_FUNTYPE of the LHS.
  721   ast_t* type = ast_type(lhs);
  722   pony_assert(ast_id(type) == TK_FUNTYPE);
  723 
  724   if(is_typecheck_error(type))
  725     return false;
  726 
  727   AST_GET_CHILDREN(type, cap, type_params, target_params, result);
  728 
  729   bool bare = ast_id(cap) == TK_AT;
  730 
  731   token_id apply_cap = TK_AT;
  732   if(!bare)
  733     apply_cap = partial_application_cap(opt, type, receiver, positional);
  734 
  735   token_id can_error = ast_id(ast_childidx(method_ast, 5));
  736   const char* recv_name = package_hygienic_id(t);
  737 
  738   // Build lambda expression.
  739   ast_t* call_receiver = NULL;
  740   if(bare)
  741   {
  742     ast_t* arg = ast_child(positional);
  743     while(arg != NULL)
  744     {
  745       if(ast_id(arg) != TK_NONE)
  746       {
  747         ast_error(opt->check.errors, arg, "the partial application of a bare "
  748           "method cannot take arguments");
  749         return false;
  750       }
  751 
  752       arg = ast_sibling(arg);
  753     }
  754 
  755     ast_t* receiver_type = ast_type(receiver);
  756     if(is_bare(receiver_type))
  757     {
  758       // Partial application on a bare object, simply return the object itself.
  759       ast_replace(astp, receiver);
  760       return true;
  761     }
  762 
  763     AST_GET_CHILDREN(receiver_type, recv_type_package, recv_type_name);
  764 
  765     const char* recv_package_str = ast_name(recv_type_package);
  766     const char* recv_name_str = ast_name(recv_type_name);
  767 
  768     ast_t* module = ast_nearest(ast, TK_MODULE);
  769     ast_t* package = ast_parent(module);
  770     ast_t* pkg_id = package_id(package);
  771     const char* pkg_str = ast_name(pkg_id);
  772 
  773     const char* pkg_alias = NULL;
  774 
  775     if(recv_package_str != pkg_str)
  776       pkg_alias = package_alias_from_id(module, recv_package_str);
  777 
  778     ast_free_unattached(pkg_id);
  779 
  780     if(pkg_alias != NULL)
  781     {
  782       // `package.Type.f`
  783       BUILD_NO_DECL(call_receiver, ast,
  784         NODE(TK_DOT,
  785           NODE(TK_DOT,
  786             NODE(TK_REFERENCE, ID(pkg_alias))
  787             ID(recv_name_str))
  788           TREE(method)));
  789     } else {
  790       // `Type.f`
  791       BUILD_NO_DECL(call_receiver, ast,
  792         NODE(TK_DOT,
  793           NODE(TK_REFERENCE, ID(recv_name_str))
  794           TREE(method)));
  795     }
  796   } else {
  797     // `$0.f`
  798     BUILD_NO_DECL(call_receiver, ast,
  799       NODE(TK_DOT,
  800         NODE(TK_REFERENCE, ID(recv_name))
  801         TREE(method)));
  802   }
  803 
  804   ast_t* captures = NULL;
  805   if(bare)
  806   {
  807     captures = ast_from(receiver, TK_NONE);
  808   } else {
  809     // Build captures. We always have at least one capture, for receiver.
  810     // Capture: `$0 = recv`
  811     BUILD_NO_DECL(captures, receiver,
  812       NODE(TK_LAMBDACAPTURES,
  813         NODE(TK_LAMBDACAPTURE,
  814           ID(recv_name)
  815           NONE  // Infer type.
  816           TREE(receiver))));
  817   }
  818 
  819   // Process arguments.
  820   ast_t* target_param = ast_child(target_params);
  821   ast_t* lambda_params = ast_from(target_params, TK_NONE);
  822   ast_t* lambda_call_args = ast_from(positional, TK_NONE);
  823   ast_t* given_arg = ast_child(positional);
  824 
  825   while(given_arg != NULL)
  826   {
  827     pony_assert(target_param != NULL);
  828     const char* target_p_name = ast_name(ast_child(target_param));
  829 
  830     if(ast_id(given_arg) == TK_NONE)
  831     {
  832       // This argument is not supplied already, must be a lambda parameter.
  833       // Like `b` in example above.
  834       // Build a new a new TK_PARAM node rather than copying the target one,
  835       // since the target has already been processed to expr pass, and we need
  836       // a clean one.
  837       AST_GET_CHILDREN(target_param, p_id, p_type, p_default);
  838 
  839       // Parameter: `b: B = b_default`
  840       BUILD(lambda_param, target_param,
  841         NODE(TK_PARAM,
  842           TREE(p_id)
  843           TREE(sanitise_type(p_type))
  844           TREE(p_default)));
  845 
  846       ast_append(lambda_params, lambda_param);
  847       ast_setid(lambda_params, TK_PARAMS);
  848 
  849       // Argument: `consume b`
  850       BUILD(target_arg, lambda_param,
  851         NODE(TK_SEQ,
  852           NODE(TK_CONSUME,
  853             NONE
  854             NODE(TK_REFERENCE, ID(target_p_name)))));
  855 
  856       ast_append(lambda_call_args, target_arg);
  857       ast_setid(lambda_call_args, TK_POSITIONALARGS);
  858     }
  859     else
  860     {
  861       // This argument is supplied to the partial, capture it.
  862       // Like `a` in example above.
  863       // Capture: `a = foo`
  864       BUILD(capture, given_arg,
  865         NODE(TK_LAMBDACAPTURE,
  866           ID(target_p_name)
  867           NONE
  868           TREE(given_arg)));
  869 
  870       ast_append(captures, capture);
  871 
  872       // Argument: `a`
  873       BUILD(target_arg, given_arg,
  874         NODE(TK_SEQ,
  875           NODE(TK_REFERENCE, ID(target_p_name))));
  876 
  877       ast_append(lambda_call_args, target_arg);
  878       ast_setid(lambda_call_args, TK_POSITIONALARGS);
  879     }
  880 
  881     given_arg = ast_sibling(given_arg);
  882     target_param = ast_sibling(target_param);
  883   }
  884 
  885   pony_assert(target_param == NULL);
  886 
  887   if(type_args != NULL)
  888   {
  889     // The partial call has type args, add them to the actual call in apply().
  890     // `$0.f[T2]`
  891     BUILD(qualified, type_args,
  892       NODE(TK_QUALIFY,
  893         TREE(call_receiver)
  894         TREE(type_args)));
  895     call_receiver = qualified;
  896   }
  897 
  898   REPLACE(astp,
  899     NODE((bare ? TK_BARELAMBDA : TK_LAMBDA),
  900       NODE(apply_cap)
  901       NONE  // Lambda function name.
  902       NONE  // Lambda type params.
  903       TREE(lambda_params)
  904       TREE(captures)
  905       TREE(sanitise_type(result))
  906       NODE(can_error)
  907       NODE(TK_SEQ,
  908         NODE(TK_CALL,
  909           TREE(call_receiver)
  910           TREE(lambda_call_args)
  911           NONE  // Named args.
  912           NODE(can_error)))
  913       NONE)); // Lambda reference capability.
  914 
  915   // Need to preserve various lambda children.
  916   ast_setflag(ast_childidx(*astp, 2), AST_FLAG_PRESERVE); // Type params.
  917   ast_setflag(ast_childidx(*astp, 3), AST_FLAG_PRESERVE); // Parameters.
  918   ast_setflag(ast_childidx(*astp, 5), AST_FLAG_PRESERVE); // Return type.
  919   ast_setflag(ast_childidx(*astp, 7), AST_FLAG_PRESERVE); // Body.
  920 
  921   // Catch up to this pass.
  922   return ast_passes_subtree(astp, opt, PASS_EXPR);
  923 }
  924 
  925 static bool method_chain(pass_opt_t* opt, ast_t* ast)
  926 {
  927   if(!method_application(opt, ast, false))
  928     return false;
  929 
  930   AST_GET_CHILDREN(ast, lhs, positional, namedargs, question);
  931 
  932   ast_t* type = ast_type(lhs);
  933   if(ast_id(ast_child(type)) == TK_AT)
  934   {
  935     ast_error(opt->check.errors, ast, "a bare method cannot be chained");
  936     return false;
  937   }
  938 
  939   // We check the receiver cap now instead of in method_application because
  940   // we need to know whether the receiver was recovered.
  941   ast_t* r_type = method_receiver_type(lhs);
  942   if(ast_id(lhs) == TK_FUNCHAIN)
  943   {
  944     bool recovered;
  945     if(!check_receiver_cap(opt, ast, &recovered))
  946       return false;
  947 
  948     if(!check_nonsendable_recover(opt, ast))
  949       return false;
  950 
  951     ast_t* f_type = ast_type(lhs);
  952     token_id f_cap = ast_id(ast_child(f_type));
  953 
  954     ast_t* c_type = chain_type(r_type, f_cap, recovered);
  955     ast_settype(ast, c_type);
  956   } else {
  957     ast_settype(ast, r_type);
  958   }
  959 
  960   return true;
  961 }
  962 
  963 bool expr_call(pass_opt_t* opt, ast_t** astp)
  964 {
  965   ast_t* ast = *astp;
  966 
  967   if(!literal_call(ast, opt))
  968     return false;
  969 
  970   // Type already set by literal handler. Check for infertype, which is a
  971   // marker for typechecking default arguments.
  972   ast_t* type = ast_type(ast);
  973 
  974   if((type != NULL) && (ast_id(type) != TK_INFERTYPE))
  975     return true;
  976 
  977   AST_GET_CHILDREN(ast, lhs, positional, namedargs, question);
  978 
  979   switch(ast_id(lhs))
  980   {
  981     case TK_NEWREF:
  982     case TK_NEWBEREF:
  983     case TK_BEREF:
  984     case TK_FUNREF:
  985       return method_call(opt, ast);
  986 
  987     case TK_NEWAPP:
  988     case TK_BEAPP:
  989     case TK_FUNAPP:
  990       return partial_application(opt, astp);
  991 
  992     case TK_BECHAIN:
  993     case TK_FUNCHAIN:
  994       return method_chain(opt, ast);
  995 
  996     default: {}
  997   }
  998 
  999   return insert_apply(opt, astp);
 1000 }