blob: ecf31dca5bc9c24f251dff5f34f54708f628bba4 [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;
1004 if (*p == ':')
1005 {
1006 p = skipwhite(p + 1);
1007 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
1008 }
1009
1010 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
1011 &vimvaridx, &type, cctx) == FAIL)
1012 goto failed;
1013 if (dest != dest_local)
1014 {
1015 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
1016 0, 0, type, name) == FAIL)
1017 goto failed;
1018 }
1019 else if (varlen == 1 && *arg == '_')
1020 {
1021 // Assigning to "_": drop the value.
1022 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
1023 goto failed;
1024 }
1025 else
1026 {
1027 // Script var is not supported.
1028 if (STRNCMP(name, "s:", 2) == 0)
1029 {
1030 emsg(_(e_cannot_use_script_variable_in_for_loop));
1031 goto failed;
1032 }
1033
1034 if (!valid_varname(arg, (int)varlen, FALSE))
1035 goto failed;
1036 if (lookup_local(arg, varlen, NULL, cctx) == OK)
1037 {
1038 semsg(_(e_variable_already_declared), arg);
1039 goto failed;
1040 }
1041
1042 // Reserve a variable to store "var".
1043 where.wt_index = var_list ? idx + 1 : 0;
1044 where.wt_variable = TRUE;
1045 if (lhs_type == &t_any)
1046 lhs_type = item_type;
1047 else if (item_type != &t_unknown
Bram Moolenaara1c51952022-02-02 16:20:26 +00001048 && need_type_where(item_type, lhs_type, -1,
1049 where, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001050 goto failed;
Bram Moolenaar159b2d52022-10-11 21:41:25 +01001051 var_lvar = reserve_local(cctx, arg, varlen, ASSIGN_FINAL,
Bram Moolenaar6586a012022-09-30 11:04:50 +01001052 lhs_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001053 if (var_lvar == NULL)
1054 // out of memory or used as an argument
1055 goto failed;
1056
1057 if (semicolon && idx == var_count - 1)
1058 var_lvar->lv_type = vartype;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001059 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
1060 }
1061
1062 if (*p == ',' || *p == ';')
1063 ++p;
1064 arg = skipwhite(p);
1065 vim_free(name);
1066 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001067
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001068 // remember the number of variables and closures, used for ENDLOOP
Bram Moolenaarcc341812022-09-19 15:54:34 +01001069 compile_fill_loop_info(&forscope->fs_loop_info, funcref_lvar_idx, cctx);
1070 forscope->fs_loop_info.li_depth = scope->se_loop_depth - 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001071 }
1072
1073 return arg_end;
1074
1075failed:
1076 vim_free(name);
1077 drop_scope(cctx);
1078 return NULL;
1079}
1080
1081/*
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001082 * Used when ending a loop of :for and :while: Generate an ISN_ENDLOOP
1083 * instruction if any variable was declared that could be used by a new
1084 * closure.
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001085 */
1086 static int
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001087compile_loop_end(loop_info_T *loop_info, cctx_T *cctx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001088{
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001089 if (cctx->ctx_locals.ga_len > loop_info->li_local_count
1090 && cctx->ctx_closure_count > loop_info->li_closure_count)
Bram Moolenaarcc341812022-09-19 15:54:34 +01001091 return generate_ENDLOOP(cctx, loop_info);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001092 return OK;
1093}
1094
1095/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001096 * compile "endfor"
1097 */
1098 char_u *
1099compile_endfor(char_u *arg, cctx_T *cctx)
1100{
1101 garray_T *instr = &cctx->ctx_instr;
1102 scope_T *scope = cctx->ctx_scope;
1103 forscope_T *forscope;
1104 isn_T *isn;
1105
1106 if (misplaced_cmdmod(cctx))
1107 return NULL;
1108
1109 if (scope == NULL || scope->se_type != FOR_SCOPE)
1110 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001111 emsg(_(e_endfor_without_for));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001112 return NULL;
1113 }
1114 forscope = &scope->se_u.se_for;
1115 cctx->ctx_scope = scope->se_outer;
1116 if (cctx->ctx_skip != SKIP_YES)
1117 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001118 // Handle the case that any local variables were declared that might be
1119 // used in a closure.
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001120 if (compile_loop_end(&forscope->fs_loop_info, cctx) == FAIL)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001121 return NULL;
1122
Bram Moolenaara275f2c2022-10-11 20:04:09 +01001123 unwind_locals(cctx, scope->se_local_count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001124
1125 // At end of ":for" scope jump back to the FOR instruction.
1126 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
1127
1128 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar2b4ecc22022-01-02 14:08:18 +00001129 // In debug mode an ISN_DEBUG was inserted.
1130 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label
1131 + (cctx->ctx_compile_type == CT_DEBUG ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001132 isn->isn_arg.forloop.for_end = instr->ga_len;
1133
1134 // Fill in the "end" label any BREAK statements
1135 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
1136
1137 // Below the ":for" scope drop the "expr" list from the stack.
1138 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
1139 return NULL;
1140 }
1141
1142 vim_free(scope);
1143
1144 return arg;
1145}
1146
1147/*
1148 * compile "while expr"
1149 *
1150 * Produces instructions:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001151 * top: EVAL expr Push result of "expr"
1152 * WHILE funcref-idx end Jump if false
1153 * ... body ...
1154 * ENDLOOP funcref-idx off count only if closure uses local var
1155 * JUMP top Jump back to repeat
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001156 * end:
1157 *
1158 */
1159 char_u *
1160compile_while(char_u *arg, cctx_T *cctx)
1161{
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001162 char_u *p = arg;
1163 scope_T *scope;
1164 whilescope_T *whilescope;
1165 lvar_T *funcref_lvar;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001166 int funcref_lvar_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001167
1168 scope = new_scope(cctx, WHILE_SCOPE);
1169 if (scope == NULL)
1170 return NULL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001171 if (scope->se_loop_depth == MAX_LOOP_DEPTH)
1172 {
1173 emsg(_(e_loop_nesting_too_deep));
1174 return NULL;
1175 }
1176 ++scope->se_loop_depth;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001177 whilescope = &scope->se_u.se_while;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001178
1179 // "endwhile" jumps back here, one before when profiling or using cmdmods
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001180 whilescope->ws_top_label = current_instr_idx(cctx);
1181
1182 // Reserve a variable to store ec_funcrefs.ga_len, used in ISN_ENDLOOP.
1183 // It is not used when no closures are encountered, we don't know yet.
Bram Moolenaar6586a012022-09-30 11:04:50 +01001184 funcref_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001185 if (funcref_lvar == NULL)
1186 {
1187 drop_scope(cctx);
1188 return NULL; // out of memory
1189 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001190 // get the index before a following reserve_local() makes the lval invalid
1191 funcref_lvar_idx = funcref_lvar->lv_idx;
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001192
1193 // remember the number of variables and closures, used for ENDLOOP
Bram Moolenaarcc341812022-09-19 15:54:34 +01001194 compile_fill_loop_info(&whilescope->ws_loop_info, funcref_lvar_idx, cctx);
1195 whilescope->ws_loop_info.li_depth = scope->se_loop_depth - 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001196
1197 // compile "expr"
1198 if (compile_expr0(&p, cctx) == FAIL)
1199 return NULL;
1200
1201 if (!ends_excmd2(arg, skipwhite(p)))
1202 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001203 semsg(_(e_trailing_characters_str), p);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001204 return NULL;
1205 }
1206
1207 if (cctx->ctx_skip != SKIP_YES)
1208 {
1209 if (bool_on_stack(cctx) == FAIL)
1210 return FAIL;
1211
1212 // CMDMOD_REV must come before the jump
1213 generate_undo_cmdmods(cctx);
1214
1215 // "while_end" is set when ":endwhile" is found
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001216 if (compile_jump_to_end(&whilescope->ws_end_label,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001217 JUMP_WHILE_FALSE, funcref_lvar_idx, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001218 return FAIL;
1219 }
1220
1221 return p;
1222}
1223
1224/*
1225 * compile "endwhile"
1226 */
1227 char_u *
1228compile_endwhile(char_u *arg, cctx_T *cctx)
1229{
1230 scope_T *scope = cctx->ctx_scope;
1231 garray_T *instr = &cctx->ctx_instr;
1232
1233 if (misplaced_cmdmod(cctx))
1234 return NULL;
1235 if (scope == NULL || scope->se_type != WHILE_SCOPE)
1236 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001237 emsg(_(e_endwhile_without_while));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001238 return NULL;
1239 }
1240 cctx->ctx_scope = scope->se_outer;
1241 if (cctx->ctx_skip != SKIP_YES)
1242 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001243 whilescope_T *whilescope = &scope->se_u.se_while;
1244
1245 // Handle the case that any local variables were declared that might be
1246 // used in a closure.
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001247 if (compile_loop_end(&whilescope->ws_loop_info, cctx) == FAIL)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001248 return NULL;
1249
Bram Moolenaara275f2c2022-10-11 20:04:09 +01001250 unwind_locals(cctx, scope->se_local_count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001251
1252#ifdef FEAT_PROFILE
1253 // count the endwhile before jumping
1254 may_generate_prof_end(cctx, cctx->ctx_lnum);
1255#endif
1256
1257 // At end of ":for" scope jump back to the FOR instruction.
1258 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
1259
1260 // Fill in the "end" label in the WHILE statement so it can jump here.
1261 // And in any jumps for ":break"
1262 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
1263 instr->ga_len, cctx);
1264 }
1265
1266 vim_free(scope);
1267
1268 return arg;
1269}
1270
1271/*
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001272 * Get the current information about variables declared inside a loop.
Bram Moolenaarcc341812022-09-19 15:54:34 +01001273 * Returns TRUE if there are any and fills "lvi".
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001274 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01001275 int
1276get_loop_var_info(cctx_T *cctx, loopvarinfo_T *lvi)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001277{
1278 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001279 int prev_local_count = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001280
Bram Moolenaarcc341812022-09-19 15:54:34 +01001281 CLEAR_POINTER(lvi);
1282 for (;;)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001283 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001284 loop_info_T *loopinfo;
1285 int cur_local_last;
1286 int start_local_count;
1287
1288 while (scope != NULL && scope->se_type != WHILE_SCOPE
1289 && scope->se_type != FOR_SCOPE)
1290 scope = scope->se_outer;
1291 if (scope == NULL)
1292 break;
1293
1294 if (scope->se_type == WHILE_SCOPE)
1295 {
1296 loopinfo = &scope->se_u.se_while.ws_loop_info;
1297 // :while reserves one variable for funcref count
1298 cur_local_last = loopinfo->li_local_count - 1;
1299 }
1300 else
1301 {
1302 loopinfo = &scope->se_u.se_for.fs_loop_info;
1303 // :for reserves three variable: loop count, funcref count and loop
1304 // var
1305 cur_local_last = loopinfo->li_local_count - 3;
1306 }
1307
1308 start_local_count = loopinfo->li_local_count;
1309 if (cctx->ctx_locals.ga_len > start_local_count)
1310 {
1311 lvi->lvi_loop[loopinfo->li_depth].var_idx =
1312 (short)start_local_count;
1313 lvi->lvi_loop[loopinfo->li_depth].var_count =
1314 (short)(cctx->ctx_locals.ga_len - start_local_count
1315 - prev_local_count);
1316 if (lvi->lvi_depth == 0)
1317 lvi->lvi_depth = loopinfo->li_depth + 1;
1318 }
1319
1320 scope = scope->se_outer;
1321 prev_local_count = cctx->ctx_locals.ga_len - cur_local_last;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001322 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001323 return lvi->lvi_depth > 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001324}
1325
1326/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01001327 * Get the index of the variable "idx" in a loop, if any.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001328 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01001329 void
1330get_loop_var_idx(cctx_T *cctx, int idx, lvar_T *lvar)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001331{
Bram Moolenaarcc341812022-09-19 15:54:34 +01001332 loopvarinfo_T lvi;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001333
Bram Moolenaarcc341812022-09-19 15:54:34 +01001334 lvar->lv_loop_depth = -1;
1335 lvar->lv_loop_idx = -1;
1336 if (get_loop_var_info(cctx, &lvi))
1337 {
1338 int depth;
1339
1340 for (depth = lvi.lvi_depth - 1; depth >= 0; --depth)
1341 if (idx >= lvi.lvi_loop[depth].var_idx
1342 && idx < lvi.lvi_loop[depth].var_idx
1343 + lvi.lvi_loop[depth].var_count)
1344 {
1345 lvar->lv_loop_depth = depth;
1346 lvar->lv_loop_idx = lvi.lvi_loop[depth].var_idx;
1347 return;
1348 }
1349 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001350}
1351
1352/*
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001353 * Common for :break, :continue and :return
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001354 */
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001355 static int
1356compile_find_scope(
1357 int *loop_label, // where to jump to or NULL
1358 endlabel_T ***el, // end label or NULL
1359 int *try_scopes, // :try scopes encountered or NULL
1360 char *error, // error to use when no scope found
1361 cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001362{
1363 scope_T *scope = cctx->ctx_scope;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001364
1365 for (;;)
1366 {
1367 if (scope == NULL)
1368 {
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001369 if (error != NULL)
1370 emsg(_(error));
1371 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001372 }
1373 if (scope->se_type == FOR_SCOPE)
1374 {
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001375 if (compile_loop_end(&scope->se_u.se_for.fs_loop_info, cctx)
1376 == FAIL)
1377 return FAIL;
1378 if (loop_label != NULL)
1379 *loop_label = scope->se_u.se_for.fs_top_label;
1380 if (el != NULL)
1381 *el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001382 break;
1383 }
1384 if (scope->se_type == WHILE_SCOPE)
1385 {
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001386 if (compile_loop_end(&scope->se_u.se_while.ws_loop_info, cctx)
1387 == FAIL)
1388 return FAIL;
1389 if (loop_label != NULL)
1390 *loop_label = scope->se_u.se_while.ws_top_label;
1391 if (el != NULL)
1392 *el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001393 break;
1394 }
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001395 if (try_scopes != NULL && scope->se_type == TRY_SCOPE)
1396 ++*try_scopes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001397 scope = scope->se_outer;
1398 }
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001399 return OK;
1400}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001401
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001402/*
1403 * compile "continue"
1404 */
1405 char_u *
1406compile_continue(char_u *arg, cctx_T *cctx)
1407{
1408 int try_scopes = 0;
1409 int loop_label;
1410
1411 if (compile_find_scope(&loop_label, NULL, &try_scopes,
1412 e_continue_without_while_or_for, cctx) == FAIL)
1413 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001414 if (try_scopes > 0)
1415 // Inside one or more try/catch blocks we first need to jump to the
1416 // "finally" or "endtry" to cleanup.
1417 generate_TRYCONT(cctx, try_scopes, loop_label);
1418 else
1419 // Jump back to the FOR or WHILE instruction.
1420 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
1421
1422 return arg;
1423}
1424
1425/*
1426 * compile "break"
1427 */
1428 char_u *
1429compile_break(char_u *arg, cctx_T *cctx)
1430{
Bram Moolenaar873f8242022-03-10 21:53:44 +00001431 int try_scopes = 0;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001432 endlabel_T **el;
1433
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001434 if (compile_find_scope(NULL, &el, &try_scopes,
1435 e_break_without_while_or_for, cctx) == FAIL)
1436 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001437
Bram Moolenaar873f8242022-03-10 21:53:44 +00001438 if (try_scopes > 0)
1439 // Inside one or more try/catch blocks we first need to jump to the
1440 // "finally" or "endtry" to cleanup. Then come to the next JUMP
dundargocc57b5bc2022-11-02 13:30:51 +00001441 // instruction, which we don't know the index of yet.
Bram Moolenaar873f8242022-03-10 21:53:44 +00001442 generate_TRYCONT(cctx, try_scopes, cctx->ctx_instr.ga_len + 1);
1443
1444 // Jump to the end of the FOR or WHILE loop. The instruction index will be
1445 // filled in later.
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001446 if (compile_jump_to_end(el, JUMP_ALWAYS, 0, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001447 return FAIL;
1448
1449 return arg;
1450}
1451
1452/*
1453 * compile "{" start of block
1454 */
1455 char_u *
1456compile_block(char_u *arg, cctx_T *cctx)
1457{
1458 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
1459 return NULL;
1460 return skipwhite(arg + 1);
1461}
1462
1463/*
1464 * compile end of block: drop one scope
1465 */
1466 void
1467compile_endblock(cctx_T *cctx)
1468{
1469 scope_T *scope = cctx->ctx_scope;
1470
1471 cctx->ctx_scope = scope->se_outer;
Bram Moolenaara275f2c2022-10-11 20:04:09 +01001472 unwind_locals(cctx, scope->se_local_count, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001473 vim_free(scope);
1474}
1475
1476/*
1477 * Compile "try".
1478 * Creates a new scope for the try-endtry, pointing to the first catch and
1479 * finally.
1480 * Creates another scope for the "try" block itself.
1481 * TRY instruction sets up exception handling at runtime.
1482 *
1483 * "try"
1484 * TRY -> catch1, -> finally push trystack entry
1485 * ... try block
1486 * "throw {exception}"
1487 * EVAL {exception}
1488 * THROW create exception
1489 * ... try block
1490 * " catch {expr}"
1491 * JUMP -> finally
1492 * catch1: PUSH exception
1493 * EVAL {expr}
1494 * MATCH
1495 * JUMP nomatch -> catch2
1496 * CATCH remove exception
1497 * ... catch block
1498 * " catch"
1499 * JUMP -> finally
1500 * catch2: CATCH remove exception
1501 * ... catch block
1502 * " finally"
1503 * finally:
1504 * ... finally block
1505 * " endtry"
1506 * ENDTRY pop trystack entry, may rethrow
1507 */
1508 char_u *
1509compile_try(char_u *arg, cctx_T *cctx)
1510{
1511 garray_T *instr = &cctx->ctx_instr;
1512 scope_T *try_scope;
1513 scope_T *scope;
1514
1515 if (misplaced_cmdmod(cctx))
1516 return NULL;
1517
1518 // scope that holds the jumps that go to catch/finally/endtry
1519 try_scope = new_scope(cctx, TRY_SCOPE);
1520 if (try_scope == NULL)
1521 return NULL;
1522
1523 if (cctx->ctx_skip != SKIP_YES)
1524 {
1525 isn_T *isn;
1526
1527 // "try_catch" is set when the first ":catch" is found or when no catch
1528 // is found and ":finally" is found.
1529 // "try_finally" is set when ":finally" is found
1530 // "try_endtry" is set when ":endtry" is found
1531 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
1532 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
1533 return NULL;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001534 isn->isn_arg.tryref.try_ref = ALLOC_CLEAR_ONE(tryref_T);
1535 if (isn->isn_arg.tryref.try_ref == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001536 return NULL;
1537 }
1538
1539 // scope for the try block itself
1540 scope = new_scope(cctx, BLOCK_SCOPE);
1541 if (scope == NULL)
1542 return NULL;
1543
1544 return arg;
1545}
1546
1547/*
1548 * Compile "catch {expr}".
1549 */
1550 char_u *
1551compile_catch(char_u *arg, cctx_T *cctx UNUSED)
1552{
1553 scope_T *scope = cctx->ctx_scope;
1554 garray_T *instr = &cctx->ctx_instr;
1555 char_u *p;
1556 isn_T *isn;
1557
1558 if (misplaced_cmdmod(cctx))
1559 return NULL;
1560
1561 // end block scope from :try or :catch
1562 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1563 compile_endblock(cctx);
1564 scope = cctx->ctx_scope;
1565
1566 // Error if not in a :try scope
1567 if (scope == NULL || scope->se_type != TRY_SCOPE)
1568 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001569 emsg(_(e_catch_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001570 return NULL;
1571 }
1572
1573 if (scope->se_u.se_try.ts_caught_all)
1574 {
1575 emsg(_(e_catch_unreachable_after_catch_all));
1576 return NULL;
1577 }
Bram Moolenaar53c29612022-01-12 16:18:18 +00001578 if (!cctx->ctx_had_return)
1579 scope->se_u.se_try.ts_no_return = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001580
1581 if (cctx->ctx_skip != SKIP_YES)
1582 {
1583#ifdef FEAT_PROFILE
1584 // the profile-start should be after the jump
1585 if (cctx->ctx_compile_type == CT_PROFILE
1586 && instr->ga_len > 0
1587 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
1588 .isn_type == ISN_PROF_START)
1589 --instr->ga_len;
1590#endif
1591 // Jump from end of previous block to :finally or :endtry
1592 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001593 JUMP_ALWAYS, 0, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001594 return NULL;
1595
1596 // End :try or :catch scope: set value in ISN_TRY instruction
1597 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001598 if (isn->isn_arg.tryref.try_ref->try_catch == 0)
1599 isn->isn_arg.tryref.try_ref->try_catch = instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001600 if (scope->se_u.se_try.ts_catch_label != 0)
1601 {
1602 // Previous catch without match jumps here
1603 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
1604 isn->isn_arg.jump.jump_where = instr->ga_len;
1605 }
1606#ifdef FEAT_PROFILE
1607 if (cctx->ctx_compile_type == CT_PROFILE)
1608 {
1609 // a "throw" that jumps here needs to be counted
1610 generate_instr(cctx, ISN_PROF_END);
1611 // the "catch" is also counted
1612 generate_instr(cctx, ISN_PROF_START);
1613 }
1614#endif
1615 if (cctx->ctx_compile_type == CT_DEBUG)
1616 generate_instr_debug(cctx);
1617 }
1618
1619 p = skipwhite(arg);
1620 if (ends_excmd2(arg, p))
1621 {
1622 scope->se_u.se_try.ts_caught_all = TRUE;
1623 scope->se_u.se_try.ts_catch_label = 0;
1624 }
1625 else
1626 {
1627 char_u *end;
1628 char_u *pat;
1629 char_u *tofree = NULL;
1630 int dropped = 0;
1631 int len;
1632
1633 // Push v:exception, push {expr} and MATCH
1634 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
1635
1636 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
1637 if (*end != *p)
1638 {
1639 semsg(_(e_separator_mismatch_str), p);
1640 vim_free(tofree);
Bram Moolenaar54969f42022-02-07 13:56:44 +00001641 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001642 }
1643 if (tofree == NULL)
1644 len = (int)(end - (p + 1));
1645 else
1646 len = (int)(end - tofree);
1647 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
1648 vim_free(tofree);
1649 p += len + 2 + dropped;
1650 if (pat == NULL)
Bram Moolenaar54969f42022-02-07 13:56:44 +00001651 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001652 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaar54969f42022-02-07 13:56:44 +00001653 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001654
1655 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
1656 return NULL;
1657
1658 scope->se_u.se_try.ts_catch_label = instr->ga_len;
1659 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
1660 return NULL;
1661 }
1662
1663 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
1664 return NULL;
1665
1666 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
1667 return NULL;
1668 return p;
1669}
1670
1671 char_u *
1672compile_finally(char_u *arg, cctx_T *cctx)
1673{
1674 scope_T *scope = cctx->ctx_scope;
1675 garray_T *instr = &cctx->ctx_instr;
1676 isn_T *isn;
1677 int this_instr;
1678
1679 if (misplaced_cmdmod(cctx))
1680 return NULL;
1681
1682 // end block scope from :try or :catch
1683 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1684 compile_endblock(cctx);
1685 scope = cctx->ctx_scope;
1686
1687 // Error if not in a :try scope
1688 if (scope == NULL || scope->se_type != TRY_SCOPE)
1689 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001690 emsg(_(e_finally_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001691 return NULL;
1692 }
1693
1694 if (cctx->ctx_skip != SKIP_YES)
1695 {
1696 // End :catch or :finally scope: set value in ISN_TRY instruction
1697 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001698 if (isn->isn_arg.tryref.try_ref->try_finally != 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001699 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001700 emsg(_(e_multiple_finally));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001701 return NULL;
1702 }
1703
1704 this_instr = instr->ga_len;
1705#ifdef FEAT_PROFILE
1706 if (cctx->ctx_compile_type == CT_PROFILE
1707 && ((isn_T *)instr->ga_data)[this_instr - 1]
1708 .isn_type == ISN_PROF_START)
1709 {
1710 // jump to the profile start of the "finally"
1711 --this_instr;
1712
1713 // jump to the profile end above it
1714 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
1715 .isn_type == ISN_PROF_END)
1716 --this_instr;
1717 }
1718#endif
1719
1720 // Fill in the "end" label in jumps at the end of the blocks.
1721 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
1722 this_instr, cctx);
1723
1724 // If there is no :catch then an exception jumps to :finally.
Bram Moolenaar0d807102021-12-21 09:42:09 +00001725 if (isn->isn_arg.tryref.try_ref->try_catch == 0)
1726 isn->isn_arg.tryref.try_ref->try_catch = this_instr;
1727 isn->isn_arg.tryref.try_ref->try_finally = this_instr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001728 if (scope->se_u.se_try.ts_catch_label != 0)
1729 {
1730 // Previous catch without match jumps here
1731 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
1732 isn->isn_arg.jump.jump_where = this_instr;
1733 scope->se_u.se_try.ts_catch_label = 0;
1734 }
Bram Moolenaar53c29612022-01-12 16:18:18 +00001735 scope->se_u.se_try.ts_has_finally = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001736 if (generate_instr(cctx, ISN_FINALLY) == NULL)
1737 return NULL;
1738 }
1739
1740 return arg;
1741}
1742
1743 char_u *
1744compile_endtry(char_u *arg, cctx_T *cctx)
1745{
1746 scope_T *scope = cctx->ctx_scope;
1747 garray_T *instr = &cctx->ctx_instr;
1748 isn_T *try_isn;
1749
1750 if (misplaced_cmdmod(cctx))
1751 return NULL;
1752
1753 // end block scope from :catch or :finally
1754 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1755 compile_endblock(cctx);
1756 scope = cctx->ctx_scope;
1757
1758 // Error if not in a :try scope
1759 if (scope == NULL || scope->se_type != TRY_SCOPE)
1760 {
1761 if (scope == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001762 emsg(_(e_endtry_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001763 else if (scope->se_type == WHILE_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001764 emsg(_(e_missing_endwhile));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001765 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001766 emsg(_(e_missing_endfor));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001767 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001768 emsg(_(e_missing_endif));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001769 return NULL;
1770 }
1771
1772 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
1773 if (cctx->ctx_skip != SKIP_YES)
1774 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00001775 if (try_isn->isn_arg.tryref.try_ref->try_catch == 0
1776 && try_isn->isn_arg.tryref.try_ref->try_finally == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001777 {
1778 emsg(_(e_missing_catch_or_finally));
1779 return NULL;
1780 }
1781
1782#ifdef FEAT_PROFILE
1783 if (cctx->ctx_compile_type == CT_PROFILE
1784 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
1785 .isn_type == ISN_PROF_START)
1786 // move the profile start after "endtry" so that it's not counted when
1787 // the exception is rethrown.
1788 --instr->ga_len;
1789#endif
1790
1791 // Fill in the "end" label in jumps at the end of the blocks, if not
1792 // done by ":finally".
1793 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
1794 instr->ga_len, cctx);
1795
1796 if (scope->se_u.se_try.ts_catch_label != 0)
1797 {
1798 // Last catch without match jumps here
1799 isn_T *isn = ((isn_T *)instr->ga_data)
1800 + scope->se_u.se_try.ts_catch_label;
1801 isn->isn_arg.jump.jump_where = instr->ga_len;
1802 }
1803 }
1804
Bram Moolenaar53c29612022-01-12 16:18:18 +00001805 // If there is a finally clause that ends in return then we will return.
1806 // If one of the blocks didn't end in "return" or we did not catch all
1807 // exceptions reset the had_return flag.
1808 if (!(scope->se_u.se_try.ts_has_finally && cctx->ctx_had_return)
1809 && (scope->se_u.se_try.ts_no_return
1810 || !scope->se_u.se_try.ts_caught_all))
1811 cctx->ctx_had_return = FALSE;
1812
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001813 compile_endblock(cctx);
1814
1815 if (cctx->ctx_skip != SKIP_YES)
1816 {
1817 // End :catch or :finally scope: set instruction index in ISN_TRY
1818 // instruction
Bram Moolenaar0d807102021-12-21 09:42:09 +00001819 try_isn->isn_arg.tryref.try_ref->try_endtry = instr->ga_len;
Bram Moolenaarfe154992022-03-22 20:42:12 +00001820 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001821 return NULL;
1822#ifdef FEAT_PROFILE
1823 if (cctx->ctx_compile_type == CT_PROFILE)
1824 generate_instr(cctx, ISN_PROF_START);
1825#endif
1826 }
1827 return arg;
1828}
1829
1830/*
1831 * compile "throw {expr}"
1832 */
1833 char_u *
1834compile_throw(char_u *arg, cctx_T *cctx UNUSED)
1835{
1836 char_u *p = skipwhite(arg);
1837
1838 if (compile_expr0(&p, cctx) == FAIL)
1839 return NULL;
1840 if (cctx->ctx_skip == SKIP_YES)
1841 return p;
1842 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1843 return NULL;
1844 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
1845 return NULL;
1846
1847 return p;
1848}
1849
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001850/*
1851 * Compile an expression or function call.
1852 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001853 char_u *
1854compile_eval(char_u *arg, cctx_T *cctx)
1855{
1856 char_u *p = arg;
1857 int name_only;
1858 long lnum = SOURCING_LNUM;
1859
1860 // find_ex_command() will consider a variable name an expression, assuming
1861 // that something follows on the next line. Check that something actually
1862 // follows, otherwise it's probably a misplaced command.
1863 name_only = cmd_is_name_only(arg);
1864
1865 if (compile_expr0(&p, cctx) == FAIL)
1866 return NULL;
1867
1868 if (name_only && lnum == SOURCING_LNUM)
1869 {
1870 semsg(_(e_expression_without_effect_str), arg);
1871 return NULL;
1872 }
1873
1874 // drop the result
1875 generate_instr_drop(cctx, ISN_DROP, 1);
1876
1877 return skipwhite(p);
1878}
1879
1880/*
Bram Moolenaarc572ad52022-09-08 20:49:22 +01001881 * Get the local variable index for deferred function calls.
1882 * Reserve it when not done already.
1883 * Returns zero for failure.
1884 */
1885 int
1886get_defer_var_idx(cctx_T *cctx)
1887{
1888 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1889 + cctx->ctx_ufunc->uf_dfunc_idx;
1890 if (dfunc->df_defer_var_idx == 0)
1891 {
1892 lvar_T *lvar = reserve_local(cctx, (char_u *)"@defer@", 7,
1893 TRUE, &t_list_any);
1894 if (lvar == NULL)
1895 return 0;
1896 dfunc->df_defer_var_idx = lvar->lv_idx + 1;
1897 }
1898 return dfunc->df_defer_var_idx;
1899}
1900
1901/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001902 * Compile "defer func(arg)".
1903 */
1904 char_u *
1905compile_defer(char_u *arg_start, cctx_T *cctx)
1906{
Bram Moolenaar16900322022-09-08 19:51:45 +01001907 char_u *paren;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001908 char_u *arg = arg_start;
1909 int argcount = 0;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001910 int defer_var_idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001911 type_T *type;
1912 int func_idx;
1913
1914 // Get a funcref for the function name.
1915 // TODO: better way to find the "(".
Bram Moolenaar16900322022-09-08 19:51:45 +01001916 paren = vim_strchr(arg, '(');
1917 if (paren == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001918 {
1919 semsg(_(e_missing_parenthesis_str), arg);
1920 return NULL;
1921 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001922 *paren = NUL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001923 func_idx = find_internal_func(arg);
1924 if (func_idx >= 0)
1925 // TODO: better type
1926 generate_PUSHFUNC(cctx, (char_u *)internal_func_name(func_idx),
1927 &t_func_any, FALSE);
1928 else if (compile_expr0(&arg, cctx) == FAIL)
1929 return NULL;
Bram Moolenaar16900322022-09-08 19:51:45 +01001930 *paren = '(';
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001931
1932 // check for function type
1933 type = get_type_on_stack(cctx, 0);
1934 if (type->tt_type != VAR_FUNC)
1935 {
1936 emsg(_(e_function_name_required));
1937 return NULL;
1938 }
1939
1940 // compile the arguments
Bram Moolenaar16900322022-09-08 19:51:45 +01001941 arg = skipwhite(paren + 1);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001942 if (compile_arguments(&arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
1943 return NULL;
1944
Bram Moolenaar16900322022-09-08 19:51:45 +01001945 if (func_idx >= 0)
1946 {
1947 type2_T *argtypes = NULL;
1948 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1949
1950 if (check_internal_func_args(cctx, func_idx, argcount, FALSE,
1951 &argtypes, shuffled_argtypes) == FAIL)
1952 return NULL;
1953 }
1954 else if (check_func_args_from_type(cctx, type, argcount, TRUE,
1955 arg_start) == FAIL)
1956 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001957
Bram Moolenaar806a2732022-09-04 15:40:36 +01001958 defer_var_idx = get_defer_var_idx(cctx);
1959 if (defer_var_idx == 0)
1960 return NULL;
1961 if (generate_DEFER(cctx, defer_var_idx - 1, argcount) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001962 return NULL;
1963
1964 return skipwhite(arg);
1965}
1966
1967/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001968 * compile "echo expr"
1969 * compile "echomsg expr"
1970 * compile "echoerr expr"
1971 * compile "echoconsole expr"
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001972 * compile "echowindow expr" - may have cmd_count set
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001973 * compile "execute expr"
1974 */
1975 char_u *
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001976compile_mult_expr(char_u *arg, int cmdidx, long cmd_count, cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001977{
1978 char_u *p = arg;
1979 char_u *prev = arg;
1980 char_u *expr_start;
1981 int count = 0;
1982 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001983 type_T *type;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001984 int r = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001985
1986 for (;;)
1987 {
1988 if (ends_excmd2(prev, p))
1989 break;
1990 expr_start = p;
1991 if (compile_expr0(&p, cctx) == FAIL)
1992 return NULL;
1993
1994 if (cctx->ctx_skip != SKIP_YES)
1995 {
1996 // check for non-void type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001997 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001998 if (type->tt_type == VAR_VOID)
1999 {
2000 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
2001 return NULL;
2002 }
2003 }
2004
2005 ++count;
2006 prev = p;
2007 p = skipwhite(p);
2008 }
2009
2010 if (count > 0)
2011 {
2012 long save_lnum = cctx->ctx_lnum;
2013
2014 // Use the line number where the command started.
2015 cctx->ctx_lnum = start_ctx_lnum;
2016
2017 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002018 r = generate_ECHO(cctx, cmdidx == CMD_echo, count);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002019 else if (cmdidx == CMD_execute)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002020 r = generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002021 else if (cmdidx == CMD_echomsg)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002022 r = generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01002023#ifdef HAS_MESSAGE_WINDOW
2024 else if (cmdidx == CMD_echowindow)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002025 r = generate_ECHOWINDOW(cctx, count, cmd_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01002026#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002027 else if (cmdidx == CMD_echoconsole)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002028 r = generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002029 else
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002030 r = generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002031
2032 cctx->ctx_lnum = save_lnum;
2033 }
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002034 return r == OK ? p : NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002035}
2036
2037/*
2038 * If "eap" has a range that is not a constant generate an ISN_RANGE
2039 * instruction to compute it and return OK.
2040 * Otherwise return FAIL, the caller must deal with any range.
2041 */
2042 static int
2043compile_variable_range(exarg_T *eap, cctx_T *cctx)
2044{
2045 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
2046 char_u *p = skipdigits(eap->cmd);
2047
2048 if (p == range_end)
2049 return FAIL;
2050 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
2051}
2052
2053/*
2054 * :put r
2055 * :put ={expr}
2056 */
2057 char_u *
2058compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
2059{
2060 char_u *line = arg;
2061 linenr_T lnum;
2062 char *errormsg;
2063 int above = eap->forceit;
2064
2065 eap->regname = *line;
2066
2067 if (eap->regname == '=')
2068 {
Bram Moolenaard0b7bfa2022-03-12 14:51:16 +00002069 char_u *p = skipwhite(line + 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002070
2071 if (compile_expr0(&p, cctx) == FAIL)
2072 return NULL;
2073 line = p;
2074 }
2075 else if (eap->regname != NUL)
2076 ++line;
2077
2078 if (compile_variable_range(eap, cctx) == OK)
2079 {
2080 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
2081 }
2082 else
2083 {
2084 // Either no range or a number.
2085 // "errormsg" will not be set because the range is ADDR_LINES.
2086 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
2087 // cannot happen
2088 return NULL;
2089 if (eap->addr_count == 0)
2090 lnum = -1;
2091 else
2092 lnum = eap->line2;
2093 if (above)
2094 --lnum;
2095 }
2096
2097 generate_PUT(cctx, eap->regname, lnum);
2098 return line;
2099}
2100
2101/*
2102 * A command that is not compiled, execute with legacy code.
2103 */
2104 char_u *
2105compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
2106{
2107 char_u *line = line_arg;
2108 char_u *p;
2109 int has_expr = FALSE;
2110 char_u *nextcmd = (char_u *)"";
2111 char_u *tofree = NULL;
2112 char_u *cmd_arg = NULL;
2113
2114 if (cctx->ctx_skip == SKIP_YES)
2115 goto theend;
2116
2117 // If there was a prececing command modifier, drop it and include it in the
2118 // EXEC command.
2119 if (cctx->ctx_has_cmdmod)
2120 {
2121 garray_T *instr = &cctx->ctx_instr;
2122 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2123
2124 if (isn->isn_type == ISN_CMDMOD)
2125 {
2126 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
2127 ->cmod_filter_regmatch.regprog);
2128 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2129 --instr->ga_len;
2130 cctx->ctx_has_cmdmod = FALSE;
2131 }
2132 }
2133
2134 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
2135 {
2136 long argt = eap->argt;
2137 int usefilter = FALSE;
2138
2139 has_expr = argt & (EX_XFILE | EX_EXPAND);
2140
2141 // If the command can be followed by a bar, find the bar and truncate
2142 // it, so that the following command can be compiled.
2143 // The '|' is overwritten with a NUL, it is put back below.
2144 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
2145 && *eap->arg == '!')
2146 // :w !filter or :r !filter or :r! filter
2147 usefilter = TRUE;
2148 if ((argt & EX_TRLBAR) && !usefilter)
2149 {
2150 eap->argt = argt;
Bram Moolenaarac485062022-03-23 19:45:01 +00002151 separate_nextcmd(eap, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002152 if (eap->nextcmd != NULL)
2153 nextcmd = eap->nextcmd;
2154 }
2155 else if (eap->cmdidx == CMD_wincmd)
2156 {
2157 p = eap->arg;
2158 if (*p != NUL)
2159 ++p;
2160 if (*p == 'g' || *p == Ctrl_G)
2161 ++p;
2162 p = skipwhite(p);
2163 if (*p == '|')
2164 {
2165 *p = NUL;
2166 nextcmd = p + 1;
2167 }
2168 }
2169 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
2170 {
2171 // If there is a trailing '{' read lines until the '}'
2172 p = eap->arg + STRLEN(eap->arg) - 1;
2173 while (p > eap->arg && VIM_ISWHITE(*p))
2174 --p;
2175 if (*p == '{')
2176 {
2177 exarg_T ea;
Bram Moolenaar62628d92022-02-25 21:10:53 +00002178 int flags = 0; // unused
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002179 int start_lnum = SOURCING_LNUM;
2180
2181 CLEAR_FIELD(ea);
2182 ea.arg = eap->arg;
2183 fill_exarg_from_cctx(&ea, cctx);
2184 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
2185 if (tofree != NULL)
2186 {
2187 *p = NUL;
2188 line = concat_str(line, tofree);
2189 if (line == NULL)
2190 goto theend;
2191 vim_free(tofree);
2192 tofree = line;
2193 SOURCING_LNUM = start_lnum;
2194 }
2195 }
2196 }
2197 }
2198
2199 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
2200 {
2201 // expand filename in "syntax include [@group] filename"
2202 has_expr = TRUE;
2203 eap->arg = skipwhite(eap->arg + 7);
2204 if (*eap->arg == '@')
2205 eap->arg = skiptowhite(eap->arg);
2206 }
2207
2208 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
2209 && STRLEN(eap->arg) > 4)
2210 {
2211 int delim = *eap->arg;
2212
2213 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
2214 if (*p == delim)
2215 cmd_arg = p + 1;
2216 }
2217
2218 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
2219 cmd_arg = eap->arg;
2220
2221 if (cmd_arg != NULL)
2222 {
2223 exarg_T nea;
2224
2225 CLEAR_FIELD(nea);
2226 nea.cmd = cmd_arg;
2227 p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
2228 if (nea.cmdidx < CMD_SIZE)
2229 {
2230 has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
2231 if (has_expr)
2232 eap->arg = skiptowhite(eap->arg);
2233 }
2234 }
2235
2236 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
2237 {
2238 int count = 0;
2239 char_u *start = skipwhite(line);
2240
2241 // :cmd xxx`=expr1`yyy`=expr2`zzz
2242 // PUSHS ":cmd xxx"
2243 // eval expr1
2244 // PUSHS "yyy"
2245 // eval expr2
2246 // PUSHS "zzz"
2247 // EXECCONCAT 5
2248 for (;;)
2249 {
2250 if (p > start)
2251 {
2252 char_u *val = vim_strnsave(start, p - start);
2253
2254 generate_PUSHS(cctx, &val);
2255 ++count;
2256 }
2257 p += 2;
2258 if (compile_expr0(&p, cctx) == FAIL)
2259 return NULL;
2260 may_generate_2STRING(-1, TRUE, cctx);
2261 ++count;
2262 p = skipwhite(p);
2263 if (*p != '`')
2264 {
2265 emsg(_(e_missing_backtick));
2266 return NULL;
2267 }
2268 start = p + 1;
2269
2270 p = (char_u *)strstr((char *)start, "`=");
2271 if (p == NULL)
2272 {
2273 if (*skipwhite(start) != NUL)
2274 {
2275 char_u *val = vim_strsave(start);
2276
2277 generate_PUSHS(cctx, &val);
2278 ++count;
2279 }
2280 break;
2281 }
2282 }
2283 generate_EXECCONCAT(cctx, count);
2284 }
2285 else
2286 generate_EXEC_copy(cctx, ISN_EXEC, line);
2287
2288theend:
2289 if (*nextcmd != NUL)
2290 {
2291 // the parser expects a pointer to the bar, put it back
2292 --nextcmd;
2293 *nextcmd = '|';
2294 }
2295 vim_free(tofree);
2296
2297 return nextcmd;
2298}
2299
2300/*
2301 * A script command with heredoc, e.g.
2302 * ruby << EOF
2303 * command
2304 * EOF
2305 * Has been turned into one long line with NL characters by
2306 * get_function_body():
2307 * ruby << EOF<NL> command<NL>EOF
2308 */
2309 char_u *
2310compile_script(char_u *line, cctx_T *cctx)
2311{
2312 if (cctx->ctx_skip != SKIP_YES)
2313 {
2314 isn_T *isn;
2315
2316 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
2317 return NULL;
2318 isn->isn_arg.string = vim_strsave(line);
2319 }
2320 return (char_u *)"";
2321}
2322
2323
2324/*
2325 * :s/pat/repl/
2326 */
2327 char_u *
2328compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
2329{
2330 char_u *cmd = eap->arg;
2331 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
2332
2333 if (expr != NULL)
2334 {
2335 int delimiter = *cmd++;
2336
2337 // There is a \=expr, find it in the substitute part.
2338 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
2339 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
2340 {
2341 garray_T save_ga = cctx->ctx_instr;
2342 char_u *end;
2343 int expr_res;
2344 int trailing_error;
2345 int instr_count;
2346 isn_T *instr;
2347 isn_T *isn;
2348
2349 cmd += 3;
2350 end = skip_substitute(cmd, delimiter);
2351
2352 // Temporarily reset the list of instructions so that the jump
2353 // labels are correct.
2354 cctx->ctx_instr.ga_len = 0;
2355 cctx->ctx_instr.ga_maxlen = 0;
2356 cctx->ctx_instr.ga_data = NULL;
2357 expr_res = compile_expr0(&cmd, cctx);
2358 if (end[-1] == NUL)
2359 end[-1] = delimiter;
2360 cmd = skipwhite(cmd);
2361 trailing_error = *cmd != delimiter && *cmd != NUL;
2362
2363 if (expr_res == FAIL || trailing_error
2364 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
2365 {
2366 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002367 semsg(_(e_trailing_characters_str), cmd);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002368 clear_instr_ga(&cctx->ctx_instr);
2369 cctx->ctx_instr = save_ga;
2370 return NULL;
2371 }
2372
2373 // Move the generated instructions into the ISN_SUBSTITUTE
2374 // instructions, then restore the list of instructions before
2375 // adding the ISN_SUBSTITUTE instruction.
2376 instr_count = cctx->ctx_instr.ga_len;
2377 instr = cctx->ctx_instr.ga_data;
2378 instr[instr_count].isn_type = ISN_FINISH;
2379
2380 cctx->ctx_instr = save_ga;
2381 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
2382 {
2383 int idx;
2384
2385 for (idx = 0; idx < instr_count; ++idx)
2386 delete_instr(instr + idx);
2387 vim_free(instr);
2388 return NULL;
2389 }
2390 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
2391 isn->isn_arg.subs.subs_instr = instr;
2392
2393 // skip over flags
2394 if (*end == '&')
2395 ++end;
2396 while (ASCII_ISALPHA(*end) || *end == '#')
2397 ++end;
2398 return end;
2399 }
2400 }
2401
2402 return compile_exec(arg, eap, cctx);
2403}
2404
2405 char_u *
2406compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
2407{
2408 char_u *arg = eap->arg;
2409 lhs_T *lhs = &cctx->ctx_redir_lhs;
2410
2411 if (lhs->lhs_name != NULL)
2412 {
2413 if (STRNCMP(arg, "END", 3) == 0)
2414 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002415 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002416 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002417 if (lhs->lhs_append)
2418 {
2419 // First load the current variable value.
2420 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002421 cctx) == FAIL)
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002422 return NULL;
2423 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002424
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002425 // Gets the redirected text and put it on the stack, then store
2426 // it in the variable.
2427 generate_instr_type(cctx, ISN_REDIREND, &t_string);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002428
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002429 if (lhs->lhs_append)
2430 generate_CONCAT(cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002431
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002432 if (lhs->lhs_has_index)
2433 {
2434 // Use the info in "lhs" to store the value at the index in
2435 // the list or dict.
2436 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002437 &t_string, cctx) == FAIL)
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002438 return NULL;
2439 }
2440 else if (generate_store_lhs(cctx, lhs, -1, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002441 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002442
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002443 VIM_CLEAR(lhs->lhs_name);
2444 VIM_CLEAR(lhs->lhs_whole);
2445 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002446 return arg + 3;
2447 }
2448 emsg(_(e_cannot_nest_redir));
2449 return NULL;
2450 }
2451
2452 if (arg[0] == '=' && arg[1] == '>')
2453 {
2454 int append = FALSE;
2455
2456 // redirect to a variable is compiled
2457 arg += 2;
2458 if (*arg == '>')
2459 {
2460 ++arg;
2461 append = TRUE;
2462 }
2463 arg = skipwhite(arg);
2464
2465 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002466 FALSE, FALSE, FALSE, 1, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002467 return NULL;
2468 if (need_type(&t_string, lhs->lhs_member_type,
2469 -1, 0, cctx, FALSE, FALSE) == FAIL)
2470 return NULL;
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002471 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002472 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002473 VIM_CLEAR(lhs->lhs_name);
2474 }
2475 else
2476 {
2477 generate_instr(cctx, ISN_REDIRSTART);
2478 lhs->lhs_append = append;
2479 if (lhs->lhs_has_index)
2480 {
2481 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
2482 if (lhs->lhs_whole == NULL)
2483 return NULL;
2484 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002485 }
2486
2487 return arg + lhs->lhs_varlen_total;
2488 }
2489
2490 // other redirects are handled like at script level
2491 return compile_exec(line, eap, cctx);
2492}
2493
2494#if defined(FEAT_QUICKFIX) || defined(PROTO)
2495 char_u *
2496compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
2497{
2498 isn_T *isn;
2499 char_u *p;
2500
2501 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
2502 if (isn == NULL)
2503 return NULL;
2504 isn->isn_arg.number = eap->cmdidx;
2505
2506 p = eap->arg;
2507 if (compile_expr0(&p, cctx) == FAIL)
2508 return NULL;
2509
2510 isn = generate_instr(cctx, ISN_CEXPR_CORE);
2511 if (isn == NULL)
2512 return NULL;
2513 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
2514 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
2515 return NULL;
2516 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
2517 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
2518 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
2519
2520 return p;
2521}
2522#endif
2523
2524/*
2525 * Compile "return [expr]".
2526 * When "legacy" is TRUE evaluate [expr] with legacy syntax
2527 */
2528 char_u *
2529compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
2530{
2531 char_u *p = arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002532 type_T *stack_type;
2533
mityu500c4442022-12-02 18:12:05 +00002534 if (*p != NUL && *p != '|' && *p != '\n'
2535 && (legacy || !vim9_comment_start(p)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002536 {
Bram Moolenaar0bfa8492022-01-22 12:27:04 +00002537 // For a lambda, "return expr" is always used, also when "expr" results
2538 // in a void.
2539 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
2540 && (cctx->ctx_ufunc->uf_flags & FC_LAMBDA) == 0)
Bram Moolenaaref7aadb2022-01-18 18:46:07 +00002541 {
2542 emsg(_(e_returning_value_in_function_without_return_type));
2543 return NULL;
2544 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002545 if (legacy)
2546 {
2547 int save_flags = cmdmod.cmod_flags;
2548
2549 generate_LEGACY_EVAL(cctx, p);
2550 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
2551 0, cctx, FALSE, FALSE) == FAIL)
2552 return NULL;
2553 cmdmod.cmod_flags |= CMOD_LEGACY;
2554 (void)skip_expr(&p, NULL);
2555 cmdmod.cmod_flags = save_flags;
2556 }
2557 else
2558 {
2559 // compile return argument into instructions
2560 if (compile_expr0(&p, cctx) == FAIL)
2561 return NULL;
2562 }
2563
2564 if (cctx->ctx_skip != SKIP_YES)
2565 {
2566 // "check_return_type" with uf_ret_type set to &t_unknown is used
2567 // for an inline function without a specified return type. Set the
2568 // return type here.
Bram Moolenaar078a4612022-01-04 15:17:03 +00002569 stack_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002570 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar1a572e92022-03-15 12:28:10 +00002571 || cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002572 || (!check_return_type
2573 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
2574 {
2575 cctx->ctx_ufunc->uf_ret_type = stack_type;
2576 }
2577 else
2578 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002579 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
2580 0, cctx, FALSE, FALSE) == FAIL)
2581 return NULL;
2582 }
2583 }
2584 }
2585 else
2586 {
2587 // "check_return_type" cannot be TRUE, only used for a lambda which
2588 // always has an argument.
2589 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
2590 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
2591 {
2592 emsg(_(e_missing_return_value));
2593 return NULL;
2594 }
2595
2596 // No argument, return zero.
2597 generate_PUSHNR(cctx, 0);
2598 }
2599
Bram Moolenaar8abb5842022-09-17 12:39:58 +01002600 // may need ENDLOOP when inside a :for or :while loop
2601 if (compile_find_scope(NULL, NULL, NULL, NULL, cctx) == FAIL)
2602
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002603 // Undo any command modifiers.
2604 generate_undo_cmdmods(cctx);
2605
2606 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
2607 return NULL;
2608
2609 // "return val | endif" is possible
2610 return skipwhite(p);
2611}
2612
2613/*
2614 * Check if the separator for a :global or :substitute command is OK.
2615 */
2616 int
2617check_global_and_subst(char_u *cmd, char_u *arg)
2618{
2619 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
2620 {
2621 semsg(_(e_separator_not_supported_str), arg);
2622 return FAIL;
2623 }
2624 if (VIM_ISWHITE(cmd[1]))
2625 {
2626 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
2627 return FAIL;
2628 }
2629 return OK;
2630}
2631
2632
2633#endif // defined(FEAT_EVAL)