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