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