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