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