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