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