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