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