blob: 6d8935a9c3f050fc09fcb0f54b00b010c474dc7d [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{
95 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
96 {
97 // "unlet s:var" is allowed in legacy script.
98 if (*name == 's' && !script_is_vim9())
99 return OK;
100 semsg(_(e_cannot_unlet_str), name);
101 return FAIL;
102 }
103 return OK;
104}
105
106/*
107 * Callback passed to ex_unletlock().
108 */
109 static int
110compile_unlet(
111 lval_T *lvp,
112 char_u *name_end,
113 exarg_T *eap,
114 int deep UNUSED,
115 void *coookie)
116{
117 cctx_T *cctx = coookie;
118 char_u *p = lvp->ll_name;
119 int cc = *name_end;
120 int ret = OK;
121
122 if (cctx->ctx_skip == SKIP_YES)
123 return OK;
124
125 *name_end = NUL;
126 if (*p == '$')
127 {
128 // :unlet $ENV_VAR
129 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
130 }
131 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
132 {
133 lhs_T lhs;
134
135 // This is similar to assigning: lookup the list/dict, compile the
136 // idx/key. Then instead of storing the value unlet the item.
137 // unlet {list}[idx]
138 // unlet {dict}[key] dict.key
139 //
140 // Figure out the LHS type and other properties.
141 //
142 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
143
144 // : unlet an indexed item
145 if (!lhs.lhs_has_index)
146 {
147 iemsg("called compile_lhs() without an index");
148 ret = FAIL;
149 }
150 else
151 {
152 // Use the info in "lhs" to unlet the item at the index in the
153 // list or dict.
154 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
155 }
156
157 vim_free(lhs.lhs_name);
158 }
159 else if (check_vim9_unlet(p) == FAIL)
160 {
161 ret = FAIL;
162 }
163 else
164 {
165 // Normal name. Only supports g:, w:, t: and b: namespaces.
166 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
167 }
168
169 *name_end = cc;
170 return ret;
171}
172
173/*
174 * Callback passed to ex_unletlock().
175 */
176 static int
177compile_lock_unlock(
178 lval_T *lvp,
179 char_u *name_end,
180 exarg_T *eap,
181 int deep UNUSED,
182 void *coookie)
183{
184 cctx_T *cctx = coookie;
185 int cc = *name_end;
186 char_u *p = lvp->ll_name;
187 int ret = OK;
188 size_t len;
189 char_u *buf;
190 isntype_T isn = ISN_EXEC;
191
192 if (cctx->ctx_skip == SKIP_YES)
193 return OK;
194
195 // Cannot use :lockvar and :unlockvar on local variables.
196 if (p[1] != ':')
197 {
198 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
199
200 if (lookup_local(p, end - p, NULL, cctx) == OK)
201 {
202 char_u *s = p;
203
204 if (*end != '.' && *end != '[')
205 {
206 emsg(_(e_cannot_lock_unlock_local_variable));
207 return FAIL;
208 }
209
210 // For "d.member" put the local variable on the stack, it will be
211 // passed to ex_lockvar() indirectly.
212 if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
213 return FAIL;
214 isn = ISN_LOCKUNLOCK;
215 }
216 }
217
218 // Checking is done at runtime.
219 *name_end = NUL;
220 len = name_end - p + 20;
221 buf = alloc(len);
222 if (buf == NULL)
223 ret = FAIL;
224 else
225 {
226 vim_snprintf((char *)buf, len, "%s %s",
227 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
228 p);
229 ret = generate_EXEC_copy(cctx, isn, buf);
230
231 vim_free(buf);
232 *name_end = cc;
233 }
234 return ret;
235}
236
237/*
238 * compile "unlet var", "lock var" and "unlock var"
239 * "arg" points to "var".
240 */
241 char_u *
242compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
243{
244 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
245 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
246 cctx);
247 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
248}
249
250/*
251 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
252 */
253 static int
254compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
255{
256 garray_T *instr = &cctx->ctx_instr;
257 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
258
259 if (endlabel == NULL)
260 return FAIL;
261 endlabel->el_next = *el;
262 *el = endlabel;
263 endlabel->el_end_label = instr->ga_len;
264
265 generate_JUMP(cctx, when, 0);
266 return OK;
267}
268
269 static void
270compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
271{
272 garray_T *instr = &cctx->ctx_instr;
273
274 while (*el != NULL)
275 {
276 endlabel_T *cur = (*el);
277 isn_T *isn;
278
279 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
280 isn->isn_arg.jump.jump_where = jump_where;
281 *el = cur->el_next;
282 vim_free(cur);
283 }
284}
285
286 static void
287compile_free_jump_to_end(endlabel_T **el)
288{
289 while (*el != NULL)
290 {
291 endlabel_T *cur = (*el);
292
293 *el = cur->el_next;
294 vim_free(cur);
295 }
296}
297
298/*
299 * Create a new scope and set up the generic items.
300 */
301 static scope_T *
302new_scope(cctx_T *cctx, scopetype_T type)
303{
304 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
305
306 if (scope == NULL)
307 return NULL;
308 scope->se_outer = cctx->ctx_scope;
309 cctx->ctx_scope = scope;
310 scope->se_type = type;
311 scope->se_local_count = cctx->ctx_locals.ga_len;
312 return scope;
313}
314
315/*
316 * Free the current scope and go back to the outer scope.
317 */
318 void
319drop_scope(cctx_T *cctx)
320{
321 scope_T *scope = cctx->ctx_scope;
322
323 if (scope == NULL)
324 {
325 iemsg("calling drop_scope() without a scope");
326 return;
327 }
328 cctx->ctx_scope = scope->se_outer;
329 switch (scope->se_type)
330 {
331 case IF_SCOPE:
332 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
333 case FOR_SCOPE:
334 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
335 case WHILE_SCOPE:
336 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
337 case TRY_SCOPE:
338 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
339 case NO_SCOPE:
340 case BLOCK_SCOPE:
341 break;
342 }
343 vim_free(scope);
344}
345
346 static int
347misplaced_cmdmod(cctx_T *cctx)
348{
349 garray_T *instr = &cctx->ctx_instr;
350
351 if (cctx->ctx_has_cmdmod
352 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
353 == ISN_CMDMOD)
354 {
355 emsg(_(e_misplaced_command_modifier));
356 return TRUE;
357 }
358 return FALSE;
359}
360
361/*
362 * compile "if expr"
363 *
364 * "if expr" Produces instructions:
365 * EVAL expr Push result of "expr"
366 * JUMP_IF_FALSE end
367 * ... body ...
368 * end:
369 *
370 * "if expr | else" Produces instructions:
371 * EVAL expr Push result of "expr"
372 * JUMP_IF_FALSE else
373 * ... body ...
374 * JUMP_ALWAYS end
375 * else:
376 * ... body ...
377 * end:
378 *
379 * "if expr1 | elseif expr2 | else" Produces instructions:
380 * EVAL expr Push result of "expr"
381 * JUMP_IF_FALSE elseif
382 * ... body ...
383 * JUMP_ALWAYS end
384 * elseif:
385 * EVAL expr Push result of "expr"
386 * JUMP_IF_FALSE else
387 * ... body ...
388 * JUMP_ALWAYS end
389 * else:
390 * ... body ...
391 * end:
392 */
393 char_u *
394compile_if(char_u *arg, cctx_T *cctx)
395{
396 char_u *p = arg;
397 garray_T *instr = &cctx->ctx_instr;
398 int instr_count = instr->ga_len;
399 scope_T *scope;
400 skip_T skip_save = cctx->ctx_skip;
401 ppconst_T ppconst;
402
403 CLEAR_FIELD(ppconst);
404 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
405 {
406 clear_ppconst(&ppconst);
407 return NULL;
408 }
409 if (!ends_excmd2(arg, skipwhite(p)))
410 {
411 semsg(_(e_trailing_arg), p);
412 return NULL;
413 }
414 if (cctx->ctx_skip == SKIP_YES)
415 clear_ppconst(&ppconst);
416 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
417 {
418 int error = FALSE;
419 int v;
420
421 // The expression results in a constant.
422 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
423 clear_ppconst(&ppconst);
424 if (error)
425 return NULL;
426 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
427 }
428 else
429 {
430 // Not a constant, generate instructions for the expression.
431 cctx->ctx_skip = SKIP_UNKNOWN;
432 if (generate_ppconst(cctx, &ppconst) == FAIL)
433 return NULL;
434 if (bool_on_stack(cctx) == FAIL)
435 return NULL;
436 }
437
438 // CMDMOD_REV must come before the jump
439 generate_undo_cmdmods(cctx);
440
441 scope = new_scope(cctx, IF_SCOPE);
442 if (scope == NULL)
443 return NULL;
444 scope->se_skip_save = skip_save;
445 // "is_had_return" will be reset if any block does not end in :return
446 scope->se_u.se_if.is_had_return = TRUE;
447
448 if (cctx->ctx_skip == SKIP_UNKNOWN)
449 {
450 // "where" is set when ":elseif", "else" or ":endif" is found
451 scope->se_u.se_if.is_if_label = instr->ga_len;
452 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
453 }
454 else
455 scope->se_u.se_if.is_if_label = -1;
456
457#ifdef FEAT_PROFILE
458 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
459 && skip_save != SKIP_YES)
460 {
461 // generated a profile start, need to generate a profile end, since it
462 // won't be done after returning
463 cctx->ctx_skip = SKIP_NOT;
464 generate_instr(cctx, ISN_PROF_END);
465 cctx->ctx_skip = SKIP_YES;
466 }
467#endif
468
469 return p;
470}
471
472 char_u *
473compile_elseif(char_u *arg, cctx_T *cctx)
474{
475 char_u *p = arg;
476 garray_T *instr = &cctx->ctx_instr;
477 int instr_count;
478 isn_T *isn;
479 scope_T *scope = cctx->ctx_scope;
480 ppconst_T ppconst;
481 skip_T save_skip = cctx->ctx_skip;
482
483 if (scope == NULL || scope->se_type != IF_SCOPE)
484 {
485 emsg(_(e_elseif_without_if));
486 return NULL;
487 }
488 unwind_locals(cctx, scope->se_local_count);
489 if (!cctx->ctx_had_return)
490 scope->se_u.se_if.is_had_return = FALSE;
491
492 if (cctx->ctx_skip == SKIP_NOT)
493 {
494 // previous block was executed, this one and following will not
495 cctx->ctx_skip = SKIP_YES;
496 scope->se_u.se_if.is_seen_skip_not = TRUE;
497 }
498 if (scope->se_u.se_if.is_seen_skip_not)
499 {
500 // A previous block was executed, skip over expression and bail out.
501 // Do not count the "elseif" for profiling and cmdmod
502 instr->ga_len = current_instr_idx(cctx);
503
504 skip_expr_cctx(&p, cctx);
505 return p;
506 }
507
508 if (cctx->ctx_skip == SKIP_UNKNOWN)
509 {
510 int moved_cmdmod = FALSE;
511 int saved_debug = FALSE;
512 isn_T debug_isn;
513
514 // Move any CMDMOD instruction to after the jump
515 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
516 {
517 if (GA_GROW_FAILS(instr, 1))
518 return NULL;
519 ((isn_T *)instr->ga_data)[instr->ga_len] =
520 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
521 --instr->ga_len;
522 moved_cmdmod = TRUE;
523 }
524
525 // Remove the already generated ISN_DEBUG, it is written below the
526 // ISN_FOR instruction.
527 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
528 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
529 .isn_type == ISN_DEBUG)
530 {
531 --instr->ga_len;
532 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
533 saved_debug = TRUE;
534 }
535
536 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
537 JUMP_ALWAYS, cctx) == FAIL)
538 return NULL;
539 // previous "if" or "elseif" jumps here
540 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
541 isn->isn_arg.jump.jump_where = instr->ga_len;
542
543 if (moved_cmdmod)
544 ++instr->ga_len;
545
546 if (saved_debug)
547 {
548 // move the debug instruction here
549 if (GA_GROW_FAILS(instr, 1))
550 return NULL;
551 ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
552 ++instr->ga_len;
553 }
554 }
555
556 // compile "expr"; if we know it evaluates to FALSE skip the block
557 CLEAR_FIELD(ppconst);
558 if (cctx->ctx_skip == SKIP_YES)
559 {
560 cctx->ctx_skip = SKIP_UNKNOWN;
561#ifdef FEAT_PROFILE
562 if (cctx->ctx_compile_type == CT_PROFILE)
563 // the previous block was skipped, need to profile this line
564 generate_instr(cctx, ISN_PROF_START);
565#endif
566 if (cctx->ctx_compile_type == CT_DEBUG)
567 // the previous block was skipped, may want to debug this line
568 generate_instr_debug(cctx);
569 }
570
571 instr_count = instr->ga_len;
572 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
573 {
574 clear_ppconst(&ppconst);
575 return NULL;
576 }
577 cctx->ctx_skip = save_skip;
578 if (!ends_excmd2(arg, skipwhite(p)))
579 {
580 clear_ppconst(&ppconst);
581 semsg(_(e_trailing_arg), p);
582 return NULL;
583 }
584 if (scope->se_skip_save == SKIP_YES)
585 clear_ppconst(&ppconst);
586 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
587 {
588 int error = FALSE;
589 int v;
590
591 // The expression result is a constant.
592 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
593 if (error)
594 {
595 clear_ppconst(&ppconst);
596 return NULL;
597 }
598 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
599 clear_ppconst(&ppconst);
600 scope->se_u.se_if.is_if_label = -1;
601 }
602 else
603 {
604 // Not a constant, generate instructions for the expression.
605 cctx->ctx_skip = SKIP_UNKNOWN;
606 if (generate_ppconst(cctx, &ppconst) == FAIL)
607 return NULL;
608 if (bool_on_stack(cctx) == FAIL)
609 return NULL;
610
611 // CMDMOD_REV must come before the jump
612 generate_undo_cmdmods(cctx);
613
614 // "where" is set when ":elseif", "else" or ":endif" is found
615 scope->se_u.se_if.is_if_label = instr->ga_len;
616 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
617 }
618
619 return p;
620}
621
622 char_u *
623compile_else(char_u *arg, cctx_T *cctx)
624{
625 char_u *p = arg;
626 garray_T *instr = &cctx->ctx_instr;
627 isn_T *isn;
628 scope_T *scope = cctx->ctx_scope;
629
630 if (scope == NULL || scope->se_type != IF_SCOPE)
631 {
632 emsg(_(e_else_without_if));
633 return NULL;
634 }
635 unwind_locals(cctx, scope->se_local_count);
636 if (!cctx->ctx_had_return)
637 scope->se_u.se_if.is_had_return = FALSE;
638 scope->se_u.se_if.is_seen_else = TRUE;
639
640#ifdef FEAT_PROFILE
641 if (cctx->ctx_compile_type == CT_PROFILE)
642 {
643 if (cctx->ctx_skip == SKIP_NOT
644 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
645 .isn_type == ISN_PROF_START)
646 // the previous block was executed, do not count "else" for
647 // profiling
648 --instr->ga_len;
649 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
650 {
651 // the previous block was not executed, this one will, do count the
652 // "else" for profiling
653 cctx->ctx_skip = SKIP_NOT;
654 generate_instr(cctx, ISN_PROF_END);
655 generate_instr(cctx, ISN_PROF_START);
656 cctx->ctx_skip = SKIP_YES;
657 }
658 }
659#endif
660
661 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
662 {
663 // jump from previous block to the end, unless the else block is empty
664 if (cctx->ctx_skip == SKIP_UNKNOWN)
665 {
666 if (!cctx->ctx_had_return
667 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
668 JUMP_ALWAYS, cctx) == FAIL)
669 return NULL;
670 }
671
672 if (cctx->ctx_skip == SKIP_UNKNOWN)
673 {
674 if (scope->se_u.se_if.is_if_label >= 0)
675 {
676 // previous "if" or "elseif" jumps here
677 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
678 isn->isn_arg.jump.jump_where = instr->ga_len;
679 scope->se_u.se_if.is_if_label = -1;
680 }
681 }
682
683 if (cctx->ctx_skip != SKIP_UNKNOWN)
684 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
685 }
686
687 return p;
688}
689
690 char_u *
691compile_endif(char_u *arg, cctx_T *cctx)
692{
693 scope_T *scope = cctx->ctx_scope;
694 ifscope_T *ifscope;
695 garray_T *instr = &cctx->ctx_instr;
696 isn_T *isn;
697
698 if (misplaced_cmdmod(cctx))
699 return NULL;
700
701 if (scope == NULL || scope->se_type != IF_SCOPE)
702 {
703 emsg(_(e_endif_without_if));
704 return NULL;
705 }
706 ifscope = &scope->se_u.se_if;
707 unwind_locals(cctx, scope->se_local_count);
708 if (!cctx->ctx_had_return)
709 ifscope->is_had_return = FALSE;
710
711 if (scope->se_u.se_if.is_if_label >= 0)
712 {
713 // previous "if" or "elseif" jumps here
714 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
715 isn->isn_arg.jump.jump_where = instr->ga_len;
716 }
717 // Fill in the "end" label in jumps at the end of the blocks.
718 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
719
720#ifdef FEAT_PROFILE
721 // even when skipping we count the endif as executed, unless the block it's
722 // in is skipped
723 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
724 && scope->se_skip_save != SKIP_YES)
725 {
726 cctx->ctx_skip = SKIP_NOT;
727 generate_instr(cctx, ISN_PROF_START);
728 }
729#endif
730 cctx->ctx_skip = scope->se_skip_save;
731
732 // If all the blocks end in :return and there is an :else then the
733 // had_return flag is set.
734 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
735
736 drop_scope(cctx);
737 return arg;
738}
739
740/*
741 * Compile "for var in expr":
742 *
743 * Produces instructions:
744 * PUSHNR -1
745 * STORE loop-idx Set index to -1
746 * EVAL expr result of "expr" on top of stack
747 * top: FOR loop-idx, end Increment index, use list on bottom of stack
748 * - if beyond end, jump to "end"
749 * - otherwise get item from list and push it
750 * STORE var Store item in "var"
751 * ... body ...
752 * JUMP top Jump back to repeat
753 * end: DROP Drop the result of "expr"
754 *
755 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
756 * UNPACK 2 Split item in 2
757 * STORE var1 Store item in "var1"
758 * STORE var2 Store item in "var2"
759 */
760 char_u *
761compile_for(char_u *arg_start, cctx_T *cctx)
762{
763 char_u *arg;
764 char_u *arg_end;
765 char_u *name = NULL;
766 char_u *p;
767 char_u *wp;
768 int var_count = 0;
769 int var_list = FALSE;
770 int semicolon = FALSE;
771 size_t varlen;
772 garray_T *stack = &cctx->ctx_type_stack;
773 garray_T *instr = &cctx->ctx_instr;
774 scope_T *scope;
775 lvar_T *loop_lvar; // loop iteration variable
776 lvar_T *var_lvar; // variable for "var"
777 type_T *vartype;
778 type_T *item_type = &t_any;
779 int idx;
780 int prev_lnum = cctx->ctx_prev_lnum;
781
782 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
783 if (p == NULL)
784 return NULL;
785 if (var_count == 0)
786 var_count = 1;
787 else
788 var_list = TRUE; // can also be a list of one variable
789
790 // consume "in"
791 wp = p;
792 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
793 return NULL;
794 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
795 {
796 if (*p == ':' && wp != p)
797 semsg(_(e_no_white_space_allowed_before_colon_str), p);
798 else
799 emsg(_(e_missing_in));
800 return NULL;
801 }
802 wp = p + 2;
803 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
804 return NULL;
805
806 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
807 // instruction.
808 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
809 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
810 .isn_type == ISN_DEBUG)
811 {
812 --instr->ga_len;
813 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
814 .isn_arg.debug.dbg_break_lnum;
815 }
816
817 scope = new_scope(cctx, FOR_SCOPE);
818 if (scope == NULL)
819 return NULL;
820
821 // Reserve a variable to store the loop iteration counter and initialize it
822 // to -1.
823 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
824 if (loop_lvar == NULL)
825 {
826 // out of memory
827 drop_scope(cctx);
828 return NULL;
829 }
830 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
831
832 // compile "expr", it remains on the stack until "endfor"
833 arg = p;
834 if (compile_expr0(&arg, cctx) == FAIL)
835 {
836 drop_scope(cctx);
837 return NULL;
838 }
839 arg_end = arg;
840
841 if (cctx->ctx_skip != SKIP_YES)
842 {
843 // If we know the type of "var" and it is a not a supported type we can
844 // give an error now.
845 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
846 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
847 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
848 {
849 semsg(_(e_for_loop_on_str_not_supported),
850 vartype_name(vartype->tt_type));
851 drop_scope(cctx);
852 return NULL;
853 }
854
855 if (vartype->tt_type == VAR_STRING)
856 item_type = &t_string;
857 else if (vartype->tt_type == VAR_BLOB)
858 item_type = &t_number;
859 else if (vartype->tt_type == VAR_LIST
860 && vartype->tt_member->tt_type != VAR_ANY)
861 {
862 if (!var_list)
863 item_type = vartype->tt_member;
864 else if (vartype->tt_member->tt_type == VAR_LIST
865 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
866 item_type = vartype->tt_member->tt_member;
867 }
868
869 // CMDMOD_REV must come before the FOR instruction.
870 generate_undo_cmdmods(cctx);
871
872 // "for_end" is set when ":endfor" is found
873 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
874
875 generate_FOR(cctx, loop_lvar->lv_idx);
876
877 arg = arg_start;
878 if (var_list)
879 {
880 generate_UNPACK(cctx, var_count, semicolon);
881 arg = skipwhite(arg + 1); // skip white after '['
882
883 // the list item is replaced by a number of items
884 if (GA_GROW_FAILS(stack, var_count - 1))
885 {
886 drop_scope(cctx);
887 return NULL;
888 }
889 --stack->ga_len;
890 for (idx = 0; idx < var_count; ++idx)
891 {
892 ((type_T **)stack->ga_data)[stack->ga_len] =
893 (semicolon && idx == 0) ? vartype : item_type;
894 ++stack->ga_len;
895 }
896 }
897
898 for (idx = 0; idx < var_count; ++idx)
899 {
900 assign_dest_T dest = dest_local;
901 int opt_flags = 0;
902 int vimvaridx = -1;
903 type_T *type = &t_any;
904 type_T *lhs_type = &t_any;
905 where_T where = WHERE_INIT;
906
907 p = skip_var_one(arg, FALSE);
908 varlen = p - arg;
909 name = vim_strnsave(arg, varlen);
910 if (name == NULL)
911 goto failed;
912 if (*p == ':')
913 {
914 p = skipwhite(p + 1);
915 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
916 }
917
918 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
919 &vimvaridx, &type, cctx) == FAIL)
920 goto failed;
921 if (dest != dest_local)
922 {
923 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
924 0, 0, type, name) == FAIL)
925 goto failed;
926 }
927 else if (varlen == 1 && *arg == '_')
928 {
929 // Assigning to "_": drop the value.
930 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
931 goto failed;
932 }
933 else
934 {
935 // Script var is not supported.
936 if (STRNCMP(name, "s:", 2) == 0)
937 {
938 emsg(_(e_cannot_use_script_variable_in_for_loop));
939 goto failed;
940 }
941
942 if (!valid_varname(arg, (int)varlen, FALSE))
943 goto failed;
944 if (lookup_local(arg, varlen, NULL, cctx) == OK)
945 {
946 semsg(_(e_variable_already_declared), arg);
947 goto failed;
948 }
949
950 // Reserve a variable to store "var".
951 where.wt_index = var_list ? idx + 1 : 0;
952 where.wt_variable = TRUE;
953 if (lhs_type == &t_any)
954 lhs_type = item_type;
955 else if (item_type != &t_unknown
956 && (item_type == &t_any
957 ? need_type(item_type, lhs_type,
958 -1, 0, cctx, FALSE, FALSE)
959 : check_type(lhs_type, item_type, TRUE, where))
960 == FAIL)
961 goto failed;
962 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
963 if (var_lvar == NULL)
964 // out of memory or used as an argument
965 goto failed;
966
967 if (semicolon && idx == var_count - 1)
968 var_lvar->lv_type = vartype;
969 else
970 var_lvar->lv_type = item_type;
971 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
972 }
973
974 if (*p == ',' || *p == ';')
975 ++p;
976 arg = skipwhite(p);
977 vim_free(name);
978 }
979
980 if (cctx->ctx_compile_type == CT_DEBUG)
981 {
982 int save_prev_lnum = cctx->ctx_prev_lnum;
983
984 // Add ISN_DEBUG here, so that the loop variables can be inspected.
985 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
986 cctx->ctx_prev_lnum = prev_lnum;
987 generate_instr_debug(cctx);
988 cctx->ctx_prev_lnum = save_prev_lnum;
989 }
990 }
991
992 return arg_end;
993
994failed:
995 vim_free(name);
996 drop_scope(cctx);
997 return NULL;
998}
999
1000/*
1001 * compile "endfor"
1002 */
1003 char_u *
1004compile_endfor(char_u *arg, cctx_T *cctx)
1005{
1006 garray_T *instr = &cctx->ctx_instr;
1007 scope_T *scope = cctx->ctx_scope;
1008 forscope_T *forscope;
1009 isn_T *isn;
1010
1011 if (misplaced_cmdmod(cctx))
1012 return NULL;
1013
1014 if (scope == NULL || scope->se_type != FOR_SCOPE)
1015 {
1016 emsg(_(e_for));
1017 return NULL;
1018 }
1019 forscope = &scope->se_u.se_for;
1020 cctx->ctx_scope = scope->se_outer;
1021 if (cctx->ctx_skip != SKIP_YES)
1022 {
1023 unwind_locals(cctx, scope->se_local_count);
1024
1025 // At end of ":for" scope jump back to the FOR instruction.
1026 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
1027
1028 // Fill in the "end" label in the FOR statement so it can jump here.
1029 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
1030 isn->isn_arg.forloop.for_end = instr->ga_len;
1031
1032 // Fill in the "end" label any BREAK statements
1033 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
1034
1035 // Below the ":for" scope drop the "expr" list from the stack.
1036 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
1037 return NULL;
1038 }
1039
1040 vim_free(scope);
1041
1042 return arg;
1043}
1044
1045/*
1046 * compile "while expr"
1047 *
1048 * Produces instructions:
1049 * top: EVAL expr Push result of "expr"
1050 * JUMP_IF_FALSE end jump if false
1051 * ... body ...
1052 * JUMP top Jump back to repeat
1053 * end:
1054 *
1055 */
1056 char_u *
1057compile_while(char_u *arg, cctx_T *cctx)
1058{
1059 char_u *p = arg;
1060 scope_T *scope;
1061
1062 scope = new_scope(cctx, WHILE_SCOPE);
1063 if (scope == NULL)
1064 return NULL;
1065
1066 // "endwhile" jumps back here, one before when profiling or using cmdmods
1067 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
1068
1069 // compile "expr"
1070 if (compile_expr0(&p, cctx) == FAIL)
1071 return NULL;
1072
1073 if (!ends_excmd2(arg, skipwhite(p)))
1074 {
1075 semsg(_(e_trailing_arg), p);
1076 return NULL;
1077 }
1078
1079 if (cctx->ctx_skip != SKIP_YES)
1080 {
1081 if (bool_on_stack(cctx) == FAIL)
1082 return FAIL;
1083
1084 // CMDMOD_REV must come before the jump
1085 generate_undo_cmdmods(cctx);
1086
1087 // "while_end" is set when ":endwhile" is found
1088 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
1089 JUMP_IF_FALSE, cctx) == FAIL)
1090 return FAIL;
1091 }
1092
1093 return p;
1094}
1095
1096/*
1097 * compile "endwhile"
1098 */
1099 char_u *
1100compile_endwhile(char_u *arg, cctx_T *cctx)
1101{
1102 scope_T *scope = cctx->ctx_scope;
1103 garray_T *instr = &cctx->ctx_instr;
1104
1105 if (misplaced_cmdmod(cctx))
1106 return NULL;
1107 if (scope == NULL || scope->se_type != WHILE_SCOPE)
1108 {
1109 emsg(_(e_while));
1110 return NULL;
1111 }
1112 cctx->ctx_scope = scope->se_outer;
1113 if (cctx->ctx_skip != SKIP_YES)
1114 {
1115 unwind_locals(cctx, scope->se_local_count);
1116
1117#ifdef FEAT_PROFILE
1118 // count the endwhile before jumping
1119 may_generate_prof_end(cctx, cctx->ctx_lnum);
1120#endif
1121
1122 // At end of ":for" scope jump back to the FOR instruction.
1123 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
1124
1125 // Fill in the "end" label in the WHILE statement so it can jump here.
1126 // And in any jumps for ":break"
1127 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
1128 instr->ga_len, cctx);
1129 }
1130
1131 vim_free(scope);
1132
1133 return arg;
1134}
1135
1136/*
1137 * compile "continue"
1138 */
1139 char_u *
1140compile_continue(char_u *arg, cctx_T *cctx)
1141{
1142 scope_T *scope = cctx->ctx_scope;
1143 int try_scopes = 0;
1144 int loop_label;
1145
1146 for (;;)
1147 {
1148 if (scope == NULL)
1149 {
1150 emsg(_(e_continue));
1151 return NULL;
1152 }
1153 if (scope->se_type == FOR_SCOPE)
1154 {
1155 loop_label = scope->se_u.se_for.fs_top_label;
1156 break;
1157 }
1158 if (scope->se_type == WHILE_SCOPE)
1159 {
1160 loop_label = scope->se_u.se_while.ws_top_label;
1161 break;
1162 }
1163 if (scope->se_type == TRY_SCOPE)
1164 ++try_scopes;
1165 scope = scope->se_outer;
1166 }
1167
1168 if (try_scopes > 0)
1169 // Inside one or more try/catch blocks we first need to jump to the
1170 // "finally" or "endtry" to cleanup.
1171 generate_TRYCONT(cctx, try_scopes, loop_label);
1172 else
1173 // Jump back to the FOR or WHILE instruction.
1174 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
1175
1176 return arg;
1177}
1178
1179/*
1180 * compile "break"
1181 */
1182 char_u *
1183compile_break(char_u *arg, cctx_T *cctx)
1184{
1185 scope_T *scope = cctx->ctx_scope;
1186 endlabel_T **el;
1187
1188 for (;;)
1189 {
1190 if (scope == NULL)
1191 {
1192 emsg(_(e_break));
1193 return NULL;
1194 }
1195 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
1196 break;
1197 scope = scope->se_outer;
1198 }
1199
1200 // Jump to the end of the FOR or WHILE loop.
1201 if (scope->se_type == FOR_SCOPE)
1202 el = &scope->se_u.se_for.fs_end_label;
1203 else
1204 el = &scope->se_u.se_while.ws_end_label;
1205 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
1206 return FAIL;
1207
1208 return arg;
1209}
1210
1211/*
1212 * compile "{" start of block
1213 */
1214 char_u *
1215compile_block(char_u *arg, cctx_T *cctx)
1216{
1217 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
1218 return NULL;
1219 return skipwhite(arg + 1);
1220}
1221
1222/*
1223 * compile end of block: drop one scope
1224 */
1225 void
1226compile_endblock(cctx_T *cctx)
1227{
1228 scope_T *scope = cctx->ctx_scope;
1229
1230 cctx->ctx_scope = scope->se_outer;
1231 unwind_locals(cctx, scope->se_local_count);
1232 vim_free(scope);
1233}
1234
1235/*
1236 * Compile "try".
1237 * Creates a new scope for the try-endtry, pointing to the first catch and
1238 * finally.
1239 * Creates another scope for the "try" block itself.
1240 * TRY instruction sets up exception handling at runtime.
1241 *
1242 * "try"
1243 * TRY -> catch1, -> finally push trystack entry
1244 * ... try block
1245 * "throw {exception}"
1246 * EVAL {exception}
1247 * THROW create exception
1248 * ... try block
1249 * " catch {expr}"
1250 * JUMP -> finally
1251 * catch1: PUSH exception
1252 * EVAL {expr}
1253 * MATCH
1254 * JUMP nomatch -> catch2
1255 * CATCH remove exception
1256 * ... catch block
1257 * " catch"
1258 * JUMP -> finally
1259 * catch2: CATCH remove exception
1260 * ... catch block
1261 * " finally"
1262 * finally:
1263 * ... finally block
1264 * " endtry"
1265 * ENDTRY pop trystack entry, may rethrow
1266 */
1267 char_u *
1268compile_try(char_u *arg, cctx_T *cctx)
1269{
1270 garray_T *instr = &cctx->ctx_instr;
1271 scope_T *try_scope;
1272 scope_T *scope;
1273
1274 if (misplaced_cmdmod(cctx))
1275 return NULL;
1276
1277 // scope that holds the jumps that go to catch/finally/endtry
1278 try_scope = new_scope(cctx, TRY_SCOPE);
1279 if (try_scope == NULL)
1280 return NULL;
1281
1282 if (cctx->ctx_skip != SKIP_YES)
1283 {
1284 isn_T *isn;
1285
1286 // "try_catch" is set when the first ":catch" is found or when no catch
1287 // is found and ":finally" is found.
1288 // "try_finally" is set when ":finally" is found
1289 // "try_endtry" is set when ":endtry" is found
1290 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
1291 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
1292 return NULL;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001293 isn->isn_arg.tryref.try_ref = ALLOC_CLEAR_ONE(tryref_T);
1294 if (isn->isn_arg.tryref.try_ref == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001295 return NULL;
1296 }
1297
1298 // scope for the try block itself
1299 scope = new_scope(cctx, BLOCK_SCOPE);
1300 if (scope == NULL)
1301 return NULL;
1302
1303 return arg;
1304}
1305
1306/*
1307 * Compile "catch {expr}".
1308 */
1309 char_u *
1310compile_catch(char_u *arg, cctx_T *cctx UNUSED)
1311{
1312 scope_T *scope = cctx->ctx_scope;
1313 garray_T *instr = &cctx->ctx_instr;
1314 char_u *p;
1315 isn_T *isn;
1316
1317 if (misplaced_cmdmod(cctx))
1318 return NULL;
1319
1320 // end block scope from :try or :catch
1321 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1322 compile_endblock(cctx);
1323 scope = cctx->ctx_scope;
1324
1325 // Error if not in a :try scope
1326 if (scope == NULL || scope->se_type != TRY_SCOPE)
1327 {
1328 emsg(_(e_catch));
1329 return NULL;
1330 }
1331
1332 if (scope->se_u.se_try.ts_caught_all)
1333 {
1334 emsg(_(e_catch_unreachable_after_catch_all));
1335 return NULL;
1336 }
1337
1338 if (cctx->ctx_skip != SKIP_YES)
1339 {
1340#ifdef FEAT_PROFILE
1341 // the profile-start should be after the jump
1342 if (cctx->ctx_compile_type == CT_PROFILE
1343 && instr->ga_len > 0
1344 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
1345 .isn_type == ISN_PROF_START)
1346 --instr->ga_len;
1347#endif
1348 // Jump from end of previous block to :finally or :endtry
1349 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
1350 JUMP_ALWAYS, cctx) == FAIL)
1351 return NULL;
1352
1353 // End :try or :catch scope: set value in ISN_TRY instruction
1354 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001355 if (isn->isn_arg.tryref.try_ref->try_catch == 0)
1356 isn->isn_arg.tryref.try_ref->try_catch = instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001357 if (scope->se_u.se_try.ts_catch_label != 0)
1358 {
1359 // Previous catch without match jumps here
1360 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
1361 isn->isn_arg.jump.jump_where = instr->ga_len;
1362 }
1363#ifdef FEAT_PROFILE
1364 if (cctx->ctx_compile_type == CT_PROFILE)
1365 {
1366 // a "throw" that jumps here needs to be counted
1367 generate_instr(cctx, ISN_PROF_END);
1368 // the "catch" is also counted
1369 generate_instr(cctx, ISN_PROF_START);
1370 }
1371#endif
1372 if (cctx->ctx_compile_type == CT_DEBUG)
1373 generate_instr_debug(cctx);
1374 }
1375
1376 p = skipwhite(arg);
1377 if (ends_excmd2(arg, p))
1378 {
1379 scope->se_u.se_try.ts_caught_all = TRUE;
1380 scope->se_u.se_try.ts_catch_label = 0;
1381 }
1382 else
1383 {
1384 char_u *end;
1385 char_u *pat;
1386 char_u *tofree = NULL;
1387 int dropped = 0;
1388 int len;
1389
1390 // Push v:exception, push {expr} and MATCH
1391 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
1392
1393 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
1394 if (*end != *p)
1395 {
1396 semsg(_(e_separator_mismatch_str), p);
1397 vim_free(tofree);
1398 return FAIL;
1399 }
1400 if (tofree == NULL)
1401 len = (int)(end - (p + 1));
1402 else
1403 len = (int)(end - tofree);
1404 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
1405 vim_free(tofree);
1406 p += len + 2 + dropped;
1407 if (pat == NULL)
1408 return FAIL;
1409 if (generate_PUSHS(cctx, &pat) == FAIL)
1410 return FAIL;
1411
1412 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
1413 return NULL;
1414
1415 scope->se_u.se_try.ts_catch_label = instr->ga_len;
1416 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
1417 return NULL;
1418 }
1419
1420 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
1421 return NULL;
1422
1423 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
1424 return NULL;
1425 return p;
1426}
1427
1428 char_u *
1429compile_finally(char_u *arg, cctx_T *cctx)
1430{
1431 scope_T *scope = cctx->ctx_scope;
1432 garray_T *instr = &cctx->ctx_instr;
1433 isn_T *isn;
1434 int this_instr;
1435
1436 if (misplaced_cmdmod(cctx))
1437 return NULL;
1438
1439 // end block scope from :try or :catch
1440 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1441 compile_endblock(cctx);
1442 scope = cctx->ctx_scope;
1443
1444 // Error if not in a :try scope
1445 if (scope == NULL || scope->se_type != TRY_SCOPE)
1446 {
1447 emsg(_(e_finally));
1448 return NULL;
1449 }
1450
1451 if (cctx->ctx_skip != SKIP_YES)
1452 {
1453 // End :catch or :finally scope: set value in ISN_TRY instruction
1454 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001455 if (isn->isn_arg.tryref.try_ref->try_finally != 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001456 {
1457 emsg(_(e_finally_dup));
1458 return NULL;
1459 }
1460
1461 this_instr = instr->ga_len;
1462#ifdef FEAT_PROFILE
1463 if (cctx->ctx_compile_type == CT_PROFILE
1464 && ((isn_T *)instr->ga_data)[this_instr - 1]
1465 .isn_type == ISN_PROF_START)
1466 {
1467 // jump to the profile start of the "finally"
1468 --this_instr;
1469
1470 // jump to the profile end above it
1471 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
1472 .isn_type == ISN_PROF_END)
1473 --this_instr;
1474 }
1475#endif
1476
1477 // Fill in the "end" label in jumps at the end of the blocks.
1478 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
1479 this_instr, cctx);
1480
1481 // If there is no :catch then an exception jumps to :finally.
Bram Moolenaar0d807102021-12-21 09:42:09 +00001482 if (isn->isn_arg.tryref.try_ref->try_catch == 0)
1483 isn->isn_arg.tryref.try_ref->try_catch = this_instr;
1484 isn->isn_arg.tryref.try_ref->try_finally = this_instr;
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 = this_instr;
1490 scope->se_u.se_try.ts_catch_label = 0;
1491 }
1492 if (generate_instr(cctx, ISN_FINALLY) == NULL)
1493 return NULL;
1494 }
1495
1496 return arg;
1497}
1498
1499 char_u *
1500compile_endtry(char_u *arg, cctx_T *cctx)
1501{
1502 scope_T *scope = cctx->ctx_scope;
1503 garray_T *instr = &cctx->ctx_instr;
1504 isn_T *try_isn;
1505
1506 if (misplaced_cmdmod(cctx))
1507 return NULL;
1508
1509 // end block scope from :catch or :finally
1510 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1511 compile_endblock(cctx);
1512 scope = cctx->ctx_scope;
1513
1514 // Error if not in a :try scope
1515 if (scope == NULL || scope->se_type != TRY_SCOPE)
1516 {
1517 if (scope == NULL)
1518 emsg(_(e_no_endtry));
1519 else if (scope->se_type == WHILE_SCOPE)
1520 emsg(_(e_endwhile));
1521 else if (scope->se_type == FOR_SCOPE)
1522 emsg(_(e_endfor));
1523 else
1524 emsg(_(e_endif));
1525 return NULL;
1526 }
1527
1528 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
1529 if (cctx->ctx_skip != SKIP_YES)
1530 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00001531 if (try_isn->isn_arg.tryref.try_ref->try_catch == 0
1532 && try_isn->isn_arg.tryref.try_ref->try_finally == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001533 {
1534 emsg(_(e_missing_catch_or_finally));
1535 return NULL;
1536 }
1537
1538#ifdef FEAT_PROFILE
1539 if (cctx->ctx_compile_type == CT_PROFILE
1540 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
1541 .isn_type == ISN_PROF_START)
1542 // move the profile start after "endtry" so that it's not counted when
1543 // the exception is rethrown.
1544 --instr->ga_len;
1545#endif
1546
1547 // Fill in the "end" label in jumps at the end of the blocks, if not
1548 // done by ":finally".
1549 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
1550 instr->ga_len, cctx);
1551
1552 if (scope->se_u.se_try.ts_catch_label != 0)
1553 {
1554 // Last catch without match jumps here
1555 isn_T *isn = ((isn_T *)instr->ga_data)
1556 + scope->se_u.se_try.ts_catch_label;
1557 isn->isn_arg.jump.jump_where = instr->ga_len;
1558 }
1559 }
1560
1561 compile_endblock(cctx);
1562
1563 if (cctx->ctx_skip != SKIP_YES)
1564 {
1565 // End :catch or :finally scope: set instruction index in ISN_TRY
1566 // instruction
Bram Moolenaar0d807102021-12-21 09:42:09 +00001567 try_isn->isn_arg.tryref.try_ref->try_endtry = instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001568 if (cctx->ctx_skip != SKIP_YES
1569 && generate_instr(cctx, ISN_ENDTRY) == NULL)
1570 return NULL;
1571#ifdef FEAT_PROFILE
1572 if (cctx->ctx_compile_type == CT_PROFILE)
1573 generate_instr(cctx, ISN_PROF_START);
1574#endif
1575 }
1576 return arg;
1577}
1578
1579/*
1580 * compile "throw {expr}"
1581 */
1582 char_u *
1583compile_throw(char_u *arg, cctx_T *cctx UNUSED)
1584{
1585 char_u *p = skipwhite(arg);
1586
1587 if (compile_expr0(&p, cctx) == FAIL)
1588 return NULL;
1589 if (cctx->ctx_skip == SKIP_YES)
1590 return p;
1591 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1592 return NULL;
1593 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
1594 return NULL;
1595
1596 return p;
1597}
1598
1599 char_u *
1600compile_eval(char_u *arg, cctx_T *cctx)
1601{
1602 char_u *p = arg;
1603 int name_only;
1604 long lnum = SOURCING_LNUM;
1605
1606 // find_ex_command() will consider a variable name an expression, assuming
1607 // that something follows on the next line. Check that something actually
1608 // follows, otherwise it's probably a misplaced command.
1609 name_only = cmd_is_name_only(arg);
1610
1611 if (compile_expr0(&p, cctx) == FAIL)
1612 return NULL;
1613
1614 if (name_only && lnum == SOURCING_LNUM)
1615 {
1616 semsg(_(e_expression_without_effect_str), arg);
1617 return NULL;
1618 }
1619
1620 // drop the result
1621 generate_instr_drop(cctx, ISN_DROP, 1);
1622
1623 return skipwhite(p);
1624}
1625
1626/*
1627 * compile "echo expr"
1628 * compile "echomsg expr"
1629 * compile "echoerr expr"
1630 * compile "echoconsole expr"
1631 * compile "execute expr"
1632 */
1633 char_u *
1634compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
1635{
1636 char_u *p = arg;
1637 char_u *prev = arg;
1638 char_u *expr_start;
1639 int count = 0;
1640 int start_ctx_lnum = cctx->ctx_lnum;
1641 garray_T *stack = &cctx->ctx_type_stack;
1642 type_T *type;
1643
1644 for (;;)
1645 {
1646 if (ends_excmd2(prev, p))
1647 break;
1648 expr_start = p;
1649 if (compile_expr0(&p, cctx) == FAIL)
1650 return NULL;
1651
1652 if (cctx->ctx_skip != SKIP_YES)
1653 {
1654 // check for non-void type
1655 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1656 if (type->tt_type == VAR_VOID)
1657 {
1658 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
1659 return NULL;
1660 }
1661 }
1662
1663 ++count;
1664 prev = p;
1665 p = skipwhite(p);
1666 }
1667
1668 if (count > 0)
1669 {
1670 long save_lnum = cctx->ctx_lnum;
1671
1672 // Use the line number where the command started.
1673 cctx->ctx_lnum = start_ctx_lnum;
1674
1675 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
1676 generate_ECHO(cctx, cmdidx == CMD_echo, count);
1677 else if (cmdidx == CMD_execute)
1678 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
1679 else if (cmdidx == CMD_echomsg)
1680 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
1681 else if (cmdidx == CMD_echoconsole)
1682 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
1683 else
1684 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
1685
1686 cctx->ctx_lnum = save_lnum;
1687 }
1688 return p;
1689}
1690
1691/*
1692 * If "eap" has a range that is not a constant generate an ISN_RANGE
1693 * instruction to compute it and return OK.
1694 * Otherwise return FAIL, the caller must deal with any range.
1695 */
1696 static int
1697compile_variable_range(exarg_T *eap, cctx_T *cctx)
1698{
1699 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
1700 char_u *p = skipdigits(eap->cmd);
1701
1702 if (p == range_end)
1703 return FAIL;
1704 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
1705}
1706
1707/*
1708 * :put r
1709 * :put ={expr}
1710 */
1711 char_u *
1712compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
1713{
1714 char_u *line = arg;
1715 linenr_T lnum;
1716 char *errormsg;
1717 int above = eap->forceit;
1718
1719 eap->regname = *line;
1720
1721 if (eap->regname == '=')
1722 {
1723 char_u *p = line + 1;
1724
1725 if (compile_expr0(&p, cctx) == FAIL)
1726 return NULL;
1727 line = p;
1728 }
1729 else if (eap->regname != NUL)
1730 ++line;
1731
1732 if (compile_variable_range(eap, cctx) == OK)
1733 {
1734 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
1735 }
1736 else
1737 {
1738 // Either no range or a number.
1739 // "errormsg" will not be set because the range is ADDR_LINES.
1740 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
1741 // cannot happen
1742 return NULL;
1743 if (eap->addr_count == 0)
1744 lnum = -1;
1745 else
1746 lnum = eap->line2;
1747 if (above)
1748 --lnum;
1749 }
1750
1751 generate_PUT(cctx, eap->regname, lnum);
1752 return line;
1753}
1754
1755/*
1756 * A command that is not compiled, execute with legacy code.
1757 */
1758 char_u *
1759compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
1760{
1761 char_u *line = line_arg;
1762 char_u *p;
1763 int has_expr = FALSE;
1764 char_u *nextcmd = (char_u *)"";
1765 char_u *tofree = NULL;
1766 char_u *cmd_arg = NULL;
1767
1768 if (cctx->ctx_skip == SKIP_YES)
1769 goto theend;
1770
1771 // If there was a prececing command modifier, drop it and include it in the
1772 // EXEC command.
1773 if (cctx->ctx_has_cmdmod)
1774 {
1775 garray_T *instr = &cctx->ctx_instr;
1776 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1777
1778 if (isn->isn_type == ISN_CMDMOD)
1779 {
1780 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
1781 ->cmod_filter_regmatch.regprog);
1782 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
1783 --instr->ga_len;
1784 cctx->ctx_has_cmdmod = FALSE;
1785 }
1786 }
1787
1788 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
1789 {
1790 long argt = eap->argt;
1791 int usefilter = FALSE;
1792
1793 has_expr = argt & (EX_XFILE | EX_EXPAND);
1794
1795 // If the command can be followed by a bar, find the bar and truncate
1796 // it, so that the following command can be compiled.
1797 // The '|' is overwritten with a NUL, it is put back below.
1798 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
1799 && *eap->arg == '!')
1800 // :w !filter or :r !filter or :r! filter
1801 usefilter = TRUE;
1802 if ((argt & EX_TRLBAR) && !usefilter)
1803 {
1804 eap->argt = argt;
1805 separate_nextcmd(eap);
1806 if (eap->nextcmd != NULL)
1807 nextcmd = eap->nextcmd;
1808 }
1809 else if (eap->cmdidx == CMD_wincmd)
1810 {
1811 p = eap->arg;
1812 if (*p != NUL)
1813 ++p;
1814 if (*p == 'g' || *p == Ctrl_G)
1815 ++p;
1816 p = skipwhite(p);
1817 if (*p == '|')
1818 {
1819 *p = NUL;
1820 nextcmd = p + 1;
1821 }
1822 }
1823 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
1824 {
1825 // If there is a trailing '{' read lines until the '}'
1826 p = eap->arg + STRLEN(eap->arg) - 1;
1827 while (p > eap->arg && VIM_ISWHITE(*p))
1828 --p;
1829 if (*p == '{')
1830 {
1831 exarg_T ea;
1832 int flags; // unused
1833 int start_lnum = SOURCING_LNUM;
1834
1835 CLEAR_FIELD(ea);
1836 ea.arg = eap->arg;
1837 fill_exarg_from_cctx(&ea, cctx);
1838 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
1839 if (tofree != NULL)
1840 {
1841 *p = NUL;
1842 line = concat_str(line, tofree);
1843 if (line == NULL)
1844 goto theend;
1845 vim_free(tofree);
1846 tofree = line;
1847 SOURCING_LNUM = start_lnum;
1848 }
1849 }
1850 }
1851 }
1852
1853 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
1854 {
1855 // expand filename in "syntax include [@group] filename"
1856 has_expr = TRUE;
1857 eap->arg = skipwhite(eap->arg + 7);
1858 if (*eap->arg == '@')
1859 eap->arg = skiptowhite(eap->arg);
1860 }
1861
1862 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
1863 && STRLEN(eap->arg) > 4)
1864 {
1865 int delim = *eap->arg;
1866
1867 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
1868 if (*p == delim)
1869 cmd_arg = p + 1;
1870 }
1871
1872 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
1873 cmd_arg = eap->arg;
1874
1875 if (cmd_arg != NULL)
1876 {
1877 exarg_T nea;
1878
1879 CLEAR_FIELD(nea);
1880 nea.cmd = cmd_arg;
1881 p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
1882 if (nea.cmdidx < CMD_SIZE)
1883 {
1884 has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
1885 if (has_expr)
1886 eap->arg = skiptowhite(eap->arg);
1887 }
1888 }
1889
1890 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
1891 {
1892 int count = 0;
1893 char_u *start = skipwhite(line);
1894
1895 // :cmd xxx`=expr1`yyy`=expr2`zzz
1896 // PUSHS ":cmd xxx"
1897 // eval expr1
1898 // PUSHS "yyy"
1899 // eval expr2
1900 // PUSHS "zzz"
1901 // EXECCONCAT 5
1902 for (;;)
1903 {
1904 if (p > start)
1905 {
1906 char_u *val = vim_strnsave(start, p - start);
1907
1908 generate_PUSHS(cctx, &val);
1909 ++count;
1910 }
1911 p += 2;
1912 if (compile_expr0(&p, cctx) == FAIL)
1913 return NULL;
1914 may_generate_2STRING(-1, TRUE, cctx);
1915 ++count;
1916 p = skipwhite(p);
1917 if (*p != '`')
1918 {
1919 emsg(_(e_missing_backtick));
1920 return NULL;
1921 }
1922 start = p + 1;
1923
1924 p = (char_u *)strstr((char *)start, "`=");
1925 if (p == NULL)
1926 {
1927 if (*skipwhite(start) != NUL)
1928 {
1929 char_u *val = vim_strsave(start);
1930
1931 generate_PUSHS(cctx, &val);
1932 ++count;
1933 }
1934 break;
1935 }
1936 }
1937 generate_EXECCONCAT(cctx, count);
1938 }
1939 else
1940 generate_EXEC_copy(cctx, ISN_EXEC, line);
1941
1942theend:
1943 if (*nextcmd != NUL)
1944 {
1945 // the parser expects a pointer to the bar, put it back
1946 --nextcmd;
1947 *nextcmd = '|';
1948 }
1949 vim_free(tofree);
1950
1951 return nextcmd;
1952}
1953
1954/*
1955 * A script command with heredoc, e.g.
1956 * ruby << EOF
1957 * command
1958 * EOF
1959 * Has been turned into one long line with NL characters by
1960 * get_function_body():
1961 * ruby << EOF<NL> command<NL>EOF
1962 */
1963 char_u *
1964compile_script(char_u *line, cctx_T *cctx)
1965{
1966 if (cctx->ctx_skip != SKIP_YES)
1967 {
1968 isn_T *isn;
1969
1970 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
1971 return NULL;
1972 isn->isn_arg.string = vim_strsave(line);
1973 }
1974 return (char_u *)"";
1975}
1976
1977
1978/*
1979 * :s/pat/repl/
1980 */
1981 char_u *
1982compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
1983{
1984 char_u *cmd = eap->arg;
1985 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
1986
1987 if (expr != NULL)
1988 {
1989 int delimiter = *cmd++;
1990
1991 // There is a \=expr, find it in the substitute part.
1992 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
1993 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
1994 {
1995 garray_T save_ga = cctx->ctx_instr;
1996 char_u *end;
1997 int expr_res;
1998 int trailing_error;
1999 int instr_count;
2000 isn_T *instr;
2001 isn_T *isn;
2002
2003 cmd += 3;
2004 end = skip_substitute(cmd, delimiter);
2005
2006 // Temporarily reset the list of instructions so that the jump
2007 // labels are correct.
2008 cctx->ctx_instr.ga_len = 0;
2009 cctx->ctx_instr.ga_maxlen = 0;
2010 cctx->ctx_instr.ga_data = NULL;
2011 expr_res = compile_expr0(&cmd, cctx);
2012 if (end[-1] == NUL)
2013 end[-1] = delimiter;
2014 cmd = skipwhite(cmd);
2015 trailing_error = *cmd != delimiter && *cmd != NUL;
2016
2017 if (expr_res == FAIL || trailing_error
2018 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
2019 {
2020 if (trailing_error)
2021 semsg(_(e_trailing_arg), cmd);
2022 clear_instr_ga(&cctx->ctx_instr);
2023 cctx->ctx_instr = save_ga;
2024 return NULL;
2025 }
2026
2027 // Move the generated instructions into the ISN_SUBSTITUTE
2028 // instructions, then restore the list of instructions before
2029 // adding the ISN_SUBSTITUTE instruction.
2030 instr_count = cctx->ctx_instr.ga_len;
2031 instr = cctx->ctx_instr.ga_data;
2032 instr[instr_count].isn_type = ISN_FINISH;
2033
2034 cctx->ctx_instr = save_ga;
2035 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
2036 {
2037 int idx;
2038
2039 for (idx = 0; idx < instr_count; ++idx)
2040 delete_instr(instr + idx);
2041 vim_free(instr);
2042 return NULL;
2043 }
2044 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
2045 isn->isn_arg.subs.subs_instr = instr;
2046
2047 // skip over flags
2048 if (*end == '&')
2049 ++end;
2050 while (ASCII_ISALPHA(*end) || *end == '#')
2051 ++end;
2052 return end;
2053 }
2054 }
2055
2056 return compile_exec(arg, eap, cctx);
2057}
2058
2059 char_u *
2060compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
2061{
2062 char_u *arg = eap->arg;
2063 lhs_T *lhs = &cctx->ctx_redir_lhs;
2064
2065 if (lhs->lhs_name != NULL)
2066 {
2067 if (STRNCMP(arg, "END", 3) == 0)
2068 {
2069 if (lhs->lhs_append)
2070 {
2071 // First load the current variable value.
2072 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
2073 cctx) == FAIL)
2074 return NULL;
2075 }
2076
2077 // Gets the redirected text and put it on the stack, then store it
2078 // in the variable.
2079 generate_instr_type(cctx, ISN_REDIREND, &t_string);
2080
2081 if (lhs->lhs_append)
2082 generate_instr_drop(cctx, ISN_CONCAT, 1);
2083
2084 if (lhs->lhs_has_index)
2085 {
2086 // Use the info in "lhs" to store the value at the index in the
2087 // list or dict.
2088 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
2089 &t_string, cctx) == FAIL)
2090 return NULL;
2091 }
2092 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
2093 return NULL;
2094
2095 VIM_CLEAR(lhs->lhs_name);
2096 VIM_CLEAR(lhs->lhs_whole);
2097 return arg + 3;
2098 }
2099 emsg(_(e_cannot_nest_redir));
2100 return NULL;
2101 }
2102
2103 if (arg[0] == '=' && arg[1] == '>')
2104 {
2105 int append = FALSE;
2106
2107 // redirect to a variable is compiled
2108 arg += 2;
2109 if (*arg == '>')
2110 {
2111 ++arg;
2112 append = TRUE;
2113 }
2114 arg = skipwhite(arg);
2115
2116 if (compile_assign_lhs(arg, lhs, CMD_redir,
2117 FALSE, FALSE, 1, cctx) == FAIL)
2118 return NULL;
2119 if (need_type(&t_string, lhs->lhs_member_type,
2120 -1, 0, cctx, FALSE, FALSE) == FAIL)
2121 return NULL;
2122 generate_instr(cctx, ISN_REDIRSTART);
2123 lhs->lhs_append = append;
2124 if (lhs->lhs_has_index)
2125 {
2126 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
2127 if (lhs->lhs_whole == NULL)
2128 return NULL;
2129 }
2130
2131 return arg + lhs->lhs_varlen_total;
2132 }
2133
2134 // other redirects are handled like at script level
2135 return compile_exec(line, eap, cctx);
2136}
2137
2138#if defined(FEAT_QUICKFIX) || defined(PROTO)
2139 char_u *
2140compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
2141{
2142 isn_T *isn;
2143 char_u *p;
2144
2145 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
2146 if (isn == NULL)
2147 return NULL;
2148 isn->isn_arg.number = eap->cmdidx;
2149
2150 p = eap->arg;
2151 if (compile_expr0(&p, cctx) == FAIL)
2152 return NULL;
2153
2154 isn = generate_instr(cctx, ISN_CEXPR_CORE);
2155 if (isn == NULL)
2156 return NULL;
2157 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
2158 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
2159 return NULL;
2160 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
2161 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
2162 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
2163
2164 return p;
2165}
2166#endif
2167
2168/*
2169 * Compile "return [expr]".
2170 * When "legacy" is TRUE evaluate [expr] with legacy syntax
2171 */
2172 char_u *
2173compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
2174{
2175 char_u *p = arg;
2176 garray_T *stack = &cctx->ctx_type_stack;
2177 type_T *stack_type;
2178
2179 if (*p != NUL && *p != '|' && *p != '\n')
2180 {
2181 if (legacy)
2182 {
2183 int save_flags = cmdmod.cmod_flags;
2184
2185 generate_LEGACY_EVAL(cctx, p);
2186 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
2187 0, cctx, FALSE, FALSE) == FAIL)
2188 return NULL;
2189 cmdmod.cmod_flags |= CMOD_LEGACY;
2190 (void)skip_expr(&p, NULL);
2191 cmdmod.cmod_flags = save_flags;
2192 }
2193 else
2194 {
2195 // compile return argument into instructions
2196 if (compile_expr0(&p, cctx) == FAIL)
2197 return NULL;
2198 }
2199
2200 if (cctx->ctx_skip != SKIP_YES)
2201 {
2202 // "check_return_type" with uf_ret_type set to &t_unknown is used
2203 // for an inline function without a specified return type. Set the
2204 // return type here.
2205 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2206 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
2207 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
2208 || cctx->ctx_ufunc->uf_ret_type == &t_any))
2209 || (!check_return_type
2210 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
2211 {
2212 cctx->ctx_ufunc->uf_ret_type = stack_type;
2213 }
2214 else
2215 {
2216 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
2217 && stack_type->tt_type != VAR_VOID
2218 && stack_type->tt_type != VAR_UNKNOWN)
2219 {
2220 emsg(_(e_returning_value_in_function_without_return_type));
2221 return NULL;
2222 }
2223 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
2224 0, cctx, FALSE, FALSE) == FAIL)
2225 return NULL;
2226 }
2227 }
2228 }
2229 else
2230 {
2231 // "check_return_type" cannot be TRUE, only used for a lambda which
2232 // always has an argument.
2233 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
2234 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
2235 {
2236 emsg(_(e_missing_return_value));
2237 return NULL;
2238 }
2239
2240 // No argument, return zero.
2241 generate_PUSHNR(cctx, 0);
2242 }
2243
2244 // Undo any command modifiers.
2245 generate_undo_cmdmods(cctx);
2246
2247 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
2248 return NULL;
2249
2250 // "return val | endif" is possible
2251 return skipwhite(p);
2252}
2253
2254/*
2255 * Check if the separator for a :global or :substitute command is OK.
2256 */
2257 int
2258check_global_and_subst(char_u *cmd, char_u *arg)
2259{
2260 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
2261 {
2262 semsg(_(e_separator_not_supported_str), arg);
2263 return FAIL;
2264 }
2265 if (VIM_ISWHITE(cmd[1]))
2266 {
2267 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
2268 return FAIL;
2269 }
2270 return OK;
2271}
2272
2273
2274#endif // defined(FEAT_EVAL)