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