blob: 92605cff32e76fd1b96c69b9a3829bd96104c4ce [file] [log] [blame]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001/* 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
29current_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 Moolenaara275f2c2022-10-11 20:04:09 +010060 * 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 Moolenaardc7c3662021-12-20 15:04:29 +000062 */
63 static void
Bram Moolenaara275f2c2022-10-11 20:04:09 +010064unwind_locals(cctx_T *cctx, int new_top, int keep)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000065{
66 if (cctx->ctx_locals.ga_len > new_top)
Bram Moolenaara275f2c2022-10-11 20:04:09 +010067 for (int idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000068 {
Bram Moolenaara275f2c2022-10-11 20:04:09 +010069 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
70 VIM_CLEAR(lvar->lv_name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +000071 }
Bram Moolenaara275f2c2022-10-11 20:04:09 +010072 if (!keep)
73 cctx->ctx_locals.ga_len = new_top;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000074}
75
76/*
77 * Free all local variables.
78 */
79 void
80free_locals(cctx_T *cctx)
81{
Bram Moolenaara275f2c2022-10-11 20:04:09 +010082 unwind_locals(cctx, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +000083 ga_clear(&cctx->ctx_locals);
84}
85
86
87/*
88 * Check if "name" can be "unlet".
89 */
90 int
91check_vim9_unlet(char_u *name)
92{
Bram Moolenaardbdd16b2022-08-14 21:46:07 +010093 if (*name == NUL)
94 {
95 semsg(_(e_argument_required_for_str), "unlet");
96 return FAIL;
97 }
98
Bram Moolenaardc7c3662021-12-20 15:04:29 +000099 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
114compile_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 Moolenaar97f8c102022-04-02 19:43:57 +0100146 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, FALSE, 0, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000147
Bram Moolenaar2ef01d92022-01-06 21:38:11 +0000148 // Use the info in "lhs" to unlet the item at the index in the
149 // list or dict.
150 if (ret == OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000151 {
Bram Moolenaar2ef01d92022-01-06 21:38:11 +0000152 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 Moolenaardc7c3662021-12-20 15:04:29 +0000159 }
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
181compile_lock_unlock(
182 lval_T *lvp,
183 char_u *name_end,
184 exarg_T *eap,
Bram Moolenaar70c43d82022-01-26 21:01:15 +0000185 int deep,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000186 void *coookie)
187{
188 cctx_T *cctx = coookie;
189 int cc = *name_end;
190 char_u *p = lvp->ll_name;
191 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000192 char_u *buf;
193 isntype_T isn = ISN_EXEC;
Bram Moolenaard1d8f6b2022-08-14 21:28:32 +0100194 char *cmd = eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar";
Ernie Raelee865f32023-09-29 19:53:55 +0200195 int is_arg = FALSE;
196
197#ifdef LOG_LOCKVAR
198 ch_log(NULL, "LKVAR: compile_lock_unlock(): cookie %p, name %s",
199 coookie, p);
200#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000201
202 if (cctx->ctx_skip == SKIP_YES)
203 return OK;
204
Bram Moolenaard1d8f6b2022-08-14 21:28:32 +0100205 if (*p == NUL)
206 {
207 semsg(_(e_argument_required_for_str), cmd);
208 return FAIL;
209 }
210
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000211 // Cannot use :lockvar and :unlockvar on local variables.
212 if (p[1] != ':')
213 {
214 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
Ernie Rael64885642023-10-04 20:16:22 +0200215
216 // The most important point is that something like
217 // name[idx].member... needs to be resolved at runtime, get_lval(),
218 // starting from the root "name".
219
220 // These checks are reminiscent of the variable_exists function.
221 // But most of the matches require special handling.
222
223 // If bare name is is locally accessible, except for local var,
Ernie Raelee865f32023-09-29 19:53:55 +0200224 // then put it on the stack to use with ISN_LOCKUNLOCK.
225 // This could be v.memb, v[idx_key]; bare class variable,
Ernie Rael64885642023-10-04 20:16:22 +0200226 // function arg. The item on the stack, will be passed
227 // to ex_lockvar() indirectly and be used as the root for get_lval.
228 // A bare script variable name needs no special handling.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000229
Ernie Raelee865f32023-09-29 19:53:55 +0200230 char_u *name = NULL;
231 int len = end - p;
232
233 if (lookup_local(p, len, NULL, cctx) == OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000234 {
Ernie Raelee865f32023-09-29 19:53:55 +0200235 // Handle "this", "this.val", "anyvar[idx]"
236 if (*end != '.' && *end != '['
237 && (len != 4 || STRNCMP("this", p, len) != 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000238 {
239 emsg(_(e_cannot_lock_unlock_local_variable));
240 return FAIL;
241 }
Ernie Raelee865f32023-09-29 19:53:55 +0200242 // Push the local on the stack, could be "this".
243 name = p;
244#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +0200245 ch_log(NULL, "LKVAR: ... lookup_local: name %s", name);
Ernie Raelee865f32023-09-29 19:53:55 +0200246#endif
247 }
248 if (name == NULL)
249 {
250 class_T *cl;
251 if (cctx_class_member_idx(cctx, p, len, &cl) >= 0)
252 {
253 if (*end != '.' && *end != '[')
254 {
255 // Push the class of the bare class variable name
256 name = cl->class_name;
Yegappan Lakshmananb8523052023-10-08 19:07:39 +0200257 len = (int)STRLEN(name);
Ernie Raelee865f32023-09-29 19:53:55 +0200258#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +0200259 ch_log(NULL, "LKVAR: ... cctx_class_member: name %s",
Ernie Raelee865f32023-09-29 19:53:55 +0200260 name);
261#endif
262 }
263 }
264 }
265 if (name == NULL)
266 {
Ernie Raelee865f32023-09-29 19:53:55 +0200267 // Can lockvar any function arg.
Ernie Rael64885642023-10-04 20:16:22 +0200268 if (arg_exists(p, len, NULL, NULL, NULL, cctx) == OK)
Ernie Raelee865f32023-09-29 19:53:55 +0200269 {
270 name = p;
271 is_arg = TRUE;
272#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +0200273 ch_log(NULL, "LKVAR: ... arg_exists: name %s", name);
274#endif
275 }
276 }
277 if (name == NULL)
278 {
279 // No special handling for a bare script variable; but
280 // if followed by '[' or '.', it's a root for get_lval().
281 if (script_var_exists(p, len, cctx, NULL) == OK
282 && (*end == '.' || *end == '['))
283 {
284 name = p;
285#ifdef LOG_LOCKVAR
286 ch_log(NULL, "LKVAR: ... script_var_exists: name %s", name);
Ernie Raelee865f32023-09-29 19:53:55 +0200287#endif
288 }
289 }
290 if (name != NULL)
291 {
292#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +0200293 ch_log(NULL, "LKVAR: ... INS_LOCKUNLOCK %s", name);
Ernie Raelee865f32023-09-29 19:53:55 +0200294#endif
295 if (compile_load(&name, name + len, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000296 return FAIL;
297 isn = ISN_LOCKUNLOCK;
298 }
299 }
300
301 // Checking is done at runtime.
302 *name_end = NUL;
Ernie Raelee865f32023-09-29 19:53:55 +0200303 size_t len = name_end - p + 20;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000304 buf = alloc(len);
305 if (buf == NULL)
306 ret = FAIL;
307 else
308 {
Bram Moolenaare939f5e2022-01-26 21:32:59 +0000309 if (deep < 0)
310 vim_snprintf((char *)buf, len, "%s! %s", cmd, p);
311 else
312 vim_snprintf((char *)buf, len, "%s %d %s", cmd, deep, p);
Ernie Raelee865f32023-09-29 19:53:55 +0200313#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +0200314 ch_log(NULL, "LKVAR: ... buf %s", buf);
Ernie Raelee865f32023-09-29 19:53:55 +0200315#endif
316 if (isn == ISN_LOCKUNLOCK)
317 ret = generate_LOCKUNLOCK(cctx, buf, is_arg);
318 else
319 ret = generate_EXEC_copy(cctx, isn, buf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000320
321 vim_free(buf);
322 *name_end = cc;
323 }
324 return ret;
325}
326
327/*
328 * compile "unlet var", "lock var" and "unlock var"
329 * "arg" points to "var".
330 */
331 char_u *
332compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
333{
Bram Moolenaar70c43d82022-01-26 21:01:15 +0000334 int deep = 0;
335 char_u *p = arg;
336
337 if (eap->cmdidx != CMD_unlet)
338 {
339 if (eap->forceit)
340 deep = -1;
341 else if (vim_isdigit(*p))
342 {
343 deep = getdigits(&p);
344 p = skipwhite(p);
345 }
346 else
347 deep = 2;
348 }
349
350 ex_unletlock(eap, p, deep, GLV_NO_AUTOLOAD | GLV_COMPILING,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000351 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
352 cctx);
353 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
354}
355
356/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100357 * Generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
358 * "funcref_idx" is used for JUMP_WHILE_FALSE
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000359 */
360 static int
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100361compile_jump_to_end(
362 endlabel_T **el,
363 jumpwhen_T when,
364 int funcref_idx,
365 cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000366{
367 garray_T *instr = &cctx->ctx_instr;
368 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
369
370 if (endlabel == NULL)
371 return FAIL;
372 endlabel->el_next = *el;
373 *el = endlabel;
374 endlabel->el_end_label = instr->ga_len;
375
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100376 if (when == JUMP_WHILE_FALSE)
377 generate_WHILE(cctx, funcref_idx);
378 else
379 generate_JUMP(cctx, when, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000380 return OK;
381}
382
383 static void
384compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
385{
386 garray_T *instr = &cctx->ctx_instr;
387
388 while (*el != NULL)
389 {
390 endlabel_T *cur = (*el);
391 isn_T *isn;
392
393 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
394 isn->isn_arg.jump.jump_where = jump_where;
395 *el = cur->el_next;
396 vim_free(cur);
397 }
398}
399
400 static void
401compile_free_jump_to_end(endlabel_T **el)
402{
403 while (*el != NULL)
404 {
405 endlabel_T *cur = (*el);
406
407 *el = cur->el_next;
408 vim_free(cur);
409 }
410}
411
412/*
413 * Create a new scope and set up the generic items.
414 */
415 static scope_T *
416new_scope(cctx_T *cctx, scopetype_T type)
417{
418 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
419
420 if (scope == NULL)
421 return NULL;
422 scope->se_outer = cctx->ctx_scope;
423 cctx->ctx_scope = scope;
424 scope->se_type = type;
425 scope->se_local_count = cctx->ctx_locals.ga_len;
Bram Moolenaarcc341812022-09-19 15:54:34 +0100426 if (scope->se_outer != NULL)
427 scope->se_loop_depth = scope->se_outer->se_loop_depth;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000428 return scope;
429}
430
431/*
432 * Free the current scope and go back to the outer scope.
433 */
434 void
435drop_scope(cctx_T *cctx)
436{
437 scope_T *scope = cctx->ctx_scope;
438
439 if (scope == NULL)
440 {
441 iemsg("calling drop_scope() without a scope");
442 return;
443 }
444 cctx->ctx_scope = scope->se_outer;
445 switch (scope->se_type)
446 {
447 case IF_SCOPE:
448 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
449 case FOR_SCOPE:
450 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
451 case WHILE_SCOPE:
452 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
453 case TRY_SCOPE:
454 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
455 case NO_SCOPE:
456 case BLOCK_SCOPE:
457 break;
458 }
459 vim_free(scope);
460}
461
462 static int
463misplaced_cmdmod(cctx_T *cctx)
464{
465 garray_T *instr = &cctx->ctx_instr;
466
467 if (cctx->ctx_has_cmdmod
468 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
469 == ISN_CMDMOD)
470 {
471 emsg(_(e_misplaced_command_modifier));
472 return TRUE;
473 }
474 return FALSE;
475}
476
477/*
478 * compile "if expr"
479 *
480 * "if expr" Produces instructions:
481 * EVAL expr Push result of "expr"
482 * JUMP_IF_FALSE end
483 * ... body ...
484 * end:
485 *
486 * "if expr | else" Produces instructions:
487 * EVAL expr Push result of "expr"
488 * JUMP_IF_FALSE else
489 * ... body ...
490 * JUMP_ALWAYS end
491 * else:
492 * ... body ...
493 * end:
494 *
495 * "if expr1 | elseif expr2 | else" Produces instructions:
496 * EVAL expr Push result of "expr"
497 * JUMP_IF_FALSE elseif
498 * ... body ...
499 * JUMP_ALWAYS end
500 * elseif:
501 * EVAL expr Push result of "expr"
502 * JUMP_IF_FALSE else
503 * ... body ...
504 * JUMP_ALWAYS end
505 * else:
506 * ... body ...
507 * end:
508 */
509 char_u *
510compile_if(char_u *arg, cctx_T *cctx)
511{
512 char_u *p = arg;
513 garray_T *instr = &cctx->ctx_instr;
514 int instr_count = instr->ga_len;
515 scope_T *scope;
516 skip_T skip_save = cctx->ctx_skip;
517 ppconst_T ppconst;
518
519 CLEAR_FIELD(ppconst);
520 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
521 {
522 clear_ppconst(&ppconst);
523 return NULL;
524 }
525 if (!ends_excmd2(arg, skipwhite(p)))
526 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000527 semsg(_(e_trailing_characters_str), p);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000528 return NULL;
529 }
530 if (cctx->ctx_skip == SKIP_YES)
531 clear_ppconst(&ppconst);
532 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
533 {
534 int error = FALSE;
535 int v;
536
537 // The expression results in a constant.
538 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
539 clear_ppconst(&ppconst);
540 if (error)
541 return NULL;
542 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
543 }
544 else
545 {
546 // Not a constant, generate instructions for the expression.
547 cctx->ctx_skip = SKIP_UNKNOWN;
548 if (generate_ppconst(cctx, &ppconst) == FAIL)
549 return NULL;
550 if (bool_on_stack(cctx) == FAIL)
551 return NULL;
552 }
553
554 // CMDMOD_REV must come before the jump
555 generate_undo_cmdmods(cctx);
556
557 scope = new_scope(cctx, IF_SCOPE);
558 if (scope == NULL)
559 return NULL;
560 scope->se_skip_save = skip_save;
561 // "is_had_return" will be reset if any block does not end in :return
562 scope->se_u.se_if.is_had_return = TRUE;
563
564 if (cctx->ctx_skip == SKIP_UNKNOWN)
565 {
566 // "where" is set when ":elseif", "else" or ":endif" is found
567 scope->se_u.se_if.is_if_label = instr->ga_len;
568 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
569 }
570 else
571 scope->se_u.se_if.is_if_label = -1;
572
573#ifdef FEAT_PROFILE
574 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
575 && skip_save != SKIP_YES)
576 {
577 // generated a profile start, need to generate a profile end, since it
578 // won't be done after returning
579 cctx->ctx_skip = SKIP_NOT;
580 generate_instr(cctx, ISN_PROF_END);
581 cctx->ctx_skip = SKIP_YES;
582 }
583#endif
584
585 return p;
586}
587
588 char_u *
589compile_elseif(char_u *arg, cctx_T *cctx)
590{
591 char_u *p = arg;
592 garray_T *instr = &cctx->ctx_instr;
593 int instr_count;
594 isn_T *isn;
595 scope_T *scope = cctx->ctx_scope;
596 ppconst_T ppconst;
597 skip_T save_skip = cctx->ctx_skip;
598
599 if (scope == NULL || scope->se_type != IF_SCOPE)
600 {
601 emsg(_(e_elseif_without_if));
602 return NULL;
603 }
Bram Moolenaara275f2c2022-10-11 20:04:09 +0100604 unwind_locals(cctx, scope->se_local_count, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000605 if (!cctx->ctx_had_return)
606 scope->se_u.se_if.is_had_return = FALSE;
607
608 if (cctx->ctx_skip == SKIP_NOT)
609 {
610 // previous block was executed, this one and following will not
611 cctx->ctx_skip = SKIP_YES;
612 scope->se_u.se_if.is_seen_skip_not = TRUE;
613 }
614 if (scope->se_u.se_if.is_seen_skip_not)
615 {
616 // A previous block was executed, skip over expression and bail out.
617 // Do not count the "elseif" for profiling and cmdmod
618 instr->ga_len = current_instr_idx(cctx);
619
620 skip_expr_cctx(&p, cctx);
621 return p;
622 }
623
624 if (cctx->ctx_skip == SKIP_UNKNOWN)
625 {
626 int moved_cmdmod = FALSE;
627 int saved_debug = FALSE;
628 isn_T debug_isn;
629
630 // Move any CMDMOD instruction to after the jump
631 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
632 {
633 if (GA_GROW_FAILS(instr, 1))
634 return NULL;
635 ((isn_T *)instr->ga_data)[instr->ga_len] =
636 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
637 --instr->ga_len;
638 moved_cmdmod = TRUE;
639 }
640
641 // Remove the already generated ISN_DEBUG, it is written below the
642 // ISN_FOR instruction.
643 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
644 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
645 .isn_type == ISN_DEBUG)
646 {
647 --instr->ga_len;
648 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
649 saved_debug = TRUE;
650 }
651
652 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100653 JUMP_ALWAYS, 0, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000654 return NULL;
655 // previous "if" or "elseif" jumps here
656 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
657 isn->isn_arg.jump.jump_where = instr->ga_len;
658
659 if (moved_cmdmod)
660 ++instr->ga_len;
661
662 if (saved_debug)
663 {
664 // move the debug instruction here
665 if (GA_GROW_FAILS(instr, 1))
666 return NULL;
667 ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
668 ++instr->ga_len;
669 }
670 }
671
672 // compile "expr"; if we know it evaluates to FALSE skip the block
673 CLEAR_FIELD(ppconst);
674 if (cctx->ctx_skip == SKIP_YES)
675 {
676 cctx->ctx_skip = SKIP_UNKNOWN;
677#ifdef FEAT_PROFILE
678 if (cctx->ctx_compile_type == CT_PROFILE)
679 // the previous block was skipped, need to profile this line
680 generate_instr(cctx, ISN_PROF_START);
681#endif
682 if (cctx->ctx_compile_type == CT_DEBUG)
683 // the previous block was skipped, may want to debug this line
684 generate_instr_debug(cctx);
685 }
686
687 instr_count = instr->ga_len;
688 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
689 {
690 clear_ppconst(&ppconst);
691 return NULL;
692 }
693 cctx->ctx_skip = save_skip;
694 if (!ends_excmd2(arg, skipwhite(p)))
695 {
696 clear_ppconst(&ppconst);
Bram Moolenaar74409f62022-01-01 15:58:22 +0000697 semsg(_(e_trailing_characters_str), p);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000698 return NULL;
699 }
700 if (scope->se_skip_save == SKIP_YES)
701 clear_ppconst(&ppconst);
702 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
703 {
704 int error = FALSE;
705 int v;
706
707 // The expression result is a constant.
708 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
709 if (error)
710 {
711 clear_ppconst(&ppconst);
712 return NULL;
713 }
714 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
715 clear_ppconst(&ppconst);
716 scope->se_u.se_if.is_if_label = -1;
717 }
718 else
719 {
720 // Not a constant, generate instructions for the expression.
721 cctx->ctx_skip = SKIP_UNKNOWN;
722 if (generate_ppconst(cctx, &ppconst) == FAIL)
723 return NULL;
724 if (bool_on_stack(cctx) == FAIL)
725 return NULL;
726
727 // CMDMOD_REV must come before the jump
728 generate_undo_cmdmods(cctx);
729
730 // "where" is set when ":elseif", "else" or ":endif" is found
731 scope->se_u.se_if.is_if_label = instr->ga_len;
732 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
733 }
734
735 return p;
736}
737
738 char_u *
739compile_else(char_u *arg, cctx_T *cctx)
740{
741 char_u *p = arg;
742 garray_T *instr = &cctx->ctx_instr;
743 isn_T *isn;
744 scope_T *scope = cctx->ctx_scope;
745
746 if (scope == NULL || scope->se_type != IF_SCOPE)
747 {
748 emsg(_(e_else_without_if));
749 return NULL;
750 }
Bram Moolenaara275f2c2022-10-11 20:04:09 +0100751 unwind_locals(cctx, scope->se_local_count, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000752 if (!cctx->ctx_had_return)
753 scope->se_u.se_if.is_had_return = FALSE;
754 scope->se_u.se_if.is_seen_else = TRUE;
755
756#ifdef FEAT_PROFILE
757 if (cctx->ctx_compile_type == CT_PROFILE)
758 {
759 if (cctx->ctx_skip == SKIP_NOT
760 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
761 .isn_type == ISN_PROF_START)
762 // the previous block was executed, do not count "else" for
763 // profiling
764 --instr->ga_len;
765 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
766 {
767 // the previous block was not executed, this one will, do count the
768 // "else" for profiling
769 cctx->ctx_skip = SKIP_NOT;
770 generate_instr(cctx, ISN_PROF_END);
771 generate_instr(cctx, ISN_PROF_START);
772 cctx->ctx_skip = SKIP_YES;
773 }
774 }
775#endif
776
777 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
778 {
779 // jump from previous block to the end, unless the else block is empty
780 if (cctx->ctx_skip == SKIP_UNKNOWN)
781 {
782 if (!cctx->ctx_had_return
783 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100784 JUMP_ALWAYS, 0, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000785 return NULL;
786 }
787
788 if (cctx->ctx_skip == SKIP_UNKNOWN)
789 {
790 if (scope->se_u.se_if.is_if_label >= 0)
791 {
792 // previous "if" or "elseif" jumps here
793 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
794 isn->isn_arg.jump.jump_where = instr->ga_len;
795 scope->se_u.se_if.is_if_label = -1;
796 }
797 }
798
799 if (cctx->ctx_skip != SKIP_UNKNOWN)
800 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
801 }
802
803 return p;
804}
805
806 char_u *
807compile_endif(char_u *arg, cctx_T *cctx)
808{
809 scope_T *scope = cctx->ctx_scope;
810 ifscope_T *ifscope;
811 garray_T *instr = &cctx->ctx_instr;
812 isn_T *isn;
813
814 if (misplaced_cmdmod(cctx))
815 return NULL;
816
817 if (scope == NULL || scope->se_type != IF_SCOPE)
818 {
819 emsg(_(e_endif_without_if));
820 return NULL;
821 }
822 ifscope = &scope->se_u.se_if;
Bram Moolenaara275f2c2022-10-11 20:04:09 +0100823 unwind_locals(cctx, scope->se_local_count, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000824 if (!cctx->ctx_had_return)
825 ifscope->is_had_return = FALSE;
826
827 if (scope->se_u.se_if.is_if_label >= 0)
828 {
829 // previous "if" or "elseif" jumps here
830 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
831 isn->isn_arg.jump.jump_where = instr->ga_len;
832 }
833 // Fill in the "end" label in jumps at the end of the blocks.
834 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
835
836#ifdef FEAT_PROFILE
837 // even when skipping we count the endif as executed, unless the block it's
838 // in is skipped
839 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
840 && scope->se_skip_save != SKIP_YES)
841 {
842 cctx->ctx_skip = SKIP_NOT;
843 generate_instr(cctx, ISN_PROF_START);
844 }
845#endif
846 cctx->ctx_skip = scope->se_skip_save;
847
848 // If all the blocks end in :return and there is an :else then the
849 // had_return flag is set.
850 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
851
852 drop_scope(cctx);
853 return arg;
854}
855
856/*
Bram Moolenaar8abb5842022-09-17 12:39:58 +0100857 * Save the info needed for ENDLOOP. Used by :for and :while.
858 */
859 static void
860compile_fill_loop_info(loop_info_T *loop_info, int funcref_idx, cctx_T *cctx)
861{
862 loop_info->li_funcref_idx = funcref_idx;
863 loop_info->li_local_count = cctx->ctx_locals.ga_len;
864 loop_info->li_closure_count = cctx->ctx_closure_count;
865}
866
867/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000868 * Compile "for var in expr":
869 *
870 * Produces instructions:
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100871 * STORE -1 in loop-idx Set index to -1
872 * EVAL expr Result of "expr" on top of stack
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000873 * top: FOR loop-idx, end Increment index, use list on bottom of stack
874 * - if beyond end, jump to "end"
875 * - otherwise get item from list and push it
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100876 * - store ec_funcrefs in var "loop-idx" + 1
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000877 * STORE var Store item in "var"
878 * ... body ...
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100879 * ENDLOOP funcref-idx off count Only if closure uses local var
880 * JUMP top Jump back to repeat
881 * end: DROP Drop the result of "expr"
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000882 *
883 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
884 * UNPACK 2 Split item in 2
885 * STORE var1 Store item in "var1"
886 * STORE var2 Store item in "var2"
887 */
888 char_u *
889compile_for(char_u *arg_start, cctx_T *cctx)
890{
891 char_u *arg;
892 char_u *arg_end;
893 char_u *name = NULL;
894 char_u *p;
895 char_u *wp;
896 int var_count = 0;
897 int var_list = FALSE;
898 int semicolon = FALSE;
899 size_t varlen;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000900 garray_T *instr = &cctx->ctx_instr;
901 scope_T *scope;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100902 forscope_T *forscope;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000903 lvar_T *loop_lvar; // loop iteration variable
Bram Moolenaarcc341812022-09-19 15:54:34 +0100904 int loop_lvar_idx;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100905 lvar_T *funcref_lvar;
Bram Moolenaarcc341812022-09-19 15:54:34 +0100906 int funcref_lvar_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000907 lvar_T *var_lvar; // variable for "var"
908 type_T *vartype;
909 type_T *item_type = &t_any;
910 int idx;
911 int prev_lnum = cctx->ctx_prev_lnum;
912
913 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
914 if (p == NULL)
915 return NULL;
916 if (var_count == 0)
917 var_count = 1;
918 else
919 var_list = TRUE; // can also be a list of one variable
920
921 // consume "in"
922 wp = p;
923 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
924 return NULL;
925 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
926 {
927 if (*p == ':' && wp != p)
928 semsg(_(e_no_white_space_allowed_before_colon_str), p);
929 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +0000930 emsg(_(e_missing_in_after_for));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000931 return NULL;
932 }
933 wp = p + 2;
934 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
935 return NULL;
936
Bram Moolenaar2b4ecc22022-01-02 14:08:18 +0000937 // Find the already generated ISN_DEBUG to get the line number for the
938 // instruction written below the ISN_FOR instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000939 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
940 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
941 .isn_type == ISN_DEBUG)
942 {
Bram Moolenaar2b4ecc22022-01-02 14:08:18 +0000943 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000944 .isn_arg.debug.dbg_break_lnum;
945 }
946
947 scope = new_scope(cctx, FOR_SCOPE);
948 if (scope == NULL)
949 return NULL;
Bram Moolenaarcc341812022-09-19 15:54:34 +0100950 if (scope->se_loop_depth == MAX_LOOP_DEPTH)
951 {
952 emsg(_(e_loop_nesting_too_deep));
953 return NULL;
954 }
955 ++scope->se_loop_depth;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100956 forscope = &scope->se_u.se_for;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000957
958 // Reserve a variable to store the loop iteration counter and initialize it
959 // to -1.
Bram Moolenaar6586a012022-09-30 11:04:50 +0100960 loop_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000961 if (loop_lvar == NULL)
962 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000963 drop_scope(cctx);
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100964 return NULL; // out of memory
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000965 }
Bram Moolenaarcc341812022-09-19 15:54:34 +0100966 // get the index before a following reserve_local() makes the lval invalid
967 loop_lvar_idx = loop_lvar->lv_idx;
968 generate_STORENR(cctx, loop_lvar_idx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000969
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100970 // Reserve a variable to store ec_funcrefs.ga_len, used in ISN_ENDLOOP.
971 // The variable index is always the loop var index plus one.
972 // It is not used when no closures are encountered, we don't know yet.
Bram Moolenaar6586a012022-09-30 11:04:50 +0100973 funcref_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number);
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100974 if (funcref_lvar == NULL)
975 {
976 drop_scope(cctx);
977 return NULL; // out of memory
978 }
Bram Moolenaarcc341812022-09-19 15:54:34 +0100979 // get the index before a following reserve_local() makes the lval invalid
980 funcref_lvar_idx = funcref_lvar->lv_idx;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100981
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000982 // compile "expr", it remains on the stack until "endfor"
983 arg = p;
984 if (compile_expr0(&arg, cctx) == FAIL)
985 {
986 drop_scope(cctx);
987 return NULL;
988 }
989 arg_end = arg;
990
991 if (cctx->ctx_skip != SKIP_YES)
992 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000993 // If we know the type of "var" and it is not a supported type we can
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000994 // give an error now.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000995 vartype = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000996 if (vartype->tt_type != VAR_LIST
997 && vartype->tt_type != VAR_STRING
998 && vartype->tt_type != VAR_BLOB
999 && vartype->tt_type != VAR_ANY
1000 && vartype->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001001 {
1002 semsg(_(e_for_loop_on_str_not_supported),
1003 vartype_name(vartype->tt_type));
1004 drop_scope(cctx);
1005 return NULL;
1006 }
1007
1008 if (vartype->tt_type == VAR_STRING)
1009 item_type = &t_string;
1010 else if (vartype->tt_type == VAR_BLOB)
1011 item_type = &t_number;
1012 else if (vartype->tt_type == VAR_LIST
1013 && vartype->tt_member->tt_type != VAR_ANY)
1014 {
1015 if (!var_list)
1016 item_type = vartype->tt_member;
1017 else if (vartype->tt_member->tt_type == VAR_LIST
1018 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
1019 item_type = vartype->tt_member->tt_member;
1020 }
1021
1022 // CMDMOD_REV must come before the FOR instruction.
1023 generate_undo_cmdmods(cctx);
1024
1025 // "for_end" is set when ":endfor" is found
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001026 forscope->fs_top_label = current_instr_idx(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001027
Bram Moolenaar2b4ecc22022-01-02 14:08:18 +00001028 if (cctx->ctx_compile_type == CT_DEBUG)
1029 {
1030 int save_prev_lnum = cctx->ctx_prev_lnum;
1031 isn_T *isn;
1032
1033 // Add ISN_DEBUG here, before deciding to end the loop. There will
1034 // be another ISN_DEBUG before the next instruction.
1035 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
1036 // Increment the variable count so that the loop variable can be
1037 // inspected.
1038 cctx->ctx_prev_lnum = prev_lnum;
1039 isn = generate_instr_debug(cctx);
1040 ++isn->isn_arg.debug.dbg_var_names_len;
1041 cctx->ctx_prev_lnum = save_prev_lnum;
1042 }
1043
Bram Moolenaarcc341812022-09-19 15:54:34 +01001044 generate_FOR(cctx, loop_lvar_idx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001045
1046 arg = arg_start;
1047 if (var_list)
1048 {
1049 generate_UNPACK(cctx, var_count, semicolon);
1050 arg = skipwhite(arg + 1); // skip white after '['
1051
Bram Moolenaar078a4612022-01-04 15:17:03 +00001052 // drop the list item
1053 --cctx->ctx_type_stack.ga_len;
1054
1055 // add type of the items
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001056 for (idx = 0; idx < var_count; ++idx)
1057 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001058 type_T *type = (semicolon && idx == 0) ? vartype : item_type;
1059
1060 if (push_type_stack(cctx, type) == FAIL)
1061 {
1062 drop_scope(cctx);
1063 return NULL;
1064 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001065 }
1066 }
1067
1068 for (idx = 0; idx < var_count; ++idx)
1069 {
1070 assign_dest_T dest = dest_local;
1071 int opt_flags = 0;
1072 int vimvaridx = -1;
1073 type_T *type = &t_any;
1074 type_T *lhs_type = &t_any;
1075 where_T where = WHERE_INIT;
1076
1077 p = skip_var_one(arg, FALSE);
1078 varlen = p - arg;
1079 name = vim_strnsave(arg, varlen);
1080 if (name == NULL)
1081 goto failed;
Bram Moolenaarce93d162023-01-30 21:12:34 +00001082 if (*skipwhite(p) == ':')
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001083 {
Bram Moolenaarce93d162023-01-30 21:12:34 +00001084 if (VIM_ISWHITE(*p))
1085 {
1086 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1087 goto failed;
1088 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001089 p = skipwhite(p + 1);
1090 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
1091 }
1092
1093 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
1094 &vimvaridx, &type, cctx) == FAIL)
1095 goto failed;
1096 if (dest != dest_local)
1097 {
1098 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00001099 type, name, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001100 goto failed;
1101 }
1102 else if (varlen == 1 && *arg == '_')
1103 {
1104 // Assigning to "_": drop the value.
1105 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
1106 goto failed;
1107 }
1108 else
1109 {
1110 // Script var is not supported.
1111 if (STRNCMP(name, "s:", 2) == 0)
1112 {
1113 emsg(_(e_cannot_use_script_variable_in_for_loop));
1114 goto failed;
1115 }
1116
1117 if (!valid_varname(arg, (int)varlen, FALSE))
1118 goto failed;
1119 if (lookup_local(arg, varlen, NULL, cctx) == OK)
1120 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001121 semsg(_(e_variable_already_declared_str), arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001122 goto failed;
1123 }
1124
1125 // Reserve a variable to store "var".
LemonBoyc5d27442023-08-19 13:02:35 +02001126 if (var_list)
1127 {
1128 where.wt_index = idx + 1;
1129 where.wt_kind = WT_VARIABLE;
1130 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001131 if (lhs_type == &t_any)
1132 lhs_type = item_type;
1133 else if (item_type != &t_unknown
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001134 && need_type_where(item_type, lhs_type, FALSE, -1,
Bram Moolenaara1c51952022-02-02 16:20:26 +00001135 where, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001136 goto failed;
Bram Moolenaar159b2d52022-10-11 21:41:25 +01001137 var_lvar = reserve_local(cctx, arg, varlen, ASSIGN_FINAL,
Bram Moolenaar6586a012022-09-30 11:04:50 +01001138 lhs_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001139 if (var_lvar == NULL)
1140 // out of memory or used as an argument
1141 goto failed;
1142
1143 if (semicolon && idx == var_count - 1)
1144 var_lvar->lv_type = vartype;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001145 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
1146 }
1147
1148 if (*p == ',' || *p == ';')
1149 ++p;
1150 arg = skipwhite(p);
1151 vim_free(name);
1152 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001153
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001154 // remember the number of variables and closures, used for ENDLOOP
Bram Moolenaarcc341812022-09-19 15:54:34 +01001155 compile_fill_loop_info(&forscope->fs_loop_info, funcref_lvar_idx, cctx);
1156 forscope->fs_loop_info.li_depth = scope->se_loop_depth - 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001157 }
1158
1159 return arg_end;
1160
1161failed:
1162 vim_free(name);
1163 drop_scope(cctx);
1164 return NULL;
1165}
1166
1167/*
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001168 * Used when ending a loop of :for and :while: Generate an ISN_ENDLOOP
1169 * instruction if any variable was declared that could be used by a new
1170 * closure.
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001171 */
1172 static int
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001173compile_loop_end(loop_info_T *loop_info, cctx_T *cctx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001174{
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001175 if (cctx->ctx_locals.ga_len > loop_info->li_local_count
1176 && cctx->ctx_closure_count > loop_info->li_closure_count)
Bram Moolenaarcc341812022-09-19 15:54:34 +01001177 return generate_ENDLOOP(cctx, loop_info);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001178 return OK;
1179}
1180
1181/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001182 * compile "endfor"
1183 */
1184 char_u *
1185compile_endfor(char_u *arg, cctx_T *cctx)
1186{
1187 garray_T *instr = &cctx->ctx_instr;
1188 scope_T *scope = cctx->ctx_scope;
1189 forscope_T *forscope;
1190 isn_T *isn;
1191
1192 if (misplaced_cmdmod(cctx))
1193 return NULL;
1194
1195 if (scope == NULL || scope->se_type != FOR_SCOPE)
1196 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001197 emsg(_(e_endfor_without_for));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001198 return NULL;
1199 }
1200 forscope = &scope->se_u.se_for;
1201 cctx->ctx_scope = scope->se_outer;
1202 if (cctx->ctx_skip != SKIP_YES)
1203 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001204 // Handle the case that any local variables were declared that might be
1205 // used in a closure.
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001206 if (compile_loop_end(&forscope->fs_loop_info, cctx) == FAIL)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001207 return NULL;
1208
Bram Moolenaara275f2c2022-10-11 20:04:09 +01001209 unwind_locals(cctx, scope->se_local_count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001210
1211 // At end of ":for" scope jump back to the FOR instruction.
1212 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
1213
1214 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar2b4ecc22022-01-02 14:08:18 +00001215 // In debug mode an ISN_DEBUG was inserted.
1216 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label
1217 + (cctx->ctx_compile_type == CT_DEBUG ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001218 isn->isn_arg.forloop.for_end = instr->ga_len;
1219
1220 // Fill in the "end" label any BREAK statements
1221 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
1222
1223 // Below the ":for" scope drop the "expr" list from the stack.
1224 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
1225 return NULL;
1226 }
1227
1228 vim_free(scope);
1229
1230 return arg;
1231}
1232
1233/*
1234 * compile "while expr"
1235 *
1236 * Produces instructions:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001237 * top: EVAL expr Push result of "expr"
1238 * WHILE funcref-idx end Jump if false
1239 * ... body ...
1240 * ENDLOOP funcref-idx off count only if closure uses local var
1241 * JUMP top Jump back to repeat
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001242 * end:
1243 *
1244 */
1245 char_u *
1246compile_while(char_u *arg, cctx_T *cctx)
1247{
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001248 char_u *p = arg;
1249 scope_T *scope;
1250 whilescope_T *whilescope;
1251 lvar_T *funcref_lvar;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001252 int funcref_lvar_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001253
1254 scope = new_scope(cctx, WHILE_SCOPE);
1255 if (scope == NULL)
1256 return NULL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001257 if (scope->se_loop_depth == MAX_LOOP_DEPTH)
1258 {
1259 emsg(_(e_loop_nesting_too_deep));
1260 return NULL;
1261 }
1262 ++scope->se_loop_depth;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001263 whilescope = &scope->se_u.se_while;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001264
1265 // "endwhile" jumps back here, one before when profiling or using cmdmods
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001266 whilescope->ws_top_label = current_instr_idx(cctx);
1267
1268 // Reserve a variable to store ec_funcrefs.ga_len, used in ISN_ENDLOOP.
1269 // It is not used when no closures are encountered, we don't know yet.
Bram Moolenaar6586a012022-09-30 11:04:50 +01001270 funcref_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001271 if (funcref_lvar == NULL)
1272 {
1273 drop_scope(cctx);
1274 return NULL; // out of memory
1275 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001276 // get the index before a following reserve_local() makes the lval invalid
1277 funcref_lvar_idx = funcref_lvar->lv_idx;
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001278
1279 // remember the number of variables and closures, used for ENDLOOP
Bram Moolenaarcc341812022-09-19 15:54:34 +01001280 compile_fill_loop_info(&whilescope->ws_loop_info, funcref_lvar_idx, cctx);
1281 whilescope->ws_loop_info.li_depth = scope->se_loop_depth - 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001282
1283 // compile "expr"
1284 if (compile_expr0(&p, cctx) == FAIL)
1285 return NULL;
1286
1287 if (!ends_excmd2(arg, skipwhite(p)))
1288 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001289 semsg(_(e_trailing_characters_str), p);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001290 return NULL;
1291 }
1292
1293 if (cctx->ctx_skip != SKIP_YES)
1294 {
1295 if (bool_on_stack(cctx) == FAIL)
1296 return FAIL;
1297
1298 // CMDMOD_REV must come before the jump
1299 generate_undo_cmdmods(cctx);
1300
1301 // "while_end" is set when ":endwhile" is found
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001302 if (compile_jump_to_end(&whilescope->ws_end_label,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001303 JUMP_WHILE_FALSE, funcref_lvar_idx, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001304 return FAIL;
1305 }
1306
1307 return p;
1308}
1309
1310/*
1311 * compile "endwhile"
1312 */
1313 char_u *
1314compile_endwhile(char_u *arg, cctx_T *cctx)
1315{
1316 scope_T *scope = cctx->ctx_scope;
1317 garray_T *instr = &cctx->ctx_instr;
1318
1319 if (misplaced_cmdmod(cctx))
1320 return NULL;
1321 if (scope == NULL || scope->se_type != WHILE_SCOPE)
1322 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001323 emsg(_(e_endwhile_without_while));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001324 return NULL;
1325 }
1326 cctx->ctx_scope = scope->se_outer;
1327 if (cctx->ctx_skip != SKIP_YES)
1328 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001329 whilescope_T *whilescope = &scope->se_u.se_while;
1330
1331 // Handle the case that any local variables were declared that might be
1332 // used in a closure.
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001333 if (compile_loop_end(&whilescope->ws_loop_info, cctx) == FAIL)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001334 return NULL;
1335
Bram Moolenaara275f2c2022-10-11 20:04:09 +01001336 unwind_locals(cctx, scope->se_local_count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001337
1338#ifdef FEAT_PROFILE
1339 // count the endwhile before jumping
1340 may_generate_prof_end(cctx, cctx->ctx_lnum);
1341#endif
1342
1343 // At end of ":for" scope jump back to the FOR instruction.
1344 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
1345
1346 // Fill in the "end" label in the WHILE statement so it can jump here.
1347 // And in any jumps for ":break"
1348 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
1349 instr->ga_len, cctx);
1350 }
1351
1352 vim_free(scope);
1353
1354 return arg;
1355}
1356
1357/*
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001358 * Get the current information about variables declared inside a loop.
Bram Moolenaarcc341812022-09-19 15:54:34 +01001359 * Returns TRUE if there are any and fills "lvi".
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001360 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01001361 int
1362get_loop_var_info(cctx_T *cctx, loopvarinfo_T *lvi)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001363{
1364 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001365 int prev_local_count = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001366
Bram Moolenaarcc341812022-09-19 15:54:34 +01001367 CLEAR_POINTER(lvi);
1368 for (;;)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001369 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001370 loop_info_T *loopinfo;
1371 int cur_local_last;
1372 int start_local_count;
1373
1374 while (scope != NULL && scope->se_type != WHILE_SCOPE
1375 && scope->se_type != FOR_SCOPE)
1376 scope = scope->se_outer;
1377 if (scope == NULL)
1378 break;
1379
1380 if (scope->se_type == WHILE_SCOPE)
1381 {
1382 loopinfo = &scope->se_u.se_while.ws_loop_info;
1383 // :while reserves one variable for funcref count
1384 cur_local_last = loopinfo->li_local_count - 1;
1385 }
1386 else
1387 {
1388 loopinfo = &scope->se_u.se_for.fs_loop_info;
1389 // :for reserves three variable: loop count, funcref count and loop
1390 // var
1391 cur_local_last = loopinfo->li_local_count - 3;
1392 }
1393
1394 start_local_count = loopinfo->li_local_count;
1395 if (cctx->ctx_locals.ga_len > start_local_count)
1396 {
1397 lvi->lvi_loop[loopinfo->li_depth].var_idx =
1398 (short)start_local_count;
1399 lvi->lvi_loop[loopinfo->li_depth].var_count =
1400 (short)(cctx->ctx_locals.ga_len - start_local_count
1401 - prev_local_count);
1402 if (lvi->lvi_depth == 0)
1403 lvi->lvi_depth = loopinfo->li_depth + 1;
1404 }
1405
1406 scope = scope->se_outer;
1407 prev_local_count = cctx->ctx_locals.ga_len - cur_local_last;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001408 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001409 return lvi->lvi_depth > 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001410}
1411
1412/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01001413 * Get the index of the variable "idx" in a loop, if any.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001414 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01001415 void
1416get_loop_var_idx(cctx_T *cctx, int idx, lvar_T *lvar)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001417{
Bram Moolenaarcc341812022-09-19 15:54:34 +01001418 loopvarinfo_T lvi;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001419
Bram Moolenaarcc341812022-09-19 15:54:34 +01001420 lvar->lv_loop_depth = -1;
1421 lvar->lv_loop_idx = -1;
1422 if (get_loop_var_info(cctx, &lvi))
1423 {
1424 int depth;
1425
1426 for (depth = lvi.lvi_depth - 1; depth >= 0; --depth)
1427 if (idx >= lvi.lvi_loop[depth].var_idx
1428 && idx < lvi.lvi_loop[depth].var_idx
1429 + lvi.lvi_loop[depth].var_count)
1430 {
1431 lvar->lv_loop_depth = depth;
1432 lvar->lv_loop_idx = lvi.lvi_loop[depth].var_idx;
1433 return;
1434 }
1435 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001436}
1437
1438/*
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001439 * Common for :break, :continue and :return
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001440 */
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001441 static int
1442compile_find_scope(
1443 int *loop_label, // where to jump to or NULL
1444 endlabel_T ***el, // end label or NULL
1445 int *try_scopes, // :try scopes encountered or NULL
1446 char *error, // error to use when no scope found
1447 cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001448{
1449 scope_T *scope = cctx->ctx_scope;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001450
1451 for (;;)
1452 {
1453 if (scope == NULL)
1454 {
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001455 if (error != NULL)
1456 emsg(_(error));
1457 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001458 }
1459 if (scope->se_type == FOR_SCOPE)
1460 {
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001461 if (compile_loop_end(&scope->se_u.se_for.fs_loop_info, cctx)
1462 == FAIL)
1463 return FAIL;
1464 if (loop_label != NULL)
1465 *loop_label = scope->se_u.se_for.fs_top_label;
1466 if (el != NULL)
1467 *el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001468 break;
1469 }
1470 if (scope->se_type == WHILE_SCOPE)
1471 {
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001472 if (compile_loop_end(&scope->se_u.se_while.ws_loop_info, cctx)
1473 == FAIL)
1474 return FAIL;
1475 if (loop_label != NULL)
1476 *loop_label = scope->se_u.se_while.ws_top_label;
1477 if (el != NULL)
1478 *el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001479 break;
1480 }
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001481 if (try_scopes != NULL && scope->se_type == TRY_SCOPE)
1482 ++*try_scopes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001483 scope = scope->se_outer;
1484 }
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001485 return OK;
1486}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001487
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001488/*
1489 * compile "continue"
1490 */
1491 char_u *
1492compile_continue(char_u *arg, cctx_T *cctx)
1493{
1494 int try_scopes = 0;
1495 int loop_label;
1496
1497 if (compile_find_scope(&loop_label, NULL, &try_scopes,
1498 e_continue_without_while_or_for, cctx) == FAIL)
1499 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001500 if (try_scopes > 0)
1501 // Inside one or more try/catch blocks we first need to jump to the
1502 // "finally" or "endtry" to cleanup.
1503 generate_TRYCONT(cctx, try_scopes, loop_label);
1504 else
1505 // Jump back to the FOR or WHILE instruction.
1506 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
1507
1508 return arg;
1509}
1510
1511/*
1512 * compile "break"
1513 */
1514 char_u *
1515compile_break(char_u *arg, cctx_T *cctx)
1516{
Bram Moolenaar873f8242022-03-10 21:53:44 +00001517 int try_scopes = 0;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001518 endlabel_T **el;
1519
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001520 if (compile_find_scope(NULL, &el, &try_scopes,
1521 e_break_without_while_or_for, cctx) == FAIL)
1522 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001523
Bram Moolenaar3f45d672023-02-27 22:06:51 +00001524 if (cctx->ctx_skip == SKIP_YES)
1525 return arg;
1526
Bram Moolenaar873f8242022-03-10 21:53:44 +00001527 if (try_scopes > 0)
1528 // Inside one or more try/catch blocks we first need to jump to the
1529 // "finally" or "endtry" to cleanup. Then come to the next JUMP
dundargocc57b5bc2022-11-02 13:30:51 +00001530 // instruction, which we don't know the index of yet.
Bram Moolenaar873f8242022-03-10 21:53:44 +00001531 generate_TRYCONT(cctx, try_scopes, cctx->ctx_instr.ga_len + 1);
1532
1533 // Jump to the end of the FOR or WHILE loop. The instruction index will be
1534 // filled in later.
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001535 if (compile_jump_to_end(el, JUMP_ALWAYS, 0, cctx) == FAIL)
Bram Moolenaar3f45d672023-02-27 22:06:51 +00001536 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001537
1538 return arg;
1539}
1540
1541/*
1542 * compile "{" start of block
1543 */
1544 char_u *
1545compile_block(char_u *arg, cctx_T *cctx)
1546{
1547 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
1548 return NULL;
1549 return skipwhite(arg + 1);
1550}
1551
1552/*
1553 * compile end of block: drop one scope
1554 */
1555 void
1556compile_endblock(cctx_T *cctx)
1557{
1558 scope_T *scope = cctx->ctx_scope;
1559
1560 cctx->ctx_scope = scope->se_outer;
Bram Moolenaara275f2c2022-10-11 20:04:09 +01001561 unwind_locals(cctx, scope->se_local_count, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001562 vim_free(scope);
1563}
1564
1565/*
1566 * Compile "try".
1567 * Creates a new scope for the try-endtry, pointing to the first catch and
1568 * finally.
1569 * Creates another scope for the "try" block itself.
1570 * TRY instruction sets up exception handling at runtime.
1571 *
1572 * "try"
1573 * TRY -> catch1, -> finally push trystack entry
1574 * ... try block
1575 * "throw {exception}"
1576 * EVAL {exception}
1577 * THROW create exception
1578 * ... try block
1579 * " catch {expr}"
1580 * JUMP -> finally
1581 * catch1: PUSH exception
1582 * EVAL {expr}
1583 * MATCH
1584 * JUMP nomatch -> catch2
1585 * CATCH remove exception
1586 * ... catch block
1587 * " catch"
1588 * JUMP -> finally
1589 * catch2: CATCH remove exception
1590 * ... catch block
1591 * " finally"
1592 * finally:
1593 * ... finally block
1594 * " endtry"
1595 * ENDTRY pop trystack entry, may rethrow
1596 */
1597 char_u *
1598compile_try(char_u *arg, cctx_T *cctx)
1599{
1600 garray_T *instr = &cctx->ctx_instr;
1601 scope_T *try_scope;
1602 scope_T *scope;
1603
1604 if (misplaced_cmdmod(cctx))
1605 return NULL;
1606
1607 // scope that holds the jumps that go to catch/finally/endtry
1608 try_scope = new_scope(cctx, TRY_SCOPE);
1609 if (try_scope == NULL)
1610 return NULL;
1611
1612 if (cctx->ctx_skip != SKIP_YES)
1613 {
1614 isn_T *isn;
1615
1616 // "try_catch" is set when the first ":catch" is found or when no catch
1617 // is found and ":finally" is found.
1618 // "try_finally" is set when ":finally" is found
1619 // "try_endtry" is set when ":endtry" is found
1620 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
1621 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
1622 return NULL;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001623 isn->isn_arg.tryref.try_ref = ALLOC_CLEAR_ONE(tryref_T);
1624 if (isn->isn_arg.tryref.try_ref == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001625 return NULL;
1626 }
1627
1628 // scope for the try block itself
1629 scope = new_scope(cctx, BLOCK_SCOPE);
1630 if (scope == NULL)
1631 return NULL;
1632
1633 return arg;
1634}
1635
1636/*
1637 * Compile "catch {expr}".
1638 */
1639 char_u *
1640compile_catch(char_u *arg, cctx_T *cctx UNUSED)
1641{
1642 scope_T *scope = cctx->ctx_scope;
1643 garray_T *instr = &cctx->ctx_instr;
1644 char_u *p;
1645 isn_T *isn;
1646
1647 if (misplaced_cmdmod(cctx))
1648 return NULL;
1649
1650 // end block scope from :try or :catch
1651 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1652 compile_endblock(cctx);
1653 scope = cctx->ctx_scope;
1654
1655 // Error if not in a :try scope
1656 if (scope == NULL || scope->se_type != TRY_SCOPE)
1657 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001658 emsg(_(e_catch_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001659 return NULL;
1660 }
1661
Bram Moolenaar9d383f32023-05-14 21:38:12 +01001662 if (scope->se_u.se_try.ts_caught_all
1663 && !ignore_unreachable_code_for_testing)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001664 {
1665 emsg(_(e_catch_unreachable_after_catch_all));
1666 return NULL;
1667 }
Bram Moolenaar53c29612022-01-12 16:18:18 +00001668 if (!cctx->ctx_had_return)
1669 scope->se_u.se_try.ts_no_return = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001670
1671 if (cctx->ctx_skip != SKIP_YES)
1672 {
1673#ifdef FEAT_PROFILE
1674 // the profile-start should be after the jump
1675 if (cctx->ctx_compile_type == CT_PROFILE
1676 && instr->ga_len > 0
1677 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
1678 .isn_type == ISN_PROF_START)
1679 --instr->ga_len;
1680#endif
1681 // Jump from end of previous block to :finally or :endtry
1682 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001683 JUMP_ALWAYS, 0, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001684 return NULL;
1685
1686 // End :try or :catch scope: set value in ISN_TRY instruction
1687 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001688 if (isn->isn_arg.tryref.try_ref->try_catch == 0)
1689 isn->isn_arg.tryref.try_ref->try_catch = instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001690 if (scope->se_u.se_try.ts_catch_label != 0)
1691 {
1692 // Previous catch without match jumps here
1693 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
1694 isn->isn_arg.jump.jump_where = instr->ga_len;
1695 }
1696#ifdef FEAT_PROFILE
1697 if (cctx->ctx_compile_type == CT_PROFILE)
1698 {
1699 // a "throw" that jumps here needs to be counted
1700 generate_instr(cctx, ISN_PROF_END);
1701 // the "catch" is also counted
1702 generate_instr(cctx, ISN_PROF_START);
1703 }
1704#endif
1705 if (cctx->ctx_compile_type == CT_DEBUG)
1706 generate_instr_debug(cctx);
1707 }
1708
1709 p = skipwhite(arg);
1710 if (ends_excmd2(arg, p))
1711 {
1712 scope->se_u.se_try.ts_caught_all = TRUE;
1713 scope->se_u.se_try.ts_catch_label = 0;
1714 }
1715 else
1716 {
1717 char_u *end;
1718 char_u *pat;
1719 char_u *tofree = NULL;
1720 int dropped = 0;
1721 int len;
1722
1723 // Push v:exception, push {expr} and MATCH
1724 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
1725
1726 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
1727 if (*end != *p)
1728 {
1729 semsg(_(e_separator_mismatch_str), p);
1730 vim_free(tofree);
Bram Moolenaar54969f42022-02-07 13:56:44 +00001731 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001732 }
1733 if (tofree == NULL)
1734 len = (int)(end - (p + 1));
1735 else
1736 len = (int)(end - tofree);
1737 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
1738 vim_free(tofree);
1739 p += len + 2 + dropped;
1740 if (pat == NULL)
Bram Moolenaar54969f42022-02-07 13:56:44 +00001741 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001742 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaar54969f42022-02-07 13:56:44 +00001743 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001744
1745 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
1746 return NULL;
1747
1748 scope->se_u.se_try.ts_catch_label = instr->ga_len;
1749 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
1750 return NULL;
1751 }
1752
1753 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
1754 return NULL;
1755
1756 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
1757 return NULL;
1758 return p;
1759}
1760
1761 char_u *
1762compile_finally(char_u *arg, cctx_T *cctx)
1763{
1764 scope_T *scope = cctx->ctx_scope;
1765 garray_T *instr = &cctx->ctx_instr;
1766 isn_T *isn;
1767 int this_instr;
1768
1769 if (misplaced_cmdmod(cctx))
1770 return NULL;
1771
1772 // end block scope from :try or :catch
1773 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1774 compile_endblock(cctx);
1775 scope = cctx->ctx_scope;
1776
1777 // Error if not in a :try scope
1778 if (scope == NULL || scope->se_type != TRY_SCOPE)
1779 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001780 emsg(_(e_finally_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001781 return NULL;
1782 }
1783
1784 if (cctx->ctx_skip != SKIP_YES)
1785 {
1786 // End :catch or :finally scope: set value in ISN_TRY instruction
1787 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001788 if (isn->isn_arg.tryref.try_ref->try_finally != 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001789 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001790 emsg(_(e_multiple_finally));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001791 return NULL;
1792 }
1793
1794 this_instr = instr->ga_len;
1795#ifdef FEAT_PROFILE
1796 if (cctx->ctx_compile_type == CT_PROFILE
1797 && ((isn_T *)instr->ga_data)[this_instr - 1]
1798 .isn_type == ISN_PROF_START)
1799 {
1800 // jump to the profile start of the "finally"
1801 --this_instr;
1802
1803 // jump to the profile end above it
1804 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
1805 .isn_type == ISN_PROF_END)
1806 --this_instr;
1807 }
1808#endif
1809
1810 // Fill in the "end" label in jumps at the end of the blocks.
1811 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
1812 this_instr, cctx);
1813
1814 // If there is no :catch then an exception jumps to :finally.
Bram Moolenaar0d807102021-12-21 09:42:09 +00001815 if (isn->isn_arg.tryref.try_ref->try_catch == 0)
1816 isn->isn_arg.tryref.try_ref->try_catch = this_instr;
1817 isn->isn_arg.tryref.try_ref->try_finally = this_instr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001818 if (scope->se_u.se_try.ts_catch_label != 0)
1819 {
1820 // Previous catch without match jumps here
1821 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
1822 isn->isn_arg.jump.jump_where = this_instr;
1823 scope->se_u.se_try.ts_catch_label = 0;
1824 }
Bram Moolenaar53c29612022-01-12 16:18:18 +00001825 scope->se_u.se_try.ts_has_finally = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001826 if (generate_instr(cctx, ISN_FINALLY) == NULL)
1827 return NULL;
1828 }
1829
1830 return arg;
1831}
1832
1833 char_u *
1834compile_endtry(char_u *arg, cctx_T *cctx)
1835{
1836 scope_T *scope = cctx->ctx_scope;
1837 garray_T *instr = &cctx->ctx_instr;
1838 isn_T *try_isn;
1839
1840 if (misplaced_cmdmod(cctx))
1841 return NULL;
1842
1843 // end block scope from :catch or :finally
1844 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1845 compile_endblock(cctx);
1846 scope = cctx->ctx_scope;
1847
1848 // Error if not in a :try scope
1849 if (scope == NULL || scope->se_type != TRY_SCOPE)
1850 {
1851 if (scope == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001852 emsg(_(e_endtry_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001853 else if (scope->se_type == WHILE_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001854 emsg(_(e_missing_endwhile));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001855 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001856 emsg(_(e_missing_endfor));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001857 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001858 emsg(_(e_missing_endif));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001859 return NULL;
1860 }
1861
1862 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
1863 if (cctx->ctx_skip != SKIP_YES)
1864 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00001865 if (try_isn->isn_arg.tryref.try_ref->try_catch == 0
1866 && try_isn->isn_arg.tryref.try_ref->try_finally == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001867 {
1868 emsg(_(e_missing_catch_or_finally));
1869 return NULL;
1870 }
1871
1872#ifdef FEAT_PROFILE
1873 if (cctx->ctx_compile_type == CT_PROFILE
1874 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
1875 .isn_type == ISN_PROF_START)
1876 // move the profile start after "endtry" so that it's not counted when
1877 // the exception is rethrown.
1878 --instr->ga_len;
1879#endif
1880
1881 // Fill in the "end" label in jumps at the end of the blocks, if not
1882 // done by ":finally".
1883 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
1884 instr->ga_len, cctx);
1885
1886 if (scope->se_u.se_try.ts_catch_label != 0)
1887 {
1888 // Last catch without match jumps here
1889 isn_T *isn = ((isn_T *)instr->ga_data)
1890 + scope->se_u.se_try.ts_catch_label;
1891 isn->isn_arg.jump.jump_where = instr->ga_len;
1892 }
1893 }
1894
Bram Moolenaar53c29612022-01-12 16:18:18 +00001895 // If there is a finally clause that ends in return then we will return.
1896 // If one of the blocks didn't end in "return" or we did not catch all
1897 // exceptions reset the had_return flag.
1898 if (!(scope->se_u.se_try.ts_has_finally && cctx->ctx_had_return)
1899 && (scope->se_u.se_try.ts_no_return
1900 || !scope->se_u.se_try.ts_caught_all))
1901 cctx->ctx_had_return = FALSE;
1902
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001903 compile_endblock(cctx);
1904
1905 if (cctx->ctx_skip != SKIP_YES)
1906 {
1907 // End :catch or :finally scope: set instruction index in ISN_TRY
1908 // instruction
Bram Moolenaar0d807102021-12-21 09:42:09 +00001909 try_isn->isn_arg.tryref.try_ref->try_endtry = instr->ga_len;
Bram Moolenaarfe154992022-03-22 20:42:12 +00001910 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001911 return NULL;
1912#ifdef FEAT_PROFILE
1913 if (cctx->ctx_compile_type == CT_PROFILE)
1914 generate_instr(cctx, ISN_PROF_START);
1915#endif
1916 }
1917 return arg;
1918}
1919
1920/*
1921 * compile "throw {expr}"
1922 */
1923 char_u *
1924compile_throw(char_u *arg, cctx_T *cctx UNUSED)
1925{
1926 char_u *p = skipwhite(arg);
1927
1928 if (compile_expr0(&p, cctx) == FAIL)
1929 return NULL;
1930 if (cctx->ctx_skip == SKIP_YES)
1931 return p;
1932 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1933 return NULL;
1934 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
1935 return NULL;
1936
1937 return p;
1938}
1939
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001940/*
1941 * Compile an expression or function call.
1942 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001943 char_u *
1944compile_eval(char_u *arg, cctx_T *cctx)
1945{
1946 char_u *p = arg;
1947 int name_only;
1948 long lnum = SOURCING_LNUM;
1949
1950 // find_ex_command() will consider a variable name an expression, assuming
1951 // that something follows on the next line. Check that something actually
1952 // follows, otherwise it's probably a misplaced command.
1953 name_only = cmd_is_name_only(arg);
1954
1955 if (compile_expr0(&p, cctx) == FAIL)
1956 return NULL;
1957
1958 if (name_only && lnum == SOURCING_LNUM)
1959 {
1960 semsg(_(e_expression_without_effect_str), arg);
1961 return NULL;
1962 }
1963
1964 // drop the result
1965 generate_instr_drop(cctx, ISN_DROP, 1);
1966
1967 return skipwhite(p);
1968}
1969
1970/*
Bram Moolenaarc572ad52022-09-08 20:49:22 +01001971 * Get the local variable index for deferred function calls.
1972 * Reserve it when not done already.
1973 * Returns zero for failure.
1974 */
1975 int
1976get_defer_var_idx(cctx_T *cctx)
1977{
1978 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1979 + cctx->ctx_ufunc->uf_dfunc_idx;
1980 if (dfunc->df_defer_var_idx == 0)
1981 {
1982 lvar_T *lvar = reserve_local(cctx, (char_u *)"@defer@", 7,
1983 TRUE, &t_list_any);
1984 if (lvar == NULL)
1985 return 0;
1986 dfunc->df_defer_var_idx = lvar->lv_idx + 1;
1987 }
1988 return dfunc->df_defer_var_idx;
1989}
1990
1991/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001992 * Compile "defer func(arg)".
1993 */
1994 char_u *
1995compile_defer(char_u *arg_start, cctx_T *cctx)
1996{
Bram Moolenaar16900322022-09-08 19:51:45 +01001997 char_u *paren;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001998 char_u *arg = arg_start;
1999 int argcount = 0;
Bram Moolenaar806a2732022-09-04 15:40:36 +01002000 int defer_var_idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002001 type_T *type;
2002 int func_idx;
2003
2004 // Get a funcref for the function name.
2005 // TODO: better way to find the "(".
Bram Moolenaar16900322022-09-08 19:51:45 +01002006 paren = vim_strchr(arg, '(');
2007 if (paren == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002008 {
2009 semsg(_(e_missing_parenthesis_str), arg);
2010 return NULL;
2011 }
Bram Moolenaar16900322022-09-08 19:51:45 +01002012 *paren = NUL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002013 func_idx = find_internal_func(arg);
2014 if (func_idx >= 0)
2015 // TODO: better type
2016 generate_PUSHFUNC(cctx, (char_u *)internal_func_name(func_idx),
2017 &t_func_any, FALSE);
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02002018 else if (compile_expr0(&arg, cctx) == FAIL)
2019 return NULL;
Bram Moolenaar16900322022-09-08 19:51:45 +01002020 *paren = '(';
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002021
2022 // check for function type
2023 type = get_type_on_stack(cctx, 0);
2024 if (type->tt_type != VAR_FUNC)
2025 {
2026 emsg(_(e_function_name_required));
2027 return NULL;
2028 }
2029
2030 // compile the arguments
Bram Moolenaar16900322022-09-08 19:51:45 +01002031 arg = skipwhite(paren + 1);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002032 if (compile_arguments(&arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
2033 return NULL;
2034
Bram Moolenaar16900322022-09-08 19:51:45 +01002035 if (func_idx >= 0)
2036 {
2037 type2_T *argtypes = NULL;
2038 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
2039
2040 if (check_internal_func_args(cctx, func_idx, argcount, FALSE,
2041 &argtypes, shuffled_argtypes) == FAIL)
2042 return NULL;
2043 }
2044 else if (check_func_args_from_type(cctx, type, argcount, TRUE,
2045 arg_start) == FAIL)
2046 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002047
Bram Moolenaar806a2732022-09-04 15:40:36 +01002048 defer_var_idx = get_defer_var_idx(cctx);
2049 if (defer_var_idx == 0)
2050 return NULL;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02002051 if (generate_DEFER(cctx, defer_var_idx - 1, argcount) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002052 return NULL;
2053
2054 return skipwhite(arg);
2055}
2056
2057/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002058 * compile "echo expr"
2059 * compile "echomsg expr"
2060 * compile "echoerr expr"
2061 * compile "echoconsole expr"
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002062 * compile "echowindow expr" - may have cmd_count set
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002063 * compile "execute expr"
2064 */
2065 char_u *
Bram Moolenaar9b994112022-12-25 15:59:25 +00002066compile_mult_expr(
2067 char_u *arg,
2068 int cmdidx,
2069 long cmd_count UNUSED,
2070 cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002071{
2072 char_u *p = arg;
2073 char_u *prev = arg;
2074 char_u *expr_start;
2075 int count = 0;
2076 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002077 type_T *type;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002078 int r = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002079
2080 for (;;)
2081 {
2082 if (ends_excmd2(prev, p))
2083 break;
2084 expr_start = p;
2085 if (compile_expr0(&p, cctx) == FAIL)
2086 return NULL;
2087
2088 if (cctx->ctx_skip != SKIP_YES)
2089 {
2090 // check for non-void type
Bram Moolenaar078a4612022-01-04 15:17:03 +00002091 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002092 if (type->tt_type == VAR_VOID)
2093 {
2094 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
2095 return NULL;
2096 }
2097 }
2098
2099 ++count;
2100 prev = p;
2101 p = skipwhite(p);
2102 }
2103
2104 if (count > 0)
2105 {
2106 long save_lnum = cctx->ctx_lnum;
2107
2108 // Use the line number where the command started.
2109 cctx->ctx_lnum = start_ctx_lnum;
2110
2111 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002112 r = generate_ECHO(cctx, cmdidx == CMD_echo, count);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002113 else if (cmdidx == CMD_execute)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002114 r = generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002115 else if (cmdidx == CMD_echomsg)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002116 r = generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01002117#ifdef HAS_MESSAGE_WINDOW
2118 else if (cmdidx == CMD_echowindow)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002119 r = generate_ECHOWINDOW(cctx, count, cmd_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01002120#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002121 else if (cmdidx == CMD_echoconsole)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002122 r = generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002123 else
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002124 r = generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002125
2126 cctx->ctx_lnum = save_lnum;
2127 }
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002128 return r == OK ? p : NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002129}
2130
2131/*
2132 * If "eap" has a range that is not a constant generate an ISN_RANGE
2133 * instruction to compute it and return OK.
2134 * Otherwise return FAIL, the caller must deal with any range.
2135 */
2136 static int
2137compile_variable_range(exarg_T *eap, cctx_T *cctx)
2138{
2139 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
2140 char_u *p = skipdigits(eap->cmd);
2141
2142 if (p == range_end)
2143 return FAIL;
2144 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
2145}
2146
2147/*
2148 * :put r
2149 * :put ={expr}
2150 */
2151 char_u *
2152compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
2153{
2154 char_u *line = arg;
2155 linenr_T lnum;
2156 char *errormsg;
2157 int above = eap->forceit;
2158
2159 eap->regname = *line;
2160
2161 if (eap->regname == '=')
2162 {
Bram Moolenaard0b7bfa2022-03-12 14:51:16 +00002163 char_u *p = skipwhite(line + 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002164
2165 if (compile_expr0(&p, cctx) == FAIL)
2166 return NULL;
2167 line = p;
2168 }
2169 else if (eap->regname != NUL)
2170 ++line;
2171
2172 if (compile_variable_range(eap, cctx) == OK)
2173 {
2174 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
2175 }
2176 else
2177 {
2178 // Either no range or a number.
2179 // "errormsg" will not be set because the range is ADDR_LINES.
2180 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
2181 // cannot happen
2182 return NULL;
2183 if (eap->addr_count == 0)
2184 lnum = -1;
2185 else
2186 lnum = eap->line2;
2187 if (above)
2188 --lnum;
2189 }
2190
2191 generate_PUT(cctx, eap->regname, lnum);
2192 return line;
2193}
2194
2195/*
2196 * A command that is not compiled, execute with legacy code.
2197 */
2198 char_u *
2199compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
2200{
2201 char_u *line = line_arg;
2202 char_u *p;
2203 int has_expr = FALSE;
2204 char_u *nextcmd = (char_u *)"";
2205 char_u *tofree = NULL;
2206 char_u *cmd_arg = NULL;
2207
2208 if (cctx->ctx_skip == SKIP_YES)
2209 goto theend;
2210
2211 // If there was a prececing command modifier, drop it and include it in the
2212 // EXEC command.
2213 if (cctx->ctx_has_cmdmod)
2214 {
2215 garray_T *instr = &cctx->ctx_instr;
2216 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2217
2218 if (isn->isn_type == ISN_CMDMOD)
2219 {
2220 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
2221 ->cmod_filter_regmatch.regprog);
2222 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2223 --instr->ga_len;
2224 cctx->ctx_has_cmdmod = FALSE;
2225 }
2226 }
2227
2228 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
2229 {
2230 long argt = eap->argt;
2231 int usefilter = FALSE;
2232
2233 has_expr = argt & (EX_XFILE | EX_EXPAND);
2234
2235 // If the command can be followed by a bar, find the bar and truncate
2236 // it, so that the following command can be compiled.
2237 // The '|' is overwritten with a NUL, it is put back below.
2238 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
2239 && *eap->arg == '!')
2240 // :w !filter or :r !filter or :r! filter
2241 usefilter = TRUE;
2242 if ((argt & EX_TRLBAR) && !usefilter)
2243 {
2244 eap->argt = argt;
Bram Moolenaarac485062022-03-23 19:45:01 +00002245 separate_nextcmd(eap, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002246 if (eap->nextcmd != NULL)
2247 nextcmd = eap->nextcmd;
2248 }
2249 else if (eap->cmdidx == CMD_wincmd)
2250 {
2251 p = eap->arg;
2252 if (*p != NUL)
2253 ++p;
2254 if (*p == 'g' || *p == Ctrl_G)
2255 ++p;
2256 p = skipwhite(p);
2257 if (*p == '|')
2258 {
2259 *p = NUL;
2260 nextcmd = p + 1;
2261 }
2262 }
2263 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
2264 {
2265 // If there is a trailing '{' read lines until the '}'
2266 p = eap->arg + STRLEN(eap->arg) - 1;
2267 while (p > eap->arg && VIM_ISWHITE(*p))
2268 --p;
2269 if (*p == '{')
2270 {
2271 exarg_T ea;
Bram Moolenaar62628d92022-02-25 21:10:53 +00002272 int flags = 0; // unused
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002273 int start_lnum = SOURCING_LNUM;
2274
2275 CLEAR_FIELD(ea);
2276 ea.arg = eap->arg;
2277 fill_exarg_from_cctx(&ea, cctx);
2278 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
2279 if (tofree != NULL)
2280 {
2281 *p = NUL;
2282 line = concat_str(line, tofree);
2283 if (line == NULL)
2284 goto theend;
2285 vim_free(tofree);
2286 tofree = line;
2287 SOURCING_LNUM = start_lnum;
2288 }
2289 }
2290 }
2291 }
2292
2293 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
2294 {
2295 // expand filename in "syntax include [@group] filename"
2296 has_expr = TRUE;
2297 eap->arg = skipwhite(eap->arg + 7);
2298 if (*eap->arg == '@')
2299 eap->arg = skiptowhite(eap->arg);
2300 }
2301
2302 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
2303 && STRLEN(eap->arg) > 4)
2304 {
2305 int delim = *eap->arg;
2306
2307 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
2308 if (*p == delim)
2309 cmd_arg = p + 1;
2310 }
2311
2312 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
2313 cmd_arg = eap->arg;
2314
2315 if (cmd_arg != NULL)
2316 {
2317 exarg_T nea;
2318
2319 CLEAR_FIELD(nea);
2320 nea.cmd = cmd_arg;
2321 p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
2322 if (nea.cmdidx < CMD_SIZE)
2323 {
2324 has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
2325 if (has_expr)
2326 eap->arg = skiptowhite(eap->arg);
2327 }
2328 }
2329
2330 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
2331 {
2332 int count = 0;
2333 char_u *start = skipwhite(line);
2334
2335 // :cmd xxx`=expr1`yyy`=expr2`zzz
2336 // PUSHS ":cmd xxx"
2337 // eval expr1
2338 // PUSHS "yyy"
2339 // eval expr2
2340 // PUSHS "zzz"
2341 // EXECCONCAT 5
2342 for (;;)
2343 {
2344 if (p > start)
2345 {
2346 char_u *val = vim_strnsave(start, p - start);
2347
2348 generate_PUSHS(cctx, &val);
2349 ++count;
2350 }
2351 p += 2;
2352 if (compile_expr0(&p, cctx) == FAIL)
2353 return NULL;
2354 may_generate_2STRING(-1, TRUE, cctx);
2355 ++count;
2356 p = skipwhite(p);
2357 if (*p != '`')
2358 {
2359 emsg(_(e_missing_backtick));
2360 return NULL;
2361 }
2362 start = p + 1;
2363
2364 p = (char_u *)strstr((char *)start, "`=");
2365 if (p == NULL)
2366 {
2367 if (*skipwhite(start) != NUL)
2368 {
2369 char_u *val = vim_strsave(start);
2370
2371 generate_PUSHS(cctx, &val);
2372 ++count;
2373 }
2374 break;
2375 }
2376 }
2377 generate_EXECCONCAT(cctx, count);
2378 }
2379 else
2380 generate_EXEC_copy(cctx, ISN_EXEC, line);
2381
2382theend:
2383 if (*nextcmd != NUL)
2384 {
2385 // the parser expects a pointer to the bar, put it back
2386 --nextcmd;
2387 *nextcmd = '|';
2388 }
2389 vim_free(tofree);
2390
2391 return nextcmd;
2392}
2393
2394/*
2395 * A script command with heredoc, e.g.
2396 * ruby << EOF
2397 * command
2398 * EOF
2399 * Has been turned into one long line with NL characters by
2400 * get_function_body():
2401 * ruby << EOF<NL> command<NL>EOF
2402 */
2403 char_u *
2404compile_script(char_u *line, cctx_T *cctx)
2405{
2406 if (cctx->ctx_skip != SKIP_YES)
2407 {
2408 isn_T *isn;
2409
2410 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
2411 return NULL;
2412 isn->isn_arg.string = vim_strsave(line);
2413 }
2414 return (char_u *)"";
2415}
2416
2417
2418/*
2419 * :s/pat/repl/
2420 */
2421 char_u *
2422compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
2423{
2424 char_u *cmd = eap->arg;
2425 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
2426
2427 if (expr != NULL)
2428 {
2429 int delimiter = *cmd++;
2430
2431 // There is a \=expr, find it in the substitute part.
2432 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
2433 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
2434 {
2435 garray_T save_ga = cctx->ctx_instr;
2436 char_u *end;
2437 int expr_res;
2438 int trailing_error;
2439 int instr_count;
2440 isn_T *instr;
2441 isn_T *isn;
2442
2443 cmd += 3;
2444 end = skip_substitute(cmd, delimiter);
2445
2446 // Temporarily reset the list of instructions so that the jump
2447 // labels are correct.
2448 cctx->ctx_instr.ga_len = 0;
2449 cctx->ctx_instr.ga_maxlen = 0;
2450 cctx->ctx_instr.ga_data = NULL;
2451 expr_res = compile_expr0(&cmd, cctx);
2452 if (end[-1] == NUL)
2453 end[-1] = delimiter;
2454 cmd = skipwhite(cmd);
2455 trailing_error = *cmd != delimiter && *cmd != NUL;
2456
2457 if (expr_res == FAIL || trailing_error
2458 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
2459 {
2460 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002461 semsg(_(e_trailing_characters_str), cmd);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002462 clear_instr_ga(&cctx->ctx_instr);
2463 cctx->ctx_instr = save_ga;
2464 return NULL;
2465 }
2466
2467 // Move the generated instructions into the ISN_SUBSTITUTE
2468 // instructions, then restore the list of instructions before
2469 // adding the ISN_SUBSTITUTE instruction.
2470 instr_count = cctx->ctx_instr.ga_len;
2471 instr = cctx->ctx_instr.ga_data;
2472 instr[instr_count].isn_type = ISN_FINISH;
2473
2474 cctx->ctx_instr = save_ga;
2475 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
2476 {
2477 int idx;
2478
2479 for (idx = 0; idx < instr_count; ++idx)
2480 delete_instr(instr + idx);
2481 vim_free(instr);
2482 return NULL;
2483 }
2484 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
2485 isn->isn_arg.subs.subs_instr = instr;
2486
2487 // skip over flags
2488 if (*end == '&')
2489 ++end;
2490 while (ASCII_ISALPHA(*end) || *end == '#')
2491 ++end;
2492 return end;
2493 }
2494 }
2495
2496 return compile_exec(arg, eap, cctx);
2497}
2498
2499 char_u *
2500compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
2501{
2502 char_u *arg = eap->arg;
2503 lhs_T *lhs = &cctx->ctx_redir_lhs;
2504
2505 if (lhs->lhs_name != NULL)
2506 {
2507 if (STRNCMP(arg, "END", 3) == 0)
2508 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002509 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002510 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002511 if (lhs->lhs_append)
2512 {
2513 // First load the current variable value.
2514 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002515 cctx) == FAIL)
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002516 return NULL;
2517 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002518
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002519 // Gets the redirected text and put it on the stack, then store
2520 // it in the variable.
2521 generate_instr_type(cctx, ISN_REDIREND, &t_string);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002522
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002523 if (lhs->lhs_append)
2524 generate_CONCAT(cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002525
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002526 if (lhs->lhs_has_index)
2527 {
2528 // Use the info in "lhs" to store the value at the index in
2529 // the list or dict.
2530 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002531 &t_string, cctx) == FAIL)
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002532 return NULL;
2533 }
2534 else if (generate_store_lhs(cctx, lhs, -1, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002535 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002536
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002537 VIM_CLEAR(lhs->lhs_name);
2538 VIM_CLEAR(lhs->lhs_whole);
2539 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002540 return arg + 3;
2541 }
2542 emsg(_(e_cannot_nest_redir));
2543 return NULL;
2544 }
2545
2546 if (arg[0] == '=' && arg[1] == '>')
2547 {
2548 int append = FALSE;
2549
2550 // redirect to a variable is compiled
2551 arg += 2;
2552 if (*arg == '>')
2553 {
2554 ++arg;
2555 append = TRUE;
2556 }
2557 arg = skipwhite(arg);
2558
2559 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002560 FALSE, FALSE, FALSE, 1, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002561 return NULL;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002562 if (need_type(&t_string, lhs->lhs_member_type, FALSE,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002563 -1, 0, cctx, FALSE, FALSE) == FAIL)
2564 return NULL;
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002565 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002566 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002567 VIM_CLEAR(lhs->lhs_name);
2568 }
2569 else
2570 {
2571 generate_instr(cctx, ISN_REDIRSTART);
2572 lhs->lhs_append = append;
2573 if (lhs->lhs_has_index)
2574 {
2575 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
2576 if (lhs->lhs_whole == NULL)
2577 return NULL;
2578 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002579 }
2580
2581 return arg + lhs->lhs_varlen_total;
2582 }
2583
2584 // other redirects are handled like at script level
2585 return compile_exec(line, eap, cctx);
2586}
2587
2588#if defined(FEAT_QUICKFIX) || defined(PROTO)
2589 char_u *
2590compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
2591{
2592 isn_T *isn;
2593 char_u *p;
2594
2595 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
2596 if (isn == NULL)
2597 return NULL;
2598 isn->isn_arg.number = eap->cmdidx;
2599
2600 p = eap->arg;
2601 if (compile_expr0(&p, cctx) == FAIL)
2602 return NULL;
2603
2604 isn = generate_instr(cctx, ISN_CEXPR_CORE);
2605 if (isn == NULL)
2606 return NULL;
2607 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
2608 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
2609 return NULL;
2610 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
2611 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
2612 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
2613
2614 return p;
2615}
2616#endif
2617
2618/*
2619 * Compile "return [expr]".
2620 * When "legacy" is TRUE evaluate [expr] with legacy syntax
2621 */
2622 char_u *
2623compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
2624{
2625 char_u *p = arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002626 type_T *stack_type;
2627
mityu500c4442022-12-02 18:12:05 +00002628 if (*p != NUL && *p != '|' && *p != '\n'
2629 && (legacy || !vim9_comment_start(p)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002630 {
Bram Moolenaar0bfa8492022-01-22 12:27:04 +00002631 // For a lambda, "return expr" is always used, also when "expr" results
2632 // in a void.
2633 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
2634 && (cctx->ctx_ufunc->uf_flags & FC_LAMBDA) == 0)
Bram Moolenaaref7aadb2022-01-18 18:46:07 +00002635 {
2636 emsg(_(e_returning_value_in_function_without_return_type));
2637 return NULL;
2638 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002639 if (legacy)
2640 {
2641 int save_flags = cmdmod.cmod_flags;
2642
2643 generate_LEGACY_EVAL(cctx, p);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002644 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, FALSE, -1,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002645 0, cctx, FALSE, FALSE) == FAIL)
2646 return NULL;
2647 cmdmod.cmod_flags |= CMOD_LEGACY;
2648 (void)skip_expr(&p, NULL);
2649 cmdmod.cmod_flags = save_flags;
2650 }
2651 else
2652 {
2653 // compile return argument into instructions
2654 if (compile_expr0(&p, cctx) == FAIL)
2655 return NULL;
2656 }
2657
2658 if (cctx->ctx_skip != SKIP_YES)
2659 {
2660 // "check_return_type" with uf_ret_type set to &t_unknown is used
2661 // for an inline function without a specified return type. Set the
2662 // return type here.
Bram Moolenaar078a4612022-01-04 15:17:03 +00002663 stack_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002664 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar1a572e92022-03-15 12:28:10 +00002665 || cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002666 || (!check_return_type
2667 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
2668 {
2669 cctx->ctx_ufunc->uf_ret_type = stack_type;
2670 }
2671 else
2672 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002673 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, FALSE,
2674 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002675 return NULL;
2676 }
2677 }
2678 }
2679 else
2680 {
2681 // "check_return_type" cannot be TRUE, only used for a lambda which
2682 // always has an argument.
2683 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
2684 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
2685 {
2686 emsg(_(e_missing_return_value));
2687 return NULL;
2688 }
2689
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +02002690 if (IS_CONSTRUCTOR_METHOD(cctx->ctx_ufunc))
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02002691 {
2692 // For a class new() constructor, return an object of the class.
2693 generate_instr(cctx, ISN_RETURN_OBJECT);
2694 cctx->ctx_ufunc->uf_ret_type =
2695 &cctx->ctx_ufunc->uf_class->class_object_type;
2696 }
2697 else
2698 // No argument, return zero.
2699 generate_PUSHNR(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002700 }
2701
Bram Moolenaar8abb5842022-09-17 12:39:58 +01002702 // may need ENDLOOP when inside a :for or :while loop
2703 if (compile_find_scope(NULL, NULL, NULL, NULL, cctx) == FAIL)
2704
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002705 // Undo any command modifiers.
2706 generate_undo_cmdmods(cctx);
2707
2708 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
2709 return NULL;
2710
2711 // "return val | endif" is possible
2712 return skipwhite(p);
2713}
2714
2715/*
2716 * Check if the separator for a :global or :substitute command is OK.
2717 */
2718 int
2719check_global_and_subst(char_u *cmd, char_u *arg)
2720{
2721 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
2722 {
2723 semsg(_(e_separator_not_supported_str), arg);
2724 return FAIL;
2725 }
2726 if (VIM_ISWHITE(cmd[1]))
2727 {
2728 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
2729 return FAIL;
2730 }
2731 return OK;
2732}
2733
2734
2735#endif // defined(FEAT_EVAL)