1 /* vi:set ts=8 sts=4 sw=4 noet: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 10 /* 11 * vim9compile.c: :def and dealing with instructions 12 */ 13 14 #define USING_FLOAT_STUFF 15 #include "vim.h" 16 17 #if defined(FEAT_EVAL) || defined(PROTO) 18 19 #ifdef VMS 20 # include <float.h> 21 #endif 22 23 #define DEFINE_VIM9_GLOBALS 24 #include "vim9.h" 25 26 // values for ctx_skip 27 typedef enum { 28 SKIP_NOT, // condition is a constant, produce code 29 SKIP_YES, // condition is a constant, do NOT produce code 30 SKIP_UNKNOWN // condition is not a constant, produce code 31 } skip_T; 32 33 /* 34 * Chain of jump instructions where the end label needs to be set. 35 */ 36 typedef struct endlabel_S endlabel_T; 37 struct endlabel_S { 38 endlabel_T *el_next; // chain end_label locations 39 int el_end_label; // instruction idx where to set end 40 }; 41 42 /* 43 * info specific for the scope of :if / elseif / else 44 */ 45 typedef struct { 46 int is_seen_else; 47 int is_seen_skip_not; // a block was unconditionally executed 48 int is_had_return; // every block ends in :return 49 int is_if_label; // instruction idx at IF or ELSEIF 50 endlabel_T *is_end_label; // instructions to set end label 51 } ifscope_T; 52 53 /* 54 * info specific for the scope of :while 55 */ 56 typedef struct { 57 int ws_top_label; // instruction idx at WHILE 58 endlabel_T *ws_end_label; // instructions to set end 59 } whilescope_T; 60 61 /* 62 * info specific for the scope of :for 63 */ 64 typedef struct { 65 int fs_top_label; // instruction idx at FOR 66 endlabel_T *fs_end_label; // break instructions 67 } forscope_T; 68 69 /* 70 * info specific for the scope of :try 71 */ 72 typedef struct { 73 int ts_try_label; // instruction idx at TRY 74 endlabel_T *ts_end_label; // jump to :finally or :endtry 75 int ts_catch_label; // instruction idx of last CATCH 76 int ts_caught_all; // "catch" without argument encountered 77 } tryscope_T; 78 79 typedef enum { 80 NO_SCOPE, 81 IF_SCOPE, 82 WHILE_SCOPE, 83 FOR_SCOPE, 84 TRY_SCOPE, 85 BLOCK_SCOPE 86 } scopetype_T; 87 88 /* 89 * Info for one scope, pointed to by "ctx_scope". 90 */ 91 typedef struct scope_S scope_T; 92 struct scope_S { 93 scope_T *se_outer; // scope containing this one 94 scopetype_T se_type; 95 int se_local_count; // ctx_locals.ga_len before scope 96 skip_T se_skip_save; // ctx_skip before the block 97 union { 98 ifscope_T se_if; 99 whilescope_T se_while; 100 forscope_T se_for; 101 tryscope_T se_try; 102 } se_u; 103 }; 104 105 /* 106 * Entry for "ctx_locals". Used for arguments and local variables. 107 */ 108 typedef struct { 109 char_u *lv_name; 110 type_T *lv_type; 111 int lv_idx; // index of the variable on the stack 112 int lv_from_outer; // nesting level, using ctx_outer scope 113 int lv_const; // when TRUE cannot be assigned to 114 int lv_arg; // when TRUE this is an argument 115 } lvar_T; 116 117 // Destination for an assignment or ":unlet" with an index. 118 typedef enum { 119 dest_local, 120 dest_option, 121 dest_env, 122 dest_global, 123 dest_buffer, 124 dest_window, 125 dest_tab, 126 dest_vimvar, 127 dest_script, 128 dest_reg, 129 dest_expr, 130 } assign_dest_T; 131 132 // Used by compile_lhs() to store information about the LHS of an assignment 133 // and one argument of ":unlet" with an index. 134 typedef struct { 135 assign_dest_T lhs_dest; // type of destination 136 137 char_u *lhs_name; // allocated name excluding the last 138 // "[expr]" or ".name". 139 size_t lhs_varlen; // length of the variable without 140 // "[expr]" or ".name" 141 char_u *lhs_whole; // allocated name including the last 142 // "[expr]" or ".name" for :redir 143 size_t lhs_varlen_total; // length of the variable including 144 // any "[expr]" or ".name" 145 char_u *lhs_dest_end; // end of the destination, including 146 // "[expr]" or ".name". 147 148 int lhs_has_index; // has "[expr]" or ".name" 149 150 int lhs_new_local; // create new local variable 151 int lhs_opt_flags; // for when destination is an option 152 int lhs_vimvaridx; // for when destination is a v:var 153 154 lvar_T lhs_local_lvar; // used for existing local destination 155 lvar_T lhs_arg_lvar; // used for argument destination 156 lvar_T *lhs_lvar; // points to destination lvar 157 int lhs_scriptvar_sid; 158 int lhs_scriptvar_idx; 159 160 int lhs_has_type; // type was specified 161 type_T *lhs_type; 162 type_T *lhs_member_type; 163 164 int lhs_append; // used by ISN_REDIREND 165 } lhs_T; 166 167 /* 168 * Context for compiling lines of Vim script. 169 * Stores info about the local variables and condition stack. 170 */ 171 struct cctx_S { 172 ufunc_T *ctx_ufunc; // current function 173 int ctx_lnum; // line number in current function 174 char_u *ctx_line_start; // start of current line or NULL 175 garray_T ctx_instr; // generated instructions 176 177 int ctx_prev_lnum; // line number below previous command, for 178 // debugging 179 180 compiletype_T ctx_compile_type; 181 182 garray_T ctx_locals; // currently visible local variables 183 184 int ctx_has_closure; // set to one if a closures was created in 185 // the function 186 187 garray_T ctx_imports; // imported items 188 189 skip_T ctx_skip; 190 scope_T *ctx_scope; // current scope, NULL at toplevel 191 int ctx_had_return; // last seen statement was "return" 192 193 cctx_T *ctx_outer; // outer scope for lambda or nested 194 // function 195 int ctx_outer_used; // var in ctx_outer was used 196 197 garray_T ctx_type_stack; // type of each item on the stack 198 garray_T *ctx_type_list; // list of pointers to allocated types 199 200 int ctx_has_cmdmod; // ISN_CMDMOD was generated 201 202 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when 203 // lhs_name is not NULL 204 }; 205 206 static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted); 207 208 /* 209 * Lookup variable "name" in the local scope and return it in "lvar". 210 * "lvar->lv_from_outer" is incremented accordingly. 211 * If "lvar" is NULL only check if the variable can be found. 212 * Return FAIL if not found. 213 */ 214 static int 215 lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx) 216 { 217 int idx; 218 lvar_T *lvp; 219 220 if (len == 0) 221 return FAIL; 222 223 // Find local in current function scope. 224 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx) 225 { 226 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; 227 if (STRNCMP(name, lvp->lv_name, len) == 0 228 && STRLEN(lvp->lv_name) == len) 229 { 230 if (lvar != NULL) 231 { 232 *lvar = *lvp; 233 lvar->lv_from_outer = 0; 234 } 235 return OK; 236 } 237 } 238 239 // Find local in outer function scope. 240 if (cctx->ctx_outer != NULL) 241 { 242 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK) 243 { 244 if (lvar != NULL) 245 { 246 cctx->ctx_outer_used = TRUE; 247 ++lvar->lv_from_outer; 248 } 249 return OK; 250 } 251 } 252 253 return FAIL; 254 } 255 256 /* 257 * Lookup an argument in the current function and an enclosing function. 258 * Returns the argument index in "idxp" 259 * Returns the argument type in "type" 260 * Sets "gen_load_outer" to TRUE if found in outer scope. 261 * Returns OK when found, FAIL otherwise. 262 */ 263 static int 264 arg_exists( 265 char_u *name, 266 size_t len, 267 int *idxp, 268 type_T **type, 269 int *gen_load_outer, 270 cctx_T *cctx) 271 { 272 int idx; 273 char_u *va_name; 274 275 if (len == 0) 276 return FAIL; 277 for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx) 278 { 279 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx); 280 281 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL) 282 { 283 if (idxp != NULL) 284 { 285 // Arguments are located above the frame pointer. One further 286 // if there is a vararg argument 287 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len 288 + STACK_FRAME_SIZE) 289 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0); 290 291 if (cctx->ctx_ufunc->uf_arg_types != NULL) 292 *type = cctx->ctx_ufunc->uf_arg_types[idx]; 293 else 294 *type = &t_any; 295 } 296 return OK; 297 } 298 } 299 300 va_name = cctx->ctx_ufunc->uf_va_name; 301 if (va_name != NULL 302 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL) 303 { 304 if (idxp != NULL) 305 { 306 // varargs is always the last argument 307 *idxp = -STACK_FRAME_SIZE - 1; 308 *type = cctx->ctx_ufunc->uf_va_type; 309 } 310 return OK; 311 } 312 313 if (cctx->ctx_outer != NULL) 314 { 315 // Lookup the name for an argument of the outer function. 316 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer) 317 == OK) 318 { 319 if (gen_load_outer != NULL) 320 ++*gen_load_outer; 321 return OK; 322 } 323 } 324 325 return FAIL; 326 } 327 328 /* 329 * Lookup a script-local variable in the current script, possibly defined in a 330 * block that contains the function "cctx->ctx_ufunc". 331 * "cctx" is NULL at the script level. 332 * If "len" is <= 0 "name" must be NUL terminated. 333 * Return NULL when not found. 334 */ 335 static sallvar_T * 336 find_script_var(char_u *name, size_t len, cctx_T *cctx) 337 { 338 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); 339 hashitem_T *hi; 340 int cc; 341 sallvar_T *sav; 342 sallvar_T *found_sav; 343 ufunc_T *ufunc; 344 345 // Find the list of all script variables with the right name. 346 if (len > 0) 347 { 348 cc = name[len]; 349 name[len] = NUL; 350 } 351 hi = hash_find(&si->sn_all_vars.dv_hashtab, name); 352 if (len > 0) 353 name[len] = cc; 354 if (HASHITEM_EMPTY(hi)) 355 return NULL; 356 357 sav = HI2SAV(hi); 358 if (sav->sav_block_id == 0) 359 // variable defined in the top script scope is always visible 360 return sav; 361 362 if (cctx == NULL) 363 { 364 // Not in a function scope, find variable with block id equal to or 365 // smaller than the current block id. 366 while (sav != NULL) 367 { 368 if (sav->sav_block_id <= si->sn_current_block_id) 369 break; 370 sav = sav->sav_next; 371 } 372 return sav; 373 } 374 375 // Go over the variables with this name and find one that was visible 376 // from the function. 377 ufunc = cctx->ctx_ufunc; 378 found_sav = sav; 379 while (sav != NULL) 380 { 381 int idx; 382 383 // Go over the blocks that this function was defined in. If the 384 // variable block ID matches it was visible to the function. 385 for (idx = 0; idx < ufunc->uf_block_depth; ++idx) 386 if (ufunc->uf_block_ids[idx] == sav->sav_block_id) 387 return sav; 388 sav = sav->sav_next; 389 } 390 391 // Not found, assume variable at script level was visible. 392 return found_sav; 393 } 394 395 /* 396 * Return TRUE if the script context is Vim9 script. 397 */ 398 static int 399 script_is_vim9() 400 { 401 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9; 402 } 403 404 /* 405 * Lookup a variable (without s: prefix) in the current script. 406 * "cctx" is NULL at the script level. 407 * Returns OK or FAIL. 408 */ 409 static int 410 script_var_exists(char_u *name, size_t len, cctx_T *cctx) 411 { 412 if (current_sctx.sc_sid <= 0) 413 return FAIL; 414 if (script_is_vim9()) 415 { 416 // Check script variables that were visible where the function was 417 // defined. 418 if (find_script_var(name, len, cctx) != NULL) 419 return OK; 420 } 421 else 422 { 423 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid); 424 dictitem_T *di; 425 int cc; 426 427 // Check script variables that are currently visible 428 cc = name[len]; 429 name[len] = NUL; 430 di = find_var_in_ht(ht, 0, name, TRUE); 431 name[len] = cc; 432 if (di != NULL) 433 return OK; 434 } 435 436 return FAIL; 437 } 438 439 /* 440 * Return TRUE if "name" is a local variable, argument, script variable or 441 * imported. 442 */ 443 static int 444 variable_exists(char_u *name, size_t len, cctx_T *cctx) 445 { 446 return (cctx != NULL 447 && (lookup_local(name, len, NULL, cctx) == OK 448 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)) 449 || script_var_exists(name, len, cctx) == OK 450 || find_imported(name, len, cctx) != NULL; 451 } 452 453 /* 454 * Return TRUE if "name" is a local variable, argument, script variable, 455 * imported or function. 456 */ 457 static int 458 item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx) 459 { 460 int is_global; 461 char_u *p; 462 463 if (variable_exists(name, len, cctx)) 464 return TRUE; 465 466 // This is similar to what is in lookup_scriptitem(): 467 // Find a function, so that a following "->" works. 468 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is 469 // a function call. 470 p = skipwhite(name + len); 471 472 if (name[len] == '(' || (p[0] == '-' && p[1] == '>')) 473 { 474 // Do not check for an internal function, since it might also be a 475 // valid command, such as ":split" versus "split()". 476 // Skip "g:" before a function name. 477 is_global = (name[0] == 'g' && name[1] == ':'); 478 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL; 479 } 480 return FALSE; 481 } 482 483 /* 484 * Check if "p[len]" is already defined, either in script "import_sid" or in 485 * compilation context "cctx". "cctx" is NULL at the script level. 486 * Does not check the global namespace. 487 * If "is_arg" is TRUE the error message is for an argument name. 488 * Return FAIL and give an error if it defined. 489 */ 490 int 491 check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg) 492 { 493 int c = p[len]; 494 ufunc_T *ufunc = NULL; 495 496 // underscore argument is OK 497 if (len == 1 && *p == '_') 498 return OK; 499 500 if (script_var_exists(p, len, cctx) == OK) 501 { 502 if (is_arg) 503 semsg(_(e_argument_already_declared_in_script_str), p); 504 else 505 semsg(_(e_variable_already_declared_in_script_str), p); 506 return FAIL; 507 } 508 509 p[len] = NUL; 510 if ((cctx != NULL 511 && (lookup_local(p, len, NULL, cctx) == OK 512 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK)) 513 || find_imported(p, len, cctx) != NULL 514 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL) 515 { 516 // A local or script-local function can shadow a global function. 517 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0 518 && (!func_is_global(ufunc) 519 || (p[0] == 'g' && p[1] == ':')))) 520 { 521 if (is_arg) 522 semsg(_(e_argument_name_shadows_existing_variable_str), p); 523 else 524 semsg(_(e_name_already_defined_str), p); 525 p[len] = c; 526 return FAIL; 527 } 528 } 529 p[len] = c; 530 return OK; 531 } 532 533 534 ///////////////////////////////////////////////////////////////////// 535 // Following generate_ functions expect the caller to call ga_grow(). 536 537 #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL 538 #define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK 539 540 /* 541 * Generate an instruction without arguments. 542 * Returns a pointer to the new instruction, NULL if failed. 543 */ 544 static isn_T * 545 generate_instr(cctx_T *cctx, isntype_T isn_type) 546 { 547 garray_T *instr = &cctx->ctx_instr; 548 isn_T *isn; 549 550 RETURN_NULL_IF_SKIP(cctx); 551 if (GA_GROW_FAILS(instr, 1)) 552 return NULL; 553 isn = ((isn_T *)instr->ga_data) + instr->ga_len; 554 isn->isn_type = isn_type; 555 isn->isn_lnum = cctx->ctx_lnum + 1; 556 ++instr->ga_len; 557 558 return isn; 559 } 560 561 /* 562 * Generate an instruction without arguments. 563 * "drop" will be removed from the stack. 564 * Returns a pointer to the new instruction, NULL if failed. 565 */ 566 static isn_T * 567 generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) 568 { 569 garray_T *stack = &cctx->ctx_type_stack; 570 571 RETURN_NULL_IF_SKIP(cctx); 572 stack->ga_len -= drop; 573 return generate_instr(cctx, isn_type); 574 } 575 576 /* 577 * Generate instruction "isn_type" and put "type" on the type stack. 578 */ 579 static isn_T * 580 generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) 581 { 582 isn_T *isn; 583 garray_T *stack = &cctx->ctx_type_stack; 584 585 if ((isn = generate_instr(cctx, isn_type)) == NULL) 586 return NULL; 587 588 if (GA_GROW_FAILS(stack, 1)) 589 return NULL; 590 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type; 591 ++stack->ga_len; 592 593 return isn; 594 } 595 596 /* 597 * Generate an ISN_DEBUG instruction. 598 */ 599 static isn_T * 600 generate_instr_debug(cctx_T *cctx) 601 { 602 isn_T *isn; 603 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 604 + cctx->ctx_ufunc->uf_dfunc_idx; 605 606 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL) 607 return NULL; 608 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len; 609 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum; 610 return isn; 611 } 612 613 /* 614 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. 615 * But only for simple types. 616 * When "tolerant" is TRUE convert most types to string, e.g. a List. 617 */ 618 static int 619 may_generate_2STRING(int offset, int tolerant, cctx_T *cctx) 620 { 621 isn_T *isn; 622 isntype_T isntype = ISN_2STRING; 623 garray_T *stack = &cctx->ctx_type_stack; 624 type_T **type; 625 626 RETURN_OK_IF_SKIP(cctx); 627 type = ((type_T **)stack->ga_data) + stack->ga_len + offset; 628 switch ((*type)->tt_type) 629 { 630 // nothing to be done 631 case VAR_STRING: return OK; 632 633 // conversion possible 634 case VAR_SPECIAL: 635 case VAR_BOOL: 636 case VAR_NUMBER: 637 case VAR_FLOAT: 638 break; 639 640 // conversion possible (with runtime check) 641 case VAR_ANY: 642 case VAR_UNKNOWN: 643 isntype = ISN_2STRING_ANY; 644 break; 645 646 // conversion possible when tolerant 647 case VAR_LIST: 648 if (tolerant) 649 { 650 isntype = ISN_2STRING_ANY; 651 break; 652 } 653 // FALLTHROUGH 654 655 // conversion not possible 656 case VAR_VOID: 657 case VAR_BLOB: 658 case VAR_FUNC: 659 case VAR_PARTIAL: 660 case VAR_DICT: 661 case VAR_JOB: 662 case VAR_CHANNEL: 663 case VAR_INSTR: 664 to_string_error((*type)->tt_type); 665 return FAIL; 666 } 667 668 *type = &t_string; 669 if ((isn = generate_instr(cctx, isntype)) == NULL) 670 return FAIL; 671 isn->isn_arg.tostring.offset = offset; 672 isn->isn_arg.tostring.tolerant = tolerant; 673 674 return OK; 675 } 676 677 static int 678 check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) 679 { 680 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) 681 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT 682 || type2 == VAR_ANY))) 683 { 684 if (*op == '+') 685 emsg(_(e_wrong_argument_type_for_plus)); 686 else 687 semsg(_(e_char_requires_number_or_float_arguments), *op); 688 return FAIL; 689 } 690 return OK; 691 } 692 693 /* 694 * Generate instruction for "+". For a list this creates a new list. 695 */ 696 static int 697 generate_add_instr( 698 cctx_T *cctx, 699 vartype_T vartype, 700 type_T *type1, 701 type_T *type2, 702 exprtype_T expr_type) 703 { 704 garray_T *stack = &cctx->ctx_type_stack; 705 isn_T *isn = generate_instr_drop(cctx, 706 vartype == VAR_NUMBER ? ISN_OPNR 707 : vartype == VAR_LIST ? ISN_ADDLIST 708 : vartype == VAR_BLOB ? ISN_ADDBLOB 709 #ifdef FEAT_FLOAT 710 : vartype == VAR_FLOAT ? ISN_OPFLOAT 711 #endif 712 : ISN_OPANY, 1); 713 714 if (vartype != VAR_LIST && vartype != VAR_BLOB 715 && type1->tt_type != VAR_ANY 716 && type2->tt_type != VAR_ANY 717 && check_number_or_float( 718 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL) 719 return FAIL; 720 721 if (isn != NULL) 722 { 723 if (isn->isn_type == ISN_ADDLIST) 724 isn->isn_arg.op.op_type = expr_type; 725 else 726 isn->isn_arg.op.op_type = EXPR_ADD; 727 } 728 729 // When concatenating two lists with different member types the member type 730 // becomes "any". 731 if (vartype == VAR_LIST 732 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST 733 && type1->tt_member != type2->tt_member) 734 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any; 735 736 return isn == NULL ? FAIL : OK; 737 } 738 739 /* 740 * Get the type to use for an instruction for an operation on "type1" and 741 * "type2". If they are matching use a type-specific instruction. Otherwise 742 * fall back to runtime type checking. 743 */ 744 static vartype_T 745 operator_type(type_T *type1, type_T *type2) 746 { 747 if (type1->tt_type == type2->tt_type 748 && (type1->tt_type == VAR_NUMBER 749 || type1->tt_type == VAR_LIST 750 #ifdef FEAT_FLOAT 751 || type1->tt_type == VAR_FLOAT 752 #endif 753 || type1->tt_type == VAR_BLOB)) 754 return type1->tt_type; 755 return VAR_ANY; 756 } 757 758 /* 759 * Generate an instruction with two arguments. The instruction depends on the 760 * type of the arguments. 761 */ 762 static int 763 generate_two_op(cctx_T *cctx, char_u *op) 764 { 765 garray_T *stack = &cctx->ctx_type_stack; 766 type_T *type1; 767 type_T *type2; 768 vartype_T vartype; 769 isn_T *isn; 770 771 RETURN_OK_IF_SKIP(cctx); 772 773 // Get the known type of the two items on the stack. 774 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; 775 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 776 vartype = operator_type(type1, type2); 777 778 switch (*op) 779 { 780 case '+': 781 if (generate_add_instr(cctx, vartype, type1, type2, 782 EXPR_COPY) == FAIL) 783 return FAIL; 784 break; 785 786 case '-': 787 case '*': 788 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, 789 op) == FAIL) 790 return FAIL; 791 if (vartype == VAR_NUMBER) 792 isn = generate_instr_drop(cctx, ISN_OPNR, 1); 793 #ifdef FEAT_FLOAT 794 else if (vartype == VAR_FLOAT) 795 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); 796 #endif 797 else 798 isn = generate_instr_drop(cctx, ISN_OPANY, 1); 799 if (isn != NULL) 800 isn->isn_arg.op.op_type = *op == '*' 801 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; 802 break; 803 804 case '%': if ((type1->tt_type != VAR_ANY 805 && type1->tt_type != VAR_NUMBER) 806 || (type2->tt_type != VAR_ANY 807 && type2->tt_type != VAR_NUMBER)) 808 { 809 emsg(_(e_percent_requires_number_arguments)); 810 return FAIL; 811 } 812 isn = generate_instr_drop(cctx, 813 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); 814 if (isn != NULL) 815 isn->isn_arg.op.op_type = EXPR_REM; 816 break; 817 } 818 819 // correct type of result 820 if (vartype == VAR_ANY) 821 { 822 type_T *type = &t_any; 823 824 #ifdef FEAT_FLOAT 825 // float+number and number+float results in float 826 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) 827 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) 828 type = &t_float; 829 #endif 830 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; 831 } 832 833 return OK; 834 } 835 836 /* 837 * Get the instruction to use for comparing "type1" with "type2" 838 * Return ISN_DROP when failed. 839 */ 840 static isntype_T 841 get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2) 842 { 843 isntype_T isntype = ISN_DROP; 844 845 if (type1 == VAR_UNKNOWN) 846 type1 = VAR_ANY; 847 if (type2 == VAR_UNKNOWN) 848 type2 = VAR_ANY; 849 850 if (type1 == type2) 851 { 852 switch (type1) 853 { 854 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; 855 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; 856 case VAR_NUMBER: isntype = ISN_COMPARENR; break; 857 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; 858 case VAR_STRING: isntype = ISN_COMPARESTRING; break; 859 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; 860 case VAR_LIST: isntype = ISN_COMPARELIST; break; 861 case VAR_DICT: isntype = ISN_COMPAREDICT; break; 862 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; 863 default: isntype = ISN_COMPAREANY; break; 864 } 865 } 866 else if (type1 == VAR_ANY || type2 == VAR_ANY 867 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) 868 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT))) 869 isntype = ISN_COMPAREANY; 870 871 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT) 872 && (isntype == ISN_COMPAREBOOL 873 || isntype == ISN_COMPARESPECIAL 874 || isntype == ISN_COMPARENR 875 || isntype == ISN_COMPAREFLOAT)) 876 { 877 semsg(_(e_cannot_use_str_with_str), 878 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); 879 return ISN_DROP; 880 } 881 if (isntype == ISN_DROP 882 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL 883 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL 884 || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) 885 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL 886 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT 887 && (type1 == VAR_BLOB || type2 == VAR_BLOB 888 || type1 == VAR_LIST || type2 == VAR_LIST)))) 889 { 890 semsg(_(e_cannot_compare_str_with_str), 891 vartype_name(type1), vartype_name(type2)); 892 return ISN_DROP; 893 } 894 return isntype; 895 } 896 897 int 898 check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2) 899 { 900 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) 901 return FAIL; 902 return OK; 903 } 904 905 /* 906 * Generate an ISN_COMPARE* instruction with a boolean result. 907 */ 908 static int 909 generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic) 910 { 911 isntype_T isntype; 912 isn_T *isn; 913 garray_T *stack = &cctx->ctx_type_stack; 914 vartype_T type1; 915 vartype_T type2; 916 917 RETURN_OK_IF_SKIP(cctx); 918 919 // Get the known type of the two items on the stack. If they are matching 920 // use a type-specific instruction. Otherwise fall back to runtime type 921 // checking. 922 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; 923 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; 924 isntype = get_compare_isn(exprtype, type1, type2); 925 if (isntype == ISN_DROP) 926 return FAIL; 927 928 if ((isn = generate_instr(cctx, isntype)) == NULL) 929 return FAIL; 930 isn->isn_arg.op.op_type = exprtype; 931 isn->isn_arg.op.op_ic = ic; 932 933 // takes two arguments, puts one bool back 934 if (stack->ga_len >= 2) 935 { 936 --stack->ga_len; 937 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; 938 } 939 940 return OK; 941 } 942 943 /* 944 * Generate an ISN_2BOOL instruction. 945 * "offset" is the offset in the type stack. 946 */ 947 static int 948 generate_2BOOL(cctx_T *cctx, int invert, int offset) 949 { 950 isn_T *isn; 951 garray_T *stack = &cctx->ctx_type_stack; 952 953 RETURN_OK_IF_SKIP(cctx); 954 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) 955 return FAIL; 956 isn->isn_arg.tobool.invert = invert; 957 isn->isn_arg.tobool.offset = offset; 958 959 // type becomes bool 960 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool; 961 962 return OK; 963 } 964 965 /* 966 * Generate an ISN_COND2BOOL instruction. 967 */ 968 static int 969 generate_COND2BOOL(cctx_T *cctx) 970 { 971 isn_T *isn; 972 garray_T *stack = &cctx->ctx_type_stack; 973 974 RETURN_OK_IF_SKIP(cctx); 975 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL) 976 return FAIL; 977 978 // type becomes bool 979 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; 980 981 return OK; 982 } 983 984 static int 985 generate_TYPECHECK( 986 cctx_T *cctx, 987 type_T *expected, 988 int offset, 989 int argidx) 990 { 991 isn_T *isn; 992 garray_T *stack = &cctx->ctx_type_stack; 993 994 RETURN_OK_IF_SKIP(cctx); 995 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) 996 return FAIL; 997 isn->isn_arg.type.ct_type = alloc_type(expected); 998 isn->isn_arg.type.ct_off = (int8_T)offset; 999 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx; 1000 1001 // type becomes expected 1002 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected; 1003 1004 return OK; 1005 } 1006 1007 static int 1008 generate_SETTYPE( 1009 cctx_T *cctx, 1010 type_T *expected) 1011 { 1012 isn_T *isn; 1013 1014 RETURN_OK_IF_SKIP(cctx); 1015 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL) 1016 return FAIL; 1017 isn->isn_arg.type.ct_type = alloc_type(expected); 1018 return OK; 1019 } 1020 1021 /* 1022 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be 1023 * used. Return FALSE if the types will never match. 1024 */ 1025 static int 1026 use_typecheck(type_T *actual, type_T *expected) 1027 { 1028 if (actual->tt_type == VAR_ANY 1029 || actual->tt_type == VAR_UNKNOWN 1030 || (actual->tt_type == VAR_FUNC 1031 && (expected->tt_type == VAR_FUNC 1032 || expected->tt_type == VAR_PARTIAL) 1033 && (actual->tt_member == &t_any || actual->tt_argcount < 0) 1034 && ((actual->tt_member == &t_void) 1035 == (expected->tt_member == &t_void)))) 1036 return TRUE; 1037 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT) 1038 && actual->tt_type == expected->tt_type) 1039 // This takes care of a nested list or dict. 1040 return use_typecheck(actual->tt_member, expected->tt_member); 1041 return FALSE; 1042 } 1043 1044 /* 1045 * Check that 1046 * - "actual" matches "expected" type or 1047 * - "actual" is a type that can be "expected" type: add a runtime check; or 1048 * - return FAIL. 1049 * If "actual_is_const" is TRUE then the type won't change at runtime, do not 1050 * generate a TYPECHECK. 1051 */ 1052 static int 1053 need_type_where( 1054 type_T *actual, 1055 type_T *expected, 1056 int offset, 1057 where_T where, 1058 cctx_T *cctx, 1059 int silent, 1060 int actual_is_const) 1061 { 1062 if (expected == &t_bool && actual != &t_bool 1063 && (actual->tt_flags & TTFLAG_BOOL_OK)) 1064 { 1065 // Using "0", "1" or the result of an expression with "&&" or "||" as a 1066 // boolean is OK but requires a conversion. 1067 generate_2BOOL(cctx, FALSE, offset); 1068 return OK; 1069 } 1070 1071 if (check_type(expected, actual, FALSE, where) == OK) 1072 return OK; 1073 1074 // If the actual type can be the expected type add a runtime check. 1075 // If it's a constant a runtime check makes no sense. 1076 if ((!actual_is_const || actual == &t_any) 1077 && use_typecheck(actual, expected)) 1078 { 1079 generate_TYPECHECK(cctx, expected, offset, where.wt_index); 1080 return OK; 1081 } 1082 1083 if (!silent) 1084 type_mismatch_where(expected, actual, where); 1085 return FAIL; 1086 } 1087 1088 int 1089 need_type( 1090 type_T *actual, 1091 type_T *expected, 1092 int offset, 1093 int arg_idx, 1094 cctx_T *cctx, 1095 int silent, 1096 int actual_is_const) 1097 { 1098 where_T where = WHERE_INIT; 1099 1100 where.wt_index = arg_idx; 1101 return need_type_where(actual, expected, offset, where, 1102 cctx, silent, actual_is_const); 1103 } 1104 1105 /* 1106 * Check that the top of the type stack has a type that can be used as a 1107 * condition. Give an error and return FAIL if not. 1108 */ 1109 static int 1110 bool_on_stack(cctx_T *cctx) 1111 { 1112 garray_T *stack = &cctx->ctx_type_stack; 1113 type_T *type; 1114 1115 type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 1116 if (type == &t_bool) 1117 return OK; 1118 1119 if (type == &t_any || type == &t_number || type == &t_number_bool) 1120 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool. 1121 // This requires a runtime type check. 1122 return generate_COND2BOOL(cctx); 1123 1124 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE); 1125 } 1126 1127 /* 1128 * Generate an ISN_PUSHNR instruction. 1129 */ 1130 static int 1131 generate_PUSHNR(cctx_T *cctx, varnumber_T number) 1132 { 1133 isn_T *isn; 1134 garray_T *stack = &cctx->ctx_type_stack; 1135 1136 RETURN_OK_IF_SKIP(cctx); 1137 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) 1138 return FAIL; 1139 isn->isn_arg.number = number; 1140 1141 if (number == 0 || number == 1) 1142 // A 0 or 1 number can also be used as a bool. 1143 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool; 1144 return OK; 1145 } 1146 1147 /* 1148 * Generate an ISN_PUSHBOOL instruction. 1149 */ 1150 static int 1151 generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) 1152 { 1153 isn_T *isn; 1154 1155 RETURN_OK_IF_SKIP(cctx); 1156 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) 1157 return FAIL; 1158 isn->isn_arg.number = number; 1159 1160 return OK; 1161 } 1162 1163 /* 1164 * Generate an ISN_PUSHSPEC instruction. 1165 */ 1166 static int 1167 generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) 1168 { 1169 isn_T *isn; 1170 1171 RETURN_OK_IF_SKIP(cctx); 1172 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) 1173 return FAIL; 1174 isn->isn_arg.number = number; 1175 1176 return OK; 1177 } 1178 1179 #ifdef FEAT_FLOAT 1180 /* 1181 * Generate an ISN_PUSHF instruction. 1182 */ 1183 static int 1184 generate_PUSHF(cctx_T *cctx, float_T fnumber) 1185 { 1186 isn_T *isn; 1187 1188 RETURN_OK_IF_SKIP(cctx); 1189 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) 1190 return FAIL; 1191 isn->isn_arg.fnumber = fnumber; 1192 1193 return OK; 1194 } 1195 #endif 1196 1197 /* 1198 * Generate an ISN_PUSHS instruction. 1199 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL. 1200 */ 1201 static int 1202 generate_PUSHS(cctx_T *cctx, char_u **str) 1203 { 1204 isn_T *isn; 1205 1206 if (cctx->ctx_skip == SKIP_YES) 1207 { 1208 if (str != NULL) 1209 VIM_CLEAR(*str); 1210 return OK; 1211 } 1212 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) 1213 { 1214 if (str != NULL) 1215 VIM_CLEAR(*str); 1216 return FAIL; 1217 } 1218 isn->isn_arg.string = str == NULL ? NULL : *str; 1219 1220 return OK; 1221 } 1222 1223 /* 1224 * Generate an ISN_PUSHCHANNEL instruction. 1225 * Consumes "channel". 1226 */ 1227 static int 1228 generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) 1229 { 1230 isn_T *isn; 1231 1232 RETURN_OK_IF_SKIP(cctx); 1233 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) 1234 return FAIL; 1235 isn->isn_arg.channel = channel; 1236 1237 return OK; 1238 } 1239 1240 /* 1241 * Generate an ISN_PUSHJOB instruction. 1242 * Consumes "job". 1243 */ 1244 static int 1245 generate_PUSHJOB(cctx_T *cctx, job_T *job) 1246 { 1247 isn_T *isn; 1248 1249 RETURN_OK_IF_SKIP(cctx); 1250 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) 1251 return FAIL; 1252 isn->isn_arg.job = job; 1253 1254 return OK; 1255 } 1256 1257 /* 1258 * Generate an ISN_PUSHBLOB instruction. 1259 * Consumes "blob". 1260 */ 1261 static int 1262 generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) 1263 { 1264 isn_T *isn; 1265 1266 RETURN_OK_IF_SKIP(cctx); 1267 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) 1268 return FAIL; 1269 isn->isn_arg.blob = blob; 1270 1271 return OK; 1272 } 1273 1274 /* 1275 * Generate an ISN_PUSHFUNC instruction with name "name". 1276 * Consumes "name". 1277 */ 1278 static int 1279 generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) 1280 { 1281 isn_T *isn; 1282 1283 RETURN_OK_IF_SKIP(cctx); 1284 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) 1285 return FAIL; 1286 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name); 1287 1288 return OK; 1289 } 1290 1291 /* 1292 * Generate an ISN_GETITEM instruction with "index". 1293 * "with_op" is TRUE for "+=" and other operators, the stack has the current 1294 * value below the list with values. 1295 */ 1296 static int 1297 generate_GETITEM(cctx_T *cctx, int index, int with_op) 1298 { 1299 isn_T *isn; 1300 garray_T *stack = &cctx->ctx_type_stack; 1301 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len 1302 - (with_op ? 2 : 1)]; 1303 type_T *item_type = &t_any; 1304 1305 RETURN_OK_IF_SKIP(cctx); 1306 1307 if (type->tt_type != VAR_LIST) 1308 { 1309 // cannot happen, caller has checked the type 1310 emsg(_(e_listreq)); 1311 return FAIL; 1312 } 1313 item_type = type->tt_member; 1314 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) 1315 return FAIL; 1316 isn->isn_arg.getitem.gi_index = index; 1317 isn->isn_arg.getitem.gi_with_op = with_op; 1318 1319 // add the item type to the type stack 1320 if (GA_GROW_FAILS(stack, 1)) 1321 return FAIL; 1322 ((type_T **)stack->ga_data)[stack->ga_len] = item_type; 1323 ++stack->ga_len; 1324 return OK; 1325 } 1326 1327 /* 1328 * Generate an ISN_SLICE instruction with "count". 1329 */ 1330 static int 1331 generate_SLICE(cctx_T *cctx, int count) 1332 { 1333 isn_T *isn; 1334 1335 RETURN_OK_IF_SKIP(cctx); 1336 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) 1337 return FAIL; 1338 isn->isn_arg.number = count; 1339 return OK; 1340 } 1341 1342 /* 1343 * Generate an ISN_CHECKLEN instruction with "min_len". 1344 */ 1345 static int 1346 generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) 1347 { 1348 isn_T *isn; 1349 1350 RETURN_OK_IF_SKIP(cctx); 1351 1352 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) 1353 return FAIL; 1354 isn->isn_arg.checklen.cl_min_len = min_len; 1355 isn->isn_arg.checklen.cl_more_OK = more_OK; 1356 1357 return OK; 1358 } 1359 1360 /* 1361 * Generate an ISN_STORE instruction. 1362 */ 1363 static int 1364 generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) 1365 { 1366 isn_T *isn; 1367 1368 RETURN_OK_IF_SKIP(cctx); 1369 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) 1370 return FAIL; 1371 if (name != NULL) 1372 isn->isn_arg.string = vim_strsave(name); 1373 else 1374 isn->isn_arg.number = idx; 1375 1376 return OK; 1377 } 1378 1379 /* 1380 * Generate an ISN_STOREOUTER instruction. 1381 */ 1382 static int 1383 generate_STOREOUTER(cctx_T *cctx, int idx, int level) 1384 { 1385 isn_T *isn; 1386 1387 RETURN_OK_IF_SKIP(cctx); 1388 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL) 1389 return FAIL; 1390 isn->isn_arg.outer.outer_idx = idx; 1391 isn->isn_arg.outer.outer_depth = level; 1392 1393 return OK; 1394 } 1395 1396 /* 1397 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) 1398 */ 1399 static int 1400 generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) 1401 { 1402 isn_T *isn; 1403 1404 RETURN_OK_IF_SKIP(cctx); 1405 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) 1406 return FAIL; 1407 isn->isn_arg.storenr.stnr_idx = idx; 1408 isn->isn_arg.storenr.stnr_val = value; 1409 1410 return OK; 1411 } 1412 1413 /* 1414 * Generate an ISN_STOREOPT instruction 1415 */ 1416 static int 1417 generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) 1418 { 1419 isn_T *isn; 1420 1421 RETURN_OK_IF_SKIP(cctx); 1422 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL) 1423 return FAIL; 1424 isn->isn_arg.storeopt.so_name = vim_strsave(name); 1425 isn->isn_arg.storeopt.so_flags = opt_flags; 1426 1427 return OK; 1428 } 1429 1430 /* 1431 * Generate an ISN_LOAD or similar instruction. 1432 */ 1433 static int 1434 generate_LOAD( 1435 cctx_T *cctx, 1436 isntype_T isn_type, 1437 int idx, 1438 char_u *name, 1439 type_T *type) 1440 { 1441 isn_T *isn; 1442 1443 RETURN_OK_IF_SKIP(cctx); 1444 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) 1445 return FAIL; 1446 if (name != NULL) 1447 isn->isn_arg.string = vim_strsave(name); 1448 else 1449 isn->isn_arg.number = idx; 1450 1451 return OK; 1452 } 1453 1454 /* 1455 * Generate an ISN_LOADOUTER instruction 1456 */ 1457 static int 1458 generate_LOADOUTER( 1459 cctx_T *cctx, 1460 int idx, 1461 int nesting, 1462 type_T *type) 1463 { 1464 isn_T *isn; 1465 1466 RETURN_OK_IF_SKIP(cctx); 1467 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL) 1468 return FAIL; 1469 isn->isn_arg.outer.outer_idx = idx; 1470 isn->isn_arg.outer.outer_depth = nesting; 1471 1472 return OK; 1473 } 1474 1475 /* 1476 * Generate an ISN_LOADV instruction for v:var. 1477 */ 1478 static int 1479 generate_LOADV( 1480 cctx_T *cctx, 1481 char_u *name, 1482 int error) 1483 { 1484 int di_flags; 1485 int vidx = find_vim_var(name, &di_flags); 1486 type_T *type; 1487 1488 RETURN_OK_IF_SKIP(cctx); 1489 if (vidx < 0) 1490 { 1491 if (error) 1492 semsg(_(e_variable_not_found_str), name); 1493 return FAIL; 1494 } 1495 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list); 1496 1497 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); 1498 } 1499 1500 /* 1501 * Generate an ISN_UNLET instruction. 1502 */ 1503 static int 1504 generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit) 1505 { 1506 isn_T *isn; 1507 1508 RETURN_OK_IF_SKIP(cctx); 1509 if ((isn = generate_instr(cctx, isn_type)) == NULL) 1510 return FAIL; 1511 isn->isn_arg.unlet.ul_name = vim_strsave(name); 1512 isn->isn_arg.unlet.ul_forceit = forceit; 1513 1514 return OK; 1515 } 1516 1517 /* 1518 * Generate an ISN_LOCKCONST instruction. 1519 */ 1520 static int 1521 generate_LOCKCONST(cctx_T *cctx) 1522 { 1523 isn_T *isn; 1524 1525 RETURN_OK_IF_SKIP(cctx); 1526 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL) 1527 return FAIL; 1528 return OK; 1529 } 1530 1531 /* 1532 * Generate an ISN_LOADS instruction. 1533 */ 1534 static int 1535 generate_OLDSCRIPT( 1536 cctx_T *cctx, 1537 isntype_T isn_type, 1538 char_u *name, 1539 int sid, 1540 type_T *type) 1541 { 1542 isn_T *isn; 1543 1544 RETURN_OK_IF_SKIP(cctx); 1545 if (isn_type == ISN_LOADS) 1546 isn = generate_instr_type(cctx, isn_type, type); 1547 else 1548 isn = generate_instr_drop(cctx, isn_type, 1); 1549 if (isn == NULL) 1550 return FAIL; 1551 isn->isn_arg.loadstore.ls_name = vim_strsave(name); 1552 isn->isn_arg.loadstore.ls_sid = sid; 1553 1554 return OK; 1555 } 1556 1557 /* 1558 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. 1559 */ 1560 static int 1561 generate_VIM9SCRIPT( 1562 cctx_T *cctx, 1563 isntype_T isn_type, 1564 int sid, 1565 int idx, 1566 type_T *type) 1567 { 1568 isn_T *isn; 1569 scriptref_T *sref; 1570 scriptitem_T *si = SCRIPT_ITEM(sid); 1571 1572 RETURN_OK_IF_SKIP(cctx); 1573 if (isn_type == ISN_LOADSCRIPT) 1574 isn = generate_instr_type(cctx, isn_type, type); 1575 else 1576 isn = generate_instr_drop(cctx, isn_type, 1); 1577 if (isn == NULL) 1578 return FAIL; 1579 1580 // This requires three arguments, which doesn't fit in an instruction, thus 1581 // we need to allocate a struct for this. 1582 sref = ALLOC_ONE(scriptref_T); 1583 if (sref == NULL) 1584 return FAIL; 1585 isn->isn_arg.script.scriptref = sref; 1586 sref->sref_sid = sid; 1587 sref->sref_idx = idx; 1588 sref->sref_seq = si->sn_script_seq; 1589 sref->sref_type = type; 1590 return OK; 1591 } 1592 1593 /* 1594 * Generate an ISN_NEWLIST instruction. 1595 */ 1596 static int 1597 generate_NEWLIST(cctx_T *cctx, int count) 1598 { 1599 isn_T *isn; 1600 garray_T *stack = &cctx->ctx_type_stack; 1601 type_T *type; 1602 type_T *member; 1603 1604 RETURN_OK_IF_SKIP(cctx); 1605 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) 1606 return FAIL; 1607 isn->isn_arg.number = count; 1608 1609 // get the member type from all the items on the stack. 1610 if (count == 0) 1611 member = &t_unknown; 1612 else 1613 member = get_member_type_from_stack( 1614 ((type_T **)stack->ga_data) + stack->ga_len, count, 1, 1615 cctx->ctx_type_list); 1616 type = get_list_type(member, cctx->ctx_type_list); 1617 1618 // drop the value types 1619 stack->ga_len -= count; 1620 1621 // add the list type to the type stack 1622 if (GA_GROW_FAILS(stack, 1)) 1623 return FAIL; 1624 ((type_T **)stack->ga_data)[stack->ga_len] = type; 1625 ++stack->ga_len; 1626 1627 return OK; 1628 } 1629 1630 /* 1631 * Generate an ISN_NEWDICT instruction. 1632 */ 1633 static int 1634 generate_NEWDICT(cctx_T *cctx, int count) 1635 { 1636 isn_T *isn; 1637 garray_T *stack = &cctx->ctx_type_stack; 1638 type_T *type; 1639 type_T *member; 1640 1641 RETURN_OK_IF_SKIP(cctx); 1642 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) 1643 return FAIL; 1644 isn->isn_arg.number = count; 1645 1646 if (count == 0) 1647 member = &t_void; 1648 else 1649 member = get_member_type_from_stack( 1650 ((type_T **)stack->ga_data) + stack->ga_len, count, 2, 1651 cctx->ctx_type_list); 1652 type = get_dict_type(member, cctx->ctx_type_list); 1653 1654 // drop the key and value types 1655 stack->ga_len -= 2 * count; 1656 1657 // add the dict type to the type stack 1658 if (GA_GROW_FAILS(stack, 1)) 1659 return FAIL; 1660 ((type_T **)stack->ga_data)[stack->ga_len] = type; 1661 ++stack->ga_len; 1662 1663 return OK; 1664 } 1665 1666 /* 1667 * Generate an ISN_FUNCREF instruction. 1668 */ 1669 static int 1670 generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc) 1671 { 1672 isn_T *isn; 1673 garray_T *stack = &cctx->ctx_type_stack; 1674 1675 RETURN_OK_IF_SKIP(cctx); 1676 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) 1677 return FAIL; 1678 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx; 1679 cctx->ctx_has_closure = 1; 1680 1681 // If the referenced function is a closure, it may use items further up in 1682 // the nested context, including this one. 1683 if (ufunc->uf_flags & FC_CLOSURE) 1684 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE; 1685 1686 if (GA_GROW_FAILS(stack, 1)) 1687 return FAIL; 1688 ((type_T **)stack->ga_data)[stack->ga_len] = 1689 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type; 1690 ++stack->ga_len; 1691 1692 return OK; 1693 } 1694 1695 /* 1696 * Generate an ISN_NEWFUNC instruction. 1697 * "lambda_name" and "func_name" must be in allocated memory and will be 1698 * consumed. 1699 */ 1700 static int 1701 generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name) 1702 { 1703 isn_T *isn; 1704 1705 if (cctx->ctx_skip == SKIP_YES) 1706 { 1707 vim_free(lambda_name); 1708 vim_free(func_name); 1709 return OK; 1710 } 1711 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL) 1712 { 1713 vim_free(lambda_name); 1714 vim_free(func_name); 1715 return FAIL; 1716 } 1717 isn->isn_arg.newfunc.nf_lambda = lambda_name; 1718 isn->isn_arg.newfunc.nf_global = func_name; 1719 1720 return OK; 1721 } 1722 1723 /* 1724 * Generate an ISN_DEF instruction: list functions 1725 */ 1726 static int 1727 generate_DEF(cctx_T *cctx, char_u *name, size_t len) 1728 { 1729 isn_T *isn; 1730 1731 RETURN_OK_IF_SKIP(cctx); 1732 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL) 1733 return FAIL; 1734 if (len > 0) 1735 { 1736 isn->isn_arg.string = vim_strnsave(name, len); 1737 if (isn->isn_arg.string == NULL) 1738 return FAIL; 1739 } 1740 return OK; 1741 } 1742 1743 /* 1744 * Generate an ISN_JUMP instruction. 1745 */ 1746 static int 1747 generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) 1748 { 1749 isn_T *isn; 1750 garray_T *stack = &cctx->ctx_type_stack; 1751 1752 RETURN_OK_IF_SKIP(cctx); 1753 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) 1754 return FAIL; 1755 isn->isn_arg.jump.jump_when = when; 1756 isn->isn_arg.jump.jump_where = where; 1757 1758 if (when != JUMP_ALWAYS && stack->ga_len > 0) 1759 --stack->ga_len; 1760 1761 return OK; 1762 } 1763 1764 /* 1765 * Generate an ISN_JUMP_IF_ARG_SET instruction. 1766 */ 1767 static int 1768 generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off) 1769 { 1770 isn_T *isn; 1771 1772 RETURN_OK_IF_SKIP(cctx); 1773 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL) 1774 return FAIL; 1775 isn->isn_arg.jumparg.jump_arg_off = arg_off; 1776 // jump_where is set later 1777 return OK; 1778 } 1779 1780 static int 1781 generate_FOR(cctx_T *cctx, int loop_idx) 1782 { 1783 isn_T *isn; 1784 garray_T *stack = &cctx->ctx_type_stack; 1785 1786 RETURN_OK_IF_SKIP(cctx); 1787 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) 1788 return FAIL; 1789 isn->isn_arg.forloop.for_idx = loop_idx; 1790 1791 if (GA_GROW_FAILS(stack, 1)) 1792 return FAIL; 1793 // type doesn't matter, will be stored next 1794 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; 1795 ++stack->ga_len; 1796 1797 return OK; 1798 } 1799 /* 1800 * Generate an ISN_TRYCONT instruction. 1801 */ 1802 static int 1803 generate_TRYCONT(cctx_T *cctx, int levels, int where) 1804 { 1805 isn_T *isn; 1806 1807 RETURN_OK_IF_SKIP(cctx); 1808 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL) 1809 return FAIL; 1810 isn->isn_arg.trycont.tct_levels = levels; 1811 isn->isn_arg.trycont.tct_where = where; 1812 1813 return OK; 1814 } 1815 1816 1817 /* 1818 * Generate an ISN_BCALL instruction. 1819 * "method_call" is TRUE for "value->method()" 1820 * Return FAIL if the number of arguments is wrong. 1821 */ 1822 static int 1823 generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call) 1824 { 1825 isn_T *isn; 1826 garray_T *stack = &cctx->ctx_type_stack; 1827 int argoff; 1828 type_T **argtypes = NULL; 1829 type_T *shuffled_argtypes[MAX_FUNC_ARGS]; 1830 type_T *maptype = NULL; 1831 1832 RETURN_OK_IF_SKIP(cctx); 1833 argoff = check_internal_func(func_idx, argcount); 1834 if (argoff < 0) 1835 return FAIL; 1836 1837 if (method_call && argoff > 1) 1838 { 1839 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) 1840 return FAIL; 1841 isn->isn_arg.shuffle.shfl_item = argcount; 1842 isn->isn_arg.shuffle.shfl_up = argoff - 1; 1843 } 1844 1845 if (argcount > 0) 1846 { 1847 // Check the types of the arguments. 1848 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount; 1849 if (method_call && argoff > 1) 1850 { 1851 int i; 1852 1853 for (i = 0; i < argcount; ++i) 1854 shuffled_argtypes[i] = (i < argoff - 1) 1855 ? argtypes[i + 1] 1856 : (i == argoff - 1) ? argtypes[0] : argtypes[i]; 1857 argtypes = shuffled_argtypes; 1858 } 1859 if (internal_func_check_arg_types(argtypes, func_idx, argcount, 1860 cctx) == FAIL) 1861 return FAIL; 1862 if (internal_func_is_map(func_idx)) 1863 maptype = *argtypes; 1864 } 1865 1866 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) 1867 return FAIL; 1868 isn->isn_arg.bfunc.cbf_idx = func_idx; 1869 isn->isn_arg.bfunc.cbf_argcount = argcount; 1870 1871 // Drop the argument types and push the return type. 1872 stack->ga_len -= argcount; 1873 if (GA_GROW_FAILS(stack, 1)) 1874 return FAIL; 1875 ((type_T **)stack->ga_data)[stack->ga_len] = 1876 internal_func_ret_type(func_idx, argcount, argtypes); 1877 ++stack->ga_len; 1878 1879 if (maptype != NULL && maptype->tt_member != NULL 1880 && maptype->tt_member != &t_any) 1881 // Check that map() didn't change the item types. 1882 generate_TYPECHECK(cctx, maptype, -1, 1); 1883 1884 return OK; 1885 } 1886 1887 /* 1888 * Generate an ISN_LISTAPPEND instruction. Works like add(). 1889 * Argument count is already checked. 1890 */ 1891 static int 1892 generate_LISTAPPEND(cctx_T *cctx) 1893 { 1894 garray_T *stack = &cctx->ctx_type_stack; 1895 type_T *list_type; 1896 type_T *item_type; 1897 type_T *expected; 1898 1899 // Caller already checked that list_type is a list. 1900 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2]; 1901 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 1902 expected = list_type->tt_member; 1903 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL) 1904 return FAIL; 1905 1906 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL) 1907 return FAIL; 1908 1909 --stack->ga_len; // drop the argument 1910 return OK; 1911 } 1912 1913 /* 1914 * Generate an ISN_BLOBAPPEND instruction. Works like add(). 1915 * Argument count is already checked. 1916 */ 1917 static int 1918 generate_BLOBAPPEND(cctx_T *cctx) 1919 { 1920 garray_T *stack = &cctx->ctx_type_stack; 1921 type_T *item_type; 1922 1923 // Caller already checked that blob_type is a blob. 1924 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 1925 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL) 1926 return FAIL; 1927 1928 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL) 1929 return FAIL; 1930 1931 --stack->ga_len; // drop the argument 1932 return OK; 1933 } 1934 1935 /* 1936 * Return TRUE if "ufunc" should be compiled, taking into account whether 1937 * "profile" indicates profiling is to be done. 1938 */ 1939 int 1940 func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type) 1941 { 1942 switch (ufunc->uf_def_status) 1943 { 1944 case UF_TO_BE_COMPILED: 1945 return TRUE; 1946 1947 case UF_COMPILED: 1948 { 1949 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 1950 + ufunc->uf_dfunc_idx; 1951 1952 switch (compile_type) 1953 { 1954 case CT_PROFILE: 1955 #ifdef FEAT_PROFILE 1956 return dfunc->df_instr_prof == NULL; 1957 #endif 1958 case CT_NONE: 1959 return dfunc->df_instr == NULL; 1960 case CT_DEBUG: 1961 return dfunc->df_instr_debug == NULL; 1962 } 1963 } 1964 1965 case UF_NOT_COMPILED: 1966 case UF_COMPILE_ERROR: 1967 case UF_COMPILING: 1968 break; 1969 } 1970 return FALSE; 1971 } 1972 1973 /* 1974 * Generate an ISN_DCALL or ISN_UCALL instruction. 1975 * Return FAIL if the number of arguments is wrong. 1976 */ 1977 static int 1978 generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) 1979 { 1980 isn_T *isn; 1981 garray_T *stack = &cctx->ctx_type_stack; 1982 int regular_args = ufunc->uf_args.ga_len; 1983 int argcount = pushed_argcount; 1984 1985 RETURN_OK_IF_SKIP(cctx); 1986 if (argcount > regular_args && !has_varargs(ufunc)) 1987 { 1988 semsg(_(e_toomanyarg), printable_func_name(ufunc)); 1989 return FAIL; 1990 } 1991 if (argcount < regular_args - ufunc->uf_def_args.ga_len) 1992 { 1993 semsg(_(e_toofewarg), printable_func_name(ufunc)); 1994 return FAIL; 1995 } 1996 1997 if (ufunc->uf_def_status != UF_NOT_COMPILED 1998 && ufunc->uf_def_status != UF_COMPILE_ERROR) 1999 { 2000 int i; 2001 2002 for (i = 0; i < argcount; ++i) 2003 { 2004 type_T *expected; 2005 type_T *actual; 2006 2007 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; 2008 if (actual == &t_special 2009 && i >= regular_args - ufunc->uf_def_args.ga_len) 2010 { 2011 // assume v:none used for default argument value 2012 continue; 2013 } 2014 if (i < regular_args) 2015 { 2016 if (ufunc->uf_arg_types == NULL) 2017 continue; 2018 expected = ufunc->uf_arg_types[i]; 2019 } 2020 else if (ufunc->uf_va_type == NULL 2021 || ufunc->uf_va_type == &t_list_any) 2022 // possibly a lambda or "...: any" 2023 expected = &t_any; 2024 else 2025 expected = ufunc->uf_va_type->tt_member; 2026 if (need_type(actual, expected, -argcount + i, i + 1, cctx, 2027 TRUE, FALSE) == FAIL) 2028 { 2029 arg_type_mismatch(expected, actual, i + 1); 2030 return FAIL; 2031 } 2032 } 2033 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)) 2034 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL, 2035 COMPILE_TYPE(ufunc), NULL) == FAIL) 2036 return FAIL; 2037 } 2038 if (ufunc->uf_def_status == UF_COMPILE_ERROR) 2039 { 2040 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str), 2041 ufunc->uf_name); 2042 return FAIL; 2043 } 2044 2045 if ((isn = generate_instr(cctx, 2046 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL 2047 : ISN_UCALL)) == NULL) 2048 return FAIL; 2049 if (isn->isn_type == ISN_DCALL) 2050 { 2051 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; 2052 isn->isn_arg.dfunc.cdf_argcount = argcount; 2053 } 2054 else 2055 { 2056 // A user function may be deleted and redefined later, can't use the 2057 // ufunc pointer, need to look it up again at runtime. 2058 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); 2059 isn->isn_arg.ufunc.cuf_argcount = argcount; 2060 } 2061 2062 stack->ga_len -= argcount; // drop the arguments 2063 if (GA_GROW_FAILS(stack, 1)) 2064 return FAIL; 2065 // add return value 2066 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; 2067 ++stack->ga_len; 2068 2069 return OK; 2070 } 2071 2072 /* 2073 * Generate an ISN_UCALL instruction when the function isn't defined yet. 2074 */ 2075 static int 2076 generate_UCALL(cctx_T *cctx, char_u *name, int argcount) 2077 { 2078 isn_T *isn; 2079 garray_T *stack = &cctx->ctx_type_stack; 2080 2081 RETURN_OK_IF_SKIP(cctx); 2082 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) 2083 return FAIL; 2084 isn->isn_arg.ufunc.cuf_name = vim_strsave(name); 2085 isn->isn_arg.ufunc.cuf_argcount = argcount; 2086 2087 stack->ga_len -= argcount; // drop the arguments 2088 if (GA_GROW_FAILS(stack, 1)) 2089 return FAIL; 2090 // add return value 2091 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; 2092 ++stack->ga_len; 2093 2094 return OK; 2095 } 2096 2097 /* 2098 * Generate an ISN_PCALL instruction. 2099 * "type" is the type of the FuncRef. 2100 */ 2101 static int 2102 generate_PCALL( 2103 cctx_T *cctx, 2104 int argcount, 2105 char_u *name, 2106 type_T *type, 2107 int at_top) 2108 { 2109 isn_T *isn; 2110 garray_T *stack = &cctx->ctx_type_stack; 2111 type_T *ret_type; 2112 2113 RETURN_OK_IF_SKIP(cctx); 2114 2115 if (type->tt_type == VAR_ANY) 2116 ret_type = &t_any; 2117 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) 2118 { 2119 if (type->tt_argcount != -1) 2120 { 2121 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; 2122 2123 if (argcount < type->tt_min_argcount - varargs) 2124 { 2125 semsg(_(e_toofewarg), name); 2126 return FAIL; 2127 } 2128 if (!varargs && argcount > type->tt_argcount) 2129 { 2130 semsg(_(e_toomanyarg), name); 2131 return FAIL; 2132 } 2133 if (type->tt_args != NULL) 2134 { 2135 int i; 2136 2137 for (i = 0; i < argcount; ++i) 2138 { 2139 int offset = -argcount + i - (at_top ? 0 : 1); 2140 type_T *actual = ((type_T **)stack->ga_data)[ 2141 stack->ga_len + offset]; 2142 type_T *expected; 2143 2144 if (varargs && i >= type->tt_argcount - 1) 2145 expected = type->tt_args[ 2146 type->tt_argcount - 1]->tt_member; 2147 else if (i >= type->tt_min_argcount 2148 && actual == &t_special) 2149 expected = &t_any; 2150 else 2151 expected = type->tt_args[i]; 2152 if (need_type(actual, expected, offset, i + 1, 2153 cctx, TRUE, FALSE) == FAIL) 2154 { 2155 arg_type_mismatch(expected, actual, i + 1); 2156 return FAIL; 2157 } 2158 } 2159 } 2160 } 2161 ret_type = type->tt_member; 2162 if (ret_type == &t_unknown) 2163 // return type not known yet, use a runtime check 2164 ret_type = &t_any; 2165 } 2166 else 2167 { 2168 semsg(_(e_not_callable_type_str), name); 2169 return FAIL; 2170 } 2171 2172 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) 2173 return FAIL; 2174 isn->isn_arg.pfunc.cpf_top = at_top; 2175 isn->isn_arg.pfunc.cpf_argcount = argcount; 2176 2177 stack->ga_len -= argcount; // drop the arguments 2178 2179 // drop the funcref/partial, get back the return value 2180 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; 2181 2182 // If partial is above the arguments it must be cleared and replaced with 2183 // the return value. 2184 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) 2185 return FAIL; 2186 2187 return OK; 2188 } 2189 2190 /* 2191 * Generate an ISN_STRINGMEMBER instruction. 2192 */ 2193 static int 2194 generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) 2195 { 2196 isn_T *isn; 2197 garray_T *stack = &cctx->ctx_type_stack; 2198 type_T *type; 2199 2200 RETURN_OK_IF_SKIP(cctx); 2201 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) 2202 return FAIL; 2203 isn->isn_arg.string = vim_strnsave(name, len); 2204 2205 // check for dict type 2206 type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 2207 if (type->tt_type != VAR_DICT && type != &t_any) 2208 { 2209 char *tofree; 2210 2211 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str), 2212 name, type_name(type, &tofree)); 2213 vim_free(tofree); 2214 return FAIL; 2215 } 2216 // change dict type to dict member type 2217 if (type->tt_type == VAR_DICT) 2218 { 2219 ((type_T **)stack->ga_data)[stack->ga_len - 1] = 2220 type->tt_member == &t_unknown ? &t_any : type->tt_member; 2221 } 2222 2223 return OK; 2224 } 2225 2226 /* 2227 * Generate an ISN_ECHO instruction. 2228 */ 2229 static int 2230 generate_ECHO(cctx_T *cctx, int with_white, int count) 2231 { 2232 isn_T *isn; 2233 2234 RETURN_OK_IF_SKIP(cctx); 2235 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) 2236 return FAIL; 2237 isn->isn_arg.echo.echo_with_white = with_white; 2238 isn->isn_arg.echo.echo_count = count; 2239 2240 return OK; 2241 } 2242 2243 /* 2244 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. 2245 */ 2246 static int 2247 generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) 2248 { 2249 isn_T *isn; 2250 2251 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) 2252 return FAIL; 2253 isn->isn_arg.number = count; 2254 2255 return OK; 2256 } 2257 2258 /* 2259 * Generate an ISN_PUT instruction. 2260 */ 2261 static int 2262 generate_PUT(cctx_T *cctx, int regname, linenr_T lnum) 2263 { 2264 isn_T *isn; 2265 2266 RETURN_OK_IF_SKIP(cctx); 2267 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL) 2268 return FAIL; 2269 isn->isn_arg.put.put_regname = regname; 2270 isn->isn_arg.put.put_lnum = lnum; 2271 return OK; 2272 } 2273 2274 static int 2275 generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *line) 2276 { 2277 isn_T *isn; 2278 2279 RETURN_OK_IF_SKIP(cctx); 2280 if ((isn = generate_instr(cctx, isntype)) == NULL) 2281 return FAIL; 2282 isn->isn_arg.string = vim_strsave(line); 2283 return OK; 2284 } 2285 2286 static int 2287 generate_LEGACY_EVAL(cctx_T *cctx, char_u *line) 2288 { 2289 isn_T *isn; 2290 garray_T *stack = &cctx->ctx_type_stack; 2291 2292 RETURN_OK_IF_SKIP(cctx); 2293 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL) 2294 return FAIL; 2295 isn->isn_arg.string = vim_strsave(line); 2296 2297 if (GA_GROW_FAILS(stack, 1)) 2298 return FAIL; 2299 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; 2300 ++stack->ga_len; 2301 2302 return OK; 2303 } 2304 2305 static int 2306 generate_EXECCONCAT(cctx_T *cctx, int count) 2307 { 2308 isn_T *isn; 2309 2310 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) 2311 return FAIL; 2312 isn->isn_arg.number = count; 2313 return OK; 2314 } 2315 2316 /* 2317 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL. 2318 */ 2319 static int 2320 generate_RANGE(cctx_T *cctx, char_u *range) 2321 { 2322 isn_T *isn; 2323 garray_T *stack = &cctx->ctx_type_stack; 2324 2325 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL) 2326 return FAIL; 2327 isn->isn_arg.string = range; 2328 2329 if (GA_GROW_FAILS(stack, 1)) 2330 return FAIL; 2331 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number; 2332 ++stack->ga_len; 2333 return OK; 2334 } 2335 2336 static int 2337 generate_UNPACK(cctx_T *cctx, int var_count, int semicolon) 2338 { 2339 isn_T *isn; 2340 2341 RETURN_OK_IF_SKIP(cctx); 2342 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL) 2343 return FAIL; 2344 isn->isn_arg.unpack.unp_count = var_count; 2345 isn->isn_arg.unpack.unp_semicolon = semicolon; 2346 return OK; 2347 } 2348 2349 /* 2350 * Generate an instruction for any command modifiers. 2351 */ 2352 static int 2353 generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod) 2354 { 2355 isn_T *isn; 2356 2357 if (has_cmdmod(cmod, FALSE)) 2358 { 2359 cctx->ctx_has_cmdmod = TRUE; 2360 2361 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL) 2362 return FAIL; 2363 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T); 2364 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL) 2365 return FAIL; 2366 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T)); 2367 // filter program now belongs to the instruction 2368 cmod->cmod_filter_regmatch.regprog = NULL; 2369 } 2370 2371 return OK; 2372 } 2373 2374 static int 2375 generate_undo_cmdmods(cctx_T *cctx) 2376 { 2377 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL) 2378 return FAIL; 2379 cctx->ctx_has_cmdmod = FALSE; 2380 return OK; 2381 } 2382 2383 static int 2384 misplaced_cmdmod(cctx_T *cctx) 2385 { 2386 garray_T *instr = &cctx->ctx_instr; 2387 2388 if (cctx->ctx_has_cmdmod 2389 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type 2390 == ISN_CMDMOD) 2391 { 2392 emsg(_(e_misplaced_command_modifier)); 2393 return TRUE; 2394 } 2395 return FALSE; 2396 } 2397 2398 /* 2399 * Get the index of the current instruction. 2400 * This compensates for a preceding ISN_CMDMOD and ISN_PROF_START. 2401 */ 2402 static int 2403 current_instr_idx(cctx_T *cctx) 2404 { 2405 garray_T *instr = &cctx->ctx_instr; 2406 int idx = instr->ga_len; 2407 2408 while (idx > 0) 2409 { 2410 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1] 2411 .isn_type == ISN_CMDMOD) 2412 { 2413 --idx; 2414 continue; 2415 } 2416 #ifdef FEAT_PROFILE 2417 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START) 2418 { 2419 --idx; 2420 continue; 2421 } 2422 #endif 2423 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG) 2424 { 2425 --idx; 2426 continue; 2427 } 2428 break; 2429 } 2430 return idx; 2431 } 2432 2433 #ifdef FEAT_PROFILE 2434 static void 2435 may_generate_prof_end(cctx_T *cctx, int prof_lnum) 2436 { 2437 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0) 2438 generate_instr(cctx, ISN_PROF_END); 2439 } 2440 #endif 2441 2442 /* 2443 * Reserve space for a local variable. 2444 * Return the variable or NULL if it failed. 2445 */ 2446 static lvar_T * 2447 reserve_local( 2448 cctx_T *cctx, 2449 char_u *name, 2450 size_t len, 2451 int isConst, 2452 type_T *type) 2453 { 2454 lvar_T *lvar; 2455 dfunc_T *dfunc; 2456 2457 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK) 2458 { 2459 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len); 2460 return NULL; 2461 } 2462 2463 if (GA_GROW_FAILS(&cctx->ctx_locals, 1)) 2464 return NULL; 2465 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++; 2466 CLEAR_POINTER(lvar); 2467 2468 // Every local variable uses the next entry on the stack. We could re-use 2469 // the last ones when leaving a scope, but then variables used in a closure 2470 // might get overwritten. To keep things simple do not re-use stack 2471 // entries. This is less efficient, but memory is cheap these days. 2472 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx; 2473 lvar->lv_idx = dfunc->df_var_names.ga_len; 2474 2475 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); 2476 lvar->lv_const = isConst; 2477 lvar->lv_type = type; 2478 2479 // Remember the name for debugging. 2480 if (GA_GROW_FAILS(&dfunc->df_var_names, 1)) 2481 return NULL; 2482 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] = 2483 vim_strsave(lvar->lv_name); 2484 ++dfunc->df_var_names.ga_len; 2485 2486 return lvar; 2487 } 2488 2489 /* 2490 * Remove local variables above "new_top". 2491 */ 2492 static void 2493 unwind_locals(cctx_T *cctx, int new_top) 2494 { 2495 if (cctx->ctx_locals.ga_len > new_top) 2496 { 2497 int idx; 2498 lvar_T *lvar; 2499 2500 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) 2501 { 2502 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; 2503 vim_free(lvar->lv_name); 2504 } 2505 } 2506 cctx->ctx_locals.ga_len = new_top; 2507 } 2508 2509 /* 2510 * Free all local variables. 2511 */ 2512 static void 2513 free_locals(cctx_T *cctx) 2514 { 2515 unwind_locals(cctx, 0); 2516 ga_clear(&cctx->ctx_locals); 2517 } 2518 2519 /* 2520 * If "check_writable" is ASSIGN_CONST give an error if the variable was 2521 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an 2522 * error if the variable was defined with :const. 2523 */ 2524 static int 2525 check_item_writable(svar_T *sv, int check_writable, char_u *name) 2526 { 2527 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0) 2528 || (check_writable == ASSIGN_FINAL 2529 && sv->sv_const == ASSIGN_CONST)) 2530 { 2531 semsg(_(e_cannot_change_readonly_variable_str), name); 2532 return FAIL; 2533 } 2534 return OK; 2535 } 2536 2537 /* 2538 * Find "name" in script-local items of script "sid". 2539 * Pass "check_writable" to check_item_writable(). 2540 * Returns the index in "sn_var_vals" if found. 2541 * If found but not in "sn_var_vals" returns -1. 2542 * If not found or the variable is not writable returns -2. 2543 */ 2544 int 2545 get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx) 2546 { 2547 hashtab_T *ht; 2548 dictitem_T *di; 2549 scriptitem_T *si = SCRIPT_ITEM(sid); 2550 svar_T *sv; 2551 int idx; 2552 2553 if (!SCRIPT_ID_VALID(sid)) 2554 return -1; 2555 if (sid == current_sctx.sc_sid) 2556 { 2557 sallvar_T *sav = find_script_var(name, 0, cctx); 2558 2559 if (sav == NULL) 2560 return -2; 2561 idx = sav->sav_var_vals_idx; 2562 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; 2563 if (check_item_writable(sv, check_writable, name) == FAIL) 2564 return -2; 2565 return idx; 2566 } 2567 2568 // First look the name up in the hashtable. 2569 ht = &SCRIPT_VARS(sid); 2570 di = find_var_in_ht(ht, 0, name, TRUE); 2571 if (di == NULL) 2572 return -2; 2573 2574 // Now find the svar_T index in sn_var_vals. 2575 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) 2576 { 2577 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; 2578 if (sv->sv_tv == &di->di_tv) 2579 { 2580 if (check_item_writable(sv, check_writable, name) == FAIL) 2581 return -2; 2582 return idx; 2583 } 2584 } 2585 return -1; 2586 } 2587 2588 /* 2589 * Find "name" in imported items of the current script or in "cctx" if not 2590 * NULL. 2591 */ 2592 imported_T * 2593 find_imported(char_u *name, size_t len, cctx_T *cctx) 2594 { 2595 int idx; 2596 2597 if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) 2598 return NULL; 2599 if (cctx != NULL) 2600 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) 2601 { 2602 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) 2603 + idx; 2604 2605 if (len == 0 ? STRCMP(name, import->imp_name) == 0 2606 : STRLEN(import->imp_name) == len 2607 && STRNCMP(name, import->imp_name, len) == 0) 2608 return import; 2609 } 2610 2611 return find_imported_in_script(name, len, current_sctx.sc_sid); 2612 } 2613 2614 imported_T * 2615 find_imported_in_script(char_u *name, size_t len, int sid) 2616 { 2617 scriptitem_T *si; 2618 int idx; 2619 2620 if (!SCRIPT_ID_VALID(sid)) 2621 return NULL; 2622 si = SCRIPT_ITEM(sid); 2623 for (idx = 0; idx < si->sn_imports.ga_len; ++idx) 2624 { 2625 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; 2626 2627 if (len == 0 ? STRCMP(name, import->imp_name) == 0 2628 : STRLEN(import->imp_name) == len 2629 && STRNCMP(name, import->imp_name, len) == 0) 2630 return import; 2631 } 2632 return NULL; 2633 } 2634 2635 /* 2636 * Free all imported variables. 2637 */ 2638 static void 2639 free_imported(cctx_T *cctx) 2640 { 2641 int idx; 2642 2643 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) 2644 { 2645 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; 2646 2647 vim_free(import->imp_name); 2648 } 2649 ga_clear(&cctx->ctx_imports); 2650 } 2651 2652 /* 2653 * Return a pointer to the next line that isn't empty or only contains a 2654 * comment. Skips over white space. 2655 * Returns NULL if there is none. 2656 */ 2657 char_u * 2658 peek_next_line_from_context(cctx_T *cctx) 2659 { 2660 int lnum = cctx->ctx_lnum; 2661 2662 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) 2663 { 2664 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum]; 2665 char_u *p; 2666 2667 // ignore NULLs inserted for continuation lines 2668 if (line != NULL) 2669 { 2670 p = skipwhite(line); 2671 if (vim9_bad_comment(p)) 2672 return NULL; 2673 if (*p != NUL && !vim9_comment_start(p)) 2674 return p; 2675 } 2676 } 2677 return NULL; 2678 } 2679 2680 /* 2681 * Called when checking for a following operator at "arg". When the rest of 2682 * the line is empty or only a comment, peek the next line. If there is a next 2683 * line return a pointer to it and set "nextp". 2684 * Otherwise skip over white space. 2685 */ 2686 static char_u * 2687 may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) 2688 { 2689 char_u *p = skipwhite(arg); 2690 2691 *nextp = NULL; 2692 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) 2693 { 2694 *nextp = peek_next_line_from_context(cctx); 2695 if (*nextp != NULL) 2696 return *nextp; 2697 } 2698 return p; 2699 } 2700 2701 /* 2702 * Get the next line of the function from "cctx". 2703 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE. 2704 * Returns NULL when at the end. 2705 */ 2706 char_u * 2707 next_line_from_context(cctx_T *cctx, int skip_comment) 2708 { 2709 char_u *line; 2710 2711 do 2712 { 2713 ++cctx->ctx_lnum; 2714 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) 2715 { 2716 line = NULL; 2717 break; 2718 } 2719 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]; 2720 cctx->ctx_line_start = line; 2721 SOURCING_LNUM = cctx->ctx_lnum + 1; 2722 } while (line == NULL || *skipwhite(line) == NUL 2723 || (skip_comment && vim9_comment_start(skipwhite(line)))); 2724 return line; 2725 } 2726 2727 /* 2728 * Skip over white space at "whitep" and assign to "*arg". 2729 * If "*arg" is at the end of the line, advance to the next line. 2730 * Also when "whitep" points to white space and "*arg" is on a "#". 2731 * Return FAIL if beyond the last line, "*arg" is unmodified then. 2732 */ 2733 static int 2734 may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx) 2735 { 2736 *arg = skipwhite(whitep); 2737 if (vim9_bad_comment(*arg)) 2738 return FAIL; 2739 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) 2740 { 2741 char_u *next = next_line_from_context(cctx, TRUE); 2742 2743 if (next == NULL) 2744 return FAIL; 2745 *arg = skipwhite(next); 2746 } 2747 return OK; 2748 } 2749 2750 /* 2751 * Idem, and give an error when failed. 2752 */ 2753 static int 2754 may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) 2755 { 2756 if (may_get_next_line(whitep, arg, cctx) == FAIL) 2757 { 2758 SOURCING_LNUM = cctx->ctx_lnum + 1; 2759 emsg(_(e_line_incomplete)); 2760 return FAIL; 2761 } 2762 return OK; 2763 } 2764 2765 2766 // Structure passed between the compile_expr* functions to keep track of 2767 // constants that have been parsed but for which no code was produced yet. If 2768 // possible expressions on these constants are applied at compile time. If 2769 // that is not possible, the code to push the constants needs to be generated 2770 // before other instructions. 2771 // Using 50 should be more than enough of 5 levels of (). 2772 #define PPSIZE 50 2773 typedef struct { 2774 typval_T pp_tv[PPSIZE]; // stack of ppconst constants 2775 int pp_used; // active entries in pp_tv[] 2776 int pp_is_const; // all generated code was constants, used for a 2777 // list or dict with constant members 2778 } ppconst_T; 2779 2780 static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const); 2781 static int compile_expr0(char_u **arg, cctx_T *cctx); 2782 static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); 2783 2784 /* 2785 * Generate a PUSH instruction for "tv". 2786 * "tv" will be consumed or cleared. 2787 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; 2788 */ 2789 static int 2790 generate_tv_PUSH(cctx_T *cctx, typval_T *tv) 2791 { 2792 if (tv != NULL) 2793 { 2794 switch (tv->v_type) 2795 { 2796 case VAR_UNKNOWN: 2797 break; 2798 case VAR_BOOL: 2799 generate_PUSHBOOL(cctx, tv->vval.v_number); 2800 break; 2801 case VAR_SPECIAL: 2802 generate_PUSHSPEC(cctx, tv->vval.v_number); 2803 break; 2804 case VAR_NUMBER: 2805 generate_PUSHNR(cctx, tv->vval.v_number); 2806 break; 2807 #ifdef FEAT_FLOAT 2808 case VAR_FLOAT: 2809 generate_PUSHF(cctx, tv->vval.v_float); 2810 break; 2811 #endif 2812 case VAR_BLOB: 2813 generate_PUSHBLOB(cctx, tv->vval.v_blob); 2814 tv->vval.v_blob = NULL; 2815 break; 2816 case VAR_STRING: 2817 generate_PUSHS(cctx, &tv->vval.v_string); 2818 tv->vval.v_string = NULL; 2819 break; 2820 default: 2821 iemsg("constant type not supported"); 2822 clear_tv(tv); 2823 return FAIL; 2824 } 2825 tv->v_type = VAR_UNKNOWN; 2826 } 2827 return OK; 2828 } 2829 2830 /* 2831 * Generate code for any ppconst entries. 2832 */ 2833 static int 2834 generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) 2835 { 2836 int i; 2837 int ret = OK; 2838 int save_skip = cctx->ctx_skip; 2839 2840 cctx->ctx_skip = SKIP_NOT; 2841 for (i = 0; i < ppconst->pp_used; ++i) 2842 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) 2843 ret = FAIL; 2844 ppconst->pp_used = 0; 2845 cctx->ctx_skip = save_skip; 2846 return ret; 2847 } 2848 2849 /* 2850 * Check that the last item of "ppconst" is a bool. 2851 */ 2852 static int 2853 check_ppconst_bool(ppconst_T *ppconst) 2854 { 2855 if (ppconst->pp_used > 0) 2856 { 2857 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1]; 2858 where_T where = WHERE_INIT; 2859 2860 return check_typval_type(&t_bool, tv, where); 2861 } 2862 return OK; 2863 } 2864 2865 /* 2866 * Clear ppconst constants. Used when failing. 2867 */ 2868 static void 2869 clear_ppconst(ppconst_T *ppconst) 2870 { 2871 int i; 2872 2873 for (i = 0; i < ppconst->pp_used; ++i) 2874 clear_tv(&ppconst->pp_tv[i]); 2875 ppconst->pp_used = 0; 2876 } 2877 2878 /* 2879 * Compile getting a member from a list/dict/string/blob. Stack has the 2880 * indexable value and the index or the two indexes of a slice. 2881 */ 2882 static int 2883 compile_member(int is_slice, cctx_T *cctx) 2884 { 2885 type_T **typep; 2886 garray_T *stack = &cctx->ctx_type_stack; 2887 vartype_T vartype; 2888 type_T *idxtype; 2889 2890 // We can index a list, dict and blob. If we don't know the type 2891 // we can use the index value type. If we still don't know use an "ANY" 2892 // instruction. 2893 typep = ((type_T **)stack->ga_data) + stack->ga_len 2894 - (is_slice ? 3 : 2); 2895 vartype = (*typep)->tt_type; 2896 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 2897 // If the index is a string, the variable must be a Dict. 2898 if (*typep == &t_any && idxtype == &t_string) 2899 vartype = VAR_DICT; 2900 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB) 2901 { 2902 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL) 2903 return FAIL; 2904 if (is_slice) 2905 { 2906 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2]; 2907 if (need_type(idxtype, &t_number, -2, 0, cctx, 2908 FALSE, FALSE) == FAIL) 2909 return FAIL; 2910 } 2911 } 2912 2913 if (vartype == VAR_DICT) 2914 { 2915 if (is_slice) 2916 { 2917 emsg(_(e_cannot_slice_dictionary)); 2918 return FAIL; 2919 } 2920 if ((*typep)->tt_type == VAR_DICT) 2921 { 2922 *typep = (*typep)->tt_member; 2923 if (*typep == &t_unknown) 2924 // empty dict was used 2925 *typep = &t_any; 2926 } 2927 else 2928 { 2929 if (need_type(*typep, &t_dict_any, -2, 0, cctx, 2930 FALSE, FALSE) == FAIL) 2931 return FAIL; 2932 *typep = &t_any; 2933 } 2934 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL) 2935 return FAIL; 2936 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) 2937 return FAIL; 2938 } 2939 else if (vartype == VAR_STRING) 2940 { 2941 *typep = &t_string; 2942 if ((is_slice 2943 ? generate_instr_drop(cctx, ISN_STRSLICE, 2) 2944 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL) 2945 return FAIL; 2946 } 2947 else if (vartype == VAR_BLOB) 2948 { 2949 if (is_slice) 2950 { 2951 *typep = &t_blob; 2952 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL) 2953 return FAIL; 2954 } 2955 else 2956 { 2957 *typep = &t_number; 2958 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL) 2959 return FAIL; 2960 } 2961 } 2962 else if (vartype == VAR_LIST || *typep == &t_any) 2963 { 2964 if (is_slice) 2965 { 2966 if (generate_instr_drop(cctx, 2967 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE, 2968 2) == FAIL) 2969 return FAIL; 2970 } 2971 else 2972 { 2973 if ((*typep)->tt_type == VAR_LIST) 2974 { 2975 *typep = (*typep)->tt_member; 2976 if (*typep == &t_unknown) 2977 // empty list was used 2978 *typep = &t_any; 2979 } 2980 if (generate_instr_drop(cctx, 2981 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1) 2982 == FAIL) 2983 return FAIL; 2984 } 2985 } 2986 else 2987 { 2988 emsg(_(e_string_list_dict_or_blob_required)); 2989 return FAIL; 2990 } 2991 return OK; 2992 } 2993 2994 /* 2995 * Generate an instruction to load script-local variable "name", without the 2996 * leading "s:". 2997 * Also finds imported variables. 2998 */ 2999 static int 3000 compile_load_scriptvar( 3001 cctx_T *cctx, 3002 char_u *name, // variable NUL terminated 3003 char_u *start, // start of variable 3004 char_u **end, // end of variable 3005 int error) // when TRUE may give error 3006 { 3007 scriptitem_T *si; 3008 int idx; 3009 imported_T *import; 3010 3011 if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) 3012 return FAIL; 3013 si = SCRIPT_ITEM(current_sctx.sc_sid); 3014 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx); 3015 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) 3016 { 3017 // variable is not in sn_var_vals: old style script. 3018 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, 3019 &t_any); 3020 } 3021 if (idx >= 0) 3022 { 3023 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; 3024 3025 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, 3026 current_sctx.sc_sid, idx, sv->sv_type); 3027 return OK; 3028 } 3029 3030 import = find_imported(name, 0, cctx); 3031 if (import != NULL) 3032 { 3033 if (import->imp_flags & IMP_FLAGS_STAR) 3034 { 3035 char_u *p = skipwhite(*end); 3036 char_u *exp_name; 3037 int cc; 3038 ufunc_T *ufunc; 3039 type_T *type; 3040 3041 // Used "import * as Name", need to lookup the member. 3042 if (*p != '.') 3043 { 3044 semsg(_(e_expected_dot_after_name_str), start); 3045 return FAIL; 3046 } 3047 ++p; 3048 if (VIM_ISWHITE(*p)) 3049 { 3050 emsg(_(e_no_white_space_allowed_after_dot)); 3051 return FAIL; 3052 } 3053 3054 // isolate one name 3055 exp_name = p; 3056 while (eval_isnamec(*p)) 3057 ++p; 3058 cc = *p; 3059 *p = NUL; 3060 3061 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, 3062 cctx, TRUE); 3063 *p = cc; 3064 p = skipwhite(p); 3065 *end = p; 3066 3067 if (idx < 0) 3068 { 3069 if (*p == '(' && ufunc != NULL) 3070 { 3071 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type); 3072 return OK; 3073 } 3074 return FAIL; 3075 } 3076 3077 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, 3078 import->imp_sid, 3079 idx, 3080 type); 3081 } 3082 else if (import->imp_funcname != NULL) 3083 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type); 3084 else 3085 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, 3086 import->imp_sid, 3087 import->imp_var_vals_idx, 3088 import->imp_type); 3089 return OK; 3090 } 3091 3092 if (error) 3093 semsg(_(e_item_not_found_str), name); 3094 return FAIL; 3095 } 3096 3097 static int 3098 generate_funcref(cctx_T *cctx, char_u *name) 3099 { 3100 ufunc_T *ufunc = find_func(name, FALSE, cctx); 3101 3102 if (ufunc == NULL) 3103 return FAIL; 3104 3105 // Need to compile any default values to get the argument types. 3106 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)) 3107 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL) 3108 == FAIL) 3109 return FAIL; 3110 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); 3111 } 3112 3113 /* 3114 * Compile a variable name into a load instruction. 3115 * "end" points to just after the name. 3116 * "is_expr" is TRUE when evaluating an expression, might be a funcref. 3117 * When "error" is FALSE do not give an error when not found. 3118 */ 3119 static int 3120 compile_load( 3121 char_u **arg, 3122 char_u *end_arg, 3123 cctx_T *cctx, 3124 int is_expr, 3125 int error) 3126 { 3127 type_T *type; 3128 char_u *name = NULL; 3129 char_u *end = end_arg; 3130 int res = FAIL; 3131 int prev_called_emsg = called_emsg; 3132 3133 if (*(*arg + 1) == ':') 3134 { 3135 if (end <= *arg + 2) 3136 { 3137 isntype_T isn_type; 3138 3139 // load dictionary of namespace 3140 switch (**arg) 3141 { 3142 case 'g': isn_type = ISN_LOADGDICT; break; 3143 case 'w': isn_type = ISN_LOADWDICT; break; 3144 case 't': isn_type = ISN_LOADTDICT; break; 3145 case 'b': isn_type = ISN_LOADBDICT; break; 3146 default: 3147 semsg(_(e_namespace_not_supported_str), *arg); 3148 goto theend; 3149 } 3150 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) 3151 goto theend; 3152 res = OK; 3153 } 3154 else 3155 { 3156 isntype_T isn_type = ISN_DROP; 3157 3158 // load namespaced variable 3159 name = vim_strnsave(*arg + 2, end - (*arg + 2)); 3160 if (name == NULL) 3161 return FAIL; 3162 3163 switch (**arg) 3164 { 3165 case 'v': res = generate_LOADV(cctx, name, error); 3166 break; 3167 case 's': if (is_expr && ASCII_ISUPPER(*name) 3168 && find_func(name, FALSE, cctx) != NULL) 3169 res = generate_funcref(cctx, name); 3170 else 3171 res = compile_load_scriptvar(cctx, name, 3172 NULL, &end, error); 3173 break; 3174 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL) 3175 { 3176 if (is_expr && ASCII_ISUPPER(*name) 3177 && find_func(name, FALSE, cctx) != NULL) 3178 res = generate_funcref(cctx, name); 3179 else 3180 isn_type = ISN_LOADG; 3181 } 3182 else 3183 { 3184 isn_type = ISN_LOADAUTO; 3185 vim_free(name); 3186 name = vim_strnsave(*arg, end - *arg); 3187 if (name == NULL) 3188 return FAIL; 3189 } 3190 break; 3191 case 'w': isn_type = ISN_LOADW; break; 3192 case 't': isn_type = ISN_LOADT; break; 3193 case 'b': isn_type = ISN_LOADB; break; 3194 default: // cannot happen, just in case 3195 semsg(_(e_namespace_not_supported_str), *arg); 3196 goto theend; 3197 } 3198 if (isn_type != ISN_DROP) 3199 { 3200 // Global, Buffer-local, Window-local and Tabpage-local 3201 // variables can be defined later, thus we don't check if it 3202 // exists, give an error at runtime. 3203 res = generate_LOAD(cctx, isn_type, 0, name, &t_any); 3204 } 3205 } 3206 } 3207 else 3208 { 3209 size_t len = end - *arg; 3210 int idx; 3211 int gen_load = FALSE; 3212 int gen_load_outer = 0; 3213 3214 name = vim_strnsave(*arg, end - *arg); 3215 if (name == NULL) 3216 return FAIL; 3217 3218 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL) 3219 { 3220 script_autoload(name, FALSE); 3221 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any); 3222 } 3223 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) 3224 == OK) 3225 { 3226 if (gen_load_outer == 0) 3227 gen_load = TRUE; 3228 } 3229 else 3230 { 3231 lvar_T lvar; 3232 3233 if (lookup_local(*arg, len, &lvar, cctx) == OK) 3234 { 3235 type = lvar.lv_type; 3236 idx = lvar.lv_idx; 3237 if (lvar.lv_from_outer != 0) 3238 gen_load_outer = lvar.lv_from_outer; 3239 else 3240 gen_load = TRUE; 3241 } 3242 else 3243 { 3244 // "var" can be script-local even without using "s:" if it 3245 // already exists in a Vim9 script or when it's imported. 3246 if (script_var_exists(*arg, len, cctx) == OK 3247 || find_imported(name, 0, cctx) != NULL) 3248 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE); 3249 3250 // When evaluating an expression and the name starts with an 3251 // uppercase letter it can be a user defined function. 3252 // generate_funcref() will fail if the function can't be found. 3253 if (res == FAIL && is_expr && ASCII_ISUPPER(*name)) 3254 res = generate_funcref(cctx, name); 3255 } 3256 } 3257 if (gen_load) 3258 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); 3259 if (gen_load_outer > 0) 3260 { 3261 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type); 3262 cctx->ctx_outer_used = TRUE; 3263 } 3264 } 3265 3266 *arg = end; 3267 3268 theend: 3269 if (res == FAIL && error && called_emsg == prev_called_emsg) 3270 semsg(_(e_variable_not_found_str), name); 3271 vim_free(name); 3272 return res; 3273 } 3274 3275 static void 3276 clear_instr_ga(garray_T *gap) 3277 { 3278 int idx; 3279 3280 for (idx = 0; idx < gap->ga_len; ++idx) 3281 delete_instr(((isn_T *)gap->ga_data) + idx); 3282 ga_clear(gap); 3283 } 3284 3285 /* 3286 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR. 3287 * Returns FAIL if compilation fails. 3288 */ 3289 static int 3290 compile_string(isn_T *isn, cctx_T *cctx) 3291 { 3292 char_u *s = isn->isn_arg.string; 3293 garray_T save_ga = cctx->ctx_instr; 3294 int expr_res; 3295 int trailing_error; 3296 int instr_count; 3297 isn_T *instr = NULL; 3298 3299 // Remove the string type from the stack. 3300 --cctx->ctx_type_stack.ga_len; 3301 3302 // Temporarily reset the list of instructions so that the jump labels are 3303 // correct. 3304 cctx->ctx_instr.ga_len = 0; 3305 cctx->ctx_instr.ga_maxlen = 0; 3306 cctx->ctx_instr.ga_data = NULL; 3307 expr_res = compile_expr0(&s, cctx); 3308 s = skipwhite(s); 3309 trailing_error = *s != NUL; 3310 3311 if (expr_res == FAIL || trailing_error 3312 || GA_GROW_FAILS(&cctx->ctx_instr, 1)) 3313 { 3314 if (trailing_error) 3315 semsg(_(e_trailing_arg), s); 3316 clear_instr_ga(&cctx->ctx_instr); 3317 cctx->ctx_instr = save_ga; 3318 ++cctx->ctx_type_stack.ga_len; 3319 return FAIL; 3320 } 3321 3322 // Move the generated instructions into the ISN_INSTR instruction, then 3323 // restore the list of instructions. 3324 instr_count = cctx->ctx_instr.ga_len; 3325 instr = cctx->ctx_instr.ga_data; 3326 instr[instr_count].isn_type = ISN_FINISH; 3327 3328 cctx->ctx_instr = save_ga; 3329 vim_free(isn->isn_arg.string); 3330 isn->isn_type = ISN_INSTR; 3331 isn->isn_arg.instr = instr; 3332 return OK; 3333 } 3334 3335 /* 3336 * Compile the argument expressions. 3337 * "arg" points to just after the "(" and is advanced to after the ")" 3338 */ 3339 static int 3340 compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair) 3341 { 3342 char_u *p = *arg; 3343 char_u *whitep = *arg; 3344 int must_end = FALSE; 3345 int instr_count; 3346 3347 for (;;) 3348 { 3349 if (may_get_next_line(whitep, &p, cctx) == FAIL) 3350 goto failret; 3351 if (*p == ')') 3352 { 3353 *arg = p + 1; 3354 return OK; 3355 } 3356 if (must_end) 3357 { 3358 semsg(_(e_missing_comma_before_argument_str), p); 3359 return FAIL; 3360 } 3361 3362 instr_count = cctx->ctx_instr.ga_len; 3363 if (compile_expr0(&p, cctx) == FAIL) 3364 return FAIL; 3365 ++*argcount; 3366 3367 if (is_searchpair && *argcount == 5 3368 && cctx->ctx_instr.ga_len == instr_count + 1) 3369 { 3370 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count; 3371 3372 // {skip} argument of searchpair() can be compiled if not empty 3373 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL) 3374 compile_string(isn, cctx); 3375 } 3376 3377 if (*p != ',' && *skipwhite(p) == ',') 3378 { 3379 semsg(_(e_no_white_space_allowed_before_str_str), ",", p); 3380 p = skipwhite(p); 3381 } 3382 if (*p == ',') 3383 { 3384 ++p; 3385 if (*p != NUL && !VIM_ISWHITE(*p)) 3386 semsg(_(e_white_space_required_after_str_str), ",", p - 1); 3387 } 3388 else 3389 must_end = TRUE; 3390 whitep = p; 3391 p = skipwhite(p); 3392 } 3393 failret: 3394 emsg(_(e_missing_close)); 3395 return FAIL; 3396 } 3397 3398 /* 3399 * Compile a function call: name(arg1, arg2) 3400 * "arg" points to "name", "arg + varlen" to the "(". 3401 * "argcount_init" is 1 for "value->method()" 3402 * Instructions: 3403 * EVAL arg1 3404 * EVAL arg2 3405 * BCALL / DCALL / UCALL 3406 */ 3407 static int 3408 compile_call( 3409 char_u **arg, 3410 size_t varlen, 3411 cctx_T *cctx, 3412 ppconst_T *ppconst, 3413 int argcount_init) 3414 { 3415 char_u *name = *arg; 3416 char_u *p; 3417 int argcount = argcount_init; 3418 char_u namebuf[100]; 3419 char_u fname_buf[FLEN_FIXED + 1]; 3420 char_u *tofree = NULL; 3421 int error = FCERR_NONE; 3422 ufunc_T *ufunc = NULL; 3423 int res = FAIL; 3424 int is_autoload; 3425 int is_searchpair; 3426 3427 // We can evaluate "has('name')" at compile time. 3428 // We always evaluate "exists_compiled()" at compile time. 3429 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0) 3430 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0)) 3431 { 3432 char_u *s = skipwhite(*arg + varlen + 1); 3433 typval_T argvars[2]; 3434 int is_has = **arg == 'h'; 3435 3436 argvars[0].v_type = VAR_UNKNOWN; 3437 if (*s == '"') 3438 (void)eval_string(&s, &argvars[0], TRUE); 3439 else if (*s == '\'') 3440 (void)eval_lit_string(&s, &argvars[0], TRUE); 3441 s = skipwhite(s); 3442 if (*s == ')' && argvars[0].v_type == VAR_STRING 3443 && ((is_has && !dynamic_feature(argvars[0].vval.v_string)) 3444 || !is_has)) 3445 { 3446 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; 3447 3448 *arg = s + 1; 3449 argvars[1].v_type = VAR_UNKNOWN; 3450 tv->v_type = VAR_NUMBER; 3451 tv->vval.v_number = 0; 3452 if (is_has) 3453 f_has(argvars, tv); 3454 else 3455 f_exists(argvars, tv); 3456 clear_tv(&argvars[0]); 3457 ++ppconst->pp_used; 3458 return OK; 3459 } 3460 clear_tv(&argvars[0]); 3461 if (!is_has) 3462 { 3463 emsg(_(e_argument_of_exists_compiled_must_be_literal_string)); 3464 return FAIL; 3465 } 3466 } 3467 3468 if (generate_ppconst(cctx, ppconst) == FAIL) 3469 return FAIL; 3470 3471 if (varlen >= sizeof(namebuf)) 3472 { 3473 semsg(_(e_name_too_long_str), name); 3474 return FAIL; 3475 } 3476 vim_strncpy(namebuf, *arg, varlen); 3477 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); 3478 3479 // We handle the "skip" argument of searchpair() and searchpairpos() 3480 // differently. 3481 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0) 3482 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0) 3483 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0) 3484 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0); 3485 3486 *arg = skipwhite(*arg + varlen + 1); 3487 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL) 3488 goto theend; 3489 3490 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL; 3491 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload) 3492 { 3493 int idx; 3494 3495 // builtin function 3496 idx = find_internal_func(name); 3497 if (idx >= 0) 3498 { 3499 if (STRCMP(name, "flatten") == 0) 3500 { 3501 emsg(_(e_cannot_use_flatten_in_vim9_script)); 3502 goto theend; 3503 } 3504 3505 if (STRCMP(name, "add") == 0 && argcount == 2) 3506 { 3507 garray_T *stack = &cctx->ctx_type_stack; 3508 type_T *type = ((type_T **)stack->ga_data)[ 3509 stack->ga_len - 2]; 3510 3511 // add() can be compiled to instructions if we know the type 3512 if (type->tt_type == VAR_LIST) 3513 { 3514 // inline "add(list, item)" so that the type can be checked 3515 res = generate_LISTAPPEND(cctx); 3516 idx = -1; 3517 } 3518 else if (type->tt_type == VAR_BLOB) 3519 { 3520 // inline "add(blob, nr)" so that the type can be checked 3521 res = generate_BLOBAPPEND(cctx); 3522 idx = -1; 3523 } 3524 } 3525 3526 if (idx >= 0) 3527 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); 3528 } 3529 else 3530 semsg(_(e_unknownfunc), namebuf); 3531 goto theend; 3532 } 3533 3534 // An argument or local variable can be a function reference, this 3535 // overrules a function name. 3536 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL 3537 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK) 3538 { 3539 // If we can find the function by name generate the right call. 3540 // Skip global functions here, a local funcref takes precedence. 3541 ufunc = find_func(name, FALSE, cctx); 3542 if (ufunc != NULL && !func_is_global(ufunc)) 3543 { 3544 res = generate_CALL(cctx, ufunc, argcount); 3545 goto theend; 3546 } 3547 } 3548 3549 // If the name is a variable, load it and use PCALL. 3550 // Not for g:Func(), we don't know if it is a variable or not. 3551 // Not for eome#Func(), it will be loaded later. 3552 p = namebuf; 3553 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload 3554 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK) 3555 { 3556 garray_T *stack = &cctx->ctx_type_stack; 3557 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 3558 3559 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); 3560 goto theend; 3561 } 3562 3563 // If we can find a global function by name generate the right call. 3564 if (ufunc != NULL) 3565 { 3566 res = generate_CALL(cctx, ufunc, argcount); 3567 goto theend; 3568 } 3569 3570 // A global function may be defined only later. Need to figure out at 3571 // runtime. Also handles a FuncRef at runtime. 3572 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload) 3573 res = generate_UCALL(cctx, name, argcount); 3574 else 3575 semsg(_(e_unknownfunc), namebuf); 3576 3577 theend: 3578 vim_free(tofree); 3579 return res; 3580 } 3581 3582 // like NAMESPACE_CHAR but with 'a' and 'l'. 3583 #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" 3584 3585 /* 3586 * Find the end of a variable or function name. Unlike find_name_end() this 3587 * does not recognize magic braces. 3588 * When "use_namespace" is TRUE recognize "b:", "s:", etc. 3589 * Return a pointer to just after the name. Equal to "arg" if there is no 3590 * valid name. 3591 */ 3592 char_u * 3593 to_name_end(char_u *arg, int use_namespace) 3594 { 3595 char_u *p; 3596 3597 // Quick check for valid starting character. 3598 if (!eval_isnamec1(*arg)) 3599 return arg; 3600 3601 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) 3602 // Include a namespace such as "s:var" and "v:var". But "n:" is not 3603 // and can be used in slice "[n:]". 3604 if (*p == ':' && (p != arg + 1 3605 || !use_namespace 3606 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) 3607 break; 3608 return p; 3609 } 3610 3611 /* 3612 * Like to_name_end() but also skip over a list or dict constant. 3613 * Also accept "<SNR>123_Func". 3614 * This intentionally does not handle line continuation. 3615 */ 3616 char_u * 3617 to_name_const_end(char_u *arg) 3618 { 3619 char_u *p = arg; 3620 typval_T rettv; 3621 3622 if (STRNCMP(p, "<SNR>", 5) == 0) 3623 p = skipdigits(p + 5); 3624 p = to_name_end(p, TRUE); 3625 if (p == arg && *arg == '[') 3626 { 3627 3628 // Can be "[1, 2, 3]->Func()". 3629 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) 3630 p = arg; 3631 } 3632 return p; 3633 } 3634 3635 /* 3636 * parse a list: [expr, expr] 3637 * "*arg" points to the '['. 3638 * ppconst->pp_is_const is set if all items are a constant. 3639 */ 3640 static int 3641 compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) 3642 { 3643 char_u *p = skipwhite(*arg + 1); 3644 char_u *whitep = *arg + 1; 3645 int count = 0; 3646 int is_const; 3647 int is_all_const = TRUE; // reset when non-const encountered 3648 3649 for (;;) 3650 { 3651 if (may_get_next_line(whitep, &p, cctx) == FAIL) 3652 { 3653 semsg(_(e_list_end), *arg); 3654 return FAIL; 3655 } 3656 if (*p == ',') 3657 { 3658 semsg(_(e_no_white_space_allowed_before_str_str), ",", p); 3659 return FAIL; 3660 } 3661 if (*p == ']') 3662 { 3663 ++p; 3664 break; 3665 } 3666 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL) 3667 return FAIL; 3668 if (!is_const) 3669 is_all_const = FALSE; 3670 ++count; 3671 if (*p == ',') 3672 { 3673 ++p; 3674 if (*p != ']' && !IS_WHITE_OR_NUL(*p)) 3675 { 3676 semsg(_(e_white_space_required_after_str_str), ",", p - 1); 3677 return FAIL; 3678 } 3679 } 3680 whitep = p; 3681 p = skipwhite(p); 3682 } 3683 *arg = p; 3684 3685 ppconst->pp_is_const = is_all_const; 3686 return generate_NEWLIST(cctx, count); 3687 } 3688 3689 /* 3690 * Parse a lambda: "(arg, arg) => expr" 3691 * "*arg" points to the '('. 3692 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda. 3693 */ 3694 static int 3695 compile_lambda(char_u **arg, cctx_T *cctx) 3696 { 3697 int r; 3698 typval_T rettv; 3699 ufunc_T *ufunc; 3700 evalarg_T evalarg; 3701 3702 CLEAR_FIELD(evalarg); 3703 evalarg.eval_flags = EVAL_EVALUATE; 3704 evalarg.eval_cctx = cctx; 3705 3706 // Get the funcref in "rettv". 3707 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg); 3708 if (r != OK) 3709 { 3710 clear_evalarg(&evalarg, NULL); 3711 return r; 3712 } 3713 3714 // "rettv" will now be a partial referencing the function. 3715 ufunc = rettv.vval.v_partial->pt_func; 3716 ++ufunc->uf_refcount; 3717 clear_tv(&rettv); 3718 3719 // Compile it here to get the return type. The return type is optional, 3720 // when it's missing use t_unknown. This is recognized in 3721 // compile_return(). 3722 if (ufunc->uf_ret_type->tt_type == VAR_VOID) 3723 ufunc->uf_ret_type = &t_unknown; 3724 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx); 3725 3726 #ifdef FEAT_PROFILE 3727 // When the outer function is compiled for profiling, the lambda may be 3728 // called without profiling. Compile it here in the right context. 3729 if (cctx->ctx_compile_type == CT_PROFILE) 3730 compile_def_function(ufunc, FALSE, CT_NONE, cctx); 3731 #endif 3732 3733 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg" 3734 // points into it. Point to the original line to avoid a dangling pointer. 3735 if (evalarg.eval_tofree_cmdline != NULL) 3736 { 3737 size_t off = *arg - evalarg.eval_tofree_cmdline; 3738 3739 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum] 3740 + off; 3741 } 3742 3743 clear_evalarg(&evalarg, NULL); 3744 3745 if (ufunc->uf_def_status == UF_COMPILED) 3746 { 3747 // The return type will now be known. 3748 set_function_type(ufunc); 3749 3750 // The function reference count will be 1. When the ISN_FUNCREF 3751 // instruction is deleted the reference count is decremented and the 3752 // function is freed. 3753 return generate_FUNCREF(cctx, ufunc); 3754 } 3755 3756 func_ptr_unref(ufunc); 3757 return FAIL; 3758 } 3759 3760 /* 3761 * Get a lambda and compile it. Uses Vim9 syntax. 3762 */ 3763 int 3764 get_lambda_tv_and_compile( 3765 char_u **arg, 3766 typval_T *rettv, 3767 int types_optional, 3768 evalarg_T *evalarg) 3769 { 3770 int r; 3771 ufunc_T *ufunc; 3772 int save_sc_version = current_sctx.sc_version; 3773 3774 // Get the funcref in "rettv". 3775 current_sctx.sc_version = SCRIPT_VERSION_VIM9; 3776 r = get_lambda_tv(arg, rettv, types_optional, evalarg); 3777 current_sctx.sc_version = save_sc_version; 3778 if (r != OK) 3779 return r; 3780 3781 // "rettv" will now be a partial referencing the function. 3782 ufunc = rettv->vval.v_partial->pt_func; 3783 3784 // Compile it here to get the return type. The return type is optional, 3785 // when it's missing use t_unknown. This is recognized in 3786 // compile_return(). 3787 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID) 3788 ufunc->uf_ret_type = &t_unknown; 3789 compile_def_function(ufunc, FALSE, CT_NONE, NULL); 3790 3791 if (ufunc->uf_def_status == UF_COMPILED) 3792 { 3793 // The return type will now be known. 3794 set_function_type(ufunc); 3795 return OK; 3796 } 3797 clear_tv(rettv); 3798 return FAIL; 3799 } 3800 3801 /* 3802 * parse a dict: {key: val, [key]: val} 3803 * "*arg" points to the '{'. 3804 * ppconst->pp_is_const is set if all item values are a constant. 3805 */ 3806 static int 3807 compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) 3808 { 3809 garray_T *instr = &cctx->ctx_instr; 3810 int count = 0; 3811 dict_T *d = dict_alloc(); 3812 dictitem_T *item; 3813 char_u *whitep = *arg + 1; 3814 char_u *p; 3815 int is_const; 3816 int is_all_const = TRUE; // reset when non-const encountered 3817 3818 if (d == NULL) 3819 return FAIL; 3820 if (generate_ppconst(cctx, ppconst) == FAIL) 3821 return FAIL; 3822 for (;;) 3823 { 3824 char_u *key = NULL; 3825 3826 if (may_get_next_line(whitep, arg, cctx) == FAIL) 3827 { 3828 *arg = NULL; 3829 goto failret; 3830 } 3831 3832 if (**arg == '}') 3833 break; 3834 3835 if (**arg == '[') 3836 { 3837 isn_T *isn; 3838 3839 // {[expr]: value} uses an evaluated key. 3840 *arg = skipwhite(*arg + 1); 3841 if (compile_expr0(arg, cctx) == FAIL) 3842 return FAIL; 3843 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; 3844 if (isn->isn_type == ISN_PUSHNR) 3845 { 3846 char buf[NUMBUFLEN]; 3847 3848 // Convert to string at compile time. 3849 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number); 3850 isn->isn_type = ISN_PUSHS; 3851 isn->isn_arg.string = vim_strsave((char_u *)buf); 3852 } 3853 if (isn->isn_type == ISN_PUSHS) 3854 key = isn->isn_arg.string; 3855 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL) 3856 return FAIL; 3857 *arg = skipwhite(*arg); 3858 if (**arg != ']') 3859 { 3860 emsg(_(e_missing_matching_bracket_after_dict_key)); 3861 return FAIL; 3862 } 3863 ++*arg; 3864 } 3865 else 3866 { 3867 // {"name": value}, 3868 // {'name': value}, 3869 // {name: value} use "name" as a literal key 3870 key = get_literal_key(arg); 3871 if (key == NULL) 3872 return FAIL; 3873 if (generate_PUSHS(cctx, &key) == FAIL) 3874 return FAIL; 3875 } 3876 3877 // Check for duplicate keys, if using string keys. 3878 if (key != NULL) 3879 { 3880 item = dict_find(d, key, -1); 3881 if (item != NULL) 3882 { 3883 semsg(_(e_duplicate_key), key); 3884 goto failret; 3885 } 3886 item = dictitem_alloc(key); 3887 if (item != NULL) 3888 { 3889 item->di_tv.v_type = VAR_UNKNOWN; 3890 item->di_tv.v_lock = 0; 3891 if (dict_add(d, item) == FAIL) 3892 dictitem_free(item); 3893 } 3894 } 3895 3896 if (**arg != ':') 3897 { 3898 if (*skipwhite(*arg) == ':') 3899 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg); 3900 else 3901 semsg(_(e_missing_dict_colon), *arg); 3902 return FAIL; 3903 } 3904 whitep = *arg + 1; 3905 if (!IS_WHITE_OR_NUL(*whitep)) 3906 { 3907 semsg(_(e_white_space_required_after_str_str), ":", *arg); 3908 return FAIL; 3909 } 3910 3911 if (may_get_next_line(whitep, arg, cctx) == FAIL) 3912 { 3913 *arg = NULL; 3914 goto failret; 3915 } 3916 3917 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL) 3918 return FAIL; 3919 if (!is_const) 3920 is_all_const = FALSE; 3921 ++count; 3922 3923 whitep = *arg; 3924 if (may_get_next_line(whitep, arg, cctx) == FAIL) 3925 { 3926 *arg = NULL; 3927 goto failret; 3928 } 3929 if (**arg == '}') 3930 break; 3931 if (**arg != ',') 3932 { 3933 semsg(_(e_missing_dict_comma), *arg); 3934 goto failret; 3935 } 3936 if (IS_WHITE_OR_NUL(*whitep)) 3937 { 3938 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep); 3939 return FAIL; 3940 } 3941 whitep = *arg + 1; 3942 if (!IS_WHITE_OR_NUL(*whitep)) 3943 { 3944 semsg(_(e_white_space_required_after_str_str), ",", *arg); 3945 return FAIL; 3946 } 3947 *arg = skipwhite(whitep); 3948 } 3949 3950 *arg = *arg + 1; 3951 3952 // Allow for following comment, after at least one space. 3953 p = skipwhite(*arg); 3954 if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) 3955 *arg += STRLEN(*arg); 3956 3957 dict_unref(d); 3958 ppconst->pp_is_const = is_all_const; 3959 return generate_NEWDICT(cctx, count); 3960 3961 failret: 3962 if (*arg == NULL) 3963 { 3964 semsg(_(e_missing_dict_end), _("[end of lines]")); 3965 *arg = (char_u *)""; 3966 } 3967 dict_unref(d); 3968 return FAIL; 3969 } 3970 3971 /* 3972 * Compile "&option". 3973 */ 3974 static int 3975 compile_get_option(char_u **arg, cctx_T *cctx) 3976 { 3977 typval_T rettv; 3978 char_u *start = *arg; 3979 int ret; 3980 3981 // parse the option and get the current value to get the type. 3982 rettv.v_type = VAR_UNKNOWN; 3983 ret = eval_option(arg, &rettv, TRUE); 3984 if (ret == OK) 3985 { 3986 // include the '&' in the name, eval_option() expects it. 3987 char_u *name = vim_strnsave(start, *arg - start); 3988 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool 3989 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string; 3990 3991 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); 3992 vim_free(name); 3993 } 3994 clear_tv(&rettv); 3995 3996 return ret; 3997 } 3998 3999 /* 4000 * Compile "$VAR". 4001 */ 4002 static int 4003 compile_get_env(char_u **arg, cctx_T *cctx) 4004 { 4005 char_u *start = *arg; 4006 int len; 4007 int ret; 4008 char_u *name; 4009 4010 ++*arg; 4011 len = get_env_len(arg); 4012 if (len == 0) 4013 { 4014 semsg(_(e_syntax_error_at_str), start - 1); 4015 return FAIL; 4016 } 4017 4018 // include the '$' in the name, eval_env_var() expects it. 4019 name = vim_strnsave(start, len + 1); 4020 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); 4021 vim_free(name); 4022 return ret; 4023 } 4024 4025 /* 4026 * Compile "@r". 4027 */ 4028 static int 4029 compile_get_register(char_u **arg, cctx_T *cctx) 4030 { 4031 int ret; 4032 4033 ++*arg; 4034 if (**arg == NUL) 4035 { 4036 semsg(_(e_syntax_error_at_str), *arg - 1); 4037 return FAIL; 4038 } 4039 if (!valid_yank_reg(**arg, FALSE)) 4040 { 4041 emsg_invreg(**arg); 4042 return FAIL; 4043 } 4044 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); 4045 ++*arg; 4046 return ret; 4047 } 4048 4049 /* 4050 * Apply leading '!', '-' and '+' to constant "rettv". 4051 * When "numeric_only" is TRUE do not apply '!'. 4052 */ 4053 static int 4054 apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end) 4055 { 4056 char_u *p = *end; 4057 4058 // this works from end to start 4059 while (p > start) 4060 { 4061 --p; 4062 if (*p == '-' || *p == '+') 4063 { 4064 // only '-' has an effect, for '+' we only check the type 4065 #ifdef FEAT_FLOAT 4066 if (rettv->v_type == VAR_FLOAT) 4067 { 4068 if (*p == '-') 4069 rettv->vval.v_float = -rettv->vval.v_float; 4070 } 4071 else 4072 #endif 4073 { 4074 varnumber_T val; 4075 int error = FALSE; 4076 4077 // tv_get_number_chk() accepts a string, but we don't want that 4078 // here 4079 if (check_not_string(rettv) == FAIL) 4080 return FAIL; 4081 val = tv_get_number_chk(rettv, &error); 4082 clear_tv(rettv); 4083 if (error) 4084 return FAIL; 4085 if (*p == '-') 4086 val = -val; 4087 rettv->v_type = VAR_NUMBER; 4088 rettv->vval.v_number = val; 4089 } 4090 } 4091 else if (numeric_only) 4092 { 4093 ++p; 4094 break; 4095 } 4096 else if (*p == '!') 4097 { 4098 int v = tv2bool(rettv); 4099 4100 // '!' is permissive in the type. 4101 clear_tv(rettv); 4102 rettv->v_type = VAR_BOOL; 4103 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; 4104 } 4105 } 4106 *end = p; 4107 return OK; 4108 } 4109 4110 /* 4111 * Recognize v: variables that are constants and set "rettv". 4112 */ 4113 static void 4114 get_vim_constant(char_u **arg, typval_T *rettv) 4115 { 4116 if (STRNCMP(*arg, "v:true", 6) == 0) 4117 { 4118 rettv->v_type = VAR_BOOL; 4119 rettv->vval.v_number = VVAL_TRUE; 4120 *arg += 6; 4121 } 4122 else if (STRNCMP(*arg, "v:false", 7) == 0) 4123 { 4124 rettv->v_type = VAR_BOOL; 4125 rettv->vval.v_number = VVAL_FALSE; 4126 *arg += 7; 4127 } 4128 else if (STRNCMP(*arg, "v:null", 6) == 0) 4129 { 4130 rettv->v_type = VAR_SPECIAL; 4131 rettv->vval.v_number = VVAL_NULL; 4132 *arg += 6; 4133 } 4134 else if (STRNCMP(*arg, "v:none", 6) == 0) 4135 { 4136 rettv->v_type = VAR_SPECIAL; 4137 rettv->vval.v_number = VVAL_NONE; 4138 *arg += 6; 4139 } 4140 } 4141 4142 exprtype_T 4143 get_compare_type(char_u *p, int *len, int *type_is) 4144 { 4145 exprtype_T type = EXPR_UNKNOWN; 4146 int i; 4147 4148 switch (p[0]) 4149 { 4150 case '=': if (p[1] == '=') 4151 type = EXPR_EQUAL; 4152 else if (p[1] == '~') 4153 type = EXPR_MATCH; 4154 break; 4155 case '!': if (p[1] == '=') 4156 type = EXPR_NEQUAL; 4157 else if (p[1] == '~') 4158 type = EXPR_NOMATCH; 4159 break; 4160 case '>': if (p[1] != '=') 4161 { 4162 type = EXPR_GREATER; 4163 *len = 1; 4164 } 4165 else 4166 type = EXPR_GEQUAL; 4167 break; 4168 case '<': if (p[1] != '=') 4169 { 4170 type = EXPR_SMALLER; 4171 *len = 1; 4172 } 4173 else 4174 type = EXPR_SEQUAL; 4175 break; 4176 case 'i': if (p[1] == 's') 4177 { 4178 // "is" and "isnot"; but not a prefix of a name 4179 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') 4180 *len = 5; 4181 i = p[*len]; 4182 if (!isalnum(i) && i != '_') 4183 { 4184 type = *len == 2 ? EXPR_IS : EXPR_ISNOT; 4185 *type_is = TRUE; 4186 } 4187 } 4188 break; 4189 } 4190 return type; 4191 } 4192 4193 /* 4194 * Skip over an expression, ignoring most errors. 4195 */ 4196 static void 4197 skip_expr_cctx(char_u **arg, cctx_T *cctx) 4198 { 4199 evalarg_T evalarg; 4200 4201 CLEAR_FIELD(evalarg); 4202 evalarg.eval_cctx = cctx; 4203 skip_expr(arg, &evalarg); 4204 } 4205 4206 /* 4207 * Compile code to apply '-', '+' and '!'. 4208 * When "numeric_only" is TRUE do not apply '!'. 4209 */ 4210 static int 4211 compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end) 4212 { 4213 char_u *p = *end; 4214 4215 // this works from end to start 4216 while (p > start) 4217 { 4218 --p; 4219 while (VIM_ISWHITE(*p)) 4220 --p; 4221 if (*p == '-' || *p == '+') 4222 { 4223 int negate = *p == '-'; 4224 isn_T *isn; 4225 garray_T *stack = &cctx->ctx_type_stack; 4226 type_T *type; 4227 4228 type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 4229 if (need_type(type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL) 4230 return FAIL; 4231 4232 while (p > start && (p[-1] == '-' || p[-1] == '+')) 4233 { 4234 --p; 4235 if (*p == '-') 4236 negate = !negate; 4237 } 4238 // only '-' has an effect, for '+' we only check the type 4239 if (negate) 4240 { 4241 isn = generate_instr(cctx, ISN_NEGATENR); 4242 if (isn == NULL) 4243 return FAIL; 4244 } 4245 } 4246 else if (numeric_only) 4247 { 4248 ++p; 4249 break; 4250 } 4251 else 4252 { 4253 int invert = *p == '!'; 4254 4255 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1]))) 4256 { 4257 if (p[-1] == '!') 4258 invert = !invert; 4259 --p; 4260 } 4261 if (generate_2BOOL(cctx, invert, -1) == FAIL) 4262 return FAIL; 4263 } 4264 } 4265 *end = p; 4266 return OK; 4267 } 4268 4269 /* 4270 * Compile "(expression)": recursive! 4271 * Return FAIL/OK. 4272 */ 4273 static int 4274 compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) 4275 { 4276 int ret; 4277 char_u *p = *arg + 1; 4278 4279 if (may_get_next_line_error(p, arg, cctx) == FAIL) 4280 return FAIL; 4281 if (ppconst->pp_used <= PPSIZE - 10) 4282 { 4283 ret = compile_expr1(arg, cctx, ppconst); 4284 } 4285 else 4286 { 4287 // Not enough space in ppconst, flush constants. 4288 if (generate_ppconst(cctx, ppconst) == FAIL) 4289 return FAIL; 4290 ret = compile_expr0(arg, cctx); 4291 } 4292 if (may_get_next_line_error(*arg, arg, cctx) == FAIL) 4293 return FAIL; 4294 if (**arg == ')') 4295 ++*arg; 4296 else if (ret == OK) 4297 { 4298 emsg(_(e_missing_close)); 4299 ret = FAIL; 4300 } 4301 return ret; 4302 } 4303 4304 /* 4305 * Compile whatever comes after "name" or "name()". 4306 * Advances "*arg" only when something was recognized. 4307 */ 4308 static int 4309 compile_subscript( 4310 char_u **arg, 4311 cctx_T *cctx, 4312 char_u *start_leader, 4313 char_u **end_leader, 4314 ppconst_T *ppconst) 4315 { 4316 char_u *name_start = *end_leader; 4317 4318 for (;;) 4319 { 4320 char_u *p = skipwhite(*arg); 4321 4322 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) 4323 { 4324 char_u *next = peek_next_line_from_context(cctx); 4325 4326 // If a following line starts with "->{" or "->X" advance to that 4327 // line, so that a line break before "->" is allowed. 4328 // Also if a following line starts with ".x". 4329 if (next != NULL && 4330 ((next[0] == '-' && next[1] == '>' 4331 && (next[2] == '{' 4332 || ASCII_ISALPHA(*skipwhite(next + 2)))) 4333 || (next[0] == '.' && eval_isdictc(next[1])))) 4334 { 4335 next = next_line_from_context(cctx, TRUE); 4336 if (next == NULL) 4337 return FAIL; 4338 *arg = next; 4339 p = skipwhite(*arg); 4340 } 4341 } 4342 4343 // Do not skip over white space to find the "(", "execute 'x' (expr)" 4344 // is not a function call. 4345 if (**arg == '(') 4346 { 4347 garray_T *stack = &cctx->ctx_type_stack; 4348 type_T *type; 4349 int argcount = 0; 4350 4351 if (generate_ppconst(cctx, ppconst) == FAIL) 4352 return FAIL; 4353 ppconst->pp_is_const = FALSE; 4354 4355 // funcref(arg) 4356 type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 4357 4358 *arg = skipwhite(p + 1); 4359 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL) 4360 return FAIL; 4361 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL) 4362 return FAIL; 4363 } 4364 else if (*p == '-' && p[1] == '>') 4365 { 4366 char_u *pstart = p; 4367 4368 if (generate_ppconst(cctx, ppconst) == FAIL) 4369 return FAIL; 4370 ppconst->pp_is_const = FALSE; 4371 4372 // something->method() 4373 // Apply the '!', '-' and '+' first: 4374 // -1.0->func() works like (-1.0)->func() 4375 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL) 4376 return FAIL; 4377 4378 p += 2; 4379 *arg = skipwhite(p); 4380 // No line break supported right after "->". 4381 if (**arg == '(') 4382 { 4383 int argcount = 1; 4384 garray_T *stack = &cctx->ctx_type_stack; 4385 int type_idx_start = stack->ga_len; 4386 type_T *type; 4387 int expr_isn_start = cctx->ctx_instr.ga_len; 4388 int expr_isn_end; 4389 int arg_isn_count; 4390 4391 // Funcref call: list->(Refs[2])(arg) 4392 // or lambda: list->((arg) => expr)(arg) 4393 // 4394 // Fist compile the function expression. 4395 if (compile_parenthesis(arg, cctx, ppconst) == FAIL) 4396 return FAIL; 4397 4398 // Remember the next instruction index, where the instructions 4399 // for arguments are being written. 4400 expr_isn_end = cctx->ctx_instr.ga_len; 4401 4402 // Compile the arguments. 4403 if (**arg != '(') 4404 { 4405 if (*skipwhite(*arg) == '(') 4406 emsg(_(e_nowhitespace)); 4407 else 4408 semsg(_(e_missing_paren), *arg); 4409 return FAIL; 4410 } 4411 *arg = skipwhite(*arg + 1); 4412 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL) 4413 return FAIL; 4414 4415 // Move the instructions for the arguments to before the 4416 // instructions of the expression and move the type of the 4417 // expression after the argument types. This is what ISN_PCALL 4418 // expects. 4419 stack = &cctx->ctx_type_stack; 4420 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end; 4421 if (arg_isn_count > 0) 4422 { 4423 int expr_isn_count = expr_isn_end - expr_isn_start; 4424 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count); 4425 4426 if (isn == NULL) 4427 return FAIL; 4428 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data) 4429 + expr_isn_start, 4430 sizeof(isn_T) * expr_isn_count); 4431 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data) 4432 + expr_isn_start, 4433 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end, 4434 sizeof(isn_T) * arg_isn_count); 4435 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data) 4436 + expr_isn_start + arg_isn_count, 4437 isn, sizeof(isn_T) * expr_isn_count); 4438 vim_free(isn); 4439 4440 type = ((type_T **)stack->ga_data)[type_idx_start]; 4441 mch_memmove(((type_T **)stack->ga_data) + type_idx_start, 4442 ((type_T **)stack->ga_data) + type_idx_start + 1, 4443 sizeof(type_T *) 4444 * (stack->ga_len - type_idx_start - 1)); 4445 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; 4446 } 4447 4448 type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 4449 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL) 4450 return FAIL; 4451 } 4452 else 4453 { 4454 // method call: list->method() 4455 p = *arg; 4456 if (!eval_isnamec1(*p)) 4457 { 4458 semsg(_(e_trailing_arg), pstart); 4459 return FAIL; 4460 } 4461 if (ASCII_ISALPHA(*p) && p[1] == ':') 4462 p += 2; 4463 for ( ; eval_isnamec(*p); ++p) 4464 ; 4465 if (*p != '(') 4466 { 4467 semsg(_(e_missing_paren), *arg); 4468 return FAIL; 4469 } 4470 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) 4471 return FAIL; 4472 } 4473 } 4474 else if (**arg == '[') 4475 { 4476 int is_slice = FALSE; 4477 4478 // list index: list[123] 4479 // dict member: dict[key] 4480 // string index: text[123] 4481 // blob index: blob[123] 4482 if (generate_ppconst(cctx, ppconst) == FAIL) 4483 return FAIL; 4484 ppconst->pp_is_const = FALSE; 4485 4486 ++p; 4487 if (may_get_next_line_error(p, arg, cctx) == FAIL) 4488 return FAIL; 4489 if (**arg == ':') 4490 { 4491 // missing first index is equal to zero 4492 generate_PUSHNR(cctx, 0); 4493 } 4494 else 4495 { 4496 if (compile_expr0(arg, cctx) == FAIL) 4497 return FAIL; 4498 if (**arg == ':') 4499 { 4500 semsg(_(e_white_space_required_before_and_after_str_at_str), 4501 ":", *arg); 4502 return FAIL; 4503 } 4504 if (may_get_next_line_error(*arg, arg, cctx) == FAIL) 4505 return FAIL; 4506 *arg = skipwhite(*arg); 4507 } 4508 if (**arg == ':') 4509 { 4510 is_slice = TRUE; 4511 ++*arg; 4512 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']') 4513 { 4514 semsg(_(e_white_space_required_before_and_after_str_at_str), 4515 ":", *arg); 4516 return FAIL; 4517 } 4518 if (may_get_next_line_error(*arg, arg, cctx) == FAIL) 4519 return FAIL; 4520 if (**arg == ']') 4521 // missing second index is equal to end of string 4522 generate_PUSHNR(cctx, -1); 4523 else 4524 { 4525 if (compile_expr0(arg, cctx) == FAIL) 4526 return FAIL; 4527 if (may_get_next_line_error(*arg, arg, cctx) == FAIL) 4528 return FAIL; 4529 *arg = skipwhite(*arg); 4530 } 4531 } 4532 4533 if (**arg != ']') 4534 { 4535 emsg(_(e_missbrac)); 4536 return FAIL; 4537 } 4538 *arg = *arg + 1; 4539 4540 if (compile_member(is_slice, cctx) == FAIL) 4541 return FAIL; 4542 } 4543 else if (*p == '.' && p[1] != '.') 4544 { 4545 // dictionary member: dict.name 4546 if (generate_ppconst(cctx, ppconst) == FAIL) 4547 return FAIL; 4548 ppconst->pp_is_const = FALSE; 4549 4550 *arg = p + 1; 4551 if (IS_WHITE_OR_NUL(**arg)) 4552 { 4553 emsg(_(e_missing_name_after_dot)); 4554 return FAIL; 4555 } 4556 p = *arg; 4557 if (eval_isdictc(*p)) 4558 while (eval_isnamec(*p)) 4559 MB_PTR_ADV(p); 4560 if (p == *arg) 4561 { 4562 semsg(_(e_syntax_error_at_str), *arg); 4563 return FAIL; 4564 } 4565 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) 4566 return FAIL; 4567 *arg = p; 4568 } 4569 else 4570 break; 4571 } 4572 4573 // TODO - see handle_subscript(): 4574 // Turn "dict.Func" into a partial for "Func" bound to "dict". 4575 // Don't do this when "Func" is already a partial that was bound 4576 // explicitly (pt_auto is FALSE). 4577 4578 return OK; 4579 } 4580 4581 /* 4582 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". 4583 * "arg" is advanced until after the expression, skipping white space. 4584 * 4585 * If the value is a constant "ppconst->pp_used" will be non-zero. 4586 * Before instructions are generated, any values in "ppconst" will generated. 4587 * 4588 * This is the compiling equivalent of eval1(), eval2(), etc. 4589 */ 4590 4591 /* 4592 * number number constant 4593 * 0zFFFFFFFF Blob constant 4594 * "string" string constant 4595 * 'string' literal string constant 4596 * &option-name option value 4597 * @r register contents 4598 * identifier variable value 4599 * function() function call 4600 * $VAR environment variable 4601 * (expression) nested expression 4602 * [expr, expr] List 4603 * {key: val, [key]: val} Dictionary 4604 * 4605 * Also handle: 4606 * ! in front logical NOT 4607 * - in front unary minus 4608 * + in front unary plus (ignored) 4609 * trailing (arg) funcref/partial call 4610 * trailing [] subscript in String or List 4611 * trailing .name entry in Dictionary 4612 * trailing ->name() method call 4613 */ 4614 static int 4615 compile_expr7( 4616 char_u **arg, 4617 cctx_T *cctx, 4618 ppconst_T *ppconst) 4619 { 4620 char_u *start_leader, *end_leader; 4621 int ret = OK; 4622 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; 4623 int used_before = ppconst->pp_used; 4624 4625 ppconst->pp_is_const = FALSE; 4626 4627 /* 4628 * Skip '!', '-' and '+' characters. They are handled later. 4629 */ 4630 start_leader = *arg; 4631 if (eval_leader(arg, TRUE) == FAIL) 4632 return FAIL; 4633 end_leader = *arg; 4634 4635 rettv->v_type = VAR_UNKNOWN; 4636 switch (**arg) 4637 { 4638 /* 4639 * Number constant. 4640 */ 4641 case '0': // also for blob starting with 0z 4642 case '1': 4643 case '2': 4644 case '3': 4645 case '4': 4646 case '5': 4647 case '6': 4648 case '7': 4649 case '8': 4650 case '9': 4651 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) 4652 return FAIL; 4653 // Apply "-" and "+" just before the number now, right to 4654 // left. Matters especially when "->" follows. Stops at 4655 // '!'. 4656 if (apply_leader(rettv, TRUE, 4657 start_leader, &end_leader) == FAIL) 4658 { 4659 clear_tv(rettv); 4660 return FAIL; 4661 } 4662 break; 4663 4664 /* 4665 * String constant: "string". 4666 */ 4667 case '"': if (eval_string(arg, rettv, TRUE) == FAIL) 4668 return FAIL; 4669 break; 4670 4671 /* 4672 * Literal string constant: 'str''ing'. 4673 */ 4674 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) 4675 return FAIL; 4676 break; 4677 4678 /* 4679 * Constant Vim variable. 4680 */ 4681 case 'v': get_vim_constant(arg, rettv); 4682 ret = NOTDONE; 4683 break; 4684 4685 /* 4686 * "true" constant 4687 */ 4688 case 't': if (STRNCMP(*arg, "true", 4) == 0 4689 && !eval_isnamec((*arg)[4])) 4690 { 4691 *arg += 4; 4692 rettv->v_type = VAR_BOOL; 4693 rettv->vval.v_number = VVAL_TRUE; 4694 } 4695 else 4696 ret = NOTDONE; 4697 break; 4698 4699 /* 4700 * "false" constant 4701 */ 4702 case 'f': if (STRNCMP(*arg, "false", 5) == 0 4703 && !eval_isnamec((*arg)[5])) 4704 { 4705 *arg += 5; 4706 rettv->v_type = VAR_BOOL; 4707 rettv->vval.v_number = VVAL_FALSE; 4708 } 4709 else 4710 ret = NOTDONE; 4711 break; 4712 4713 /* 4714 * "null" constant 4715 */ 4716 case 'n': if (STRNCMP(*arg, "null", 4) == 0 4717 && !eval_isnamec((*arg)[4])) 4718 { 4719 *arg += 4; 4720 rettv->v_type = VAR_SPECIAL; 4721 rettv->vval.v_number = VVAL_NULL; 4722 } 4723 else 4724 ret = NOTDONE; 4725 break; 4726 4727 /* 4728 * List: [expr, expr] 4729 */ 4730 case '[': if (generate_ppconst(cctx, ppconst) == FAIL) 4731 return FAIL; 4732 ret = compile_list(arg, cctx, ppconst); 4733 break; 4734 4735 /* 4736 * Dictionary: {'key': val, 'key': val} 4737 */ 4738 case '{': if (generate_ppconst(cctx, ppconst) == FAIL) 4739 return FAIL; 4740 ret = compile_dict(arg, cctx, ppconst); 4741 break; 4742 4743 /* 4744 * Option value: &name 4745 */ 4746 case '&': if (generate_ppconst(cctx, ppconst) == FAIL) 4747 return FAIL; 4748 ret = compile_get_option(arg, cctx); 4749 break; 4750 4751 /* 4752 * Environment variable: $VAR. 4753 */ 4754 case '$': if (generate_ppconst(cctx, ppconst) == FAIL) 4755 return FAIL; 4756 ret = compile_get_env(arg, cctx); 4757 break; 4758 4759 /* 4760 * Register contents: @r. 4761 */ 4762 case '@': if (generate_ppconst(cctx, ppconst) == FAIL) 4763 return FAIL; 4764 ret = compile_get_register(arg, cctx); 4765 break; 4766 /* 4767 * nested expression: (expression). 4768 * lambda: (arg, arg) => expr 4769 * funcref: (arg, arg) => { statement } 4770 */ 4771 case '(': // if compile_lambda returns NOTDONE then it must be (expr) 4772 ret = compile_lambda(arg, cctx); 4773 if (ret == NOTDONE) 4774 ret = compile_parenthesis(arg, cctx, ppconst); 4775 break; 4776 4777 default: ret = NOTDONE; 4778 break; 4779 } 4780 if (ret == FAIL) 4781 return FAIL; 4782 4783 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) 4784 { 4785 if (cctx->ctx_skip == SKIP_YES) 4786 clear_tv(rettv); 4787 else 4788 // A constant expression can possibly be handled compile time, 4789 // return the value instead of generating code. 4790 ++ppconst->pp_used; 4791 } 4792 else if (ret == NOTDONE) 4793 { 4794 char_u *p; 4795 int r; 4796 4797 if (!eval_isnamec1(**arg)) 4798 { 4799 if (!vim9_bad_comment(*arg)) 4800 { 4801 if (ends_excmd(*skipwhite(*arg))) 4802 semsg(_(e_empty_expression_str), *arg); 4803 else 4804 semsg(_(e_name_expected_str), *arg); 4805 } 4806 return FAIL; 4807 } 4808 4809 // "name" or "name()" 4810 p = to_name_end(*arg, TRUE); 4811 if (p - *arg == (size_t)1 && **arg == '_') 4812 { 4813 emsg(_(e_cannot_use_underscore_here)); 4814 return FAIL; 4815 } 4816 4817 if (*p == '(') 4818 { 4819 r = compile_call(arg, p - *arg, cctx, ppconst, 0); 4820 } 4821 else 4822 { 4823 if (generate_ppconst(cctx, ppconst) == FAIL) 4824 return FAIL; 4825 r = compile_load(arg, p, cctx, TRUE, TRUE); 4826 } 4827 if (r == FAIL) 4828 return FAIL; 4829 } 4830 4831 // Handle following "[]", ".member", etc. 4832 // Then deal with prefixed '-', '+' and '!', if not done already. 4833 if (compile_subscript(arg, cctx, start_leader, &end_leader, 4834 ppconst) == FAIL) 4835 return FAIL; 4836 if (ppconst->pp_used > 0) 4837 { 4838 // apply the '!', '-' and '+' before the constant 4839 rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; 4840 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL) 4841 return FAIL; 4842 return OK; 4843 } 4844 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL) 4845 return FAIL; 4846 return OK; 4847 } 4848 4849 /* 4850 * Give the "white on both sides" error, taking the operator from "p[len]". 4851 */ 4852 void 4853 error_white_both(char_u *op, int len) 4854 { 4855 char_u buf[10]; 4856 4857 vim_strncpy(buf, op, len); 4858 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op); 4859 } 4860 4861 /* 4862 * <type>expr7: runtime type check / conversion 4863 */ 4864 static int 4865 compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) 4866 { 4867 type_T *want_type = NULL; 4868 4869 // Recognize <type> 4870 if (**arg == '<' && eval_isnamec1((*arg)[1])) 4871 { 4872 ++*arg; 4873 want_type = parse_type(arg, cctx->ctx_type_list, TRUE); 4874 if (want_type == NULL) 4875 return FAIL; 4876 4877 if (**arg != '>') 4878 { 4879 if (*skipwhite(*arg) == '>') 4880 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg); 4881 else 4882 emsg(_(e_missing_gt)); 4883 return FAIL; 4884 } 4885 ++*arg; 4886 if (may_get_next_line_error(*arg, arg, cctx) == FAIL) 4887 return FAIL; 4888 } 4889 4890 if (compile_expr7(arg, cctx, ppconst) == FAIL) 4891 return FAIL; 4892 4893 if (want_type != NULL) 4894 { 4895 garray_T *stack = &cctx->ctx_type_stack; 4896 type_T *actual; 4897 where_T where = WHERE_INIT; 4898 4899 generate_ppconst(cctx, ppconst); 4900 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 4901 if (check_type(want_type, actual, FALSE, where) == FAIL) 4902 { 4903 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL) 4904 return FAIL; 4905 } 4906 } 4907 4908 return OK; 4909 } 4910 4911 /* 4912 * * number multiplication 4913 * / number division 4914 * % number modulo 4915 */ 4916 static int 4917 compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) 4918 { 4919 char_u *op; 4920 char_u *next; 4921 int ppconst_used = ppconst->pp_used; 4922 4923 // get the first expression 4924 if (compile_expr7t(arg, cctx, ppconst) == FAIL) 4925 return FAIL; 4926 4927 /* 4928 * Repeat computing, until no "*", "/" or "%" is following. 4929 */ 4930 for (;;) 4931 { 4932 op = may_peek_next_line(cctx, *arg, &next); 4933 if (*op != '*' && *op != '/' && *op != '%') 4934 break; 4935 if (next != NULL) 4936 { 4937 *arg = next_line_from_context(cctx, TRUE); 4938 op = skipwhite(*arg); 4939 } 4940 4941 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) 4942 { 4943 error_white_both(op, 1); 4944 return FAIL; 4945 } 4946 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL) 4947 return FAIL; 4948 4949 // get the second expression 4950 if (compile_expr7t(arg, cctx, ppconst) == FAIL) 4951 return FAIL; 4952 4953 if (ppconst->pp_used == ppconst_used + 2 4954 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER 4955 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) 4956 { 4957 typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; 4958 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; 4959 varnumber_T res = 0; 4960 int failed = FALSE; 4961 4962 // both are numbers: compute the result 4963 switch (*op) 4964 { 4965 case '*': res = tv1->vval.v_number * tv2->vval.v_number; 4966 break; 4967 case '/': res = num_divide(tv1->vval.v_number, 4968 tv2->vval.v_number, &failed); 4969 break; 4970 case '%': res = num_modulus(tv1->vval.v_number, 4971 tv2->vval.v_number, &failed); 4972 break; 4973 } 4974 if (failed) 4975 return FAIL; 4976 tv1->vval.v_number = res; 4977 --ppconst->pp_used; 4978 } 4979 else 4980 { 4981 generate_ppconst(cctx, ppconst); 4982 generate_two_op(cctx, op); 4983 } 4984 } 4985 4986 return OK; 4987 } 4988 4989 /* 4990 * + number addition or list/blobl concatenation 4991 * - number subtraction 4992 * .. string concatenation 4993 */ 4994 static int 4995 compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) 4996 { 4997 char_u *op; 4998 char_u *next; 4999 int oplen; 5000 int ppconst_used = ppconst->pp_used; 5001 5002 // get the first variable 5003 if (compile_expr6(arg, cctx, ppconst) == FAIL) 5004 return FAIL; 5005 5006 /* 5007 * Repeat computing, until no "+", "-" or ".." is following. 5008 */ 5009 for (;;) 5010 { 5011 op = may_peek_next_line(cctx, *arg, &next); 5012 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) 5013 break; 5014 if (op[0] == op[1] && *op != '.' && next) 5015 // Finding "++" or "--" on the next line is a separate command. 5016 // But ".." is concatenation. 5017 break; 5018 oplen = (*op == '.' ? 2 : 1); 5019 if (next != NULL) 5020 { 5021 *arg = next_line_from_context(cctx, TRUE); 5022 op = skipwhite(*arg); 5023 } 5024 5025 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) 5026 { 5027 error_white_both(op, oplen); 5028 return FAIL; 5029 } 5030 5031 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL) 5032 return FAIL; 5033 5034 // get the second expression 5035 if (compile_expr6(arg, cctx, ppconst) == FAIL) 5036 return FAIL; 5037 5038 if (ppconst->pp_used == ppconst_used + 2 5039 && (*op == '.' 5040 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING 5041 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) 5042 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER 5043 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) 5044 { 5045 typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; 5046 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; 5047 5048 // concat/subtract/add constant numbers 5049 if (*op == '+') 5050 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; 5051 else if (*op == '-') 5052 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; 5053 else 5054 { 5055 // concatenate constant strings 5056 char_u *s1 = tv1->vval.v_string; 5057 char_u *s2 = tv2->vval.v_string; 5058 size_t len1 = STRLEN(s1); 5059 5060 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); 5061 if (tv1->vval.v_string == NULL) 5062 { 5063 clear_ppconst(ppconst); 5064 return FAIL; 5065 } 5066 mch_memmove(tv1->vval.v_string, s1, len1); 5067 STRCPY(tv1->vval.v_string + len1, s2); 5068 vim_free(s1); 5069 vim_free(s2); 5070 } 5071 --ppconst->pp_used; 5072 } 5073 else 5074 { 5075 generate_ppconst(cctx, ppconst); 5076 ppconst->pp_is_const = FALSE; 5077 if (*op == '.') 5078 { 5079 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL 5080 || may_generate_2STRING(-1, FALSE, cctx) == FAIL) 5081 return FAIL; 5082 generate_instr_drop(cctx, ISN_CONCAT, 1); 5083 } 5084 else 5085 generate_two_op(cctx, op); 5086 } 5087 } 5088 5089 return OK; 5090 } 5091 5092 /* 5093 * expr5a == expr5b 5094 * expr5a =~ expr5b 5095 * expr5a != expr5b 5096 * expr5a !~ expr5b 5097 * expr5a > expr5b 5098 * expr5a >= expr5b 5099 * expr5a < expr5b 5100 * expr5a <= expr5b 5101 * expr5a is expr5b 5102 * expr5a isnot expr5b 5103 * 5104 * Produces instructions: 5105 * EVAL expr5a Push result of "expr5a" 5106 * EVAL expr5b Push result of "expr5b" 5107 * COMPARE one of the compare instructions 5108 */ 5109 static int 5110 compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) 5111 { 5112 exprtype_T type = EXPR_UNKNOWN; 5113 char_u *p; 5114 char_u *next; 5115 int len = 2; 5116 int type_is = FALSE; 5117 int ppconst_used = ppconst->pp_used; 5118 5119 // get the first variable 5120 if (compile_expr5(arg, cctx, ppconst) == FAIL) 5121 return FAIL; 5122 5123 p = may_peek_next_line(cctx, *arg, &next); 5124 type = get_compare_type(p, &len, &type_is); 5125 5126 /* 5127 * If there is a comparative operator, use it. 5128 */ 5129 if (type != EXPR_UNKNOWN) 5130 { 5131 int ic = FALSE; // Default: do not ignore case 5132 5133 if (next != NULL) 5134 { 5135 *arg = next_line_from_context(cctx, TRUE); 5136 p = skipwhite(*arg); 5137 } 5138 if (type_is && (p[len] == '?' || p[len] == '#')) 5139 { 5140 semsg(_(e_invalid_expression_str), *arg); 5141 return FAIL; 5142 } 5143 // extra question mark appended: ignore case 5144 if (p[len] == '?') 5145 { 5146 ic = TRUE; 5147 ++len; 5148 } 5149 // extra '#' appended: match case (ignored) 5150 else if (p[len] == '#') 5151 ++len; 5152 // nothing appended: match case 5153 5154 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) 5155 { 5156 error_white_both(p, len); 5157 return FAIL; 5158 } 5159 5160 // get the second variable 5161 if (may_get_next_line_error(p + len, arg, cctx) == FAIL) 5162 return FAIL; 5163 5164 if (compile_expr5(arg, cctx, ppconst) == FAIL) 5165 return FAIL; 5166 5167 if (ppconst->pp_used == ppconst_used + 2) 5168 { 5169 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; 5170 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; 5171 int ret; 5172 5173 // Both sides are a constant, compute the result now. 5174 // First check for a valid combination of types, this is more 5175 // strict than typval_compare(). 5176 if (check_compare_types(type, tv1, tv2) == FAIL) 5177 ret = FAIL; 5178 else 5179 { 5180 ret = typval_compare(tv1, tv2, type, ic); 5181 tv1->v_type = VAR_BOOL; 5182 tv1->vval.v_number = tv1->vval.v_number 5183 ? VVAL_TRUE : VVAL_FALSE; 5184 clear_tv(tv2); 5185 --ppconst->pp_used; 5186 } 5187 return ret; 5188 } 5189 5190 generate_ppconst(cctx, ppconst); 5191 return generate_COMPARE(cctx, type, ic); 5192 } 5193 5194 return OK; 5195 } 5196 5197 static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); 5198 5199 /* 5200 * Compile || or &&. 5201 */ 5202 static int 5203 compile_and_or( 5204 char_u **arg, 5205 cctx_T *cctx, 5206 char *op, 5207 ppconst_T *ppconst, 5208 int ppconst_used UNUSED) 5209 { 5210 char_u *next; 5211 char_u *p = may_peek_next_line(cctx, *arg, &next); 5212 int opchar = *op; 5213 5214 if (p[0] == opchar && p[1] == opchar) 5215 { 5216 garray_T *instr = &cctx->ctx_instr; 5217 garray_T end_ga; 5218 5219 /* 5220 * Repeat until there is no following "||" or "&&" 5221 */ 5222 ga_init2(&end_ga, sizeof(int), 10); 5223 while (p[0] == opchar && p[1] == opchar) 5224 { 5225 long start_lnum = SOURCING_LNUM; 5226 long save_sourcing_lnum; 5227 int start_ctx_lnum = cctx->ctx_lnum; 5228 int save_lnum; 5229 int status; 5230 5231 if (next != NULL) 5232 { 5233 *arg = next_line_from_context(cctx, TRUE); 5234 p = skipwhite(*arg); 5235 } 5236 5237 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) 5238 { 5239 semsg(_(e_white_space_required_before_and_after_str_at_str), 5240 op, p); 5241 return FAIL; 5242 } 5243 5244 save_sourcing_lnum = SOURCING_LNUM; 5245 SOURCING_LNUM = start_lnum; 5246 save_lnum = cctx->ctx_lnum; 5247 cctx->ctx_lnum = start_ctx_lnum; 5248 5249 status = check_ppconst_bool(ppconst); 5250 if (status != FAIL) 5251 { 5252 // TODO: use ppconst if the value is a constant 5253 generate_ppconst(cctx, ppconst); 5254 5255 // Every part must evaluate to a bool. 5256 status = bool_on_stack(cctx); 5257 if (status != FAIL) 5258 status = ga_grow(&end_ga, 1); 5259 } 5260 cctx->ctx_lnum = save_lnum; 5261 if (status == FAIL) 5262 { 5263 ga_clear(&end_ga); 5264 return FAIL; 5265 } 5266 5267 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; 5268 ++end_ga.ga_len; 5269 generate_JUMP(cctx, opchar == '|' 5270 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0); 5271 5272 // eval the next expression 5273 SOURCING_LNUM = save_sourcing_lnum; 5274 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL) 5275 { 5276 ga_clear(&end_ga); 5277 return FAIL; 5278 } 5279 5280 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) 5281 : compile_expr4(arg, cctx, ppconst)) == FAIL) 5282 { 5283 ga_clear(&end_ga); 5284 return FAIL; 5285 } 5286 5287 p = may_peek_next_line(cctx, *arg, &next); 5288 } 5289 5290 if (check_ppconst_bool(ppconst) == FAIL) 5291 { 5292 ga_clear(&end_ga); 5293 return FAIL; 5294 } 5295 generate_ppconst(cctx, ppconst); 5296 5297 // Every part must evaluate to a bool. 5298 if (bool_on_stack(cctx) == FAIL) 5299 { 5300 ga_clear(&end_ga); 5301 return FAIL; 5302 } 5303 5304 // Fill in the end label in all jumps. 5305 while (end_ga.ga_len > 0) 5306 { 5307 isn_T *isn; 5308 5309 --end_ga.ga_len; 5310 isn = ((isn_T *)instr->ga_data) 5311 + *(((int *)end_ga.ga_data) + end_ga.ga_len); 5312 isn->isn_arg.jump.jump_where = instr->ga_len; 5313 } 5314 ga_clear(&end_ga); 5315 } 5316 5317 return OK; 5318 } 5319 5320 /* 5321 * expr4a && expr4a && expr4a logical AND 5322 * 5323 * Produces instructions: 5324 * EVAL expr4a Push result of "expr4a" 5325 * COND2BOOL convert to bool if needed 5326 * JUMP_IF_COND_FALSE end 5327 * EVAL expr4b Push result of "expr4b" 5328 * JUMP_IF_COND_FALSE end 5329 * EVAL expr4c Push result of "expr4c" 5330 * end: 5331 */ 5332 static int 5333 compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) 5334 { 5335 int ppconst_used = ppconst->pp_used; 5336 5337 // get the first variable 5338 if (compile_expr4(arg, cctx, ppconst) == FAIL) 5339 return FAIL; 5340 5341 // || and && work almost the same 5342 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); 5343 } 5344 5345 /* 5346 * expr3a || expr3b || expr3c logical OR 5347 * 5348 * Produces instructions: 5349 * EVAL expr3a Push result of "expr3a" 5350 * COND2BOOL convert to bool if needed 5351 * JUMP_IF_COND_TRUE end 5352 * EVAL expr3b Push result of "expr3b" 5353 * JUMP_IF_COND_TRUE end 5354 * EVAL expr3c Push result of "expr3c" 5355 * end: 5356 */ 5357 static int 5358 compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) 5359 { 5360 int ppconst_used = ppconst->pp_used; 5361 5362 // eval the first expression 5363 if (compile_expr3(arg, cctx, ppconst) == FAIL) 5364 return FAIL; 5365 5366 // || and && work almost the same 5367 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); 5368 } 5369 5370 /* 5371 * Toplevel expression: expr2 ? expr1a : expr1b 5372 * Produces instructions: 5373 * EVAL expr2 Push result of "expr2" 5374 * JUMP_IF_FALSE alt jump if false 5375 * EVAL expr1a 5376 * JUMP_ALWAYS end 5377 * alt: EVAL expr1b 5378 * end: 5379 * 5380 * Toplevel expression: expr2 ?? expr1 5381 * Produces instructions: 5382 * EVAL expr2 Push result of "expr2" 5383 * JUMP_AND_KEEP_IF_TRUE end jump if true 5384 * EVAL expr1 5385 * end: 5386 */ 5387 static int 5388 compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) 5389 { 5390 char_u *p; 5391 int ppconst_used = ppconst->pp_used; 5392 char_u *next; 5393 5394 // Ignore all kinds of errors when not producing code. 5395 if (cctx->ctx_skip == SKIP_YES) 5396 { 5397 skip_expr_cctx(arg, cctx); 5398 return OK; 5399 } 5400 5401 // Evaluate the first expression. 5402 if (compile_expr2(arg, cctx, ppconst) == FAIL) 5403 return FAIL; 5404 5405 p = may_peek_next_line(cctx, *arg, &next); 5406 if (*p == '?') 5407 { 5408 int op_falsy = p[1] == '?'; 5409 garray_T *instr = &cctx->ctx_instr; 5410 garray_T *stack = &cctx->ctx_type_stack; 5411 int alt_idx = instr->ga_len; 5412 int end_idx = 0; 5413 isn_T *isn; 5414 type_T *type1 = NULL; 5415 int has_const_expr = FALSE; 5416 int const_value = FALSE; 5417 int save_skip = cctx->ctx_skip; 5418 5419 if (next != NULL) 5420 { 5421 *arg = next_line_from_context(cctx, TRUE); 5422 p = skipwhite(*arg); 5423 } 5424 5425 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy])) 5426 { 5427 semsg(_(e_white_space_required_before_and_after_str_at_str), 5428 op_falsy ? "??" : "?", p); 5429 return FAIL; 5430 } 5431 5432 if (ppconst->pp_used == ppconst_used + 1) 5433 { 5434 // the condition is a constant, we know whether the ? or the : 5435 // expression is to be evaluated. 5436 has_const_expr = TRUE; 5437 if (op_falsy) 5438 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); 5439 else 5440 { 5441 int error = FALSE; 5442 5443 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used], 5444 &error); 5445 if (error) 5446 return FAIL; 5447 } 5448 cctx->ctx_skip = save_skip == SKIP_YES || 5449 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT; 5450 5451 if (op_falsy && cctx->ctx_skip == SKIP_YES) 5452 // "left ?? right" and "left" is truthy: produce "left" 5453 generate_ppconst(cctx, ppconst); 5454 else 5455 { 5456 clear_tv(&ppconst->pp_tv[ppconst_used]); 5457 --ppconst->pp_used; 5458 } 5459 } 5460 else 5461 { 5462 generate_ppconst(cctx, ppconst); 5463 if (op_falsy) 5464 end_idx = instr->ga_len; 5465 generate_JUMP(cctx, op_falsy 5466 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0); 5467 if (op_falsy) 5468 type1 = ((type_T **)stack->ga_data)[stack->ga_len]; 5469 } 5470 5471 // evaluate the second expression; any type is accepted 5472 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL) 5473 return FAIL; 5474 if (compile_expr1(arg, cctx, ppconst) == FAIL) 5475 return FAIL; 5476 5477 if (!has_const_expr) 5478 { 5479 generate_ppconst(cctx, ppconst); 5480 5481 if (!op_falsy) 5482 { 5483 // remember the type and drop it 5484 --stack->ga_len; 5485 type1 = ((type_T **)stack->ga_data)[stack->ga_len]; 5486 5487 end_idx = instr->ga_len; 5488 generate_JUMP(cctx, JUMP_ALWAYS, 0); 5489 5490 // jump here from JUMP_IF_FALSE 5491 isn = ((isn_T *)instr->ga_data) + alt_idx; 5492 isn->isn_arg.jump.jump_where = instr->ga_len; 5493 } 5494 } 5495 5496 if (!op_falsy) 5497 { 5498 // Check for the ":". 5499 p = may_peek_next_line(cctx, *arg, &next); 5500 if (*p != ':') 5501 { 5502 emsg(_(e_missing_colon)); 5503 return FAIL; 5504 } 5505 if (next != NULL) 5506 { 5507 *arg = next_line_from_context(cctx, TRUE); 5508 p = skipwhite(*arg); 5509 } 5510 5511 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) 5512 { 5513 semsg(_(e_white_space_required_before_and_after_str_at_str), 5514 ":", p); 5515 return FAIL; 5516 } 5517 5518 // evaluate the third expression 5519 if (has_const_expr) 5520 cctx->ctx_skip = save_skip == SKIP_YES || const_value 5521 ? SKIP_YES : SKIP_NOT; 5522 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL) 5523 return FAIL; 5524 if (compile_expr1(arg, cctx, ppconst) == FAIL) 5525 return FAIL; 5526 } 5527 5528 if (!has_const_expr) 5529 { 5530 type_T **typep; 5531 5532 generate_ppconst(cctx, ppconst); 5533 5534 // If the types differ, the result has a more generic type. 5535 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1; 5536 common_type(type1, *typep, typep, cctx->ctx_type_list); 5537 5538 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE 5539 isn = ((isn_T *)instr->ga_data) + end_idx; 5540 isn->isn_arg.jump.jump_where = instr->ga_len; 5541 } 5542 5543 cctx->ctx_skip = save_skip; 5544 } 5545 return OK; 5546 } 5547 5548 /* 5549 * Toplevel expression. 5550 * Sets "is_const" (if not NULL) to indicate the value is a constant. 5551 * Returns OK or FAIL. 5552 */ 5553 static int 5554 compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const) 5555 { 5556 ppconst_T ppconst; 5557 5558 CLEAR_FIELD(ppconst); 5559 if (compile_expr1(arg, cctx, &ppconst) == FAIL) 5560 { 5561 clear_ppconst(&ppconst); 5562 return FAIL; 5563 } 5564 if (is_const != NULL) 5565 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const; 5566 if (generate_ppconst(cctx, &ppconst) == FAIL) 5567 return FAIL; 5568 return OK; 5569 } 5570 5571 /* 5572 * Toplevel expression. 5573 */ 5574 static int 5575 compile_expr0(char_u **arg, cctx_T *cctx) 5576 { 5577 return compile_expr0_ext(arg, cctx, NULL); 5578 } 5579 5580 /* 5581 * Compile "return [expr]". 5582 * When "legacy" is TRUE evaluate [expr] with legacy syntax 5583 */ 5584 static char_u * 5585 compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx) 5586 { 5587 char_u *p = arg; 5588 garray_T *stack = &cctx->ctx_type_stack; 5589 type_T *stack_type; 5590 5591 if (*p != NUL && *p != '|' && *p != '\n') 5592 { 5593 if (legacy) 5594 { 5595 int save_flags = cmdmod.cmod_flags; 5596 5597 generate_LEGACY_EVAL(cctx, p); 5598 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1, 5599 0, cctx, FALSE, FALSE) == FAIL) 5600 return NULL; 5601 cmdmod.cmod_flags |= CMOD_LEGACY; 5602 (void)skip_expr(&p, NULL); 5603 cmdmod.cmod_flags = save_flags; 5604 } 5605 else 5606 { 5607 // compile return argument into instructions 5608 if (compile_expr0(&p, cctx) == FAIL) 5609 return NULL; 5610 } 5611 5612 if (cctx->ctx_skip != SKIP_YES) 5613 { 5614 // "check_return_type" with uf_ret_type set to &t_unknown is used 5615 // for an inline function without a specified return type. Set the 5616 // return type here. 5617 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 5618 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL 5619 || cctx->ctx_ufunc->uf_ret_type == &t_unknown 5620 || cctx->ctx_ufunc->uf_ret_type == &t_any)) 5621 || (!check_return_type 5622 && cctx->ctx_ufunc->uf_ret_type == &t_unknown)) 5623 { 5624 cctx->ctx_ufunc->uf_ret_type = stack_type; 5625 } 5626 else 5627 { 5628 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID 5629 && stack_type->tt_type != VAR_VOID 5630 && stack_type->tt_type != VAR_UNKNOWN) 5631 { 5632 emsg(_(e_returning_value_in_function_without_return_type)); 5633 return NULL; 5634 } 5635 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, 5636 0, cctx, FALSE, FALSE) == FAIL) 5637 return NULL; 5638 } 5639 } 5640 } 5641 else 5642 { 5643 // "check_return_type" cannot be TRUE, only used for a lambda which 5644 // always has an argument. 5645 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID 5646 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) 5647 { 5648 emsg(_(e_missing_return_value)); 5649 return NULL; 5650 } 5651 5652 // No argument, return zero. 5653 generate_PUSHNR(cctx, 0); 5654 } 5655 5656 // Undo any command modifiers. 5657 generate_undo_cmdmods(cctx); 5658 5659 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL) 5660 return NULL; 5661 5662 // "return val | endif" is possible 5663 return skipwhite(p); 5664 } 5665 5666 /* 5667 * Get a line from the compilation context, compatible with exarg_T getline(). 5668 * Return a pointer to the line in allocated memory. 5669 * Return NULL for end-of-file or some error. 5670 */ 5671 static char_u * 5672 exarg_getline( 5673 int c UNUSED, 5674 void *cookie, 5675 int indent UNUSED, 5676 getline_opt_T options UNUSED) 5677 { 5678 cctx_T *cctx = (cctx_T *)cookie; 5679 char_u *p; 5680 5681 for (;;) 5682 { 5683 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1) 5684 return NULL; 5685 ++cctx->ctx_lnum; 5686 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]; 5687 // Comment lines result in NULL pointers, skip them. 5688 if (p != NULL) 5689 return vim_strsave(p); 5690 } 5691 } 5692 5693 void 5694 fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx) 5695 { 5696 eap->getline = exarg_getline; 5697 eap->cookie = cctx; 5698 } 5699 5700 /* 5701 * Compile a nested :def command. 5702 */ 5703 static char_u * 5704 compile_nested_function(exarg_T *eap, cctx_T *cctx) 5705 { 5706 int is_global = *eap->arg == 'g' && eap->arg[1] == ':'; 5707 char_u *name_start = eap->arg; 5708 char_u *name_end = to_name_end(eap->arg, TRUE); 5709 char_u *lambda_name; 5710 ufunc_T *ufunc; 5711 int r = FAIL; 5712 compiletype_T compile_type; 5713 5714 if (eap->forceit) 5715 { 5716 emsg(_(e_cannot_use_bang_with_nested_def)); 5717 return NULL; 5718 } 5719 5720 if (*name_start == '/') 5721 { 5722 name_end = skip_regexp(name_start + 1, '/', TRUE); 5723 if (*name_end == '/') 5724 ++name_end; 5725 set_nextcmd(eap, name_end); 5726 } 5727 if (name_end == name_start || *skipwhite(name_end) != '(') 5728 { 5729 if (!ends_excmd2(name_start, name_end)) 5730 { 5731 semsg(_(e_invalid_command_str), eap->cmd); 5732 return NULL; 5733 } 5734 5735 // "def" or "def Name": list functions 5736 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL) 5737 return NULL; 5738 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; 5739 } 5740 5741 // Only g:Func() can use a namespace. 5742 if (name_start[1] == ':' && !is_global) 5743 { 5744 semsg(_(e_namespace_not_supported_str), name_start); 5745 return NULL; 5746 } 5747 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL) 5748 return NULL; 5749 5750 eap->arg = name_end; 5751 fill_exarg_from_cctx(eap, cctx); 5752 5753 eap->forceit = FALSE; 5754 lambda_name = vim_strsave(get_lambda_name()); 5755 if (lambda_name == NULL) 5756 return NULL; 5757 ufunc = define_function(eap, lambda_name); 5758 5759 if (ufunc == NULL) 5760 { 5761 r = eap->skip ? OK : FAIL; 5762 goto theend; 5763 } 5764 5765 // copy over the block scope IDs before compiling 5766 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0) 5767 { 5768 int block_depth = cctx->ctx_ufunc->uf_block_depth; 5769 5770 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth); 5771 if (ufunc->uf_block_ids != NULL) 5772 { 5773 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids, 5774 sizeof(int) * block_depth); 5775 ufunc->uf_block_depth = block_depth; 5776 } 5777 } 5778 5779 compile_type = COMPILE_TYPE(ufunc); 5780 #ifdef FEAT_PROFILE 5781 // If the outer function is profiled, also compile the nested function for 5782 // profiling. 5783 if (cctx->ctx_compile_type == CT_PROFILE) 5784 compile_type = CT_PROFILE; 5785 #endif 5786 if (func_needs_compiling(ufunc, compile_type) 5787 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL) 5788 { 5789 func_ptr_unref(ufunc); 5790 goto theend; 5791 } 5792 5793 #ifdef FEAT_PROFILE 5794 // When the outer function is compiled for profiling, the nested function 5795 // may be called without profiling. Compile it here in the right context. 5796 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE)) 5797 compile_def_function(ufunc, FALSE, CT_NONE, cctx); 5798 #endif 5799 5800 if (is_global) 5801 { 5802 char_u *func_name = vim_strnsave(name_start + 2, 5803 name_end - name_start - 2); 5804 5805 if (func_name == NULL) 5806 r = FAIL; 5807 else 5808 { 5809 r = generate_NEWFUNC(cctx, lambda_name, func_name); 5810 lambda_name = NULL; 5811 } 5812 } 5813 else 5814 { 5815 // Define a local variable for the function reference. 5816 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start, 5817 TRUE, ufunc->uf_func_type); 5818 5819 if (lvar == NULL) 5820 goto theend; 5821 if (generate_FUNCREF(cctx, ufunc) == FAIL) 5822 goto theend; 5823 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); 5824 } 5825 5826 theend: 5827 vim_free(lambda_name); 5828 return r == FAIL ? NULL : (char_u *)""; 5829 } 5830 5831 /* 5832 * Return the length of an assignment operator, or zero if there isn't one. 5833 */ 5834 int 5835 assignment_len(char_u *p, int *heredoc) 5836 { 5837 if (*p == '=') 5838 { 5839 if (p[1] == '<' && p[2] == '<') 5840 { 5841 *heredoc = TRUE; 5842 return 3; 5843 } 5844 return 1; 5845 } 5846 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') 5847 return 2; 5848 if (STRNCMP(p, "..=", 3) == 0) 5849 return 3; 5850 return 0; 5851 } 5852 5853 /* 5854 * Generate the load instruction for "name". 5855 */ 5856 static void 5857 generate_loadvar( 5858 cctx_T *cctx, 5859 assign_dest_T dest, 5860 char_u *name, 5861 lvar_T *lvar, 5862 type_T *type) 5863 { 5864 switch (dest) 5865 { 5866 case dest_option: 5867 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); 5868 break; 5869 case dest_global: 5870 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL) 5871 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); 5872 else 5873 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type); 5874 break; 5875 case dest_buffer: 5876 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); 5877 break; 5878 case dest_window: 5879 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); 5880 break; 5881 case dest_tab: 5882 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); 5883 break; 5884 case dest_script: 5885 compile_load_scriptvar(cctx, 5886 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); 5887 break; 5888 case dest_env: 5889 // Include $ in the name here 5890 generate_LOAD(cctx, ISN_LOADENV, 0, name, type); 5891 break; 5892 case dest_reg: 5893 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); 5894 break; 5895 case dest_vimvar: 5896 generate_LOADV(cctx, name + 2, TRUE); 5897 break; 5898 case dest_local: 5899 if (lvar->lv_from_outer > 0) 5900 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer, 5901 type); 5902 else 5903 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); 5904 break; 5905 case dest_expr: 5906 // list or dict value should already be on the stack. 5907 break; 5908 } 5909 } 5910 5911 /* 5912 * Skip over "[expr]" or ".member". 5913 * Does not check for any errors. 5914 */ 5915 static char_u * 5916 skip_index(char_u *start) 5917 { 5918 char_u *p = start; 5919 5920 if (*p == '[') 5921 { 5922 p = skipwhite(p + 1); 5923 (void)skip_expr(&p, NULL); 5924 p = skipwhite(p); 5925 if (*p == ']') 5926 return p + 1; 5927 return p; 5928 } 5929 // if (*p == '.') 5930 return to_name_end(p + 1, TRUE); 5931 } 5932 5933 void 5934 vim9_declare_error(char_u *name) 5935 { 5936 char *scope = ""; 5937 5938 switch (*name) 5939 { 5940 case 'g': scope = _("global"); break; 5941 case 'b': scope = _("buffer"); break; 5942 case 'w': scope = _("window"); break; 5943 case 't': scope = _("tab"); break; 5944 case 'v': scope = "v:"; break; 5945 case '$': semsg(_(e_cannot_declare_an_environment_variable), name); 5946 return; 5947 case '&': semsg(_(e_cannot_declare_an_option), name); 5948 return; 5949 case '@': semsg(_(e_cannot_declare_a_register_str), name); 5950 return; 5951 default: return; 5952 } 5953 semsg(_(e_cannot_declare_a_scope_variable), scope, name); 5954 } 5955 5956 /* 5957 * For one assignment figure out the type of destination. Return it in "dest". 5958 * When not recognized "dest" is not set. 5959 * For an option "opt_flags" is set. 5960 * For a v:var "vimvaridx" is set. 5961 * "type" is set to the destination type if known, unchanted otherwise. 5962 * Return FAIL if an error message was given. 5963 */ 5964 static int 5965 get_var_dest( 5966 char_u *name, 5967 assign_dest_T *dest, 5968 int cmdidx, 5969 int *opt_flags, 5970 int *vimvaridx, 5971 type_T **type, 5972 cctx_T *cctx) 5973 { 5974 char_u *p; 5975 5976 if (*name == '&') 5977 { 5978 int cc; 5979 long numval; 5980 getoption_T opt_type; 5981 5982 *dest = dest_option; 5983 if (cmdidx == CMD_final || cmdidx == CMD_const) 5984 { 5985 emsg(_(e_const_option)); 5986 return FAIL; 5987 } 5988 p = name; 5989 p = find_option_end(&p, opt_flags); 5990 if (p == NULL) 5991 { 5992 // cannot happen? 5993 emsg(_(e_unexpected_characters_in_assignment)); 5994 return FAIL; 5995 } 5996 cc = *p; 5997 *p = NUL; 5998 opt_type = get_option_value(skip_option_env_lead(name), 5999 &numval, NULL, *opt_flags); 6000 *p = cc; 6001 switch (opt_type) 6002 { 6003 case gov_unknown: 6004 semsg(_(e_unknown_option), name); 6005 return FAIL; 6006 case gov_string: 6007 case gov_hidden_string: 6008 *type = &t_string; 6009 break; 6010 case gov_bool: 6011 case gov_hidden_bool: 6012 *type = &t_bool; 6013 break; 6014 case gov_number: 6015 case gov_hidden_number: 6016 *type = &t_number; 6017 break; 6018 } 6019 } 6020 else if (*name == '$') 6021 { 6022 *dest = dest_env; 6023 *type = &t_string; 6024 } 6025 else if (*name == '@') 6026 { 6027 if (name[1] != '@' 6028 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.')) 6029 { 6030 emsg_invreg(name[1]); 6031 return FAIL; 6032 } 6033 *dest = dest_reg; 6034 *type = name[1] == '#' ? &t_number_or_string : &t_string; 6035 } 6036 else if (STRNCMP(name, "g:", 2) == 0) 6037 { 6038 *dest = dest_global; 6039 } 6040 else if (STRNCMP(name, "b:", 2) == 0) 6041 { 6042 *dest = dest_buffer; 6043 } 6044 else if (STRNCMP(name, "w:", 2) == 0) 6045 { 6046 *dest = dest_window; 6047 } 6048 else if (STRNCMP(name, "t:", 2) == 0) 6049 { 6050 *dest = dest_tab; 6051 } 6052 else if (STRNCMP(name, "v:", 2) == 0) 6053 { 6054 typval_T *vtv; 6055 int di_flags; 6056 6057 *vimvaridx = find_vim_var(name + 2, &di_flags); 6058 if (*vimvaridx < 0) 6059 { 6060 semsg(_(e_variable_not_found_str), name); 6061 return FAIL; 6062 } 6063 // We use the current value of "sandbox" here, is that OK? 6064 if (var_check_ro(di_flags, name, FALSE)) 6065 return FAIL; 6066 *dest = dest_vimvar; 6067 vtv = get_vim_var_tv(*vimvaridx); 6068 *type = typval2type_vimvar(vtv, cctx->ctx_type_list); 6069 } 6070 return OK; 6071 } 6072 6073 /* 6074 * Generate a STORE instruction for "dest", not being "dest_local". 6075 * Return FAIL when out of memory. 6076 */ 6077 static int 6078 generate_store_var( 6079 cctx_T *cctx, 6080 assign_dest_T dest, 6081 int opt_flags, 6082 int vimvaridx, 6083 int scriptvar_idx, 6084 int scriptvar_sid, 6085 type_T *type, 6086 char_u *name) 6087 { 6088 switch (dest) 6089 { 6090 case dest_option: 6091 return generate_STOREOPT(cctx, skip_option_env_lead(name), 6092 opt_flags); 6093 case dest_global: 6094 // include g: with the name, easier to execute that way 6095 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL 6096 ? ISN_STOREG : ISN_STOREAUTO, 0, name); 6097 case dest_buffer: 6098 // include b: with the name, easier to execute that way 6099 return generate_STORE(cctx, ISN_STOREB, 0, name); 6100 case dest_window: 6101 // include w: with the name, easier to execute that way 6102 return generate_STORE(cctx, ISN_STOREW, 0, name); 6103 case dest_tab: 6104 // include t: with the name, easier to execute that way 6105 return generate_STORE(cctx, ISN_STORET, 0, name); 6106 case dest_env: 6107 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1); 6108 case dest_reg: 6109 return generate_STORE(cctx, ISN_STOREREG, 6110 name[1] == '@' ? '"' : name[1], NULL); 6111 case dest_vimvar: 6112 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); 6113 case dest_script: 6114 if (scriptvar_idx < 0) 6115 // "s:" may be included in the name. 6116 return generate_OLDSCRIPT(cctx, ISN_STORES, name, 6117 scriptvar_sid, type); 6118 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, 6119 scriptvar_sid, scriptvar_idx, type); 6120 case dest_local: 6121 case dest_expr: 6122 // cannot happen 6123 break; 6124 } 6125 return FAIL; 6126 } 6127 6128 static int 6129 generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count) 6130 { 6131 if (lhs->lhs_dest != dest_local) 6132 return generate_store_var(cctx, lhs->lhs_dest, 6133 lhs->lhs_opt_flags, lhs->lhs_vimvaridx, 6134 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid, 6135 lhs->lhs_type, lhs->lhs_name); 6136 6137 if (lhs->lhs_lvar != NULL) 6138 { 6139 garray_T *instr = &cctx->ctx_instr; 6140 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; 6141 6142 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into 6143 // ISN_STORENR 6144 if (lhs->lhs_lvar->lv_from_outer == 0 6145 && instr->ga_len == instr_count + 1 6146 && isn->isn_type == ISN_PUSHNR) 6147 { 6148 varnumber_T val = isn->isn_arg.number; 6149 garray_T *stack = &cctx->ctx_type_stack; 6150 6151 isn->isn_type = ISN_STORENR; 6152 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx; 6153 isn->isn_arg.storenr.stnr_val = val; 6154 if (stack->ga_len > 0) 6155 --stack->ga_len; 6156 } 6157 else if (lhs->lhs_lvar->lv_from_outer > 0) 6158 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx, 6159 lhs->lhs_lvar->lv_from_outer); 6160 else 6161 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL); 6162 } 6163 return OK; 6164 } 6165 6166 static int 6167 is_decl_command(int cmdidx) 6168 { 6169 return cmdidx == CMD_let || cmdidx == CMD_var 6170 || cmdidx == CMD_final || cmdidx == CMD_const; 6171 } 6172 6173 /* 6174 * Figure out the LHS type and other properties for an assignment or one item 6175 * of ":unlet" with an index. 6176 * Returns OK or FAIL. 6177 */ 6178 static int 6179 compile_lhs( 6180 char_u *var_start, 6181 lhs_T *lhs, 6182 int cmdidx, 6183 int heredoc, 6184 int oplen, 6185 cctx_T *cctx) 6186 { 6187 char_u *var_end; 6188 int is_decl = is_decl_command(cmdidx); 6189 6190 CLEAR_POINTER(lhs); 6191 lhs->lhs_dest = dest_local; 6192 lhs->lhs_vimvaridx = -1; 6193 lhs->lhs_scriptvar_idx = -1; 6194 6195 // "dest_end" is the end of the destination, including "[expr]" or 6196 // ".name". 6197 // "var_end" is the end of the variable/option/etc. name. 6198 lhs->lhs_dest_end = skip_var_one(var_start, FALSE); 6199 if (*var_start == '@') 6200 var_end = var_start + 2; 6201 else 6202 { 6203 // skip over the leading "&", "&l:", "&g:" and "$" 6204 var_end = skip_option_env_lead(var_start); 6205 var_end = to_name_end(var_end, TRUE); 6206 } 6207 6208 // "a: type" is declaring variable "a" with a type, not dict "a:". 6209 if (is_decl && lhs->lhs_dest_end == var_start + 2 6210 && lhs->lhs_dest_end[-1] == ':') 6211 --lhs->lhs_dest_end; 6212 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') 6213 --var_end; 6214 6215 // compute the length of the destination without "[expr]" or ".name" 6216 lhs->lhs_varlen = var_end - var_start; 6217 lhs->lhs_varlen_total = lhs->lhs_varlen; 6218 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen); 6219 if (lhs->lhs_name == NULL) 6220 return FAIL; 6221 6222 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen) 6223 // Something follows after the variable: "var[idx]" or "var.key". 6224 lhs->lhs_has_index = TRUE; 6225 6226 if (heredoc) 6227 lhs->lhs_type = &t_list_string; 6228 else 6229 lhs->lhs_type = &t_any; 6230 6231 if (cctx->ctx_skip != SKIP_YES) 6232 { 6233 int declare_error = FALSE; 6234 6235 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx, 6236 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx, 6237 &lhs->lhs_type, cctx) == FAIL) 6238 return FAIL; 6239 if (lhs->lhs_dest != dest_local 6240 && cmdidx != CMD_const && cmdidx != CMD_final) 6241 { 6242 // Specific kind of variable recognized. 6243 declare_error = is_decl; 6244 } 6245 else 6246 { 6247 // No specific kind of variable recognized, just a name. 6248 if (check_reserved_name(lhs->lhs_name) == FAIL) 6249 return FAIL; 6250 6251 if (lookup_local(var_start, lhs->lhs_varlen, 6252 &lhs->lhs_local_lvar, cctx) == OK) 6253 lhs->lhs_lvar = &lhs->lhs_local_lvar; 6254 else 6255 { 6256 CLEAR_FIELD(lhs->lhs_arg_lvar); 6257 if (arg_exists(var_start, lhs->lhs_varlen, 6258 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type, 6259 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK) 6260 { 6261 if (is_decl) 6262 { 6263 semsg(_(e_str_is_used_as_argument), lhs->lhs_name); 6264 return FAIL; 6265 } 6266 lhs->lhs_lvar = &lhs->lhs_arg_lvar; 6267 } 6268 } 6269 if (lhs->lhs_lvar != NULL) 6270 { 6271 if (is_decl) 6272 { 6273 semsg(_(e_variable_already_declared), lhs->lhs_name); 6274 return FAIL; 6275 } 6276 } 6277 else 6278 { 6279 int script_namespace = lhs->lhs_varlen > 1 6280 && STRNCMP(var_start, "s:", 2) == 0; 6281 int script_var = (script_namespace 6282 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2, 6283 cctx) 6284 : script_var_exists(var_start, lhs->lhs_varlen, 6285 cctx)) == OK; 6286 imported_T *import = 6287 find_imported(var_start, lhs->lhs_varlen, cctx); 6288 6289 if (script_namespace || script_var || import != NULL) 6290 { 6291 char_u *rawname = lhs->lhs_name 6292 + (lhs->lhs_name[1] == ':' ? 2 : 0); 6293 6294 if (is_decl) 6295 { 6296 if (script_namespace) 6297 semsg(_(e_cannot_declare_script_variable_in_function), 6298 lhs->lhs_name); 6299 else 6300 semsg(_(e_variable_already_declared_in_script_str), 6301 lhs->lhs_name); 6302 return FAIL; 6303 } 6304 else if (cctx->ctx_ufunc->uf_script_ctx_version 6305 == SCRIPT_VERSION_VIM9 6306 && script_namespace 6307 && !script_var && import == NULL) 6308 { 6309 semsg(_(e_unknown_variable_str), lhs->lhs_name); 6310 return FAIL; 6311 } 6312 6313 lhs->lhs_dest = dest_script; 6314 6315 // existing script-local variables should have a type 6316 lhs->lhs_scriptvar_sid = current_sctx.sc_sid; 6317 if (import != NULL) 6318 lhs->lhs_scriptvar_sid = import->imp_sid; 6319 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid)) 6320 { 6321 // Check writable only when no index follows. 6322 lhs->lhs_scriptvar_idx = get_script_item_idx( 6323 lhs->lhs_scriptvar_sid, rawname, 6324 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST, 6325 cctx); 6326 if (lhs->lhs_scriptvar_idx >= 0) 6327 { 6328 scriptitem_T *si = SCRIPT_ITEM( 6329 lhs->lhs_scriptvar_sid); 6330 svar_T *sv = 6331 ((svar_T *)si->sn_var_vals.ga_data) 6332 + lhs->lhs_scriptvar_idx; 6333 lhs->lhs_type = sv->sv_type; 6334 } 6335 } 6336 } 6337 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE) 6338 == FAIL) 6339 return FAIL; 6340 } 6341 } 6342 6343 if (declare_error) 6344 { 6345 vim9_declare_error(lhs->lhs_name); 6346 return FAIL; 6347 } 6348 } 6349 6350 // handle "a:name" as a name, not index "name" on "a" 6351 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':') 6352 var_end = lhs->lhs_dest_end; 6353 6354 if (lhs->lhs_dest != dest_option) 6355 { 6356 if (is_decl && *var_end == ':') 6357 { 6358 char_u *p; 6359 6360 // parse optional type: "let var: type = expr" 6361 if (!VIM_ISWHITE(var_end[1])) 6362 { 6363 semsg(_(e_white_space_required_after_str_str), ":", var_end); 6364 return FAIL; 6365 } 6366 p = skipwhite(var_end + 1); 6367 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE); 6368 if (lhs->lhs_type == NULL) 6369 return FAIL; 6370 lhs->lhs_has_type = TRUE; 6371 } 6372 else if (lhs->lhs_lvar != NULL) 6373 lhs->lhs_type = lhs->lhs_lvar->lv_type; 6374 } 6375 6376 if (oplen == 3 && !heredoc 6377 && lhs->lhs_dest != dest_global 6378 && !lhs->lhs_has_index 6379 && lhs->lhs_type->tt_type != VAR_STRING 6380 && lhs->lhs_type->tt_type != VAR_ANY) 6381 { 6382 emsg(_(e_can_only_concatenate_to_string)); 6383 return FAIL; 6384 } 6385 6386 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local 6387 && cctx->ctx_skip != SKIP_YES) 6388 { 6389 if (oplen > 1 && !heredoc) 6390 { 6391 // +=, /=, etc. require an existing variable 6392 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name); 6393 return FAIL; 6394 } 6395 if (!is_decl) 6396 { 6397 semsg(_(e_unknown_variable_str), lhs->lhs_name); 6398 return FAIL; 6399 } 6400 6401 // Check the name is valid for a funcref. 6402 if ((lhs->lhs_type->tt_type == VAR_FUNC 6403 || lhs->lhs_type->tt_type == VAR_PARTIAL) 6404 && var_wrong_func_name(lhs->lhs_name, TRUE)) 6405 return FAIL; 6406 6407 // New local variable. 6408 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen, 6409 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type); 6410 if (lhs->lhs_lvar == NULL) 6411 return FAIL; 6412 lhs->lhs_new_local = TRUE; 6413 } 6414 6415 lhs->lhs_member_type = lhs->lhs_type; 6416 if (lhs->lhs_has_index) 6417 { 6418 // Something follows after the variable: "var[idx]" or "var.key". 6419 // TODO: should we also handle "->func()" here? 6420 if (is_decl) 6421 { 6422 emsg(_(e_cannot_use_index_when_declaring_variable)); 6423 return FAIL; 6424 } 6425 6426 if (var_start[lhs->lhs_varlen] == '[' 6427 || var_start[lhs->lhs_varlen] == '.') 6428 { 6429 char_u *after = var_start + lhs->lhs_varlen; 6430 char_u *p; 6431 6432 // Only the last index is used below, if there are others 6433 // before it generate code for the expression. Thus for 6434 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index. 6435 for (;;) 6436 { 6437 p = skip_index(after); 6438 if (*p != '[' && *p != '.') 6439 { 6440 lhs->lhs_varlen_total = p - var_start; 6441 break; 6442 } 6443 after = p; 6444 } 6445 if (after > var_start + lhs->lhs_varlen) 6446 { 6447 lhs->lhs_varlen = after - var_start; 6448 lhs->lhs_dest = dest_expr; 6449 // We don't know the type before evaluating the expression, 6450 // use "any" until then. 6451 lhs->lhs_type = &t_any; 6452 } 6453 6454 if (lhs->lhs_type->tt_member == NULL) 6455 lhs->lhs_member_type = &t_any; 6456 else 6457 lhs->lhs_member_type = lhs->lhs_type->tt_member; 6458 } 6459 else 6460 { 6461 semsg("Not supported yet: %s", var_start); 6462 return FAIL; 6463 } 6464 } 6465 return OK; 6466 } 6467 6468 /* 6469 * Figure out the LHS and check a few errors. 6470 */ 6471 static int 6472 compile_assign_lhs( 6473 char_u *var_start, 6474 lhs_T *lhs, 6475 int cmdidx, 6476 int is_decl, 6477 int heredoc, 6478 int oplen, 6479 cctx_T *cctx) 6480 { 6481 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL) 6482 return FAIL; 6483 6484 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar) 6485 { 6486 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name); 6487 return FAIL; 6488 } 6489 if (!is_decl && lhs->lhs_lvar != NULL 6490 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index) 6491 { 6492 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name); 6493 return FAIL; 6494 } 6495 return OK; 6496 } 6497 6498 /* 6499 * Return TRUE if "lhs" has a range index: "[expr : expr]". 6500 */ 6501 static int 6502 has_list_index(char_u *idx_start, cctx_T *cctx) 6503 { 6504 char_u *p = idx_start; 6505 int save_skip; 6506 6507 if (*p != '[') 6508 return FALSE; 6509 6510 p = skipwhite(p + 1); 6511 if (*p == ':') 6512 return TRUE; 6513 6514 save_skip = cctx->ctx_skip; 6515 cctx->ctx_skip = SKIP_YES; 6516 (void)compile_expr0(&p, cctx); 6517 cctx->ctx_skip = save_skip; 6518 return *skipwhite(p) == ':'; 6519 } 6520 6521 /* 6522 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in 6523 * "var.key". 6524 */ 6525 static int 6526 compile_assign_index( 6527 char_u *var_start, 6528 lhs_T *lhs, 6529 int *range, 6530 cctx_T *cctx) 6531 { 6532 size_t varlen = lhs->lhs_varlen; 6533 char_u *p; 6534 int r = OK; 6535 int need_white_before = TRUE; 6536 int empty_second; 6537 6538 p = var_start + varlen; 6539 if (*p == '[') 6540 { 6541 p = skipwhite(p + 1); 6542 if (*p == ':') 6543 { 6544 // empty first index, push zero 6545 r = generate_PUSHNR(cctx, 0); 6546 need_white_before = FALSE; 6547 } 6548 else 6549 r = compile_expr0(&p, cctx); 6550 6551 if (r == OK && *skipwhite(p) == ':') 6552 { 6553 // unlet var[idx : idx] 6554 // blob[idx : idx] = value 6555 *range = TRUE; 6556 p = skipwhite(p); 6557 empty_second = *skipwhite(p + 1) == ']'; 6558 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1])) 6559 || (!empty_second && !IS_WHITE_OR_NUL(p[1]))) 6560 { 6561 semsg(_(e_white_space_required_before_and_after_str_at_str), 6562 ":", p); 6563 return FAIL; 6564 } 6565 p = skipwhite(p + 1); 6566 if (*p == ']') 6567 // empty second index, push "none" 6568 r = generate_PUSHSPEC(cctx, VVAL_NONE); 6569 else 6570 r = compile_expr0(&p, cctx); 6571 } 6572 6573 if (r == OK && *skipwhite(p) != ']') 6574 { 6575 // this should not happen 6576 emsg(_(e_missbrac)); 6577 r = FAIL; 6578 } 6579 } 6580 else // if (*p == '.') 6581 { 6582 char_u *key_end = to_name_end(p + 1, TRUE); 6583 char_u *key = vim_strnsave(p + 1, key_end - p - 1); 6584 6585 r = generate_PUSHS(cctx, &key); 6586 } 6587 return r; 6588 } 6589 6590 /* 6591 * For a LHS with an index, load the variable to be indexed. 6592 */ 6593 static int 6594 compile_load_lhs( 6595 lhs_T *lhs, 6596 char_u *var_start, 6597 type_T *rhs_type, 6598 cctx_T *cctx) 6599 { 6600 if (lhs->lhs_dest == dest_expr) 6601 { 6602 size_t varlen = lhs->lhs_varlen; 6603 int c = var_start[varlen]; 6604 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len; 6605 char_u *p = var_start; 6606 garray_T *stack = &cctx->ctx_type_stack; 6607 int res; 6608 6609 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and 6610 // limit the lines array length to avoid skipping to a following line. 6611 var_start[varlen] = NUL; 6612 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1; 6613 res = compile_expr0(&p, cctx); 6614 var_start[varlen] = c; 6615 cctx->ctx_ufunc->uf_lines.ga_len = lines_len; 6616 if (res == FAIL || p != var_start + varlen) 6617 { 6618 // this should not happen 6619 if (res != FAIL) 6620 emsg(_(e_missbrac)); 6621 return FAIL; 6622 } 6623 6624 lhs->lhs_type = stack->ga_len == 0 ? &t_void 6625 : ((type_T **)stack->ga_data)[stack->ga_len - 1]; 6626 // now we can properly check the type 6627 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL 6628 && rhs_type != &t_void 6629 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx, 6630 FALSE, FALSE) == FAIL) 6631 return FAIL; 6632 } 6633 else 6634 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name, 6635 lhs->lhs_lvar, lhs->lhs_type); 6636 return OK; 6637 } 6638 6639 /* 6640 * Produce code for loading "lhs" and also take care of an index. 6641 * Return OK/FAIL. 6642 */ 6643 static int 6644 compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx) 6645 { 6646 compile_load_lhs(lhs, var_start, NULL, cctx); 6647 6648 if (lhs->lhs_has_index) 6649 { 6650 int range = FALSE; 6651 6652 // Get member from list or dict. First compile the 6653 // index value. 6654 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL) 6655 return FAIL; 6656 if (range) 6657 { 6658 semsg(_(e_cannot_use_range_with_assignment_operator_str), 6659 var_start); 6660 return FAIL; 6661 } 6662 6663 // Get the member. 6664 if (compile_member(FALSE, cctx) == FAIL) 6665 return FAIL; 6666 } 6667 return OK; 6668 } 6669 6670 /* 6671 * Assignment to a list or dict member, or ":unlet" for the item, using the 6672 * information in "lhs". 6673 * Returns OK or FAIL. 6674 */ 6675 static int 6676 compile_assign_unlet( 6677 char_u *var_start, 6678 lhs_T *lhs, 6679 int is_assign, 6680 type_T *rhs_type, 6681 cctx_T *cctx) 6682 { 6683 vartype_T dest_type; 6684 garray_T *stack = &cctx->ctx_type_stack; 6685 int range = FALSE; 6686 6687 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL) 6688 return FAIL; 6689 if (is_assign && range 6690 && lhs->lhs_type->tt_type != VAR_LIST 6691 && lhs->lhs_type != &t_blob 6692 && lhs->lhs_type != &t_any) 6693 { 6694 semsg(_(e_cannot_use_range_with_assignment_str), var_start); 6695 return FAIL; 6696 } 6697 6698 if (lhs->lhs_type == &t_any) 6699 { 6700 // Index on variable of unknown type: check at runtime. 6701 dest_type = VAR_ANY; 6702 } 6703 else 6704 { 6705 dest_type = lhs->lhs_type->tt_type; 6706 if (dest_type == VAR_DICT && range) 6707 { 6708 emsg(e_cannot_use_range_with_dictionary); 6709 return FAIL; 6710 } 6711 if (dest_type == VAR_DICT 6712 && may_generate_2STRING(-1, FALSE, cctx) == FAIL) 6713 return FAIL; 6714 if (dest_type == VAR_LIST || dest_type == VAR_BLOB) 6715 { 6716 type_T *type; 6717 6718 if (range) 6719 { 6720 type = ((type_T **)stack->ga_data)[stack->ga_len - 2]; 6721 if (need_type(type, &t_number, 6722 -1, 0, cctx, FALSE, FALSE) == FAIL) 6723 return FAIL; 6724 } 6725 type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 6726 if ((dest_type != VAR_BLOB && type != &t_special) 6727 && need_type(type, &t_number, 6728 -1, 0, cctx, FALSE, FALSE) == FAIL) 6729 return FAIL; 6730 } 6731 } 6732 6733 // Load the dict or list. On the stack we then have: 6734 // - value (for assignment, not for :unlet) 6735 // - index 6736 // - for [a : b] second index 6737 // - variable 6738 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL) 6739 return FAIL; 6740 6741 if (dest_type == VAR_LIST || dest_type == VAR_DICT 6742 || dest_type == VAR_BLOB || dest_type == VAR_ANY) 6743 { 6744 if (is_assign) 6745 { 6746 if (range) 6747 { 6748 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL) 6749 return FAIL; 6750 } 6751 else 6752 { 6753 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3); 6754 6755 if (isn == NULL) 6756 return FAIL; 6757 isn->isn_arg.vartype = dest_type; 6758 } 6759 } 6760 else if (range) 6761 { 6762 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL) 6763 return FAIL; 6764 } 6765 else 6766 { 6767 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL) 6768 return FAIL; 6769 } 6770 } 6771 else 6772 { 6773 emsg(_(e_indexable_type_required)); 6774 return FAIL; 6775 } 6776 6777 return OK; 6778 } 6779 6780 /* 6781 * Compile declaration and assignment: 6782 * "let name" 6783 * "var name = expr" 6784 * "final name = expr" 6785 * "const name = expr" 6786 * "name = expr" 6787 * "arg" points to "name". 6788 * "++arg" and "--arg" 6789 * Return NULL for an error. 6790 * Return "arg" if it does not look like a variable list. 6791 */ 6792 static char_u * 6793 compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) 6794 { 6795 char_u *var_start; 6796 char_u *p; 6797 char_u *end = arg; 6798 char_u *ret = NULL; 6799 int var_count = 0; 6800 int var_idx; 6801 int semicolon = 0; 6802 int did_generate_slice = FALSE; 6803 garray_T *instr = &cctx->ctx_instr; 6804 garray_T *stack = &cctx->ctx_type_stack; 6805 char_u *op; 6806 int oplen = 0; 6807 int heredoc = FALSE; 6808 int incdec = FALSE; 6809 type_T *rhs_type = &t_any; 6810 char_u *sp; 6811 int is_decl = is_decl_command(cmdidx); 6812 lhs_T lhs; 6813 long start_lnum = SOURCING_LNUM; 6814 6815 // Skip over the "var" or "[var, var]" to get to any "=". 6816 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); 6817 if (p == NULL) 6818 return *arg == '[' ? arg : NULL; 6819 6820 if (var_count > 0 && is_decl) 6821 { 6822 // TODO: should we allow this, and figure out type inference from list 6823 // members? 6824 emsg(_(e_cannot_use_list_for_declaration)); 6825 return NULL; 6826 } 6827 lhs.lhs_name = NULL; 6828 6829 sp = p; 6830 p = skipwhite(p); 6831 op = p; 6832 oplen = assignment_len(p, &heredoc); 6833 6834 if (var_count > 0 && oplen == 0) 6835 // can be something like "[1, 2]->func()" 6836 return arg; 6837 6838 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen]))) 6839 { 6840 error_white_both(op, oplen); 6841 return NULL; 6842 } 6843 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement) 6844 { 6845 if (VIM_ISWHITE(eap->cmd[2])) 6846 { 6847 semsg(_(e_no_white_space_allowed_after_str_str), 6848 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd); 6849 return NULL; 6850 } 6851 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-="); 6852 oplen = 2; 6853 incdec = TRUE; 6854 } 6855 6856 if (heredoc) 6857 { 6858 list_T *l; 6859 listitem_T *li; 6860 6861 // [let] varname =<< [trim] {end} 6862 eap->getline = exarg_getline; 6863 eap->cookie = cctx; 6864 l = heredoc_get(eap, op + 3, FALSE); 6865 if (l == NULL) 6866 return NULL; 6867 6868 if (cctx->ctx_skip != SKIP_YES) 6869 { 6870 // Push each line and the create the list. 6871 FOR_ALL_LIST_ITEMS(l, li) 6872 { 6873 generate_PUSHS(cctx, &li->li_tv.vval.v_string); 6874 li->li_tv.vval.v_string = NULL; 6875 } 6876 generate_NEWLIST(cctx, l->lv_len); 6877 } 6878 list_free(l); 6879 p += STRLEN(p); 6880 end = p; 6881 } 6882 else if (var_count > 0) 6883 { 6884 char_u *wp; 6885 6886 // for "[var, var] = expr" evaluate the expression here, loop over the 6887 // list of variables below. 6888 // A line break may follow the "=". 6889 6890 wp = op + oplen; 6891 if (may_get_next_line_error(wp, &p, cctx) == FAIL) 6892 return FAIL; 6893 if (compile_expr0(&p, cctx) == FAIL) 6894 return NULL; 6895 end = p; 6896 6897 if (cctx->ctx_skip != SKIP_YES) 6898 { 6899 type_T *stacktype; 6900 6901 stacktype = stack->ga_len == 0 ? &t_void 6902 : ((type_T **)stack->ga_data)[stack->ga_len - 1]; 6903 if (stacktype->tt_type == VAR_VOID) 6904 { 6905 emsg(_(e_cannot_use_void_value)); 6906 goto theend; 6907 } 6908 if (need_type(stacktype, &t_list_any, -1, 0, cctx, 6909 FALSE, FALSE) == FAIL) 6910 goto theend; 6911 // TODO: check the length of a constant list here 6912 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, 6913 semicolon); 6914 if (stacktype->tt_member != NULL) 6915 rhs_type = stacktype->tt_member; 6916 } 6917 } 6918 6919 /* 6920 * Loop over variables in "[var, var] = expr". 6921 * For "var = expr" and "let var: type" this is done only once. 6922 */ 6923 if (var_count > 0) 6924 var_start = skipwhite(arg + 1); // skip over the "[" 6925 else 6926 var_start = arg; 6927 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) 6928 { 6929 int instr_count = -1; 6930 int save_lnum; 6931 6932 if (var_start[0] == '_' && !eval_isnamec(var_start[1])) 6933 { 6934 // Ignore underscore in "[a, _, b] = list". 6935 if (var_count > 0) 6936 { 6937 var_start = skipwhite(var_start + 2); 6938 continue; 6939 } 6940 emsg(_(e_cannot_use_underscore_here)); 6941 goto theend; 6942 } 6943 vim_free(lhs.lhs_name); 6944 6945 /* 6946 * Figure out the LHS type and other properties. 6947 */ 6948 if (compile_assign_lhs(var_start, &lhs, cmdidx, 6949 is_decl, heredoc, oplen, cctx) == FAIL) 6950 goto theend; 6951 if (heredoc) 6952 { 6953 SOURCING_LNUM = start_lnum; 6954 if (lhs.lhs_has_type 6955 && need_type(&t_list_string, lhs.lhs_type, 6956 -1, 0, cctx, FALSE, FALSE) == FAIL) 6957 goto theend; 6958 } 6959 else 6960 { 6961 if (cctx->ctx_skip == SKIP_YES) 6962 { 6963 if (oplen > 0 && var_count == 0) 6964 { 6965 // skip over the "=" and the expression 6966 p = skipwhite(op + oplen); 6967 (void)compile_expr0(&p, cctx); 6968 } 6969 } 6970 else if (oplen > 0) 6971 { 6972 int is_const = FALSE; 6973 char_u *wp; 6974 6975 // for "+=", "*=", "..=" etc. first load the current value 6976 if (*op != '=' 6977 && compile_load_lhs_with_index(&lhs, var_start, 6978 cctx) == FAIL) 6979 goto theend; 6980 6981 // For "var = expr" evaluate the expression. 6982 if (var_count == 0) 6983 { 6984 int r; 6985 6986 // Compile the expression. 6987 instr_count = instr->ga_len; 6988 if (incdec) 6989 { 6990 r = generate_PUSHNR(cctx, 1); 6991 } 6992 else 6993 { 6994 // Temporarily hide the new local variable here, it is 6995 // not available to this expression. 6996 if (lhs.lhs_new_local) 6997 --cctx->ctx_locals.ga_len; 6998 wp = op + oplen; 6999 if (may_get_next_line_error(wp, &p, cctx) == FAIL) 7000 { 7001 if (lhs.lhs_new_local) 7002 ++cctx->ctx_locals.ga_len; 7003 goto theend; 7004 } 7005 r = compile_expr0_ext(&p, cctx, &is_const); 7006 if (lhs.lhs_new_local) 7007 ++cctx->ctx_locals.ga_len; 7008 if (r == FAIL) 7009 goto theend; 7010 } 7011 } 7012 else if (semicolon && var_idx == var_count - 1) 7013 { 7014 // For "[var; var] = expr" get the rest of the list 7015 did_generate_slice = TRUE; 7016 if (generate_SLICE(cctx, var_count - 1) == FAIL) 7017 goto theend; 7018 } 7019 else 7020 { 7021 // For "[var, var] = expr" get the "var_idx" item from the 7022 // list. 7023 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL) 7024 goto theend; 7025 } 7026 7027 rhs_type = stack->ga_len == 0 ? &t_void 7028 : ((type_T **)stack->ga_data)[stack->ga_len - 1]; 7029 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type)) 7030 { 7031 if ((rhs_type->tt_type == VAR_FUNC 7032 || rhs_type->tt_type == VAR_PARTIAL) 7033 && !lhs.lhs_has_index 7034 && var_wrong_func_name(lhs.lhs_name, TRUE)) 7035 goto theend; 7036 7037 if (lhs.lhs_new_local && !lhs.lhs_has_type) 7038 { 7039 if (rhs_type->tt_type == VAR_VOID) 7040 { 7041 emsg(_(e_cannot_use_void_value)); 7042 goto theend; 7043 } 7044 else 7045 { 7046 // An empty list or dict has a &t_unknown member, 7047 // for a variable that implies &t_any. 7048 if (rhs_type == &t_list_empty) 7049 lhs.lhs_lvar->lv_type = &t_list_any; 7050 else if (rhs_type == &t_dict_empty) 7051 lhs.lhs_lvar->lv_type = &t_dict_any; 7052 else if (rhs_type == &t_unknown) 7053 lhs.lhs_lvar->lv_type = &t_any; 7054 else 7055 lhs.lhs_lvar->lv_type = rhs_type; 7056 } 7057 } 7058 else if (*op == '=') 7059 { 7060 type_T *use_type = lhs.lhs_lvar->lv_type; 7061 where_T where = WHERE_INIT; 7062 7063 // Without operator check type here, otherwise below. 7064 // Use the line number of the assignment. 7065 SOURCING_LNUM = start_lnum; 7066 where.wt_index = var_count > 0 ? var_idx + 1 : 0; 7067 where.wt_variable = var_count > 0; 7068 // If assigning to a list or dict member, use the 7069 // member type. Not for "list[:] =". 7070 if (lhs.lhs_has_index 7071 && !has_list_index(var_start + lhs.lhs_varlen, 7072 cctx)) 7073 use_type = lhs.lhs_member_type; 7074 if (need_type_where(rhs_type, use_type, -1, where, 7075 cctx, FALSE, is_const) == FAIL) 7076 goto theend; 7077 } 7078 } 7079 else 7080 { 7081 type_T *lhs_type = lhs.lhs_member_type; 7082 7083 // Special case: assigning to @# can use a number or a 7084 // string. 7085 // Also: can assign a number to a float. 7086 if ((lhs_type == &t_number_or_string 7087 || lhs_type == &t_float) 7088 && rhs_type->tt_type == VAR_NUMBER) 7089 lhs_type = &t_number; 7090 if (*p != '=' && need_type(rhs_type, lhs_type, 7091 -1, 0, cctx, FALSE, FALSE) == FAIL) 7092 goto theend; 7093 } 7094 } 7095 else if (cmdidx == CMD_final) 7096 { 7097 emsg(_(e_final_requires_a_value)); 7098 goto theend; 7099 } 7100 else if (cmdidx == CMD_const) 7101 { 7102 emsg(_(e_const_requires_a_value)); 7103 goto theend; 7104 } 7105 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option) 7106 { 7107 emsg(_(e_type_or_initialization_required)); 7108 goto theend; 7109 } 7110 else 7111 { 7112 // variables are always initialized 7113 if (GA_GROW_FAILS(instr, 1)) 7114 goto theend; 7115 switch (lhs.lhs_member_type->tt_type) 7116 { 7117 case VAR_BOOL: 7118 generate_PUSHBOOL(cctx, VVAL_FALSE); 7119 break; 7120 case VAR_FLOAT: 7121 #ifdef FEAT_FLOAT 7122 generate_PUSHF(cctx, 0.0); 7123 #endif 7124 break; 7125 case VAR_STRING: 7126 generate_PUSHS(cctx, NULL); 7127 break; 7128 case VAR_BLOB: 7129 generate_PUSHBLOB(cctx, blob_alloc()); 7130 break; 7131 case VAR_FUNC: 7132 generate_PUSHFUNC(cctx, NULL, &t_func_void); 7133 break; 7134 case VAR_LIST: 7135 generate_NEWLIST(cctx, 0); 7136 break; 7137 case VAR_DICT: 7138 generate_NEWDICT(cctx, 0); 7139 break; 7140 case VAR_JOB: 7141 generate_PUSHJOB(cctx, NULL); 7142 break; 7143 case VAR_CHANNEL: 7144 generate_PUSHCHANNEL(cctx, NULL); 7145 break; 7146 case VAR_NUMBER: 7147 case VAR_UNKNOWN: 7148 case VAR_ANY: 7149 case VAR_PARTIAL: 7150 case VAR_VOID: 7151 case VAR_INSTR: 7152 case VAR_SPECIAL: // cannot happen 7153 generate_PUSHNR(cctx, 0); 7154 break; 7155 } 7156 } 7157 if (var_count == 0) 7158 end = p; 7159 } 7160 7161 // no need to parse more when skipping 7162 if (cctx->ctx_skip == SKIP_YES) 7163 break; 7164 7165 if (oplen > 0 && *op != '=') 7166 { 7167 type_T *expected; 7168 type_T *stacktype = NULL; 7169 7170 if (*op == '.') 7171 { 7172 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL) 7173 goto theend; 7174 } 7175 else 7176 { 7177 expected = lhs.lhs_member_type; 7178 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 7179 if ( 7180 #ifdef FEAT_FLOAT 7181 // If variable is float operation with number is OK. 7182 !(expected == &t_float && (stacktype == &t_number 7183 || stacktype == &t_number_bool)) && 7184 #endif 7185 need_type(stacktype, expected, -1, 0, cctx, 7186 FALSE, FALSE) == FAIL) 7187 goto theend; 7188 } 7189 7190 if (*op == '.') 7191 { 7192 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL) 7193 goto theend; 7194 } 7195 else if (*op == '+') 7196 { 7197 if (generate_add_instr(cctx, 7198 operator_type(lhs.lhs_member_type, stacktype), 7199 lhs.lhs_member_type, stacktype, 7200 EXPR_APPEND) == FAIL) 7201 goto theend; 7202 } 7203 else if (generate_two_op(cctx, op) == FAIL) 7204 goto theend; 7205 } 7206 7207 // Use the line number of the assignment for store instruction. 7208 save_lnum = cctx->ctx_lnum; 7209 cctx->ctx_lnum = start_lnum - 1; 7210 7211 if (lhs.lhs_has_index) 7212 { 7213 // Use the info in "lhs" to store the value at the index in the 7214 // list or dict. 7215 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx) 7216 == FAIL) 7217 { 7218 cctx->ctx_lnum = save_lnum; 7219 goto theend; 7220 } 7221 } 7222 else 7223 { 7224 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script 7225 || lhs.lhs_dest == dest_global 7226 || lhs.lhs_dest == dest_local)) 7227 // ":const var": lock the value, but not referenced variables 7228 generate_LOCKCONST(cctx); 7229 7230 if (is_decl 7231 && (lhs.lhs_type->tt_type == VAR_DICT 7232 || lhs.lhs_type->tt_type == VAR_LIST) 7233 && lhs.lhs_type->tt_member != NULL 7234 && !(lhs.lhs_type->tt_member == &t_any 7235 && oplen > 0 7236 && rhs_type != NULL 7237 && rhs_type->tt_type == lhs.lhs_type->tt_type 7238 && rhs_type->tt_member != &t_unknown) 7239 && lhs.lhs_type->tt_member != &t_unknown) 7240 // Set the type in the list or dict, so that it can be checked, 7241 // also in legacy script. Not for "list<any> = val", then the 7242 // type of "val" is used. 7243 generate_SETTYPE(cctx, lhs.lhs_type); 7244 7245 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL) 7246 { 7247 cctx->ctx_lnum = save_lnum; 7248 goto theend; 7249 } 7250 } 7251 cctx->ctx_lnum = save_lnum; 7252 7253 if (var_idx + 1 < var_count) 7254 var_start = skipwhite(lhs.lhs_dest_end + 1); 7255 } 7256 7257 // For "[var, var] = expr" drop the "expr" value. 7258 // Also for "[var, var; _] = expr". 7259 if (var_count > 0 && (!semicolon || !did_generate_slice)) 7260 { 7261 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) 7262 goto theend; 7263 } 7264 7265 ret = skipwhite(end); 7266 7267 theend: 7268 vim_free(lhs.lhs_name); 7269 return ret; 7270 } 7271 7272 /* 7273 * Check for an assignment at "eap->cmd", compile it if found. 7274 * Return NOTDONE if there is none, FAIL for failure, OK if done. 7275 */ 7276 static int 7277 may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx) 7278 { 7279 char_u *pskip; 7280 char_u *p; 7281 7282 // Assuming the command starts with a variable or function name, 7283 // find what follows. 7284 // Skip over "var.member", "var[idx]" and the like. 7285 // Also "&opt = val", "$ENV = val" and "@r = val". 7286 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@') 7287 ? eap->cmd + 1 : eap->cmd; 7288 p = to_name_end(pskip, TRUE); 7289 if (p > eap->cmd && *p != NUL) 7290 { 7291 char_u *var_end; 7292 int oplen; 7293 int heredoc; 7294 7295 if (eap->cmd[0] == '@') 7296 var_end = eap->cmd + 2; 7297 else 7298 var_end = find_name_end(pskip, NULL, NULL, 7299 FNE_CHECK_START | FNE_INCL_BR); 7300 oplen = assignment_len(skipwhite(var_end), &heredoc); 7301 if (oplen > 0) 7302 { 7303 size_t len = p - eap->cmd; 7304 7305 // Recognize an assignment if we recognize the variable 7306 // name: 7307 // "g:var = expr" 7308 // "local = expr" where "local" is a local var. 7309 // "script = expr" where "script" is a script-local var. 7310 // "import = expr" where "import" is an imported var 7311 // "&opt = expr" 7312 // "$ENV = expr" 7313 // "@r = expr" 7314 if (*eap->cmd == '&' 7315 || *eap->cmd == '$' 7316 || *eap->cmd == '@' 7317 || ((len) > 2 && eap->cmd[1] == ':') 7318 || variable_exists(eap->cmd, len, cctx)) 7319 { 7320 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx); 7321 if (*line == NULL || *line == eap->cmd) 7322 return FAIL; 7323 return OK; 7324 } 7325 } 7326 } 7327 7328 if (*eap->cmd == '[') 7329 { 7330 // [var, var] = expr 7331 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx); 7332 if (*line == NULL) 7333 return FAIL; 7334 if (*line != eap->cmd) 7335 return OK; 7336 } 7337 return NOTDONE; 7338 } 7339 7340 /* 7341 * Check if "name" can be "unlet". 7342 */ 7343 int 7344 check_vim9_unlet(char_u *name) 7345 { 7346 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) 7347 { 7348 // "unlet s:var" is allowed in legacy script. 7349 if (*name == 's' && !script_is_vim9()) 7350 return OK; 7351 semsg(_(e_cannot_unlet_str), name); 7352 return FAIL; 7353 } 7354 return OK; 7355 } 7356 7357 /* 7358 * Callback passed to ex_unletlock(). 7359 */ 7360 static int 7361 compile_unlet( 7362 lval_T *lvp, 7363 char_u *name_end, 7364 exarg_T *eap, 7365 int deep UNUSED, 7366 void *coookie) 7367 { 7368 cctx_T *cctx = coookie; 7369 char_u *p = lvp->ll_name; 7370 int cc = *name_end; 7371 int ret = OK; 7372 7373 if (cctx->ctx_skip == SKIP_YES) 7374 return OK; 7375 7376 *name_end = NUL; 7377 if (*p == '$') 7378 { 7379 // :unlet $ENV_VAR 7380 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); 7381 } 7382 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL) 7383 { 7384 lhs_T lhs; 7385 7386 // This is similar to assigning: lookup the list/dict, compile the 7387 // idx/key. Then instead of storing the value unlet the item. 7388 // unlet {list}[idx] 7389 // unlet {dict}[key] dict.key 7390 // 7391 // Figure out the LHS type and other properties. 7392 // 7393 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx); 7394 7395 // : unlet an indexed item 7396 if (!lhs.lhs_has_index) 7397 { 7398 iemsg("called compile_lhs() without an index"); 7399 ret = FAIL; 7400 } 7401 else 7402 { 7403 // Use the info in "lhs" to unlet the item at the index in the 7404 // list or dict. 7405 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx); 7406 } 7407 7408 vim_free(lhs.lhs_name); 7409 } 7410 else if (check_vim9_unlet(p) == FAIL) 7411 { 7412 ret = FAIL; 7413 } 7414 else 7415 { 7416 // Normal name. Only supports g:, w:, t: and b: namespaces. 7417 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); 7418 } 7419 7420 *name_end = cc; 7421 return ret; 7422 } 7423 7424 /* 7425 * Callback passed to ex_unletlock(). 7426 */ 7427 static int 7428 compile_lock_unlock( 7429 lval_T *lvp, 7430 char_u *name_end, 7431 exarg_T *eap, 7432 int deep UNUSED, 7433 void *coookie) 7434 { 7435 cctx_T *cctx = coookie; 7436 int cc = *name_end; 7437 char_u *p = lvp->ll_name; 7438 int ret = OK; 7439 size_t len; 7440 char_u *buf; 7441 isntype_T isn = ISN_EXEC; 7442 7443 if (cctx->ctx_skip == SKIP_YES) 7444 return OK; 7445 7446 // Cannot use :lockvar and :unlockvar on local variables. 7447 if (p[1] != ':') 7448 { 7449 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START); 7450 7451 if (lookup_local(p, end - p, NULL, cctx) == OK) 7452 { 7453 char_u *s = p; 7454 7455 if (*end != '.' && *end != '[') 7456 { 7457 emsg(_(e_cannot_lock_unlock_local_variable)); 7458 return FAIL; 7459 } 7460 7461 // For "d.member" put the local variable on the stack, it will be 7462 // passed to ex_lockvar() indirectly. 7463 if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL) 7464 return FAIL; 7465 isn = ISN_LOCKUNLOCK; 7466 } 7467 } 7468 7469 // Checking is done at runtime. 7470 *name_end = NUL; 7471 len = name_end - p + 20; 7472 buf = alloc(len); 7473 if (buf == NULL) 7474 ret = FAIL; 7475 else 7476 { 7477 vim_snprintf((char *)buf, len, "%s %s", 7478 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar", 7479 p); 7480 ret = generate_EXEC(cctx, isn, buf); 7481 7482 vim_free(buf); 7483 *name_end = cc; 7484 } 7485 return ret; 7486 } 7487 7488 /* 7489 * compile "unlet var", "lock var" and "unlock var" 7490 * "arg" points to "var". 7491 */ 7492 static char_u * 7493 compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) 7494 { 7495 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING, 7496 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock, 7497 cctx); 7498 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; 7499 } 7500 7501 /* 7502 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". 7503 */ 7504 static int 7505 compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) 7506 { 7507 garray_T *instr = &cctx->ctx_instr; 7508 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); 7509 7510 if (endlabel == NULL) 7511 return FAIL; 7512 endlabel->el_next = *el; 7513 *el = endlabel; 7514 endlabel->el_end_label = instr->ga_len; 7515 7516 generate_JUMP(cctx, when, 0); 7517 return OK; 7518 } 7519 7520 static void 7521 compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx) 7522 { 7523 garray_T *instr = &cctx->ctx_instr; 7524 7525 while (*el != NULL) 7526 { 7527 endlabel_T *cur = (*el); 7528 isn_T *isn; 7529 7530 isn = ((isn_T *)instr->ga_data) + cur->el_end_label; 7531 isn->isn_arg.jump.jump_where = jump_where; 7532 *el = cur->el_next; 7533 vim_free(cur); 7534 } 7535 } 7536 7537 static void 7538 compile_free_jump_to_end(endlabel_T **el) 7539 { 7540 while (*el != NULL) 7541 { 7542 endlabel_T *cur = (*el); 7543 7544 *el = cur->el_next; 7545 vim_free(cur); 7546 } 7547 } 7548 7549 /* 7550 * Create a new scope and set up the generic items. 7551 */ 7552 static scope_T * 7553 new_scope(cctx_T *cctx, scopetype_T type) 7554 { 7555 scope_T *scope = ALLOC_CLEAR_ONE(scope_T); 7556 7557 if (scope == NULL) 7558 return NULL; 7559 scope->se_outer = cctx->ctx_scope; 7560 cctx->ctx_scope = scope; 7561 scope->se_type = type; 7562 scope->se_local_count = cctx->ctx_locals.ga_len; 7563 return scope; 7564 } 7565 7566 /* 7567 * Free the current scope and go back to the outer scope. 7568 */ 7569 static void 7570 drop_scope(cctx_T *cctx) 7571 { 7572 scope_T *scope = cctx->ctx_scope; 7573 7574 if (scope == NULL) 7575 { 7576 iemsg("calling drop_scope() without a scope"); 7577 return; 7578 } 7579 cctx->ctx_scope = scope->se_outer; 7580 switch (scope->se_type) 7581 { 7582 case IF_SCOPE: 7583 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; 7584 case FOR_SCOPE: 7585 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; 7586 case WHILE_SCOPE: 7587 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; 7588 case TRY_SCOPE: 7589 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; 7590 case NO_SCOPE: 7591 case BLOCK_SCOPE: 7592 break; 7593 } 7594 vim_free(scope); 7595 } 7596 7597 /* 7598 * compile "if expr" 7599 * 7600 * "if expr" Produces instructions: 7601 * EVAL expr Push result of "expr" 7602 * JUMP_IF_FALSE end 7603 * ... body ... 7604 * end: 7605 * 7606 * "if expr | else" Produces instructions: 7607 * EVAL expr Push result of "expr" 7608 * JUMP_IF_FALSE else 7609 * ... body ... 7610 * JUMP_ALWAYS end 7611 * else: 7612 * ... body ... 7613 * end: 7614 * 7615 * "if expr1 | elseif expr2 | else" Produces instructions: 7616 * EVAL expr Push result of "expr" 7617 * JUMP_IF_FALSE elseif 7618 * ... body ... 7619 * JUMP_ALWAYS end 7620 * elseif: 7621 * EVAL expr Push result of "expr" 7622 * JUMP_IF_FALSE else 7623 * ... body ... 7624 * JUMP_ALWAYS end 7625 * else: 7626 * ... body ... 7627 * end: 7628 */ 7629 static char_u * 7630 compile_if(char_u *arg, cctx_T *cctx) 7631 { 7632 char_u *p = arg; 7633 garray_T *instr = &cctx->ctx_instr; 7634 int instr_count = instr->ga_len; 7635 scope_T *scope; 7636 skip_T skip_save = cctx->ctx_skip; 7637 ppconst_T ppconst; 7638 7639 CLEAR_FIELD(ppconst); 7640 if (compile_expr1(&p, cctx, &ppconst) == FAIL) 7641 { 7642 clear_ppconst(&ppconst); 7643 return NULL; 7644 } 7645 if (!ends_excmd2(arg, skipwhite(p))) 7646 { 7647 semsg(_(e_trailing_arg), p); 7648 return NULL; 7649 } 7650 if (cctx->ctx_skip == SKIP_YES) 7651 clear_ppconst(&ppconst); 7652 else if (instr->ga_len == instr_count && ppconst.pp_used == 1) 7653 { 7654 int error = FALSE; 7655 int v; 7656 7657 // The expression results in a constant. 7658 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error); 7659 clear_ppconst(&ppconst); 7660 if (error) 7661 return NULL; 7662 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES; 7663 } 7664 else 7665 { 7666 // Not a constant, generate instructions for the expression. 7667 cctx->ctx_skip = SKIP_UNKNOWN; 7668 if (generate_ppconst(cctx, &ppconst) == FAIL) 7669 return NULL; 7670 if (bool_on_stack(cctx) == FAIL) 7671 return NULL; 7672 } 7673 7674 // CMDMOD_REV must come before the jump 7675 generate_undo_cmdmods(cctx); 7676 7677 scope = new_scope(cctx, IF_SCOPE); 7678 if (scope == NULL) 7679 return NULL; 7680 scope->se_skip_save = skip_save; 7681 // "is_had_return" will be reset if any block does not end in :return 7682 scope->se_u.se_if.is_had_return = TRUE; 7683 7684 if (cctx->ctx_skip == SKIP_UNKNOWN) 7685 { 7686 // "where" is set when ":elseif", "else" or ":endif" is found 7687 scope->se_u.se_if.is_if_label = instr->ga_len; 7688 generate_JUMP(cctx, JUMP_IF_FALSE, 0); 7689 } 7690 else 7691 scope->se_u.se_if.is_if_label = -1; 7692 7693 #ifdef FEAT_PROFILE 7694 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES 7695 && skip_save != SKIP_YES) 7696 { 7697 // generated a profile start, need to generate a profile end, since it 7698 // won't be done after returning 7699 cctx->ctx_skip = SKIP_NOT; 7700 generate_instr(cctx, ISN_PROF_END); 7701 cctx->ctx_skip = SKIP_YES; 7702 } 7703 #endif 7704 7705 return p; 7706 } 7707 7708 static char_u * 7709 compile_elseif(char_u *arg, cctx_T *cctx) 7710 { 7711 char_u *p = arg; 7712 garray_T *instr = &cctx->ctx_instr; 7713 int instr_count = instr->ga_len; 7714 isn_T *isn; 7715 scope_T *scope = cctx->ctx_scope; 7716 ppconst_T ppconst; 7717 skip_T save_skip = cctx->ctx_skip; 7718 7719 if (scope == NULL || scope->se_type != IF_SCOPE) 7720 { 7721 emsg(_(e_elseif_without_if)); 7722 return NULL; 7723 } 7724 unwind_locals(cctx, scope->se_local_count); 7725 if (!cctx->ctx_had_return) 7726 scope->se_u.se_if.is_had_return = FALSE; 7727 7728 if (cctx->ctx_skip == SKIP_NOT) 7729 { 7730 // previous block was executed, this one and following will not 7731 cctx->ctx_skip = SKIP_YES; 7732 scope->se_u.se_if.is_seen_skip_not = TRUE; 7733 } 7734 if (scope->se_u.se_if.is_seen_skip_not) 7735 { 7736 // A previous block was executed, skip over expression and bail out. 7737 // Do not count the "elseif" for profiling and cmdmod 7738 instr->ga_len = current_instr_idx(cctx); 7739 7740 skip_expr_cctx(&p, cctx); 7741 return p; 7742 } 7743 7744 if (cctx->ctx_skip == SKIP_UNKNOWN) 7745 { 7746 int moved_cmdmod = FALSE; 7747 int saved_debug = FALSE; 7748 isn_T debug_isn; 7749 7750 // Move any CMDMOD instruction to after the jump 7751 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD) 7752 { 7753 if (GA_GROW_FAILS(instr, 1)) 7754 return NULL; 7755 ((isn_T *)instr->ga_data)[instr->ga_len] = 7756 ((isn_T *)instr->ga_data)[instr->ga_len - 1]; 7757 --instr->ga_len; 7758 moved_cmdmod = TRUE; 7759 } 7760 7761 // Remove the already generated ISN_DEBUG, it is written below the 7762 // ISN_FOR instruction. 7763 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0 7764 && ((isn_T *)instr->ga_data)[instr->ga_len - 1] 7765 .isn_type == ISN_DEBUG) 7766 { 7767 --instr->ga_len; 7768 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len]; 7769 saved_debug = TRUE; 7770 } 7771 7772 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label, 7773 JUMP_ALWAYS, cctx) == FAIL) 7774 return NULL; 7775 // previous "if" or "elseif" jumps here 7776 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; 7777 isn->isn_arg.jump.jump_where = instr->ga_len; 7778 7779 if (moved_cmdmod) 7780 ++instr->ga_len; 7781 7782 if (saved_debug) 7783 { 7784 // move the debug instruction here 7785 if (GA_GROW_FAILS(instr, 1)) 7786 return NULL; 7787 ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn; 7788 ++instr->ga_len; 7789 } 7790 } 7791 7792 // compile "expr"; if we know it evaluates to FALSE skip the block 7793 CLEAR_FIELD(ppconst); 7794 if (cctx->ctx_skip == SKIP_YES) 7795 { 7796 cctx->ctx_skip = SKIP_UNKNOWN; 7797 #ifdef FEAT_PROFILE 7798 if (cctx->ctx_compile_type == CT_PROFILE) 7799 { 7800 // the previous block was skipped, need to profile this line 7801 generate_instr(cctx, ISN_PROF_START); 7802 instr_count = instr->ga_len; 7803 } 7804 #endif 7805 if (cctx->ctx_compile_type == CT_DEBUG) 7806 { 7807 // the previous block was skipped, may want to debug this line 7808 generate_instr_debug(cctx); 7809 instr_count = instr->ga_len; 7810 } 7811 } 7812 if (compile_expr1(&p, cctx, &ppconst) == FAIL) 7813 { 7814 clear_ppconst(&ppconst); 7815 return NULL; 7816 } 7817 cctx->ctx_skip = save_skip; 7818 if (!ends_excmd2(arg, skipwhite(p))) 7819 { 7820 semsg(_(e_trailing_arg), p); 7821 return NULL; 7822 } 7823 if (scope->se_skip_save == SKIP_YES) 7824 clear_ppconst(&ppconst); 7825 else if (instr->ga_len == instr_count && ppconst.pp_used == 1) 7826 { 7827 int error = FALSE; 7828 int v; 7829 7830 // The expression results in a constant. 7831 // TODO: how about nesting? 7832 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error); 7833 if (error) 7834 return NULL; 7835 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES; 7836 clear_ppconst(&ppconst); 7837 scope->se_u.se_if.is_if_label = -1; 7838 } 7839 else 7840 { 7841 // Not a constant, generate instructions for the expression. 7842 cctx->ctx_skip = SKIP_UNKNOWN; 7843 if (generate_ppconst(cctx, &ppconst) == FAIL) 7844 return NULL; 7845 if (bool_on_stack(cctx) == FAIL) 7846 return NULL; 7847 7848 // CMDMOD_REV must come before the jump 7849 generate_undo_cmdmods(cctx); 7850 7851 // "where" is set when ":elseif", "else" or ":endif" is found 7852 scope->se_u.se_if.is_if_label = instr->ga_len; 7853 generate_JUMP(cctx, JUMP_IF_FALSE, 0); 7854 } 7855 7856 return p; 7857 } 7858 7859 static char_u * 7860 compile_else(char_u *arg, cctx_T *cctx) 7861 { 7862 char_u *p = arg; 7863 garray_T *instr = &cctx->ctx_instr; 7864 isn_T *isn; 7865 scope_T *scope = cctx->ctx_scope; 7866 7867 if (scope == NULL || scope->se_type != IF_SCOPE) 7868 { 7869 emsg(_(e_else_without_if)); 7870 return NULL; 7871 } 7872 unwind_locals(cctx, scope->se_local_count); 7873 if (!cctx->ctx_had_return) 7874 scope->se_u.se_if.is_had_return = FALSE; 7875 scope->se_u.se_if.is_seen_else = TRUE; 7876 7877 #ifdef FEAT_PROFILE 7878 if (cctx->ctx_compile_type == CT_PROFILE) 7879 { 7880 if (cctx->ctx_skip == SKIP_NOT 7881 && ((isn_T *)instr->ga_data)[instr->ga_len - 1] 7882 .isn_type == ISN_PROF_START) 7883 // the previous block was executed, do not count "else" for 7884 // profiling 7885 --instr->ga_len; 7886 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not) 7887 { 7888 // the previous block was not executed, this one will, do count the 7889 // "else" for profiling 7890 cctx->ctx_skip = SKIP_NOT; 7891 generate_instr(cctx, ISN_PROF_END); 7892 generate_instr(cctx, ISN_PROF_START); 7893 cctx->ctx_skip = SKIP_YES; 7894 } 7895 } 7896 #endif 7897 7898 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES) 7899 { 7900 // jump from previous block to the end, unless the else block is empty 7901 if (cctx->ctx_skip == SKIP_UNKNOWN) 7902 { 7903 if (!cctx->ctx_had_return 7904 && compile_jump_to_end(&scope->se_u.se_if.is_end_label, 7905 JUMP_ALWAYS, cctx) == FAIL) 7906 return NULL; 7907 } 7908 7909 if (cctx->ctx_skip == SKIP_UNKNOWN) 7910 { 7911 if (scope->se_u.se_if.is_if_label >= 0) 7912 { 7913 // previous "if" or "elseif" jumps here 7914 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; 7915 isn->isn_arg.jump.jump_where = instr->ga_len; 7916 scope->se_u.se_if.is_if_label = -1; 7917 } 7918 } 7919 7920 if (cctx->ctx_skip != SKIP_UNKNOWN) 7921 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; 7922 } 7923 7924 return p; 7925 } 7926 7927 static char_u * 7928 compile_endif(char_u *arg, cctx_T *cctx) 7929 { 7930 scope_T *scope = cctx->ctx_scope; 7931 ifscope_T *ifscope; 7932 garray_T *instr = &cctx->ctx_instr; 7933 isn_T *isn; 7934 7935 if (misplaced_cmdmod(cctx)) 7936 return NULL; 7937 7938 if (scope == NULL || scope->se_type != IF_SCOPE) 7939 { 7940 emsg(_(e_endif_without_if)); 7941 return NULL; 7942 } 7943 ifscope = &scope->se_u.se_if; 7944 unwind_locals(cctx, scope->se_local_count); 7945 if (!cctx->ctx_had_return) 7946 ifscope->is_had_return = FALSE; 7947 7948 if (scope->se_u.se_if.is_if_label >= 0) 7949 { 7950 // previous "if" or "elseif" jumps here 7951 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; 7952 isn->isn_arg.jump.jump_where = instr->ga_len; 7953 } 7954 // Fill in the "end" label in jumps at the end of the blocks. 7955 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx); 7956 7957 #ifdef FEAT_PROFILE 7958 // even when skipping we count the endif as executed, unless the block it's 7959 // in is skipped 7960 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES 7961 && scope->se_skip_save != SKIP_YES) 7962 { 7963 cctx->ctx_skip = SKIP_NOT; 7964 generate_instr(cctx, ISN_PROF_START); 7965 } 7966 #endif 7967 cctx->ctx_skip = scope->se_skip_save; 7968 7969 // If all the blocks end in :return and there is an :else then the 7970 // had_return flag is set. 7971 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; 7972 7973 drop_scope(cctx); 7974 return arg; 7975 } 7976 7977 /* 7978 * Compile "for var in expr": 7979 * 7980 * Produces instructions: 7981 * PUSHNR -1 7982 * STORE loop-idx Set index to -1 7983 * EVAL expr result of "expr" on top of stack 7984 * top: FOR loop-idx, end Increment index, use list on bottom of stack 7985 * - if beyond end, jump to "end" 7986 * - otherwise get item from list and push it 7987 * STORE var Store item in "var" 7988 * ... body ... 7989 * JUMP top Jump back to repeat 7990 * end: DROP Drop the result of "expr" 7991 * 7992 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var": 7993 * UNPACK 2 Split item in 2 7994 * STORE var1 Store item in "var1" 7995 * STORE var2 Store item in "var2" 7996 */ 7997 static char_u * 7998 compile_for(char_u *arg_start, cctx_T *cctx) 7999 { 8000 char_u *arg; 8001 char_u *arg_end; 8002 char_u *name = NULL; 8003 char_u *p; 8004 char_u *wp; 8005 int var_count = 0; 8006 int var_list = FALSE; 8007 int semicolon = FALSE; 8008 size_t varlen; 8009 garray_T *stack = &cctx->ctx_type_stack; 8010 garray_T *instr = &cctx->ctx_instr; 8011 scope_T *scope; 8012 lvar_T *loop_lvar; // loop iteration variable 8013 lvar_T *var_lvar; // variable for "var" 8014 type_T *vartype; 8015 type_T *item_type = &t_any; 8016 int idx; 8017 int prev_lnum = cctx->ctx_prev_lnum; 8018 8019 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE); 8020 if (p == NULL) 8021 return NULL; 8022 if (var_count == 0) 8023 var_count = 1; 8024 else 8025 var_list = TRUE; // can also be a list of one variable 8026 8027 // consume "in" 8028 wp = p; 8029 if (may_get_next_line_error(wp, &p, cctx) == FAIL) 8030 return NULL; 8031 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2])) 8032 { 8033 if (*p == ':' && wp != p) 8034 semsg(_(e_no_white_space_allowed_before_colon_str), p); 8035 else 8036 emsg(_(e_missing_in)); 8037 return NULL; 8038 } 8039 wp = p + 2; 8040 if (may_get_next_line_error(wp, &p, cctx) == FAIL) 8041 return NULL; 8042 8043 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR 8044 // instruction. 8045 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0 8046 && ((isn_T *)instr->ga_data)[instr->ga_len - 1] 8047 .isn_type == ISN_DEBUG) 8048 { 8049 --instr->ga_len; 8050 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len] 8051 .isn_arg.debug.dbg_break_lnum; 8052 } 8053 8054 scope = new_scope(cctx, FOR_SCOPE); 8055 if (scope == NULL) 8056 return NULL; 8057 8058 // Reserve a variable to store the loop iteration counter and initialize it 8059 // to -1. 8060 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); 8061 if (loop_lvar == NULL) 8062 { 8063 // out of memory 8064 drop_scope(cctx); 8065 return NULL; 8066 } 8067 generate_STORENR(cctx, loop_lvar->lv_idx, -1); 8068 8069 // compile "expr", it remains on the stack until "endfor" 8070 arg = p; 8071 if (compile_expr0(&arg, cctx) == FAIL) 8072 { 8073 drop_scope(cctx); 8074 return NULL; 8075 } 8076 arg_end = arg; 8077 8078 if (cctx->ctx_skip != SKIP_YES) 8079 { 8080 // If we know the type of "var" and it is a not a supported type we can 8081 // give an error now. 8082 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 8083 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING 8084 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY) 8085 { 8086 semsg(_(e_for_loop_on_str_not_supported), 8087 vartype_name(vartype->tt_type)); 8088 drop_scope(cctx); 8089 return NULL; 8090 } 8091 8092 if (vartype->tt_type == VAR_STRING) 8093 item_type = &t_string; 8094 else if (vartype->tt_type == VAR_BLOB) 8095 item_type = &t_number; 8096 else if (vartype->tt_type == VAR_LIST 8097 && vartype->tt_member->tt_type != VAR_ANY) 8098 { 8099 if (!var_list) 8100 item_type = vartype->tt_member; 8101 else if (vartype->tt_member->tt_type == VAR_LIST 8102 && vartype->tt_member->tt_member->tt_type != VAR_ANY) 8103 // TODO: should get the type for each lhs 8104 item_type = vartype->tt_member->tt_member; 8105 } 8106 8107 // CMDMOD_REV must come before the FOR instruction. 8108 generate_undo_cmdmods(cctx); 8109 8110 // "for_end" is set when ":endfor" is found 8111 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx); 8112 8113 generate_FOR(cctx, loop_lvar->lv_idx); 8114 8115 arg = arg_start; 8116 if (var_list) 8117 { 8118 generate_UNPACK(cctx, var_count, semicolon); 8119 arg = skipwhite(arg + 1); // skip white after '[' 8120 8121 // the list item is replaced by a number of items 8122 if (GA_GROW_FAILS(stack, var_count - 1)) 8123 { 8124 drop_scope(cctx); 8125 return NULL; 8126 } 8127 --stack->ga_len; 8128 for (idx = 0; idx < var_count; ++idx) 8129 { 8130 ((type_T **)stack->ga_data)[stack->ga_len] = 8131 (semicolon && idx == 0) ? vartype : item_type; 8132 ++stack->ga_len; 8133 } 8134 } 8135 8136 for (idx = 0; idx < var_count; ++idx) 8137 { 8138 assign_dest_T dest = dest_local; 8139 int opt_flags = 0; 8140 int vimvaridx = -1; 8141 type_T *type = &t_any; 8142 type_T *lhs_type = &t_any; 8143 where_T where = WHERE_INIT; 8144 8145 p = skip_var_one(arg, FALSE); 8146 varlen = p - arg; 8147 name = vim_strnsave(arg, varlen); 8148 if (name == NULL) 8149 goto failed; 8150 if (*p == ':') 8151 { 8152 p = skipwhite(p + 1); 8153 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE); 8154 } 8155 8156 // TODO: script var not supported? 8157 if (get_var_dest(name, &dest, CMD_for, &opt_flags, 8158 &vimvaridx, &type, cctx) == FAIL) 8159 goto failed; 8160 if (dest != dest_local) 8161 { 8162 if (generate_store_var(cctx, dest, opt_flags, vimvaridx, 8163 0, 0, type, name) == FAIL) 8164 goto failed; 8165 } 8166 else if (varlen == 1 && *arg == '_') 8167 { 8168 // Assigning to "_": drop the value. 8169 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) 8170 goto failed; 8171 } 8172 else 8173 { 8174 if (lookup_local(arg, varlen, NULL, cctx) == OK) 8175 { 8176 semsg(_(e_variable_already_declared), arg); 8177 goto failed; 8178 } 8179 8180 if (STRNCMP(name, "s:", 2) == 0) 8181 { 8182 semsg(_(e_cannot_declare_script_variable_in_function), name); 8183 goto failed; 8184 } 8185 8186 // Reserve a variable to store "var". 8187 where.wt_index = var_list ? idx + 1 : 0; 8188 where.wt_variable = TRUE; 8189 if (lhs_type == &t_any) 8190 lhs_type = item_type; 8191 else if (item_type != &t_unknown 8192 && (item_type == &t_any 8193 ? need_type(item_type, lhs_type, 8194 -1, 0, cctx, FALSE, FALSE) 8195 : check_type(lhs_type, item_type, TRUE, where)) 8196 == FAIL) 8197 goto failed; 8198 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type); 8199 if (var_lvar == NULL) 8200 // out of memory or used as an argument 8201 goto failed; 8202 8203 if (semicolon && idx == var_count - 1) 8204 var_lvar->lv_type = vartype; 8205 else 8206 var_lvar->lv_type = item_type; 8207 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); 8208 } 8209 8210 if (*p == ',' || *p == ';') 8211 ++p; 8212 arg = skipwhite(p); 8213 vim_free(name); 8214 } 8215 8216 if (cctx->ctx_compile_type == CT_DEBUG) 8217 { 8218 int save_prev_lnum = cctx->ctx_prev_lnum; 8219 8220 // Add ISN_DEBUG here, so that the loop variables can be inspected. 8221 // Use the prev_lnum from the ISN_DEBUG instruction removed above. 8222 cctx->ctx_prev_lnum = prev_lnum; 8223 generate_instr_debug(cctx); 8224 cctx->ctx_prev_lnum = save_prev_lnum; 8225 } 8226 } 8227 8228 return arg_end; 8229 8230 failed: 8231 vim_free(name); 8232 drop_scope(cctx); 8233 return NULL; 8234 } 8235 8236 /* 8237 * compile "endfor" 8238 */ 8239 static char_u * 8240 compile_endfor(char_u *arg, cctx_T *cctx) 8241 { 8242 garray_T *instr = &cctx->ctx_instr; 8243 scope_T *scope = cctx->ctx_scope; 8244 forscope_T *forscope; 8245 isn_T *isn; 8246 8247 if (misplaced_cmdmod(cctx)) 8248 return NULL; 8249 8250 if (scope == NULL || scope->se_type != FOR_SCOPE) 8251 { 8252 emsg(_(e_for)); 8253 return NULL; 8254 } 8255 forscope = &scope->se_u.se_for; 8256 cctx->ctx_scope = scope->se_outer; 8257 if (cctx->ctx_skip != SKIP_YES) 8258 { 8259 unwind_locals(cctx, scope->se_local_count); 8260 8261 // At end of ":for" scope jump back to the FOR instruction. 8262 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); 8263 8264 // Fill in the "end" label in the FOR statement so it can jump here. 8265 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; 8266 isn->isn_arg.forloop.for_end = instr->ga_len; 8267 8268 // Fill in the "end" label any BREAK statements 8269 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx); 8270 8271 // Below the ":for" scope drop the "expr" list from the stack. 8272 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) 8273 return NULL; 8274 } 8275 8276 vim_free(scope); 8277 8278 return arg; 8279 } 8280 8281 /* 8282 * compile "while expr" 8283 * 8284 * Produces instructions: 8285 * top: EVAL expr Push result of "expr" 8286 * JUMP_IF_FALSE end jump if false 8287 * ... body ... 8288 * JUMP top Jump back to repeat 8289 * end: 8290 * 8291 */ 8292 static char_u * 8293 compile_while(char_u *arg, cctx_T *cctx) 8294 { 8295 char_u *p = arg; 8296 scope_T *scope; 8297 8298 scope = new_scope(cctx, WHILE_SCOPE); 8299 if (scope == NULL) 8300 return NULL; 8301 8302 // "endwhile" jumps back here, one before when profiling or using cmdmods 8303 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx); 8304 8305 // compile "expr" 8306 if (compile_expr0(&p, cctx) == FAIL) 8307 return NULL; 8308 8309 if (!ends_excmd2(arg, skipwhite(p))) 8310 { 8311 semsg(_(e_trailing_arg), p); 8312 return NULL; 8313 } 8314 8315 if (cctx->ctx_skip != SKIP_YES) 8316 { 8317 if (bool_on_stack(cctx) == FAIL) 8318 return FAIL; 8319 8320 // CMDMOD_REV must come before the jump 8321 generate_undo_cmdmods(cctx); 8322 8323 // "while_end" is set when ":endwhile" is found 8324 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label, 8325 JUMP_IF_FALSE, cctx) == FAIL) 8326 return FAIL; 8327 } 8328 8329 return p; 8330 } 8331 8332 /* 8333 * compile "endwhile" 8334 */ 8335 static char_u * 8336 compile_endwhile(char_u *arg, cctx_T *cctx) 8337 { 8338 scope_T *scope = cctx->ctx_scope; 8339 garray_T *instr = &cctx->ctx_instr; 8340 8341 if (misplaced_cmdmod(cctx)) 8342 return NULL; 8343 if (scope == NULL || scope->se_type != WHILE_SCOPE) 8344 { 8345 emsg(_(e_while)); 8346 return NULL; 8347 } 8348 cctx->ctx_scope = scope->se_outer; 8349 if (cctx->ctx_skip != SKIP_YES) 8350 { 8351 unwind_locals(cctx, scope->se_local_count); 8352 8353 #ifdef FEAT_PROFILE 8354 // count the endwhile before jumping 8355 may_generate_prof_end(cctx, cctx->ctx_lnum); 8356 #endif 8357 8358 // At end of ":for" scope jump back to the FOR instruction. 8359 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label); 8360 8361 // Fill in the "end" label in the WHILE statement so it can jump here. 8362 // And in any jumps for ":break" 8363 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, 8364 instr->ga_len, cctx); 8365 } 8366 8367 vim_free(scope); 8368 8369 return arg; 8370 } 8371 8372 /* 8373 * compile "continue" 8374 */ 8375 static char_u * 8376 compile_continue(char_u *arg, cctx_T *cctx) 8377 { 8378 scope_T *scope = cctx->ctx_scope; 8379 int try_scopes = 0; 8380 int loop_label; 8381 8382 for (;;) 8383 { 8384 if (scope == NULL) 8385 { 8386 emsg(_(e_continue)); 8387 return NULL; 8388 } 8389 if (scope->se_type == FOR_SCOPE) 8390 { 8391 loop_label = scope->se_u.se_for.fs_top_label; 8392 break; 8393 } 8394 if (scope->se_type == WHILE_SCOPE) 8395 { 8396 loop_label = scope->se_u.se_while.ws_top_label; 8397 break; 8398 } 8399 if (scope->se_type == TRY_SCOPE) 8400 ++try_scopes; 8401 scope = scope->se_outer; 8402 } 8403 8404 if (try_scopes > 0) 8405 // Inside one or more try/catch blocks we first need to jump to the 8406 // "finally" or "endtry" to cleanup. 8407 generate_TRYCONT(cctx, try_scopes, loop_label); 8408 else 8409 // Jump back to the FOR or WHILE instruction. 8410 generate_JUMP(cctx, JUMP_ALWAYS, loop_label); 8411 8412 return arg; 8413 } 8414 8415 /* 8416 * compile "break" 8417 */ 8418 static char_u * 8419 compile_break(char_u *arg, cctx_T *cctx) 8420 { 8421 scope_T *scope = cctx->ctx_scope; 8422 endlabel_T **el; 8423 8424 for (;;) 8425 { 8426 if (scope == NULL) 8427 { 8428 emsg(_(e_break)); 8429 return NULL; 8430 } 8431 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) 8432 break; 8433 scope = scope->se_outer; 8434 } 8435 8436 // Jump to the end of the FOR or WHILE loop. 8437 if (scope->se_type == FOR_SCOPE) 8438 el = &scope->se_u.se_for.fs_end_label; 8439 else 8440 el = &scope->se_u.se_while.ws_end_label; 8441 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) 8442 return FAIL; 8443 8444 return arg; 8445 } 8446 8447 /* 8448 * compile "{" start of block 8449 */ 8450 static char_u * 8451 compile_block(char_u *arg, cctx_T *cctx) 8452 { 8453 if (new_scope(cctx, BLOCK_SCOPE) == NULL) 8454 return NULL; 8455 return skipwhite(arg + 1); 8456 } 8457 8458 /* 8459 * compile end of block: drop one scope 8460 */ 8461 static void 8462 compile_endblock(cctx_T *cctx) 8463 { 8464 scope_T *scope = cctx->ctx_scope; 8465 8466 cctx->ctx_scope = scope->se_outer; 8467 unwind_locals(cctx, scope->se_local_count); 8468 vim_free(scope); 8469 } 8470 8471 /* 8472 * compile "try" 8473 * Creates a new scope for the try-endtry, pointing to the first catch and 8474 * finally. 8475 * Creates another scope for the "try" block itself. 8476 * TRY instruction sets up exception handling at runtime. 8477 * 8478 * "try" 8479 * TRY -> catch1, -> finally push trystack entry 8480 * ... try block 8481 * "throw {exception}" 8482 * EVAL {exception} 8483 * THROW create exception 8484 * ... try block 8485 * " catch {expr}" 8486 * JUMP -> finally 8487 * catch1: PUSH exception 8488 * EVAL {expr} 8489 * MATCH 8490 * JUMP nomatch -> catch2 8491 * CATCH remove exception 8492 * ... catch block 8493 * " catch" 8494 * JUMP -> finally 8495 * catch2: CATCH remove exception 8496 * ... catch block 8497 * " finally" 8498 * finally: 8499 * ... finally block 8500 * " endtry" 8501 * ENDTRY pop trystack entry, may rethrow 8502 */ 8503 static char_u * 8504 compile_try(char_u *arg, cctx_T *cctx) 8505 { 8506 garray_T *instr = &cctx->ctx_instr; 8507 scope_T *try_scope; 8508 scope_T *scope; 8509 8510 if (misplaced_cmdmod(cctx)) 8511 return NULL; 8512 8513 // scope that holds the jumps that go to catch/finally/endtry 8514 try_scope = new_scope(cctx, TRY_SCOPE); 8515 if (try_scope == NULL) 8516 return NULL; 8517 8518 if (cctx->ctx_skip != SKIP_YES) 8519 { 8520 isn_T *isn; 8521 8522 // "try_catch" is set when the first ":catch" is found or when no catch 8523 // is found and ":finally" is found. 8524 // "try_finally" is set when ":finally" is found 8525 // "try_endtry" is set when ":endtry" is found 8526 try_scope->se_u.se_try.ts_try_label = instr->ga_len; 8527 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL) 8528 return NULL; 8529 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T); 8530 if (isn->isn_arg.try.try_ref == NULL) 8531 return NULL; 8532 } 8533 8534 // scope for the try block itself 8535 scope = new_scope(cctx, BLOCK_SCOPE); 8536 if (scope == NULL) 8537 return NULL; 8538 8539 return arg; 8540 } 8541 8542 /* 8543 * compile "catch {expr}" 8544 */ 8545 static char_u * 8546 compile_catch(char_u *arg, cctx_T *cctx UNUSED) 8547 { 8548 scope_T *scope = cctx->ctx_scope; 8549 garray_T *instr = &cctx->ctx_instr; 8550 char_u *p; 8551 isn_T *isn; 8552 8553 if (misplaced_cmdmod(cctx)) 8554 return NULL; 8555 8556 // end block scope from :try or :catch 8557 if (scope != NULL && scope->se_type == BLOCK_SCOPE) 8558 compile_endblock(cctx); 8559 scope = cctx->ctx_scope; 8560 8561 // Error if not in a :try scope 8562 if (scope == NULL || scope->se_type != TRY_SCOPE) 8563 { 8564 emsg(_(e_catch)); 8565 return NULL; 8566 } 8567 8568 if (scope->se_u.se_try.ts_caught_all) 8569 { 8570 emsg(_(e_catch_unreachable_after_catch_all)); 8571 return NULL; 8572 } 8573 8574 if (cctx->ctx_skip != SKIP_YES) 8575 { 8576 #ifdef FEAT_PROFILE 8577 // the profile-start should be after the jump 8578 if (cctx->ctx_compile_type == CT_PROFILE 8579 && instr->ga_len > 0 8580 && ((isn_T *)instr->ga_data)[instr->ga_len - 1] 8581 .isn_type == ISN_PROF_START) 8582 --instr->ga_len; 8583 #endif 8584 // Jump from end of previous block to :finally or :endtry 8585 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label, 8586 JUMP_ALWAYS, cctx) == FAIL) 8587 return NULL; 8588 8589 // End :try or :catch scope: set value in ISN_TRY instruction 8590 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; 8591 if (isn->isn_arg.try.try_ref->try_catch == 0) 8592 isn->isn_arg.try.try_ref->try_catch = instr->ga_len; 8593 if (scope->se_u.se_try.ts_catch_label != 0) 8594 { 8595 // Previous catch without match jumps here 8596 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; 8597 isn->isn_arg.jump.jump_where = instr->ga_len; 8598 } 8599 #ifdef FEAT_PROFILE 8600 if (cctx->ctx_compile_type == CT_PROFILE) 8601 { 8602 // a "throw" that jumps here needs to be counted 8603 generate_instr(cctx, ISN_PROF_END); 8604 // the "catch" is also counted 8605 generate_instr(cctx, ISN_PROF_START); 8606 } 8607 #endif 8608 if (cctx->ctx_compile_type == CT_DEBUG) 8609 generate_instr_debug(cctx); 8610 } 8611 8612 p = skipwhite(arg); 8613 if (ends_excmd2(arg, p)) 8614 { 8615 scope->se_u.se_try.ts_caught_all = TRUE; 8616 scope->se_u.se_try.ts_catch_label = 0; 8617 } 8618 else 8619 { 8620 char_u *end; 8621 char_u *pat; 8622 char_u *tofree = NULL; 8623 int dropped = 0; 8624 int len; 8625 8626 // Push v:exception, push {expr} and MATCH 8627 generate_instr_type(cctx, ISN_PUSHEXC, &t_string); 8628 8629 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL); 8630 if (*end != *p) 8631 { 8632 semsg(_(e_separator_mismatch_str), p); 8633 vim_free(tofree); 8634 return FAIL; 8635 } 8636 if (tofree == NULL) 8637 len = (int)(end - (p + 1)); 8638 else 8639 len = (int)(end - tofree); 8640 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); 8641 vim_free(tofree); 8642 p += len + 2 + dropped; 8643 if (pat == NULL) 8644 return FAIL; 8645 if (generate_PUSHS(cctx, &pat) == FAIL) 8646 return FAIL; 8647 8648 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) 8649 return NULL; 8650 8651 scope->se_u.se_try.ts_catch_label = instr->ga_len; 8652 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) 8653 return NULL; 8654 } 8655 8656 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL) 8657 return NULL; 8658 8659 if (new_scope(cctx, BLOCK_SCOPE) == NULL) 8660 return NULL; 8661 return p; 8662 } 8663 8664 static char_u * 8665 compile_finally(char_u *arg, cctx_T *cctx) 8666 { 8667 scope_T *scope = cctx->ctx_scope; 8668 garray_T *instr = &cctx->ctx_instr; 8669 isn_T *isn; 8670 int this_instr; 8671 8672 if (misplaced_cmdmod(cctx)) 8673 return NULL; 8674 8675 // end block scope from :try or :catch 8676 if (scope != NULL && scope->se_type == BLOCK_SCOPE) 8677 compile_endblock(cctx); 8678 scope = cctx->ctx_scope; 8679 8680 // Error if not in a :try scope 8681 if (scope == NULL || scope->se_type != TRY_SCOPE) 8682 { 8683 emsg(_(e_finally)); 8684 return NULL; 8685 } 8686 8687 if (cctx->ctx_skip != SKIP_YES) 8688 { 8689 // End :catch or :finally scope: set value in ISN_TRY instruction 8690 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; 8691 if (isn->isn_arg.try.try_ref->try_finally != 0) 8692 { 8693 emsg(_(e_finally_dup)); 8694 return NULL; 8695 } 8696 8697 this_instr = instr->ga_len; 8698 #ifdef FEAT_PROFILE 8699 if (cctx->ctx_compile_type == CT_PROFILE 8700 && ((isn_T *)instr->ga_data)[this_instr - 1] 8701 .isn_type == ISN_PROF_START) 8702 { 8703 // jump to the profile start of the "finally" 8704 --this_instr; 8705 8706 // jump to the profile end above it 8707 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1] 8708 .isn_type == ISN_PROF_END) 8709 --this_instr; 8710 } 8711 #endif 8712 8713 // Fill in the "end" label in jumps at the end of the blocks. 8714 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, 8715 this_instr, cctx); 8716 8717 // If there is no :catch then an exception jumps to :finally. 8718 if (isn->isn_arg.try.try_ref->try_catch == 0) 8719 isn->isn_arg.try.try_ref->try_catch = this_instr; 8720 isn->isn_arg.try.try_ref->try_finally = this_instr; 8721 if (scope->se_u.se_try.ts_catch_label != 0) 8722 { 8723 // Previous catch without match jumps here 8724 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; 8725 isn->isn_arg.jump.jump_where = this_instr; 8726 scope->se_u.se_try.ts_catch_label = 0; 8727 } 8728 if (generate_instr(cctx, ISN_FINALLY) == NULL) 8729 return NULL; 8730 8731 // TODO: set index in ts_finally_label jumps 8732 } 8733 8734 return arg; 8735 } 8736 8737 static char_u * 8738 compile_endtry(char_u *arg, cctx_T *cctx) 8739 { 8740 scope_T *scope = cctx->ctx_scope; 8741 garray_T *instr = &cctx->ctx_instr; 8742 isn_T *try_isn; 8743 8744 if (misplaced_cmdmod(cctx)) 8745 return NULL; 8746 8747 // end block scope from :catch or :finally 8748 if (scope != NULL && scope->se_type == BLOCK_SCOPE) 8749 compile_endblock(cctx); 8750 scope = cctx->ctx_scope; 8751 8752 // Error if not in a :try scope 8753 if (scope == NULL || scope->se_type != TRY_SCOPE) 8754 { 8755 if (scope == NULL) 8756 emsg(_(e_no_endtry)); 8757 else if (scope->se_type == WHILE_SCOPE) 8758 emsg(_(e_endwhile)); 8759 else if (scope->se_type == FOR_SCOPE) 8760 emsg(_(e_endfor)); 8761 else 8762 emsg(_(e_endif)); 8763 return NULL; 8764 } 8765 8766 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; 8767 if (cctx->ctx_skip != SKIP_YES) 8768 { 8769 if (try_isn->isn_arg.try.try_ref->try_catch == 0 8770 && try_isn->isn_arg.try.try_ref->try_finally == 0) 8771 { 8772 emsg(_(e_missing_catch_or_finally)); 8773 return NULL; 8774 } 8775 8776 #ifdef FEAT_PROFILE 8777 if (cctx->ctx_compile_type == CT_PROFILE 8778 && ((isn_T *)instr->ga_data)[instr->ga_len - 1] 8779 .isn_type == ISN_PROF_START) 8780 // move the profile start after "endtry" so that it's not counted when 8781 // the exception is rethrown. 8782 --instr->ga_len; 8783 #endif 8784 8785 // Fill in the "end" label in jumps at the end of the blocks, if not 8786 // done by ":finally". 8787 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, 8788 instr->ga_len, cctx); 8789 8790 if (scope->se_u.se_try.ts_catch_label != 0) 8791 { 8792 // Last catch without match jumps here 8793 isn_T *isn = ((isn_T *)instr->ga_data) 8794 + scope->se_u.se_try.ts_catch_label; 8795 isn->isn_arg.jump.jump_where = instr->ga_len; 8796 } 8797 } 8798 8799 compile_endblock(cctx); 8800 8801 if (cctx->ctx_skip != SKIP_YES) 8802 { 8803 // End :catch or :finally scope: set instruction index in ISN_TRY 8804 // instruction 8805 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len; 8806 if (cctx->ctx_skip != SKIP_YES 8807 && generate_instr(cctx, ISN_ENDTRY) == NULL) 8808 return NULL; 8809 #ifdef FEAT_PROFILE 8810 if (cctx->ctx_compile_type == CT_PROFILE) 8811 generate_instr(cctx, ISN_PROF_START); 8812 #endif 8813 } 8814 return arg; 8815 } 8816 8817 /* 8818 * compile "throw {expr}" 8819 */ 8820 static char_u * 8821 compile_throw(char_u *arg, cctx_T *cctx UNUSED) 8822 { 8823 char_u *p = skipwhite(arg); 8824 8825 if (compile_expr0(&p, cctx) == FAIL) 8826 return NULL; 8827 if (cctx->ctx_skip == SKIP_YES) 8828 return p; 8829 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL) 8830 return NULL; 8831 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) 8832 return NULL; 8833 8834 return p; 8835 } 8836 8837 static char_u * 8838 compile_eval(char_u *arg, cctx_T *cctx) 8839 { 8840 char_u *p = arg; 8841 int name_only; 8842 long lnum = SOURCING_LNUM; 8843 8844 // find_ex_command() will consider a variable name an expression, assuming 8845 // that something follows on the next line. Check that something actually 8846 // follows, otherwise it's probably a misplaced command. 8847 name_only = cmd_is_name_only(arg); 8848 8849 if (compile_expr0(&p, cctx) == FAIL) 8850 return NULL; 8851 8852 if (name_only && lnum == SOURCING_LNUM) 8853 { 8854 semsg(_(e_expression_without_effect_str), arg); 8855 return NULL; 8856 } 8857 8858 // drop the result 8859 generate_instr_drop(cctx, ISN_DROP, 1); 8860 8861 return skipwhite(p); 8862 } 8863 8864 /* 8865 * compile "echo expr" 8866 * compile "echomsg expr" 8867 * compile "echoerr expr" 8868 * compile "echoconsole expr" 8869 * compile "execute expr" 8870 */ 8871 static char_u * 8872 compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) 8873 { 8874 char_u *p = arg; 8875 char_u *prev = arg; 8876 char_u *expr_start; 8877 int count = 0; 8878 int start_ctx_lnum = cctx->ctx_lnum; 8879 garray_T *stack = &cctx->ctx_type_stack; 8880 type_T *type; 8881 8882 for (;;) 8883 { 8884 if (ends_excmd2(prev, p)) 8885 break; 8886 expr_start = p; 8887 if (compile_expr0(&p, cctx) == FAIL) 8888 return NULL; 8889 8890 if (cctx->ctx_skip != SKIP_YES) 8891 { 8892 // check for non-void type 8893 type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 8894 if (type->tt_type == VAR_VOID) 8895 { 8896 semsg(_(e_expression_does_not_result_in_value_str), expr_start); 8897 return NULL; 8898 } 8899 } 8900 8901 ++count; 8902 prev = p; 8903 p = skipwhite(p); 8904 } 8905 8906 if (count > 0) 8907 { 8908 long save_lnum = cctx->ctx_lnum; 8909 8910 // Use the line number where the command started. 8911 cctx->ctx_lnum = start_ctx_lnum; 8912 8913 if (cmdidx == CMD_echo || cmdidx == CMD_echon) 8914 generate_ECHO(cctx, cmdidx == CMD_echo, count); 8915 else if (cmdidx == CMD_execute) 8916 generate_MULT_EXPR(cctx, ISN_EXECUTE, count); 8917 else if (cmdidx == CMD_echomsg) 8918 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); 8919 else if (cmdidx == CMD_echoconsole) 8920 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count); 8921 else 8922 generate_MULT_EXPR(cctx, ISN_ECHOERR, count); 8923 8924 cctx->ctx_lnum = save_lnum; 8925 } 8926 return p; 8927 } 8928 8929 /* 8930 * If "eap" has a range that is not a constant generate an ISN_RANGE 8931 * instruction to compute it and return OK. 8932 * Otherwise return FAIL, the caller must deal with any range. 8933 */ 8934 static int 8935 compile_variable_range(exarg_T *eap, cctx_T *cctx) 8936 { 8937 char_u *range_end = skip_range(eap->cmd, TRUE, NULL); 8938 char_u *p = skipdigits(eap->cmd); 8939 8940 if (p == range_end) 8941 return FAIL; 8942 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd)); 8943 } 8944 8945 /* 8946 * :put r 8947 * :put ={expr} 8948 */ 8949 static char_u * 8950 compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx) 8951 { 8952 char_u *line = arg; 8953 linenr_T lnum; 8954 char *errormsg; 8955 int above = eap->forceit; 8956 8957 eap->regname = *line; 8958 8959 if (eap->regname == '=') 8960 { 8961 char_u *p = line + 1; 8962 8963 if (compile_expr0(&p, cctx) == FAIL) 8964 return NULL; 8965 line = p; 8966 } 8967 else if (eap->regname != NUL) 8968 ++line; 8969 8970 if (compile_variable_range(eap, cctx) == OK) 8971 { 8972 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE; 8973 } 8974 else 8975 { 8976 // Either no range or a number. 8977 // "errormsg" will not be set because the range is ADDR_LINES. 8978 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL) 8979 // cannot happen 8980 return NULL; 8981 if (eap->addr_count == 0) 8982 lnum = -1; 8983 else 8984 lnum = eap->line2; 8985 if (above) 8986 --lnum; 8987 } 8988 8989 generate_PUT(cctx, eap->regname, lnum); 8990 return line; 8991 } 8992 8993 /* 8994 * A command that is not compiled, execute with legacy code. 8995 */ 8996 static char_u * 8997 compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx) 8998 { 8999 char_u *line = line_arg; 9000 char_u *p; 9001 int has_expr = FALSE; 9002 char_u *nextcmd = (char_u *)""; 9003 char_u *tofree = NULL; 9004 9005 if (cctx->ctx_skip == SKIP_YES) 9006 goto theend; 9007 9008 // If there was a prececing command modifier, drop it and include it in the 9009 // EXEC command. 9010 if (cctx->ctx_has_cmdmod) 9011 { 9012 garray_T *instr = &cctx->ctx_instr; 9013 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; 9014 9015 if (isn->isn_type == ISN_CMDMOD) 9016 { 9017 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod 9018 ->cmod_filter_regmatch.regprog); 9019 vim_free(isn->isn_arg.cmdmod.cf_cmdmod); 9020 --instr->ga_len; 9021 cctx->ctx_has_cmdmod = FALSE; 9022 } 9023 } 9024 9025 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) 9026 { 9027 long argt = eap->argt; 9028 int usefilter = FALSE; 9029 9030 has_expr = argt & (EX_XFILE | EX_EXPAND); 9031 9032 // If the command can be followed by a bar, find the bar and truncate 9033 // it, so that the following command can be compiled. 9034 // The '|' is overwritten with a NUL, it is put back below. 9035 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) 9036 && *eap->arg == '!') 9037 // :w !filter or :r !filter or :r! filter 9038 usefilter = TRUE; 9039 if ((argt & EX_TRLBAR) && !usefilter) 9040 { 9041 eap->argt = argt; 9042 separate_nextcmd(eap); 9043 if (eap->nextcmd != NULL) 9044 nextcmd = eap->nextcmd; 9045 } 9046 else if (eap->cmdidx == CMD_wincmd) 9047 { 9048 p = eap->arg; 9049 if (*p != NUL) 9050 ++p; 9051 if (*p == 'g' || *p == Ctrl_G) 9052 ++p; 9053 p = skipwhite(p); 9054 if (*p == '|') 9055 { 9056 *p = NUL; 9057 nextcmd = p + 1; 9058 } 9059 } 9060 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd) 9061 { 9062 // If there is a trailing '{' read lines until the '}' 9063 p = eap->arg + STRLEN(eap->arg) - 1; 9064 while (p > eap->arg && VIM_ISWHITE(*p)) 9065 --p; 9066 if (*p == '{') 9067 { 9068 exarg_T ea; 9069 int flags; // unused 9070 int start_lnum = SOURCING_LNUM; 9071 9072 CLEAR_FIELD(ea); 9073 ea.arg = eap->arg; 9074 fill_exarg_from_cctx(&ea, cctx); 9075 (void)may_get_cmd_block(&ea, p, &tofree, &flags); 9076 if (tofree != NULL) 9077 { 9078 *p = NUL; 9079 line = concat_str(line, tofree); 9080 if (line == NULL) 9081 goto theend; 9082 vim_free(tofree); 9083 tofree = line; 9084 SOURCING_LNUM = start_lnum; 9085 } 9086 } 9087 } 9088 } 9089 9090 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) 9091 { 9092 // expand filename in "syntax include [@group] filename" 9093 has_expr = TRUE; 9094 eap->arg = skipwhite(eap->arg + 7); 9095 if (*eap->arg == '@') 9096 eap->arg = skiptowhite(eap->arg); 9097 } 9098 9099 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal) 9100 && STRLEN(eap->arg) > 4) 9101 { 9102 int delim = *eap->arg; 9103 9104 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL); 9105 if (*p == delim) 9106 { 9107 eap->arg = p + 1; 9108 has_expr = TRUE; 9109 } 9110 } 9111 9112 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed) 9113 { 9114 // TODO: should only expand when appropriate for the command 9115 eap->arg = skiptowhite(eap->arg); 9116 has_expr = TRUE; 9117 } 9118 9119 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) 9120 { 9121 int count = 0; 9122 char_u *start = skipwhite(line); 9123 9124 // :cmd xxx`=expr1`yyy`=expr2`zzz 9125 // PUSHS ":cmd xxx" 9126 // eval expr1 9127 // PUSHS "yyy" 9128 // eval expr2 9129 // PUSHS "zzz" 9130 // EXECCONCAT 5 9131 for (;;) 9132 { 9133 if (p > start) 9134 { 9135 char_u *val = vim_strnsave(start, p - start); 9136 9137 generate_PUSHS(cctx, &val); 9138 ++count; 9139 } 9140 p += 2; 9141 if (compile_expr0(&p, cctx) == FAIL) 9142 return NULL; 9143 may_generate_2STRING(-1, TRUE, cctx); 9144 ++count; 9145 p = skipwhite(p); 9146 if (*p != '`') 9147 { 9148 emsg(_(e_missing_backtick)); 9149 return NULL; 9150 } 9151 start = p + 1; 9152 9153 p = (char_u *)strstr((char *)start, "`="); 9154 if (p == NULL) 9155 { 9156 if (*skipwhite(start) != NUL) 9157 { 9158 char_u *val = vim_strsave(start); 9159 9160 generate_PUSHS(cctx, &val); 9161 ++count; 9162 } 9163 break; 9164 } 9165 } 9166 generate_EXECCONCAT(cctx, count); 9167 } 9168 else 9169 generate_EXEC(cctx, ISN_EXEC, line); 9170 9171 theend: 9172 if (*nextcmd != NUL) 9173 { 9174 // the parser expects a pointer to the bar, put it back 9175 --nextcmd; 9176 *nextcmd = '|'; 9177 } 9178 vim_free(tofree); 9179 9180 return nextcmd; 9181 } 9182 9183 /* 9184 * A script command with heredoc, e.g. 9185 * ruby << EOF 9186 * command 9187 * EOF 9188 * Has been turned into one long line with NL characters by 9189 * get_function_body(): 9190 * ruby << EOF<NL> command<NL>EOF 9191 */ 9192 static char_u * 9193 compile_script(char_u *line, cctx_T *cctx) 9194 { 9195 if (cctx->ctx_skip != SKIP_YES) 9196 { 9197 isn_T *isn; 9198 9199 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL) 9200 return NULL; 9201 isn->isn_arg.string = vim_strsave(line); 9202 } 9203 return (char_u *)""; 9204 } 9205 9206 9207 /* 9208 * :s/pat/repl/ 9209 */ 9210 static char_u * 9211 compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx) 9212 { 9213 char_u *cmd = eap->arg; 9214 char_u *expr = (char_u *)strstr((char *)cmd, "\\="); 9215 9216 if (expr != NULL) 9217 { 9218 int delimiter = *cmd++; 9219 9220 // There is a \=expr, find it in the substitute part. 9221 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL); 9222 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=') 9223 { 9224 garray_T save_ga = cctx->ctx_instr; 9225 char_u *end; 9226 int expr_res; 9227 int trailing_error; 9228 int instr_count; 9229 isn_T *instr; 9230 isn_T *isn; 9231 9232 cmd += 3; 9233 end = skip_substitute(cmd, delimiter); 9234 9235 // Temporarily reset the list of instructions so that the jump 9236 // labels are correct. 9237 cctx->ctx_instr.ga_len = 0; 9238 cctx->ctx_instr.ga_maxlen = 0; 9239 cctx->ctx_instr.ga_data = NULL; 9240 expr_res = compile_expr0(&cmd, cctx); 9241 if (end[-1] == NUL) 9242 end[-1] = delimiter; 9243 cmd = skipwhite(cmd); 9244 trailing_error = *cmd != delimiter && *cmd != NUL; 9245 9246 if (expr_res == FAIL || trailing_error 9247 || GA_GROW_FAILS(&cctx->ctx_instr, 1)) 9248 { 9249 if (trailing_error) 9250 semsg(_(e_trailing_arg), cmd); 9251 clear_instr_ga(&cctx->ctx_instr); 9252 cctx->ctx_instr = save_ga; 9253 return NULL; 9254 } 9255 9256 // Move the generated instructions into the ISN_SUBSTITUTE 9257 // instructions, then restore the list of instructions before 9258 // adding the ISN_SUBSTITUTE instruction. 9259 instr_count = cctx->ctx_instr.ga_len; 9260 instr = cctx->ctx_instr.ga_data; 9261 instr[instr_count].isn_type = ISN_FINISH; 9262 9263 cctx->ctx_instr = save_ga; 9264 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL) 9265 { 9266 int idx; 9267 9268 for (idx = 0; idx < instr_count; ++idx) 9269 delete_instr(instr + idx); 9270 vim_free(instr); 9271 return NULL; 9272 } 9273 isn->isn_arg.subs.subs_cmd = vim_strsave(arg); 9274 isn->isn_arg.subs.subs_instr = instr; 9275 9276 // skip over flags 9277 if (*end == '&') 9278 ++end; 9279 while (ASCII_ISALPHA(*end) || *end == '#') 9280 ++end; 9281 return end; 9282 } 9283 } 9284 9285 return compile_exec(arg, eap, cctx); 9286 } 9287 9288 static char_u * 9289 compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx) 9290 { 9291 char_u *arg = eap->arg; 9292 lhs_T *lhs = &cctx->ctx_redir_lhs; 9293 9294 if (lhs->lhs_name != NULL) 9295 { 9296 if (STRNCMP(arg, "END", 3) == 0) 9297 { 9298 if (lhs->lhs_append) 9299 { 9300 // First load the current variable value. 9301 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole, 9302 cctx) == FAIL) 9303 return NULL; 9304 } 9305 9306 // Gets the redirected text and put it on the stack, then store it 9307 // in the variable. 9308 generate_instr_type(cctx, ISN_REDIREND, &t_string); 9309 9310 if (lhs->lhs_append) 9311 generate_instr_drop(cctx, ISN_CONCAT, 1); 9312 9313 if (lhs->lhs_has_index) 9314 { 9315 // Use the info in "lhs" to store the value at the index in the 9316 // list or dict. 9317 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE, 9318 &t_string, cctx) == FAIL) 9319 return NULL; 9320 } 9321 else if (generate_store_lhs(cctx, lhs, -1) == FAIL) 9322 return NULL; 9323 9324 VIM_CLEAR(lhs->lhs_name); 9325 VIM_CLEAR(lhs->lhs_whole); 9326 return arg + 3; 9327 } 9328 emsg(_(e_cannot_nest_redir)); 9329 return NULL; 9330 } 9331 9332 if (arg[0] == '=' && arg[1] == '>') 9333 { 9334 int append = FALSE; 9335 9336 // redirect to a variable is compiled 9337 arg += 2; 9338 if (*arg == '>') 9339 { 9340 ++arg; 9341 append = TRUE; 9342 } 9343 arg = skipwhite(arg); 9344 9345 if (compile_assign_lhs(arg, lhs, CMD_redir, 9346 FALSE, FALSE, 1, cctx) == FAIL) 9347 return NULL; 9348 generate_instr(cctx, ISN_REDIRSTART); 9349 lhs->lhs_append = append; 9350 if (lhs->lhs_has_index) 9351 { 9352 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total); 9353 if (lhs->lhs_whole == NULL) 9354 return NULL; 9355 } 9356 9357 return arg + lhs->lhs_varlen_total; 9358 } 9359 9360 // other redirects are handled like at script level 9361 return compile_exec(line, eap, cctx); 9362 } 9363 9364 #ifdef FEAT_QUICKFIX 9365 static char_u * 9366 compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx) 9367 { 9368 isn_T *isn; 9369 char_u *p; 9370 9371 isn = generate_instr(cctx, ISN_CEXPR_AUCMD); 9372 if (isn == NULL) 9373 return NULL; 9374 isn->isn_arg.number = eap->cmdidx; 9375 9376 p = eap->arg; 9377 if (compile_expr0(&p, cctx) == FAIL) 9378 return NULL; 9379 9380 isn = generate_instr(cctx, ISN_CEXPR_CORE); 9381 if (isn == NULL) 9382 return NULL; 9383 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T); 9384 if (isn->isn_arg.cexpr.cexpr_ref == NULL) 9385 return NULL; 9386 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx; 9387 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit; 9388 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line)); 9389 9390 return p; 9391 } 9392 #endif 9393 9394 /* 9395 * Add a function to the list of :def functions. 9396 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet. 9397 */ 9398 static int 9399 add_def_function(ufunc_T *ufunc) 9400 { 9401 dfunc_T *dfunc; 9402 9403 if (def_functions.ga_len == 0) 9404 { 9405 // The first position is not used, so that a zero uf_dfunc_idx means it 9406 // wasn't set. 9407 if (GA_GROW_FAILS(&def_functions, 1)) 9408 return FAIL; 9409 ++def_functions.ga_len; 9410 } 9411 9412 // Add the function to "def_functions". 9413 if (GA_GROW_FAILS(&def_functions, 1)) 9414 return FAIL; 9415 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; 9416 CLEAR_POINTER(dfunc); 9417 dfunc->df_idx = def_functions.ga_len; 9418 ufunc->uf_dfunc_idx = dfunc->df_idx; 9419 dfunc->df_ufunc = ufunc; 9420 dfunc->df_name = vim_strsave(ufunc->uf_name); 9421 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10); 9422 ++dfunc->df_refcount; 9423 ++def_functions.ga_len; 9424 return OK; 9425 } 9426 9427 /* 9428 * After ex_function() has collected all the function lines: parse and compile 9429 * the lines into instructions. 9430 * Adds the function to "def_functions". 9431 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of 9432 * the return statement (used for lambda). When uf_ret_type is already set 9433 * then check that it matches. 9434 * When "profiling" is true add ISN_PROF_START instructions. 9435 * "outer_cctx" is set for a nested function. 9436 * This can be used recursively through compile_lambda(), which may reallocate 9437 * "def_functions". 9438 * Returns OK or FAIL. 9439 */ 9440 int 9441 compile_def_function( 9442 ufunc_T *ufunc, 9443 int check_return_type, 9444 compiletype_T compile_type, 9445 cctx_T *outer_cctx) 9446 { 9447 char_u *line = NULL; 9448 char_u *line_to_free = NULL; 9449 char_u *p; 9450 char *errormsg = NULL; // error message 9451 cctx_T cctx; 9452 garray_T *instr; 9453 int did_emsg_before = did_emsg; 9454 int did_emsg_silent_before = did_emsg_silent; 9455 int ret = FAIL; 9456 sctx_T save_current_sctx = current_sctx; 9457 int save_estack_compiling = estack_compiling; 9458 int save_cmod_flags = cmdmod.cmod_flags; 9459 int do_estack_push; 9460 int new_def_function = FALSE; 9461 #ifdef FEAT_PROFILE 9462 int prof_lnum = -1; 9463 #endif 9464 int debug_lnum = -1; 9465 9466 // When using a function that was compiled before: Free old instructions. 9467 // The index is reused. Otherwise add a new entry in "def_functions". 9468 if (ufunc->uf_dfunc_idx > 0) 9469 { 9470 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 9471 + ufunc->uf_dfunc_idx; 9472 isn_T *instr_dest = NULL; 9473 9474 switch (compile_type) 9475 { 9476 case CT_PROFILE: 9477 #ifdef FEAT_PROFILE 9478 instr_dest = dfunc->df_instr_prof; break; 9479 #endif 9480 case CT_NONE: instr_dest = dfunc->df_instr; break; 9481 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break; 9482 } 9483 if (instr_dest != NULL) 9484 // Was compiled in this mode before: Free old instructions. 9485 delete_def_function_contents(dfunc, FALSE); 9486 ga_clear_strings(&dfunc->df_var_names); 9487 } 9488 else 9489 { 9490 if (add_def_function(ufunc) == FAIL) 9491 return FAIL; 9492 new_def_function = TRUE; 9493 } 9494 9495 ufunc->uf_def_status = UF_COMPILING; 9496 9497 CLEAR_FIELD(cctx); 9498 9499 cctx.ctx_compile_type = compile_type; 9500 cctx.ctx_ufunc = ufunc; 9501 cctx.ctx_lnum = -1; 9502 cctx.ctx_outer = outer_cctx; 9503 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); 9504 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); 9505 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); 9506 cctx.ctx_type_list = &ufunc->uf_type_list; 9507 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); 9508 instr = &cctx.ctx_instr; 9509 9510 // Set the context to the function, it may be compiled when called from 9511 // another script. Set the script version to the most modern one. 9512 // The line number will be set in next_line_from_context(). 9513 current_sctx = ufunc->uf_script_ctx; 9514 current_sctx.sc_version = SCRIPT_VERSION_VIM9; 9515 9516 // Don't use the flag from ":legacy" here. 9517 cmdmod.cmod_flags &= ~CMOD_LEGACY; 9518 9519 // Make sure error messages are OK. 9520 do_estack_push = !estack_top_is_ufunc(ufunc, 1); 9521 if (do_estack_push) 9522 estack_push_ufunc(ufunc, 1); 9523 estack_compiling = TRUE; 9524 9525 if (ufunc->uf_def_args.ga_len > 0) 9526 { 9527 int count = ufunc->uf_def_args.ga_len; 9528 int first_def_arg = ufunc->uf_args.ga_len - count; 9529 int i; 9530 char_u *arg; 9531 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); 9532 int did_set_arg_type = FALSE; 9533 9534 // Produce instructions for the default values of optional arguments. 9535 SOURCING_LNUM = 0; // line number unknown 9536 for (i = 0; i < count; ++i) 9537 { 9538 garray_T *stack = &cctx.ctx_type_stack; 9539 type_T *val_type; 9540 int arg_idx = first_def_arg + i; 9541 where_T where = WHERE_INIT; 9542 int r; 9543 int jump_instr_idx = instr->ga_len; 9544 isn_T *isn; 9545 9546 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given. 9547 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL) 9548 goto erret; 9549 9550 // Make sure later arguments are not found. 9551 ufunc->uf_args_visible = arg_idx; 9552 9553 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; 9554 r = compile_expr0(&arg, &cctx); 9555 9556 if (r == FAIL) 9557 goto erret; 9558 9559 // If no type specified use the type of the default value. 9560 // Otherwise check that the default value type matches the 9561 // specified type. 9562 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 9563 where.wt_index = arg_idx + 1; 9564 if (ufunc->uf_arg_types[arg_idx] == &t_unknown) 9565 { 9566 did_set_arg_type = TRUE; 9567 ufunc->uf_arg_types[arg_idx] = val_type; 9568 } 9569 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, 9570 TRUE, where) == FAIL) 9571 goto erret; 9572 9573 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) 9574 goto erret; 9575 9576 // set instruction index in JUMP_IF_ARG_SET to here 9577 isn = ((isn_T *)instr->ga_data) + jump_instr_idx; 9578 isn->isn_arg.jumparg.jump_where = instr->ga_len; 9579 } 9580 9581 if (did_set_arg_type) 9582 set_function_type(ufunc); 9583 } 9584 ufunc->uf_args_visible = ufunc->uf_args.ga_len; 9585 9586 /* 9587 * Loop over all the lines of the function and generate instructions. 9588 */ 9589 for (;;) 9590 { 9591 exarg_T ea; 9592 int starts_with_colon = FALSE; 9593 char_u *cmd; 9594 cmdmod_T local_cmdmod; 9595 9596 // Bail out on the first error to avoid a flood of errors and report 9597 // the right line number when inside try/catch. 9598 if (did_emsg_before != did_emsg) 9599 goto erret; 9600 9601 if (line != NULL && *line == '|') 9602 // the line continues after a '|' 9603 ++line; 9604 else if (line != NULL && *skipwhite(line) != NUL 9605 && !(*line == '#' && (line == cctx.ctx_line_start 9606 || VIM_ISWHITE(line[-1])))) 9607 { 9608 semsg(_(e_trailing_arg), line); 9609 goto erret; 9610 } 9611 else if (line != NULL && vim9_bad_comment(skipwhite(line))) 9612 goto erret; 9613 else 9614 { 9615 line = next_line_from_context(&cctx, FALSE); 9616 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) 9617 { 9618 // beyond the last line 9619 #ifdef FEAT_PROFILE 9620 if (cctx.ctx_skip != SKIP_YES) 9621 may_generate_prof_end(&cctx, prof_lnum); 9622 #endif 9623 break; 9624 } 9625 // Make a copy, splitting off nextcmd and removing trailing spaces 9626 // may change it. 9627 if (line != NULL) 9628 { 9629 line = vim_strsave(line); 9630 vim_free(line_to_free); 9631 line_to_free = line; 9632 } 9633 } 9634 9635 CLEAR_FIELD(ea); 9636 ea.cmdlinep = &line; 9637 ea.cmd = skipwhite(line); 9638 9639 if (*ea.cmd == '#') 9640 { 9641 // "#" starts a comment 9642 line = (char_u *)""; 9643 continue; 9644 } 9645 9646 #ifdef FEAT_PROFILE 9647 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum 9648 && cctx.ctx_skip != SKIP_YES) 9649 { 9650 may_generate_prof_end(&cctx, prof_lnum); 9651 9652 prof_lnum = cctx.ctx_lnum; 9653 generate_instr(&cctx, ISN_PROF_START); 9654 } 9655 #endif 9656 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum 9657 && cctx.ctx_skip != SKIP_YES) 9658 { 9659 debug_lnum = cctx.ctx_lnum; 9660 generate_instr_debug(&cctx); 9661 } 9662 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1; 9663 9664 // Some things can be recognized by the first character. 9665 switch (*ea.cmd) 9666 { 9667 case '}': 9668 { 9669 // "}" ends a block scope 9670 scopetype_T stype = cctx.ctx_scope == NULL 9671 ? NO_SCOPE : cctx.ctx_scope->se_type; 9672 9673 if (stype == BLOCK_SCOPE) 9674 { 9675 compile_endblock(&cctx); 9676 line = ea.cmd; 9677 } 9678 else 9679 { 9680 emsg(_(e_using_rcurly_outside_if_block_scope)); 9681 goto erret; 9682 } 9683 if (line != NULL) 9684 line = skipwhite(ea.cmd + 1); 9685 continue; 9686 } 9687 9688 case '{': 9689 // "{" starts a block scope 9690 // "{'a': 1}->func() is something else 9691 if (ends_excmd(*skipwhite(ea.cmd + 1))) 9692 { 9693 line = compile_block(ea.cmd, &cctx); 9694 continue; 9695 } 9696 break; 9697 } 9698 9699 /* 9700 * COMMAND MODIFIERS 9701 */ 9702 cctx.ctx_has_cmdmod = FALSE; 9703 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE) 9704 == FAIL) 9705 { 9706 if (errormsg != NULL) 9707 goto erret; 9708 // empty line or comment 9709 line = (char_u *)""; 9710 continue; 9711 } 9712 generate_cmdmods(&cctx, &local_cmdmod); 9713 undo_cmdmod(&local_cmdmod); 9714 9715 // Check if there was a colon after the last command modifier or before 9716 // the current position. 9717 for (p = ea.cmd; p >= line; --p) 9718 { 9719 if (*p == ':') 9720 starts_with_colon = TRUE; 9721 if (p < ea.cmd && !VIM_ISWHITE(*p)) 9722 break; 9723 } 9724 9725 // Skip ":call" to get to the function name, unless using :legacy 9726 p = ea.cmd; 9727 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)) 9728 { 9729 if (checkforcmd(&ea.cmd, "call", 3)) 9730 { 9731 if (*ea.cmd == '(') 9732 // not for "call()" 9733 ea.cmd = p; 9734 else 9735 ea.cmd = skipwhite(ea.cmd); 9736 } 9737 9738 if (!starts_with_colon) 9739 { 9740 int assign; 9741 9742 // Check for assignment after command modifiers. 9743 assign = may_compile_assignment(&ea, &line, &cctx); 9744 if (assign == OK) 9745 goto nextline; 9746 if (assign == FAIL) 9747 goto erret; 9748 } 9749 } 9750 9751 /* 9752 * COMMAND after range 9753 * 'text'->func() should not be confused with 'a mark 9754 * "++nr" and "--nr" are eval commands 9755 * in "$ENV->func()" the "$" is not a range 9756 */ 9757 cmd = ea.cmd; 9758 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY) 9759 && (*cmd != '$' || starts_with_colon) 9760 && (starts_with_colon || !(*cmd == '\'' 9761 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-'))))) 9762 { 9763 ea.cmd = skip_range(ea.cmd, TRUE, NULL); 9764 if (ea.cmd > cmd) 9765 { 9766 if (!starts_with_colon) 9767 { 9768 semsg(_(e_colon_required_before_range_str), cmd); 9769 goto erret; 9770 } 9771 ea.addr_count = 1; 9772 if (ends_excmd2(line, ea.cmd)) 9773 { 9774 // A range without a command: jump to the line. 9775 // TODO: compile to a more efficient command, possibly 9776 // calling parse_cmd_address(). 9777 ea.cmdidx = CMD_SIZE; 9778 line = compile_exec(line, &ea, &cctx); 9779 goto nextline; 9780 } 9781 } 9782 } 9783 p = find_ex_command(&ea, NULL, 9784 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY) 9785 ? NULL : item_exists, &cctx); 9786 9787 if (p == NULL) 9788 { 9789 if (cctx.ctx_skip != SKIP_YES) 9790 emsg(_(e_ambiguous_use_of_user_defined_command)); 9791 goto erret; 9792 } 9793 9794 // When using ":legacy cmd" always use compile_exec(). 9795 if (local_cmdmod.cmod_flags & CMOD_LEGACY) 9796 { 9797 char_u *start = ea.cmd; 9798 9799 switch (ea.cmdidx) 9800 { 9801 case CMD_if: 9802 case CMD_elseif: 9803 case CMD_else: 9804 case CMD_endif: 9805 case CMD_for: 9806 case CMD_endfor: 9807 case CMD_continue: 9808 case CMD_break: 9809 case CMD_while: 9810 case CMD_endwhile: 9811 case CMD_try: 9812 case CMD_catch: 9813 case CMD_finally: 9814 case CMD_endtry: 9815 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd); 9816 goto erret; 9817 default: break; 9818 } 9819 9820 // ":legacy return expr" needs to be handled differently. 9821 if (checkforcmd(&start, "return", 4)) 9822 ea.cmdidx = CMD_return; 9823 else 9824 ea.cmdidx = CMD_legacy; 9825 } 9826 9827 if (p == ea.cmd && ea.cmdidx != CMD_SIZE) 9828 { 9829 if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval) 9830 { 9831 line += STRLEN(line); 9832 goto nextline; 9833 } 9834 else if (ea.cmdidx != CMD_eval) 9835 { 9836 // CMD_var cannot happen, compile_assignment() above would be 9837 // used. Most likely an assignment to a non-existing variable. 9838 semsg(_(e_command_not_recognized_str), ea.cmd); 9839 goto erret; 9840 } 9841 } 9842 9843 if (cctx.ctx_had_return 9844 && ea.cmdidx != CMD_elseif 9845 && ea.cmdidx != CMD_else 9846 && ea.cmdidx != CMD_endif 9847 && ea.cmdidx != CMD_endfor 9848 && ea.cmdidx != CMD_endwhile 9849 && ea.cmdidx != CMD_catch 9850 && ea.cmdidx != CMD_finally 9851 && ea.cmdidx != CMD_endtry) 9852 { 9853 emsg(_(e_unreachable_code_after_return)); 9854 goto erret; 9855 } 9856 9857 p = skipwhite(p); 9858 if (ea.cmdidx != CMD_SIZE 9859 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read) 9860 { 9861 if (ea.cmdidx >= 0) 9862 ea.argt = excmd_get_argt(ea.cmdidx); 9863 if ((ea.argt & EX_BANG) && *p == '!') 9864 { 9865 ea.forceit = TRUE; 9866 p = skipwhite(p + 1); 9867 } 9868 } 9869 9870 switch (ea.cmdidx) 9871 { 9872 case CMD_def: 9873 ea.arg = p; 9874 line = compile_nested_function(&ea, &cctx); 9875 break; 9876 9877 case CMD_function: 9878 // TODO: should we allow this, e.g. to declare a global 9879 // function? 9880 emsg(_(e_cannot_use_function_inside_def)); 9881 goto erret; 9882 9883 case CMD_return: 9884 line = compile_return(p, check_return_type, 9885 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx); 9886 cctx.ctx_had_return = TRUE; 9887 break; 9888 9889 case CMD_let: 9890 emsg(_(e_cannot_use_let_in_vim9_script)); 9891 break; 9892 case CMD_var: 9893 case CMD_final: 9894 case CMD_const: 9895 case CMD_increment: 9896 case CMD_decrement: 9897 line = compile_assignment(p, &ea, ea.cmdidx, &cctx); 9898 if (line == p) 9899 line = NULL; 9900 break; 9901 9902 case CMD_unlet: 9903 case CMD_unlockvar: 9904 case CMD_lockvar: 9905 line = compile_unletlock(p, &ea, &cctx); 9906 break; 9907 9908 case CMD_import: 9909 emsg(_(e_import_can_only_be_used_in_script)); 9910 line = NULL; 9911 break; 9912 9913 case CMD_if: 9914 line = compile_if(p, &cctx); 9915 break; 9916 case CMD_elseif: 9917 line = compile_elseif(p, &cctx); 9918 cctx.ctx_had_return = FALSE; 9919 break; 9920 case CMD_else: 9921 line = compile_else(p, &cctx); 9922 cctx.ctx_had_return = FALSE; 9923 break; 9924 case CMD_endif: 9925 line = compile_endif(p, &cctx); 9926 break; 9927 9928 case CMD_while: 9929 line = compile_while(p, &cctx); 9930 break; 9931 case CMD_endwhile: 9932 line = compile_endwhile(p, &cctx); 9933 cctx.ctx_had_return = FALSE; 9934 break; 9935 9936 case CMD_for: 9937 line = compile_for(p, &cctx); 9938 break; 9939 case CMD_endfor: 9940 line = compile_endfor(p, &cctx); 9941 cctx.ctx_had_return = FALSE; 9942 break; 9943 case CMD_continue: 9944 line = compile_continue(p, &cctx); 9945 break; 9946 case CMD_break: 9947 line = compile_break(p, &cctx); 9948 break; 9949 9950 case CMD_try: 9951 line = compile_try(p, &cctx); 9952 break; 9953 case CMD_catch: 9954 line = compile_catch(p, &cctx); 9955 cctx.ctx_had_return = FALSE; 9956 break; 9957 case CMD_finally: 9958 line = compile_finally(p, &cctx); 9959 cctx.ctx_had_return = FALSE; 9960 break; 9961 case CMD_endtry: 9962 line = compile_endtry(p, &cctx); 9963 cctx.ctx_had_return = FALSE; 9964 break; 9965 case CMD_throw: 9966 line = compile_throw(p, &cctx); 9967 break; 9968 9969 case CMD_eval: 9970 line = compile_eval(p, &cctx); 9971 break; 9972 9973 case CMD_echo: 9974 case CMD_echon: 9975 case CMD_execute: 9976 case CMD_echomsg: 9977 case CMD_echoerr: 9978 case CMD_echoconsole: 9979 line = compile_mult_expr(p, ea.cmdidx, &cctx); 9980 break; 9981 9982 case CMD_put: 9983 ea.cmd = cmd; 9984 line = compile_put(p, &ea, &cctx); 9985 break; 9986 9987 case CMD_substitute: 9988 if (cctx.ctx_skip == SKIP_YES) 9989 line = (char_u *)""; 9990 else 9991 { 9992 ea.arg = p; 9993 line = compile_substitute(line, &ea, &cctx); 9994 } 9995 break; 9996 9997 case CMD_redir: 9998 ea.arg = p; 9999 line = compile_redir(line, &ea, &cctx); 10000 break; 10001 10002 case CMD_cexpr: 10003 case CMD_lexpr: 10004 case CMD_caddexpr: 10005 case CMD_laddexpr: 10006 case CMD_cgetexpr: 10007 case CMD_lgetexpr: 10008 #ifdef FEAT_QUICKFIX 10009 ea.arg = p; 10010 line = compile_cexpr(line, &ea, &cctx); 10011 #else 10012 ex_ni(&ea); 10013 line = NULL; 10014 #endif 10015 break; 10016 10017 case CMD_append: 10018 case CMD_change: 10019 case CMD_insert: 10020 case CMD_k: 10021 case CMD_t: 10022 case CMD_xit: 10023 not_in_vim9(&ea); 10024 goto erret; 10025 10026 case CMD_SIZE: 10027 if (cctx.ctx_skip != SKIP_YES) 10028 { 10029 semsg(_(e_invalid_command_str), ea.cmd); 10030 goto erret; 10031 } 10032 // We don't check for a next command here. 10033 line = (char_u *)""; 10034 break; 10035 10036 case CMD_lua: 10037 case CMD_mzscheme: 10038 case CMD_perl: 10039 case CMD_py3: 10040 case CMD_python3: 10041 case CMD_python: 10042 case CMD_pythonx: 10043 case CMD_ruby: 10044 case CMD_tcl: 10045 ea.arg = p; 10046 if (vim_strchr(line, '\n') == NULL) 10047 line = compile_exec(line, &ea, &cctx); 10048 else 10049 // heredoc lines have been concatenated with NL 10050 // characters in get_function_body() 10051 line = compile_script(line, &cctx); 10052 break; 10053 10054 default: 10055 // Not recognized, execute with do_cmdline_cmd(). 10056 ea.arg = p; 10057 line = compile_exec(line, &ea, &cctx); 10058 break; 10059 } 10060 nextline: 10061 if (line == NULL) 10062 goto erret; 10063 line = skipwhite(line); 10064 10065 // Undo any command modifiers. 10066 generate_undo_cmdmods(&cctx); 10067 10068 if (cctx.ctx_type_stack.ga_len < 0) 10069 { 10070 iemsg("Type stack underflow"); 10071 goto erret; 10072 } 10073 } 10074 10075 if (cctx.ctx_scope != NULL) 10076 { 10077 if (cctx.ctx_scope->se_type == IF_SCOPE) 10078 emsg(_(e_endif)); 10079 else if (cctx.ctx_scope->se_type == WHILE_SCOPE) 10080 emsg(_(e_endwhile)); 10081 else if (cctx.ctx_scope->se_type == FOR_SCOPE) 10082 emsg(_(e_endfor)); 10083 else 10084 emsg(_(e_missing_rcurly)); 10085 goto erret; 10086 } 10087 10088 if (!cctx.ctx_had_return) 10089 { 10090 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN) 10091 ufunc->uf_ret_type = &t_void; 10092 else if (ufunc->uf_ret_type->tt_type != VAR_VOID) 10093 { 10094 emsg(_(e_missing_return_statement)); 10095 goto erret; 10096 } 10097 10098 // Return void if there is no return at the end. 10099 generate_instr(&cctx, ISN_RETURN_VOID); 10100 } 10101 10102 // When compiled with ":silent!" and there was an error don't consider the 10103 // function compiled. 10104 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before) 10105 { 10106 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 10107 + ufunc->uf_dfunc_idx; 10108 dfunc->df_deleted = FALSE; 10109 dfunc->df_script_seq = current_sctx.sc_seq; 10110 #ifdef FEAT_PROFILE 10111 if (cctx.ctx_compile_type == CT_PROFILE) 10112 { 10113 dfunc->df_instr_prof = instr->ga_data; 10114 dfunc->df_instr_prof_count = instr->ga_len; 10115 } 10116 else 10117 #endif 10118 if (cctx.ctx_compile_type == CT_DEBUG) 10119 { 10120 dfunc->df_instr_debug = instr->ga_data; 10121 dfunc->df_instr_debug_count = instr->ga_len; 10122 } 10123 else 10124 { 10125 dfunc->df_instr = instr->ga_data; 10126 dfunc->df_instr_count = instr->ga_len; 10127 } 10128 dfunc->df_varcount = dfunc->df_var_names.ga_len; 10129 dfunc->df_has_closure = cctx.ctx_has_closure; 10130 if (cctx.ctx_outer_used) 10131 ufunc->uf_flags |= FC_CLOSURE; 10132 ufunc->uf_def_status = UF_COMPILED; 10133 } 10134 10135 ret = OK; 10136 10137 erret: 10138 if (ufunc->uf_def_status == UF_COMPILING) 10139 { 10140 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 10141 + ufunc->uf_dfunc_idx; 10142 10143 // Compiling aborted, free the generated instructions. 10144 clear_instr_ga(instr); 10145 VIM_CLEAR(dfunc->df_name); 10146 ga_clear_strings(&dfunc->df_var_names); 10147 10148 // If using the last entry in the table and it was added above, we 10149 // might as well remove it. 10150 if (!dfunc->df_deleted && new_def_function 10151 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) 10152 { 10153 --def_functions.ga_len; 10154 ufunc->uf_dfunc_idx = 0; 10155 } 10156 ufunc->uf_def_status = UF_COMPILE_ERROR; 10157 10158 while (cctx.ctx_scope != NULL) 10159 drop_scope(&cctx); 10160 10161 if (errormsg != NULL) 10162 emsg(errormsg); 10163 else if (did_emsg == did_emsg_before) 10164 emsg(_(e_compiling_def_function_failed)); 10165 } 10166 10167 if (cctx.ctx_redir_lhs.lhs_name != NULL) 10168 { 10169 if (ret == OK) 10170 { 10171 emsg(_(e_missing_redir_end)); 10172 ret = FAIL; 10173 } 10174 vim_free(cctx.ctx_redir_lhs.lhs_name); 10175 vim_free(cctx.ctx_redir_lhs.lhs_whole); 10176 } 10177 10178 current_sctx = save_current_sctx; 10179 estack_compiling = save_estack_compiling; 10180 cmdmod.cmod_flags = save_cmod_flags; 10181 if (do_estack_push) 10182 estack_pop(); 10183 10184 vim_free(line_to_free); 10185 free_imported(&cctx); 10186 free_locals(&cctx); 10187 ga_clear(&cctx.ctx_type_stack); 10188 return ret; 10189 } 10190 10191 void 10192 set_function_type(ufunc_T *ufunc) 10193 { 10194 int varargs = ufunc->uf_va_name != NULL; 10195 int argcount = ufunc->uf_args.ga_len; 10196 10197 // Create a type for the function, with the return type and any 10198 // argument types. 10199 // A vararg is included in uf_args.ga_len but not in uf_arg_types. 10200 // The type is included in "tt_args". 10201 if (argcount > 0 || varargs) 10202 { 10203 if (ufunc->uf_type_list.ga_itemsize == 0) 10204 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); 10205 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, 10206 argcount, &ufunc->uf_type_list); 10207 // Add argument types to the function type. 10208 if (func_type_add_arg_types(ufunc->uf_func_type, 10209 argcount + varargs, 10210 &ufunc->uf_type_list) == FAIL) 10211 return; 10212 ufunc->uf_func_type->tt_argcount = argcount + varargs; 10213 ufunc->uf_func_type->tt_min_argcount = 10214 argcount - ufunc->uf_def_args.ga_len; 10215 if (ufunc->uf_arg_types == NULL) 10216 { 10217 int i; 10218 10219 // lambda does not have argument types. 10220 for (i = 0; i < argcount; ++i) 10221 ufunc->uf_func_type->tt_args[i] = &t_any; 10222 } 10223 else 10224 mch_memmove(ufunc->uf_func_type->tt_args, 10225 ufunc->uf_arg_types, sizeof(type_T *) * argcount); 10226 if (varargs) 10227 { 10228 ufunc->uf_func_type->tt_args[argcount] = 10229 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type; 10230 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; 10231 } 10232 } 10233 else 10234 // No arguments, can use a predefined type. 10235 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, 10236 argcount, &ufunc->uf_type_list); 10237 } 10238 10239 10240 /* 10241 * Delete an instruction, free what it contains. 10242 */ 10243 void 10244 delete_instr(isn_T *isn) 10245 { 10246 switch (isn->isn_type) 10247 { 10248 case ISN_DEF: 10249 case ISN_EXEC: 10250 case ISN_EXEC_SPLIT: 10251 case ISN_LEGACY_EVAL: 10252 case ISN_LOADAUTO: 10253 case ISN_LOADB: 10254 case ISN_LOADENV: 10255 case ISN_LOADG: 10256 case ISN_LOADOPT: 10257 case ISN_LOADT: 10258 case ISN_LOADW: 10259 case ISN_LOCKUNLOCK: 10260 case ISN_PUSHEXC: 10261 case ISN_PUSHFUNC: 10262 case ISN_PUSHS: 10263 case ISN_RANGE: 10264 case ISN_STOREAUTO: 10265 case ISN_STOREB: 10266 case ISN_STOREENV: 10267 case ISN_STOREG: 10268 case ISN_STORET: 10269 case ISN_STOREW: 10270 case ISN_STRINGMEMBER: 10271 vim_free(isn->isn_arg.string); 10272 break; 10273 10274 case ISN_SUBSTITUTE: 10275 { 10276 int idx; 10277 isn_T *list = isn->isn_arg.subs.subs_instr; 10278 10279 vim_free(isn->isn_arg.subs.subs_cmd); 10280 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx) 10281 delete_instr(list + idx); 10282 vim_free(list); 10283 } 10284 break; 10285 10286 case ISN_INSTR: 10287 { 10288 int idx; 10289 isn_T *list = isn->isn_arg.instr; 10290 10291 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx) 10292 delete_instr(list + idx); 10293 vim_free(list); 10294 } 10295 break; 10296 10297 case ISN_LOADS: 10298 case ISN_STORES: 10299 vim_free(isn->isn_arg.loadstore.ls_name); 10300 break; 10301 10302 case ISN_UNLET: 10303 case ISN_UNLETENV: 10304 vim_free(isn->isn_arg.unlet.ul_name); 10305 break; 10306 10307 case ISN_STOREOPT: 10308 vim_free(isn->isn_arg.storeopt.so_name); 10309 break; 10310 10311 case ISN_PUSHBLOB: // push blob isn_arg.blob 10312 blob_unref(isn->isn_arg.blob); 10313 break; 10314 10315 case ISN_PUSHJOB: 10316 #ifdef FEAT_JOB_CHANNEL 10317 job_unref(isn->isn_arg.job); 10318 #endif 10319 break; 10320 10321 case ISN_PUSHCHANNEL: 10322 #ifdef FEAT_JOB_CHANNEL 10323 channel_unref(isn->isn_arg.channel); 10324 #endif 10325 break; 10326 10327 case ISN_UCALL: 10328 vim_free(isn->isn_arg.ufunc.cuf_name); 10329 break; 10330 10331 case ISN_FUNCREF: 10332 { 10333 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 10334 + isn->isn_arg.funcref.fr_func; 10335 ufunc_T *ufunc = dfunc->df_ufunc; 10336 10337 if (ufunc != NULL && func_name_refcount(ufunc->uf_name)) 10338 func_ptr_unref(ufunc); 10339 } 10340 break; 10341 10342 case ISN_DCALL: 10343 { 10344 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 10345 + isn->isn_arg.dfunc.cdf_idx; 10346 10347 if (dfunc->df_ufunc != NULL 10348 && func_name_refcount(dfunc->df_ufunc->uf_name)) 10349 func_ptr_unref(dfunc->df_ufunc); 10350 } 10351 break; 10352 10353 case ISN_NEWFUNC: 10354 { 10355 char_u *lambda = isn->isn_arg.newfunc.nf_lambda; 10356 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL); 10357 10358 if (ufunc != NULL) 10359 { 10360 unlink_def_function(ufunc); 10361 func_ptr_unref(ufunc); 10362 } 10363 10364 vim_free(lambda); 10365 vim_free(isn->isn_arg.newfunc.nf_global); 10366 } 10367 break; 10368 10369 case ISN_CHECKTYPE: 10370 case ISN_SETTYPE: 10371 free_type(isn->isn_arg.type.ct_type); 10372 break; 10373 10374 case ISN_CMDMOD: 10375 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod 10376 ->cmod_filter_regmatch.regprog); 10377 vim_free(isn->isn_arg.cmdmod.cf_cmdmod); 10378 break; 10379 10380 case ISN_LOADSCRIPT: 10381 case ISN_STORESCRIPT: 10382 vim_free(isn->isn_arg.script.scriptref); 10383 break; 10384 10385 case ISN_TRY: 10386 vim_free(isn->isn_arg.try.try_ref); 10387 break; 10388 10389 case ISN_CEXPR_CORE: 10390 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline); 10391 vim_free(isn->isn_arg.cexpr.cexpr_ref); 10392 break; 10393 10394 case ISN_2BOOL: 10395 case ISN_2STRING: 10396 case ISN_2STRING_ANY: 10397 case ISN_ADDBLOB: 10398 case ISN_ADDLIST: 10399 case ISN_ANYINDEX: 10400 case ISN_ANYSLICE: 10401 case ISN_BCALL: 10402 case ISN_BLOBAPPEND: 10403 case ISN_BLOBINDEX: 10404 case ISN_BLOBSLICE: 10405 case ISN_CATCH: 10406 case ISN_CEXPR_AUCMD: 10407 case ISN_CHECKLEN: 10408 case ISN_CHECKNR: 10409 case ISN_CMDMOD_REV: 10410 case ISN_COMPAREANY: 10411 case ISN_COMPAREBLOB: 10412 case ISN_COMPAREBOOL: 10413 case ISN_COMPAREDICT: 10414 case ISN_COMPAREFLOAT: 10415 case ISN_COMPAREFUNC: 10416 case ISN_COMPARELIST: 10417 case ISN_COMPARENR: 10418 case ISN_COMPARESPECIAL: 10419 case ISN_COMPARESTRING: 10420 case ISN_CONCAT: 10421 case ISN_COND2BOOL: 10422 case ISN_DEBUG: 10423 case ISN_DROP: 10424 case ISN_ECHO: 10425 case ISN_ECHOCONSOLE: 10426 case ISN_ECHOERR: 10427 case ISN_ECHOMSG: 10428 case ISN_ENDTRY: 10429 case ISN_EXECCONCAT: 10430 case ISN_EXECUTE: 10431 case ISN_FINALLY: 10432 case ISN_FINISH: 10433 case ISN_FOR: 10434 case ISN_GETITEM: 10435 case ISN_JUMP: 10436 case ISN_JUMP_IF_ARG_SET: 10437 case ISN_LISTAPPEND: 10438 case ISN_LISTINDEX: 10439 case ISN_LISTSLICE: 10440 case ISN_LOAD: 10441 case ISN_LOADBDICT: 10442 case ISN_LOADGDICT: 10443 case ISN_LOADOUTER: 10444 case ISN_LOADREG: 10445 case ISN_LOADTDICT: 10446 case ISN_LOADV: 10447 case ISN_LOADWDICT: 10448 case ISN_LOCKCONST: 10449 case ISN_MEMBER: 10450 case ISN_NEGATENR: 10451 case ISN_NEWDICT: 10452 case ISN_NEWLIST: 10453 case ISN_OPANY: 10454 case ISN_OPFLOAT: 10455 case ISN_OPNR: 10456 case ISN_PCALL: 10457 case ISN_PCALL_END: 10458 case ISN_PROF_END: 10459 case ISN_PROF_START: 10460 case ISN_PUSHBOOL: 10461 case ISN_PUSHF: 10462 case ISN_PUSHNR: 10463 case ISN_PUSHSPEC: 10464 case ISN_PUT: 10465 case ISN_REDIREND: 10466 case ISN_REDIRSTART: 10467 case ISN_RETURN: 10468 case ISN_RETURN_VOID: 10469 case ISN_SHUFFLE: 10470 case ISN_SLICE: 10471 case ISN_STORE: 10472 case ISN_STOREINDEX: 10473 case ISN_STORENR: 10474 case ISN_STOREOUTER: 10475 case ISN_STORERANGE: 10476 case ISN_STOREREG: 10477 case ISN_STOREV: 10478 case ISN_STRINDEX: 10479 case ISN_STRSLICE: 10480 case ISN_THROW: 10481 case ISN_TRYCONT: 10482 case ISN_UNLETINDEX: 10483 case ISN_UNLETRANGE: 10484 case ISN_UNPACK: 10485 // nothing allocated 10486 break; 10487 } 10488 } 10489 10490 /* 10491 * Free all instructions for "dfunc" except df_name. 10492 */ 10493 static void 10494 delete_def_function_contents(dfunc_T *dfunc, int mark_deleted) 10495 { 10496 int idx; 10497 10498 ga_clear(&dfunc->df_def_args_isn); 10499 ga_clear_strings(&dfunc->df_var_names); 10500 10501 if (dfunc->df_instr != NULL) 10502 { 10503 for (idx = 0; idx < dfunc->df_instr_count; ++idx) 10504 delete_instr(dfunc->df_instr + idx); 10505 VIM_CLEAR(dfunc->df_instr); 10506 dfunc->df_instr = NULL; 10507 } 10508 if (dfunc->df_instr_debug != NULL) 10509 { 10510 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx) 10511 delete_instr(dfunc->df_instr_debug + idx); 10512 VIM_CLEAR(dfunc->df_instr_debug); 10513 dfunc->df_instr_debug = NULL; 10514 } 10515 #ifdef FEAT_PROFILE 10516 if (dfunc->df_instr_prof != NULL) 10517 { 10518 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx) 10519 delete_instr(dfunc->df_instr_prof + idx); 10520 VIM_CLEAR(dfunc->df_instr_prof); 10521 dfunc->df_instr_prof = NULL; 10522 } 10523 #endif 10524 10525 if (mark_deleted) 10526 dfunc->df_deleted = TRUE; 10527 if (dfunc->df_ufunc != NULL) 10528 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED; 10529 } 10530 10531 /* 10532 * When a user function is deleted, clear the contents of any associated def 10533 * function, unless another user function still uses it. 10534 * The position in def_functions can be re-used. 10535 */ 10536 void 10537 unlink_def_function(ufunc_T *ufunc) 10538 { 10539 if (ufunc->uf_dfunc_idx > 0) 10540 { 10541 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 10542 + ufunc->uf_dfunc_idx; 10543 10544 if (--dfunc->df_refcount <= 0) 10545 delete_def_function_contents(dfunc, TRUE); 10546 ufunc->uf_def_status = UF_NOT_COMPILED; 10547 ufunc->uf_dfunc_idx = 0; 10548 if (dfunc->df_ufunc == ufunc) 10549 dfunc->df_ufunc = NULL; 10550 } 10551 } 10552 10553 /* 10554 * Used when a user function refers to an existing dfunc. 10555 */ 10556 void 10557 link_def_function(ufunc_T *ufunc) 10558 { 10559 if (ufunc->uf_dfunc_idx > 0) 10560 { 10561 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 10562 + ufunc->uf_dfunc_idx; 10563 10564 ++dfunc->df_refcount; 10565 } 10566 } 10567 10568 #if defined(EXITFREE) || defined(PROTO) 10569 /* 10570 * Free all functions defined with ":def". 10571 */ 10572 void 10573 free_def_functions(void) 10574 { 10575 int idx; 10576 10577 for (idx = 0; idx < def_functions.ga_len; ++idx) 10578 { 10579 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; 10580 10581 delete_def_function_contents(dfunc, TRUE); 10582 vim_free(dfunc->df_name); 10583 } 10584 10585 ga_clear(&def_functions); 10586 } 10587 #endif 10588 10589 10590 #endif // FEAT_EVAL 10591