blob: bc01cd83dac715b25d735bad47295e94decec89a [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;
192 size_t len;
193 char_u *buf;
194 isntype_T isn = ISN_EXEC;
Bram Moolenaard1d8f6b2022-08-14 21:28:32 +0100195 char *cmd = eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar";
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000196
197 if (cctx->ctx_skip == SKIP_YES)
198 return OK;
199
Bram Moolenaard1d8f6b2022-08-14 21:28:32 +0100200 if (*p == NUL)
201 {
202 semsg(_(e_argument_required_for_str), cmd);
203 return FAIL;
204 }
205
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000206 // 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 Moolenaare939f5e2022-01-26 21:32:59 +0000237 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 Moolenaardc7c3662021-12-20 15:04:29 +0000241 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 *
254compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
255{
Bram Moolenaar70c43d82022-01-26 21:01:15 +0000256 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 Moolenaardc7c3662021-12-20 15:04:29 +0000273 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
274 cctx);
275 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
276}
277
278/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100279 * Generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
280 * "funcref_idx" is used for JUMP_WHILE_FALSE
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000281 */
282 static int
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100283compile_jump_to_end(
284 endlabel_T **el,
285 jumpwhen_T when,
286 int funcref_idx,
287 cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000288{
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 Moolenaarb46c0832022-09-15 17:19:37 +0100298 if (when == JUMP_WHILE_FALSE)
299 generate_WHILE(cctx, funcref_idx);
300 else
301 generate_JUMP(cctx, when, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000302 return OK;
303}
304
305 static void
306compile_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
323compile_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 *
338new_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 Moolenaarcc341812022-09-19 15:54:34 +0100348 if (scope->se_outer != NULL)
349 scope->se_loop_depth = scope->se_outer->se_loop_depth;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000350 return scope;
351}
352
353/*
354 * Free the current scope and go back to the outer scope.
355 */
356 void
357drop_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
385misplaced_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 *
432compile_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 Moolenaar74409f62022-01-01 15:58:22 +0000449 semsg(_(e_trailing_characters_str), p);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000450 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 *
511compile_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 Moolenaara275f2c2022-10-11 20:04:09 +0100526 unwind_locals(cctx, scope->se_local_count, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000527 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 Moolenaarb46c0832022-09-15 17:19:37 +0100575 JUMP_ALWAYS, 0, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000576 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 Moolenaar74409f62022-01-01 15:58:22 +0000619 semsg(_(e_trailing_characters_str), p);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000620 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 *
661compile_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 Moolenaara275f2c2022-10-11 20:04:09 +0100673 unwind_locals(cctx, scope->se_local_count, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000674 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 Moolenaarb46c0832022-09-15 17:19:37 +0100706 JUMP_ALWAYS, 0, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000707 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 *
729compile_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 Moolenaara275f2c2022-10-11 20:04:09 +0100745 unwind_locals(cctx, scope->se_local_count, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000746 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 Moolenaar8abb5842022-09-17 12:39:58 +0100779 * Save the info needed for ENDLOOP. Used by :for and :while.
780 */
781 static void
782compile_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 Moolenaardc7c3662021-12-20 15:04:29 +0000790 * Compile "for var in expr":
791 *
792 * Produces instructions:
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100793 * STORE -1 in loop-idx Set index to -1
794 * EVAL expr Result of "expr" on top of stack
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000795 * 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 Moolenaarb46c0832022-09-15 17:19:37 +0100798 * - store ec_funcrefs in var "loop-idx" + 1
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000799 * STORE var Store item in "var"
800 * ... body ...
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100801 * 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 Moolenaardc7c3662021-12-20 15:04:29 +0000804 *
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 *
811compile_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 Moolenaardc7c3662021-12-20 15:04:29 +0000822 garray_T *instr = &cctx->ctx_instr;
823 scope_T *scope;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100824 forscope_T *forscope;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000825 lvar_T *loop_lvar; // loop iteration variable
Bram Moolenaarcc341812022-09-19 15:54:34 +0100826 int loop_lvar_idx;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100827 lvar_T *funcref_lvar;
Bram Moolenaarcc341812022-09-19 15:54:34 +0100828 int funcref_lvar_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000829 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 Moolenaar3a846e62022-01-01 16:21:00 +0000852 emsg(_(e_missing_in_after_for));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853 return NULL;
854 }
855 wp = p + 2;
856 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
857 return NULL;
858
Bram Moolenaar2b4ecc22022-01-02 14:08:18 +0000859 // Find the already generated ISN_DEBUG to get the line number for the
860 // instruction written below the ISN_FOR instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000861 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 Moolenaar2b4ecc22022-01-02 14:08:18 +0000865 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000866 .isn_arg.debug.dbg_break_lnum;
867 }
868
869 scope = new_scope(cctx, FOR_SCOPE);
870 if (scope == NULL)
871 return NULL;
Bram Moolenaarcc341812022-09-19 15:54:34 +0100872 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 Moolenaarb46c0832022-09-15 17:19:37 +0100878 forscope = &scope->se_u.se_for;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000879
880 // Reserve a variable to store the loop iteration counter and initialize it
881 // to -1.
Bram Moolenaar6586a012022-09-30 11:04:50 +0100882 loop_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000883 if (loop_lvar == NULL)
884 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000885 drop_scope(cctx);
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100886 return NULL; // out of memory
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000887 }
Bram Moolenaarcc341812022-09-19 15:54:34 +0100888 // 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 Moolenaardc7c3662021-12-20 15:04:29 +0000891
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100892 // 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 Moolenaar6586a012022-09-30 11:04:50 +0100895 funcref_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number);
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100896 if (funcref_lvar == NULL)
897 {
898 drop_scope(cctx);
899 return NULL; // out of memory
900 }
Bram Moolenaarcc341812022-09-19 15:54:34 +0100901 // get the index before a following reserve_local() makes the lval invalid
902 funcref_lvar_idx = funcref_lvar->lv_idx;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100903
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000904 // 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 Pelleaf4a61a2021-12-27 17:21:41 +0000915 // If we know the type of "var" and it is not a supported type we can
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000916 // give an error now.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000917 vartype = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000918 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 Moolenaardc7c3662021-12-20 15:04:29 +0000923 {
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 Moolenaarb46c0832022-09-15 17:19:37 +0100948 forscope->fs_top_label = current_instr_idx(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000949
Bram Moolenaar2b4ecc22022-01-02 14:08:18 +0000950 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 Moolenaarcc341812022-09-19 15:54:34 +0100966 generate_FOR(cctx, loop_lvar_idx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000967
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 Moolenaar078a4612022-01-04 15:17:03 +0000974 // drop the list item
975 --cctx->ctx_type_stack.ga_len;
976
977 // add type of the items
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000978 for (idx = 0; idx < var_count; ++idx)
979 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000980 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 Moolenaardc7c3662021-12-20 15:04:29 +0000987 }
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 Moolenaarce93d162023-01-30 21:12:34 +00001004 if (*skipwhite(p) == ':')
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001005 {
Bram Moolenaarce93d162023-01-30 21:12:34 +00001006 if (VIM_ISWHITE(*p))
1007 {
1008 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1009 goto failed;
1010 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001011 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 Moolenaard505d172022-12-18 21:42:55 +00001021 type, name, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001022 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 Moolenaara9fa8c52023-01-02 18:10:04 +00001043 semsg(_(e_variable_already_declared_str), arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001044 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 Moolenaarc6951a72022-12-29 20:56:24 +00001053 && need_type_where(item_type, lhs_type, FALSE, -1,
Bram Moolenaara1c51952022-02-02 16:20:26 +00001054 where, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001055 goto failed;
Bram Moolenaar159b2d52022-10-11 21:41:25 +01001056 var_lvar = reserve_local(cctx, arg, varlen, ASSIGN_FINAL,
Bram Moolenaar6586a012022-09-30 11:04:50 +01001057 lhs_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001058 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 Moolenaardc7c3662021-12-20 15:04:29 +00001064 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 Moolenaarb46c0832022-09-15 17:19:37 +01001072
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001073 // remember the number of variables and closures, used for ENDLOOP
Bram Moolenaarcc341812022-09-19 15:54:34 +01001074 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 Moolenaardc7c3662021-12-20 15:04:29 +00001076 }
1077
1078 return arg_end;
1079
1080failed:
1081 vim_free(name);
1082 drop_scope(cctx);
1083 return NULL;
1084}
1085
1086/*
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001087 * 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 Moolenaarb46c0832022-09-15 17:19:37 +01001090 */
1091 static int
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001092compile_loop_end(loop_info_T *loop_info, cctx_T *cctx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001093{
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001094 if (cctx->ctx_locals.ga_len > loop_info->li_local_count
1095 && cctx->ctx_closure_count > loop_info->li_closure_count)
Bram Moolenaarcc341812022-09-19 15:54:34 +01001096 return generate_ENDLOOP(cctx, loop_info);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001097 return OK;
1098}
1099
1100/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001101 * compile "endfor"
1102 */
1103 char_u *
1104compile_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 Moolenaar436b5ad2021-12-31 22:49:24 +00001116 emsg(_(e_endfor_without_for));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001117 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 Moolenaarb46c0832022-09-15 17:19:37 +01001123 // Handle the case that any local variables were declared that might be
1124 // used in a closure.
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001125 if (compile_loop_end(&forscope->fs_loop_info, cctx) == FAIL)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001126 return NULL;
1127
Bram Moolenaara275f2c2022-10-11 20:04:09 +01001128 unwind_locals(cctx, scope->se_local_count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001129
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 Moolenaar2b4ecc22022-01-02 14:08:18 +00001134 // 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 Moolenaardc7c3662021-12-20 15:04:29 +00001137 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 Moolenaarb46c0832022-09-15 17:19:37 +01001156 * 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 Moolenaardc7c3662021-12-20 15:04:29 +00001161 * end:
1162 *
1163 */
1164 char_u *
1165compile_while(char_u *arg, cctx_T *cctx)
1166{
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001167 char_u *p = arg;
1168 scope_T *scope;
1169 whilescope_T *whilescope;
1170 lvar_T *funcref_lvar;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001171 int funcref_lvar_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001172
1173 scope = new_scope(cctx, WHILE_SCOPE);
1174 if (scope == NULL)
1175 return NULL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001176 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 Moolenaarb46c0832022-09-15 17:19:37 +01001182 whilescope = &scope->se_u.se_while;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001183
1184 // "endwhile" jumps back here, one before when profiling or using cmdmods
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001185 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 Moolenaar6586a012022-09-30 11:04:50 +01001189 funcref_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001190 if (funcref_lvar == NULL)
1191 {
1192 drop_scope(cctx);
1193 return NULL; // out of memory
1194 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001195 // get the index before a following reserve_local() makes the lval invalid
1196 funcref_lvar_idx = funcref_lvar->lv_idx;
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001197
1198 // remember the number of variables and closures, used for ENDLOOP
Bram Moolenaarcc341812022-09-19 15:54:34 +01001199 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 Moolenaardc7c3662021-12-20 15:04:29 +00001201
1202 // compile "expr"
1203 if (compile_expr0(&p, cctx) == FAIL)
1204 return NULL;
1205
1206 if (!ends_excmd2(arg, skipwhite(p)))
1207 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001208 semsg(_(e_trailing_characters_str), p);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001209 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 Moolenaarb46c0832022-09-15 17:19:37 +01001221 if (compile_jump_to_end(&whilescope->ws_end_label,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001222 JUMP_WHILE_FALSE, funcref_lvar_idx, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001223 return FAIL;
1224 }
1225
1226 return p;
1227}
1228
1229/*
1230 * compile "endwhile"
1231 */
1232 char_u *
1233compile_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 Moolenaar436b5ad2021-12-31 22:49:24 +00001242 emsg(_(e_endwhile_without_while));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001243 return NULL;
1244 }
1245 cctx->ctx_scope = scope->se_outer;
1246 if (cctx->ctx_skip != SKIP_YES)
1247 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001248 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 Moolenaar8abb5842022-09-17 12:39:58 +01001252 if (compile_loop_end(&whilescope->ws_loop_info, cctx) == FAIL)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001253 return NULL;
1254
Bram Moolenaara275f2c2022-10-11 20:04:09 +01001255 unwind_locals(cctx, scope->se_local_count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001256
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 Moolenaar8fa745e2022-09-16 19:04:24 +01001277 * Get the current information about variables declared inside a loop.
Bram Moolenaarcc341812022-09-19 15:54:34 +01001278 * Returns TRUE if there are any and fills "lvi".
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001279 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01001280 int
1281get_loop_var_info(cctx_T *cctx, loopvarinfo_T *lvi)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001282{
1283 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001284 int prev_local_count = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001285
Bram Moolenaarcc341812022-09-19 15:54:34 +01001286 CLEAR_POINTER(lvi);
1287 for (;;)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001288 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001289 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 Moolenaar8fa745e2022-09-16 19:04:24 +01001327 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001328 return lvi->lvi_depth > 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001329}
1330
1331/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01001332 * Get the index of the variable "idx" in a loop, if any.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001333 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01001334 void
1335get_loop_var_idx(cctx_T *cctx, int idx, lvar_T *lvar)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001336{
Bram Moolenaarcc341812022-09-19 15:54:34 +01001337 loopvarinfo_T lvi;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001338
Bram Moolenaarcc341812022-09-19 15:54:34 +01001339 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 Moolenaar8fa745e2022-09-16 19:04:24 +01001355}
1356
1357/*
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001358 * Common for :break, :continue and :return
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001359 */
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001360 static int
1361compile_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 Moolenaardc7c3662021-12-20 15:04:29 +00001367{
1368 scope_T *scope = cctx->ctx_scope;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001369
1370 for (;;)
1371 {
1372 if (scope == NULL)
1373 {
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001374 if (error != NULL)
1375 emsg(_(error));
1376 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001377 }
1378 if (scope->se_type == FOR_SCOPE)
1379 {
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001380 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 Moolenaardc7c3662021-12-20 15:04:29 +00001387 break;
1388 }
1389 if (scope->se_type == WHILE_SCOPE)
1390 {
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001391 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 Moolenaardc7c3662021-12-20 15:04:29 +00001398 break;
1399 }
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001400 if (try_scopes != NULL && scope->se_type == TRY_SCOPE)
1401 ++*try_scopes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001402 scope = scope->se_outer;
1403 }
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001404 return OK;
1405}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001406
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001407/*
1408 * compile "continue"
1409 */
1410 char_u *
1411compile_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 Moolenaardc7c3662021-12-20 15:04:29 +00001419 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 *
1434compile_break(char_u *arg, cctx_T *cctx)
1435{
Bram Moolenaar873f8242022-03-10 21:53:44 +00001436 int try_scopes = 0;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001437 endlabel_T **el;
1438
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001439 if (compile_find_scope(NULL, &el, &try_scopes,
1440 e_break_without_while_or_for, cctx) == FAIL)
1441 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001442
Bram Moolenaar3f45d672023-02-27 22:06:51 +00001443 if (cctx->ctx_skip == SKIP_YES)
1444 return arg;
1445
Bram Moolenaar873f8242022-03-10 21:53:44 +00001446 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
dundargocc57b5bc2022-11-02 13:30:51 +00001449 // instruction, which we don't know the index of yet.
Bram Moolenaar873f8242022-03-10 21:53:44 +00001450 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 Moolenaarb46c0832022-09-15 17:19:37 +01001454 if (compile_jump_to_end(el, JUMP_ALWAYS, 0, cctx) == FAIL)
Bram Moolenaar3f45d672023-02-27 22:06:51 +00001455 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001456
1457 return arg;
1458}
1459
1460/*
1461 * compile "{" start of block
1462 */
1463 char_u *
1464compile_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
1475compile_endblock(cctx_T *cctx)
1476{
1477 scope_T *scope = cctx->ctx_scope;
1478
1479 cctx->ctx_scope = scope->se_outer;
Bram Moolenaara275f2c2022-10-11 20:04:09 +01001480 unwind_locals(cctx, scope->se_local_count, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001481 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 *
1517compile_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 Moolenaar0d807102021-12-21 09:42:09 +00001542 isn->isn_arg.tryref.try_ref = ALLOC_CLEAR_ONE(tryref_T);
1543 if (isn->isn_arg.tryref.try_ref == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001544 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 *
1559compile_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 Moolenaar436b5ad2021-12-31 22:49:24 +00001577 emsg(_(e_catch_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001578 return NULL;
1579 }
1580
1581 if (scope->se_u.se_try.ts_caught_all)
1582 {
1583 emsg(_(e_catch_unreachable_after_catch_all));
1584 return NULL;
1585 }
Bram Moolenaar53c29612022-01-12 16:18:18 +00001586 if (!cctx->ctx_had_return)
1587 scope->se_u.se_try.ts_no_return = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001588
1589 if (cctx->ctx_skip != SKIP_YES)
1590 {
1591#ifdef FEAT_PROFILE
1592 // the profile-start should be after the jump
1593 if (cctx->ctx_compile_type == CT_PROFILE
1594 && instr->ga_len > 0
1595 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
1596 .isn_type == ISN_PROF_START)
1597 --instr->ga_len;
1598#endif
1599 // Jump from end of previous block to :finally or :endtry
1600 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001601 JUMP_ALWAYS, 0, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001602 return NULL;
1603
1604 // End :try or :catch scope: set value in ISN_TRY instruction
1605 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001606 if (isn->isn_arg.tryref.try_ref->try_catch == 0)
1607 isn->isn_arg.tryref.try_ref->try_catch = instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001608 if (scope->se_u.se_try.ts_catch_label != 0)
1609 {
1610 // Previous catch without match jumps here
1611 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
1612 isn->isn_arg.jump.jump_where = instr->ga_len;
1613 }
1614#ifdef FEAT_PROFILE
1615 if (cctx->ctx_compile_type == CT_PROFILE)
1616 {
1617 // a "throw" that jumps here needs to be counted
1618 generate_instr(cctx, ISN_PROF_END);
1619 // the "catch" is also counted
1620 generate_instr(cctx, ISN_PROF_START);
1621 }
1622#endif
1623 if (cctx->ctx_compile_type == CT_DEBUG)
1624 generate_instr_debug(cctx);
1625 }
1626
1627 p = skipwhite(arg);
1628 if (ends_excmd2(arg, p))
1629 {
1630 scope->se_u.se_try.ts_caught_all = TRUE;
1631 scope->se_u.se_try.ts_catch_label = 0;
1632 }
1633 else
1634 {
1635 char_u *end;
1636 char_u *pat;
1637 char_u *tofree = NULL;
1638 int dropped = 0;
1639 int len;
1640
1641 // Push v:exception, push {expr} and MATCH
1642 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
1643
1644 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
1645 if (*end != *p)
1646 {
1647 semsg(_(e_separator_mismatch_str), p);
1648 vim_free(tofree);
Bram Moolenaar54969f42022-02-07 13:56:44 +00001649 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001650 }
1651 if (tofree == NULL)
1652 len = (int)(end - (p + 1));
1653 else
1654 len = (int)(end - tofree);
1655 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
1656 vim_free(tofree);
1657 p += len + 2 + dropped;
1658 if (pat == NULL)
Bram Moolenaar54969f42022-02-07 13:56:44 +00001659 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001660 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaar54969f42022-02-07 13:56:44 +00001661 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001662
1663 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
1664 return NULL;
1665
1666 scope->se_u.se_try.ts_catch_label = instr->ga_len;
1667 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
1668 return NULL;
1669 }
1670
1671 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
1672 return NULL;
1673
1674 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
1675 return NULL;
1676 return p;
1677}
1678
1679 char_u *
1680compile_finally(char_u *arg, cctx_T *cctx)
1681{
1682 scope_T *scope = cctx->ctx_scope;
1683 garray_T *instr = &cctx->ctx_instr;
1684 isn_T *isn;
1685 int this_instr;
1686
1687 if (misplaced_cmdmod(cctx))
1688 return NULL;
1689
1690 // end block scope from :try or :catch
1691 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1692 compile_endblock(cctx);
1693 scope = cctx->ctx_scope;
1694
1695 // Error if not in a :try scope
1696 if (scope == NULL || scope->se_type != TRY_SCOPE)
1697 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001698 emsg(_(e_finally_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001699 return NULL;
1700 }
1701
1702 if (cctx->ctx_skip != SKIP_YES)
1703 {
1704 // End :catch or :finally scope: set value in ISN_TRY instruction
1705 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001706 if (isn->isn_arg.tryref.try_ref->try_finally != 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001707 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001708 emsg(_(e_multiple_finally));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001709 return NULL;
1710 }
1711
1712 this_instr = instr->ga_len;
1713#ifdef FEAT_PROFILE
1714 if (cctx->ctx_compile_type == CT_PROFILE
1715 && ((isn_T *)instr->ga_data)[this_instr - 1]
1716 .isn_type == ISN_PROF_START)
1717 {
1718 // jump to the profile start of the "finally"
1719 --this_instr;
1720
1721 // jump to the profile end above it
1722 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
1723 .isn_type == ISN_PROF_END)
1724 --this_instr;
1725 }
1726#endif
1727
1728 // Fill in the "end" label in jumps at the end of the blocks.
1729 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
1730 this_instr, cctx);
1731
1732 // If there is no :catch then an exception jumps to :finally.
Bram Moolenaar0d807102021-12-21 09:42:09 +00001733 if (isn->isn_arg.tryref.try_ref->try_catch == 0)
1734 isn->isn_arg.tryref.try_ref->try_catch = this_instr;
1735 isn->isn_arg.tryref.try_ref->try_finally = this_instr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001736 if (scope->se_u.se_try.ts_catch_label != 0)
1737 {
1738 // Previous catch without match jumps here
1739 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
1740 isn->isn_arg.jump.jump_where = this_instr;
1741 scope->se_u.se_try.ts_catch_label = 0;
1742 }
Bram Moolenaar53c29612022-01-12 16:18:18 +00001743 scope->se_u.se_try.ts_has_finally = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001744 if (generate_instr(cctx, ISN_FINALLY) == NULL)
1745 return NULL;
1746 }
1747
1748 return arg;
1749}
1750
1751 char_u *
1752compile_endtry(char_u *arg, cctx_T *cctx)
1753{
1754 scope_T *scope = cctx->ctx_scope;
1755 garray_T *instr = &cctx->ctx_instr;
1756 isn_T *try_isn;
1757
1758 if (misplaced_cmdmod(cctx))
1759 return NULL;
1760
1761 // end block scope from :catch or :finally
1762 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1763 compile_endblock(cctx);
1764 scope = cctx->ctx_scope;
1765
1766 // Error if not in a :try scope
1767 if (scope == NULL || scope->se_type != TRY_SCOPE)
1768 {
1769 if (scope == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001770 emsg(_(e_endtry_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001771 else if (scope->se_type == WHILE_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001772 emsg(_(e_missing_endwhile));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001773 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001774 emsg(_(e_missing_endfor));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001775 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001776 emsg(_(e_missing_endif));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001777 return NULL;
1778 }
1779
1780 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
1781 if (cctx->ctx_skip != SKIP_YES)
1782 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00001783 if (try_isn->isn_arg.tryref.try_ref->try_catch == 0
1784 && try_isn->isn_arg.tryref.try_ref->try_finally == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001785 {
1786 emsg(_(e_missing_catch_or_finally));
1787 return NULL;
1788 }
1789
1790#ifdef FEAT_PROFILE
1791 if (cctx->ctx_compile_type == CT_PROFILE
1792 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
1793 .isn_type == ISN_PROF_START)
1794 // move the profile start after "endtry" so that it's not counted when
1795 // the exception is rethrown.
1796 --instr->ga_len;
1797#endif
1798
1799 // Fill in the "end" label in jumps at the end of the blocks, if not
1800 // done by ":finally".
1801 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
1802 instr->ga_len, cctx);
1803
1804 if (scope->se_u.se_try.ts_catch_label != 0)
1805 {
1806 // Last catch without match jumps here
1807 isn_T *isn = ((isn_T *)instr->ga_data)
1808 + scope->se_u.se_try.ts_catch_label;
1809 isn->isn_arg.jump.jump_where = instr->ga_len;
1810 }
1811 }
1812
Bram Moolenaar53c29612022-01-12 16:18:18 +00001813 // If there is a finally clause that ends in return then we will return.
1814 // If one of the blocks didn't end in "return" or we did not catch all
1815 // exceptions reset the had_return flag.
1816 if (!(scope->se_u.se_try.ts_has_finally && cctx->ctx_had_return)
1817 && (scope->se_u.se_try.ts_no_return
1818 || !scope->se_u.se_try.ts_caught_all))
1819 cctx->ctx_had_return = FALSE;
1820
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001821 compile_endblock(cctx);
1822
1823 if (cctx->ctx_skip != SKIP_YES)
1824 {
1825 // End :catch or :finally scope: set instruction index in ISN_TRY
1826 // instruction
Bram Moolenaar0d807102021-12-21 09:42:09 +00001827 try_isn->isn_arg.tryref.try_ref->try_endtry = instr->ga_len;
Bram Moolenaarfe154992022-03-22 20:42:12 +00001828 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001829 return NULL;
1830#ifdef FEAT_PROFILE
1831 if (cctx->ctx_compile_type == CT_PROFILE)
1832 generate_instr(cctx, ISN_PROF_START);
1833#endif
1834 }
1835 return arg;
1836}
1837
1838/*
1839 * compile "throw {expr}"
1840 */
1841 char_u *
1842compile_throw(char_u *arg, cctx_T *cctx UNUSED)
1843{
1844 char_u *p = skipwhite(arg);
1845
1846 if (compile_expr0(&p, cctx) == FAIL)
1847 return NULL;
1848 if (cctx->ctx_skip == SKIP_YES)
1849 return p;
1850 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1851 return NULL;
1852 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
1853 return NULL;
1854
1855 return p;
1856}
1857
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001858/*
1859 * Compile an expression or function call.
1860 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001861 char_u *
1862compile_eval(char_u *arg, cctx_T *cctx)
1863{
1864 char_u *p = arg;
1865 int name_only;
1866 long lnum = SOURCING_LNUM;
1867
1868 // find_ex_command() will consider a variable name an expression, assuming
1869 // that something follows on the next line. Check that something actually
1870 // follows, otherwise it's probably a misplaced command.
1871 name_only = cmd_is_name_only(arg);
1872
1873 if (compile_expr0(&p, cctx) == FAIL)
1874 return NULL;
1875
1876 if (name_only && lnum == SOURCING_LNUM)
1877 {
1878 semsg(_(e_expression_without_effect_str), arg);
1879 return NULL;
1880 }
1881
1882 // drop the result
1883 generate_instr_drop(cctx, ISN_DROP, 1);
1884
1885 return skipwhite(p);
1886}
1887
1888/*
Bram Moolenaarc572ad52022-09-08 20:49:22 +01001889 * Get the local variable index for deferred function calls.
1890 * Reserve it when not done already.
1891 * Returns zero for failure.
1892 */
1893 int
1894get_defer_var_idx(cctx_T *cctx)
1895{
1896 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1897 + cctx->ctx_ufunc->uf_dfunc_idx;
1898 if (dfunc->df_defer_var_idx == 0)
1899 {
1900 lvar_T *lvar = reserve_local(cctx, (char_u *)"@defer@", 7,
1901 TRUE, &t_list_any);
1902 if (lvar == NULL)
1903 return 0;
1904 dfunc->df_defer_var_idx = lvar->lv_idx + 1;
1905 }
1906 return dfunc->df_defer_var_idx;
1907}
1908
1909/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001910 * Compile "defer func(arg)".
1911 */
1912 char_u *
1913compile_defer(char_u *arg_start, cctx_T *cctx)
1914{
Bram Moolenaar16900322022-09-08 19:51:45 +01001915 char_u *paren;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001916 char_u *arg = arg_start;
1917 int argcount = 0;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001918 int defer_var_idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001919 type_T *type;
1920 int func_idx;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001921 int obj_method = 0;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001922
1923 // Get a funcref for the function name.
1924 // TODO: better way to find the "(".
Bram Moolenaar16900322022-09-08 19:51:45 +01001925 paren = vim_strchr(arg, '(');
1926 if (paren == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001927 {
1928 semsg(_(e_missing_parenthesis_str), arg);
1929 return NULL;
1930 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001931 *paren = NUL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001932 func_idx = find_internal_func(arg);
1933 if (func_idx >= 0)
1934 // TODO: better type
1935 generate_PUSHFUNC(cctx, (char_u *)internal_func_name(func_idx),
1936 &t_func_any, FALSE);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001937 else
1938 {
1939 int typecount = cctx->ctx_type_stack.ga_len;
1940 if (compile_expr0(&arg, cctx) == FAIL)
1941 return NULL;
1942 if (cctx->ctx_type_stack.ga_len >= typecount + 2)
1943 // must have seen "obj.Func", pushed an object and a function
1944 obj_method = 1;
1945 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001946 *paren = '(';
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001947
1948 // check for function type
1949 type = get_type_on_stack(cctx, 0);
1950 if (type->tt_type != VAR_FUNC)
1951 {
1952 emsg(_(e_function_name_required));
1953 return NULL;
1954 }
1955
1956 // compile the arguments
Bram Moolenaar16900322022-09-08 19:51:45 +01001957 arg = skipwhite(paren + 1);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001958 if (compile_arguments(&arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
1959 return NULL;
1960
Bram Moolenaar16900322022-09-08 19:51:45 +01001961 if (func_idx >= 0)
1962 {
1963 type2_T *argtypes = NULL;
1964 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1965
1966 if (check_internal_func_args(cctx, func_idx, argcount, FALSE,
1967 &argtypes, shuffled_argtypes) == FAIL)
1968 return NULL;
1969 }
1970 else if (check_func_args_from_type(cctx, type, argcount, TRUE,
1971 arg_start) == FAIL)
1972 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001973
Bram Moolenaar806a2732022-09-04 15:40:36 +01001974 defer_var_idx = get_defer_var_idx(cctx);
1975 if (defer_var_idx == 0)
1976 return NULL;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001977 if (generate_DEFER(cctx, defer_var_idx - 1, obj_method, argcount) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001978 return NULL;
1979
1980 return skipwhite(arg);
1981}
1982
1983/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001984 * compile "echo expr"
1985 * compile "echomsg expr"
1986 * compile "echoerr expr"
1987 * compile "echoconsole expr"
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001988 * compile "echowindow expr" - may have cmd_count set
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001989 * compile "execute expr"
1990 */
1991 char_u *
Bram Moolenaar9b994112022-12-25 15:59:25 +00001992compile_mult_expr(
1993 char_u *arg,
1994 int cmdidx,
1995 long cmd_count UNUSED,
1996 cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001997{
1998 char_u *p = arg;
1999 char_u *prev = arg;
2000 char_u *expr_start;
2001 int count = 0;
2002 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002003 type_T *type;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002004 int r = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002005
2006 for (;;)
2007 {
2008 if (ends_excmd2(prev, p))
2009 break;
2010 expr_start = p;
2011 if (compile_expr0(&p, cctx) == FAIL)
2012 return NULL;
2013
2014 if (cctx->ctx_skip != SKIP_YES)
2015 {
2016 // check for non-void type
Bram Moolenaar078a4612022-01-04 15:17:03 +00002017 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002018 if (type->tt_type == VAR_VOID)
2019 {
2020 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
2021 return NULL;
2022 }
2023 }
2024
2025 ++count;
2026 prev = p;
2027 p = skipwhite(p);
2028 }
2029
2030 if (count > 0)
2031 {
2032 long save_lnum = cctx->ctx_lnum;
2033
2034 // Use the line number where the command started.
2035 cctx->ctx_lnum = start_ctx_lnum;
2036
2037 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002038 r = generate_ECHO(cctx, cmdidx == CMD_echo, count);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002039 else if (cmdidx == CMD_execute)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002040 r = generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002041 else if (cmdidx == CMD_echomsg)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002042 r = generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01002043#ifdef HAS_MESSAGE_WINDOW
2044 else if (cmdidx == CMD_echowindow)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002045 r = generate_ECHOWINDOW(cctx, count, cmd_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01002046#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002047 else if (cmdidx == CMD_echoconsole)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002048 r = generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002049 else
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002050 r = generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002051
2052 cctx->ctx_lnum = save_lnum;
2053 }
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002054 return r == OK ? p : NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002055}
2056
2057/*
2058 * If "eap" has a range that is not a constant generate an ISN_RANGE
2059 * instruction to compute it and return OK.
2060 * Otherwise return FAIL, the caller must deal with any range.
2061 */
2062 static int
2063compile_variable_range(exarg_T *eap, cctx_T *cctx)
2064{
2065 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
2066 char_u *p = skipdigits(eap->cmd);
2067
2068 if (p == range_end)
2069 return FAIL;
2070 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
2071}
2072
2073/*
2074 * :put r
2075 * :put ={expr}
2076 */
2077 char_u *
2078compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
2079{
2080 char_u *line = arg;
2081 linenr_T lnum;
2082 char *errormsg;
2083 int above = eap->forceit;
2084
2085 eap->regname = *line;
2086
2087 if (eap->regname == '=')
2088 {
Bram Moolenaard0b7bfa2022-03-12 14:51:16 +00002089 char_u *p = skipwhite(line + 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002090
2091 if (compile_expr0(&p, cctx) == FAIL)
2092 return NULL;
2093 line = p;
2094 }
2095 else if (eap->regname != NUL)
2096 ++line;
2097
2098 if (compile_variable_range(eap, cctx) == OK)
2099 {
2100 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
2101 }
2102 else
2103 {
2104 // Either no range or a number.
2105 // "errormsg" will not be set because the range is ADDR_LINES.
2106 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
2107 // cannot happen
2108 return NULL;
2109 if (eap->addr_count == 0)
2110 lnum = -1;
2111 else
2112 lnum = eap->line2;
2113 if (above)
2114 --lnum;
2115 }
2116
2117 generate_PUT(cctx, eap->regname, lnum);
2118 return line;
2119}
2120
2121/*
2122 * A command that is not compiled, execute with legacy code.
2123 */
2124 char_u *
2125compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
2126{
2127 char_u *line = line_arg;
2128 char_u *p;
2129 int has_expr = FALSE;
2130 char_u *nextcmd = (char_u *)"";
2131 char_u *tofree = NULL;
2132 char_u *cmd_arg = NULL;
2133
2134 if (cctx->ctx_skip == SKIP_YES)
2135 goto theend;
2136
2137 // If there was a prececing command modifier, drop it and include it in the
2138 // EXEC command.
2139 if (cctx->ctx_has_cmdmod)
2140 {
2141 garray_T *instr = &cctx->ctx_instr;
2142 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2143
2144 if (isn->isn_type == ISN_CMDMOD)
2145 {
2146 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
2147 ->cmod_filter_regmatch.regprog);
2148 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2149 --instr->ga_len;
2150 cctx->ctx_has_cmdmod = FALSE;
2151 }
2152 }
2153
2154 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
2155 {
2156 long argt = eap->argt;
2157 int usefilter = FALSE;
2158
2159 has_expr = argt & (EX_XFILE | EX_EXPAND);
2160
2161 // If the command can be followed by a bar, find the bar and truncate
2162 // it, so that the following command can be compiled.
2163 // The '|' is overwritten with a NUL, it is put back below.
2164 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
2165 && *eap->arg == '!')
2166 // :w !filter or :r !filter or :r! filter
2167 usefilter = TRUE;
2168 if ((argt & EX_TRLBAR) && !usefilter)
2169 {
2170 eap->argt = argt;
Bram Moolenaarac485062022-03-23 19:45:01 +00002171 separate_nextcmd(eap, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002172 if (eap->nextcmd != NULL)
2173 nextcmd = eap->nextcmd;
2174 }
2175 else if (eap->cmdidx == CMD_wincmd)
2176 {
2177 p = eap->arg;
2178 if (*p != NUL)
2179 ++p;
2180 if (*p == 'g' || *p == Ctrl_G)
2181 ++p;
2182 p = skipwhite(p);
2183 if (*p == '|')
2184 {
2185 *p = NUL;
2186 nextcmd = p + 1;
2187 }
2188 }
2189 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
2190 {
2191 // If there is a trailing '{' read lines until the '}'
2192 p = eap->arg + STRLEN(eap->arg) - 1;
2193 while (p > eap->arg && VIM_ISWHITE(*p))
2194 --p;
2195 if (*p == '{')
2196 {
2197 exarg_T ea;
Bram Moolenaar62628d92022-02-25 21:10:53 +00002198 int flags = 0; // unused
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002199 int start_lnum = SOURCING_LNUM;
2200
2201 CLEAR_FIELD(ea);
2202 ea.arg = eap->arg;
2203 fill_exarg_from_cctx(&ea, cctx);
2204 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
2205 if (tofree != NULL)
2206 {
2207 *p = NUL;
2208 line = concat_str(line, tofree);
2209 if (line == NULL)
2210 goto theend;
2211 vim_free(tofree);
2212 tofree = line;
2213 SOURCING_LNUM = start_lnum;
2214 }
2215 }
2216 }
2217 }
2218
2219 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
2220 {
2221 // expand filename in "syntax include [@group] filename"
2222 has_expr = TRUE;
2223 eap->arg = skipwhite(eap->arg + 7);
2224 if (*eap->arg == '@')
2225 eap->arg = skiptowhite(eap->arg);
2226 }
2227
2228 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
2229 && STRLEN(eap->arg) > 4)
2230 {
2231 int delim = *eap->arg;
2232
2233 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
2234 if (*p == delim)
2235 cmd_arg = p + 1;
2236 }
2237
2238 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
2239 cmd_arg = eap->arg;
2240
2241 if (cmd_arg != NULL)
2242 {
2243 exarg_T nea;
2244
2245 CLEAR_FIELD(nea);
2246 nea.cmd = cmd_arg;
2247 p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
2248 if (nea.cmdidx < CMD_SIZE)
2249 {
2250 has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
2251 if (has_expr)
2252 eap->arg = skiptowhite(eap->arg);
2253 }
2254 }
2255
2256 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
2257 {
2258 int count = 0;
2259 char_u *start = skipwhite(line);
2260
2261 // :cmd xxx`=expr1`yyy`=expr2`zzz
2262 // PUSHS ":cmd xxx"
2263 // eval expr1
2264 // PUSHS "yyy"
2265 // eval expr2
2266 // PUSHS "zzz"
2267 // EXECCONCAT 5
2268 for (;;)
2269 {
2270 if (p > start)
2271 {
2272 char_u *val = vim_strnsave(start, p - start);
2273
2274 generate_PUSHS(cctx, &val);
2275 ++count;
2276 }
2277 p += 2;
2278 if (compile_expr0(&p, cctx) == FAIL)
2279 return NULL;
2280 may_generate_2STRING(-1, TRUE, cctx);
2281 ++count;
2282 p = skipwhite(p);
2283 if (*p != '`')
2284 {
2285 emsg(_(e_missing_backtick));
2286 return NULL;
2287 }
2288 start = p + 1;
2289
2290 p = (char_u *)strstr((char *)start, "`=");
2291 if (p == NULL)
2292 {
2293 if (*skipwhite(start) != NUL)
2294 {
2295 char_u *val = vim_strsave(start);
2296
2297 generate_PUSHS(cctx, &val);
2298 ++count;
2299 }
2300 break;
2301 }
2302 }
2303 generate_EXECCONCAT(cctx, count);
2304 }
2305 else
2306 generate_EXEC_copy(cctx, ISN_EXEC, line);
2307
2308theend:
2309 if (*nextcmd != NUL)
2310 {
2311 // the parser expects a pointer to the bar, put it back
2312 --nextcmd;
2313 *nextcmd = '|';
2314 }
2315 vim_free(tofree);
2316
2317 return nextcmd;
2318}
2319
2320/*
2321 * A script command with heredoc, e.g.
2322 * ruby << EOF
2323 * command
2324 * EOF
2325 * Has been turned into one long line with NL characters by
2326 * get_function_body():
2327 * ruby << EOF<NL> command<NL>EOF
2328 */
2329 char_u *
2330compile_script(char_u *line, cctx_T *cctx)
2331{
2332 if (cctx->ctx_skip != SKIP_YES)
2333 {
2334 isn_T *isn;
2335
2336 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
2337 return NULL;
2338 isn->isn_arg.string = vim_strsave(line);
2339 }
2340 return (char_u *)"";
2341}
2342
2343
2344/*
2345 * :s/pat/repl/
2346 */
2347 char_u *
2348compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
2349{
2350 char_u *cmd = eap->arg;
2351 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
2352
2353 if (expr != NULL)
2354 {
2355 int delimiter = *cmd++;
2356
2357 // There is a \=expr, find it in the substitute part.
2358 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
2359 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
2360 {
2361 garray_T save_ga = cctx->ctx_instr;
2362 char_u *end;
2363 int expr_res;
2364 int trailing_error;
2365 int instr_count;
2366 isn_T *instr;
2367 isn_T *isn;
2368
2369 cmd += 3;
2370 end = skip_substitute(cmd, delimiter);
2371
2372 // Temporarily reset the list of instructions so that the jump
2373 // labels are correct.
2374 cctx->ctx_instr.ga_len = 0;
2375 cctx->ctx_instr.ga_maxlen = 0;
2376 cctx->ctx_instr.ga_data = NULL;
2377 expr_res = compile_expr0(&cmd, cctx);
2378 if (end[-1] == NUL)
2379 end[-1] = delimiter;
2380 cmd = skipwhite(cmd);
2381 trailing_error = *cmd != delimiter && *cmd != NUL;
2382
2383 if (expr_res == FAIL || trailing_error
2384 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
2385 {
2386 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002387 semsg(_(e_trailing_characters_str), cmd);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002388 clear_instr_ga(&cctx->ctx_instr);
2389 cctx->ctx_instr = save_ga;
2390 return NULL;
2391 }
2392
2393 // Move the generated instructions into the ISN_SUBSTITUTE
2394 // instructions, then restore the list of instructions before
2395 // adding the ISN_SUBSTITUTE instruction.
2396 instr_count = cctx->ctx_instr.ga_len;
2397 instr = cctx->ctx_instr.ga_data;
2398 instr[instr_count].isn_type = ISN_FINISH;
2399
2400 cctx->ctx_instr = save_ga;
2401 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
2402 {
2403 int idx;
2404
2405 for (idx = 0; idx < instr_count; ++idx)
2406 delete_instr(instr + idx);
2407 vim_free(instr);
2408 return NULL;
2409 }
2410 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
2411 isn->isn_arg.subs.subs_instr = instr;
2412
2413 // skip over flags
2414 if (*end == '&')
2415 ++end;
2416 while (ASCII_ISALPHA(*end) || *end == '#')
2417 ++end;
2418 return end;
2419 }
2420 }
2421
2422 return compile_exec(arg, eap, cctx);
2423}
2424
2425 char_u *
2426compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
2427{
2428 char_u *arg = eap->arg;
2429 lhs_T *lhs = &cctx->ctx_redir_lhs;
2430
2431 if (lhs->lhs_name != NULL)
2432 {
2433 if (STRNCMP(arg, "END", 3) == 0)
2434 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002435 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002436 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002437 if (lhs->lhs_append)
2438 {
2439 // First load the current variable value.
2440 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002441 cctx) == FAIL)
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002442 return NULL;
2443 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002444
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002445 // Gets the redirected text and put it on the stack, then store
2446 // it in the variable.
2447 generate_instr_type(cctx, ISN_REDIREND, &t_string);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002448
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002449 if (lhs->lhs_append)
2450 generate_CONCAT(cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002451
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002452 if (lhs->lhs_has_index)
2453 {
2454 // Use the info in "lhs" to store the value at the index in
2455 // the list or dict.
2456 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002457 &t_string, cctx) == FAIL)
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002458 return NULL;
2459 }
2460 else if (generate_store_lhs(cctx, lhs, -1, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002461 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002462
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002463 VIM_CLEAR(lhs->lhs_name);
2464 VIM_CLEAR(lhs->lhs_whole);
2465 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002466 return arg + 3;
2467 }
2468 emsg(_(e_cannot_nest_redir));
2469 return NULL;
2470 }
2471
2472 if (arg[0] == '=' && arg[1] == '>')
2473 {
2474 int append = FALSE;
2475
2476 // redirect to a variable is compiled
2477 arg += 2;
2478 if (*arg == '>')
2479 {
2480 ++arg;
2481 append = TRUE;
2482 }
2483 arg = skipwhite(arg);
2484
2485 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002486 FALSE, FALSE, FALSE, 1, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002487 return NULL;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002488 if (need_type(&t_string, lhs->lhs_member_type, FALSE,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002489 -1, 0, cctx, FALSE, FALSE) == FAIL)
2490 return NULL;
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002491 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002492 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002493 VIM_CLEAR(lhs->lhs_name);
2494 }
2495 else
2496 {
2497 generate_instr(cctx, ISN_REDIRSTART);
2498 lhs->lhs_append = append;
2499 if (lhs->lhs_has_index)
2500 {
2501 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
2502 if (lhs->lhs_whole == NULL)
2503 return NULL;
2504 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002505 }
2506
2507 return arg + lhs->lhs_varlen_total;
2508 }
2509
2510 // other redirects are handled like at script level
2511 return compile_exec(line, eap, cctx);
2512}
2513
2514#if defined(FEAT_QUICKFIX) || defined(PROTO)
2515 char_u *
2516compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
2517{
2518 isn_T *isn;
2519 char_u *p;
2520
2521 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
2522 if (isn == NULL)
2523 return NULL;
2524 isn->isn_arg.number = eap->cmdidx;
2525
2526 p = eap->arg;
2527 if (compile_expr0(&p, cctx) == FAIL)
2528 return NULL;
2529
2530 isn = generate_instr(cctx, ISN_CEXPR_CORE);
2531 if (isn == NULL)
2532 return NULL;
2533 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
2534 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
2535 return NULL;
2536 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
2537 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
2538 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
2539
2540 return p;
2541}
2542#endif
2543
2544/*
2545 * Compile "return [expr]".
2546 * When "legacy" is TRUE evaluate [expr] with legacy syntax
2547 */
2548 char_u *
2549compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
2550{
2551 char_u *p = arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002552 type_T *stack_type;
2553
mityu500c4442022-12-02 18:12:05 +00002554 if (*p != NUL && *p != '|' && *p != '\n'
2555 && (legacy || !vim9_comment_start(p)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002556 {
Bram Moolenaar0bfa8492022-01-22 12:27:04 +00002557 // For a lambda, "return expr" is always used, also when "expr" results
2558 // in a void.
2559 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
2560 && (cctx->ctx_ufunc->uf_flags & FC_LAMBDA) == 0)
Bram Moolenaaref7aadb2022-01-18 18:46:07 +00002561 {
2562 emsg(_(e_returning_value_in_function_without_return_type));
2563 return NULL;
2564 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002565 if (legacy)
2566 {
2567 int save_flags = cmdmod.cmod_flags;
2568
2569 generate_LEGACY_EVAL(cctx, p);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002570 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, FALSE, -1,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002571 0, cctx, FALSE, FALSE) == FAIL)
2572 return NULL;
2573 cmdmod.cmod_flags |= CMOD_LEGACY;
2574 (void)skip_expr(&p, NULL);
2575 cmdmod.cmod_flags = save_flags;
2576 }
2577 else
2578 {
2579 // compile return argument into instructions
2580 if (compile_expr0(&p, cctx) == FAIL)
2581 return NULL;
2582 }
2583
2584 if (cctx->ctx_skip != SKIP_YES)
2585 {
2586 // "check_return_type" with uf_ret_type set to &t_unknown is used
2587 // for an inline function without a specified return type. Set the
2588 // return type here.
Bram Moolenaar078a4612022-01-04 15:17:03 +00002589 stack_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002590 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar1a572e92022-03-15 12:28:10 +00002591 || cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002592 || (!check_return_type
2593 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
2594 {
2595 cctx->ctx_ufunc->uf_ret_type = stack_type;
2596 }
2597 else
2598 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002599 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, FALSE,
2600 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002601 return NULL;
2602 }
2603 }
2604 }
2605 else
2606 {
2607 // "check_return_type" cannot be TRUE, only used for a lambda which
2608 // always has an argument.
2609 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
2610 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
2611 {
2612 emsg(_(e_missing_return_value));
2613 return NULL;
2614 }
2615
2616 // No argument, return zero.
2617 generate_PUSHNR(cctx, 0);
2618 }
2619
Bram Moolenaar8abb5842022-09-17 12:39:58 +01002620 // may need ENDLOOP when inside a :for or :while loop
2621 if (compile_find_scope(NULL, NULL, NULL, NULL, cctx) == FAIL)
2622
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002623 // Undo any command modifiers.
2624 generate_undo_cmdmods(cctx);
2625
2626 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
2627 return NULL;
2628
2629 // "return val | endif" is possible
2630 return skipwhite(p);
2631}
2632
2633/*
2634 * Check if the separator for a :global or :substitute command is OK.
2635 */
2636 int
2637check_global_and_subst(char_u *cmd, char_u *arg)
2638{
2639 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
2640 {
2641 semsg(_(e_separator_not_supported_str), arg);
2642 return FAIL;
2643 }
2644 if (VIM_ISWHITE(cmd[1]))
2645 {
2646 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
2647 return FAIL;
2648 }
2649 return OK;
2650}
2651
2652
2653#endif // defined(FEAT_EVAL)