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