blob: f8ebfb12888d01cc1afb3dbdc2b464d4e11104d0 [file] [log] [blame]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9cmds.c: Dealing with commands of a compiled function
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
24/*
25 * Get the index of the current instruction.
26 * This compensates for a preceding ISN_CMDMOD and ISN_PROF_START.
27 */
28 static int
29current_instr_idx(cctx_T *cctx)
30{
31 garray_T *instr = &cctx->ctx_instr;
32 int idx = instr->ga_len;
33
34 while (idx > 0)
35 {
36 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
37 .isn_type == ISN_CMDMOD)
38 {
39 --idx;
40 continue;
41 }
42#ifdef FEAT_PROFILE
43 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
44 {
45 --idx;
46 continue;
47 }
48#endif
49 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
50 {
51 --idx;
52 continue;
53 }
54 break;
55 }
56 return idx;
57}
58/*
59 * Remove local variables above "new_top".
Bram Moolenaara275f2c2022-10-11 20:04:09 +010060 * Do this by clearing the name. If "keep" is TRUE do not reset the length, a
61 * closure may still need location of the variable.
Bram Moolenaardc7c3662021-12-20 15:04:29 +000062 */
63 static void
Bram Moolenaara275f2c2022-10-11 20:04:09 +010064unwind_locals(cctx_T *cctx, int new_top, int keep)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000065{
66 if (cctx->ctx_locals.ga_len > new_top)
Bram Moolenaara275f2c2022-10-11 20:04:09 +010067 for (int idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000068 {
Bram Moolenaara275f2c2022-10-11 20:04:09 +010069 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
70 VIM_CLEAR(lvar->lv_name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +000071 }
Bram Moolenaara275f2c2022-10-11 20:04:09 +010072 if (!keep)
73 cctx->ctx_locals.ga_len = new_top;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000074}
75
76/*
77 * Free all local variables.
78 */
79 void
80free_locals(cctx_T *cctx)
81{
Bram Moolenaara275f2c2022-10-11 20:04:09 +010082 unwind_locals(cctx, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +000083 ga_clear(&cctx->ctx_locals);
84}
85
86
87/*
88 * Check if "name" can be "unlet".
89 */
90 int
91check_vim9_unlet(char_u *name)
92{
Bram Moolenaardbdd16b2022-08-14 21:46:07 +010093 if (*name == NUL)
94 {
95 semsg(_(e_argument_required_for_str), "unlet");
96 return FAIL;
97 }
98
Bram Moolenaardc7c3662021-12-20 15:04:29 +000099 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
100 {
101 // "unlet s:var" is allowed in legacy script.
102 if (*name == 's' && !script_is_vim9())
103 return OK;
104 semsg(_(e_cannot_unlet_str), name);
105 return FAIL;
106 }
107 return OK;
108}
109
110/*
111 * Callback passed to ex_unletlock().
112 */
113 static int
114compile_unlet(
115 lval_T *lvp,
116 char_u *name_end,
117 exarg_T *eap,
118 int deep UNUSED,
119 void *coookie)
120{
121 cctx_T *cctx = coookie;
122 char_u *p = lvp->ll_name;
123 int cc = *name_end;
124 int ret = OK;
125
126 if (cctx->ctx_skip == SKIP_YES)
127 return OK;
128
129 *name_end = NUL;
130 if (*p == '$')
131 {
132 // :unlet $ENV_VAR
133 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
134 }
135 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
136 {
137 lhs_T lhs;
138
139 // This is similar to assigning: lookup the list/dict, compile the
140 // idx/key. Then instead of storing the value unlet the item.
141 // unlet {list}[idx]
142 // unlet {dict}[key] dict.key
143 //
144 // Figure out the LHS type and other properties.
145 //
Bram Moolenaar97f8c102022-04-02 19:43:57 +0100146 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, FALSE, 0, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000147
Bram Moolenaar2ef01d92022-01-06 21:38:11 +0000148 // Use the info in "lhs" to unlet the item at the index in the
149 // list or dict.
150 if (ret == OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000151 {
Bram Moolenaar2ef01d92022-01-06 21:38:11 +0000152 if (!lhs.lhs_has_index)
153 {
154 semsg(_(e_cannot_unlet_imported_item_str), p);
155 ret = FAIL;
156 }
157 else
158 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000159 }
160
161 vim_free(lhs.lhs_name);
162 }
163 else if (check_vim9_unlet(p) == FAIL)
164 {
165 ret = FAIL;
166 }
167 else
168 {
169 // Normal name. Only supports g:, w:, t: and b: namespaces.
170 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
171 }
172
173 *name_end = cc;
174 return ret;
175}
176
177/*
178 * Callback passed to ex_unletlock().
179 */
180 static int
181compile_lock_unlock(
182 lval_T *lvp,
183 char_u *name_end,
184 exarg_T *eap,
Bram Moolenaar70c43d82022-01-26 21:01:15 +0000185 int deep,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000186 void *coookie)
187{
188 cctx_T *cctx = coookie;
189 int cc = *name_end;
190 char_u *p = lvp->ll_name;
191 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000192 char_u *buf;
193 isntype_T isn = ISN_EXEC;
Bram Moolenaard1d8f6b2022-08-14 21:28:32 +0100194 char *cmd = eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar";
Ernie Raelee865f32023-09-29 19:53:55 +0200195 int is_arg = FALSE;
196
197#ifdef LOG_LOCKVAR
198 ch_log(NULL, "LKVAR: compile_lock_unlock(): cookie %p, name %s",
199 coookie, p);
200#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000201
202 if (cctx->ctx_skip == SKIP_YES)
203 return OK;
204
Bram Moolenaard1d8f6b2022-08-14 21:28:32 +0100205 if (*p == NUL)
206 {
207 semsg(_(e_argument_required_for_str), cmd);
208 return FAIL;
209 }
210
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000211 // Cannot use :lockvar and :unlockvar on local variables.
212 if (p[1] != ':')
213 {
214 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
Ernie Rael64885642023-10-04 20:16:22 +0200215
216 // The most important point is that something like
217 // name[idx].member... needs to be resolved at runtime, get_lval(),
218 // starting from the root "name".
219
220 // These checks are reminiscent of the variable_exists function.
221 // But most of the matches require special handling.
222
223 // If bare name is is locally accessible, except for local var,
Ernie Raelee865f32023-09-29 19:53:55 +0200224 // then put it on the stack to use with ISN_LOCKUNLOCK.
225 // This could be v.memb, v[idx_key]; bare class variable,
Ernie Rael64885642023-10-04 20:16:22 +0200226 // function arg. The item on the stack, will be passed
227 // to ex_lockvar() indirectly and be used as the root for get_lval.
228 // A bare script variable name needs no special handling.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000229
Ernie Raelee865f32023-09-29 19:53:55 +0200230 char_u *name = NULL;
231 int len = end - p;
232
233 if (lookup_local(p, len, NULL, cctx) == OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000234 {
Ernie Raelee865f32023-09-29 19:53:55 +0200235 // Handle "this", "this.val", "anyvar[idx]"
236 if (*end != '.' && *end != '['
237 && (len != 4 || STRNCMP("this", p, len) != 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000238 {
239 emsg(_(e_cannot_lock_unlock_local_variable));
240 return FAIL;
241 }
Ernie Raelee865f32023-09-29 19:53:55 +0200242 // Push the local on the stack, could be "this".
243 name = p;
244#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +0200245 ch_log(NULL, "LKVAR: ... lookup_local: name %s", name);
Ernie Raelee865f32023-09-29 19:53:55 +0200246#endif
247 }
248 if (name == NULL)
249 {
250 class_T *cl;
251 if (cctx_class_member_idx(cctx, p, len, &cl) >= 0)
252 {
253 if (*end != '.' && *end != '[')
254 {
255 // Push the class of the bare class variable name
256 name = cl->class_name;
Yegappan Lakshmananb8523052023-10-08 19:07:39 +0200257 len = (int)STRLEN(name);
Ernie Raelee865f32023-09-29 19:53:55 +0200258#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +0200259 ch_log(NULL, "LKVAR: ... cctx_class_member: name %s",
Ernie Raelee865f32023-09-29 19:53:55 +0200260 name);
261#endif
262 }
263 }
264 }
265 if (name == NULL)
266 {
Ernie Raelee865f32023-09-29 19:53:55 +0200267 // Can lockvar any function arg.
Ernie Rael64885642023-10-04 20:16:22 +0200268 if (arg_exists(p, len, NULL, NULL, NULL, cctx) == OK)
Ernie Raelee865f32023-09-29 19:53:55 +0200269 {
270 name = p;
271 is_arg = TRUE;
272#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +0200273 ch_log(NULL, "LKVAR: ... arg_exists: name %s", name);
274#endif
275 }
276 }
277 if (name == NULL)
278 {
279 // No special handling for a bare script variable; but
280 // if followed by '[' or '.', it's a root for get_lval().
281 if (script_var_exists(p, len, cctx, NULL) == OK
282 && (*end == '.' || *end == '['))
283 {
284 name = p;
285#ifdef LOG_LOCKVAR
286 ch_log(NULL, "LKVAR: ... script_var_exists: name %s", name);
Ernie Raelee865f32023-09-29 19:53:55 +0200287#endif
288 }
289 }
290 if (name != NULL)
291 {
292#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +0200293 ch_log(NULL, "LKVAR: ... INS_LOCKUNLOCK %s", name);
Ernie Raelee865f32023-09-29 19:53:55 +0200294#endif
295 if (compile_load(&name, name + len, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000296 return FAIL;
297 isn = ISN_LOCKUNLOCK;
298 }
299 }
300
301 // Checking is done at runtime.
302 *name_end = NUL;
Ernie Raelee865f32023-09-29 19:53:55 +0200303 size_t len = name_end - p + 20;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000304 buf = alloc(len);
305 if (buf == NULL)
306 ret = FAIL;
307 else
308 {
Bram Moolenaare939f5e2022-01-26 21:32:59 +0000309 if (deep < 0)
310 vim_snprintf((char *)buf, len, "%s! %s", cmd, p);
311 else
312 vim_snprintf((char *)buf, len, "%s %d %s", cmd, deep, p);
Ernie Raelee865f32023-09-29 19:53:55 +0200313#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +0200314 ch_log(NULL, "LKVAR: ... buf %s", buf);
Ernie Raelee865f32023-09-29 19:53:55 +0200315#endif
316 if (isn == ISN_LOCKUNLOCK)
317 ret = generate_LOCKUNLOCK(cctx, buf, is_arg);
318 else
319 ret = generate_EXEC_copy(cctx, isn, buf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000320
321 vim_free(buf);
322 *name_end = cc;
323 }
324 return ret;
325}
326
327/*
328 * compile "unlet var", "lock var" and "unlock var"
329 * "arg" points to "var".
330 */
331 char_u *
332compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
333{
Bram Moolenaar70c43d82022-01-26 21:01:15 +0000334 int deep = 0;
335 char_u *p = arg;
336
337 if (eap->cmdidx != CMD_unlet)
338 {
339 if (eap->forceit)
340 deep = -1;
341 else if (vim_isdigit(*p))
342 {
343 deep = getdigits(&p);
344 p = skipwhite(p);
345 }
346 else
347 deep = 2;
348 }
349
350 ex_unletlock(eap, p, deep, GLV_NO_AUTOLOAD | GLV_COMPILING,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000351 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
352 cctx);
353 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
354}
355
356/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100357 * Generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
358 * "funcref_idx" is used for JUMP_WHILE_FALSE
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000359 */
360 static int
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100361compile_jump_to_end(
362 endlabel_T **el,
363 jumpwhen_T when,
364 int funcref_idx,
365 cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000366{
367 garray_T *instr = &cctx->ctx_instr;
368 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
369
370 if (endlabel == NULL)
371 return FAIL;
372 endlabel->el_next = *el;
373 *el = endlabel;
374 endlabel->el_end_label = instr->ga_len;
375
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100376 if (when == JUMP_WHILE_FALSE)
377 generate_WHILE(cctx, funcref_idx);
378 else
379 generate_JUMP(cctx, when, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000380 return OK;
381}
382
383 static void
384compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
385{
386 garray_T *instr = &cctx->ctx_instr;
387
388 while (*el != NULL)
389 {
390 endlabel_T *cur = (*el);
391 isn_T *isn;
392
393 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
394 isn->isn_arg.jump.jump_where = jump_where;
395 *el = cur->el_next;
396 vim_free(cur);
397 }
398}
399
400 static void
401compile_free_jump_to_end(endlabel_T **el)
402{
403 while (*el != NULL)
404 {
405 endlabel_T *cur = (*el);
406
407 *el = cur->el_next;
408 vim_free(cur);
409 }
410}
411
412/*
413 * Create a new scope and set up the generic items.
414 */
415 static scope_T *
416new_scope(cctx_T *cctx, scopetype_T type)
417{
418 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
419
420 if (scope == NULL)
421 return NULL;
422 scope->se_outer = cctx->ctx_scope;
423 cctx->ctx_scope = scope;
424 scope->se_type = type;
425 scope->se_local_count = cctx->ctx_locals.ga_len;
Bram Moolenaarcc341812022-09-19 15:54:34 +0100426 if (scope->se_outer != NULL)
427 scope->se_loop_depth = scope->se_outer->se_loop_depth;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000428 return scope;
429}
430
431/*
432 * Free the current scope and go back to the outer scope.
433 */
434 void
435drop_scope(cctx_T *cctx)
436{
437 scope_T *scope = cctx->ctx_scope;
438
439 if (scope == NULL)
440 {
441 iemsg("calling drop_scope() without a scope");
442 return;
443 }
444 cctx->ctx_scope = scope->se_outer;
445 switch (scope->se_type)
446 {
447 case IF_SCOPE:
448 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
449 case FOR_SCOPE:
450 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
451 case WHILE_SCOPE:
452 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
453 case TRY_SCOPE:
454 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
455 case NO_SCOPE:
456 case BLOCK_SCOPE:
457 break;
458 }
459 vim_free(scope);
460}
461
462 static int
463misplaced_cmdmod(cctx_T *cctx)
464{
465 garray_T *instr = &cctx->ctx_instr;
466
467 if (cctx->ctx_has_cmdmod
468 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
469 == ISN_CMDMOD)
470 {
471 emsg(_(e_misplaced_command_modifier));
472 return TRUE;
473 }
474 return FALSE;
475}
476
477/*
478 * compile "if expr"
479 *
480 * "if expr" Produces instructions:
481 * EVAL expr Push result of "expr"
482 * JUMP_IF_FALSE end
483 * ... body ...
484 * end:
485 *
486 * "if expr | else" Produces instructions:
487 * EVAL expr Push result of "expr"
488 * JUMP_IF_FALSE else
489 * ... body ...
490 * JUMP_ALWAYS end
491 * else:
492 * ... body ...
493 * end:
494 *
495 * "if expr1 | elseif expr2 | else" Produces instructions:
496 * EVAL expr Push result of "expr"
497 * JUMP_IF_FALSE elseif
498 * ... body ...
499 * JUMP_ALWAYS end
500 * elseif:
501 * EVAL expr Push result of "expr"
502 * JUMP_IF_FALSE else
503 * ... body ...
504 * JUMP_ALWAYS end
505 * else:
506 * ... body ...
507 * end:
508 */
509 char_u *
510compile_if(char_u *arg, cctx_T *cctx)
511{
512 char_u *p = arg;
513 garray_T *instr = &cctx->ctx_instr;
514 int instr_count = instr->ga_len;
515 scope_T *scope;
516 skip_T skip_save = cctx->ctx_skip;
517 ppconst_T ppconst;
518
519 CLEAR_FIELD(ppconst);
520 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
521 {
522 clear_ppconst(&ppconst);
523 return NULL;
524 }
525 if (!ends_excmd2(arg, skipwhite(p)))
526 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000527 semsg(_(e_trailing_characters_str), p);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000528 return NULL;
529 }
530 if (cctx->ctx_skip == SKIP_YES)
531 clear_ppconst(&ppconst);
532 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
533 {
534 int error = FALSE;
535 int v;
536
537 // The expression results in a constant.
538 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
539 clear_ppconst(&ppconst);
540 if (error)
541 return NULL;
542 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
543 }
544 else
545 {
546 // Not a constant, generate instructions for the expression.
547 cctx->ctx_skip = SKIP_UNKNOWN;
548 if (generate_ppconst(cctx, &ppconst) == FAIL)
549 return NULL;
550 if (bool_on_stack(cctx) == FAIL)
551 return NULL;
552 }
553
554 // CMDMOD_REV must come before the jump
555 generate_undo_cmdmods(cctx);
556
557 scope = new_scope(cctx, IF_SCOPE);
558 if (scope == NULL)
559 return NULL;
560 scope->se_skip_save = skip_save;
561 // "is_had_return" will be reset if any block does not end in :return
562 scope->se_u.se_if.is_had_return = TRUE;
563
564 if (cctx->ctx_skip == SKIP_UNKNOWN)
565 {
566 // "where" is set when ":elseif", "else" or ":endif" is found
567 scope->se_u.se_if.is_if_label = instr->ga_len;
568 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
569 }
570 else
571 scope->se_u.se_if.is_if_label = -1;
572
573#ifdef FEAT_PROFILE
574 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
575 && skip_save != SKIP_YES)
576 {
577 // generated a profile start, need to generate a profile end, since it
578 // won't be done after returning
579 cctx->ctx_skip = SKIP_NOT;
580 generate_instr(cctx, ISN_PROF_END);
581 cctx->ctx_skip = SKIP_YES;
582 }
583#endif
584
585 return p;
586}
587
588 char_u *
589compile_elseif(char_u *arg, cctx_T *cctx)
590{
591 char_u *p = arg;
592 garray_T *instr = &cctx->ctx_instr;
593 int instr_count;
594 isn_T *isn;
595 scope_T *scope = cctx->ctx_scope;
596 ppconst_T ppconst;
597 skip_T save_skip = cctx->ctx_skip;
598
599 if (scope == NULL || scope->se_type != IF_SCOPE)
600 {
601 emsg(_(e_elseif_without_if));
602 return NULL;
603 }
Bram Moolenaara275f2c2022-10-11 20:04:09 +0100604 unwind_locals(cctx, scope->se_local_count, TRUE);
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +0100605 if (!cctx->ctx_had_return && !cctx->ctx_had_throw)
606 // the previous if block didn't end in a "return" or a "throw"
607 // statement.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000608 scope->se_u.se_if.is_had_return = FALSE;
609
610 if (cctx->ctx_skip == SKIP_NOT)
611 {
612 // previous block was executed, this one and following will not
613 cctx->ctx_skip = SKIP_YES;
614 scope->se_u.se_if.is_seen_skip_not = TRUE;
615 }
616 if (scope->se_u.se_if.is_seen_skip_not)
617 {
618 // A previous block was executed, skip over expression and bail out.
619 // Do not count the "elseif" for profiling and cmdmod
620 instr->ga_len = current_instr_idx(cctx);
621
622 skip_expr_cctx(&p, cctx);
623 return p;
624 }
625
626 if (cctx->ctx_skip == SKIP_UNKNOWN)
627 {
628 int moved_cmdmod = FALSE;
629 int saved_debug = FALSE;
630 isn_T debug_isn;
631
632 // Move any CMDMOD instruction to after the jump
633 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
634 {
635 if (GA_GROW_FAILS(instr, 1))
636 return NULL;
637 ((isn_T *)instr->ga_data)[instr->ga_len] =
638 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
639 --instr->ga_len;
640 moved_cmdmod = TRUE;
641 }
642
643 // Remove the already generated ISN_DEBUG, it is written below the
644 // ISN_FOR instruction.
645 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
646 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
647 .isn_type == ISN_DEBUG)
648 {
649 --instr->ga_len;
650 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
651 saved_debug = TRUE;
652 }
653
654 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100655 JUMP_ALWAYS, 0, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000656 return NULL;
657 // previous "if" or "elseif" jumps here
658 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
659 isn->isn_arg.jump.jump_where = instr->ga_len;
660
661 if (moved_cmdmod)
662 ++instr->ga_len;
663
664 if (saved_debug)
665 {
666 // move the debug instruction here
667 if (GA_GROW_FAILS(instr, 1))
668 return NULL;
669 ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
670 ++instr->ga_len;
671 }
672 }
673
674 // compile "expr"; if we know it evaluates to FALSE skip the block
675 CLEAR_FIELD(ppconst);
676 if (cctx->ctx_skip == SKIP_YES)
677 {
678 cctx->ctx_skip = SKIP_UNKNOWN;
679#ifdef FEAT_PROFILE
680 if (cctx->ctx_compile_type == CT_PROFILE)
681 // the previous block was skipped, need to profile this line
682 generate_instr(cctx, ISN_PROF_START);
683#endif
684 if (cctx->ctx_compile_type == CT_DEBUG)
685 // the previous block was skipped, may want to debug this line
686 generate_instr_debug(cctx);
687 }
688
689 instr_count = instr->ga_len;
690 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
691 {
692 clear_ppconst(&ppconst);
693 return NULL;
694 }
695 cctx->ctx_skip = save_skip;
696 if (!ends_excmd2(arg, skipwhite(p)))
697 {
698 clear_ppconst(&ppconst);
Bram Moolenaar74409f62022-01-01 15:58:22 +0000699 semsg(_(e_trailing_characters_str), p);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000700 return NULL;
701 }
702 if (scope->se_skip_save == SKIP_YES)
703 clear_ppconst(&ppconst);
704 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
705 {
706 int error = FALSE;
707 int v;
708
709 // The expression result is a constant.
710 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
711 if (error)
712 {
713 clear_ppconst(&ppconst);
714 return NULL;
715 }
716 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
717 clear_ppconst(&ppconst);
718 scope->se_u.se_if.is_if_label = -1;
719 }
720 else
721 {
722 // Not a constant, generate instructions for the expression.
723 cctx->ctx_skip = SKIP_UNKNOWN;
724 if (generate_ppconst(cctx, &ppconst) == FAIL)
725 return NULL;
726 if (bool_on_stack(cctx) == FAIL)
727 return NULL;
728
729 // CMDMOD_REV must come before the jump
730 generate_undo_cmdmods(cctx);
731
732 // "where" is set when ":elseif", "else" or ":endif" is found
733 scope->se_u.se_if.is_if_label = instr->ga_len;
734 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
735 }
736
737 return p;
738}
739
740 char_u *
741compile_else(char_u *arg, cctx_T *cctx)
742{
743 char_u *p = arg;
744 garray_T *instr = &cctx->ctx_instr;
745 isn_T *isn;
746 scope_T *scope = cctx->ctx_scope;
747
748 if (scope == NULL || scope->se_type != IF_SCOPE)
749 {
750 emsg(_(e_else_without_if));
751 return NULL;
752 }
Bram Moolenaara275f2c2022-10-11 20:04:09 +0100753 unwind_locals(cctx, scope->se_local_count, TRUE);
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +0100754 if (!cctx->ctx_had_return && !cctx->ctx_had_throw)
755 // the previous if block didn't end in a "return" or a "throw"
756 // statement.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000757 scope->se_u.se_if.is_had_return = FALSE;
758 scope->se_u.se_if.is_seen_else = TRUE;
759
760#ifdef FEAT_PROFILE
761 if (cctx->ctx_compile_type == CT_PROFILE)
762 {
763 if (cctx->ctx_skip == SKIP_NOT
764 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
765 .isn_type == ISN_PROF_START)
766 // the previous block was executed, do not count "else" for
767 // profiling
768 --instr->ga_len;
769 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
770 {
771 // the previous block was not executed, this one will, do count the
772 // "else" for profiling
773 cctx->ctx_skip = SKIP_NOT;
774 generate_instr(cctx, ISN_PROF_END);
775 generate_instr(cctx, ISN_PROF_START);
776 cctx->ctx_skip = SKIP_YES;
777 }
778 }
779#endif
780
781 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
782 {
783 // jump from previous block to the end, unless the else block is empty
784 if (cctx->ctx_skip == SKIP_UNKNOWN)
785 {
786 if (!cctx->ctx_had_return
787 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100788 JUMP_ALWAYS, 0, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000789 return NULL;
790 }
791
792 if (cctx->ctx_skip == SKIP_UNKNOWN)
793 {
794 if (scope->se_u.se_if.is_if_label >= 0)
795 {
796 // previous "if" or "elseif" jumps here
797 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
798 isn->isn_arg.jump.jump_where = instr->ga_len;
799 scope->se_u.se_if.is_if_label = -1;
800 }
801 }
802
803 if (cctx->ctx_skip != SKIP_UNKNOWN)
804 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
805 }
806
807 return p;
808}
809
810 char_u *
811compile_endif(char_u *arg, cctx_T *cctx)
812{
813 scope_T *scope = cctx->ctx_scope;
814 ifscope_T *ifscope;
815 garray_T *instr = &cctx->ctx_instr;
816 isn_T *isn;
817
818 if (misplaced_cmdmod(cctx))
819 return NULL;
820
821 if (scope == NULL || scope->se_type != IF_SCOPE)
822 {
823 emsg(_(e_endif_without_if));
824 return NULL;
825 }
826 ifscope = &scope->se_u.se_if;
Bram Moolenaara275f2c2022-10-11 20:04:09 +0100827 unwind_locals(cctx, scope->se_local_count, TRUE);
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +0100828 if (!cctx->ctx_had_return && !cctx->ctx_had_throw)
829 // the previous if block didn't end in a "return" or a "throw"
830 // statement.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000831 ifscope->is_had_return = FALSE;
832
833 if (scope->se_u.se_if.is_if_label >= 0)
834 {
835 // previous "if" or "elseif" jumps here
836 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
837 isn->isn_arg.jump.jump_where = instr->ga_len;
838 }
839 // Fill in the "end" label in jumps at the end of the blocks.
840 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
841
842#ifdef FEAT_PROFILE
843 // even when skipping we count the endif as executed, unless the block it's
844 // in is skipped
845 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
846 && scope->se_skip_save != SKIP_YES)
847 {
848 cctx->ctx_skip = SKIP_NOT;
849 generate_instr(cctx, ISN_PROF_START);
850 }
851#endif
852 cctx->ctx_skip = scope->se_skip_save;
853
854 // If all the blocks end in :return and there is an :else then the
855 // had_return flag is set.
856 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
857
858 drop_scope(cctx);
859 return arg;
860}
861
862/*
Bram Moolenaar8abb5842022-09-17 12:39:58 +0100863 * Save the info needed for ENDLOOP. Used by :for and :while.
864 */
865 static void
866compile_fill_loop_info(loop_info_T *loop_info, int funcref_idx, cctx_T *cctx)
867{
868 loop_info->li_funcref_idx = funcref_idx;
869 loop_info->li_local_count = cctx->ctx_locals.ga_len;
870 loop_info->li_closure_count = cctx->ctx_closure_count;
871}
872
873/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000874 * Compile "for var in expr":
875 *
876 * Produces instructions:
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100877 * STORE -1 in loop-idx Set index to -1
878 * EVAL expr Result of "expr" on top of stack
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000879 * top: FOR loop-idx, end Increment index, use list on bottom of stack
880 * - if beyond end, jump to "end"
881 * - otherwise get item from list and push it
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100882 * - store ec_funcrefs in var "loop-idx" + 1
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000883 * STORE var Store item in "var"
884 * ... body ...
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100885 * ENDLOOP funcref-idx off count Only if closure uses local var
886 * JUMP top Jump back to repeat
887 * end: DROP Drop the result of "expr"
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000888 *
889 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
890 * UNPACK 2 Split item in 2
891 * STORE var1 Store item in "var1"
892 * STORE var2 Store item in "var2"
893 */
894 char_u *
895compile_for(char_u *arg_start, cctx_T *cctx)
896{
897 char_u *arg;
898 char_u *arg_end;
899 char_u *name = NULL;
900 char_u *p;
901 char_u *wp;
902 int var_count = 0;
903 int var_list = FALSE;
904 int semicolon = FALSE;
905 size_t varlen;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000906 garray_T *instr = &cctx->ctx_instr;
907 scope_T *scope;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100908 forscope_T *forscope;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000909 lvar_T *loop_lvar; // loop iteration variable
Bram Moolenaarcc341812022-09-19 15:54:34 +0100910 int loop_lvar_idx;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100911 lvar_T *funcref_lvar;
Bram Moolenaarcc341812022-09-19 15:54:34 +0100912 int funcref_lvar_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000913 lvar_T *var_lvar; // variable for "var"
914 type_T *vartype;
915 type_T *item_type = &t_any;
916 int idx;
917 int prev_lnum = cctx->ctx_prev_lnum;
918
919 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
920 if (p == NULL)
921 return NULL;
922 if (var_count == 0)
923 var_count = 1;
924 else
925 var_list = TRUE; // can also be a list of one variable
926
927 // consume "in"
928 wp = p;
929 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
930 return NULL;
931 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
932 {
933 if (*p == ':' && wp != p)
934 semsg(_(e_no_white_space_allowed_before_colon_str), p);
935 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +0000936 emsg(_(e_missing_in_after_for));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000937 return NULL;
938 }
939 wp = p + 2;
940 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
941 return NULL;
942
Bram Moolenaar2b4ecc22022-01-02 14:08:18 +0000943 // Find the already generated ISN_DEBUG to get the line number for the
944 // instruction written below the ISN_FOR instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000945 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
946 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
947 .isn_type == ISN_DEBUG)
948 {
Bram Moolenaar2b4ecc22022-01-02 14:08:18 +0000949 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000950 .isn_arg.debug.dbg_break_lnum;
951 }
952
953 scope = new_scope(cctx, FOR_SCOPE);
954 if (scope == NULL)
955 return NULL;
Bram Moolenaarcc341812022-09-19 15:54:34 +0100956 if (scope->se_loop_depth == MAX_LOOP_DEPTH)
957 {
958 emsg(_(e_loop_nesting_too_deep));
959 return NULL;
960 }
961 ++scope->se_loop_depth;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100962 forscope = &scope->se_u.se_for;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000963
964 // Reserve a variable to store the loop iteration counter and initialize it
965 // to -1.
Bram Moolenaar6586a012022-09-30 11:04:50 +0100966 loop_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000967 if (loop_lvar == NULL)
968 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000969 drop_scope(cctx);
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100970 return NULL; // out of memory
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000971 }
Bram Moolenaarcc341812022-09-19 15:54:34 +0100972 // get the index before a following reserve_local() makes the lval invalid
973 loop_lvar_idx = loop_lvar->lv_idx;
974 generate_STORENR(cctx, loop_lvar_idx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000975
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100976 // Reserve a variable to store ec_funcrefs.ga_len, used in ISN_ENDLOOP.
977 // The variable index is always the loop var index plus one.
978 // It is not used when no closures are encountered, we don't know yet.
Bram Moolenaar6586a012022-09-30 11:04:50 +0100979 funcref_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number);
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100980 if (funcref_lvar == NULL)
981 {
982 drop_scope(cctx);
983 return NULL; // out of memory
984 }
Bram Moolenaarcc341812022-09-19 15:54:34 +0100985 // get the index before a following reserve_local() makes the lval invalid
986 funcref_lvar_idx = funcref_lvar->lv_idx;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100987
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000988 // compile "expr", it remains on the stack until "endfor"
989 arg = p;
990 if (compile_expr0(&arg, cctx) == FAIL)
991 {
992 drop_scope(cctx);
993 return NULL;
994 }
995 arg_end = arg;
996
997 if (cctx->ctx_skip != SKIP_YES)
998 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000999 // If we know the type of "var" and it is not a supported type we can
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001000 // give an error now.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001001 vartype = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001002 if (vartype->tt_type != VAR_LIST
1003 && vartype->tt_type != VAR_STRING
1004 && vartype->tt_type != VAR_BLOB
1005 && vartype->tt_type != VAR_ANY
1006 && vartype->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001007 {
1008 semsg(_(e_for_loop_on_str_not_supported),
1009 vartype_name(vartype->tt_type));
1010 drop_scope(cctx);
1011 return NULL;
1012 }
1013
1014 if (vartype->tt_type == VAR_STRING)
1015 item_type = &t_string;
1016 else if (vartype->tt_type == VAR_BLOB)
1017 item_type = &t_number;
1018 else if (vartype->tt_type == VAR_LIST
1019 && vartype->tt_member->tt_type != VAR_ANY)
1020 {
1021 if (!var_list)
1022 item_type = vartype->tt_member;
1023 else if (vartype->tt_member->tt_type == VAR_LIST
1024 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
1025 item_type = vartype->tt_member->tt_member;
1026 }
1027
1028 // CMDMOD_REV must come before the FOR instruction.
1029 generate_undo_cmdmods(cctx);
1030
1031 // "for_end" is set when ":endfor" is found
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001032 forscope->fs_top_label = current_instr_idx(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001033
Bram Moolenaar2b4ecc22022-01-02 14:08:18 +00001034 if (cctx->ctx_compile_type == CT_DEBUG)
1035 {
1036 int save_prev_lnum = cctx->ctx_prev_lnum;
1037 isn_T *isn;
1038
1039 // Add ISN_DEBUG here, before deciding to end the loop. There will
1040 // be another ISN_DEBUG before the next instruction.
1041 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
1042 // Increment the variable count so that the loop variable can be
1043 // inspected.
1044 cctx->ctx_prev_lnum = prev_lnum;
1045 isn = generate_instr_debug(cctx);
1046 ++isn->isn_arg.debug.dbg_var_names_len;
1047 cctx->ctx_prev_lnum = save_prev_lnum;
1048 }
1049
Bram Moolenaarcc341812022-09-19 15:54:34 +01001050 generate_FOR(cctx, loop_lvar_idx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001051
1052 arg = arg_start;
1053 if (var_list)
1054 {
1055 generate_UNPACK(cctx, var_count, semicolon);
1056 arg = skipwhite(arg + 1); // skip white after '['
1057
Bram Moolenaar078a4612022-01-04 15:17:03 +00001058 // drop the list item
1059 --cctx->ctx_type_stack.ga_len;
1060
1061 // add type of the items
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001062 for (idx = 0; idx < var_count; ++idx)
1063 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001064 type_T *type = (semicolon && idx == 0) ? vartype : item_type;
1065
1066 if (push_type_stack(cctx, type) == FAIL)
1067 {
1068 drop_scope(cctx);
1069 return NULL;
1070 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001071 }
1072 }
1073
1074 for (idx = 0; idx < var_count; ++idx)
1075 {
1076 assign_dest_T dest = dest_local;
1077 int opt_flags = 0;
1078 int vimvaridx = -1;
1079 type_T *type = &t_any;
1080 type_T *lhs_type = &t_any;
1081 where_T where = WHERE_INIT;
1082
1083 p = skip_var_one(arg, FALSE);
1084 varlen = p - arg;
1085 name = vim_strnsave(arg, varlen);
1086 if (name == NULL)
1087 goto failed;
Bram Moolenaarce93d162023-01-30 21:12:34 +00001088 if (*skipwhite(p) == ':')
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001089 {
Bram Moolenaarce93d162023-01-30 21:12:34 +00001090 if (VIM_ISWHITE(*p))
1091 {
1092 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1093 goto failed;
1094 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001095 p = skipwhite(p + 1);
1096 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
Yegappan Lakshmanan062bb6b2023-12-16 14:46:40 +01001097 if (lhs_type == NULL)
1098 goto failed;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001099 }
1100
1101 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
1102 &vimvaridx, &type, cctx) == FAIL)
1103 goto failed;
1104 if (dest != dest_local)
1105 {
1106 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00001107 type, name, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001108 goto failed;
1109 }
1110 else if (varlen == 1 && *arg == '_')
1111 {
1112 // Assigning to "_": drop the value.
1113 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
1114 goto failed;
1115 }
1116 else
1117 {
1118 // Script var is not supported.
1119 if (STRNCMP(name, "s:", 2) == 0)
1120 {
1121 emsg(_(e_cannot_use_script_variable_in_for_loop));
1122 goto failed;
1123 }
1124
1125 if (!valid_varname(arg, (int)varlen, FALSE))
1126 goto failed;
1127 if (lookup_local(arg, varlen, NULL, cctx) == OK)
1128 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001129 semsg(_(e_variable_already_declared_str), arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001130 goto failed;
1131 }
1132
1133 // Reserve a variable to store "var".
LemonBoyc5d27442023-08-19 13:02:35 +02001134 if (var_list)
1135 {
1136 where.wt_index = idx + 1;
1137 where.wt_kind = WT_VARIABLE;
1138 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001139 if (lhs_type == &t_any)
1140 lhs_type = item_type;
1141 else if (item_type != &t_unknown
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001142 && need_type_where(item_type, lhs_type, FALSE, -1,
Bram Moolenaara1c51952022-02-02 16:20:26 +00001143 where, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001144 goto failed;
Bram Moolenaar159b2d52022-10-11 21:41:25 +01001145 var_lvar = reserve_local(cctx, arg, varlen, ASSIGN_FINAL,
Bram Moolenaar6586a012022-09-30 11:04:50 +01001146 lhs_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001147 if (var_lvar == NULL)
1148 // out of memory or used as an argument
1149 goto failed;
1150
1151 if (semicolon && idx == var_count - 1)
1152 var_lvar->lv_type = vartype;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001153 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
1154 }
1155
1156 if (*p == ',' || *p == ';')
1157 ++p;
1158 arg = skipwhite(p);
1159 vim_free(name);
1160 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001161
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001162 // remember the number of variables and closures, used for ENDLOOP
Bram Moolenaarcc341812022-09-19 15:54:34 +01001163 compile_fill_loop_info(&forscope->fs_loop_info, funcref_lvar_idx, cctx);
1164 forscope->fs_loop_info.li_depth = scope->se_loop_depth - 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001165 }
1166
1167 return arg_end;
1168
1169failed:
1170 vim_free(name);
1171 drop_scope(cctx);
1172 return NULL;
1173}
1174
1175/*
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001176 * Used when ending a loop of :for and :while: Generate an ISN_ENDLOOP
1177 * instruction if any variable was declared that could be used by a new
1178 * closure.
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001179 */
1180 static int
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001181compile_loop_end(loop_info_T *loop_info, cctx_T *cctx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001182{
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001183 if (cctx->ctx_locals.ga_len > loop_info->li_local_count
1184 && cctx->ctx_closure_count > loop_info->li_closure_count)
Bram Moolenaarcc341812022-09-19 15:54:34 +01001185 return generate_ENDLOOP(cctx, loop_info);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001186 return OK;
1187}
1188
1189/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001190 * compile "endfor"
1191 */
1192 char_u *
1193compile_endfor(char_u *arg, cctx_T *cctx)
1194{
1195 garray_T *instr = &cctx->ctx_instr;
1196 scope_T *scope = cctx->ctx_scope;
1197 forscope_T *forscope;
1198 isn_T *isn;
1199
1200 if (misplaced_cmdmod(cctx))
1201 return NULL;
1202
1203 if (scope == NULL || scope->se_type != FOR_SCOPE)
1204 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001205 emsg(_(e_endfor_without_for));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001206 return NULL;
1207 }
1208 forscope = &scope->se_u.se_for;
1209 cctx->ctx_scope = scope->se_outer;
1210 if (cctx->ctx_skip != SKIP_YES)
1211 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001212 // Handle the case that any local variables were declared that might be
1213 // used in a closure.
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001214 if (compile_loop_end(&forscope->fs_loop_info, cctx) == FAIL)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001215 return NULL;
1216
Bram Moolenaara275f2c2022-10-11 20:04:09 +01001217 unwind_locals(cctx, scope->se_local_count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001218
1219 // At end of ":for" scope jump back to the FOR instruction.
1220 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
1221
1222 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar2b4ecc22022-01-02 14:08:18 +00001223 // In debug mode an ISN_DEBUG was inserted.
1224 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label
1225 + (cctx->ctx_compile_type == CT_DEBUG ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001226 isn->isn_arg.forloop.for_end = instr->ga_len;
1227
1228 // Fill in the "end" label any BREAK statements
1229 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
1230
1231 // Below the ":for" scope drop the "expr" list from the stack.
1232 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
1233 return NULL;
1234 }
1235
1236 vim_free(scope);
1237
1238 return arg;
1239}
1240
1241/*
1242 * compile "while expr"
1243 *
1244 * Produces instructions:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001245 * top: EVAL expr Push result of "expr"
1246 * WHILE funcref-idx end Jump if false
1247 * ... body ...
1248 * ENDLOOP funcref-idx off count only if closure uses local var
1249 * JUMP top Jump back to repeat
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001250 * end:
1251 *
1252 */
1253 char_u *
1254compile_while(char_u *arg, cctx_T *cctx)
1255{
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001256 char_u *p = arg;
1257 scope_T *scope;
1258 whilescope_T *whilescope;
1259 lvar_T *funcref_lvar;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001260 int funcref_lvar_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001261
1262 scope = new_scope(cctx, WHILE_SCOPE);
1263 if (scope == NULL)
1264 return NULL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001265 if (scope->se_loop_depth == MAX_LOOP_DEPTH)
1266 {
1267 emsg(_(e_loop_nesting_too_deep));
1268 return NULL;
1269 }
1270 ++scope->se_loop_depth;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001271 whilescope = &scope->se_u.se_while;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001272
1273 // "endwhile" jumps back here, one before when profiling or using cmdmods
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001274 whilescope->ws_top_label = current_instr_idx(cctx);
1275
1276 // Reserve a variable to store ec_funcrefs.ga_len, used in ISN_ENDLOOP.
1277 // It is not used when no closures are encountered, we don't know yet.
Bram Moolenaar6586a012022-09-30 11:04:50 +01001278 funcref_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001279 if (funcref_lvar == NULL)
1280 {
1281 drop_scope(cctx);
1282 return NULL; // out of memory
1283 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001284 // get the index before a following reserve_local() makes the lval invalid
1285 funcref_lvar_idx = funcref_lvar->lv_idx;
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001286
1287 // remember the number of variables and closures, used for ENDLOOP
Bram Moolenaarcc341812022-09-19 15:54:34 +01001288 compile_fill_loop_info(&whilescope->ws_loop_info, funcref_lvar_idx, cctx);
1289 whilescope->ws_loop_info.li_depth = scope->se_loop_depth - 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001290
1291 // compile "expr"
1292 if (compile_expr0(&p, cctx) == FAIL)
1293 return NULL;
1294
1295 if (!ends_excmd2(arg, skipwhite(p)))
1296 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001297 semsg(_(e_trailing_characters_str), p);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001298 return NULL;
1299 }
1300
1301 if (cctx->ctx_skip != SKIP_YES)
1302 {
1303 if (bool_on_stack(cctx) == FAIL)
1304 return FAIL;
1305
1306 // CMDMOD_REV must come before the jump
1307 generate_undo_cmdmods(cctx);
1308
1309 // "while_end" is set when ":endwhile" is found
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001310 if (compile_jump_to_end(&whilescope->ws_end_label,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001311 JUMP_WHILE_FALSE, funcref_lvar_idx, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001312 return FAIL;
1313 }
1314
1315 return p;
1316}
1317
1318/*
1319 * compile "endwhile"
1320 */
1321 char_u *
1322compile_endwhile(char_u *arg, cctx_T *cctx)
1323{
1324 scope_T *scope = cctx->ctx_scope;
1325 garray_T *instr = &cctx->ctx_instr;
1326
1327 if (misplaced_cmdmod(cctx))
1328 return NULL;
1329 if (scope == NULL || scope->se_type != WHILE_SCOPE)
1330 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001331 emsg(_(e_endwhile_without_while));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001332 return NULL;
1333 }
1334 cctx->ctx_scope = scope->se_outer;
1335 if (cctx->ctx_skip != SKIP_YES)
1336 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001337 whilescope_T *whilescope = &scope->se_u.se_while;
1338
1339 // Handle the case that any local variables were declared that might be
1340 // used in a closure.
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001341 if (compile_loop_end(&whilescope->ws_loop_info, cctx) == FAIL)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001342 return NULL;
1343
Bram Moolenaara275f2c2022-10-11 20:04:09 +01001344 unwind_locals(cctx, scope->se_local_count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001345
1346#ifdef FEAT_PROFILE
1347 // count the endwhile before jumping
1348 may_generate_prof_end(cctx, cctx->ctx_lnum);
1349#endif
1350
1351 // At end of ":for" scope jump back to the FOR instruction.
1352 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
1353
1354 // Fill in the "end" label in the WHILE statement so it can jump here.
1355 // And in any jumps for ":break"
1356 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
1357 instr->ga_len, cctx);
1358 }
1359
1360 vim_free(scope);
1361
1362 return arg;
1363}
1364
1365/*
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001366 * Get the current information about variables declared inside a loop.
Bram Moolenaarcc341812022-09-19 15:54:34 +01001367 * Returns TRUE if there are any and fills "lvi".
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001368 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01001369 int
1370get_loop_var_info(cctx_T *cctx, loopvarinfo_T *lvi)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001371{
1372 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001373 int prev_local_count = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001374
Bram Moolenaarcc341812022-09-19 15:54:34 +01001375 CLEAR_POINTER(lvi);
1376 for (;;)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001377 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001378 loop_info_T *loopinfo;
1379 int cur_local_last;
1380 int start_local_count;
1381
1382 while (scope != NULL && scope->se_type != WHILE_SCOPE
1383 && scope->se_type != FOR_SCOPE)
1384 scope = scope->se_outer;
1385 if (scope == NULL)
1386 break;
1387
1388 if (scope->se_type == WHILE_SCOPE)
1389 {
1390 loopinfo = &scope->se_u.se_while.ws_loop_info;
1391 // :while reserves one variable for funcref count
1392 cur_local_last = loopinfo->li_local_count - 1;
1393 }
1394 else
1395 {
1396 loopinfo = &scope->se_u.se_for.fs_loop_info;
1397 // :for reserves three variable: loop count, funcref count and loop
1398 // var
1399 cur_local_last = loopinfo->li_local_count - 3;
1400 }
1401
1402 start_local_count = loopinfo->li_local_count;
1403 if (cctx->ctx_locals.ga_len > start_local_count)
1404 {
1405 lvi->lvi_loop[loopinfo->li_depth].var_idx =
1406 (short)start_local_count;
1407 lvi->lvi_loop[loopinfo->li_depth].var_count =
1408 (short)(cctx->ctx_locals.ga_len - start_local_count
1409 - prev_local_count);
1410 if (lvi->lvi_depth == 0)
1411 lvi->lvi_depth = loopinfo->li_depth + 1;
1412 }
1413
1414 scope = scope->se_outer;
1415 prev_local_count = cctx->ctx_locals.ga_len - cur_local_last;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001416 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001417 return lvi->lvi_depth > 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001418}
1419
1420/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01001421 * Get the index of the variable "idx" in a loop, if any.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001422 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01001423 void
1424get_loop_var_idx(cctx_T *cctx, int idx, lvar_T *lvar)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001425{
Bram Moolenaarcc341812022-09-19 15:54:34 +01001426 loopvarinfo_T lvi;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001427
Bram Moolenaarcc341812022-09-19 15:54:34 +01001428 lvar->lv_loop_depth = -1;
1429 lvar->lv_loop_idx = -1;
1430 if (get_loop_var_info(cctx, &lvi))
1431 {
1432 int depth;
1433
1434 for (depth = lvi.lvi_depth - 1; depth >= 0; --depth)
1435 if (idx >= lvi.lvi_loop[depth].var_idx
1436 && idx < lvi.lvi_loop[depth].var_idx
1437 + lvi.lvi_loop[depth].var_count)
1438 {
1439 lvar->lv_loop_depth = depth;
1440 lvar->lv_loop_idx = lvi.lvi_loop[depth].var_idx;
1441 return;
1442 }
1443 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001444}
1445
1446/*
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001447 * Common for :break, :continue and :return
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001448 */
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001449 static int
1450compile_find_scope(
1451 int *loop_label, // where to jump to or NULL
1452 endlabel_T ***el, // end label or NULL
1453 int *try_scopes, // :try scopes encountered or NULL
1454 char *error, // error to use when no scope found
1455 cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001456{
1457 scope_T *scope = cctx->ctx_scope;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001458
1459 for (;;)
1460 {
1461 if (scope == NULL)
1462 {
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001463 if (error != NULL)
1464 emsg(_(error));
1465 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001466 }
1467 if (scope->se_type == FOR_SCOPE)
1468 {
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001469 if (compile_loop_end(&scope->se_u.se_for.fs_loop_info, cctx)
1470 == FAIL)
1471 return FAIL;
1472 if (loop_label != NULL)
1473 *loop_label = scope->se_u.se_for.fs_top_label;
1474 if (el != NULL)
1475 *el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001476 break;
1477 }
1478 if (scope->se_type == WHILE_SCOPE)
1479 {
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001480 if (compile_loop_end(&scope->se_u.se_while.ws_loop_info, cctx)
1481 == FAIL)
1482 return FAIL;
1483 if (loop_label != NULL)
1484 *loop_label = scope->se_u.se_while.ws_top_label;
1485 if (el != NULL)
1486 *el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001487 break;
1488 }
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001489 if (try_scopes != NULL && scope->se_type == TRY_SCOPE)
1490 ++*try_scopes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001491 scope = scope->se_outer;
1492 }
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001493 return OK;
1494}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001495
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001496/*
1497 * compile "continue"
1498 */
1499 char_u *
1500compile_continue(char_u *arg, cctx_T *cctx)
1501{
1502 int try_scopes = 0;
1503 int loop_label;
1504
1505 if (compile_find_scope(&loop_label, NULL, &try_scopes,
1506 e_continue_without_while_or_for, cctx) == FAIL)
1507 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001508 if (try_scopes > 0)
1509 // Inside one or more try/catch blocks we first need to jump to the
1510 // "finally" or "endtry" to cleanup.
1511 generate_TRYCONT(cctx, try_scopes, loop_label);
1512 else
1513 // Jump back to the FOR or WHILE instruction.
1514 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
1515
1516 return arg;
1517}
1518
1519/*
1520 * compile "break"
1521 */
1522 char_u *
1523compile_break(char_u *arg, cctx_T *cctx)
1524{
Bram Moolenaar873f8242022-03-10 21:53:44 +00001525 int try_scopes = 0;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001526 endlabel_T **el;
1527
Bram Moolenaar8abb5842022-09-17 12:39:58 +01001528 if (compile_find_scope(NULL, &el, &try_scopes,
1529 e_break_without_while_or_for, cctx) == FAIL)
1530 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001531
Bram Moolenaar3f45d672023-02-27 22:06:51 +00001532 if (cctx->ctx_skip == SKIP_YES)
1533 return arg;
1534
Bram Moolenaar873f8242022-03-10 21:53:44 +00001535 if (try_scopes > 0)
1536 // Inside one or more try/catch blocks we first need to jump to the
1537 // "finally" or "endtry" to cleanup. Then come to the next JUMP
dundargocc57b5bc2022-11-02 13:30:51 +00001538 // instruction, which we don't know the index of yet.
Bram Moolenaar873f8242022-03-10 21:53:44 +00001539 generate_TRYCONT(cctx, try_scopes, cctx->ctx_instr.ga_len + 1);
1540
1541 // Jump to the end of the FOR or WHILE loop. The instruction index will be
1542 // filled in later.
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001543 if (compile_jump_to_end(el, JUMP_ALWAYS, 0, cctx) == FAIL)
Bram Moolenaar3f45d672023-02-27 22:06:51 +00001544 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001545
1546 return arg;
1547}
1548
1549/*
1550 * compile "{" start of block
1551 */
1552 char_u *
1553compile_block(char_u *arg, cctx_T *cctx)
1554{
1555 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
1556 return NULL;
1557 return skipwhite(arg + 1);
1558}
1559
1560/*
1561 * compile end of block: drop one scope
1562 */
1563 void
1564compile_endblock(cctx_T *cctx)
1565{
1566 scope_T *scope = cctx->ctx_scope;
1567
1568 cctx->ctx_scope = scope->se_outer;
Bram Moolenaara275f2c2022-10-11 20:04:09 +01001569 unwind_locals(cctx, scope->se_local_count, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001570 vim_free(scope);
1571}
1572
1573/*
1574 * Compile "try".
1575 * Creates a new scope for the try-endtry, pointing to the first catch and
1576 * finally.
1577 * Creates another scope for the "try" block itself.
1578 * TRY instruction sets up exception handling at runtime.
1579 *
1580 * "try"
1581 * TRY -> catch1, -> finally push trystack entry
1582 * ... try block
1583 * "throw {exception}"
1584 * EVAL {exception}
1585 * THROW create exception
1586 * ... try block
1587 * " catch {expr}"
1588 * JUMP -> finally
1589 * catch1: PUSH exception
1590 * EVAL {expr}
1591 * MATCH
1592 * JUMP nomatch -> catch2
1593 * CATCH remove exception
1594 * ... catch block
1595 * " catch"
1596 * JUMP -> finally
1597 * catch2: CATCH remove exception
1598 * ... catch block
1599 * " finally"
1600 * finally:
1601 * ... finally block
1602 * " endtry"
1603 * ENDTRY pop trystack entry, may rethrow
1604 */
1605 char_u *
1606compile_try(char_u *arg, cctx_T *cctx)
1607{
1608 garray_T *instr = &cctx->ctx_instr;
1609 scope_T *try_scope;
1610 scope_T *scope;
1611
1612 if (misplaced_cmdmod(cctx))
1613 return NULL;
1614
1615 // scope that holds the jumps that go to catch/finally/endtry
1616 try_scope = new_scope(cctx, TRY_SCOPE);
1617 if (try_scope == NULL)
1618 return NULL;
1619
1620 if (cctx->ctx_skip != SKIP_YES)
1621 {
1622 isn_T *isn;
1623
1624 // "try_catch" is set when the first ":catch" is found or when no catch
1625 // is found and ":finally" is found.
1626 // "try_finally" is set when ":finally" is found
1627 // "try_endtry" is set when ":endtry" is found
1628 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
1629 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
1630 return NULL;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001631 isn->isn_arg.tryref.try_ref = ALLOC_CLEAR_ONE(tryref_T);
1632 if (isn->isn_arg.tryref.try_ref == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001633 return NULL;
1634 }
1635
1636 // scope for the try block itself
1637 scope = new_scope(cctx, BLOCK_SCOPE);
1638 if (scope == NULL)
1639 return NULL;
1640
1641 return arg;
1642}
1643
1644/*
1645 * Compile "catch {expr}".
1646 */
1647 char_u *
Dominique Pellé0268ff32024-07-28 21:12:20 +02001648compile_catch(char_u *arg, cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001649{
1650 scope_T *scope = cctx->ctx_scope;
1651 garray_T *instr = &cctx->ctx_instr;
1652 char_u *p;
1653 isn_T *isn;
1654
1655 if (misplaced_cmdmod(cctx))
1656 return NULL;
1657
1658 // end block scope from :try or :catch
1659 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1660 compile_endblock(cctx);
1661 scope = cctx->ctx_scope;
1662
1663 // Error if not in a :try scope
1664 if (scope == NULL || scope->se_type != TRY_SCOPE)
1665 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001666 emsg(_(e_catch_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001667 return NULL;
1668 }
1669
Bram Moolenaar9d383f32023-05-14 21:38:12 +01001670 if (scope->se_u.se_try.ts_caught_all
1671 && !ignore_unreachable_code_for_testing)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001672 {
1673 emsg(_(e_catch_unreachable_after_catch_all));
1674 return NULL;
1675 }
Bram Moolenaar53c29612022-01-12 16:18:18 +00001676 if (!cctx->ctx_had_return)
1677 scope->se_u.se_try.ts_no_return = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001678
1679 if (cctx->ctx_skip != SKIP_YES)
1680 {
1681#ifdef FEAT_PROFILE
1682 // the profile-start should be after the jump
1683 if (cctx->ctx_compile_type == CT_PROFILE
1684 && instr->ga_len > 0
1685 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
1686 .isn_type == ISN_PROF_START)
1687 --instr->ga_len;
1688#endif
1689 // Jump from end of previous block to :finally or :endtry
1690 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001691 JUMP_ALWAYS, 0, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001692 return NULL;
1693
1694 // End :try or :catch scope: set value in ISN_TRY instruction
1695 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001696 if (isn->isn_arg.tryref.try_ref->try_catch == 0)
1697 isn->isn_arg.tryref.try_ref->try_catch = instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001698 if (scope->se_u.se_try.ts_catch_label != 0)
1699 {
1700 // Previous catch without match jumps here
1701 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
1702 isn->isn_arg.jump.jump_where = instr->ga_len;
1703 }
1704#ifdef FEAT_PROFILE
1705 if (cctx->ctx_compile_type == CT_PROFILE)
1706 {
1707 // a "throw" that jumps here needs to be counted
1708 generate_instr(cctx, ISN_PROF_END);
1709 // the "catch" is also counted
1710 generate_instr(cctx, ISN_PROF_START);
1711 }
1712#endif
1713 if (cctx->ctx_compile_type == CT_DEBUG)
1714 generate_instr_debug(cctx);
1715 }
1716
1717 p = skipwhite(arg);
1718 if (ends_excmd2(arg, p))
1719 {
1720 scope->se_u.se_try.ts_caught_all = TRUE;
1721 scope->se_u.se_try.ts_catch_label = 0;
1722 }
1723 else
1724 {
1725 char_u *end;
1726 char_u *pat;
1727 char_u *tofree = NULL;
1728 int dropped = 0;
1729 int len;
1730
1731 // Push v:exception, push {expr} and MATCH
1732 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
1733
1734 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
1735 if (*end != *p)
1736 {
1737 semsg(_(e_separator_mismatch_str), p);
1738 vim_free(tofree);
Bram Moolenaar54969f42022-02-07 13:56:44 +00001739 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001740 }
1741 if (tofree == NULL)
1742 len = (int)(end - (p + 1));
1743 else
1744 len = (int)(end - tofree);
1745 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
1746 vim_free(tofree);
1747 p += len + 2 + dropped;
1748 if (pat == NULL)
Bram Moolenaar54969f42022-02-07 13:56:44 +00001749 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001750 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaar54969f42022-02-07 13:56:44 +00001751 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001752
1753 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
1754 return NULL;
1755
1756 scope->se_u.se_try.ts_catch_label = instr->ga_len;
1757 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
1758 return NULL;
1759 }
1760
1761 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
1762 return NULL;
1763
1764 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
1765 return NULL;
1766 return p;
1767}
1768
1769 char_u *
1770compile_finally(char_u *arg, cctx_T *cctx)
1771{
1772 scope_T *scope = cctx->ctx_scope;
1773 garray_T *instr = &cctx->ctx_instr;
1774 isn_T *isn;
1775 int this_instr;
1776
1777 if (misplaced_cmdmod(cctx))
1778 return NULL;
1779
1780 // end block scope from :try or :catch
1781 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1782 compile_endblock(cctx);
1783 scope = cctx->ctx_scope;
1784
1785 // Error if not in a :try scope
1786 if (scope == NULL || scope->se_type != TRY_SCOPE)
1787 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001788 emsg(_(e_finally_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001789 return NULL;
1790 }
1791
1792 if (cctx->ctx_skip != SKIP_YES)
1793 {
1794 // End :catch or :finally scope: set value in ISN_TRY instruction
1795 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar0d807102021-12-21 09:42:09 +00001796 if (isn->isn_arg.tryref.try_ref->try_finally != 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001797 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001798 emsg(_(e_multiple_finally));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001799 return NULL;
1800 }
1801
1802 this_instr = instr->ga_len;
1803#ifdef FEAT_PROFILE
1804 if (cctx->ctx_compile_type == CT_PROFILE
1805 && ((isn_T *)instr->ga_data)[this_instr - 1]
1806 .isn_type == ISN_PROF_START)
1807 {
1808 // jump to the profile start of the "finally"
1809 --this_instr;
1810
1811 // jump to the profile end above it
1812 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
1813 .isn_type == ISN_PROF_END)
1814 --this_instr;
1815 }
1816#endif
1817
1818 // Fill in the "end" label in jumps at the end of the blocks.
1819 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
1820 this_instr, cctx);
1821
1822 // If there is no :catch then an exception jumps to :finally.
Bram Moolenaar0d807102021-12-21 09:42:09 +00001823 if (isn->isn_arg.tryref.try_ref->try_catch == 0)
1824 isn->isn_arg.tryref.try_ref->try_catch = this_instr;
1825 isn->isn_arg.tryref.try_ref->try_finally = this_instr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001826 if (scope->se_u.se_try.ts_catch_label != 0)
1827 {
1828 // Previous catch without match jumps here
1829 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
1830 isn->isn_arg.jump.jump_where = this_instr;
1831 scope->se_u.se_try.ts_catch_label = 0;
1832 }
Bram Moolenaar53c29612022-01-12 16:18:18 +00001833 scope->se_u.se_try.ts_has_finally = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001834 if (generate_instr(cctx, ISN_FINALLY) == NULL)
1835 return NULL;
1836 }
1837
1838 return arg;
1839}
1840
1841 char_u *
1842compile_endtry(char_u *arg, cctx_T *cctx)
1843{
1844 scope_T *scope = cctx->ctx_scope;
1845 garray_T *instr = &cctx->ctx_instr;
1846 isn_T *try_isn;
1847
1848 if (misplaced_cmdmod(cctx))
1849 return NULL;
1850
1851 // end block scope from :catch or :finally
1852 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
1853 compile_endblock(cctx);
1854 scope = cctx->ctx_scope;
1855
1856 // Error if not in a :try scope
1857 if (scope == NULL || scope->se_type != TRY_SCOPE)
1858 {
1859 if (scope == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001860 emsg(_(e_endtry_without_try));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001861 else if (scope->se_type == WHILE_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001862 emsg(_(e_missing_endwhile));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001863 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001864 emsg(_(e_missing_endfor));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001865 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001866 emsg(_(e_missing_endif));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001867 return NULL;
1868 }
1869
1870 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
1871 if (cctx->ctx_skip != SKIP_YES)
1872 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00001873 if (try_isn->isn_arg.tryref.try_ref->try_catch == 0
1874 && try_isn->isn_arg.tryref.try_ref->try_finally == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001875 {
1876 emsg(_(e_missing_catch_or_finally));
1877 return NULL;
1878 }
1879
1880#ifdef FEAT_PROFILE
1881 if (cctx->ctx_compile_type == CT_PROFILE
1882 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
1883 .isn_type == ISN_PROF_START)
1884 // move the profile start after "endtry" so that it's not counted when
1885 // the exception is rethrown.
1886 --instr->ga_len;
1887#endif
1888
1889 // Fill in the "end" label in jumps at the end of the blocks, if not
1890 // done by ":finally".
1891 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
1892 instr->ga_len, cctx);
1893
1894 if (scope->se_u.se_try.ts_catch_label != 0)
1895 {
1896 // Last catch without match jumps here
1897 isn_T *isn = ((isn_T *)instr->ga_data)
1898 + scope->se_u.se_try.ts_catch_label;
1899 isn->isn_arg.jump.jump_where = instr->ga_len;
1900 }
1901 }
1902
Bram Moolenaar53c29612022-01-12 16:18:18 +00001903 // If there is a finally clause that ends in return then we will return.
1904 // If one of the blocks didn't end in "return" or we did not catch all
1905 // exceptions reset the had_return flag.
1906 if (!(scope->se_u.se_try.ts_has_finally && cctx->ctx_had_return)
1907 && (scope->se_u.se_try.ts_no_return
1908 || !scope->se_u.se_try.ts_caught_all))
1909 cctx->ctx_had_return = FALSE;
1910
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001911 compile_endblock(cctx);
1912
1913 if (cctx->ctx_skip != SKIP_YES)
1914 {
1915 // End :catch or :finally scope: set instruction index in ISN_TRY
1916 // instruction
Bram Moolenaar0d807102021-12-21 09:42:09 +00001917 try_isn->isn_arg.tryref.try_ref->try_endtry = instr->ga_len;
Bram Moolenaarfe154992022-03-22 20:42:12 +00001918 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001919 return NULL;
1920#ifdef FEAT_PROFILE
1921 if (cctx->ctx_compile_type == CT_PROFILE)
1922 generate_instr(cctx, ISN_PROF_START);
1923#endif
1924 }
1925 return arg;
1926}
1927
1928/*
1929 * compile "throw {expr}"
1930 */
1931 char_u *
Dominique Pellé0268ff32024-07-28 21:12:20 +02001932compile_throw(char_u *arg, cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001933{
1934 char_u *p = skipwhite(arg);
1935
1936 if (compile_expr0(&p, cctx) == FAIL)
1937 return NULL;
1938 if (cctx->ctx_skip == SKIP_YES)
1939 return p;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001940 if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001941 return NULL;
1942 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
1943 return NULL;
1944
1945 return p;
1946}
1947
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001948/*
1949 * Compile an expression or function call.
1950 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001951 char_u *
1952compile_eval(char_u *arg, cctx_T *cctx)
1953{
1954 char_u *p = arg;
1955 int name_only;
1956 long lnum = SOURCING_LNUM;
1957
1958 // find_ex_command() will consider a variable name an expression, assuming
1959 // that something follows on the next line. Check that something actually
1960 // follows, otherwise it's probably a misplaced command.
1961 name_only = cmd_is_name_only(arg);
1962
1963 if (compile_expr0(&p, cctx) == FAIL)
1964 return NULL;
1965
1966 if (name_only && lnum == SOURCING_LNUM)
1967 {
1968 semsg(_(e_expression_without_effect_str), arg);
1969 return NULL;
1970 }
1971
1972 // drop the result
1973 generate_instr_drop(cctx, ISN_DROP, 1);
1974
1975 return skipwhite(p);
1976}
1977
1978/*
Bram Moolenaarc572ad52022-09-08 20:49:22 +01001979 * Get the local variable index for deferred function calls.
1980 * Reserve it when not done already.
1981 * Returns zero for failure.
1982 */
1983 int
1984get_defer_var_idx(cctx_T *cctx)
1985{
1986 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1987 + cctx->ctx_ufunc->uf_dfunc_idx;
1988 if (dfunc->df_defer_var_idx == 0)
1989 {
1990 lvar_T *lvar = reserve_local(cctx, (char_u *)"@defer@", 7,
1991 TRUE, &t_list_any);
1992 if (lvar == NULL)
1993 return 0;
1994 dfunc->df_defer_var_idx = lvar->lv_idx + 1;
1995 }
1996 return dfunc->df_defer_var_idx;
1997}
1998
1999/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002000 * Compile "defer func(arg)".
2001 */
2002 char_u *
2003compile_defer(char_u *arg_start, cctx_T *cctx)
2004{
Bram Moolenaar16900322022-09-08 19:51:45 +01002005 char_u *paren;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002006 char_u *arg = arg_start;
2007 int argcount = 0;
Bram Moolenaar806a2732022-09-04 15:40:36 +01002008 int defer_var_idx;
Yegappan Lakshmanan38ce1a72023-12-18 08:58:29 +01002009 type_T *type = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002010 int func_idx;
2011
2012 // Get a funcref for the function name.
2013 // TODO: better way to find the "(".
Bram Moolenaar16900322022-09-08 19:51:45 +01002014 paren = vim_strchr(arg, '(');
2015 if (paren == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002016 {
2017 semsg(_(e_missing_parenthesis_str), arg);
2018 return NULL;
2019 }
Bram Moolenaar16900322022-09-08 19:51:45 +01002020 *paren = NUL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002021 func_idx = find_internal_func(arg);
2022 if (func_idx >= 0)
2023 // TODO: better type
2024 generate_PUSHFUNC(cctx, (char_u *)internal_func_name(func_idx),
2025 &t_func_any, FALSE);
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02002026 else if (compile_expr0(&arg, cctx) == FAIL)
2027 return NULL;
Bram Moolenaar16900322022-09-08 19:51:45 +01002028 *paren = '(';
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002029
2030 // check for function type
Yegappan Lakshmanana185a312023-12-16 14:36:08 +01002031 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002032 {
Yegappan Lakshmanana185a312023-12-16 14:36:08 +01002033 type = get_type_on_stack(cctx, 0);
2034 if (type->tt_type != VAR_FUNC)
2035 {
2036 emsg(_(e_function_name_required));
2037 return NULL;
2038 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002039 }
2040
2041 // compile the arguments
Bram Moolenaar16900322022-09-08 19:51:45 +01002042 arg = skipwhite(paren + 1);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002043 if (compile_arguments(&arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
2044 return NULL;
2045
Yegappan Lakshmanana185a312023-12-16 14:36:08 +01002046 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar16900322022-09-08 19:51:45 +01002047 {
Yegappan Lakshmanana185a312023-12-16 14:36:08 +01002048 if (func_idx >= 0)
2049 {
2050 type2_T *argtypes = NULL;
2051 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
Bram Moolenaar16900322022-09-08 19:51:45 +01002052
Yegappan Lakshmanana185a312023-12-16 14:36:08 +01002053 if (check_internal_func_args(cctx, func_idx, argcount, FALSE,
2054 &argtypes, shuffled_argtypes) == FAIL)
2055 return NULL;
2056 }
2057 else if (check_func_args_from_type(cctx, type, argcount, TRUE,
2058 arg_start) == FAIL)
2059 return NULL;
2060
2061 defer_var_idx = get_defer_var_idx(cctx);
2062 if (defer_var_idx == 0)
2063 return NULL;
2064 if (generate_DEFER(cctx, defer_var_idx - 1, argcount) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01002065 return NULL;
2066 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002067
2068 return skipwhite(arg);
2069}
2070
2071/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002072 * compile "echo expr"
2073 * compile "echomsg expr"
2074 * compile "echoerr expr"
2075 * compile "echoconsole expr"
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002076 * compile "echowindow expr" - may have cmd_count set
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002077 * compile "execute expr"
2078 */
2079 char_u *
Bram Moolenaar9b994112022-12-25 15:59:25 +00002080compile_mult_expr(
2081 char_u *arg,
2082 int cmdidx,
2083 long cmd_count UNUSED,
2084 cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002085{
2086 char_u *p = arg;
2087 char_u *prev = arg;
2088 char_u *expr_start;
2089 int count = 0;
2090 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002091 type_T *type;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002092 int r = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002093
2094 for (;;)
2095 {
2096 if (ends_excmd2(prev, p))
2097 break;
2098 expr_start = p;
2099 if (compile_expr0(&p, cctx) == FAIL)
2100 return NULL;
2101
2102 if (cctx->ctx_skip != SKIP_YES)
2103 {
2104 // check for non-void type
Bram Moolenaar078a4612022-01-04 15:17:03 +00002105 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002106 if (type->tt_type == VAR_VOID)
2107 {
2108 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
2109 return NULL;
2110 }
2111 }
2112
2113 ++count;
2114 prev = p;
2115 p = skipwhite(p);
2116 }
2117
2118 if (count > 0)
2119 {
2120 long save_lnum = cctx->ctx_lnum;
2121
2122 // Use the line number where the command started.
2123 cctx->ctx_lnum = start_ctx_lnum;
2124
2125 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002126 r = generate_ECHO(cctx, cmdidx == CMD_echo, count);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002127 else if (cmdidx == CMD_execute)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002128 r = generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002129 else if (cmdidx == CMD_echomsg)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002130 r = generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01002131#ifdef HAS_MESSAGE_WINDOW
2132 else if (cmdidx == CMD_echowindow)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002133 r = generate_ECHOWINDOW(cctx, count, cmd_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01002134#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002135 else if (cmdidx == CMD_echoconsole)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002136 r = generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002137 else
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002138 r = generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002139
2140 cctx->ctx_lnum = save_lnum;
2141 }
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002142 return r == OK ? p : NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002143}
2144
2145/*
2146 * If "eap" has a range that is not a constant generate an ISN_RANGE
2147 * instruction to compute it and return OK.
2148 * Otherwise return FAIL, the caller must deal with any range.
2149 */
2150 static int
2151compile_variable_range(exarg_T *eap, cctx_T *cctx)
2152{
2153 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
2154 char_u *p = skipdigits(eap->cmd);
2155
2156 if (p == range_end)
2157 return FAIL;
2158 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
2159}
2160
2161/*
2162 * :put r
2163 * :put ={expr}
2164 */
2165 char_u *
Christian Brabandt762a79e2025-03-16 21:39:58 +01002166compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002167{
2168 char_u *line = arg;
2169 linenr_T lnum;
2170 char *errormsg;
2171 int above = eap->forceit;
2172
2173 eap->regname = *line;
2174
2175 if (eap->regname == '=')
2176 {
Bram Moolenaard0b7bfa2022-03-12 14:51:16 +00002177 char_u *p = skipwhite(line + 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002178
2179 if (compile_expr0(&p, cctx) == FAIL)
2180 return NULL;
2181 line = p;
2182 }
2183 else if (eap->regname != NUL)
2184 ++line;
2185
2186 if (compile_variable_range(eap, cctx) == OK)
2187 {
2188 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
2189 }
2190 else
2191 {
2192 // Either no range or a number.
2193 // "errormsg" will not be set because the range is ADDR_LINES.
2194 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
2195 // cannot happen
2196 return NULL;
2197 if (eap->addr_count == 0)
2198 lnum = -1;
2199 else
2200 lnum = eap->line2;
2201 if (above)
2202 --lnum;
2203 }
2204
Christian Brabandt762a79e2025-03-16 21:39:58 +01002205 generate_PUT(cctx, eap->regname, lnum);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002206 return line;
2207}
2208
2209/*
2210 * A command that is not compiled, execute with legacy code.
2211 */
2212 char_u *
2213compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
2214{
2215 char_u *line = line_arg;
2216 char_u *p;
2217 int has_expr = FALSE;
2218 char_u *nextcmd = (char_u *)"";
2219 char_u *tofree = NULL;
2220 char_u *cmd_arg = NULL;
2221
2222 if (cctx->ctx_skip == SKIP_YES)
2223 goto theend;
2224
2225 // If there was a prececing command modifier, drop it and include it in the
2226 // EXEC command.
2227 if (cctx->ctx_has_cmdmod)
2228 {
2229 garray_T *instr = &cctx->ctx_instr;
2230 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2231
2232 if (isn->isn_type == ISN_CMDMOD)
2233 {
2234 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
2235 ->cmod_filter_regmatch.regprog);
2236 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2237 --instr->ga_len;
2238 cctx->ctx_has_cmdmod = FALSE;
2239 }
2240 }
2241
2242 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
2243 {
2244 long argt = eap->argt;
2245 int usefilter = FALSE;
2246
2247 has_expr = argt & (EX_XFILE | EX_EXPAND);
2248
2249 // If the command can be followed by a bar, find the bar and truncate
2250 // it, so that the following command can be compiled.
2251 // The '|' is overwritten with a NUL, it is put back below.
2252 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
2253 && *eap->arg == '!')
2254 // :w !filter or :r !filter or :r! filter
2255 usefilter = TRUE;
2256 if ((argt & EX_TRLBAR) && !usefilter)
2257 {
2258 eap->argt = argt;
Bram Moolenaarac485062022-03-23 19:45:01 +00002259 separate_nextcmd(eap, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002260 if (eap->nextcmd != NULL)
2261 nextcmd = eap->nextcmd;
2262 }
2263 else if (eap->cmdidx == CMD_wincmd)
2264 {
2265 p = eap->arg;
2266 if (*p != NUL)
2267 ++p;
2268 if (*p == 'g' || *p == Ctrl_G)
2269 ++p;
2270 p = skipwhite(p);
2271 if (*p == '|')
2272 {
2273 *p = NUL;
2274 nextcmd = p + 1;
2275 }
2276 }
2277 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
2278 {
2279 // If there is a trailing '{' read lines until the '}'
2280 p = eap->arg + STRLEN(eap->arg) - 1;
2281 while (p > eap->arg && VIM_ISWHITE(*p))
2282 --p;
2283 if (*p == '{')
2284 {
2285 exarg_T ea;
Bram Moolenaar62628d92022-02-25 21:10:53 +00002286 int flags = 0; // unused
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002287 int start_lnum = SOURCING_LNUM;
2288
2289 CLEAR_FIELD(ea);
2290 ea.arg = eap->arg;
2291 fill_exarg_from_cctx(&ea, cctx);
2292 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
2293 if (tofree != NULL)
2294 {
2295 *p = NUL;
2296 line = concat_str(line, tofree);
2297 if (line == NULL)
2298 goto theend;
2299 vim_free(tofree);
2300 tofree = line;
2301 SOURCING_LNUM = start_lnum;
2302 }
2303 }
2304 }
2305 }
2306
2307 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
2308 {
2309 // expand filename in "syntax include [@group] filename"
2310 has_expr = TRUE;
2311 eap->arg = skipwhite(eap->arg + 7);
2312 if (*eap->arg == '@')
2313 eap->arg = skiptowhite(eap->arg);
2314 }
2315
2316 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
2317 && STRLEN(eap->arg) > 4)
2318 {
2319 int delim = *eap->arg;
2320
2321 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
2322 if (*p == delim)
2323 cmd_arg = p + 1;
2324 }
2325
2326 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
2327 cmd_arg = eap->arg;
2328
2329 if (cmd_arg != NULL)
2330 {
2331 exarg_T nea;
2332
2333 CLEAR_FIELD(nea);
2334 nea.cmd = cmd_arg;
2335 p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
2336 if (nea.cmdidx < CMD_SIZE)
2337 {
2338 has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
2339 if (has_expr)
2340 eap->arg = skiptowhite(eap->arg);
2341 }
2342 }
2343
2344 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
2345 {
2346 int count = 0;
2347 char_u *start = skipwhite(line);
2348
2349 // :cmd xxx`=expr1`yyy`=expr2`zzz
2350 // PUSHS ":cmd xxx"
2351 // eval expr1
2352 // PUSHS "yyy"
2353 // eval expr2
2354 // PUSHS "zzz"
2355 // EXECCONCAT 5
2356 for (;;)
2357 {
2358 if (p > start)
2359 {
2360 char_u *val = vim_strnsave(start, p - start);
2361
2362 generate_PUSHS(cctx, &val);
2363 ++count;
2364 }
2365 p += 2;
2366 if (compile_expr0(&p, cctx) == FAIL)
2367 return NULL;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02002368 may_generate_2STRING(-1, TOSTRING_TOLERANT, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002369 ++count;
2370 p = skipwhite(p);
2371 if (*p != '`')
2372 {
2373 emsg(_(e_missing_backtick));
2374 return NULL;
2375 }
2376 start = p + 1;
2377
2378 p = (char_u *)strstr((char *)start, "`=");
2379 if (p == NULL)
2380 {
2381 if (*skipwhite(start) != NUL)
2382 {
2383 char_u *val = vim_strsave(start);
2384
2385 generate_PUSHS(cctx, &val);
2386 ++count;
2387 }
2388 break;
2389 }
2390 }
2391 generate_EXECCONCAT(cctx, count);
2392 }
2393 else
2394 generate_EXEC_copy(cctx, ISN_EXEC, line);
2395
2396theend:
2397 if (*nextcmd != NUL)
2398 {
2399 // the parser expects a pointer to the bar, put it back
2400 --nextcmd;
2401 *nextcmd = '|';
2402 }
2403 vim_free(tofree);
2404
2405 return nextcmd;
2406}
2407
2408/*
2409 * A script command with heredoc, e.g.
2410 * ruby << EOF
2411 * command
2412 * EOF
2413 * Has been turned into one long line with NL characters by
2414 * get_function_body():
2415 * ruby << EOF<NL> command<NL>EOF
2416 */
2417 char_u *
2418compile_script(char_u *line, cctx_T *cctx)
2419{
2420 if (cctx->ctx_skip != SKIP_YES)
2421 {
2422 isn_T *isn;
2423
2424 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
2425 return NULL;
2426 isn->isn_arg.string = vim_strsave(line);
2427 }
2428 return (char_u *)"";
2429}
2430
2431
2432/*
2433 * :s/pat/repl/
2434 */
2435 char_u *
2436compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
2437{
2438 char_u *cmd = eap->arg;
2439 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
2440
2441 if (expr != NULL)
2442 {
2443 int delimiter = *cmd++;
2444
2445 // There is a \=expr, find it in the substitute part.
2446 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
2447 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
2448 {
2449 garray_T save_ga = cctx->ctx_instr;
2450 char_u *end;
2451 int expr_res;
2452 int trailing_error;
2453 int instr_count;
2454 isn_T *instr;
2455 isn_T *isn;
2456
2457 cmd += 3;
2458 end = skip_substitute(cmd, delimiter);
2459
2460 // Temporarily reset the list of instructions so that the jump
2461 // labels are correct.
2462 cctx->ctx_instr.ga_len = 0;
2463 cctx->ctx_instr.ga_maxlen = 0;
2464 cctx->ctx_instr.ga_data = NULL;
2465 expr_res = compile_expr0(&cmd, cctx);
2466 if (end[-1] == NUL)
2467 end[-1] = delimiter;
2468 cmd = skipwhite(cmd);
2469 trailing_error = *cmd != delimiter && *cmd != NUL;
2470
2471 if (expr_res == FAIL || trailing_error
2472 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
2473 {
2474 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002475 semsg(_(e_trailing_characters_str), cmd);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002476 clear_instr_ga(&cctx->ctx_instr);
2477 cctx->ctx_instr = save_ga;
2478 return NULL;
2479 }
2480
2481 // Move the generated instructions into the ISN_SUBSTITUTE
2482 // instructions, then restore the list of instructions before
2483 // adding the ISN_SUBSTITUTE instruction.
2484 instr_count = cctx->ctx_instr.ga_len;
2485 instr = cctx->ctx_instr.ga_data;
2486 instr[instr_count].isn_type = ISN_FINISH;
2487
2488 cctx->ctx_instr = save_ga;
2489 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
2490 {
2491 int idx;
2492
2493 for (idx = 0; idx < instr_count; ++idx)
2494 delete_instr(instr + idx);
2495 vim_free(instr);
2496 return NULL;
2497 }
2498 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
2499 isn->isn_arg.subs.subs_instr = instr;
2500
2501 // skip over flags
2502 if (*end == '&')
2503 ++end;
2504 while (ASCII_ISALPHA(*end) || *end == '#')
2505 ++end;
2506 return end;
2507 }
2508 }
2509
2510 return compile_exec(arg, eap, cctx);
2511}
2512
2513 char_u *
2514compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
2515{
2516 char_u *arg = eap->arg;
2517 lhs_T *lhs = &cctx->ctx_redir_lhs;
2518
2519 if (lhs->lhs_name != NULL)
2520 {
2521 if (STRNCMP(arg, "END", 3) == 0)
2522 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002523 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002524 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002525 if (lhs->lhs_append)
2526 {
2527 // First load the current variable value.
2528 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002529 cctx) == FAIL)
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002530 return NULL;
2531 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002532
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002533 // Gets the redirected text and put it on the stack, then store
2534 // it in the variable.
2535 generate_instr_type(cctx, ISN_REDIREND, &t_string);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002536
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002537 if (lhs->lhs_append)
2538 generate_CONCAT(cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002539
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002540 if (lhs->lhs_has_index)
2541 {
2542 // Use the info in "lhs" to store the value at the index in
2543 // the list or dict.
2544 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002545 &t_string, cctx) == FAIL)
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002546 return NULL;
2547 }
2548 else if (generate_store_lhs(cctx, lhs, -1, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002549 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002550
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002551 VIM_CLEAR(lhs->lhs_name);
2552 VIM_CLEAR(lhs->lhs_whole);
2553 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002554 return arg + 3;
2555 }
2556 emsg(_(e_cannot_nest_redir));
2557 return NULL;
2558 }
2559
2560 if (arg[0] == '=' && arg[1] == '>')
2561 {
2562 int append = FALSE;
2563
2564 // redirect to a variable is compiled
2565 arg += 2;
2566 if (*arg == '>')
2567 {
2568 ++arg;
2569 append = TRUE;
2570 }
2571 arg = skipwhite(arg);
2572
2573 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002574 FALSE, FALSE, FALSE, 1, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002575 return NULL;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002576 if (need_type(&t_string, lhs->lhs_member_type, FALSE,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002577 -1, 0, cctx, FALSE, FALSE) == FAIL)
2578 return NULL;
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002579 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002580 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01002581 VIM_CLEAR(lhs->lhs_name);
2582 }
2583 else
2584 {
2585 generate_instr(cctx, ISN_REDIRSTART);
2586 lhs->lhs_append = append;
2587 if (lhs->lhs_has_index)
2588 {
2589 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
2590 if (lhs->lhs_whole == NULL)
2591 return NULL;
2592 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002593 }
2594
2595 return arg + lhs->lhs_varlen_total;
2596 }
2597
2598 // other redirects are handled like at script level
2599 return compile_exec(line, eap, cctx);
2600}
2601
2602#if defined(FEAT_QUICKFIX) || defined(PROTO)
2603 char_u *
2604compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
2605{
2606 isn_T *isn;
2607 char_u *p;
2608
2609 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
2610 if (isn == NULL)
2611 return NULL;
2612 isn->isn_arg.number = eap->cmdidx;
2613
2614 p = eap->arg;
2615 if (compile_expr0(&p, cctx) == FAIL)
2616 return NULL;
2617
2618 isn = generate_instr(cctx, ISN_CEXPR_CORE);
2619 if (isn == NULL)
2620 return NULL;
2621 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
2622 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
2623 return NULL;
2624 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
2625 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
2626 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
2627
2628 return p;
2629}
2630#endif
2631
2632/*
2633 * Compile "return [expr]".
2634 * When "legacy" is TRUE evaluate [expr] with legacy syntax
2635 */
2636 char_u *
2637compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
2638{
2639 char_u *p = arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002640 type_T *stack_type;
2641
mityu500c4442022-12-02 18:12:05 +00002642 if (*p != NUL && *p != '|' && *p != '\n'
2643 && (legacy || !vim9_comment_start(p)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002644 {
Bram Moolenaar0bfa8492022-01-22 12:27:04 +00002645 // For a lambda, "return expr" is always used, also when "expr" results
2646 // in a void.
2647 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
2648 && (cctx->ctx_ufunc->uf_flags & FC_LAMBDA) == 0)
Bram Moolenaaref7aadb2022-01-18 18:46:07 +00002649 {
2650 emsg(_(e_returning_value_in_function_without_return_type));
2651 return NULL;
2652 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002653 if (legacy)
2654 {
2655 int save_flags = cmdmod.cmod_flags;
2656
2657 generate_LEGACY_EVAL(cctx, p);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002658 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, FALSE, -1,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002659 0, cctx, FALSE, FALSE) == FAIL)
2660 return NULL;
2661 cmdmod.cmod_flags |= CMOD_LEGACY;
2662 (void)skip_expr(&p, NULL);
2663 cmdmod.cmod_flags = save_flags;
2664 }
2665 else
2666 {
2667 // compile return argument into instructions
2668 if (compile_expr0(&p, cctx) == FAIL)
2669 return NULL;
2670 }
2671
2672 if (cctx->ctx_skip != SKIP_YES)
2673 {
2674 // "check_return_type" with uf_ret_type set to &t_unknown is used
2675 // for an inline function without a specified return type. Set the
2676 // return type here.
Bram Moolenaar078a4612022-01-04 15:17:03 +00002677 stack_type = get_type_on_stack(cctx, 0);
Ernie Raelb077b582023-12-14 20:11:44 +01002678 if (check_type_is_value(stack_type) == FAIL)
2679 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002680 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar1a572e92022-03-15 12:28:10 +00002681 || cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002682 || (!check_return_type
2683 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
2684 {
2685 cctx->ctx_ufunc->uf_ret_type = stack_type;
2686 }
2687 else
2688 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002689 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, FALSE,
2690 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002691 return NULL;
2692 }
2693 }
2694 }
2695 else
2696 {
2697 // "check_return_type" cannot be TRUE, only used for a lambda which
2698 // always has an argument.
2699 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
2700 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
2701 {
2702 emsg(_(e_missing_return_value));
2703 return NULL;
2704 }
2705
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +02002706 if (IS_CONSTRUCTOR_METHOD(cctx->ctx_ufunc))
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02002707 {
2708 // For a class new() constructor, return an object of the class.
2709 generate_instr(cctx, ISN_RETURN_OBJECT);
2710 cctx->ctx_ufunc->uf_ret_type =
2711 &cctx->ctx_ufunc->uf_class->class_object_type;
2712 }
2713 else
2714 // No argument, return zero.
2715 generate_PUSHNR(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002716 }
2717
Bram Moolenaar8abb5842022-09-17 12:39:58 +01002718 // may need ENDLOOP when inside a :for or :while loop
2719 if (compile_find_scope(NULL, NULL, NULL, NULL, cctx) == FAIL)
2720
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002721 // Undo any command modifiers.
2722 generate_undo_cmdmods(cctx);
2723
2724 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
2725 return NULL;
2726
2727 // "return val | endif" is possible
2728 return skipwhite(p);
2729}
2730
2731/*
2732 * Check if the separator for a :global or :substitute command is OK.
2733 */
2734 int
2735check_global_and_subst(char_u *cmd, char_u *arg)
2736{
2737 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
2738 {
2739 semsg(_(e_separator_not_supported_str), arg);
2740 return FAIL;
2741 }
2742 if (VIM_ISWHITE(cmd[1]))
2743 {
2744 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
2745 return FAIL;
2746 }
2747 return OK;
2748}
2749
2750
2751#endif // defined(FEAT_EVAL)