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