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