Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 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 | * vim9cmds.c: Dealing with commands of a compiled function |
| 12 | */ |
| 13 | |
| 14 | #define USING_FLOAT_STUFF |
| 15 | #include "vim.h" |
| 16 | |
| 17 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 18 | |
| 19 | // When not generating protos this is included in proto.h |
| 20 | #ifdef PROTO |
| 21 | # include "vim9.h" |
| 22 | #endif |
| 23 | |
| 24 | /* |
| 25 | * Get the index of the current instruction. |
| 26 | * This compensates for a preceding ISN_CMDMOD and ISN_PROF_START. |
| 27 | */ |
| 28 | static int |
| 29 | current_instr_idx(cctx_T *cctx) |
| 30 | { |
| 31 | garray_T *instr = &cctx->ctx_instr; |
| 32 | int idx = instr->ga_len; |
| 33 | |
| 34 | while (idx > 0) |
| 35 | { |
| 36 | if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1] |
| 37 | .isn_type == ISN_CMDMOD) |
| 38 | { |
| 39 | --idx; |
| 40 | continue; |
| 41 | } |
| 42 | #ifdef FEAT_PROFILE |
| 43 | if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START) |
| 44 | { |
| 45 | --idx; |
| 46 | continue; |
| 47 | } |
| 48 | #endif |
| 49 | if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG) |
| 50 | { |
| 51 | --idx; |
| 52 | continue; |
| 53 | } |
| 54 | break; |
| 55 | } |
| 56 | return idx; |
| 57 | } |
| 58 | /* |
| 59 | * Remove local variables above "new_top". |
Bram Moolenaar | a275f2c | 2022-10-11 20:04:09 +0100 | [diff] [blame] | 60 | * Do this by clearing the name. If "keep" is TRUE do not reset the length, a |
| 61 | * closure may still need location of the variable. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 62 | */ |
| 63 | static void |
Bram Moolenaar | a275f2c | 2022-10-11 20:04:09 +0100 | [diff] [blame] | 64 | unwind_locals(cctx_T *cctx, int new_top, int keep) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 65 | { |
| 66 | if (cctx->ctx_locals.ga_len > new_top) |
Bram Moolenaar | a275f2c | 2022-10-11 20:04:09 +0100 | [diff] [blame] | 67 | for (int idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 68 | { |
Bram Moolenaar | a275f2c | 2022-10-11 20:04:09 +0100 | [diff] [blame] | 69 | lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 70 | VIM_CLEAR(lvar->lv_name); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 71 | } |
Bram Moolenaar | a275f2c | 2022-10-11 20:04:09 +0100 | [diff] [blame] | 72 | if (!keep) |
| 73 | cctx->ctx_locals.ga_len = new_top; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Free all local variables. |
| 78 | */ |
| 79 | void |
| 80 | free_locals(cctx_T *cctx) |
| 81 | { |
Bram Moolenaar | a275f2c | 2022-10-11 20:04:09 +0100 | [diff] [blame] | 82 | unwind_locals(cctx, 0, FALSE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 83 | ga_clear(&cctx->ctx_locals); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /* |
| 88 | * Check if "name" can be "unlet". |
| 89 | */ |
| 90 | int |
| 91 | check_vim9_unlet(char_u *name) |
| 92 | { |
Bram Moolenaar | dbdd16b | 2022-08-14 21:46:07 +0100 | [diff] [blame] | 93 | if (*name == NUL) |
| 94 | { |
| 95 | semsg(_(e_argument_required_for_str), "unlet"); |
| 96 | return FAIL; |
| 97 | } |
| 98 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 99 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 100 | { |
| 101 | // "unlet s:var" is allowed in legacy script. |
| 102 | if (*name == 's' && !script_is_vim9()) |
| 103 | return OK; |
| 104 | semsg(_(e_cannot_unlet_str), name); |
| 105 | return FAIL; |
| 106 | } |
| 107 | return OK; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Callback passed to ex_unletlock(). |
| 112 | */ |
| 113 | static int |
| 114 | compile_unlet( |
| 115 | lval_T *lvp, |
| 116 | char_u *name_end, |
| 117 | exarg_T *eap, |
| 118 | int deep UNUSED, |
| 119 | void *coookie) |
| 120 | { |
| 121 | cctx_T *cctx = coookie; |
| 122 | char_u *p = lvp->ll_name; |
| 123 | int cc = *name_end; |
| 124 | int ret = OK; |
| 125 | |
| 126 | if (cctx->ctx_skip == SKIP_YES) |
| 127 | return OK; |
| 128 | |
| 129 | *name_end = NUL; |
| 130 | if (*p == '$') |
| 131 | { |
| 132 | // :unlet $ENV_VAR |
| 133 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 134 | } |
| 135 | else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL) |
| 136 | { |
| 137 | lhs_T lhs; |
| 138 | |
| 139 | // This is similar to assigning: lookup the list/dict, compile the |
| 140 | // idx/key. Then instead of storing the value unlet the item. |
| 141 | // unlet {list}[idx] |
| 142 | // unlet {dict}[key] dict.key |
| 143 | // |
| 144 | // Figure out the LHS type and other properties. |
| 145 | // |
Bram Moolenaar | 97f8c10 | 2022-04-02 19:43:57 +0100 | [diff] [blame] | 146 | ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, FALSE, 0, cctx); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 147 | |
Bram Moolenaar | 2ef01d9 | 2022-01-06 21:38:11 +0000 | [diff] [blame] | 148 | // Use the info in "lhs" to unlet the item at the index in the |
| 149 | // list or dict. |
| 150 | if (ret == OK) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 151 | { |
Bram Moolenaar | 2ef01d9 | 2022-01-06 21:38:11 +0000 | [diff] [blame] | 152 | if (!lhs.lhs_has_index) |
| 153 | { |
| 154 | semsg(_(e_cannot_unlet_imported_item_str), p); |
| 155 | ret = FAIL; |
| 156 | } |
| 157 | else |
| 158 | ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | vim_free(lhs.lhs_name); |
| 162 | } |
| 163 | else if (check_vim9_unlet(p) == FAIL) |
| 164 | { |
| 165 | ret = FAIL; |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 170 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
| 171 | } |
| 172 | |
| 173 | *name_end = cc; |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Callback passed to ex_unletlock(). |
| 179 | */ |
| 180 | static int |
| 181 | compile_lock_unlock( |
| 182 | lval_T *lvp, |
| 183 | char_u *name_end, |
| 184 | exarg_T *eap, |
Bram Moolenaar | 70c43d8 | 2022-01-26 21:01:15 +0000 | [diff] [blame] | 185 | int deep, |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 186 | void *coookie) |
| 187 | { |
| 188 | cctx_T *cctx = coookie; |
| 189 | int cc = *name_end; |
| 190 | char_u *p = lvp->ll_name; |
| 191 | int ret = OK; |
| 192 | size_t len; |
| 193 | char_u *buf; |
| 194 | isntype_T isn = ISN_EXEC; |
Bram Moolenaar | d1d8f6b | 2022-08-14 21:28:32 +0100 | [diff] [blame] | 195 | char *cmd = eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar"; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 196 | |
| 197 | if (cctx->ctx_skip == SKIP_YES) |
| 198 | return OK; |
| 199 | |
Bram Moolenaar | d1d8f6b | 2022-08-14 21:28:32 +0100 | [diff] [blame] | 200 | if (*p == NUL) |
| 201 | { |
| 202 | semsg(_(e_argument_required_for_str), cmd); |
| 203 | return FAIL; |
| 204 | } |
| 205 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 206 | // Cannot use :lockvar and :unlockvar on local variables. |
| 207 | if (p[1] != ':') |
| 208 | { |
| 209 | char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START); |
| 210 | |
| 211 | if (lookup_local(p, end - p, NULL, cctx) == OK) |
| 212 | { |
| 213 | char_u *s = p; |
| 214 | |
| 215 | if (*end != '.' && *end != '[') |
| 216 | { |
| 217 | emsg(_(e_cannot_lock_unlock_local_variable)); |
| 218 | return FAIL; |
| 219 | } |
| 220 | |
| 221 | // For "d.member" put the local variable on the stack, it will be |
| 222 | // passed to ex_lockvar() indirectly. |
| 223 | if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL) |
| 224 | return FAIL; |
| 225 | isn = ISN_LOCKUNLOCK; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // Checking is done at runtime. |
| 230 | *name_end = NUL; |
| 231 | len = name_end - p + 20; |
| 232 | buf = alloc(len); |
| 233 | if (buf == NULL) |
| 234 | ret = FAIL; |
| 235 | else |
| 236 | { |
Bram Moolenaar | e939f5e | 2022-01-26 21:32:59 +0000 | [diff] [blame] | 237 | if (deep < 0) |
| 238 | vim_snprintf((char *)buf, len, "%s! %s", cmd, p); |
| 239 | else |
| 240 | vim_snprintf((char *)buf, len, "%s %d %s", cmd, deep, p); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 241 | ret = generate_EXEC_copy(cctx, isn, buf); |
| 242 | |
| 243 | vim_free(buf); |
| 244 | *name_end = cc; |
| 245 | } |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * compile "unlet var", "lock var" and "unlock var" |
| 251 | * "arg" points to "var". |
| 252 | */ |
| 253 | char_u * |
| 254 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 255 | { |
Bram Moolenaar | 70c43d8 | 2022-01-26 21:01:15 +0000 | [diff] [blame] | 256 | int deep = 0; |
| 257 | char_u *p = arg; |
| 258 | |
| 259 | if (eap->cmdidx != CMD_unlet) |
| 260 | { |
| 261 | if (eap->forceit) |
| 262 | deep = -1; |
| 263 | else if (vim_isdigit(*p)) |
| 264 | { |
| 265 | deep = getdigits(&p); |
| 266 | p = skipwhite(p); |
| 267 | } |
| 268 | else |
| 269 | deep = 2; |
| 270 | } |
| 271 | |
| 272 | ex_unletlock(eap, p, deep, GLV_NO_AUTOLOAD | GLV_COMPILING, |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 273 | eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock, |
| 274 | cctx); |
| 275 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 276 | } |
| 277 | |
| 278 | /* |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 279 | * Generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 280 | * "funcref_idx" is used for JUMP_WHILE_FALSE |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 281 | */ |
| 282 | static int |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 283 | compile_jump_to_end( |
| 284 | endlabel_T **el, |
| 285 | jumpwhen_T when, |
| 286 | int funcref_idx, |
| 287 | cctx_T *cctx) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 288 | { |
| 289 | garray_T *instr = &cctx->ctx_instr; |
| 290 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 291 | |
| 292 | if (endlabel == NULL) |
| 293 | return FAIL; |
| 294 | endlabel->el_next = *el; |
| 295 | *el = endlabel; |
| 296 | endlabel->el_end_label = instr->ga_len; |
| 297 | |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 298 | if (when == JUMP_WHILE_FALSE) |
| 299 | generate_WHILE(cctx, funcref_idx); |
| 300 | else |
| 301 | generate_JUMP(cctx, when, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 302 | return OK; |
| 303 | } |
| 304 | |
| 305 | static void |
| 306 | compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx) |
| 307 | { |
| 308 | garray_T *instr = &cctx->ctx_instr; |
| 309 | |
| 310 | while (*el != NULL) |
| 311 | { |
| 312 | endlabel_T *cur = (*el); |
| 313 | isn_T *isn; |
| 314 | |
| 315 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 316 | isn->isn_arg.jump.jump_where = jump_where; |
| 317 | *el = cur->el_next; |
| 318 | vim_free(cur); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | static void |
| 323 | compile_free_jump_to_end(endlabel_T **el) |
| 324 | { |
| 325 | while (*el != NULL) |
| 326 | { |
| 327 | endlabel_T *cur = (*el); |
| 328 | |
| 329 | *el = cur->el_next; |
| 330 | vim_free(cur); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Create a new scope and set up the generic items. |
| 336 | */ |
| 337 | static scope_T * |
| 338 | new_scope(cctx_T *cctx, scopetype_T type) |
| 339 | { |
| 340 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 341 | |
| 342 | if (scope == NULL) |
| 343 | return NULL; |
| 344 | scope->se_outer = cctx->ctx_scope; |
| 345 | cctx->ctx_scope = scope; |
| 346 | scope->se_type = type; |
| 347 | scope->se_local_count = cctx->ctx_locals.ga_len; |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 348 | if (scope->se_outer != NULL) |
| 349 | scope->se_loop_depth = scope->se_outer->se_loop_depth; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 350 | return scope; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Free the current scope and go back to the outer scope. |
| 355 | */ |
| 356 | void |
| 357 | drop_scope(cctx_T *cctx) |
| 358 | { |
| 359 | scope_T *scope = cctx->ctx_scope; |
| 360 | |
| 361 | if (scope == NULL) |
| 362 | { |
| 363 | iemsg("calling drop_scope() without a scope"); |
| 364 | return; |
| 365 | } |
| 366 | cctx->ctx_scope = scope->se_outer; |
| 367 | switch (scope->se_type) |
| 368 | { |
| 369 | case IF_SCOPE: |
| 370 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 371 | case FOR_SCOPE: |
| 372 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 373 | case WHILE_SCOPE: |
| 374 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 375 | case TRY_SCOPE: |
| 376 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 377 | case NO_SCOPE: |
| 378 | case BLOCK_SCOPE: |
| 379 | break; |
| 380 | } |
| 381 | vim_free(scope); |
| 382 | } |
| 383 | |
| 384 | static int |
| 385 | misplaced_cmdmod(cctx_T *cctx) |
| 386 | { |
| 387 | garray_T *instr = &cctx->ctx_instr; |
| 388 | |
| 389 | if (cctx->ctx_has_cmdmod |
| 390 | && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type |
| 391 | == ISN_CMDMOD) |
| 392 | { |
| 393 | emsg(_(e_misplaced_command_modifier)); |
| 394 | return TRUE; |
| 395 | } |
| 396 | return FALSE; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * compile "if expr" |
| 401 | * |
| 402 | * "if expr" Produces instructions: |
| 403 | * EVAL expr Push result of "expr" |
| 404 | * JUMP_IF_FALSE end |
| 405 | * ... body ... |
| 406 | * end: |
| 407 | * |
| 408 | * "if expr | else" Produces instructions: |
| 409 | * EVAL expr Push result of "expr" |
| 410 | * JUMP_IF_FALSE else |
| 411 | * ... body ... |
| 412 | * JUMP_ALWAYS end |
| 413 | * else: |
| 414 | * ... body ... |
| 415 | * end: |
| 416 | * |
| 417 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 418 | * EVAL expr Push result of "expr" |
| 419 | * JUMP_IF_FALSE elseif |
| 420 | * ... body ... |
| 421 | * JUMP_ALWAYS end |
| 422 | * elseif: |
| 423 | * EVAL expr Push result of "expr" |
| 424 | * JUMP_IF_FALSE else |
| 425 | * ... body ... |
| 426 | * JUMP_ALWAYS end |
| 427 | * else: |
| 428 | * ... body ... |
| 429 | * end: |
| 430 | */ |
| 431 | char_u * |
| 432 | compile_if(char_u *arg, cctx_T *cctx) |
| 433 | { |
| 434 | char_u *p = arg; |
| 435 | garray_T *instr = &cctx->ctx_instr; |
| 436 | int instr_count = instr->ga_len; |
| 437 | scope_T *scope; |
| 438 | skip_T skip_save = cctx->ctx_skip; |
| 439 | ppconst_T ppconst; |
| 440 | |
| 441 | CLEAR_FIELD(ppconst); |
| 442 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
| 443 | { |
| 444 | clear_ppconst(&ppconst); |
| 445 | return NULL; |
| 446 | } |
| 447 | if (!ends_excmd2(arg, skipwhite(p))) |
| 448 | { |
Bram Moolenaar | 74409f6 | 2022-01-01 15:58:22 +0000 | [diff] [blame] | 449 | semsg(_(e_trailing_characters_str), p); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 450 | return NULL; |
| 451 | } |
| 452 | if (cctx->ctx_skip == SKIP_YES) |
| 453 | clear_ppconst(&ppconst); |
| 454 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
| 455 | { |
| 456 | int error = FALSE; |
| 457 | int v; |
| 458 | |
| 459 | // The expression results in a constant. |
| 460 | v = tv_get_bool_chk(&ppconst.pp_tv[0], &error); |
| 461 | clear_ppconst(&ppconst); |
| 462 | if (error) |
| 463 | return NULL; |
| 464 | cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES; |
| 465 | } |
| 466 | else |
| 467 | { |
| 468 | // Not a constant, generate instructions for the expression. |
| 469 | cctx->ctx_skip = SKIP_UNKNOWN; |
| 470 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 471 | return NULL; |
| 472 | if (bool_on_stack(cctx) == FAIL) |
| 473 | return NULL; |
| 474 | } |
| 475 | |
| 476 | // CMDMOD_REV must come before the jump |
| 477 | generate_undo_cmdmods(cctx); |
| 478 | |
| 479 | scope = new_scope(cctx, IF_SCOPE); |
| 480 | if (scope == NULL) |
| 481 | return NULL; |
| 482 | scope->se_skip_save = skip_save; |
| 483 | // "is_had_return" will be reset if any block does not end in :return |
| 484 | scope->se_u.se_if.is_had_return = TRUE; |
| 485 | |
| 486 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
| 487 | { |
| 488 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 489 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 490 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 491 | } |
| 492 | else |
| 493 | scope->se_u.se_if.is_if_label = -1; |
| 494 | |
| 495 | #ifdef FEAT_PROFILE |
| 496 | if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES |
| 497 | && skip_save != SKIP_YES) |
| 498 | { |
| 499 | // generated a profile start, need to generate a profile end, since it |
| 500 | // won't be done after returning |
| 501 | cctx->ctx_skip = SKIP_NOT; |
| 502 | generate_instr(cctx, ISN_PROF_END); |
| 503 | cctx->ctx_skip = SKIP_YES; |
| 504 | } |
| 505 | #endif |
| 506 | |
| 507 | return p; |
| 508 | } |
| 509 | |
| 510 | char_u * |
| 511 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 512 | { |
| 513 | char_u *p = arg; |
| 514 | garray_T *instr = &cctx->ctx_instr; |
| 515 | int instr_count; |
| 516 | isn_T *isn; |
| 517 | scope_T *scope = cctx->ctx_scope; |
| 518 | ppconst_T ppconst; |
| 519 | skip_T save_skip = cctx->ctx_skip; |
| 520 | |
| 521 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 522 | { |
| 523 | emsg(_(e_elseif_without_if)); |
| 524 | return NULL; |
| 525 | } |
Bram Moolenaar | a275f2c | 2022-10-11 20:04:09 +0100 | [diff] [blame] | 526 | unwind_locals(cctx, scope->se_local_count, TRUE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 527 | if (!cctx->ctx_had_return) |
| 528 | scope->se_u.se_if.is_had_return = FALSE; |
| 529 | |
| 530 | if (cctx->ctx_skip == SKIP_NOT) |
| 531 | { |
| 532 | // previous block was executed, this one and following will not |
| 533 | cctx->ctx_skip = SKIP_YES; |
| 534 | scope->se_u.se_if.is_seen_skip_not = TRUE; |
| 535 | } |
| 536 | if (scope->se_u.se_if.is_seen_skip_not) |
| 537 | { |
| 538 | // A previous block was executed, skip over expression and bail out. |
| 539 | // Do not count the "elseif" for profiling and cmdmod |
| 540 | instr->ga_len = current_instr_idx(cctx); |
| 541 | |
| 542 | skip_expr_cctx(&p, cctx); |
| 543 | return p; |
| 544 | } |
| 545 | |
| 546 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
| 547 | { |
| 548 | int moved_cmdmod = FALSE; |
| 549 | int saved_debug = FALSE; |
| 550 | isn_T debug_isn; |
| 551 | |
| 552 | // Move any CMDMOD instruction to after the jump |
| 553 | if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD) |
| 554 | { |
| 555 | if (GA_GROW_FAILS(instr, 1)) |
| 556 | return NULL; |
| 557 | ((isn_T *)instr->ga_data)[instr->ga_len] = |
| 558 | ((isn_T *)instr->ga_data)[instr->ga_len - 1]; |
| 559 | --instr->ga_len; |
| 560 | moved_cmdmod = TRUE; |
| 561 | } |
| 562 | |
| 563 | // Remove the already generated ISN_DEBUG, it is written below the |
| 564 | // ISN_FOR instruction. |
| 565 | if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0 |
| 566 | && ((isn_T *)instr->ga_data)[instr->ga_len - 1] |
| 567 | .isn_type == ISN_DEBUG) |
| 568 | { |
| 569 | --instr->ga_len; |
| 570 | debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len]; |
| 571 | saved_debug = TRUE; |
| 572 | } |
| 573 | |
| 574 | if (compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 575 | JUMP_ALWAYS, 0, cctx) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 576 | return NULL; |
| 577 | // previous "if" or "elseif" jumps here |
| 578 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 579 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 580 | |
| 581 | if (moved_cmdmod) |
| 582 | ++instr->ga_len; |
| 583 | |
| 584 | if (saved_debug) |
| 585 | { |
| 586 | // move the debug instruction here |
| 587 | if (GA_GROW_FAILS(instr, 1)) |
| 588 | return NULL; |
| 589 | ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn; |
| 590 | ++instr->ga_len; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | // compile "expr"; if we know it evaluates to FALSE skip the block |
| 595 | CLEAR_FIELD(ppconst); |
| 596 | if (cctx->ctx_skip == SKIP_YES) |
| 597 | { |
| 598 | cctx->ctx_skip = SKIP_UNKNOWN; |
| 599 | #ifdef FEAT_PROFILE |
| 600 | if (cctx->ctx_compile_type == CT_PROFILE) |
| 601 | // the previous block was skipped, need to profile this line |
| 602 | generate_instr(cctx, ISN_PROF_START); |
| 603 | #endif |
| 604 | if (cctx->ctx_compile_type == CT_DEBUG) |
| 605 | // the previous block was skipped, may want to debug this line |
| 606 | generate_instr_debug(cctx); |
| 607 | } |
| 608 | |
| 609 | instr_count = instr->ga_len; |
| 610 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
| 611 | { |
| 612 | clear_ppconst(&ppconst); |
| 613 | return NULL; |
| 614 | } |
| 615 | cctx->ctx_skip = save_skip; |
| 616 | if (!ends_excmd2(arg, skipwhite(p))) |
| 617 | { |
| 618 | clear_ppconst(&ppconst); |
Bram Moolenaar | 74409f6 | 2022-01-01 15:58:22 +0000 | [diff] [blame] | 619 | semsg(_(e_trailing_characters_str), p); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 620 | return NULL; |
| 621 | } |
| 622 | if (scope->se_skip_save == SKIP_YES) |
| 623 | clear_ppconst(&ppconst); |
| 624 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
| 625 | { |
| 626 | int error = FALSE; |
| 627 | int v; |
| 628 | |
| 629 | // The expression result is a constant. |
| 630 | v = tv_get_bool_chk(&ppconst.pp_tv[0], &error); |
| 631 | if (error) |
| 632 | { |
| 633 | clear_ppconst(&ppconst); |
| 634 | return NULL; |
| 635 | } |
| 636 | cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES; |
| 637 | clear_ppconst(&ppconst); |
| 638 | scope->se_u.se_if.is_if_label = -1; |
| 639 | } |
| 640 | else |
| 641 | { |
| 642 | // Not a constant, generate instructions for the expression. |
| 643 | cctx->ctx_skip = SKIP_UNKNOWN; |
| 644 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 645 | return NULL; |
| 646 | if (bool_on_stack(cctx) == FAIL) |
| 647 | return NULL; |
| 648 | |
| 649 | // CMDMOD_REV must come before the jump |
| 650 | generate_undo_cmdmods(cctx); |
| 651 | |
| 652 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 653 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 654 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 655 | } |
| 656 | |
| 657 | return p; |
| 658 | } |
| 659 | |
| 660 | char_u * |
| 661 | compile_else(char_u *arg, cctx_T *cctx) |
| 662 | { |
| 663 | char_u *p = arg; |
| 664 | garray_T *instr = &cctx->ctx_instr; |
| 665 | isn_T *isn; |
| 666 | scope_T *scope = cctx->ctx_scope; |
| 667 | |
| 668 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 669 | { |
| 670 | emsg(_(e_else_without_if)); |
| 671 | return NULL; |
| 672 | } |
Bram Moolenaar | a275f2c | 2022-10-11 20:04:09 +0100 | [diff] [blame] | 673 | unwind_locals(cctx, scope->se_local_count, TRUE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 674 | if (!cctx->ctx_had_return) |
| 675 | scope->se_u.se_if.is_had_return = FALSE; |
| 676 | scope->se_u.se_if.is_seen_else = TRUE; |
| 677 | |
| 678 | #ifdef FEAT_PROFILE |
| 679 | if (cctx->ctx_compile_type == CT_PROFILE) |
| 680 | { |
| 681 | if (cctx->ctx_skip == SKIP_NOT |
| 682 | && ((isn_T *)instr->ga_data)[instr->ga_len - 1] |
| 683 | .isn_type == ISN_PROF_START) |
| 684 | // the previous block was executed, do not count "else" for |
| 685 | // profiling |
| 686 | --instr->ga_len; |
| 687 | if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not) |
| 688 | { |
| 689 | // the previous block was not executed, this one will, do count the |
| 690 | // "else" for profiling |
| 691 | cctx->ctx_skip = SKIP_NOT; |
| 692 | generate_instr(cctx, ISN_PROF_END); |
| 693 | generate_instr(cctx, ISN_PROF_START); |
| 694 | cctx->ctx_skip = SKIP_YES; |
| 695 | } |
| 696 | } |
| 697 | #endif |
| 698 | |
| 699 | if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES) |
| 700 | { |
| 701 | // jump from previous block to the end, unless the else block is empty |
| 702 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
| 703 | { |
| 704 | if (!cctx->ctx_had_return |
| 705 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 706 | JUMP_ALWAYS, 0, cctx) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 707 | return NULL; |
| 708 | } |
| 709 | |
| 710 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
| 711 | { |
| 712 | if (scope->se_u.se_if.is_if_label >= 0) |
| 713 | { |
| 714 | // previous "if" or "elseif" jumps here |
| 715 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 716 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 717 | scope->se_u.se_if.is_if_label = -1; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
| 722 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 723 | } |
| 724 | |
| 725 | return p; |
| 726 | } |
| 727 | |
| 728 | char_u * |
| 729 | compile_endif(char_u *arg, cctx_T *cctx) |
| 730 | { |
| 731 | scope_T *scope = cctx->ctx_scope; |
| 732 | ifscope_T *ifscope; |
| 733 | garray_T *instr = &cctx->ctx_instr; |
| 734 | isn_T *isn; |
| 735 | |
| 736 | if (misplaced_cmdmod(cctx)) |
| 737 | return NULL; |
| 738 | |
| 739 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 740 | { |
| 741 | emsg(_(e_endif_without_if)); |
| 742 | return NULL; |
| 743 | } |
| 744 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | a275f2c | 2022-10-11 20:04:09 +0100 | [diff] [blame] | 745 | unwind_locals(cctx, scope->se_local_count, TRUE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 746 | if (!cctx->ctx_had_return) |
| 747 | ifscope->is_had_return = FALSE; |
| 748 | |
| 749 | if (scope->se_u.se_if.is_if_label >= 0) |
| 750 | { |
| 751 | // previous "if" or "elseif" jumps here |
| 752 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 753 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 754 | } |
| 755 | // Fill in the "end" label in jumps at the end of the blocks. |
| 756 | compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx); |
| 757 | |
| 758 | #ifdef FEAT_PROFILE |
| 759 | // even when skipping we count the endif as executed, unless the block it's |
| 760 | // in is skipped |
| 761 | if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES |
| 762 | && scope->se_skip_save != SKIP_YES) |
| 763 | { |
| 764 | cctx->ctx_skip = SKIP_NOT; |
| 765 | generate_instr(cctx, ISN_PROF_START); |
| 766 | } |
| 767 | #endif |
| 768 | cctx->ctx_skip = scope->se_skip_save; |
| 769 | |
| 770 | // If all the blocks end in :return and there is an :else then the |
| 771 | // had_return flag is set. |
| 772 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
| 773 | |
| 774 | drop_scope(cctx); |
| 775 | return arg; |
| 776 | } |
| 777 | |
| 778 | /* |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 779 | * Save the info needed for ENDLOOP. Used by :for and :while. |
| 780 | */ |
| 781 | static void |
| 782 | compile_fill_loop_info(loop_info_T *loop_info, int funcref_idx, cctx_T *cctx) |
| 783 | { |
| 784 | loop_info->li_funcref_idx = funcref_idx; |
| 785 | loop_info->li_local_count = cctx->ctx_locals.ga_len; |
| 786 | loop_info->li_closure_count = cctx->ctx_closure_count; |
| 787 | } |
| 788 | |
| 789 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 790 | * Compile "for var in expr": |
| 791 | * |
| 792 | * Produces instructions: |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 793 | * STORE -1 in loop-idx Set index to -1 |
| 794 | * EVAL expr Result of "expr" on top of stack |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 795 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 796 | * - if beyond end, jump to "end" |
| 797 | * - otherwise get item from list and push it |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 798 | * - store ec_funcrefs in var "loop-idx" + 1 |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 799 | * STORE var Store item in "var" |
| 800 | * ... body ... |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 801 | * ENDLOOP funcref-idx off count Only if closure uses local var |
| 802 | * JUMP top Jump back to repeat |
| 803 | * end: DROP Drop the result of "expr" |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 804 | * |
| 805 | * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var": |
| 806 | * UNPACK 2 Split item in 2 |
| 807 | * STORE var1 Store item in "var1" |
| 808 | * STORE var2 Store item in "var2" |
| 809 | */ |
| 810 | char_u * |
| 811 | compile_for(char_u *arg_start, cctx_T *cctx) |
| 812 | { |
| 813 | char_u *arg; |
| 814 | char_u *arg_end; |
| 815 | char_u *name = NULL; |
| 816 | char_u *p; |
| 817 | char_u *wp; |
| 818 | int var_count = 0; |
| 819 | int var_list = FALSE; |
| 820 | int semicolon = FALSE; |
| 821 | size_t varlen; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 822 | garray_T *instr = &cctx->ctx_instr; |
| 823 | scope_T *scope; |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 824 | forscope_T *forscope; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 825 | lvar_T *loop_lvar; // loop iteration variable |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 826 | int loop_lvar_idx; |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 827 | lvar_T *funcref_lvar; |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 828 | int funcref_lvar_idx; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 829 | lvar_T *var_lvar; // variable for "var" |
| 830 | type_T *vartype; |
| 831 | type_T *item_type = &t_any; |
| 832 | int idx; |
| 833 | int prev_lnum = cctx->ctx_prev_lnum; |
| 834 | |
| 835 | p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE); |
| 836 | if (p == NULL) |
| 837 | return NULL; |
| 838 | if (var_count == 0) |
| 839 | var_count = 1; |
| 840 | else |
| 841 | var_list = TRUE; // can also be a list of one variable |
| 842 | |
| 843 | // consume "in" |
| 844 | wp = p; |
| 845 | if (may_get_next_line_error(wp, &p, cctx) == FAIL) |
| 846 | return NULL; |
| 847 | if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2])) |
| 848 | { |
| 849 | if (*p == ':' && wp != p) |
| 850 | semsg(_(e_no_white_space_allowed_before_colon_str), p); |
| 851 | else |
Bram Moolenaar | 3a846e6 | 2022-01-01 16:21:00 +0000 | [diff] [blame] | 852 | emsg(_(e_missing_in_after_for)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 853 | return NULL; |
| 854 | } |
| 855 | wp = p + 2; |
| 856 | if (may_get_next_line_error(wp, &p, cctx) == FAIL) |
| 857 | return NULL; |
| 858 | |
Bram Moolenaar | 2b4ecc2 | 2022-01-02 14:08:18 +0000 | [diff] [blame] | 859 | // Find the already generated ISN_DEBUG to get the line number for the |
| 860 | // instruction written below the ISN_FOR instruction. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 861 | if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0 |
| 862 | && ((isn_T *)instr->ga_data)[instr->ga_len - 1] |
| 863 | .isn_type == ISN_DEBUG) |
| 864 | { |
Bram Moolenaar | 2b4ecc2 | 2022-01-02 14:08:18 +0000 | [diff] [blame] | 865 | prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len - 1] |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 866 | .isn_arg.debug.dbg_break_lnum; |
| 867 | } |
| 868 | |
| 869 | scope = new_scope(cctx, FOR_SCOPE); |
| 870 | if (scope == NULL) |
| 871 | return NULL; |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 872 | if (scope->se_loop_depth == MAX_LOOP_DEPTH) |
| 873 | { |
| 874 | emsg(_(e_loop_nesting_too_deep)); |
| 875 | return NULL; |
| 876 | } |
| 877 | ++scope->se_loop_depth; |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 878 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 879 | |
| 880 | // Reserve a variable to store the loop iteration counter and initialize it |
| 881 | // to -1. |
Bram Moolenaar | 6586a01 | 2022-09-30 11:04:50 +0100 | [diff] [blame] | 882 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 883 | if (loop_lvar == NULL) |
| 884 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 885 | drop_scope(cctx); |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 886 | return NULL; // out of memory |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 887 | } |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 888 | // get the index before a following reserve_local() makes the lval invalid |
| 889 | loop_lvar_idx = loop_lvar->lv_idx; |
| 890 | generate_STORENR(cctx, loop_lvar_idx, -1); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 891 | |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 892 | // Reserve a variable to store ec_funcrefs.ga_len, used in ISN_ENDLOOP. |
| 893 | // The variable index is always the loop var index plus one. |
| 894 | // It is not used when no closures are encountered, we don't know yet. |
Bram Moolenaar | 6586a01 | 2022-09-30 11:04:50 +0100 | [diff] [blame] | 895 | funcref_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number); |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 896 | if (funcref_lvar == NULL) |
| 897 | { |
| 898 | drop_scope(cctx); |
| 899 | return NULL; // out of memory |
| 900 | } |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 901 | // get the index before a following reserve_local() makes the lval invalid |
| 902 | funcref_lvar_idx = funcref_lvar->lv_idx; |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 903 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 904 | // compile "expr", it remains on the stack until "endfor" |
| 905 | arg = p; |
| 906 | if (compile_expr0(&arg, cctx) == FAIL) |
| 907 | { |
| 908 | drop_scope(cctx); |
| 909 | return NULL; |
| 910 | } |
| 911 | arg_end = arg; |
| 912 | |
| 913 | if (cctx->ctx_skip != SKIP_YES) |
| 914 | { |
Dominique Pelle | af4a61a | 2021-12-27 17:21:41 +0000 | [diff] [blame] | 915 | // If we know the type of "var" and it is not a supported type we can |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 916 | // give an error now. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 917 | vartype = get_type_on_stack(cctx, 0); |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 918 | if (vartype->tt_type != VAR_LIST |
| 919 | && vartype->tt_type != VAR_STRING |
| 920 | && vartype->tt_type != VAR_BLOB |
| 921 | && vartype->tt_type != VAR_ANY |
| 922 | && vartype->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 923 | { |
| 924 | semsg(_(e_for_loop_on_str_not_supported), |
| 925 | vartype_name(vartype->tt_type)); |
| 926 | drop_scope(cctx); |
| 927 | return NULL; |
| 928 | } |
| 929 | |
| 930 | if (vartype->tt_type == VAR_STRING) |
| 931 | item_type = &t_string; |
| 932 | else if (vartype->tt_type == VAR_BLOB) |
| 933 | item_type = &t_number; |
| 934 | else if (vartype->tt_type == VAR_LIST |
| 935 | && vartype->tt_member->tt_type != VAR_ANY) |
| 936 | { |
| 937 | if (!var_list) |
| 938 | item_type = vartype->tt_member; |
| 939 | else if (vartype->tt_member->tt_type == VAR_LIST |
| 940 | && vartype->tt_member->tt_member->tt_type != VAR_ANY) |
| 941 | item_type = vartype->tt_member->tt_member; |
| 942 | } |
| 943 | |
| 944 | // CMDMOD_REV must come before the FOR instruction. |
| 945 | generate_undo_cmdmods(cctx); |
| 946 | |
| 947 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 948 | forscope->fs_top_label = current_instr_idx(cctx); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 949 | |
Bram Moolenaar | 2b4ecc2 | 2022-01-02 14:08:18 +0000 | [diff] [blame] | 950 | if (cctx->ctx_compile_type == CT_DEBUG) |
| 951 | { |
| 952 | int save_prev_lnum = cctx->ctx_prev_lnum; |
| 953 | isn_T *isn; |
| 954 | |
| 955 | // Add ISN_DEBUG here, before deciding to end the loop. There will |
| 956 | // be another ISN_DEBUG before the next instruction. |
| 957 | // Use the prev_lnum from the ISN_DEBUG instruction removed above. |
| 958 | // Increment the variable count so that the loop variable can be |
| 959 | // inspected. |
| 960 | cctx->ctx_prev_lnum = prev_lnum; |
| 961 | isn = generate_instr_debug(cctx); |
| 962 | ++isn->isn_arg.debug.dbg_var_names_len; |
| 963 | cctx->ctx_prev_lnum = save_prev_lnum; |
| 964 | } |
| 965 | |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 966 | generate_FOR(cctx, loop_lvar_idx); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 967 | |
| 968 | arg = arg_start; |
| 969 | if (var_list) |
| 970 | { |
| 971 | generate_UNPACK(cctx, var_count, semicolon); |
| 972 | arg = skipwhite(arg + 1); // skip white after '[' |
| 973 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 974 | // drop the list item |
| 975 | --cctx->ctx_type_stack.ga_len; |
| 976 | |
| 977 | // add type of the items |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 978 | for (idx = 0; idx < var_count; ++idx) |
| 979 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 980 | type_T *type = (semicolon && idx == 0) ? vartype : item_type; |
| 981 | |
| 982 | if (push_type_stack(cctx, type) == FAIL) |
| 983 | { |
| 984 | drop_scope(cctx); |
| 985 | return NULL; |
| 986 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
| 990 | for (idx = 0; idx < var_count; ++idx) |
| 991 | { |
| 992 | assign_dest_T dest = dest_local; |
| 993 | int opt_flags = 0; |
| 994 | int vimvaridx = -1; |
| 995 | type_T *type = &t_any; |
| 996 | type_T *lhs_type = &t_any; |
| 997 | where_T where = WHERE_INIT; |
| 998 | |
| 999 | p = skip_var_one(arg, FALSE); |
| 1000 | varlen = p - arg; |
| 1001 | name = vim_strnsave(arg, varlen); |
| 1002 | if (name == NULL) |
| 1003 | goto failed; |
Bram Moolenaar | ce93d16 | 2023-01-30 21:12:34 +0000 | [diff] [blame] | 1004 | if (*skipwhite(p) == ':') |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1005 | { |
Bram Moolenaar | ce93d16 | 2023-01-30 21:12:34 +0000 | [diff] [blame] | 1006 | if (VIM_ISWHITE(*p)) |
| 1007 | { |
| 1008 | semsg(_(e_no_white_space_allowed_before_colon_str), p); |
| 1009 | goto failed; |
| 1010 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1011 | p = skipwhite(p + 1); |
| 1012 | lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE); |
| 1013 | } |
| 1014 | |
| 1015 | if (get_var_dest(name, &dest, CMD_for, &opt_flags, |
| 1016 | &vimvaridx, &type, cctx) == FAIL) |
| 1017 | goto failed; |
| 1018 | if (dest != dest_local) |
| 1019 | { |
| 1020 | if (generate_store_var(cctx, dest, opt_flags, vimvaridx, |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 1021 | type, name, NULL) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1022 | goto failed; |
| 1023 | } |
| 1024 | else if (varlen == 1 && *arg == '_') |
| 1025 | { |
| 1026 | // Assigning to "_": drop the value. |
| 1027 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 1028 | goto failed; |
| 1029 | } |
| 1030 | else |
| 1031 | { |
| 1032 | // Script var is not supported. |
| 1033 | if (STRNCMP(name, "s:", 2) == 0) |
| 1034 | { |
| 1035 | emsg(_(e_cannot_use_script_variable_in_for_loop)); |
| 1036 | goto failed; |
| 1037 | } |
| 1038 | |
| 1039 | if (!valid_varname(arg, (int)varlen, FALSE)) |
| 1040 | goto failed; |
| 1041 | if (lookup_local(arg, varlen, NULL, cctx) == OK) |
| 1042 | { |
Bram Moolenaar | a9fa8c5 | 2023-01-02 18:10:04 +0000 | [diff] [blame] | 1043 | semsg(_(e_variable_already_declared_str), arg); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1044 | goto failed; |
| 1045 | } |
| 1046 | |
| 1047 | // Reserve a variable to store "var". |
| 1048 | where.wt_index = var_list ? idx + 1 : 0; |
| 1049 | where.wt_variable = TRUE; |
| 1050 | if (lhs_type == &t_any) |
| 1051 | lhs_type = item_type; |
| 1052 | else if (item_type != &t_unknown |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 1053 | && need_type_where(item_type, lhs_type, FALSE, -1, |
Bram Moolenaar | a1c5195 | 2022-02-02 16:20:26 +0000 | [diff] [blame] | 1054 | where, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1055 | goto failed; |
Bram Moolenaar | 159b2d5 | 2022-10-11 21:41:25 +0100 | [diff] [blame] | 1056 | var_lvar = reserve_local(cctx, arg, varlen, ASSIGN_FINAL, |
Bram Moolenaar | 6586a01 | 2022-09-30 11:04:50 +0100 | [diff] [blame] | 1057 | lhs_type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1058 | if (var_lvar == NULL) |
| 1059 | // out of memory or used as an argument |
| 1060 | goto failed; |
| 1061 | |
| 1062 | if (semicolon && idx == var_count - 1) |
| 1063 | var_lvar->lv_type = vartype; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1064 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
| 1065 | } |
| 1066 | |
| 1067 | if (*p == ',' || *p == ';') |
| 1068 | ++p; |
| 1069 | arg = skipwhite(p); |
| 1070 | vim_free(name); |
| 1071 | } |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1072 | |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 1073 | // remember the number of variables and closures, used for ENDLOOP |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1074 | compile_fill_loop_info(&forscope->fs_loop_info, funcref_lvar_idx, cctx); |
| 1075 | forscope->fs_loop_info.li_depth = scope->se_loop_depth - 1; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | return arg_end; |
| 1079 | |
| 1080 | failed: |
| 1081 | vim_free(name); |
| 1082 | drop_scope(cctx); |
| 1083 | return NULL; |
| 1084 | } |
| 1085 | |
| 1086 | /* |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 1087 | * Used when ending a loop of :for and :while: Generate an ISN_ENDLOOP |
| 1088 | * instruction if any variable was declared that could be used by a new |
| 1089 | * closure. |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1090 | */ |
| 1091 | static int |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 1092 | compile_loop_end(loop_info_T *loop_info, cctx_T *cctx) |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1093 | { |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 1094 | if (cctx->ctx_locals.ga_len > loop_info->li_local_count |
| 1095 | && cctx->ctx_closure_count > loop_info->li_closure_count) |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1096 | return generate_ENDLOOP(cctx, loop_info); |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1097 | return OK; |
| 1098 | } |
| 1099 | |
| 1100 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1101 | * compile "endfor" |
| 1102 | */ |
| 1103 | char_u * |
| 1104 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 1105 | { |
| 1106 | garray_T *instr = &cctx->ctx_instr; |
| 1107 | scope_T *scope = cctx->ctx_scope; |
| 1108 | forscope_T *forscope; |
| 1109 | isn_T *isn; |
| 1110 | |
| 1111 | if (misplaced_cmdmod(cctx)) |
| 1112 | return NULL; |
| 1113 | |
| 1114 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 1115 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1116 | emsg(_(e_endfor_without_for)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1117 | return NULL; |
| 1118 | } |
| 1119 | forscope = &scope->se_u.se_for; |
| 1120 | cctx->ctx_scope = scope->se_outer; |
| 1121 | if (cctx->ctx_skip != SKIP_YES) |
| 1122 | { |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1123 | // Handle the case that any local variables were declared that might be |
| 1124 | // used in a closure. |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 1125 | if (compile_loop_end(&forscope->fs_loop_info, cctx) == FAIL) |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1126 | return NULL; |
| 1127 | |
Bram Moolenaar | a275f2c | 2022-10-11 20:04:09 +0100 | [diff] [blame] | 1128 | unwind_locals(cctx, scope->se_local_count, FALSE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1129 | |
| 1130 | // At end of ":for" scope jump back to the FOR instruction. |
| 1131 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 1132 | |
| 1133 | // Fill in the "end" label in the FOR statement so it can jump here. |
Bram Moolenaar | 2b4ecc2 | 2022-01-02 14:08:18 +0000 | [diff] [blame] | 1134 | // In debug mode an ISN_DEBUG was inserted. |
| 1135 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label |
| 1136 | + (cctx->ctx_compile_type == CT_DEBUG ? 1 : 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1137 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 1138 | |
| 1139 | // Fill in the "end" label any BREAK statements |
| 1140 | compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx); |
| 1141 | |
| 1142 | // Below the ":for" scope drop the "expr" list from the stack. |
| 1143 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 1144 | return NULL; |
| 1145 | } |
| 1146 | |
| 1147 | vim_free(scope); |
| 1148 | |
| 1149 | return arg; |
| 1150 | } |
| 1151 | |
| 1152 | /* |
| 1153 | * compile "while expr" |
| 1154 | * |
| 1155 | * Produces instructions: |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1156 | * top: EVAL expr Push result of "expr" |
| 1157 | * WHILE funcref-idx end Jump if false |
| 1158 | * ... body ... |
| 1159 | * ENDLOOP funcref-idx off count only if closure uses local var |
| 1160 | * JUMP top Jump back to repeat |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1161 | * end: |
| 1162 | * |
| 1163 | */ |
| 1164 | char_u * |
| 1165 | compile_while(char_u *arg, cctx_T *cctx) |
| 1166 | { |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1167 | char_u *p = arg; |
| 1168 | scope_T *scope; |
| 1169 | whilescope_T *whilescope; |
| 1170 | lvar_T *funcref_lvar; |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1171 | int funcref_lvar_idx; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1172 | |
| 1173 | scope = new_scope(cctx, WHILE_SCOPE); |
| 1174 | if (scope == NULL) |
| 1175 | return NULL; |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1176 | if (scope->se_loop_depth == MAX_LOOP_DEPTH) |
| 1177 | { |
| 1178 | emsg(_(e_loop_nesting_too_deep)); |
| 1179 | return NULL; |
| 1180 | } |
| 1181 | ++scope->se_loop_depth; |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1182 | whilescope = &scope->se_u.se_while; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1183 | |
| 1184 | // "endwhile" jumps back here, one before when profiling or using cmdmods |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1185 | whilescope->ws_top_label = current_instr_idx(cctx); |
| 1186 | |
| 1187 | // Reserve a variable to store ec_funcrefs.ga_len, used in ISN_ENDLOOP. |
| 1188 | // It is not used when no closures are encountered, we don't know yet. |
Bram Moolenaar | 6586a01 | 2022-09-30 11:04:50 +0100 | [diff] [blame] | 1189 | funcref_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number); |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1190 | if (funcref_lvar == NULL) |
| 1191 | { |
| 1192 | drop_scope(cctx); |
| 1193 | return NULL; // out of memory |
| 1194 | } |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1195 | // get the index before a following reserve_local() makes the lval invalid |
| 1196 | funcref_lvar_idx = funcref_lvar->lv_idx; |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 1197 | |
| 1198 | // remember the number of variables and closures, used for ENDLOOP |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1199 | compile_fill_loop_info(&whilescope->ws_loop_info, funcref_lvar_idx, cctx); |
| 1200 | whilescope->ws_loop_info.li_depth = scope->se_loop_depth - 1; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1201 | |
| 1202 | // compile "expr" |
| 1203 | if (compile_expr0(&p, cctx) == FAIL) |
| 1204 | return NULL; |
| 1205 | |
| 1206 | if (!ends_excmd2(arg, skipwhite(p))) |
| 1207 | { |
Bram Moolenaar | 74409f6 | 2022-01-01 15:58:22 +0000 | [diff] [blame] | 1208 | semsg(_(e_trailing_characters_str), p); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1209 | return NULL; |
| 1210 | } |
| 1211 | |
| 1212 | if (cctx->ctx_skip != SKIP_YES) |
| 1213 | { |
| 1214 | if (bool_on_stack(cctx) == FAIL) |
| 1215 | return FAIL; |
| 1216 | |
| 1217 | // CMDMOD_REV must come before the jump |
| 1218 | generate_undo_cmdmods(cctx); |
| 1219 | |
| 1220 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1221 | if (compile_jump_to_end(&whilescope->ws_end_label, |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1222 | JUMP_WHILE_FALSE, funcref_lvar_idx, cctx) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1223 | return FAIL; |
| 1224 | } |
| 1225 | |
| 1226 | return p; |
| 1227 | } |
| 1228 | |
| 1229 | /* |
| 1230 | * compile "endwhile" |
| 1231 | */ |
| 1232 | char_u * |
| 1233 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 1234 | { |
| 1235 | scope_T *scope = cctx->ctx_scope; |
| 1236 | garray_T *instr = &cctx->ctx_instr; |
| 1237 | |
| 1238 | if (misplaced_cmdmod(cctx)) |
| 1239 | return NULL; |
| 1240 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 1241 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1242 | emsg(_(e_endwhile_without_while)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1243 | return NULL; |
| 1244 | } |
| 1245 | cctx->ctx_scope = scope->se_outer; |
| 1246 | if (cctx->ctx_skip != SKIP_YES) |
| 1247 | { |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1248 | whilescope_T *whilescope = &scope->se_u.se_while; |
| 1249 | |
| 1250 | // Handle the case that any local variables were declared that might be |
| 1251 | // used in a closure. |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 1252 | if (compile_loop_end(&whilescope->ws_loop_info, cctx) == FAIL) |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1253 | return NULL; |
| 1254 | |
Bram Moolenaar | a275f2c | 2022-10-11 20:04:09 +0100 | [diff] [blame] | 1255 | unwind_locals(cctx, scope->se_local_count, FALSE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1256 | |
| 1257 | #ifdef FEAT_PROFILE |
| 1258 | // count the endwhile before jumping |
| 1259 | may_generate_prof_end(cctx, cctx->ctx_lnum); |
| 1260 | #endif |
| 1261 | |
| 1262 | // At end of ":for" scope jump back to the FOR instruction. |
| 1263 | generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label); |
| 1264 | |
| 1265 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 1266 | // And in any jumps for ":break" |
| 1267 | compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, |
| 1268 | instr->ga_len, cctx); |
| 1269 | } |
| 1270 | |
| 1271 | vim_free(scope); |
| 1272 | |
| 1273 | return arg; |
| 1274 | } |
| 1275 | |
| 1276 | /* |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1277 | * Get the current information about variables declared inside a loop. |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1278 | * Returns TRUE if there are any and fills "lvi". |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1279 | */ |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1280 | int |
| 1281 | get_loop_var_info(cctx_T *cctx, loopvarinfo_T *lvi) |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1282 | { |
| 1283 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1284 | int prev_local_count = 0; |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1285 | |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1286 | CLEAR_POINTER(lvi); |
| 1287 | for (;;) |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1288 | { |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1289 | loop_info_T *loopinfo; |
| 1290 | int cur_local_last; |
| 1291 | int start_local_count; |
| 1292 | |
| 1293 | while (scope != NULL && scope->se_type != WHILE_SCOPE |
| 1294 | && scope->se_type != FOR_SCOPE) |
| 1295 | scope = scope->se_outer; |
| 1296 | if (scope == NULL) |
| 1297 | break; |
| 1298 | |
| 1299 | if (scope->se_type == WHILE_SCOPE) |
| 1300 | { |
| 1301 | loopinfo = &scope->se_u.se_while.ws_loop_info; |
| 1302 | // :while reserves one variable for funcref count |
| 1303 | cur_local_last = loopinfo->li_local_count - 1; |
| 1304 | } |
| 1305 | else |
| 1306 | { |
| 1307 | loopinfo = &scope->se_u.se_for.fs_loop_info; |
| 1308 | // :for reserves three variable: loop count, funcref count and loop |
| 1309 | // var |
| 1310 | cur_local_last = loopinfo->li_local_count - 3; |
| 1311 | } |
| 1312 | |
| 1313 | start_local_count = loopinfo->li_local_count; |
| 1314 | if (cctx->ctx_locals.ga_len > start_local_count) |
| 1315 | { |
| 1316 | lvi->lvi_loop[loopinfo->li_depth].var_idx = |
| 1317 | (short)start_local_count; |
| 1318 | lvi->lvi_loop[loopinfo->li_depth].var_count = |
| 1319 | (short)(cctx->ctx_locals.ga_len - start_local_count |
| 1320 | - prev_local_count); |
| 1321 | if (lvi->lvi_depth == 0) |
| 1322 | lvi->lvi_depth = loopinfo->li_depth + 1; |
| 1323 | } |
| 1324 | |
| 1325 | scope = scope->se_outer; |
| 1326 | prev_local_count = cctx->ctx_locals.ga_len - cur_local_last; |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1327 | } |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1328 | return lvi->lvi_depth > 0; |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | /* |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1332 | * Get the index of the variable "idx" in a loop, if any. |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1333 | */ |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1334 | void |
| 1335 | get_loop_var_idx(cctx_T *cctx, int idx, lvar_T *lvar) |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1336 | { |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1337 | loopvarinfo_T lvi; |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1338 | |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1339 | lvar->lv_loop_depth = -1; |
| 1340 | lvar->lv_loop_idx = -1; |
| 1341 | if (get_loop_var_info(cctx, &lvi)) |
| 1342 | { |
| 1343 | int depth; |
| 1344 | |
| 1345 | for (depth = lvi.lvi_depth - 1; depth >= 0; --depth) |
| 1346 | if (idx >= lvi.lvi_loop[depth].var_idx |
| 1347 | && idx < lvi.lvi_loop[depth].var_idx |
| 1348 | + lvi.lvi_loop[depth].var_count) |
| 1349 | { |
| 1350 | lvar->lv_loop_depth = depth; |
| 1351 | lvar->lv_loop_idx = lvi.lvi_loop[depth].var_idx; |
| 1352 | return; |
| 1353 | } |
| 1354 | } |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | /* |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 1358 | * Common for :break, :continue and :return |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1359 | */ |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 1360 | static int |
| 1361 | compile_find_scope( |
| 1362 | int *loop_label, // where to jump to or NULL |
| 1363 | endlabel_T ***el, // end label or NULL |
| 1364 | int *try_scopes, // :try scopes encountered or NULL |
| 1365 | char *error, // error to use when no scope found |
| 1366 | cctx_T *cctx) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1367 | { |
| 1368 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1369 | |
| 1370 | for (;;) |
| 1371 | { |
| 1372 | if (scope == NULL) |
| 1373 | { |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 1374 | if (error != NULL) |
| 1375 | emsg(_(error)); |
| 1376 | return FAIL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1377 | } |
| 1378 | if (scope->se_type == FOR_SCOPE) |
| 1379 | { |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 1380 | if (compile_loop_end(&scope->se_u.se_for.fs_loop_info, cctx) |
| 1381 | == FAIL) |
| 1382 | return FAIL; |
| 1383 | if (loop_label != NULL) |
| 1384 | *loop_label = scope->se_u.se_for.fs_top_label; |
| 1385 | if (el != NULL) |
| 1386 | *el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1387 | break; |
| 1388 | } |
| 1389 | if (scope->se_type == WHILE_SCOPE) |
| 1390 | { |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 1391 | if (compile_loop_end(&scope->se_u.se_while.ws_loop_info, cctx) |
| 1392 | == FAIL) |
| 1393 | return FAIL; |
| 1394 | if (loop_label != NULL) |
| 1395 | *loop_label = scope->se_u.se_while.ws_top_label; |
| 1396 | if (el != NULL) |
| 1397 | *el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1398 | break; |
| 1399 | } |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 1400 | if (try_scopes != NULL && scope->se_type == TRY_SCOPE) |
| 1401 | ++*try_scopes; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1402 | scope = scope->se_outer; |
| 1403 | } |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 1404 | return OK; |
| 1405 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1406 | |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 1407 | /* |
| 1408 | * compile "continue" |
| 1409 | */ |
| 1410 | char_u * |
| 1411 | compile_continue(char_u *arg, cctx_T *cctx) |
| 1412 | { |
| 1413 | int try_scopes = 0; |
| 1414 | int loop_label; |
| 1415 | |
| 1416 | if (compile_find_scope(&loop_label, NULL, &try_scopes, |
| 1417 | e_continue_without_while_or_for, cctx) == FAIL) |
| 1418 | return NULL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1419 | if (try_scopes > 0) |
| 1420 | // Inside one or more try/catch blocks we first need to jump to the |
| 1421 | // "finally" or "endtry" to cleanup. |
| 1422 | generate_TRYCONT(cctx, try_scopes, loop_label); |
| 1423 | else |
| 1424 | // Jump back to the FOR or WHILE instruction. |
| 1425 | generate_JUMP(cctx, JUMP_ALWAYS, loop_label); |
| 1426 | |
| 1427 | return arg; |
| 1428 | } |
| 1429 | |
| 1430 | /* |
| 1431 | * compile "break" |
| 1432 | */ |
| 1433 | char_u * |
| 1434 | compile_break(char_u *arg, cctx_T *cctx) |
| 1435 | { |
Bram Moolenaar | 873f824 | 2022-03-10 21:53:44 +0000 | [diff] [blame] | 1436 | int try_scopes = 0; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1437 | endlabel_T **el; |
| 1438 | |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 1439 | if (compile_find_scope(NULL, &el, &try_scopes, |
| 1440 | e_break_without_while_or_for, cctx) == FAIL) |
| 1441 | return NULL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1442 | |
Bram Moolenaar | 3f45d67 | 2023-02-27 22:06:51 +0000 | [diff] [blame] | 1443 | if (cctx->ctx_skip == SKIP_YES) |
| 1444 | return arg; |
| 1445 | |
Bram Moolenaar | 873f824 | 2022-03-10 21:53:44 +0000 | [diff] [blame] | 1446 | if (try_scopes > 0) |
| 1447 | // Inside one or more try/catch blocks we first need to jump to the |
| 1448 | // "finally" or "endtry" to cleanup. Then come to the next JUMP |
dundargoc | c57b5bc | 2022-11-02 13:30:51 +0000 | [diff] [blame] | 1449 | // instruction, which we don't know the index of yet. |
Bram Moolenaar | 873f824 | 2022-03-10 21:53:44 +0000 | [diff] [blame] | 1450 | generate_TRYCONT(cctx, try_scopes, cctx->ctx_instr.ga_len + 1); |
| 1451 | |
| 1452 | // Jump to the end of the FOR or WHILE loop. The instruction index will be |
| 1453 | // filled in later. |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1454 | if (compile_jump_to_end(el, JUMP_ALWAYS, 0, cctx) == FAIL) |
Bram Moolenaar | 3f45d67 | 2023-02-27 22:06:51 +0000 | [diff] [blame] | 1455 | return NULL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1456 | |
| 1457 | return arg; |
| 1458 | } |
| 1459 | |
| 1460 | /* |
| 1461 | * compile "{" start of block |
| 1462 | */ |
| 1463 | char_u * |
| 1464 | compile_block(char_u *arg, cctx_T *cctx) |
| 1465 | { |
| 1466 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 1467 | return NULL; |
| 1468 | return skipwhite(arg + 1); |
| 1469 | } |
| 1470 | |
| 1471 | /* |
| 1472 | * compile end of block: drop one scope |
| 1473 | */ |
| 1474 | void |
| 1475 | compile_endblock(cctx_T *cctx) |
| 1476 | { |
| 1477 | scope_T *scope = cctx->ctx_scope; |
| 1478 | |
| 1479 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | a275f2c | 2022-10-11 20:04:09 +0100 | [diff] [blame] | 1480 | unwind_locals(cctx, scope->se_local_count, TRUE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1481 | vim_free(scope); |
| 1482 | } |
| 1483 | |
| 1484 | /* |
| 1485 | * Compile "try". |
| 1486 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 1487 | * finally. |
| 1488 | * Creates another scope for the "try" block itself. |
| 1489 | * TRY instruction sets up exception handling at runtime. |
| 1490 | * |
| 1491 | * "try" |
| 1492 | * TRY -> catch1, -> finally push trystack entry |
| 1493 | * ... try block |
| 1494 | * "throw {exception}" |
| 1495 | * EVAL {exception} |
| 1496 | * THROW create exception |
| 1497 | * ... try block |
| 1498 | * " catch {expr}" |
| 1499 | * JUMP -> finally |
| 1500 | * catch1: PUSH exception |
| 1501 | * EVAL {expr} |
| 1502 | * MATCH |
| 1503 | * JUMP nomatch -> catch2 |
| 1504 | * CATCH remove exception |
| 1505 | * ... catch block |
| 1506 | * " catch" |
| 1507 | * JUMP -> finally |
| 1508 | * catch2: CATCH remove exception |
| 1509 | * ... catch block |
| 1510 | * " finally" |
| 1511 | * finally: |
| 1512 | * ... finally block |
| 1513 | * " endtry" |
| 1514 | * ENDTRY pop trystack entry, may rethrow |
| 1515 | */ |
| 1516 | char_u * |
| 1517 | compile_try(char_u *arg, cctx_T *cctx) |
| 1518 | { |
| 1519 | garray_T *instr = &cctx->ctx_instr; |
| 1520 | scope_T *try_scope; |
| 1521 | scope_T *scope; |
| 1522 | |
| 1523 | if (misplaced_cmdmod(cctx)) |
| 1524 | return NULL; |
| 1525 | |
| 1526 | // scope that holds the jumps that go to catch/finally/endtry |
| 1527 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 1528 | if (try_scope == NULL) |
| 1529 | return NULL; |
| 1530 | |
| 1531 | if (cctx->ctx_skip != SKIP_YES) |
| 1532 | { |
| 1533 | isn_T *isn; |
| 1534 | |
| 1535 | // "try_catch" is set when the first ":catch" is found or when no catch |
| 1536 | // is found and ":finally" is found. |
| 1537 | // "try_finally" is set when ":finally" is found |
| 1538 | // "try_endtry" is set when ":endtry" is found |
| 1539 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
| 1540 | if ((isn = generate_instr(cctx, ISN_TRY)) == NULL) |
| 1541 | return NULL; |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 1542 | isn->isn_arg.tryref.try_ref = ALLOC_CLEAR_ONE(tryref_T); |
| 1543 | if (isn->isn_arg.tryref.try_ref == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1544 | return NULL; |
| 1545 | } |
| 1546 | |
| 1547 | // scope for the try block itself |
| 1548 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 1549 | if (scope == NULL) |
| 1550 | return NULL; |
| 1551 | |
| 1552 | return arg; |
| 1553 | } |
| 1554 | |
| 1555 | /* |
| 1556 | * Compile "catch {expr}". |
| 1557 | */ |
| 1558 | char_u * |
| 1559 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 1560 | { |
| 1561 | scope_T *scope = cctx->ctx_scope; |
| 1562 | garray_T *instr = &cctx->ctx_instr; |
| 1563 | char_u *p; |
| 1564 | isn_T *isn; |
| 1565 | |
| 1566 | if (misplaced_cmdmod(cctx)) |
| 1567 | return NULL; |
| 1568 | |
| 1569 | // end block scope from :try or :catch |
| 1570 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 1571 | compile_endblock(cctx); |
| 1572 | scope = cctx->ctx_scope; |
| 1573 | |
| 1574 | // Error if not in a :try scope |
| 1575 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 1576 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1577 | emsg(_(e_catch_without_try)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1578 | return NULL; |
| 1579 | } |
| 1580 | |
Bram Moolenaar | 9d383f3 | 2023-05-14 21:38:12 +0100 | [diff] [blame] | 1581 | if (scope->se_u.se_try.ts_caught_all |
| 1582 | && !ignore_unreachable_code_for_testing) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1583 | { |
| 1584 | emsg(_(e_catch_unreachable_after_catch_all)); |
| 1585 | return NULL; |
| 1586 | } |
Bram Moolenaar | 53c2961 | 2022-01-12 16:18:18 +0000 | [diff] [blame] | 1587 | if (!cctx->ctx_had_return) |
| 1588 | scope->se_u.se_try.ts_no_return = TRUE; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1589 | |
| 1590 | if (cctx->ctx_skip != SKIP_YES) |
| 1591 | { |
| 1592 | #ifdef FEAT_PROFILE |
| 1593 | // the profile-start should be after the jump |
| 1594 | if (cctx->ctx_compile_type == CT_PROFILE |
| 1595 | && instr->ga_len > 0 |
| 1596 | && ((isn_T *)instr->ga_data)[instr->ga_len - 1] |
| 1597 | .isn_type == ISN_PROF_START) |
| 1598 | --instr->ga_len; |
| 1599 | #endif |
| 1600 | // Jump from end of previous block to :finally or :endtry |
| 1601 | if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label, |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1602 | JUMP_ALWAYS, 0, cctx) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1603 | return NULL; |
| 1604 | |
| 1605 | // End :try or :catch scope: set value in ISN_TRY instruction |
| 1606 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 1607 | if (isn->isn_arg.tryref.try_ref->try_catch == 0) |
| 1608 | isn->isn_arg.tryref.try_ref->try_catch = instr->ga_len; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1609 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 1610 | { |
| 1611 | // Previous catch without match jumps here |
| 1612 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 1613 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 1614 | } |
| 1615 | #ifdef FEAT_PROFILE |
| 1616 | if (cctx->ctx_compile_type == CT_PROFILE) |
| 1617 | { |
| 1618 | // a "throw" that jumps here needs to be counted |
| 1619 | generate_instr(cctx, ISN_PROF_END); |
| 1620 | // the "catch" is also counted |
| 1621 | generate_instr(cctx, ISN_PROF_START); |
| 1622 | } |
| 1623 | #endif |
| 1624 | if (cctx->ctx_compile_type == CT_DEBUG) |
| 1625 | generate_instr_debug(cctx); |
| 1626 | } |
| 1627 | |
| 1628 | p = skipwhite(arg); |
| 1629 | if (ends_excmd2(arg, p)) |
| 1630 | { |
| 1631 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 1632 | scope->se_u.se_try.ts_catch_label = 0; |
| 1633 | } |
| 1634 | else |
| 1635 | { |
| 1636 | char_u *end; |
| 1637 | char_u *pat; |
| 1638 | char_u *tofree = NULL; |
| 1639 | int dropped = 0; |
| 1640 | int len; |
| 1641 | |
| 1642 | // Push v:exception, push {expr} and MATCH |
| 1643 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 1644 | |
| 1645 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL); |
| 1646 | if (*end != *p) |
| 1647 | { |
| 1648 | semsg(_(e_separator_mismatch_str), p); |
| 1649 | vim_free(tofree); |
Bram Moolenaar | 54969f4 | 2022-02-07 13:56:44 +0000 | [diff] [blame] | 1650 | return NULL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1651 | } |
| 1652 | if (tofree == NULL) |
| 1653 | len = (int)(end - (p + 1)); |
| 1654 | else |
| 1655 | len = (int)(end - tofree); |
| 1656 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
| 1657 | vim_free(tofree); |
| 1658 | p += len + 2 + dropped; |
| 1659 | if (pat == NULL) |
Bram Moolenaar | 54969f4 | 2022-02-07 13:56:44 +0000 | [diff] [blame] | 1660 | return NULL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1661 | if (generate_PUSHS(cctx, &pat) == FAIL) |
Bram Moolenaar | 54969f4 | 2022-02-07 13:56:44 +0000 | [diff] [blame] | 1662 | return NULL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1663 | |
| 1664 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 1665 | return NULL; |
| 1666 | |
| 1667 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
| 1668 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 1669 | return NULL; |
| 1670 | } |
| 1671 | |
| 1672 | if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL) |
| 1673 | return NULL; |
| 1674 | |
| 1675 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 1676 | return NULL; |
| 1677 | return p; |
| 1678 | } |
| 1679 | |
| 1680 | char_u * |
| 1681 | compile_finally(char_u *arg, cctx_T *cctx) |
| 1682 | { |
| 1683 | scope_T *scope = cctx->ctx_scope; |
| 1684 | garray_T *instr = &cctx->ctx_instr; |
| 1685 | isn_T *isn; |
| 1686 | int this_instr; |
| 1687 | |
| 1688 | if (misplaced_cmdmod(cctx)) |
| 1689 | return NULL; |
| 1690 | |
| 1691 | // end block scope from :try or :catch |
| 1692 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 1693 | compile_endblock(cctx); |
| 1694 | scope = cctx->ctx_scope; |
| 1695 | |
| 1696 | // Error if not in a :try scope |
| 1697 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 1698 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1699 | emsg(_(e_finally_without_try)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1700 | return NULL; |
| 1701 | } |
| 1702 | |
| 1703 | if (cctx->ctx_skip != SKIP_YES) |
| 1704 | { |
| 1705 | // End :catch or :finally scope: set value in ISN_TRY instruction |
| 1706 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 1707 | if (isn->isn_arg.tryref.try_ref->try_finally != 0) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1708 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1709 | emsg(_(e_multiple_finally)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1710 | return NULL; |
| 1711 | } |
| 1712 | |
| 1713 | this_instr = instr->ga_len; |
| 1714 | #ifdef FEAT_PROFILE |
| 1715 | if (cctx->ctx_compile_type == CT_PROFILE |
| 1716 | && ((isn_T *)instr->ga_data)[this_instr - 1] |
| 1717 | .isn_type == ISN_PROF_START) |
| 1718 | { |
| 1719 | // jump to the profile start of the "finally" |
| 1720 | --this_instr; |
| 1721 | |
| 1722 | // jump to the profile end above it |
| 1723 | if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1] |
| 1724 | .isn_type == ISN_PROF_END) |
| 1725 | --this_instr; |
| 1726 | } |
| 1727 | #endif |
| 1728 | |
| 1729 | // Fill in the "end" label in jumps at the end of the blocks. |
| 1730 | compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, |
| 1731 | this_instr, cctx); |
| 1732 | |
| 1733 | // If there is no :catch then an exception jumps to :finally. |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 1734 | if (isn->isn_arg.tryref.try_ref->try_catch == 0) |
| 1735 | isn->isn_arg.tryref.try_ref->try_catch = this_instr; |
| 1736 | isn->isn_arg.tryref.try_ref->try_finally = this_instr; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1737 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 1738 | { |
| 1739 | // Previous catch without match jumps here |
| 1740 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 1741 | isn->isn_arg.jump.jump_where = this_instr; |
| 1742 | scope->se_u.se_try.ts_catch_label = 0; |
| 1743 | } |
Bram Moolenaar | 53c2961 | 2022-01-12 16:18:18 +0000 | [diff] [blame] | 1744 | scope->se_u.se_try.ts_has_finally = TRUE; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1745 | if (generate_instr(cctx, ISN_FINALLY) == NULL) |
| 1746 | return NULL; |
| 1747 | } |
| 1748 | |
| 1749 | return arg; |
| 1750 | } |
| 1751 | |
| 1752 | char_u * |
| 1753 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 1754 | { |
| 1755 | scope_T *scope = cctx->ctx_scope; |
| 1756 | garray_T *instr = &cctx->ctx_instr; |
| 1757 | isn_T *try_isn; |
| 1758 | |
| 1759 | if (misplaced_cmdmod(cctx)) |
| 1760 | return NULL; |
| 1761 | |
| 1762 | // end block scope from :catch or :finally |
| 1763 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 1764 | compile_endblock(cctx); |
| 1765 | scope = cctx->ctx_scope; |
| 1766 | |
| 1767 | // Error if not in a :try scope |
| 1768 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 1769 | { |
| 1770 | if (scope == NULL) |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1771 | emsg(_(e_endtry_without_try)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1772 | else if (scope->se_type == WHILE_SCOPE) |
Bram Moolenaar | 1a99222 | 2021-12-31 17:25:48 +0000 | [diff] [blame] | 1773 | emsg(_(e_missing_endwhile)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1774 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 1a99222 | 2021-12-31 17:25:48 +0000 | [diff] [blame] | 1775 | emsg(_(e_missing_endfor)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1776 | else |
Bram Moolenaar | 1a99222 | 2021-12-31 17:25:48 +0000 | [diff] [blame] | 1777 | emsg(_(e_missing_endif)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1778 | return NULL; |
| 1779 | } |
| 1780 | |
| 1781 | try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
| 1782 | if (cctx->ctx_skip != SKIP_YES) |
| 1783 | { |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 1784 | if (try_isn->isn_arg.tryref.try_ref->try_catch == 0 |
| 1785 | && try_isn->isn_arg.tryref.try_ref->try_finally == 0) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1786 | { |
| 1787 | emsg(_(e_missing_catch_or_finally)); |
| 1788 | return NULL; |
| 1789 | } |
| 1790 | |
| 1791 | #ifdef FEAT_PROFILE |
| 1792 | if (cctx->ctx_compile_type == CT_PROFILE |
| 1793 | && ((isn_T *)instr->ga_data)[instr->ga_len - 1] |
| 1794 | .isn_type == ISN_PROF_START) |
| 1795 | // move the profile start after "endtry" so that it's not counted when |
| 1796 | // the exception is rethrown. |
| 1797 | --instr->ga_len; |
| 1798 | #endif |
| 1799 | |
| 1800 | // Fill in the "end" label in jumps at the end of the blocks, if not |
| 1801 | // done by ":finally". |
| 1802 | compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, |
| 1803 | instr->ga_len, cctx); |
| 1804 | |
| 1805 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 1806 | { |
| 1807 | // Last catch without match jumps here |
| 1808 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 1809 | + scope->se_u.se_try.ts_catch_label; |
| 1810 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 1811 | } |
| 1812 | } |
| 1813 | |
Bram Moolenaar | 53c2961 | 2022-01-12 16:18:18 +0000 | [diff] [blame] | 1814 | // If there is a finally clause that ends in return then we will return. |
| 1815 | // If one of the blocks didn't end in "return" or we did not catch all |
| 1816 | // exceptions reset the had_return flag. |
| 1817 | if (!(scope->se_u.se_try.ts_has_finally && cctx->ctx_had_return) |
| 1818 | && (scope->se_u.se_try.ts_no_return |
| 1819 | || !scope->se_u.se_try.ts_caught_all)) |
| 1820 | cctx->ctx_had_return = FALSE; |
| 1821 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1822 | compile_endblock(cctx); |
| 1823 | |
| 1824 | if (cctx->ctx_skip != SKIP_YES) |
| 1825 | { |
| 1826 | // End :catch or :finally scope: set instruction index in ISN_TRY |
| 1827 | // instruction |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 1828 | try_isn->isn_arg.tryref.try_ref->try_endtry = instr->ga_len; |
Bram Moolenaar | fe15499 | 2022-03-22 20:42:12 +0000 | [diff] [blame] | 1829 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1830 | return NULL; |
| 1831 | #ifdef FEAT_PROFILE |
| 1832 | if (cctx->ctx_compile_type == CT_PROFILE) |
| 1833 | generate_instr(cctx, ISN_PROF_START); |
| 1834 | #endif |
| 1835 | } |
| 1836 | return arg; |
| 1837 | } |
| 1838 | |
| 1839 | /* |
| 1840 | * compile "throw {expr}" |
| 1841 | */ |
| 1842 | char_u * |
| 1843 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 1844 | { |
| 1845 | char_u *p = skipwhite(arg); |
| 1846 | |
| 1847 | if (compile_expr0(&p, cctx) == FAIL) |
| 1848 | return NULL; |
| 1849 | if (cctx->ctx_skip == SKIP_YES) |
| 1850 | return p; |
| 1851 | if (may_generate_2STRING(-1, FALSE, cctx) == FAIL) |
| 1852 | return NULL; |
| 1853 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 1854 | return NULL; |
| 1855 | |
| 1856 | return p; |
| 1857 | } |
| 1858 | |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 1859 | /* |
| 1860 | * Compile an expression or function call. |
| 1861 | */ |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1862 | char_u * |
| 1863 | compile_eval(char_u *arg, cctx_T *cctx) |
| 1864 | { |
| 1865 | char_u *p = arg; |
| 1866 | int name_only; |
| 1867 | long lnum = SOURCING_LNUM; |
| 1868 | |
| 1869 | // find_ex_command() will consider a variable name an expression, assuming |
| 1870 | // that something follows on the next line. Check that something actually |
| 1871 | // follows, otherwise it's probably a misplaced command. |
| 1872 | name_only = cmd_is_name_only(arg); |
| 1873 | |
| 1874 | if (compile_expr0(&p, cctx) == FAIL) |
| 1875 | return NULL; |
| 1876 | |
| 1877 | if (name_only && lnum == SOURCING_LNUM) |
| 1878 | { |
| 1879 | semsg(_(e_expression_without_effect_str), arg); |
| 1880 | return NULL; |
| 1881 | } |
| 1882 | |
| 1883 | // drop the result |
| 1884 | generate_instr_drop(cctx, ISN_DROP, 1); |
| 1885 | |
| 1886 | return skipwhite(p); |
| 1887 | } |
| 1888 | |
| 1889 | /* |
Bram Moolenaar | c572ad5 | 2022-09-08 20:49:22 +0100 | [diff] [blame] | 1890 | * Get the local variable index for deferred function calls. |
| 1891 | * Reserve it when not done already. |
| 1892 | * Returns zero for failure. |
| 1893 | */ |
| 1894 | int |
| 1895 | get_defer_var_idx(cctx_T *cctx) |
| 1896 | { |
| 1897 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 1898 | + cctx->ctx_ufunc->uf_dfunc_idx; |
| 1899 | if (dfunc->df_defer_var_idx == 0) |
| 1900 | { |
| 1901 | lvar_T *lvar = reserve_local(cctx, (char_u *)"@defer@", 7, |
| 1902 | TRUE, &t_list_any); |
| 1903 | if (lvar == NULL) |
| 1904 | return 0; |
| 1905 | dfunc->df_defer_var_idx = lvar->lv_idx + 1; |
| 1906 | } |
| 1907 | return dfunc->df_defer_var_idx; |
| 1908 | } |
| 1909 | |
| 1910 | /* |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 1911 | * Compile "defer func(arg)". |
| 1912 | */ |
| 1913 | char_u * |
| 1914 | compile_defer(char_u *arg_start, cctx_T *cctx) |
| 1915 | { |
Bram Moolenaar | 1690032 | 2022-09-08 19:51:45 +0100 | [diff] [blame] | 1916 | char_u *paren; |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 1917 | char_u *arg = arg_start; |
| 1918 | int argcount = 0; |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 1919 | int defer_var_idx; |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 1920 | type_T *type; |
| 1921 | int func_idx; |
Bram Moolenaar | 8dbab1d | 2023-01-27 20:14:02 +0000 | [diff] [blame] | 1922 | int obj_method = 0; |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 1923 | |
| 1924 | // Get a funcref for the function name. |
| 1925 | // TODO: better way to find the "(". |
Bram Moolenaar | 1690032 | 2022-09-08 19:51:45 +0100 | [diff] [blame] | 1926 | paren = vim_strchr(arg, '('); |
| 1927 | if (paren == NULL) |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 1928 | { |
| 1929 | semsg(_(e_missing_parenthesis_str), arg); |
| 1930 | return NULL; |
| 1931 | } |
Bram Moolenaar | 1690032 | 2022-09-08 19:51:45 +0100 | [diff] [blame] | 1932 | *paren = NUL; |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 1933 | func_idx = find_internal_func(arg); |
| 1934 | if (func_idx >= 0) |
| 1935 | // TODO: better type |
| 1936 | generate_PUSHFUNC(cctx, (char_u *)internal_func_name(func_idx), |
| 1937 | &t_func_any, FALSE); |
Bram Moolenaar | 8dbab1d | 2023-01-27 20:14:02 +0000 | [diff] [blame] | 1938 | else |
| 1939 | { |
| 1940 | int typecount = cctx->ctx_type_stack.ga_len; |
| 1941 | if (compile_expr0(&arg, cctx) == FAIL) |
| 1942 | return NULL; |
| 1943 | if (cctx->ctx_type_stack.ga_len >= typecount + 2) |
| 1944 | // must have seen "obj.Func", pushed an object and a function |
| 1945 | obj_method = 1; |
| 1946 | } |
Bram Moolenaar | 1690032 | 2022-09-08 19:51:45 +0100 | [diff] [blame] | 1947 | *paren = '('; |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 1948 | |
| 1949 | // check for function type |
| 1950 | type = get_type_on_stack(cctx, 0); |
| 1951 | if (type->tt_type != VAR_FUNC) |
| 1952 | { |
| 1953 | emsg(_(e_function_name_required)); |
| 1954 | return NULL; |
| 1955 | } |
| 1956 | |
| 1957 | // compile the arguments |
Bram Moolenaar | 1690032 | 2022-09-08 19:51:45 +0100 | [diff] [blame] | 1958 | arg = skipwhite(paren + 1); |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 1959 | if (compile_arguments(&arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL) |
| 1960 | return NULL; |
| 1961 | |
Bram Moolenaar | 1690032 | 2022-09-08 19:51:45 +0100 | [diff] [blame] | 1962 | if (func_idx >= 0) |
| 1963 | { |
| 1964 | type2_T *argtypes = NULL; |
| 1965 | type2_T shuffled_argtypes[MAX_FUNC_ARGS]; |
| 1966 | |
| 1967 | if (check_internal_func_args(cctx, func_idx, argcount, FALSE, |
| 1968 | &argtypes, shuffled_argtypes) == FAIL) |
| 1969 | return NULL; |
| 1970 | } |
| 1971 | else if (check_func_args_from_type(cctx, type, argcount, TRUE, |
| 1972 | arg_start) == FAIL) |
| 1973 | return NULL; |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 1974 | |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 1975 | defer_var_idx = get_defer_var_idx(cctx); |
| 1976 | if (defer_var_idx == 0) |
| 1977 | return NULL; |
Bram Moolenaar | 8dbab1d | 2023-01-27 20:14:02 +0000 | [diff] [blame] | 1978 | if (generate_DEFER(cctx, defer_var_idx - 1, obj_method, argcount) == FAIL) |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 1979 | return NULL; |
| 1980 | |
| 1981 | return skipwhite(arg); |
| 1982 | } |
| 1983 | |
| 1984 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1985 | * compile "echo expr" |
| 1986 | * compile "echomsg expr" |
| 1987 | * compile "echoerr expr" |
| 1988 | * compile "echoconsole expr" |
Bram Moolenaar | bdc09a1 | 2022-10-07 14:31:45 +0100 | [diff] [blame] | 1989 | * compile "echowindow expr" - may have cmd_count set |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1990 | * compile "execute expr" |
| 1991 | */ |
| 1992 | char_u * |
Bram Moolenaar | 9b99411 | 2022-12-25 15:59:25 +0000 | [diff] [blame] | 1993 | compile_mult_expr( |
| 1994 | char_u *arg, |
| 1995 | int cmdidx, |
| 1996 | long cmd_count UNUSED, |
| 1997 | cctx_T *cctx) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1998 | { |
| 1999 | char_u *p = arg; |
| 2000 | char_u *prev = arg; |
| 2001 | char_u *expr_start; |
| 2002 | int count = 0; |
| 2003 | int start_ctx_lnum = cctx->ctx_lnum; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2004 | type_T *type; |
Bram Moolenaar | bdc09a1 | 2022-10-07 14:31:45 +0100 | [diff] [blame] | 2005 | int r = OK; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2006 | |
| 2007 | for (;;) |
| 2008 | { |
| 2009 | if (ends_excmd2(prev, p)) |
| 2010 | break; |
| 2011 | expr_start = p; |
| 2012 | if (compile_expr0(&p, cctx) == FAIL) |
| 2013 | return NULL; |
| 2014 | |
| 2015 | if (cctx->ctx_skip != SKIP_YES) |
| 2016 | { |
| 2017 | // check for non-void type |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2018 | type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2019 | if (type->tt_type == VAR_VOID) |
| 2020 | { |
| 2021 | semsg(_(e_expression_does_not_result_in_value_str), expr_start); |
| 2022 | return NULL; |
| 2023 | } |
| 2024 | } |
| 2025 | |
| 2026 | ++count; |
| 2027 | prev = p; |
| 2028 | p = skipwhite(p); |
| 2029 | } |
| 2030 | |
| 2031 | if (count > 0) |
| 2032 | { |
| 2033 | long save_lnum = cctx->ctx_lnum; |
| 2034 | |
| 2035 | // Use the line number where the command started. |
| 2036 | cctx->ctx_lnum = start_ctx_lnum; |
| 2037 | |
| 2038 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
Bram Moolenaar | bdc09a1 | 2022-10-07 14:31:45 +0100 | [diff] [blame] | 2039 | r = generate_ECHO(cctx, cmdidx == CMD_echo, count); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2040 | else if (cmdidx == CMD_execute) |
Bram Moolenaar | bdc09a1 | 2022-10-07 14:31:45 +0100 | [diff] [blame] | 2041 | r = generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2042 | else if (cmdidx == CMD_echomsg) |
Bram Moolenaar | bdc09a1 | 2022-10-07 14:31:45 +0100 | [diff] [blame] | 2043 | r = generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
Bram Moolenaar | 7d7ad7b | 2022-09-01 16:00:53 +0100 | [diff] [blame] | 2044 | #ifdef HAS_MESSAGE_WINDOW |
| 2045 | else if (cmdidx == CMD_echowindow) |
Bram Moolenaar | bdc09a1 | 2022-10-07 14:31:45 +0100 | [diff] [blame] | 2046 | r = generate_ECHOWINDOW(cctx, count, cmd_count); |
Bram Moolenaar | 7d7ad7b | 2022-09-01 16:00:53 +0100 | [diff] [blame] | 2047 | #endif |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2048 | else if (cmdidx == CMD_echoconsole) |
Bram Moolenaar | bdc09a1 | 2022-10-07 14:31:45 +0100 | [diff] [blame] | 2049 | r = generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2050 | else |
Bram Moolenaar | bdc09a1 | 2022-10-07 14:31:45 +0100 | [diff] [blame] | 2051 | r = generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2052 | |
| 2053 | cctx->ctx_lnum = save_lnum; |
| 2054 | } |
Bram Moolenaar | bdc09a1 | 2022-10-07 14:31:45 +0100 | [diff] [blame] | 2055 | return r == OK ? p : NULL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | /* |
| 2059 | * If "eap" has a range that is not a constant generate an ISN_RANGE |
| 2060 | * instruction to compute it and return OK. |
| 2061 | * Otherwise return FAIL, the caller must deal with any range. |
| 2062 | */ |
| 2063 | static int |
| 2064 | compile_variable_range(exarg_T *eap, cctx_T *cctx) |
| 2065 | { |
| 2066 | char_u *range_end = skip_range(eap->cmd, TRUE, NULL); |
| 2067 | char_u *p = skipdigits(eap->cmd); |
| 2068 | |
| 2069 | if (p == range_end) |
| 2070 | return FAIL; |
| 2071 | return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd)); |
| 2072 | } |
| 2073 | |
| 2074 | /* |
| 2075 | * :put r |
| 2076 | * :put ={expr} |
| 2077 | */ |
| 2078 | char_u * |
| 2079 | compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 2080 | { |
| 2081 | char_u *line = arg; |
| 2082 | linenr_T lnum; |
| 2083 | char *errormsg; |
| 2084 | int above = eap->forceit; |
| 2085 | |
| 2086 | eap->regname = *line; |
| 2087 | |
| 2088 | if (eap->regname == '=') |
| 2089 | { |
Bram Moolenaar | d0b7bfa | 2022-03-12 14:51:16 +0000 | [diff] [blame] | 2090 | char_u *p = skipwhite(line + 1); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2091 | |
| 2092 | if (compile_expr0(&p, cctx) == FAIL) |
| 2093 | return NULL; |
| 2094 | line = p; |
| 2095 | } |
| 2096 | else if (eap->regname != NUL) |
| 2097 | ++line; |
| 2098 | |
| 2099 | if (compile_variable_range(eap, cctx) == OK) |
| 2100 | { |
| 2101 | lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE; |
| 2102 | } |
| 2103 | else |
| 2104 | { |
| 2105 | // Either no range or a number. |
| 2106 | // "errormsg" will not be set because the range is ADDR_LINES. |
| 2107 | if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL) |
| 2108 | // cannot happen |
| 2109 | return NULL; |
| 2110 | if (eap->addr_count == 0) |
| 2111 | lnum = -1; |
| 2112 | else |
| 2113 | lnum = eap->line2; |
| 2114 | if (above) |
| 2115 | --lnum; |
| 2116 | } |
| 2117 | |
| 2118 | generate_PUT(cctx, eap->regname, lnum); |
| 2119 | return line; |
| 2120 | } |
| 2121 | |
| 2122 | /* |
| 2123 | * A command that is not compiled, execute with legacy code. |
| 2124 | */ |
| 2125 | char_u * |
| 2126 | compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx) |
| 2127 | { |
| 2128 | char_u *line = line_arg; |
| 2129 | char_u *p; |
| 2130 | int has_expr = FALSE; |
| 2131 | char_u *nextcmd = (char_u *)""; |
| 2132 | char_u *tofree = NULL; |
| 2133 | char_u *cmd_arg = NULL; |
| 2134 | |
| 2135 | if (cctx->ctx_skip == SKIP_YES) |
| 2136 | goto theend; |
| 2137 | |
| 2138 | // If there was a prececing command modifier, drop it and include it in the |
| 2139 | // EXEC command. |
| 2140 | if (cctx->ctx_has_cmdmod) |
| 2141 | { |
| 2142 | garray_T *instr = &cctx->ctx_instr; |
| 2143 | isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 2144 | |
| 2145 | if (isn->isn_type == ISN_CMDMOD) |
| 2146 | { |
| 2147 | vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod |
| 2148 | ->cmod_filter_regmatch.regprog); |
| 2149 | vim_free(isn->isn_arg.cmdmod.cf_cmdmod); |
| 2150 | --instr->ga_len; |
| 2151 | cctx->ctx_has_cmdmod = FALSE; |
| 2152 | } |
| 2153 | } |
| 2154 | |
| 2155 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
| 2156 | { |
| 2157 | long argt = eap->argt; |
| 2158 | int usefilter = FALSE; |
| 2159 | |
| 2160 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 2161 | |
| 2162 | // If the command can be followed by a bar, find the bar and truncate |
| 2163 | // it, so that the following command can be compiled. |
| 2164 | // The '|' is overwritten with a NUL, it is put back below. |
| 2165 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 2166 | && *eap->arg == '!') |
| 2167 | // :w !filter or :r !filter or :r! filter |
| 2168 | usefilter = TRUE; |
| 2169 | if ((argt & EX_TRLBAR) && !usefilter) |
| 2170 | { |
| 2171 | eap->argt = argt; |
Bram Moolenaar | ac48506 | 2022-03-23 19:45:01 +0000 | [diff] [blame] | 2172 | separate_nextcmd(eap, TRUE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2173 | if (eap->nextcmd != NULL) |
| 2174 | nextcmd = eap->nextcmd; |
| 2175 | } |
| 2176 | else if (eap->cmdidx == CMD_wincmd) |
| 2177 | { |
| 2178 | p = eap->arg; |
| 2179 | if (*p != NUL) |
| 2180 | ++p; |
| 2181 | if (*p == 'g' || *p == Ctrl_G) |
| 2182 | ++p; |
| 2183 | p = skipwhite(p); |
| 2184 | if (*p == '|') |
| 2185 | { |
| 2186 | *p = NUL; |
| 2187 | nextcmd = p + 1; |
| 2188 | } |
| 2189 | } |
| 2190 | else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd) |
| 2191 | { |
| 2192 | // If there is a trailing '{' read lines until the '}' |
| 2193 | p = eap->arg + STRLEN(eap->arg) - 1; |
| 2194 | while (p > eap->arg && VIM_ISWHITE(*p)) |
| 2195 | --p; |
| 2196 | if (*p == '{') |
| 2197 | { |
| 2198 | exarg_T ea; |
Bram Moolenaar | 62628d9 | 2022-02-25 21:10:53 +0000 | [diff] [blame] | 2199 | int flags = 0; // unused |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2200 | int start_lnum = SOURCING_LNUM; |
| 2201 | |
| 2202 | CLEAR_FIELD(ea); |
| 2203 | ea.arg = eap->arg; |
| 2204 | fill_exarg_from_cctx(&ea, cctx); |
| 2205 | (void)may_get_cmd_block(&ea, p, &tofree, &flags); |
| 2206 | if (tofree != NULL) |
| 2207 | { |
| 2208 | *p = NUL; |
| 2209 | line = concat_str(line, tofree); |
| 2210 | if (line == NULL) |
| 2211 | goto theend; |
| 2212 | vim_free(tofree); |
| 2213 | tofree = line; |
| 2214 | SOURCING_LNUM = start_lnum; |
| 2215 | } |
| 2216 | } |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 2221 | { |
| 2222 | // expand filename in "syntax include [@group] filename" |
| 2223 | has_expr = TRUE; |
| 2224 | eap->arg = skipwhite(eap->arg + 7); |
| 2225 | if (*eap->arg == '@') |
| 2226 | eap->arg = skiptowhite(eap->arg); |
| 2227 | } |
| 2228 | |
| 2229 | if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal) |
| 2230 | && STRLEN(eap->arg) > 4) |
| 2231 | { |
| 2232 | int delim = *eap->arg; |
| 2233 | |
| 2234 | p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL); |
| 2235 | if (*p == delim) |
| 2236 | cmd_arg = p + 1; |
| 2237 | } |
| 2238 | |
| 2239 | if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed) |
| 2240 | cmd_arg = eap->arg; |
| 2241 | |
| 2242 | if (cmd_arg != NULL) |
| 2243 | { |
| 2244 | exarg_T nea; |
| 2245 | |
| 2246 | CLEAR_FIELD(nea); |
| 2247 | nea.cmd = cmd_arg; |
| 2248 | p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL); |
| 2249 | if (nea.cmdidx < CMD_SIZE) |
| 2250 | { |
| 2251 | has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND); |
| 2252 | if (has_expr) |
| 2253 | eap->arg = skiptowhite(eap->arg); |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
| 2258 | { |
| 2259 | int count = 0; |
| 2260 | char_u *start = skipwhite(line); |
| 2261 | |
| 2262 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 2263 | // PUSHS ":cmd xxx" |
| 2264 | // eval expr1 |
| 2265 | // PUSHS "yyy" |
| 2266 | // eval expr2 |
| 2267 | // PUSHS "zzz" |
| 2268 | // EXECCONCAT 5 |
| 2269 | for (;;) |
| 2270 | { |
| 2271 | if (p > start) |
| 2272 | { |
| 2273 | char_u *val = vim_strnsave(start, p - start); |
| 2274 | |
| 2275 | generate_PUSHS(cctx, &val); |
| 2276 | ++count; |
| 2277 | } |
| 2278 | p += 2; |
| 2279 | if (compile_expr0(&p, cctx) == FAIL) |
| 2280 | return NULL; |
| 2281 | may_generate_2STRING(-1, TRUE, cctx); |
| 2282 | ++count; |
| 2283 | p = skipwhite(p); |
| 2284 | if (*p != '`') |
| 2285 | { |
| 2286 | emsg(_(e_missing_backtick)); |
| 2287 | return NULL; |
| 2288 | } |
| 2289 | start = p + 1; |
| 2290 | |
| 2291 | p = (char_u *)strstr((char *)start, "`="); |
| 2292 | if (p == NULL) |
| 2293 | { |
| 2294 | if (*skipwhite(start) != NUL) |
| 2295 | { |
| 2296 | char_u *val = vim_strsave(start); |
| 2297 | |
| 2298 | generate_PUSHS(cctx, &val); |
| 2299 | ++count; |
| 2300 | } |
| 2301 | break; |
| 2302 | } |
| 2303 | } |
| 2304 | generate_EXECCONCAT(cctx, count); |
| 2305 | } |
| 2306 | else |
| 2307 | generate_EXEC_copy(cctx, ISN_EXEC, line); |
| 2308 | |
| 2309 | theend: |
| 2310 | if (*nextcmd != NUL) |
| 2311 | { |
| 2312 | // the parser expects a pointer to the bar, put it back |
| 2313 | --nextcmd; |
| 2314 | *nextcmd = '|'; |
| 2315 | } |
| 2316 | vim_free(tofree); |
| 2317 | |
| 2318 | return nextcmd; |
| 2319 | } |
| 2320 | |
| 2321 | /* |
| 2322 | * A script command with heredoc, e.g. |
| 2323 | * ruby << EOF |
| 2324 | * command |
| 2325 | * EOF |
| 2326 | * Has been turned into one long line with NL characters by |
| 2327 | * get_function_body(): |
| 2328 | * ruby << EOF<NL> command<NL>EOF |
| 2329 | */ |
| 2330 | char_u * |
| 2331 | compile_script(char_u *line, cctx_T *cctx) |
| 2332 | { |
| 2333 | if (cctx->ctx_skip != SKIP_YES) |
| 2334 | { |
| 2335 | isn_T *isn; |
| 2336 | |
| 2337 | if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL) |
| 2338 | return NULL; |
| 2339 | isn->isn_arg.string = vim_strsave(line); |
| 2340 | } |
| 2341 | return (char_u *)""; |
| 2342 | } |
| 2343 | |
| 2344 | |
| 2345 | /* |
| 2346 | * :s/pat/repl/ |
| 2347 | */ |
| 2348 | char_u * |
| 2349 | compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 2350 | { |
| 2351 | char_u *cmd = eap->arg; |
| 2352 | char_u *expr = (char_u *)strstr((char *)cmd, "\\="); |
| 2353 | |
| 2354 | if (expr != NULL) |
| 2355 | { |
| 2356 | int delimiter = *cmd++; |
| 2357 | |
| 2358 | // There is a \=expr, find it in the substitute part. |
| 2359 | cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL); |
| 2360 | if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=') |
| 2361 | { |
| 2362 | garray_T save_ga = cctx->ctx_instr; |
| 2363 | char_u *end; |
| 2364 | int expr_res; |
| 2365 | int trailing_error; |
| 2366 | int instr_count; |
| 2367 | isn_T *instr; |
| 2368 | isn_T *isn; |
| 2369 | |
| 2370 | cmd += 3; |
| 2371 | end = skip_substitute(cmd, delimiter); |
| 2372 | |
| 2373 | // Temporarily reset the list of instructions so that the jump |
| 2374 | // labels are correct. |
| 2375 | cctx->ctx_instr.ga_len = 0; |
| 2376 | cctx->ctx_instr.ga_maxlen = 0; |
| 2377 | cctx->ctx_instr.ga_data = NULL; |
| 2378 | expr_res = compile_expr0(&cmd, cctx); |
| 2379 | if (end[-1] == NUL) |
| 2380 | end[-1] = delimiter; |
| 2381 | cmd = skipwhite(cmd); |
| 2382 | trailing_error = *cmd != delimiter && *cmd != NUL; |
| 2383 | |
| 2384 | if (expr_res == FAIL || trailing_error |
| 2385 | || GA_GROW_FAILS(&cctx->ctx_instr, 1)) |
| 2386 | { |
| 2387 | if (trailing_error) |
Bram Moolenaar | 74409f6 | 2022-01-01 15:58:22 +0000 | [diff] [blame] | 2388 | semsg(_(e_trailing_characters_str), cmd); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2389 | clear_instr_ga(&cctx->ctx_instr); |
| 2390 | cctx->ctx_instr = save_ga; |
| 2391 | return NULL; |
| 2392 | } |
| 2393 | |
| 2394 | // Move the generated instructions into the ISN_SUBSTITUTE |
| 2395 | // instructions, then restore the list of instructions before |
| 2396 | // adding the ISN_SUBSTITUTE instruction. |
| 2397 | instr_count = cctx->ctx_instr.ga_len; |
| 2398 | instr = cctx->ctx_instr.ga_data; |
| 2399 | instr[instr_count].isn_type = ISN_FINISH; |
| 2400 | |
| 2401 | cctx->ctx_instr = save_ga; |
| 2402 | if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL) |
| 2403 | { |
| 2404 | int idx; |
| 2405 | |
| 2406 | for (idx = 0; idx < instr_count; ++idx) |
| 2407 | delete_instr(instr + idx); |
| 2408 | vim_free(instr); |
| 2409 | return NULL; |
| 2410 | } |
| 2411 | isn->isn_arg.subs.subs_cmd = vim_strsave(arg); |
| 2412 | isn->isn_arg.subs.subs_instr = instr; |
| 2413 | |
| 2414 | // skip over flags |
| 2415 | if (*end == '&') |
| 2416 | ++end; |
| 2417 | while (ASCII_ISALPHA(*end) || *end == '#') |
| 2418 | ++end; |
| 2419 | return end; |
| 2420 | } |
| 2421 | } |
| 2422 | |
| 2423 | return compile_exec(arg, eap, cctx); |
| 2424 | } |
| 2425 | |
| 2426 | char_u * |
| 2427 | compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 2428 | { |
| 2429 | char_u *arg = eap->arg; |
| 2430 | lhs_T *lhs = &cctx->ctx_redir_lhs; |
| 2431 | |
| 2432 | if (lhs->lhs_name != NULL) |
| 2433 | { |
| 2434 | if (STRNCMP(arg, "END", 3) == 0) |
| 2435 | { |
Bram Moolenaar | 3558afe | 2022-10-13 16:12:57 +0100 | [diff] [blame] | 2436 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2437 | { |
Bram Moolenaar | 3558afe | 2022-10-13 16:12:57 +0100 | [diff] [blame] | 2438 | if (lhs->lhs_append) |
| 2439 | { |
| 2440 | // First load the current variable value. |
| 2441 | if (compile_load_lhs_with_index(lhs, lhs->lhs_whole, |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2442 | cctx) == FAIL) |
Bram Moolenaar | 3558afe | 2022-10-13 16:12:57 +0100 | [diff] [blame] | 2443 | return NULL; |
| 2444 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2445 | |
Bram Moolenaar | 3558afe | 2022-10-13 16:12:57 +0100 | [diff] [blame] | 2446 | // Gets the redirected text and put it on the stack, then store |
| 2447 | // it in the variable. |
| 2448 | generate_instr_type(cctx, ISN_REDIREND, &t_string); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2449 | |
Bram Moolenaar | 3558afe | 2022-10-13 16:12:57 +0100 | [diff] [blame] | 2450 | if (lhs->lhs_append) |
| 2451 | generate_CONCAT(cctx, 2); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2452 | |
Bram Moolenaar | 3558afe | 2022-10-13 16:12:57 +0100 | [diff] [blame] | 2453 | if (lhs->lhs_has_index) |
| 2454 | { |
| 2455 | // Use the info in "lhs" to store the value at the index in |
| 2456 | // the list or dict. |
| 2457 | if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE, |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2458 | &t_string, cctx) == FAIL) |
Bram Moolenaar | 3558afe | 2022-10-13 16:12:57 +0100 | [diff] [blame] | 2459 | return NULL; |
| 2460 | } |
| 2461 | else if (generate_store_lhs(cctx, lhs, -1, FALSE) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2462 | return NULL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2463 | |
Bram Moolenaar | 3558afe | 2022-10-13 16:12:57 +0100 | [diff] [blame] | 2464 | VIM_CLEAR(lhs->lhs_name); |
| 2465 | VIM_CLEAR(lhs->lhs_whole); |
| 2466 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2467 | return arg + 3; |
| 2468 | } |
| 2469 | emsg(_(e_cannot_nest_redir)); |
| 2470 | return NULL; |
| 2471 | } |
| 2472 | |
| 2473 | if (arg[0] == '=' && arg[1] == '>') |
| 2474 | { |
| 2475 | int append = FALSE; |
| 2476 | |
| 2477 | // redirect to a variable is compiled |
| 2478 | arg += 2; |
| 2479 | if (*arg == '>') |
| 2480 | { |
| 2481 | ++arg; |
| 2482 | append = TRUE; |
| 2483 | } |
| 2484 | arg = skipwhite(arg); |
| 2485 | |
| 2486 | if (compile_assign_lhs(arg, lhs, CMD_redir, |
Bram Moolenaar | 97f8c10 | 2022-04-02 19:43:57 +0100 | [diff] [blame] | 2487 | FALSE, FALSE, FALSE, 1, cctx) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2488 | return NULL; |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 2489 | if (need_type(&t_string, lhs->lhs_member_type, FALSE, |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2490 | -1, 0, cctx, FALSE, FALSE) == FAIL) |
| 2491 | return NULL; |
Bram Moolenaar | 3558afe | 2022-10-13 16:12:57 +0100 | [diff] [blame] | 2492 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2493 | { |
Bram Moolenaar | 3558afe | 2022-10-13 16:12:57 +0100 | [diff] [blame] | 2494 | VIM_CLEAR(lhs->lhs_name); |
| 2495 | } |
| 2496 | else |
| 2497 | { |
| 2498 | generate_instr(cctx, ISN_REDIRSTART); |
| 2499 | lhs->lhs_append = append; |
| 2500 | if (lhs->lhs_has_index) |
| 2501 | { |
| 2502 | lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total); |
| 2503 | if (lhs->lhs_whole == NULL) |
| 2504 | return NULL; |
| 2505 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | return arg + lhs->lhs_varlen_total; |
| 2509 | } |
| 2510 | |
| 2511 | // other redirects are handled like at script level |
| 2512 | return compile_exec(line, eap, cctx); |
| 2513 | } |
| 2514 | |
| 2515 | #if defined(FEAT_QUICKFIX) || defined(PROTO) |
| 2516 | char_u * |
| 2517 | compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 2518 | { |
| 2519 | isn_T *isn; |
| 2520 | char_u *p; |
| 2521 | |
| 2522 | isn = generate_instr(cctx, ISN_CEXPR_AUCMD); |
| 2523 | if (isn == NULL) |
| 2524 | return NULL; |
| 2525 | isn->isn_arg.number = eap->cmdidx; |
| 2526 | |
| 2527 | p = eap->arg; |
| 2528 | if (compile_expr0(&p, cctx) == FAIL) |
| 2529 | return NULL; |
| 2530 | |
| 2531 | isn = generate_instr(cctx, ISN_CEXPR_CORE); |
| 2532 | if (isn == NULL) |
| 2533 | return NULL; |
| 2534 | isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T); |
| 2535 | if (isn->isn_arg.cexpr.cexpr_ref == NULL) |
| 2536 | return NULL; |
| 2537 | isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx; |
| 2538 | isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit; |
| 2539 | isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line)); |
| 2540 | |
| 2541 | return p; |
| 2542 | } |
| 2543 | #endif |
| 2544 | |
| 2545 | /* |
| 2546 | * Compile "return [expr]". |
| 2547 | * When "legacy" is TRUE evaluate [expr] with legacy syntax |
| 2548 | */ |
| 2549 | char_u * |
| 2550 | compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx) |
| 2551 | { |
| 2552 | char_u *p = arg; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2553 | type_T *stack_type; |
| 2554 | |
mityu | 500c444 | 2022-12-02 18:12:05 +0000 | [diff] [blame] | 2555 | if (*p != NUL && *p != '|' && *p != '\n' |
| 2556 | && (legacy || !vim9_comment_start(p))) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2557 | { |
Bram Moolenaar | 0bfa849 | 2022-01-22 12:27:04 +0000 | [diff] [blame] | 2558 | // For a lambda, "return expr" is always used, also when "expr" results |
| 2559 | // in a void. |
| 2560 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 2561 | && (cctx->ctx_ufunc->uf_flags & FC_LAMBDA) == 0) |
Bram Moolenaar | ef7aadb | 2022-01-18 18:46:07 +0000 | [diff] [blame] | 2562 | { |
| 2563 | emsg(_(e_returning_value_in_function_without_return_type)); |
| 2564 | return NULL; |
| 2565 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2566 | if (legacy) |
| 2567 | { |
| 2568 | int save_flags = cmdmod.cmod_flags; |
| 2569 | |
| 2570 | generate_LEGACY_EVAL(cctx, p); |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 2571 | if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, FALSE, -1, |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2572 | 0, cctx, FALSE, FALSE) == FAIL) |
| 2573 | return NULL; |
| 2574 | cmdmod.cmod_flags |= CMOD_LEGACY; |
| 2575 | (void)skip_expr(&p, NULL); |
| 2576 | cmdmod.cmod_flags = save_flags; |
| 2577 | } |
| 2578 | else |
| 2579 | { |
| 2580 | // compile return argument into instructions |
| 2581 | if (compile_expr0(&p, cctx) == FAIL) |
| 2582 | return NULL; |
| 2583 | } |
| 2584 | |
| 2585 | if (cctx->ctx_skip != SKIP_YES) |
| 2586 | { |
| 2587 | // "check_return_type" with uf_ret_type set to &t_unknown is used |
| 2588 | // for an inline function without a specified return type. Set the |
| 2589 | // return type here. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2590 | stack_type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2591 | if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL |
Bram Moolenaar | 1a572e9 | 2022-03-15 12:28:10 +0000 | [diff] [blame] | 2592 | || cctx->ctx_ufunc->uf_ret_type == &t_unknown)) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2593 | || (!check_return_type |
| 2594 | && cctx->ctx_ufunc->uf_ret_type == &t_unknown)) |
| 2595 | { |
| 2596 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
| 2597 | } |
| 2598 | else |
| 2599 | { |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 2600 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, FALSE, |
| 2601 | -1, 0, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2602 | return NULL; |
| 2603 | } |
| 2604 | } |
| 2605 | } |
| 2606 | else |
| 2607 | { |
| 2608 | // "check_return_type" cannot be TRUE, only used for a lambda which |
| 2609 | // always has an argument. |
| 2610 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 2611 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
| 2612 | { |
| 2613 | emsg(_(e_missing_return_value)); |
| 2614 | return NULL; |
| 2615 | } |
| 2616 | |
| 2617 | // No argument, return zero. |
| 2618 | generate_PUSHNR(cctx, 0); |
| 2619 | } |
| 2620 | |
Bram Moolenaar | 8abb584 | 2022-09-17 12:39:58 +0100 | [diff] [blame] | 2621 | // may need ENDLOOP when inside a :for or :while loop |
| 2622 | if (compile_find_scope(NULL, NULL, NULL, NULL, cctx) == FAIL) |
| 2623 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2624 | // Undo any command modifiers. |
| 2625 | generate_undo_cmdmods(cctx); |
| 2626 | |
| 2627 | if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL) |
| 2628 | return NULL; |
| 2629 | |
| 2630 | // "return val | endif" is possible |
| 2631 | return skipwhite(p); |
| 2632 | } |
| 2633 | |
| 2634 | /* |
| 2635 | * Check if the separator for a :global or :substitute command is OK. |
| 2636 | */ |
| 2637 | int |
| 2638 | check_global_and_subst(char_u *cmd, char_u *arg) |
| 2639 | { |
| 2640 | if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL) |
| 2641 | { |
| 2642 | semsg(_(e_separator_not_supported_str), arg); |
| 2643 | return FAIL; |
| 2644 | } |
| 2645 | if (VIM_ISWHITE(cmd[1])) |
| 2646 | { |
| 2647 | semsg(_(e_no_white_space_allowed_before_separator_str), cmd); |
| 2648 | return FAIL; |
| 2649 | } |
| 2650 | return OK; |
| 2651 | } |
| 2652 | |
| 2653 | |
| 2654 | #endif // defined(FEAT_EVAL) |