blob: f393afeb484291ca2b18f5b97d5f49a7a807076a [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".
60 */
61 static void
62unwind_locals(cctx_T *cctx, int new_top)
63{
64 if (cctx->ctx_locals.ga_len > new_top)
65 {
66 int idx;
67 lvar_T *lvar;
68
69 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
70 {
71 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
72 vim_free(lvar->lv_name);
73 }
74 }
75 cctx->ctx_locals.ga_len = new_top;
76}
77
78/*
79 * Free all local variables.
80 */
81 void
82free_locals(cctx_T *cctx)
83{
84 unwind_locals(cctx, 0);
85 ga_clear(&cctx->ctx_locals);
86}
87
88
89/*
90 * Check if "name" can be "unlet".
91 */
92 int
93check_vim9_unlet(char_u *name)
94{
Bram Moolenaardbdd16b2022-08-14 21:46:07 +010095 if (*name == NUL)
96 {
97 semsg(_(e_argument_required_for_str), "unlet");
98 return FAIL;
99 }
100
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000101 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
102 {
103 // "unlet s:var" is allowed in legacy script.
104 if (*name == 's' && !script_is_vim9())
105 return OK;
106 semsg(_(e_cannot_unlet_str), name);
107 return FAIL;
108 }
109 return OK;
110}
111
112/*
113 * Callback passed to ex_unletlock().
114 */
115 static int
116compile_unlet(
117 lval_T *lvp,
118 char_u *name_end,
119 exarg_T *eap,
120 int deep UNUSED,
121 void *coookie)
122{
123 cctx_T *cctx = coookie;
124 char_u *p = lvp->ll_name;
125 int cc = *name_end;
126 int ret = OK;
127
128 if (cctx->ctx_skip == SKIP_YES)
129 return OK;
130
131 *name_end = NUL;
132 if (*p == '$')
133 {
134 // :unlet $ENV_VAR
135 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
136 }
137 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
138 {
139 lhs_T lhs;
140
141 // This is similar to assigning: lookup the list/dict, compile the
142 // idx/key. Then instead of storing the value unlet the item.
143 // unlet {list}[idx]
144 // unlet {dict}[key] dict.key
145 //
146 // Figure out the LHS type and other properties.
147 //
Bram Moolenaar97f8c102022-04-02 19:43:57 +0100148 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, FALSE, 0, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000149
Bram Moolenaar2ef01d92022-01-06 21:38:11 +0000150 // Use the info in "lhs" to unlet the item at the index in the
151 // list or dict.
152 if (ret == OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000153 {
Bram Moolenaar2ef01d92022-01-06 21:38:11 +0000154 if (!lhs.lhs_has_index)
155 {
156 semsg(_(e_cannot_unlet_imported_item_str), p);
157 ret = FAIL;
158 }
159 else
160 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000161 }
162
163 vim_free(lhs.lhs_name);
164 }
165 else if (check_vim9_unlet(p) == FAIL)
166 {
167 ret = FAIL;
168 }
169 else
170 {
171 // Normal name. Only supports g:, w:, t: and b: namespaces.
172 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
173 }
174
175 *name_end = cc;
176 return ret;
177}
178
179/*
180 * Callback passed to ex_unletlock().
181 */
182 static int
183compile_lock_unlock(
184 lval_T *lvp,
185 char_u *name_end,
186 exarg_T *eap,
Bram Moolenaar70c43d82022-01-26 21:01:15 +0000187 int deep,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000188 void *coookie)
189{
190 cctx_T *cctx = coookie;
191 int cc = *name_end;
192 char_u *p = lvp->ll_name;
193 int ret = OK;
194 size_t len;
195 char_u *buf;
196 isntype_T isn = ISN_EXEC;
Bram Moolenaard1d8f6b2022-08-14 21:28:32 +0100197 char *cmd = eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar";
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000198
199 if (cctx->ctx_skip == SKIP_YES)
200 return OK;
201
Bram Moolenaard1d8f6b2022-08-14 21:28:32 +0100202 if (*p == NUL)
203 {
204 semsg(_(e_argument_required_for_str), cmd);
205 return FAIL;
206 }
207
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000208 // Cannot use :lockvar and :unlockvar on local variables.
209 if (p[1] != ':')
210 {
211 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
212
213 if (lookup_local(p, end - p, NULL, cctx) == OK)
214 {
215 char_u *s = p;
216
217 if (*end != '.' && *end != '[')
218 {
219 emsg(_(e_cannot_lock_unlock_local_variable));
220 return FAIL;
221 }
222
223 // For "d.member" put the local variable on the stack, it will be
224 // passed to ex_lockvar() indirectly.
225 if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
226 return FAIL;
227 isn = ISN_LOCKUNLOCK;
228 }
229 }
230
231 // Checking is done at runtime.
232 *name_end = NUL;
233 len = name_end - p + 20;
234 buf = alloc(len);
235 if (buf == NULL)
236 ret = FAIL;
237 else
238 {
Bram Moolenaare939f5e2022-01-26 21:32:59 +0000239 if (deep < 0)
240 vim_snprintf((char *)buf, len, "%s! %s", cmd, p);
241 else
242 vim_snprintf((char *)buf, len, "%s %d %s", cmd, deep, p);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000243 ret = generate_EXEC_copy(cctx, isn, buf);
244
245 vim_free(buf);
246 *name_end = cc;
247 }
248 return ret;
249}
250
251/*
252 * compile "unlet var", "lock var" and "unlock var"
253 * "arg" points to "var".
254 */
255 char_u *
256compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
257{
Bram Moolenaar70c43d82022-01-26 21:01:15 +0000258 int deep = 0;
259 char_u *p = arg;
260
261 if (eap->cmdidx != CMD_unlet)
262 {
263 if (eap->forceit)
264 deep = -1;
265 else if (vim_isdigit(*p))
266 {
267 deep = getdigits(&p);
268 p = skipwhite(p);
269 }
270 else
271 deep = 2;
272 }
273
274 ex_unletlock(eap, p, deep, GLV_NO_AUTOLOAD | GLV_COMPILING,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000275 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
276 cctx);
277 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
278}
279
280/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100281 * Generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
282 * "funcref_idx" is used for JUMP_WHILE_FALSE
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000283 */
284 static int
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100285compile_jump_to_end(
286 endlabel_T **el,
287 jumpwhen_T when,
288 int funcref_idx,
289 cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000290{
291 garray_T *instr = &cctx->ctx_instr;
292 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
293
294 if (endlabel == NULL)
295 return FAIL;
296 endlabel->el_next = *el;
297 *el = endlabel;
298 endlabel->el_end_label = instr->ga_len;
299
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100300 if (when == JUMP_WHILE_FALSE)
301 generate_WHILE(cctx, funcref_idx);
302 else
303 generate_JUMP(cctx, when, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000304 return OK;
305}
306
307 static void
308compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
309{
310 garray_T *instr = &cctx->ctx_instr;
311
312 while (*el != NULL)
313 {
314 endlabel_T *cur = (*el);
315 isn_T *isn;
316
317 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
318 isn->isn_arg.jump.jump_where = jump_where;
319 *el = cur->el_next;
320 vim_free(cur);
321 }
322}
323
324 static void
325compile_free_jump_to_end(endlabel_T **el)
326{
327 while (*el != NULL)
328 {
329 endlabel_T *cur = (*el);
330
331 *el = cur->el_next;
332 vim_free(cur);
333 }
334}
335
336/*
337 * Create a new scope and set up the generic items.
338 */
339 static scope_T *
340new_scope(cctx_T *cctx, scopetype_T type)
341{
342 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
343
344 if (scope == NULL)
345 return NULL;
346 scope->se_outer = cctx->ctx_scope;
347 cctx->ctx_scope = scope;
348 scope->se_type = type;
349 scope->se_local_count = cctx->ctx_locals.ga_len;
350 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 }
526 unwind_locals(cctx, scope->se_local_count);
527 if (!cctx->ctx_had_return)
528 scope->se_u.se_if.is_had_return = FALSE;
529
530 if (cctx->ctx_skip == SKIP_NOT)
531 {
532 // previous block was executed, this one and following will not
533 cctx->ctx_skip = SKIP_YES;
534 scope->se_u.se_if.is_seen_skip_not = TRUE;
535 }
536 if (scope->se_u.se_if.is_seen_skip_not)
537 {
538 // A previous block was executed, skip over expression and bail out.
539 // Do not count the "elseif" for profiling and cmdmod
540 instr->ga_len = current_instr_idx(cctx);
541
542 skip_expr_cctx(&p, cctx);
543 return p;
544 }
545
546 if (cctx->ctx_skip == SKIP_UNKNOWN)
547 {
548 int moved_cmdmod = FALSE;
549 int saved_debug = FALSE;
550 isn_T debug_isn;
551
552 // Move any CMDMOD instruction to after the jump
553 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
554 {
555 if (GA_GROW_FAILS(instr, 1))
556 return NULL;
557 ((isn_T *)instr->ga_data)[instr->ga_len] =
558 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
559 --instr->ga_len;
560 moved_cmdmod = TRUE;
561 }
562
563 // Remove the already generated ISN_DEBUG, it is written below the
564 // ISN_FOR instruction.
565 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
566 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
567 .isn_type == ISN_DEBUG)
568 {
569 --instr->ga_len;
570 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
571 saved_debug = TRUE;
572 }
573
574 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram 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 }
673 unwind_locals(cctx, scope->se_local_count);
674 if (!cctx->ctx_had_return)
675 scope->se_u.se_if.is_had_return = FALSE;
676 scope->se_u.se_if.is_seen_else = TRUE;
677
678#ifdef FEAT_PROFILE
679 if (cctx->ctx_compile_type == CT_PROFILE)
680 {
681 if (cctx->ctx_skip == SKIP_NOT
682 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
683 .isn_type == ISN_PROF_START)
684 // the previous block was executed, do not count "else" for
685 // profiling
686 --instr->ga_len;
687 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
688 {
689 // the previous block was not executed, this one will, do count the
690 // "else" for profiling
691 cctx->ctx_skip = SKIP_NOT;
692 generate_instr(cctx, ISN_PROF_END);
693 generate_instr(cctx, ISN_PROF_START);
694 cctx->ctx_skip = SKIP_YES;
695 }
696 }
697#endif
698
699 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
700 {
701 // jump from previous block to the end, unless the else block is empty
702 if (cctx->ctx_skip == SKIP_UNKNOWN)
703 {
704 if (!cctx->ctx_had_return
705 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram 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;
745 unwind_locals(cctx, scope->se_local_count);
746 if (!cctx->ctx_had_return)
747 ifscope->is_had_return = FALSE;
748
749 if (scope->se_u.se_if.is_if_label >= 0)
750 {
751 // previous "if" or "elseif" jumps here
752 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
753 isn->isn_arg.jump.jump_where = instr->ga_len;
754 }
755 // Fill in the "end" label in jumps at the end of the blocks.
756 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
757
758#ifdef FEAT_PROFILE
759 // even when skipping we count the endif as executed, unless the block it's
760 // in is skipped
761 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
762 && scope->se_skip_save != SKIP_YES)
763 {
764 cctx->ctx_skip = SKIP_NOT;
765 generate_instr(cctx, ISN_PROF_START);
766 }
767#endif
768 cctx->ctx_skip = scope->se_skip_save;
769
770 // If all the blocks end in :return and there is an :else then the
771 // had_return flag is set.
772 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
773
774 drop_scope(cctx);
775 return arg;
776}
777
778/*
779 * Compile "for var in expr":
780 *
781 * Produces instructions:
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100782 * STORE -1 in loop-idx Set index to -1
783 * EVAL expr Result of "expr" on top of stack
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000784 * top: FOR loop-idx, end Increment index, use list on bottom of stack
785 * - if beyond end, jump to "end"
786 * - otherwise get item from list and push it
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100787 * - store ec_funcrefs in var "loop-idx" + 1
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000788 * STORE var Store item in "var"
789 * ... body ...
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100790 * ENDLOOP funcref-idx off count Only if closure uses local var
791 * JUMP top Jump back to repeat
792 * end: DROP Drop the result of "expr"
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000793 *
794 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
795 * UNPACK 2 Split item in 2
796 * STORE var1 Store item in "var1"
797 * STORE var2 Store item in "var2"
798 */
799 char_u *
800compile_for(char_u *arg_start, cctx_T *cctx)
801{
802 char_u *arg;
803 char_u *arg_end;
804 char_u *name = NULL;
805 char_u *p;
806 char_u *wp;
807 int var_count = 0;
808 int var_list = FALSE;
809 int semicolon = FALSE;
810 size_t varlen;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000811 garray_T *instr = &cctx->ctx_instr;
812 scope_T *scope;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100813 forscope_T *forscope;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000814 lvar_T *loop_lvar; // loop iteration variable
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100815 lvar_T *funcref_lvar;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000816 lvar_T *var_lvar; // variable for "var"
817 type_T *vartype;
818 type_T *item_type = &t_any;
819 int idx;
820 int prev_lnum = cctx->ctx_prev_lnum;
821
822 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
823 if (p == NULL)
824 return NULL;
825 if (var_count == 0)
826 var_count = 1;
827 else
828 var_list = TRUE; // can also be a list of one variable
829
830 // consume "in"
831 wp = p;
832 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
833 return NULL;
834 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
835 {
836 if (*p == ':' && wp != p)
837 semsg(_(e_no_white_space_allowed_before_colon_str), p);
838 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +0000839 emsg(_(e_missing_in_after_for));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000840 return NULL;
841 }
842 wp = p + 2;
843 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
844 return NULL;
845
Bram Moolenaar2b4ecc22022-01-02 14:08:18 +0000846 // Find the already generated ISN_DEBUG to get the line number for the
847 // instruction written below the ISN_FOR instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000848 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
849 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
850 .isn_type == ISN_DEBUG)
851 {
Bram Moolenaar2b4ecc22022-01-02 14:08:18 +0000852 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853 .isn_arg.debug.dbg_break_lnum;
854 }
855
856 scope = new_scope(cctx, FOR_SCOPE);
857 if (scope == NULL)
858 return NULL;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100859 forscope = &scope->se_u.se_for;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000860
861 // Reserve a variable to store the loop iteration counter and initialize it
862 // to -1.
863 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
864 if (loop_lvar == NULL)
865 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000866 drop_scope(cctx);
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100867 return NULL; // out of memory
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000868 }
869 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
870
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100871 // Reserve a variable to store ec_funcrefs.ga_len, used in ISN_ENDLOOP.
872 // The variable index is always the loop var index plus one.
873 // It is not used when no closures are encountered, we don't know yet.
874 funcref_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
875 if (funcref_lvar == NULL)
876 {
877 drop_scope(cctx);
878 return NULL; // out of memory
879 }
880
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000881 // compile "expr", it remains on the stack until "endfor"
882 arg = p;
883 if (compile_expr0(&arg, cctx) == FAIL)
884 {
885 drop_scope(cctx);
886 return NULL;
887 }
888 arg_end = arg;
889
890 if (cctx->ctx_skip != SKIP_YES)
891 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000892 // If we know the type of "var" and it is not a supported type we can
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000893 // give an error now.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000894 vartype = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000895 if (vartype->tt_type != VAR_LIST
896 && vartype->tt_type != VAR_STRING
897 && vartype->tt_type != VAR_BLOB
898 && vartype->tt_type != VAR_ANY
899 && vartype->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000900 {
901 semsg(_(e_for_loop_on_str_not_supported),
902 vartype_name(vartype->tt_type));
903 drop_scope(cctx);
904 return NULL;
905 }
906
907 if (vartype->tt_type == VAR_STRING)
908 item_type = &t_string;
909 else if (vartype->tt_type == VAR_BLOB)
910 item_type = &t_number;
911 else if (vartype->tt_type == VAR_LIST
912 && vartype->tt_member->tt_type != VAR_ANY)
913 {
914 if (!var_list)
915 item_type = vartype->tt_member;
916 else if (vartype->tt_member->tt_type == VAR_LIST
917 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
918 item_type = vartype->tt_member->tt_member;
919 }
920
921 // CMDMOD_REV must come before the FOR instruction.
922 generate_undo_cmdmods(cctx);
923
924 // "for_end" is set when ":endfor" is found
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100925 forscope->fs_top_label = current_instr_idx(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000926
Bram Moolenaar2b4ecc22022-01-02 14:08:18 +0000927 if (cctx->ctx_compile_type == CT_DEBUG)
928 {
929 int save_prev_lnum = cctx->ctx_prev_lnum;
930 isn_T *isn;
931
932 // Add ISN_DEBUG here, before deciding to end the loop. There will
933 // be another ISN_DEBUG before the next instruction.
934 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
935 // Increment the variable count so that the loop variable can be
936 // inspected.
937 cctx->ctx_prev_lnum = prev_lnum;
938 isn = generate_instr_debug(cctx);
939 ++isn->isn_arg.debug.dbg_var_names_len;
940 cctx->ctx_prev_lnum = save_prev_lnum;
941 }
942
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000943 generate_FOR(cctx, loop_lvar->lv_idx);
944
945 arg = arg_start;
946 if (var_list)
947 {
948 generate_UNPACK(cctx, var_count, semicolon);
949 arg = skipwhite(arg + 1); // skip white after '['
950
Bram Moolenaar078a4612022-01-04 15:17:03 +0000951 // drop the list item
952 --cctx->ctx_type_stack.ga_len;
953
954 // add type of the items
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000955 for (idx = 0; idx < var_count; ++idx)
956 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000957 type_T *type = (semicolon && idx == 0) ? vartype : item_type;
958
959 if (push_type_stack(cctx, type) == FAIL)
960 {
961 drop_scope(cctx);
962 return NULL;
963 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000964 }
965 }
966
967 for (idx = 0; idx < var_count; ++idx)
968 {
969 assign_dest_T dest = dest_local;
970 int opt_flags = 0;
971 int vimvaridx = -1;
972 type_T *type = &t_any;
973 type_T *lhs_type = &t_any;
974 where_T where = WHERE_INIT;
975
976 p = skip_var_one(arg, FALSE);
977 varlen = p - arg;
978 name = vim_strnsave(arg, varlen);
979 if (name == NULL)
980 goto failed;
981 if (*p == ':')
982 {
983 p = skipwhite(p + 1);
984 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
985 }
986
987 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
988 &vimvaridx, &type, cctx) == FAIL)
989 goto failed;
990 if (dest != dest_local)
991 {
992 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
993 0, 0, type, name) == FAIL)
994 goto failed;
995 }
996 else if (varlen == 1 && *arg == '_')
997 {
998 // Assigning to "_": drop the value.
999 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
1000 goto failed;
1001 }
1002 else
1003 {
1004 // Script var is not supported.
1005 if (STRNCMP(name, "s:", 2) == 0)
1006 {
1007 emsg(_(e_cannot_use_script_variable_in_for_loop));
1008 goto failed;
1009 }
1010
1011 if (!valid_varname(arg, (int)varlen, FALSE))
1012 goto failed;
1013 if (lookup_local(arg, varlen, NULL, cctx) == OK)
1014 {
1015 semsg(_(e_variable_already_declared), arg);
1016 goto failed;
1017 }
1018
1019 // Reserve a variable to store "var".
1020 where.wt_index = var_list ? idx + 1 : 0;
1021 where.wt_variable = TRUE;
1022 if (lhs_type == &t_any)
1023 lhs_type = item_type;
1024 else if (item_type != &t_unknown
Bram Moolenaara1c51952022-02-02 16:20:26 +00001025 && need_type_where(item_type, lhs_type, -1,
1026 where, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001027 goto failed;
1028 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
1029 if (var_lvar == NULL)
1030 // out of memory or used as an argument
1031 goto failed;
1032
1033 if (semicolon && idx == var_count - 1)
1034 var_lvar->lv_type = vartype;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001035 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
1036 }
1037
1038 if (*p == ',' || *p == ';')
1039 ++p;
1040 arg = skipwhite(p);
1041 vim_free(name);
1042 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001043
1044 forscope->fs_funcref_idx = funcref_lvar->lv_idx;
1045 // remember the number of variables and closures, used in :endfor
1046 forscope->fs_local_count = cctx->ctx_locals.ga_len;
1047 forscope->fs_closure_count = cctx->ctx_closure_count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001048 }
1049
1050 return arg_end;
1051
1052failed:
1053 vim_free(name);
1054 drop_scope(cctx);
1055 return NULL;
1056}
1057
1058/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001059 * At :endfor and :endwhile: Generate an ISN_ENDLOOP instruction if any
1060 * variable was declared that could be used by a new closure.
1061 */
1062 static int
1063compile_loop_end(
1064 int prev_local_count,
1065 int prev_closure_count,
1066 int funcref_idx,
1067 cctx_T *cctx)
1068{
1069 if (cctx->ctx_locals.ga_len > prev_local_count
1070 && cctx->ctx_closure_count > prev_closure_count)
1071 return generate_ENDLOOP(cctx, funcref_idx, prev_local_count);
1072 return OK;
1073}
1074
1075/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001076 * compile "endfor"
1077 */
1078 char_u *
1079compile_endfor(char_u *arg, cctx_T *cctx)
1080{
1081 garray_T *instr = &cctx->ctx_instr;
1082 scope_T *scope = cctx->ctx_scope;
1083 forscope_T *forscope;
1084 isn_T *isn;
1085
1086 if (misplaced_cmdmod(cctx))
1087 return NULL;
1088
1089 if (scope == NULL || scope->se_type != FOR_SCOPE)
1090 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001091 emsg(_(e_endfor_without_for));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001092 return NULL;
1093 }
1094 forscope = &scope->se_u.se_for;
1095 cctx->ctx_scope = scope->se_outer;
1096 if (cctx->ctx_skip != SKIP_YES)
1097 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001098 // Handle the case that any local variables were declared that might be
1099 // used in a closure.
1100 if (compile_loop_end(forscope->fs_local_count,
1101 forscope->fs_closure_count,
1102 forscope->fs_funcref_idx,
1103 cctx) == FAIL)
1104 return NULL;
1105
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001106 unwind_locals(cctx, scope->se_local_count);
1107
1108 // At end of ":for" scope jump back to the FOR instruction.
1109 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
1110
1111 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar2b4ecc22022-01-02 14:08:18 +00001112 // In debug mode an ISN_DEBUG was inserted.
1113 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label
1114 + (cctx->ctx_compile_type == CT_DEBUG ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001115 isn->isn_arg.forloop.for_end = instr->ga_len;
1116
1117 // Fill in the "end" label any BREAK statements
1118 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
1119
1120 // Below the ":for" scope drop the "expr" list from the stack.
1121 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
1122 return NULL;
1123 }
1124
1125 vim_free(scope);
1126
1127 return arg;
1128}
1129
1130/*
1131 * compile "while expr"
1132 *
1133 * Produces instructions:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001134 * top: EVAL expr Push result of "expr"
1135 * WHILE funcref-idx end Jump if false
1136 * ... body ...
1137 * ENDLOOP funcref-idx off count only if closure uses local var
1138 * JUMP top Jump back to repeat
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001139 * end:
1140 *
1141 */
1142 char_u *
1143compile_while(char_u *arg, cctx_T *cctx)
1144{
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001145 char_u *p = arg;
1146 scope_T *scope;
1147 whilescope_T *whilescope;
1148 lvar_T *funcref_lvar;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001149
1150 scope = new_scope(cctx, WHILE_SCOPE);
1151 if (scope == NULL)
1152 return NULL;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001153 whilescope = &scope->se_u.se_while;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001154
1155 // "endwhile" jumps back here, one before when profiling or using cmdmods
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001156 whilescope->ws_top_label = current_instr_idx(cctx);
1157
1158 // Reserve a variable to store ec_funcrefs.ga_len, used in ISN_ENDLOOP.
1159 // It is not used when no closures are encountered, we don't know yet.
1160 funcref_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
1161 if (funcref_lvar == NULL)
1162 {
1163 drop_scope(cctx);
1164 return NULL; // out of memory
1165 }
1166 whilescope->ws_funcref_idx = funcref_lvar->lv_idx;
1167 // remember the number of variables and closures, used in :endwhile
1168 whilescope->ws_local_count = cctx->ctx_locals.ga_len;
1169 whilescope->ws_closure_count = cctx->ctx_closure_count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001170
1171 // compile "expr"
1172 if (compile_expr0(&p, cctx) == FAIL)
1173 return NULL;
1174
1175 if (!ends_excmd2(arg, skipwhite(p)))
1176 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001177 semsg(_(e_trailing_characters_str), p);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001178 return NULL;
1179 }
1180
1181 if (cctx->ctx_skip != SKIP_YES)
1182 {
1183 if (bool_on_stack(cctx) == FAIL)
1184 return FAIL;
1185
1186 // CMDMOD_REV must come before the jump
1187 generate_undo_cmdmods(cctx);
1188
1189 // "while_end" is set when ":endwhile" is found
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001190 if (compile_jump_to_end(&whilescope->ws_end_label,
1191 JUMP_WHILE_FALSE, funcref_lvar->lv_idx, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001192 return FAIL;
1193 }
1194
1195 return p;
1196}
1197
1198/*
1199 * compile "endwhile"
1200 */
1201 char_u *
1202compile_endwhile(char_u *arg, cctx_T *cctx)
1203{
1204 scope_T *scope = cctx->ctx_scope;
1205 garray_T *instr = &cctx->ctx_instr;
1206
1207 if (misplaced_cmdmod(cctx))
1208 return NULL;
1209 if (scope == NULL || scope->se_type != WHILE_SCOPE)
1210 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001211 emsg(_(e_endwhile_without_while));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001212 return NULL;
1213 }
1214 cctx->ctx_scope = scope->se_outer;
1215 if (cctx->ctx_skip != SKIP_YES)
1216 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001217 whilescope_T *whilescope = &scope->se_u.se_while;
1218
1219 // Handle the case that any local variables were declared that might be
1220 // used in a closure.
1221 if (compile_loop_end(whilescope->ws_local_count,
1222 whilescope->ws_closure_count,
1223 whilescope->ws_funcref_idx,
1224 cctx) == FAIL)
1225 return NULL;
1226
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001227 unwind_locals(cctx, scope->se_local_count);
1228
1229#ifdef FEAT_PROFILE
1230 // count the endwhile before jumping
1231 may_generate_prof_end(cctx, cctx->ctx_lnum);
1232#endif
1233
1234 // At end of ":for" scope jump back to the FOR instruction.
1235 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
1236
1237 // Fill in the "end" label in the WHILE statement so it can jump here.
1238 // And in any jumps for ":break"
1239 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
1240 instr->ga_len, cctx);
1241 }
1242
1243 vim_free(scope);
1244
1245 return arg;
1246}
1247
1248/*
1249 * compile "continue"
1250 */
1251 char_u *
1252compile_continue(char_u *arg, cctx_T *cctx)
1253{
1254 scope_T *scope = cctx->ctx_scope;
1255 int try_scopes = 0;
1256 int loop_label;
1257
1258 for (;;)
1259 {
1260 if (scope == NULL)
1261 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001262 emsg(_(e_continue_without_while_or_for));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001263 return NULL;
1264 }
1265 if (scope->se_type == FOR_SCOPE)
1266 {
1267 loop_label = scope->se_u.se_for.fs_top_label;
1268 break;
1269 }
1270 if (scope->se_type == WHILE_SCOPE)
1271 {
1272 loop_label = scope->se_u.se_while.ws_top_label;
1273 break;
1274 }
1275 if (scope->se_type == TRY_SCOPE)
1276 ++try_scopes;
1277 scope = scope->se_outer;
1278 }
1279
1280 if (try_scopes > 0)
1281 // Inside one or more try/catch blocks we first need to jump to the
1282 // "finally" or "endtry" to cleanup.
1283 generate_TRYCONT(cctx, try_scopes, loop_label);
1284 else
1285 // Jump back to the FOR or WHILE instruction.
1286 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
1287
1288 return arg;
1289}
1290
1291/*
1292 * compile "break"
1293 */
1294 char_u *
1295compile_break(char_u *arg, cctx_T *cctx)
1296{
1297 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar873f8242022-03-10 21:53:44 +00001298 int try_scopes = 0;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001299 endlabel_T **el;
1300
1301 for (;;)
1302 {
1303 if (scope == NULL)
1304 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001305 emsg(_(e_break_without_while_or_for));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001306 return NULL;
1307 }
Bram Moolenaar873f8242022-03-10 21:53:44 +00001308 if (scope->se_type == FOR_SCOPE)
1309 {
1310 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001311 break;
Bram Moolenaar873f8242022-03-10 21:53:44 +00001312 }
1313 if (scope->se_type == WHILE_SCOPE)
1314 {
1315 el = &scope->se_u.se_while.ws_end_label;
1316 break;
1317 }
1318 if (scope->se_type == TRY_SCOPE)
1319 ++try_scopes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001320 scope = scope->se_outer;
1321 }
1322
Bram Moolenaar873f8242022-03-10 21:53:44 +00001323 if (try_scopes > 0)
1324 // Inside one or more try/catch blocks we first need to jump to the
1325 // "finally" or "endtry" to cleanup. Then come to the next JUMP
1326 // intruction, which we don't know the index of yet.
1327 generate_TRYCONT(cctx, try_scopes, cctx->ctx_instr.ga_len + 1);
1328
1329 // Jump to the end of the FOR or WHILE loop. The instruction index will be
1330 // filled in later.
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001331 if (compile_jump_to_end(el, JUMP_ALWAYS, 0, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001332 return FAIL;
1333
1334 return arg;
1335}
1336
1337/*
1338 * compile "{" start of block
1339 */
1340 char_u *
1341compile_block(char_u *arg, cctx_T *cctx)
1342{
1343 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
1344 return NULL;
1345 return skipwhite(arg + 1);
1346}
1347
1348/*
1349 * compile end of block: drop one scope
1350 */
1351 void
1352compile_endblock(cctx_T *cctx)
1353{
1354 scope_T *scope = cctx->ctx_scope;
1355
1356 cctx->ctx_scope = scope->se_outer;
1357 unwind_locals(cctx, scope->se_local_count);
1358 vim_free(scope);
1359}
1360
1361/*
1362 * Compile "try".
1363 * Creates a new scope for the try-endtry, pointing to the first catch and
1364 * finally.
1365 * Creates another scope for the "try" block itself.
1366 * TRY instruction sets up exception handling at runtime.
1367 *
1368 * "try"
1369 * TRY -> catch1, -> finally push trystack entry
1370 * ... try block
1371 * "throw {exception}"
1372 * EVAL {exception}
1373 * THROW create exception
1374 * ... try block
1375 * " catch {expr}"
1376 * JUMP -> finally
1377 * catch1: PUSH exception
1378 * EVAL {expr}
1379 * MATCH
1380 * JUMP nomatch -> catch2
1381 * CATCH remove exception
1382 * ... catch block
1383 * " catch"
1384 * JUMP -> finally
1385 * catch2: CATCH remove exception
1386 * ... catch block
1387 * " finally"
1388 * finally:
1389 * ... finally block
1390 * " endtry"
1391 * ENDTRY pop trystack entry, may rethrow
1392 */
1393 char_u *
1394compile_try(char_u *arg, cctx_T *cctx)
1395{
1396 garray_T *instr = &cctx->ctx_instr;
1397 scope_T *try_scope;
1398 scope_T *scope;
1399
1400 if (misplaced_cmdmod(cctx))
1401 return NULL;
1402
1403 // scope that holds the jumps that go to catch/finally/endtry
1404 try_scope = new_scope(cctx, TRY_SCOPE);
1405 if (try_scope == NULL)
1406 return NULL;
1407
1408 if (cctx->ctx_skip != SKIP_YES)
1409 {
1410 isn_T *isn;
1411
1412 // "try_catch" is set when the first ":catch" is found or when no catch
1413 // is found and ":finally" is found.
1414 // "try_finally" is set when ":finally" is found
1415 // "try_endtry" is set when ":endtry" is found
1416 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
1417 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
1418 return NULL;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001419 isn->isn_arg.tryref.try_ref = ALLOC_CLEAR_ONE(tryref_T);
1420 if (isn->isn_arg.tryref.try_ref == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001421 return NULL;
1422 }
1423
1424 // scope for the try block itself
1425 scope = new_scope(cctx, BLOCK_SCOPE);
1426 if (scope == NULL)
1427 return NULL;
1428
1429 return arg;
1430}
1431
1432/*
1433 * Compile "catch {expr}".
1434 */
1435 char_u *
1436compile_catch(char_u *arg, cctx_T *cctx UNUSED)
1437{
1438 scope_T *scope = cctx->ctx_scope;
1439 garray_T *instr = &cctx->ctx_instr;
1440 char_u *p;
1441 isn_T *isn;
1442
1443 if (misplaced_cmdmod(cctx))
1444 return NULL;
1445
1446 // end block scope from :try or :catch
1447 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1448 compile_endblock(cctx);
1449 scope = cctx->ctx_scope;
1450
1451 // Error if not in a :try scope
1452 if (scope == NULL || scope->se_type != TRY_SCOPE)
1453 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001454 emsg(_(e_catch_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001455 return NULL;
1456 }
1457
1458 if (scope->se_u.se_try.ts_caught_all)
1459 {
1460 emsg(_(e_catch_unreachable_after_catch_all));
1461 return NULL;
1462 }
Bram Moolenaar53c29612022-01-12 16:18:18 +00001463 if (!cctx->ctx_had_return)
1464 scope->se_u.se_try.ts_no_return = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001465
1466 if (cctx->ctx_skip != SKIP_YES)
1467 {
1468#ifdef FEAT_PROFILE
1469 // the profile-start should be after the jump
1470 if (cctx->ctx_compile_type == CT_PROFILE
1471 && instr->ga_len > 0
1472 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
1473 .isn_type == ISN_PROF_START)
1474 --instr->ga_len;
1475#endif
1476 // Jump from end of previous block to :finally or :endtry
1477 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001478 JUMP_ALWAYS, 0, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001479 return NULL;
1480
1481 // End :try or :catch scope: set value in ISN_TRY instruction
1482 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001483 if (isn->isn_arg.tryref.try_ref->try_catch == 0)
1484 isn->isn_arg.tryref.try_ref->try_catch = instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001485 if (scope->se_u.se_try.ts_catch_label != 0)
1486 {
1487 // Previous catch without match jumps here
1488 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
1489 isn->isn_arg.jump.jump_where = instr->ga_len;
1490 }
1491#ifdef FEAT_PROFILE
1492 if (cctx->ctx_compile_type == CT_PROFILE)
1493 {
1494 // a "throw" that jumps here needs to be counted
1495 generate_instr(cctx, ISN_PROF_END);
1496 // the "catch" is also counted
1497 generate_instr(cctx, ISN_PROF_START);
1498 }
1499#endif
1500 if (cctx->ctx_compile_type == CT_DEBUG)
1501 generate_instr_debug(cctx);
1502 }
1503
1504 p = skipwhite(arg);
1505 if (ends_excmd2(arg, p))
1506 {
1507 scope->se_u.se_try.ts_caught_all = TRUE;
1508 scope->se_u.se_try.ts_catch_label = 0;
1509 }
1510 else
1511 {
1512 char_u *end;
1513 char_u *pat;
1514 char_u *tofree = NULL;
1515 int dropped = 0;
1516 int len;
1517
1518 // Push v:exception, push {expr} and MATCH
1519 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
1520
1521 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
1522 if (*end != *p)
1523 {
1524 semsg(_(e_separator_mismatch_str), p);
1525 vim_free(tofree);
Bram Moolenaar54969f42022-02-07 13:56:44 +00001526 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001527 }
1528 if (tofree == NULL)
1529 len = (int)(end - (p + 1));
1530 else
1531 len = (int)(end - tofree);
1532 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
1533 vim_free(tofree);
1534 p += len + 2 + dropped;
1535 if (pat == NULL)
Bram Moolenaar54969f42022-02-07 13:56:44 +00001536 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001537 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaar54969f42022-02-07 13:56:44 +00001538 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001539
1540 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
1541 return NULL;
1542
1543 scope->se_u.se_try.ts_catch_label = instr->ga_len;
1544 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
1545 return NULL;
1546 }
1547
1548 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
1549 return NULL;
1550
1551 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
1552 return NULL;
1553 return p;
1554}
1555
1556 char_u *
1557compile_finally(char_u *arg, cctx_T *cctx)
1558{
1559 scope_T *scope = cctx->ctx_scope;
1560 garray_T *instr = &cctx->ctx_instr;
1561 isn_T *isn;
1562 int this_instr;
1563
1564 if (misplaced_cmdmod(cctx))
1565 return NULL;
1566
1567 // end block scope from :try or :catch
1568 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1569 compile_endblock(cctx);
1570 scope = cctx->ctx_scope;
1571
1572 // Error if not in a :try scope
1573 if (scope == NULL || scope->se_type != TRY_SCOPE)
1574 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001575 emsg(_(e_finally_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001576 return NULL;
1577 }
1578
1579 if (cctx->ctx_skip != SKIP_YES)
1580 {
1581 // End :catch or :finally scope: set value in ISN_TRY instruction
1582 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001583 if (isn->isn_arg.tryref.try_ref->try_finally != 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001584 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001585 emsg(_(e_multiple_finally));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001586 return NULL;
1587 }
1588
1589 this_instr = instr->ga_len;
1590#ifdef FEAT_PROFILE
1591 if (cctx->ctx_compile_type == CT_PROFILE
1592 && ((isn_T *)instr->ga_data)[this_instr - 1]
1593 .isn_type == ISN_PROF_START)
1594 {
1595 // jump to the profile start of the "finally"
1596 --this_instr;
1597
1598 // jump to the profile end above it
1599 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
1600 .isn_type == ISN_PROF_END)
1601 --this_instr;
1602 }
1603#endif
1604
1605 // Fill in the "end" label in jumps at the end of the blocks.
1606 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
1607 this_instr, cctx);
1608
1609 // If there is no :catch then an exception jumps to :finally.
Bram Moolenaar0d807102021-12-21 09:42:09 +00001610 if (isn->isn_arg.tryref.try_ref->try_catch == 0)
1611 isn->isn_arg.tryref.try_ref->try_catch = this_instr;
1612 isn->isn_arg.tryref.try_ref->try_finally = this_instr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001613 if (scope->se_u.se_try.ts_catch_label != 0)
1614 {
1615 // Previous catch without match jumps here
1616 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
1617 isn->isn_arg.jump.jump_where = this_instr;
1618 scope->se_u.se_try.ts_catch_label = 0;
1619 }
Bram Moolenaar53c29612022-01-12 16:18:18 +00001620 scope->se_u.se_try.ts_has_finally = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001621 if (generate_instr(cctx, ISN_FINALLY) == NULL)
1622 return NULL;
1623 }
1624
1625 return arg;
1626}
1627
1628 char_u *
1629compile_endtry(char_u *arg, cctx_T *cctx)
1630{
1631 scope_T *scope = cctx->ctx_scope;
1632 garray_T *instr = &cctx->ctx_instr;
1633 isn_T *try_isn;
1634
1635 if (misplaced_cmdmod(cctx))
1636 return NULL;
1637
1638 // end block scope from :catch or :finally
1639 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1640 compile_endblock(cctx);
1641 scope = cctx->ctx_scope;
1642
1643 // Error if not in a :try scope
1644 if (scope == NULL || scope->se_type != TRY_SCOPE)
1645 {
1646 if (scope == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001647 emsg(_(e_endtry_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001648 else if (scope->se_type == WHILE_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001649 emsg(_(e_missing_endwhile));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001650 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001651 emsg(_(e_missing_endfor));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001652 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001653 emsg(_(e_missing_endif));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001654 return NULL;
1655 }
1656
1657 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
1658 if (cctx->ctx_skip != SKIP_YES)
1659 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00001660 if (try_isn->isn_arg.tryref.try_ref->try_catch == 0
1661 && try_isn->isn_arg.tryref.try_ref->try_finally == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001662 {
1663 emsg(_(e_missing_catch_or_finally));
1664 return NULL;
1665 }
1666
1667#ifdef FEAT_PROFILE
1668 if (cctx->ctx_compile_type == CT_PROFILE
1669 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
1670 .isn_type == ISN_PROF_START)
1671 // move the profile start after "endtry" so that it's not counted when
1672 // the exception is rethrown.
1673 --instr->ga_len;
1674#endif
1675
1676 // Fill in the "end" label in jumps at the end of the blocks, if not
1677 // done by ":finally".
1678 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
1679 instr->ga_len, cctx);
1680
1681 if (scope->se_u.se_try.ts_catch_label != 0)
1682 {
1683 // Last catch without match jumps here
1684 isn_T *isn = ((isn_T *)instr->ga_data)
1685 + scope->se_u.se_try.ts_catch_label;
1686 isn->isn_arg.jump.jump_where = instr->ga_len;
1687 }
1688 }
1689
Bram Moolenaar53c29612022-01-12 16:18:18 +00001690 // If there is a finally clause that ends in return then we will return.
1691 // If one of the blocks didn't end in "return" or we did not catch all
1692 // exceptions reset the had_return flag.
1693 if (!(scope->se_u.se_try.ts_has_finally && cctx->ctx_had_return)
1694 && (scope->se_u.se_try.ts_no_return
1695 || !scope->se_u.se_try.ts_caught_all))
1696 cctx->ctx_had_return = FALSE;
1697
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001698 compile_endblock(cctx);
1699
1700 if (cctx->ctx_skip != SKIP_YES)
1701 {
1702 // End :catch or :finally scope: set instruction index in ISN_TRY
1703 // instruction
Bram Moolenaar0d807102021-12-21 09:42:09 +00001704 try_isn->isn_arg.tryref.try_ref->try_endtry = instr->ga_len;
Bram Moolenaarfe154992022-03-22 20:42:12 +00001705 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001706 return NULL;
1707#ifdef FEAT_PROFILE
1708 if (cctx->ctx_compile_type == CT_PROFILE)
1709 generate_instr(cctx, ISN_PROF_START);
1710#endif
1711 }
1712 return arg;
1713}
1714
1715/*
1716 * compile "throw {expr}"
1717 */
1718 char_u *
1719compile_throw(char_u *arg, cctx_T *cctx UNUSED)
1720{
1721 char_u *p = skipwhite(arg);
1722
1723 if (compile_expr0(&p, cctx) == FAIL)
1724 return NULL;
1725 if (cctx->ctx_skip == SKIP_YES)
1726 return p;
1727 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1728 return NULL;
1729 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
1730 return NULL;
1731
1732 return p;
1733}
1734
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001735/*
1736 * Compile an expression or function call.
1737 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001738 char_u *
1739compile_eval(char_u *arg, cctx_T *cctx)
1740{
1741 char_u *p = arg;
1742 int name_only;
1743 long lnum = SOURCING_LNUM;
1744
1745 // find_ex_command() will consider a variable name an expression, assuming
1746 // that something follows on the next line. Check that something actually
1747 // follows, otherwise it's probably a misplaced command.
1748 name_only = cmd_is_name_only(arg);
1749
1750 if (compile_expr0(&p, cctx) == FAIL)
1751 return NULL;
1752
1753 if (name_only && lnum == SOURCING_LNUM)
1754 {
1755 semsg(_(e_expression_without_effect_str), arg);
1756 return NULL;
1757 }
1758
1759 // drop the result
1760 generate_instr_drop(cctx, ISN_DROP, 1);
1761
1762 return skipwhite(p);
1763}
1764
1765/*
Bram Moolenaarc572ad52022-09-08 20:49:22 +01001766 * Get the local variable index for deferred function calls.
1767 * Reserve it when not done already.
1768 * Returns zero for failure.
1769 */
1770 int
1771get_defer_var_idx(cctx_T *cctx)
1772{
1773 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1774 + cctx->ctx_ufunc->uf_dfunc_idx;
1775 if (dfunc->df_defer_var_idx == 0)
1776 {
1777 lvar_T *lvar = reserve_local(cctx, (char_u *)"@defer@", 7,
1778 TRUE, &t_list_any);
1779 if (lvar == NULL)
1780 return 0;
1781 dfunc->df_defer_var_idx = lvar->lv_idx + 1;
1782 }
1783 return dfunc->df_defer_var_idx;
1784}
1785
1786/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001787 * Compile "defer func(arg)".
1788 */
1789 char_u *
1790compile_defer(char_u *arg_start, cctx_T *cctx)
1791{
Bram Moolenaar16900322022-09-08 19:51:45 +01001792 char_u *paren;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001793 char_u *arg = arg_start;
1794 int argcount = 0;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001795 int defer_var_idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001796 type_T *type;
1797 int func_idx;
1798
1799 // Get a funcref for the function name.
1800 // TODO: better way to find the "(".
Bram Moolenaar16900322022-09-08 19:51:45 +01001801 paren = vim_strchr(arg, '(');
1802 if (paren == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001803 {
1804 semsg(_(e_missing_parenthesis_str), arg);
1805 return NULL;
1806 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001807 *paren = NUL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001808 func_idx = find_internal_func(arg);
1809 if (func_idx >= 0)
1810 // TODO: better type
1811 generate_PUSHFUNC(cctx, (char_u *)internal_func_name(func_idx),
1812 &t_func_any, FALSE);
1813 else if (compile_expr0(&arg, cctx) == FAIL)
1814 return NULL;
Bram Moolenaar16900322022-09-08 19:51:45 +01001815 *paren = '(';
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001816
1817 // check for function type
1818 type = get_type_on_stack(cctx, 0);
1819 if (type->tt_type != VAR_FUNC)
1820 {
1821 emsg(_(e_function_name_required));
1822 return NULL;
1823 }
1824
1825 // compile the arguments
Bram Moolenaar16900322022-09-08 19:51:45 +01001826 arg = skipwhite(paren + 1);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001827 if (compile_arguments(&arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
1828 return NULL;
1829
Bram Moolenaar16900322022-09-08 19:51:45 +01001830 if (func_idx >= 0)
1831 {
1832 type2_T *argtypes = NULL;
1833 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1834
1835 if (check_internal_func_args(cctx, func_idx, argcount, FALSE,
1836 &argtypes, shuffled_argtypes) == FAIL)
1837 return NULL;
1838 }
1839 else if (check_func_args_from_type(cctx, type, argcount, TRUE,
1840 arg_start) == FAIL)
1841 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001842
Bram Moolenaar806a2732022-09-04 15:40:36 +01001843 defer_var_idx = get_defer_var_idx(cctx);
1844 if (defer_var_idx == 0)
1845 return NULL;
1846 if (generate_DEFER(cctx, defer_var_idx - 1, argcount) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001847 return NULL;
1848
1849 return skipwhite(arg);
1850}
1851
1852/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001853 * compile "echo expr"
1854 * compile "echomsg expr"
1855 * compile "echoerr expr"
1856 * compile "echoconsole expr"
1857 * compile "execute expr"
1858 */
1859 char_u *
1860compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
1861{
1862 char_u *p = arg;
1863 char_u *prev = arg;
1864 char_u *expr_start;
1865 int count = 0;
1866 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001867 type_T *type;
1868
1869 for (;;)
1870 {
1871 if (ends_excmd2(prev, p))
1872 break;
1873 expr_start = p;
1874 if (compile_expr0(&p, cctx) == FAIL)
1875 return NULL;
1876
1877 if (cctx->ctx_skip != SKIP_YES)
1878 {
1879 // check for non-void type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001880 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001881 if (type->tt_type == VAR_VOID)
1882 {
1883 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
1884 return NULL;
1885 }
1886 }
1887
1888 ++count;
1889 prev = p;
1890 p = skipwhite(p);
1891 }
1892
1893 if (count > 0)
1894 {
1895 long save_lnum = cctx->ctx_lnum;
1896
1897 // Use the line number where the command started.
1898 cctx->ctx_lnum = start_ctx_lnum;
1899
1900 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
1901 generate_ECHO(cctx, cmdidx == CMD_echo, count);
1902 else if (cmdidx == CMD_execute)
1903 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
1904 else if (cmdidx == CMD_echomsg)
1905 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01001906#ifdef HAS_MESSAGE_WINDOW
1907 else if (cmdidx == CMD_echowindow)
1908 generate_MULT_EXPR(cctx, ISN_ECHOWINDOW, count);
1909#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001910 else if (cmdidx == CMD_echoconsole)
1911 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
1912 else
1913 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
1914
1915 cctx->ctx_lnum = save_lnum;
1916 }
1917 return p;
1918}
1919
1920/*
1921 * If "eap" has a range that is not a constant generate an ISN_RANGE
1922 * instruction to compute it and return OK.
1923 * Otherwise return FAIL, the caller must deal with any range.
1924 */
1925 static int
1926compile_variable_range(exarg_T *eap, cctx_T *cctx)
1927{
1928 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
1929 char_u *p = skipdigits(eap->cmd);
1930
1931 if (p == range_end)
1932 return FAIL;
1933 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
1934}
1935
1936/*
1937 * :put r
1938 * :put ={expr}
1939 */
1940 char_u *
1941compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
1942{
1943 char_u *line = arg;
1944 linenr_T lnum;
1945 char *errormsg;
1946 int above = eap->forceit;
1947
1948 eap->regname = *line;
1949
1950 if (eap->regname == '=')
1951 {
Bram Moolenaard0b7bfa2022-03-12 14:51:16 +00001952 char_u *p = skipwhite(line + 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001953
1954 if (compile_expr0(&p, cctx) == FAIL)
1955 return NULL;
1956 line = p;
1957 }
1958 else if (eap->regname != NUL)
1959 ++line;
1960
1961 if (compile_variable_range(eap, cctx) == OK)
1962 {
1963 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
1964 }
1965 else
1966 {
1967 // Either no range or a number.
1968 // "errormsg" will not be set because the range is ADDR_LINES.
1969 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
1970 // cannot happen
1971 return NULL;
1972 if (eap->addr_count == 0)
1973 lnum = -1;
1974 else
1975 lnum = eap->line2;
1976 if (above)
1977 --lnum;
1978 }
1979
1980 generate_PUT(cctx, eap->regname, lnum);
1981 return line;
1982}
1983
1984/*
1985 * A command that is not compiled, execute with legacy code.
1986 */
1987 char_u *
1988compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
1989{
1990 char_u *line = line_arg;
1991 char_u *p;
1992 int has_expr = FALSE;
1993 char_u *nextcmd = (char_u *)"";
1994 char_u *tofree = NULL;
1995 char_u *cmd_arg = NULL;
1996
1997 if (cctx->ctx_skip == SKIP_YES)
1998 goto theend;
1999
2000 // If there was a prececing command modifier, drop it and include it in the
2001 // EXEC command.
2002 if (cctx->ctx_has_cmdmod)
2003 {
2004 garray_T *instr = &cctx->ctx_instr;
2005 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2006
2007 if (isn->isn_type == ISN_CMDMOD)
2008 {
2009 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
2010 ->cmod_filter_regmatch.regprog);
2011 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2012 --instr->ga_len;
2013 cctx->ctx_has_cmdmod = FALSE;
2014 }
2015 }
2016
2017 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
2018 {
2019 long argt = eap->argt;
2020 int usefilter = FALSE;
2021
2022 has_expr = argt & (EX_XFILE | EX_EXPAND);
2023
2024 // If the command can be followed by a bar, find the bar and truncate
2025 // it, so that the following command can be compiled.
2026 // The '|' is overwritten with a NUL, it is put back below.
2027 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
2028 && *eap->arg == '!')
2029 // :w !filter or :r !filter or :r! filter
2030 usefilter = TRUE;
2031 if ((argt & EX_TRLBAR) && !usefilter)
2032 {
2033 eap->argt = argt;
Bram Moolenaarac485062022-03-23 19:45:01 +00002034 separate_nextcmd(eap, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002035 if (eap->nextcmd != NULL)
2036 nextcmd = eap->nextcmd;
2037 }
2038 else if (eap->cmdidx == CMD_wincmd)
2039 {
2040 p = eap->arg;
2041 if (*p != NUL)
2042 ++p;
2043 if (*p == 'g' || *p == Ctrl_G)
2044 ++p;
2045 p = skipwhite(p);
2046 if (*p == '|')
2047 {
2048 *p = NUL;
2049 nextcmd = p + 1;
2050 }
2051 }
2052 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
2053 {
2054 // If there is a trailing '{' read lines until the '}'
2055 p = eap->arg + STRLEN(eap->arg) - 1;
2056 while (p > eap->arg && VIM_ISWHITE(*p))
2057 --p;
2058 if (*p == '{')
2059 {
2060 exarg_T ea;
Bram Moolenaar62628d92022-02-25 21:10:53 +00002061 int flags = 0; // unused
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002062 int start_lnum = SOURCING_LNUM;
2063
2064 CLEAR_FIELD(ea);
2065 ea.arg = eap->arg;
2066 fill_exarg_from_cctx(&ea, cctx);
2067 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
2068 if (tofree != NULL)
2069 {
2070 *p = NUL;
2071 line = concat_str(line, tofree);
2072 if (line == NULL)
2073 goto theend;
2074 vim_free(tofree);
2075 tofree = line;
2076 SOURCING_LNUM = start_lnum;
2077 }
2078 }
2079 }
2080 }
2081
2082 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
2083 {
2084 // expand filename in "syntax include [@group] filename"
2085 has_expr = TRUE;
2086 eap->arg = skipwhite(eap->arg + 7);
2087 if (*eap->arg == '@')
2088 eap->arg = skiptowhite(eap->arg);
2089 }
2090
2091 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
2092 && STRLEN(eap->arg) > 4)
2093 {
2094 int delim = *eap->arg;
2095
2096 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
2097 if (*p == delim)
2098 cmd_arg = p + 1;
2099 }
2100
2101 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
2102 cmd_arg = eap->arg;
2103
2104 if (cmd_arg != NULL)
2105 {
2106 exarg_T nea;
2107
2108 CLEAR_FIELD(nea);
2109 nea.cmd = cmd_arg;
2110 p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
2111 if (nea.cmdidx < CMD_SIZE)
2112 {
2113 has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
2114 if (has_expr)
2115 eap->arg = skiptowhite(eap->arg);
2116 }
2117 }
2118
2119 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
2120 {
2121 int count = 0;
2122 char_u *start = skipwhite(line);
2123
2124 // :cmd xxx`=expr1`yyy`=expr2`zzz
2125 // PUSHS ":cmd xxx"
2126 // eval expr1
2127 // PUSHS "yyy"
2128 // eval expr2
2129 // PUSHS "zzz"
2130 // EXECCONCAT 5
2131 for (;;)
2132 {
2133 if (p > start)
2134 {
2135 char_u *val = vim_strnsave(start, p - start);
2136
2137 generate_PUSHS(cctx, &val);
2138 ++count;
2139 }
2140 p += 2;
2141 if (compile_expr0(&p, cctx) == FAIL)
2142 return NULL;
2143 may_generate_2STRING(-1, TRUE, cctx);
2144 ++count;
2145 p = skipwhite(p);
2146 if (*p != '`')
2147 {
2148 emsg(_(e_missing_backtick));
2149 return NULL;
2150 }
2151 start = p + 1;
2152
2153 p = (char_u *)strstr((char *)start, "`=");
2154 if (p == NULL)
2155 {
2156 if (*skipwhite(start) != NUL)
2157 {
2158 char_u *val = vim_strsave(start);
2159
2160 generate_PUSHS(cctx, &val);
2161 ++count;
2162 }
2163 break;
2164 }
2165 }
2166 generate_EXECCONCAT(cctx, count);
2167 }
2168 else
2169 generate_EXEC_copy(cctx, ISN_EXEC, line);
2170
2171theend:
2172 if (*nextcmd != NUL)
2173 {
2174 // the parser expects a pointer to the bar, put it back
2175 --nextcmd;
2176 *nextcmd = '|';
2177 }
2178 vim_free(tofree);
2179
2180 return nextcmd;
2181}
2182
2183/*
2184 * A script command with heredoc, e.g.
2185 * ruby << EOF
2186 * command
2187 * EOF
2188 * Has been turned into one long line with NL characters by
2189 * get_function_body():
2190 * ruby << EOF<NL> command<NL>EOF
2191 */
2192 char_u *
2193compile_script(char_u *line, cctx_T *cctx)
2194{
2195 if (cctx->ctx_skip != SKIP_YES)
2196 {
2197 isn_T *isn;
2198
2199 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
2200 return NULL;
2201 isn->isn_arg.string = vim_strsave(line);
2202 }
2203 return (char_u *)"";
2204}
2205
2206
2207/*
2208 * :s/pat/repl/
2209 */
2210 char_u *
2211compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
2212{
2213 char_u *cmd = eap->arg;
2214 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
2215
2216 if (expr != NULL)
2217 {
2218 int delimiter = *cmd++;
2219
2220 // There is a \=expr, find it in the substitute part.
2221 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
2222 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
2223 {
2224 garray_T save_ga = cctx->ctx_instr;
2225 char_u *end;
2226 int expr_res;
2227 int trailing_error;
2228 int instr_count;
2229 isn_T *instr;
2230 isn_T *isn;
2231
2232 cmd += 3;
2233 end = skip_substitute(cmd, delimiter);
2234
2235 // Temporarily reset the list of instructions so that the jump
2236 // labels are correct.
2237 cctx->ctx_instr.ga_len = 0;
2238 cctx->ctx_instr.ga_maxlen = 0;
2239 cctx->ctx_instr.ga_data = NULL;
2240 expr_res = compile_expr0(&cmd, cctx);
2241 if (end[-1] == NUL)
2242 end[-1] = delimiter;
2243 cmd = skipwhite(cmd);
2244 trailing_error = *cmd != delimiter && *cmd != NUL;
2245
2246 if (expr_res == FAIL || trailing_error
2247 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
2248 {
2249 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002250 semsg(_(e_trailing_characters_str), cmd);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002251 clear_instr_ga(&cctx->ctx_instr);
2252 cctx->ctx_instr = save_ga;
2253 return NULL;
2254 }
2255
2256 // Move the generated instructions into the ISN_SUBSTITUTE
2257 // instructions, then restore the list of instructions before
2258 // adding the ISN_SUBSTITUTE instruction.
2259 instr_count = cctx->ctx_instr.ga_len;
2260 instr = cctx->ctx_instr.ga_data;
2261 instr[instr_count].isn_type = ISN_FINISH;
2262
2263 cctx->ctx_instr = save_ga;
2264 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
2265 {
2266 int idx;
2267
2268 for (idx = 0; idx < instr_count; ++idx)
2269 delete_instr(instr + idx);
2270 vim_free(instr);
2271 return NULL;
2272 }
2273 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
2274 isn->isn_arg.subs.subs_instr = instr;
2275
2276 // skip over flags
2277 if (*end == '&')
2278 ++end;
2279 while (ASCII_ISALPHA(*end) || *end == '#')
2280 ++end;
2281 return end;
2282 }
2283 }
2284
2285 return compile_exec(arg, eap, cctx);
2286}
2287
2288 char_u *
2289compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
2290{
2291 char_u *arg = eap->arg;
2292 lhs_T *lhs = &cctx->ctx_redir_lhs;
2293
2294 if (lhs->lhs_name != NULL)
2295 {
2296 if (STRNCMP(arg, "END", 3) == 0)
2297 {
2298 if (lhs->lhs_append)
2299 {
2300 // First load the current variable value.
2301 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
2302 cctx) == FAIL)
2303 return NULL;
2304 }
2305
2306 // Gets the redirected text and put it on the stack, then store it
2307 // in the variable.
2308 generate_instr_type(cctx, ISN_REDIREND, &t_string);
2309
2310 if (lhs->lhs_append)
LemonBoy372bcce2022-04-25 12:43:20 +01002311 generate_CONCAT(cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002312
2313 if (lhs->lhs_has_index)
2314 {
2315 // Use the info in "lhs" to store the value at the index in the
2316 // list or dict.
2317 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
2318 &t_string, cctx) == FAIL)
2319 return NULL;
2320 }
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002321 else if (generate_store_lhs(cctx, lhs, -1, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002322 return NULL;
2323
2324 VIM_CLEAR(lhs->lhs_name);
2325 VIM_CLEAR(lhs->lhs_whole);
2326 return arg + 3;
2327 }
2328 emsg(_(e_cannot_nest_redir));
2329 return NULL;
2330 }
2331
2332 if (arg[0] == '=' && arg[1] == '>')
2333 {
2334 int append = FALSE;
2335
2336 // redirect to a variable is compiled
2337 arg += 2;
2338 if (*arg == '>')
2339 {
2340 ++arg;
2341 append = TRUE;
2342 }
2343 arg = skipwhite(arg);
2344
2345 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002346 FALSE, FALSE, FALSE, 1, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002347 return NULL;
2348 if (need_type(&t_string, lhs->lhs_member_type,
2349 -1, 0, cctx, FALSE, FALSE) == FAIL)
2350 return NULL;
2351 generate_instr(cctx, ISN_REDIRSTART);
2352 lhs->lhs_append = append;
2353 if (lhs->lhs_has_index)
2354 {
2355 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
2356 if (lhs->lhs_whole == NULL)
2357 return NULL;
2358 }
2359
2360 return arg + lhs->lhs_varlen_total;
2361 }
2362
2363 // other redirects are handled like at script level
2364 return compile_exec(line, eap, cctx);
2365}
2366
2367#if defined(FEAT_QUICKFIX) || defined(PROTO)
2368 char_u *
2369compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
2370{
2371 isn_T *isn;
2372 char_u *p;
2373
2374 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
2375 if (isn == NULL)
2376 return NULL;
2377 isn->isn_arg.number = eap->cmdidx;
2378
2379 p = eap->arg;
2380 if (compile_expr0(&p, cctx) == FAIL)
2381 return NULL;
2382
2383 isn = generate_instr(cctx, ISN_CEXPR_CORE);
2384 if (isn == NULL)
2385 return NULL;
2386 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
2387 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
2388 return NULL;
2389 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
2390 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
2391 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
2392
2393 return p;
2394}
2395#endif
2396
2397/*
2398 * Compile "return [expr]".
2399 * When "legacy" is TRUE evaluate [expr] with legacy syntax
2400 */
2401 char_u *
2402compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
2403{
2404 char_u *p = arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002405 type_T *stack_type;
2406
2407 if (*p != NUL && *p != '|' && *p != '\n')
2408 {
Bram Moolenaar0bfa8492022-01-22 12:27:04 +00002409 // For a lambda, "return expr" is always used, also when "expr" results
2410 // in a void.
2411 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
2412 && (cctx->ctx_ufunc->uf_flags & FC_LAMBDA) == 0)
Bram Moolenaaref7aadb2022-01-18 18:46:07 +00002413 {
2414 emsg(_(e_returning_value_in_function_without_return_type));
2415 return NULL;
2416 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002417 if (legacy)
2418 {
2419 int save_flags = cmdmod.cmod_flags;
2420
2421 generate_LEGACY_EVAL(cctx, p);
2422 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
2423 0, cctx, FALSE, FALSE) == FAIL)
2424 return NULL;
2425 cmdmod.cmod_flags |= CMOD_LEGACY;
2426 (void)skip_expr(&p, NULL);
2427 cmdmod.cmod_flags = save_flags;
2428 }
2429 else
2430 {
2431 // compile return argument into instructions
2432 if (compile_expr0(&p, cctx) == FAIL)
2433 return NULL;
2434 }
2435
2436 if (cctx->ctx_skip != SKIP_YES)
2437 {
2438 // "check_return_type" with uf_ret_type set to &t_unknown is used
2439 // for an inline function without a specified return type. Set the
2440 // return type here.
Bram Moolenaar078a4612022-01-04 15:17:03 +00002441 stack_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002442 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar1a572e92022-03-15 12:28:10 +00002443 || cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002444 || (!check_return_type
2445 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
2446 {
2447 cctx->ctx_ufunc->uf_ret_type = stack_type;
2448 }
2449 else
2450 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002451 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
2452 0, cctx, FALSE, FALSE) == FAIL)
2453 return NULL;
2454 }
2455 }
2456 }
2457 else
2458 {
2459 // "check_return_type" cannot be TRUE, only used for a lambda which
2460 // always has an argument.
2461 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
2462 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
2463 {
2464 emsg(_(e_missing_return_value));
2465 return NULL;
2466 }
2467
2468 // No argument, return zero.
2469 generate_PUSHNR(cctx, 0);
2470 }
2471
2472 // Undo any command modifiers.
2473 generate_undo_cmdmods(cctx);
2474
2475 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
2476 return NULL;
2477
2478 // "return val | endif" is possible
2479 return skipwhite(p);
2480}
2481
2482/*
2483 * Check if the separator for a :global or :substitute command is OK.
2484 */
2485 int
2486check_global_and_subst(char_u *cmd, char_u *arg)
2487{
2488 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
2489 {
2490 semsg(_(e_separator_not_supported_str), arg);
2491 return FAIL;
2492 }
2493 if (VIM_ISWHITE(cmd[1]))
2494 {
2495 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
2496 return FAIL;
2497 }
2498 return OK;
2499}
2500
2501
2502#endif // defined(FEAT_EVAL)