blob: ad54659d826f7589ad1ad144e56500c73f5993e9 [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 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000843 // If we know the type of "var" and it is not a supported type we can
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000844 // give an error now.
845 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000846 if (vartype->tt_type != VAR_LIST
847 && vartype->tt_type != VAR_STRING
848 && vartype->tt_type != VAR_BLOB
849 && vartype->tt_type != VAR_ANY
850 && vartype->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000851 {
852 semsg(_(e_for_loop_on_str_not_supported),
853 vartype_name(vartype->tt_type));
854 drop_scope(cctx);
855 return NULL;
856 }
857
858 if (vartype->tt_type == VAR_STRING)
859 item_type = &t_string;
860 else if (vartype->tt_type == VAR_BLOB)
861 item_type = &t_number;
862 else if (vartype->tt_type == VAR_LIST
863 && vartype->tt_member->tt_type != VAR_ANY)
864 {
865 if (!var_list)
866 item_type = vartype->tt_member;
867 else if (vartype->tt_member->tt_type == VAR_LIST
868 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
869 item_type = vartype->tt_member->tt_member;
870 }
871
872 // CMDMOD_REV must come before the FOR instruction.
873 generate_undo_cmdmods(cctx);
874
875 // "for_end" is set when ":endfor" is found
876 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
877
878 generate_FOR(cctx, loop_lvar->lv_idx);
879
880 arg = arg_start;
881 if (var_list)
882 {
883 generate_UNPACK(cctx, var_count, semicolon);
884 arg = skipwhite(arg + 1); // skip white after '['
885
886 // the list item is replaced by a number of items
887 if (GA_GROW_FAILS(stack, var_count - 1))
888 {
889 drop_scope(cctx);
890 return NULL;
891 }
892 --stack->ga_len;
893 for (idx = 0; idx < var_count; ++idx)
894 {
895 ((type_T **)stack->ga_data)[stack->ga_len] =
896 (semicolon && idx == 0) ? vartype : item_type;
897 ++stack->ga_len;
898 }
899 }
900
901 for (idx = 0; idx < var_count; ++idx)
902 {
903 assign_dest_T dest = dest_local;
904 int opt_flags = 0;
905 int vimvaridx = -1;
906 type_T *type = &t_any;
907 type_T *lhs_type = &t_any;
908 where_T where = WHERE_INIT;
909
910 p = skip_var_one(arg, FALSE);
911 varlen = p - arg;
912 name = vim_strnsave(arg, varlen);
913 if (name == NULL)
914 goto failed;
915 if (*p == ':')
916 {
917 p = skipwhite(p + 1);
918 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
919 }
920
921 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
922 &vimvaridx, &type, cctx) == FAIL)
923 goto failed;
924 if (dest != dest_local)
925 {
926 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
927 0, 0, type, name) == FAIL)
928 goto failed;
929 }
930 else if (varlen == 1 && *arg == '_')
931 {
932 // Assigning to "_": drop the value.
933 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
934 goto failed;
935 }
936 else
937 {
938 // Script var is not supported.
939 if (STRNCMP(name, "s:", 2) == 0)
940 {
941 emsg(_(e_cannot_use_script_variable_in_for_loop));
942 goto failed;
943 }
944
945 if (!valid_varname(arg, (int)varlen, FALSE))
946 goto failed;
947 if (lookup_local(arg, varlen, NULL, cctx) == OK)
948 {
949 semsg(_(e_variable_already_declared), arg);
950 goto failed;
951 }
952
953 // Reserve a variable to store "var".
954 where.wt_index = var_list ? idx + 1 : 0;
955 where.wt_variable = TRUE;
956 if (lhs_type == &t_any)
957 lhs_type = item_type;
958 else if (item_type != &t_unknown
959 && (item_type == &t_any
960 ? need_type(item_type, lhs_type,
961 -1, 0, cctx, FALSE, FALSE)
962 : check_type(lhs_type, item_type, TRUE, where))
963 == FAIL)
964 goto failed;
965 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
966 if (var_lvar == NULL)
967 // out of memory or used as an argument
968 goto failed;
969
970 if (semicolon && idx == var_count - 1)
971 var_lvar->lv_type = vartype;
972 else
973 var_lvar->lv_type = item_type;
974 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
975 }
976
977 if (*p == ',' || *p == ';')
978 ++p;
979 arg = skipwhite(p);
980 vim_free(name);
981 }
982
983 if (cctx->ctx_compile_type == CT_DEBUG)
984 {
985 int save_prev_lnum = cctx->ctx_prev_lnum;
986
987 // Add ISN_DEBUG here, so that the loop variables can be inspected.
988 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
989 cctx->ctx_prev_lnum = prev_lnum;
990 generate_instr_debug(cctx);
991 cctx->ctx_prev_lnum = save_prev_lnum;
992 }
993 }
994
995 return arg_end;
996
997failed:
998 vim_free(name);
999 drop_scope(cctx);
1000 return NULL;
1001}
1002
1003/*
1004 * compile "endfor"
1005 */
1006 char_u *
1007compile_endfor(char_u *arg, cctx_T *cctx)
1008{
1009 garray_T *instr = &cctx->ctx_instr;
1010 scope_T *scope = cctx->ctx_scope;
1011 forscope_T *forscope;
1012 isn_T *isn;
1013
1014 if (misplaced_cmdmod(cctx))
1015 return NULL;
1016
1017 if (scope == NULL || scope->se_type != FOR_SCOPE)
1018 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001019 emsg(_(e_endfor_without_for));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001020 return NULL;
1021 }
1022 forscope = &scope->se_u.se_for;
1023 cctx->ctx_scope = scope->se_outer;
1024 if (cctx->ctx_skip != SKIP_YES)
1025 {
1026 unwind_locals(cctx, scope->se_local_count);
1027
1028 // At end of ":for" scope jump back to the FOR instruction.
1029 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
1030
1031 // Fill in the "end" label in the FOR statement so it can jump here.
1032 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
1033 isn->isn_arg.forloop.for_end = instr->ga_len;
1034
1035 // Fill in the "end" label any BREAK statements
1036 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
1037
1038 // Below the ":for" scope drop the "expr" list from the stack.
1039 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
1040 return NULL;
1041 }
1042
1043 vim_free(scope);
1044
1045 return arg;
1046}
1047
1048/*
1049 * compile "while expr"
1050 *
1051 * Produces instructions:
1052 * top: EVAL expr Push result of "expr"
1053 * JUMP_IF_FALSE end jump if false
1054 * ... body ...
1055 * JUMP top Jump back to repeat
1056 * end:
1057 *
1058 */
1059 char_u *
1060compile_while(char_u *arg, cctx_T *cctx)
1061{
1062 char_u *p = arg;
1063 scope_T *scope;
1064
1065 scope = new_scope(cctx, WHILE_SCOPE);
1066 if (scope == NULL)
1067 return NULL;
1068
1069 // "endwhile" jumps back here, one before when profiling or using cmdmods
1070 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
1071
1072 // compile "expr"
1073 if (compile_expr0(&p, cctx) == FAIL)
1074 return NULL;
1075
1076 if (!ends_excmd2(arg, skipwhite(p)))
1077 {
1078 semsg(_(e_trailing_arg), p);
1079 return NULL;
1080 }
1081
1082 if (cctx->ctx_skip != SKIP_YES)
1083 {
1084 if (bool_on_stack(cctx) == FAIL)
1085 return FAIL;
1086
1087 // CMDMOD_REV must come before the jump
1088 generate_undo_cmdmods(cctx);
1089
1090 // "while_end" is set when ":endwhile" is found
1091 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
1092 JUMP_IF_FALSE, cctx) == FAIL)
1093 return FAIL;
1094 }
1095
1096 return p;
1097}
1098
1099/*
1100 * compile "endwhile"
1101 */
1102 char_u *
1103compile_endwhile(char_u *arg, cctx_T *cctx)
1104{
1105 scope_T *scope = cctx->ctx_scope;
1106 garray_T *instr = &cctx->ctx_instr;
1107
1108 if (misplaced_cmdmod(cctx))
1109 return NULL;
1110 if (scope == NULL || scope->se_type != WHILE_SCOPE)
1111 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001112 emsg(_(e_endwhile_without_while));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001113 return NULL;
1114 }
1115 cctx->ctx_scope = scope->se_outer;
1116 if (cctx->ctx_skip != SKIP_YES)
1117 {
1118 unwind_locals(cctx, scope->se_local_count);
1119
1120#ifdef FEAT_PROFILE
1121 // count the endwhile before jumping
1122 may_generate_prof_end(cctx, cctx->ctx_lnum);
1123#endif
1124
1125 // At end of ":for" scope jump back to the FOR instruction.
1126 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
1127
1128 // Fill in the "end" label in the WHILE statement so it can jump here.
1129 // And in any jumps for ":break"
1130 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
1131 instr->ga_len, cctx);
1132 }
1133
1134 vim_free(scope);
1135
1136 return arg;
1137}
1138
1139/*
1140 * compile "continue"
1141 */
1142 char_u *
1143compile_continue(char_u *arg, cctx_T *cctx)
1144{
1145 scope_T *scope = cctx->ctx_scope;
1146 int try_scopes = 0;
1147 int loop_label;
1148
1149 for (;;)
1150 {
1151 if (scope == NULL)
1152 {
1153 emsg(_(e_continue));
1154 return NULL;
1155 }
1156 if (scope->se_type == FOR_SCOPE)
1157 {
1158 loop_label = scope->se_u.se_for.fs_top_label;
1159 break;
1160 }
1161 if (scope->se_type == WHILE_SCOPE)
1162 {
1163 loop_label = scope->se_u.se_while.ws_top_label;
1164 break;
1165 }
1166 if (scope->se_type == TRY_SCOPE)
1167 ++try_scopes;
1168 scope = scope->se_outer;
1169 }
1170
1171 if (try_scopes > 0)
1172 // Inside one or more try/catch blocks we first need to jump to the
1173 // "finally" or "endtry" to cleanup.
1174 generate_TRYCONT(cctx, try_scopes, loop_label);
1175 else
1176 // Jump back to the FOR or WHILE instruction.
1177 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
1178
1179 return arg;
1180}
1181
1182/*
1183 * compile "break"
1184 */
1185 char_u *
1186compile_break(char_u *arg, cctx_T *cctx)
1187{
1188 scope_T *scope = cctx->ctx_scope;
1189 endlabel_T **el;
1190
1191 for (;;)
1192 {
1193 if (scope == NULL)
1194 {
1195 emsg(_(e_break));
1196 return NULL;
1197 }
1198 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
1199 break;
1200 scope = scope->se_outer;
1201 }
1202
1203 // Jump to the end of the FOR or WHILE loop.
1204 if (scope->se_type == FOR_SCOPE)
1205 el = &scope->se_u.se_for.fs_end_label;
1206 else
1207 el = &scope->se_u.se_while.ws_end_label;
1208 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
1209 return FAIL;
1210
1211 return arg;
1212}
1213
1214/*
1215 * compile "{" start of block
1216 */
1217 char_u *
1218compile_block(char_u *arg, cctx_T *cctx)
1219{
1220 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
1221 return NULL;
1222 return skipwhite(arg + 1);
1223}
1224
1225/*
1226 * compile end of block: drop one scope
1227 */
1228 void
1229compile_endblock(cctx_T *cctx)
1230{
1231 scope_T *scope = cctx->ctx_scope;
1232
1233 cctx->ctx_scope = scope->se_outer;
1234 unwind_locals(cctx, scope->se_local_count);
1235 vim_free(scope);
1236}
1237
1238/*
1239 * Compile "try".
1240 * Creates a new scope for the try-endtry, pointing to the first catch and
1241 * finally.
1242 * Creates another scope for the "try" block itself.
1243 * TRY instruction sets up exception handling at runtime.
1244 *
1245 * "try"
1246 * TRY -> catch1, -> finally push trystack entry
1247 * ... try block
1248 * "throw {exception}"
1249 * EVAL {exception}
1250 * THROW create exception
1251 * ... try block
1252 * " catch {expr}"
1253 * JUMP -> finally
1254 * catch1: PUSH exception
1255 * EVAL {expr}
1256 * MATCH
1257 * JUMP nomatch -> catch2
1258 * CATCH remove exception
1259 * ... catch block
1260 * " catch"
1261 * JUMP -> finally
1262 * catch2: CATCH remove exception
1263 * ... catch block
1264 * " finally"
1265 * finally:
1266 * ... finally block
1267 * " endtry"
1268 * ENDTRY pop trystack entry, may rethrow
1269 */
1270 char_u *
1271compile_try(char_u *arg, cctx_T *cctx)
1272{
1273 garray_T *instr = &cctx->ctx_instr;
1274 scope_T *try_scope;
1275 scope_T *scope;
1276
1277 if (misplaced_cmdmod(cctx))
1278 return NULL;
1279
1280 // scope that holds the jumps that go to catch/finally/endtry
1281 try_scope = new_scope(cctx, TRY_SCOPE);
1282 if (try_scope == NULL)
1283 return NULL;
1284
1285 if (cctx->ctx_skip != SKIP_YES)
1286 {
1287 isn_T *isn;
1288
1289 // "try_catch" is set when the first ":catch" is found or when no catch
1290 // is found and ":finally" is found.
1291 // "try_finally" is set when ":finally" is found
1292 // "try_endtry" is set when ":endtry" is found
1293 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
1294 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
1295 return NULL;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001296 isn->isn_arg.tryref.try_ref = ALLOC_CLEAR_ONE(tryref_T);
1297 if (isn->isn_arg.tryref.try_ref == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001298 return NULL;
1299 }
1300
1301 // scope for the try block itself
1302 scope = new_scope(cctx, BLOCK_SCOPE);
1303 if (scope == NULL)
1304 return NULL;
1305
1306 return arg;
1307}
1308
1309/*
1310 * Compile "catch {expr}".
1311 */
1312 char_u *
1313compile_catch(char_u *arg, cctx_T *cctx UNUSED)
1314{
1315 scope_T *scope = cctx->ctx_scope;
1316 garray_T *instr = &cctx->ctx_instr;
1317 char_u *p;
1318 isn_T *isn;
1319
1320 if (misplaced_cmdmod(cctx))
1321 return NULL;
1322
1323 // end block scope from :try or :catch
1324 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1325 compile_endblock(cctx);
1326 scope = cctx->ctx_scope;
1327
1328 // Error if not in a :try scope
1329 if (scope == NULL || scope->se_type != TRY_SCOPE)
1330 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001331 emsg(_(e_catch_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001332 return NULL;
1333 }
1334
1335 if (scope->se_u.se_try.ts_caught_all)
1336 {
1337 emsg(_(e_catch_unreachable_after_catch_all));
1338 return NULL;
1339 }
1340
1341 if (cctx->ctx_skip != SKIP_YES)
1342 {
1343#ifdef FEAT_PROFILE
1344 // the profile-start should be after the jump
1345 if (cctx->ctx_compile_type == CT_PROFILE
1346 && instr->ga_len > 0
1347 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
1348 .isn_type == ISN_PROF_START)
1349 --instr->ga_len;
1350#endif
1351 // Jump from end of previous block to :finally or :endtry
1352 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
1353 JUMP_ALWAYS, cctx) == FAIL)
1354 return NULL;
1355
1356 // End :try or :catch scope: set value in ISN_TRY instruction
1357 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001358 if (isn->isn_arg.tryref.try_ref->try_catch == 0)
1359 isn->isn_arg.tryref.try_ref->try_catch = instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001360 if (scope->se_u.se_try.ts_catch_label != 0)
1361 {
1362 // Previous catch without match jumps here
1363 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
1364 isn->isn_arg.jump.jump_where = instr->ga_len;
1365 }
1366#ifdef FEAT_PROFILE
1367 if (cctx->ctx_compile_type == CT_PROFILE)
1368 {
1369 // a "throw" that jumps here needs to be counted
1370 generate_instr(cctx, ISN_PROF_END);
1371 // the "catch" is also counted
1372 generate_instr(cctx, ISN_PROF_START);
1373 }
1374#endif
1375 if (cctx->ctx_compile_type == CT_DEBUG)
1376 generate_instr_debug(cctx);
1377 }
1378
1379 p = skipwhite(arg);
1380 if (ends_excmd2(arg, p))
1381 {
1382 scope->se_u.se_try.ts_caught_all = TRUE;
1383 scope->se_u.se_try.ts_catch_label = 0;
1384 }
1385 else
1386 {
1387 char_u *end;
1388 char_u *pat;
1389 char_u *tofree = NULL;
1390 int dropped = 0;
1391 int len;
1392
1393 // Push v:exception, push {expr} and MATCH
1394 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
1395
1396 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
1397 if (*end != *p)
1398 {
1399 semsg(_(e_separator_mismatch_str), p);
1400 vim_free(tofree);
1401 return FAIL;
1402 }
1403 if (tofree == NULL)
1404 len = (int)(end - (p + 1));
1405 else
1406 len = (int)(end - tofree);
1407 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
1408 vim_free(tofree);
1409 p += len + 2 + dropped;
1410 if (pat == NULL)
1411 return FAIL;
1412 if (generate_PUSHS(cctx, &pat) == FAIL)
1413 return FAIL;
1414
1415 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
1416 return NULL;
1417
1418 scope->se_u.se_try.ts_catch_label = instr->ga_len;
1419 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
1420 return NULL;
1421 }
1422
1423 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
1424 return NULL;
1425
1426 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
1427 return NULL;
1428 return p;
1429}
1430
1431 char_u *
1432compile_finally(char_u *arg, cctx_T *cctx)
1433{
1434 scope_T *scope = cctx->ctx_scope;
1435 garray_T *instr = &cctx->ctx_instr;
1436 isn_T *isn;
1437 int this_instr;
1438
1439 if (misplaced_cmdmod(cctx))
1440 return NULL;
1441
1442 // end block scope from :try or :catch
1443 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1444 compile_endblock(cctx);
1445 scope = cctx->ctx_scope;
1446
1447 // Error if not in a :try scope
1448 if (scope == NULL || scope->se_type != TRY_SCOPE)
1449 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001450 emsg(_(e_finally_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001451 return NULL;
1452 }
1453
1454 if (cctx->ctx_skip != SKIP_YES)
1455 {
1456 // End :catch or :finally scope: set value in ISN_TRY instruction
1457 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001458 if (isn->isn_arg.tryref.try_ref->try_finally != 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001459 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001460 emsg(_(e_multiple_finally));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001461 return NULL;
1462 }
1463
1464 this_instr = instr->ga_len;
1465#ifdef FEAT_PROFILE
1466 if (cctx->ctx_compile_type == CT_PROFILE
1467 && ((isn_T *)instr->ga_data)[this_instr - 1]
1468 .isn_type == ISN_PROF_START)
1469 {
1470 // jump to the profile start of the "finally"
1471 --this_instr;
1472
1473 // jump to the profile end above it
1474 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
1475 .isn_type == ISN_PROF_END)
1476 --this_instr;
1477 }
1478#endif
1479
1480 // Fill in the "end" label in jumps at the end of the blocks.
1481 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
1482 this_instr, cctx);
1483
1484 // If there is no :catch then an exception jumps to :finally.
Bram Moolenaar0d807102021-12-21 09:42:09 +00001485 if (isn->isn_arg.tryref.try_ref->try_catch == 0)
1486 isn->isn_arg.tryref.try_ref->try_catch = this_instr;
1487 isn->isn_arg.tryref.try_ref->try_finally = this_instr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001488 if (scope->se_u.se_try.ts_catch_label != 0)
1489 {
1490 // Previous catch without match jumps here
1491 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
1492 isn->isn_arg.jump.jump_where = this_instr;
1493 scope->se_u.se_try.ts_catch_label = 0;
1494 }
1495 if (generate_instr(cctx, ISN_FINALLY) == NULL)
1496 return NULL;
1497 }
1498
1499 return arg;
1500}
1501
1502 char_u *
1503compile_endtry(char_u *arg, cctx_T *cctx)
1504{
1505 scope_T *scope = cctx->ctx_scope;
1506 garray_T *instr = &cctx->ctx_instr;
1507 isn_T *try_isn;
1508
1509 if (misplaced_cmdmod(cctx))
1510 return NULL;
1511
1512 // end block scope from :catch or :finally
1513 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1514 compile_endblock(cctx);
1515 scope = cctx->ctx_scope;
1516
1517 // Error if not in a :try scope
1518 if (scope == NULL || scope->se_type != TRY_SCOPE)
1519 {
1520 if (scope == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001521 emsg(_(e_endtry_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001522 else if (scope->se_type == WHILE_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001523 emsg(_(e_missing_endwhile));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001524 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001525 emsg(_(e_missing_endfor));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001526 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001527 emsg(_(e_missing_endif));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001528 return NULL;
1529 }
1530
1531 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
1532 if (cctx->ctx_skip != SKIP_YES)
1533 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00001534 if (try_isn->isn_arg.tryref.try_ref->try_catch == 0
1535 && try_isn->isn_arg.tryref.try_ref->try_finally == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001536 {
1537 emsg(_(e_missing_catch_or_finally));
1538 return NULL;
1539 }
1540
1541#ifdef FEAT_PROFILE
1542 if (cctx->ctx_compile_type == CT_PROFILE
1543 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
1544 .isn_type == ISN_PROF_START)
1545 // move the profile start after "endtry" so that it's not counted when
1546 // the exception is rethrown.
1547 --instr->ga_len;
1548#endif
1549
1550 // Fill in the "end" label in jumps at the end of the blocks, if not
1551 // done by ":finally".
1552 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
1553 instr->ga_len, cctx);
1554
1555 if (scope->se_u.se_try.ts_catch_label != 0)
1556 {
1557 // Last catch without match jumps here
1558 isn_T *isn = ((isn_T *)instr->ga_data)
1559 + scope->se_u.se_try.ts_catch_label;
1560 isn->isn_arg.jump.jump_where = instr->ga_len;
1561 }
1562 }
1563
1564 compile_endblock(cctx);
1565
1566 if (cctx->ctx_skip != SKIP_YES)
1567 {
1568 // End :catch or :finally scope: set instruction index in ISN_TRY
1569 // instruction
Bram Moolenaar0d807102021-12-21 09:42:09 +00001570 try_isn->isn_arg.tryref.try_ref->try_endtry = instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001571 if (cctx->ctx_skip != SKIP_YES
1572 && generate_instr(cctx, ISN_ENDTRY) == NULL)
1573 return NULL;
1574#ifdef FEAT_PROFILE
1575 if (cctx->ctx_compile_type == CT_PROFILE)
1576 generate_instr(cctx, ISN_PROF_START);
1577#endif
1578 }
1579 return arg;
1580}
1581
1582/*
1583 * compile "throw {expr}"
1584 */
1585 char_u *
1586compile_throw(char_u *arg, cctx_T *cctx UNUSED)
1587{
1588 char_u *p = skipwhite(arg);
1589
1590 if (compile_expr0(&p, cctx) == FAIL)
1591 return NULL;
1592 if (cctx->ctx_skip == SKIP_YES)
1593 return p;
1594 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1595 return NULL;
1596 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
1597 return NULL;
1598
1599 return p;
1600}
1601
1602 char_u *
1603compile_eval(char_u *arg, cctx_T *cctx)
1604{
1605 char_u *p = arg;
1606 int name_only;
1607 long lnum = SOURCING_LNUM;
1608
1609 // find_ex_command() will consider a variable name an expression, assuming
1610 // that something follows on the next line. Check that something actually
1611 // follows, otherwise it's probably a misplaced command.
1612 name_only = cmd_is_name_only(arg);
1613
1614 if (compile_expr0(&p, cctx) == FAIL)
1615 return NULL;
1616
1617 if (name_only && lnum == SOURCING_LNUM)
1618 {
1619 semsg(_(e_expression_without_effect_str), arg);
1620 return NULL;
1621 }
1622
1623 // drop the result
1624 generate_instr_drop(cctx, ISN_DROP, 1);
1625
1626 return skipwhite(p);
1627}
1628
1629/*
1630 * compile "echo expr"
1631 * compile "echomsg expr"
1632 * compile "echoerr expr"
1633 * compile "echoconsole expr"
1634 * compile "execute expr"
1635 */
1636 char_u *
1637compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
1638{
1639 char_u *p = arg;
1640 char_u *prev = arg;
1641 char_u *expr_start;
1642 int count = 0;
1643 int start_ctx_lnum = cctx->ctx_lnum;
1644 garray_T *stack = &cctx->ctx_type_stack;
1645 type_T *type;
1646
1647 for (;;)
1648 {
1649 if (ends_excmd2(prev, p))
1650 break;
1651 expr_start = p;
1652 if (compile_expr0(&p, cctx) == FAIL)
1653 return NULL;
1654
1655 if (cctx->ctx_skip != SKIP_YES)
1656 {
1657 // check for non-void type
1658 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1659 if (type->tt_type == VAR_VOID)
1660 {
1661 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
1662 return NULL;
1663 }
1664 }
1665
1666 ++count;
1667 prev = p;
1668 p = skipwhite(p);
1669 }
1670
1671 if (count > 0)
1672 {
1673 long save_lnum = cctx->ctx_lnum;
1674
1675 // Use the line number where the command started.
1676 cctx->ctx_lnum = start_ctx_lnum;
1677
1678 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
1679 generate_ECHO(cctx, cmdidx == CMD_echo, count);
1680 else if (cmdidx == CMD_execute)
1681 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
1682 else if (cmdidx == CMD_echomsg)
1683 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
1684 else if (cmdidx == CMD_echoconsole)
1685 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
1686 else
1687 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
1688
1689 cctx->ctx_lnum = save_lnum;
1690 }
1691 return p;
1692}
1693
1694/*
1695 * If "eap" has a range that is not a constant generate an ISN_RANGE
1696 * instruction to compute it and return OK.
1697 * Otherwise return FAIL, the caller must deal with any range.
1698 */
1699 static int
1700compile_variable_range(exarg_T *eap, cctx_T *cctx)
1701{
1702 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
1703 char_u *p = skipdigits(eap->cmd);
1704
1705 if (p == range_end)
1706 return FAIL;
1707 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
1708}
1709
1710/*
1711 * :put r
1712 * :put ={expr}
1713 */
1714 char_u *
1715compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
1716{
1717 char_u *line = arg;
1718 linenr_T lnum;
1719 char *errormsg;
1720 int above = eap->forceit;
1721
1722 eap->regname = *line;
1723
1724 if (eap->regname == '=')
1725 {
1726 char_u *p = line + 1;
1727
1728 if (compile_expr0(&p, cctx) == FAIL)
1729 return NULL;
1730 line = p;
1731 }
1732 else if (eap->regname != NUL)
1733 ++line;
1734
1735 if (compile_variable_range(eap, cctx) == OK)
1736 {
1737 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
1738 }
1739 else
1740 {
1741 // Either no range or a number.
1742 // "errormsg" will not be set because the range is ADDR_LINES.
1743 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
1744 // cannot happen
1745 return NULL;
1746 if (eap->addr_count == 0)
1747 lnum = -1;
1748 else
1749 lnum = eap->line2;
1750 if (above)
1751 --lnum;
1752 }
1753
1754 generate_PUT(cctx, eap->regname, lnum);
1755 return line;
1756}
1757
1758/*
1759 * A command that is not compiled, execute with legacy code.
1760 */
1761 char_u *
1762compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
1763{
1764 char_u *line = line_arg;
1765 char_u *p;
1766 int has_expr = FALSE;
1767 char_u *nextcmd = (char_u *)"";
1768 char_u *tofree = NULL;
1769 char_u *cmd_arg = NULL;
1770
1771 if (cctx->ctx_skip == SKIP_YES)
1772 goto theend;
1773
1774 // If there was a prececing command modifier, drop it and include it in the
1775 // EXEC command.
1776 if (cctx->ctx_has_cmdmod)
1777 {
1778 garray_T *instr = &cctx->ctx_instr;
1779 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1780
1781 if (isn->isn_type == ISN_CMDMOD)
1782 {
1783 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
1784 ->cmod_filter_regmatch.regprog);
1785 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
1786 --instr->ga_len;
1787 cctx->ctx_has_cmdmod = FALSE;
1788 }
1789 }
1790
1791 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
1792 {
1793 long argt = eap->argt;
1794 int usefilter = FALSE;
1795
1796 has_expr = argt & (EX_XFILE | EX_EXPAND);
1797
1798 // If the command can be followed by a bar, find the bar and truncate
1799 // it, so that the following command can be compiled.
1800 // The '|' is overwritten with a NUL, it is put back below.
1801 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
1802 && *eap->arg == '!')
1803 // :w !filter or :r !filter or :r! filter
1804 usefilter = TRUE;
1805 if ((argt & EX_TRLBAR) && !usefilter)
1806 {
1807 eap->argt = argt;
1808 separate_nextcmd(eap);
1809 if (eap->nextcmd != NULL)
1810 nextcmd = eap->nextcmd;
1811 }
1812 else if (eap->cmdidx == CMD_wincmd)
1813 {
1814 p = eap->arg;
1815 if (*p != NUL)
1816 ++p;
1817 if (*p == 'g' || *p == Ctrl_G)
1818 ++p;
1819 p = skipwhite(p);
1820 if (*p == '|')
1821 {
1822 *p = NUL;
1823 nextcmd = p + 1;
1824 }
1825 }
1826 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
1827 {
1828 // If there is a trailing '{' read lines until the '}'
1829 p = eap->arg + STRLEN(eap->arg) - 1;
1830 while (p > eap->arg && VIM_ISWHITE(*p))
1831 --p;
1832 if (*p == '{')
1833 {
1834 exarg_T ea;
1835 int flags; // unused
1836 int start_lnum = SOURCING_LNUM;
1837
1838 CLEAR_FIELD(ea);
1839 ea.arg = eap->arg;
1840 fill_exarg_from_cctx(&ea, cctx);
1841 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
1842 if (tofree != NULL)
1843 {
1844 *p = NUL;
1845 line = concat_str(line, tofree);
1846 if (line == NULL)
1847 goto theend;
1848 vim_free(tofree);
1849 tofree = line;
1850 SOURCING_LNUM = start_lnum;
1851 }
1852 }
1853 }
1854 }
1855
1856 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
1857 {
1858 // expand filename in "syntax include [@group] filename"
1859 has_expr = TRUE;
1860 eap->arg = skipwhite(eap->arg + 7);
1861 if (*eap->arg == '@')
1862 eap->arg = skiptowhite(eap->arg);
1863 }
1864
1865 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
1866 && STRLEN(eap->arg) > 4)
1867 {
1868 int delim = *eap->arg;
1869
1870 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
1871 if (*p == delim)
1872 cmd_arg = p + 1;
1873 }
1874
1875 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
1876 cmd_arg = eap->arg;
1877
1878 if (cmd_arg != NULL)
1879 {
1880 exarg_T nea;
1881
1882 CLEAR_FIELD(nea);
1883 nea.cmd = cmd_arg;
1884 p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
1885 if (nea.cmdidx < CMD_SIZE)
1886 {
1887 has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
1888 if (has_expr)
1889 eap->arg = skiptowhite(eap->arg);
1890 }
1891 }
1892
1893 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
1894 {
1895 int count = 0;
1896 char_u *start = skipwhite(line);
1897
1898 // :cmd xxx`=expr1`yyy`=expr2`zzz
1899 // PUSHS ":cmd xxx"
1900 // eval expr1
1901 // PUSHS "yyy"
1902 // eval expr2
1903 // PUSHS "zzz"
1904 // EXECCONCAT 5
1905 for (;;)
1906 {
1907 if (p > start)
1908 {
1909 char_u *val = vim_strnsave(start, p - start);
1910
1911 generate_PUSHS(cctx, &val);
1912 ++count;
1913 }
1914 p += 2;
1915 if (compile_expr0(&p, cctx) == FAIL)
1916 return NULL;
1917 may_generate_2STRING(-1, TRUE, cctx);
1918 ++count;
1919 p = skipwhite(p);
1920 if (*p != '`')
1921 {
1922 emsg(_(e_missing_backtick));
1923 return NULL;
1924 }
1925 start = p + 1;
1926
1927 p = (char_u *)strstr((char *)start, "`=");
1928 if (p == NULL)
1929 {
1930 if (*skipwhite(start) != NUL)
1931 {
1932 char_u *val = vim_strsave(start);
1933
1934 generate_PUSHS(cctx, &val);
1935 ++count;
1936 }
1937 break;
1938 }
1939 }
1940 generate_EXECCONCAT(cctx, count);
1941 }
1942 else
1943 generate_EXEC_copy(cctx, ISN_EXEC, line);
1944
1945theend:
1946 if (*nextcmd != NUL)
1947 {
1948 // the parser expects a pointer to the bar, put it back
1949 --nextcmd;
1950 *nextcmd = '|';
1951 }
1952 vim_free(tofree);
1953
1954 return nextcmd;
1955}
1956
1957/*
1958 * A script command with heredoc, e.g.
1959 * ruby << EOF
1960 * command
1961 * EOF
1962 * Has been turned into one long line with NL characters by
1963 * get_function_body():
1964 * ruby << EOF<NL> command<NL>EOF
1965 */
1966 char_u *
1967compile_script(char_u *line, cctx_T *cctx)
1968{
1969 if (cctx->ctx_skip != SKIP_YES)
1970 {
1971 isn_T *isn;
1972
1973 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
1974 return NULL;
1975 isn->isn_arg.string = vim_strsave(line);
1976 }
1977 return (char_u *)"";
1978}
1979
1980
1981/*
1982 * :s/pat/repl/
1983 */
1984 char_u *
1985compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
1986{
1987 char_u *cmd = eap->arg;
1988 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
1989
1990 if (expr != NULL)
1991 {
1992 int delimiter = *cmd++;
1993
1994 // There is a \=expr, find it in the substitute part.
1995 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
1996 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
1997 {
1998 garray_T save_ga = cctx->ctx_instr;
1999 char_u *end;
2000 int expr_res;
2001 int trailing_error;
2002 int instr_count;
2003 isn_T *instr;
2004 isn_T *isn;
2005
2006 cmd += 3;
2007 end = skip_substitute(cmd, delimiter);
2008
2009 // Temporarily reset the list of instructions so that the jump
2010 // labels are correct.
2011 cctx->ctx_instr.ga_len = 0;
2012 cctx->ctx_instr.ga_maxlen = 0;
2013 cctx->ctx_instr.ga_data = NULL;
2014 expr_res = compile_expr0(&cmd, cctx);
2015 if (end[-1] == NUL)
2016 end[-1] = delimiter;
2017 cmd = skipwhite(cmd);
2018 trailing_error = *cmd != delimiter && *cmd != NUL;
2019
2020 if (expr_res == FAIL || trailing_error
2021 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
2022 {
2023 if (trailing_error)
2024 semsg(_(e_trailing_arg), cmd);
2025 clear_instr_ga(&cctx->ctx_instr);
2026 cctx->ctx_instr = save_ga;
2027 return NULL;
2028 }
2029
2030 // Move the generated instructions into the ISN_SUBSTITUTE
2031 // instructions, then restore the list of instructions before
2032 // adding the ISN_SUBSTITUTE instruction.
2033 instr_count = cctx->ctx_instr.ga_len;
2034 instr = cctx->ctx_instr.ga_data;
2035 instr[instr_count].isn_type = ISN_FINISH;
2036
2037 cctx->ctx_instr = save_ga;
2038 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
2039 {
2040 int idx;
2041
2042 for (idx = 0; idx < instr_count; ++idx)
2043 delete_instr(instr + idx);
2044 vim_free(instr);
2045 return NULL;
2046 }
2047 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
2048 isn->isn_arg.subs.subs_instr = instr;
2049
2050 // skip over flags
2051 if (*end == '&')
2052 ++end;
2053 while (ASCII_ISALPHA(*end) || *end == '#')
2054 ++end;
2055 return end;
2056 }
2057 }
2058
2059 return compile_exec(arg, eap, cctx);
2060}
2061
2062 char_u *
2063compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
2064{
2065 char_u *arg = eap->arg;
2066 lhs_T *lhs = &cctx->ctx_redir_lhs;
2067
2068 if (lhs->lhs_name != NULL)
2069 {
2070 if (STRNCMP(arg, "END", 3) == 0)
2071 {
2072 if (lhs->lhs_append)
2073 {
2074 // First load the current variable value.
2075 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
2076 cctx) == FAIL)
2077 return NULL;
2078 }
2079
2080 // Gets the redirected text and put it on the stack, then store it
2081 // in the variable.
2082 generate_instr_type(cctx, ISN_REDIREND, &t_string);
2083
2084 if (lhs->lhs_append)
2085 generate_instr_drop(cctx, ISN_CONCAT, 1);
2086
2087 if (lhs->lhs_has_index)
2088 {
2089 // Use the info in "lhs" to store the value at the index in the
2090 // list or dict.
2091 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
2092 &t_string, cctx) == FAIL)
2093 return NULL;
2094 }
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002095 else if (generate_store_lhs(cctx, lhs, -1, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002096 return NULL;
2097
2098 VIM_CLEAR(lhs->lhs_name);
2099 VIM_CLEAR(lhs->lhs_whole);
2100 return arg + 3;
2101 }
2102 emsg(_(e_cannot_nest_redir));
2103 return NULL;
2104 }
2105
2106 if (arg[0] == '=' && arg[1] == '>')
2107 {
2108 int append = FALSE;
2109
2110 // redirect to a variable is compiled
2111 arg += 2;
2112 if (*arg == '>')
2113 {
2114 ++arg;
2115 append = TRUE;
2116 }
2117 arg = skipwhite(arg);
2118
2119 if (compile_assign_lhs(arg, lhs, CMD_redir,
2120 FALSE, FALSE, 1, cctx) == FAIL)
2121 return NULL;
2122 if (need_type(&t_string, lhs->lhs_member_type,
2123 -1, 0, cctx, FALSE, FALSE) == FAIL)
2124 return NULL;
2125 generate_instr(cctx, ISN_REDIRSTART);
2126 lhs->lhs_append = append;
2127 if (lhs->lhs_has_index)
2128 {
2129 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
2130 if (lhs->lhs_whole == NULL)
2131 return NULL;
2132 }
2133
2134 return arg + lhs->lhs_varlen_total;
2135 }
2136
2137 // other redirects are handled like at script level
2138 return compile_exec(line, eap, cctx);
2139}
2140
2141#if defined(FEAT_QUICKFIX) || defined(PROTO)
2142 char_u *
2143compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
2144{
2145 isn_T *isn;
2146 char_u *p;
2147
2148 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
2149 if (isn == NULL)
2150 return NULL;
2151 isn->isn_arg.number = eap->cmdidx;
2152
2153 p = eap->arg;
2154 if (compile_expr0(&p, cctx) == FAIL)
2155 return NULL;
2156
2157 isn = generate_instr(cctx, ISN_CEXPR_CORE);
2158 if (isn == NULL)
2159 return NULL;
2160 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
2161 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
2162 return NULL;
2163 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
2164 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
2165 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
2166
2167 return p;
2168}
2169#endif
2170
2171/*
2172 * Compile "return [expr]".
2173 * When "legacy" is TRUE evaluate [expr] with legacy syntax
2174 */
2175 char_u *
2176compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
2177{
2178 char_u *p = arg;
2179 garray_T *stack = &cctx->ctx_type_stack;
2180 type_T *stack_type;
2181
2182 if (*p != NUL && *p != '|' && *p != '\n')
2183 {
2184 if (legacy)
2185 {
2186 int save_flags = cmdmod.cmod_flags;
2187
2188 generate_LEGACY_EVAL(cctx, p);
2189 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
2190 0, cctx, FALSE, FALSE) == FAIL)
2191 return NULL;
2192 cmdmod.cmod_flags |= CMOD_LEGACY;
2193 (void)skip_expr(&p, NULL);
2194 cmdmod.cmod_flags = save_flags;
2195 }
2196 else
2197 {
2198 // compile return argument into instructions
2199 if (compile_expr0(&p, cctx) == FAIL)
2200 return NULL;
2201 }
2202
2203 if (cctx->ctx_skip != SKIP_YES)
2204 {
2205 // "check_return_type" with uf_ret_type set to &t_unknown is used
2206 // for an inline function without a specified return type. Set the
2207 // return type here.
2208 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2209 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
2210 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
2211 || cctx->ctx_ufunc->uf_ret_type == &t_any))
2212 || (!check_return_type
2213 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
2214 {
2215 cctx->ctx_ufunc->uf_ret_type = stack_type;
2216 }
2217 else
2218 {
2219 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
2220 && stack_type->tt_type != VAR_VOID
2221 && stack_type->tt_type != VAR_UNKNOWN)
2222 {
2223 emsg(_(e_returning_value_in_function_without_return_type));
2224 return NULL;
2225 }
2226 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
2227 0, cctx, FALSE, FALSE) == FAIL)
2228 return NULL;
2229 }
2230 }
2231 }
2232 else
2233 {
2234 // "check_return_type" cannot be TRUE, only used for a lambda which
2235 // always has an argument.
2236 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
2237 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
2238 {
2239 emsg(_(e_missing_return_value));
2240 return NULL;
2241 }
2242
2243 // No argument, return zero.
2244 generate_PUSHNR(cctx, 0);
2245 }
2246
2247 // Undo any command modifiers.
2248 generate_undo_cmdmods(cctx);
2249
2250 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
2251 return NULL;
2252
2253 // "return val | endif" is possible
2254 return skipwhite(p);
2255}
2256
2257/*
2258 * Check if the separator for a :global or :substitute command is OK.
2259 */
2260 int
2261check_global_and_subst(char_u *cmd, char_u *arg)
2262{
2263 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
2264 {
2265 semsg(_(e_separator_not_supported_str), arg);
2266 return FAIL;
2267 }
2268 if (VIM_ISWHITE(cmd[1]))
2269 {
2270 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
2271 return FAIL;
2272 }
2273 return OK;
2274}
2275
2276
2277#endif // defined(FEAT_EVAL)