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