-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvim9cmds.c
2653 lines (2349 loc) · 66.9 KB
/
vim9cmds.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* vim9cmds.c: Dealing with commands of a compiled function
*/
#define USING_FLOAT_STUFF
#include "vim.h"
#if defined(FEAT_EVAL) || defined(PROTO)
// When not generating protos this is included in proto.h
#ifdef PROTO
# include "vim9.h"
#endif
/*
* Get the index of the current instruction.
* This compensates for a preceding ISN_CMDMOD and ISN_PROF_START.
*/
static int
current_instr_idx(cctx_T *cctx)
{
garray_T *instr = &cctx->ctx_instr;
int idx = instr->ga_len;
while (idx > 0)
{
if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
.isn_type == ISN_CMDMOD)
{
--idx;
continue;
}
#ifdef FEAT_PROFILE
if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
{
--idx;
continue;
}
#endif
if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
{
--idx;
continue;
}
break;
}
return idx;
}
/*
* Remove local variables above "new_top".
* Do this by clearing the name. If "keep" is TRUE do not reset the length, a
* closure may still need location of the variable.
*/
static void
unwind_locals(cctx_T *cctx, int new_top, int keep)
{
if (cctx->ctx_locals.ga_len > new_top)
for (int idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
{
lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
VIM_CLEAR(lvar->lv_name);
}
if (!keep)
cctx->ctx_locals.ga_len = new_top;
}
/*
* Free all local variables.
*/
void
free_locals(cctx_T *cctx)
{
unwind_locals(cctx, 0, FALSE);
ga_clear(&cctx->ctx_locals);
}
/*
* Check if "name" can be "unlet".
*/
int
check_vim9_unlet(char_u *name)
{
if (*name == NUL)
{
semsg(_(e_argument_required_for_str), "unlet");
return FAIL;
}
if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
{
// "unlet s:var" is allowed in legacy script.
if (*name == 's' && !script_is_vim9())
return OK;
semsg(_(e_cannot_unlet_str), name);
return FAIL;
}
return OK;
}
/*
* Callback passed to ex_unletlock().
*/
static int
compile_unlet(
lval_T *lvp,
char_u *name_end,
exarg_T *eap,
int deep UNUSED,
void *coookie)
{
cctx_T *cctx = coookie;
char_u *p = lvp->ll_name;
int cc = *name_end;
int ret = OK;
if (cctx->ctx_skip == SKIP_YES)
return OK;
*name_end = NUL;
if (*p == '$')
{
// :unlet $ENV_VAR
ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
}
else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
{
lhs_T lhs;
// This is similar to assigning: lookup the list/dict, compile the
// idx/key. Then instead of storing the value unlet the item.
// unlet {list}[idx]
// unlet {dict}[key] dict.key
//
// Figure out the LHS type and other properties.
//
ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, FALSE, 0, cctx);
// Use the info in "lhs" to unlet the item at the index in the
// list or dict.
if (ret == OK)
{
if (!lhs.lhs_has_index)
{
semsg(_(e_cannot_unlet_imported_item_str), p);
ret = FAIL;
}
else
ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
}
vim_free(lhs.lhs_name);
}
else if (check_vim9_unlet(p) == FAIL)
{
ret = FAIL;
}
else
{
// Normal name. Only supports g:, w:, t: and b: namespaces.
ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
}
*name_end = cc;
return ret;
}
/*
* Callback passed to ex_unletlock().
*/
static int
compile_lock_unlock(
lval_T *lvp,
char_u *name_end,
exarg_T *eap,
int deep,
void *coookie)
{
cctx_T *cctx = coookie;
int cc = *name_end;
char_u *p = lvp->ll_name;
int ret = OK;
size_t len;
char_u *buf;
isntype_T isn = ISN_EXEC;
char *cmd = eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar";
if (cctx->ctx_skip == SKIP_YES)
return OK;
if (*p == NUL)
{
semsg(_(e_argument_required_for_str), cmd);
return FAIL;
}
// Cannot use :lockvar and :unlockvar on local variables.
if (p[1] != ':')
{
char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
if (lookup_local(p, end - p, NULL, cctx) == OK)
{
char_u *s = p;
if (*end != '.' && *end != '[')
{
emsg(_(e_cannot_lock_unlock_local_variable));
return FAIL;
}
// For "d.member" put the local variable on the stack, it will be
// passed to ex_lockvar() indirectly.
if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
return FAIL;
isn = ISN_LOCKUNLOCK;
}
}
// Checking is done at runtime.
*name_end = NUL;
len = name_end - p + 20;
buf = alloc(len);
if (buf == NULL)
ret = FAIL;
else
{
if (deep < 0)
vim_snprintf((char *)buf, len, "%s! %s", cmd, p);
else
vim_snprintf((char *)buf, len, "%s %d %s", cmd, deep, p);
ret = generate_EXEC_copy(cctx, isn, buf);
vim_free(buf);
*name_end = cc;
}
return ret;
}
/*
* compile "unlet var", "lock var" and "unlock var"
* "arg" points to "var".
*/
char_u *
compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
{
int deep = 0;
char_u *p = arg;
if (eap->cmdidx != CMD_unlet)
{
if (eap->forceit)
deep = -1;
else if (vim_isdigit(*p))
{
deep = getdigits(&p);
p = skipwhite(p);
}
else
deep = 2;
}
ex_unletlock(eap, p, deep, GLV_NO_AUTOLOAD | GLV_COMPILING,
eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
cctx);
return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
}
/*
* Generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
* "funcref_idx" is used for JUMP_WHILE_FALSE
*/
static int
compile_jump_to_end(
endlabel_T **el,
jumpwhen_T when,
int funcref_idx,
cctx_T *cctx)
{
garray_T *instr = &cctx->ctx_instr;
endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
if (endlabel == NULL)
return FAIL;
endlabel->el_next = *el;
*el = endlabel;
endlabel->el_end_label = instr->ga_len;
if (when == JUMP_WHILE_FALSE)
generate_WHILE(cctx, funcref_idx);
else
generate_JUMP(cctx, when, 0);
return OK;
}
static void
compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
{
garray_T *instr = &cctx->ctx_instr;
while (*el != NULL)
{
endlabel_T *cur = (*el);
isn_T *isn;
isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
isn->isn_arg.jump.jump_where = jump_where;
*el = cur->el_next;
vim_free(cur);
}
}
static void
compile_free_jump_to_end(endlabel_T **el)
{
while (*el != NULL)
{
endlabel_T *cur = (*el);
*el = cur->el_next;
vim_free(cur);
}
}
/*
* Create a new scope and set up the generic items.
*/
static scope_T *
new_scope(cctx_T *cctx, scopetype_T type)
{
scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
if (scope == NULL)
return NULL;
scope->se_outer = cctx->ctx_scope;
cctx->ctx_scope = scope;
scope->se_type = type;
scope->se_local_count = cctx->ctx_locals.ga_len;
if (scope->se_outer != NULL)
scope->se_loop_depth = scope->se_outer->se_loop_depth;
return scope;
}
/*
* Free the current scope and go back to the outer scope.
*/
void
drop_scope(cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
if (scope == NULL)
{
iemsg("calling drop_scope() without a scope");
return;
}
cctx->ctx_scope = scope->se_outer;
switch (scope->se_type)
{
case IF_SCOPE:
compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
case FOR_SCOPE:
compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
case WHILE_SCOPE:
compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
case TRY_SCOPE:
compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
case NO_SCOPE:
case BLOCK_SCOPE:
break;
}
vim_free(scope);
}
static int
misplaced_cmdmod(cctx_T *cctx)
{
garray_T *instr = &cctx->ctx_instr;
if (cctx->ctx_has_cmdmod
&& ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
== ISN_CMDMOD)
{
emsg(_(e_misplaced_command_modifier));
return TRUE;
}
return FALSE;
}
/*
* compile "if expr"
*
* "if expr" Produces instructions:
* EVAL expr Push result of "expr"
* JUMP_IF_FALSE end
* ... body ...
* end:
*
* "if expr | else" Produces instructions:
* EVAL expr Push result of "expr"
* JUMP_IF_FALSE else
* ... body ...
* JUMP_ALWAYS end
* else:
* ... body ...
* end:
*
* "if expr1 | elseif expr2 | else" Produces instructions:
* EVAL expr Push result of "expr"
* JUMP_IF_FALSE elseif
* ... body ...
* JUMP_ALWAYS end
* elseif:
* EVAL expr Push result of "expr"
* JUMP_IF_FALSE else
* ... body ...
* JUMP_ALWAYS end
* else:
* ... body ...
* end:
*/
char_u *
compile_if(char_u *arg, cctx_T *cctx)
{
char_u *p = arg;
garray_T *instr = &cctx->ctx_instr;
int instr_count = instr->ga_len;
scope_T *scope;
skip_T skip_save = cctx->ctx_skip;
ppconst_T ppconst;
CLEAR_FIELD(ppconst);
if (compile_expr1(&p, cctx, &ppconst) == FAIL)
{
clear_ppconst(&ppconst);
return NULL;
}
if (!ends_excmd2(arg, skipwhite(p)))
{
semsg(_(e_trailing_characters_str), p);
return NULL;
}
if (cctx->ctx_skip == SKIP_YES)
clear_ppconst(&ppconst);
else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
{
int error = FALSE;
int v;
// The expression results in a constant.
v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
clear_ppconst(&ppconst);
if (error)
return NULL;
cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
}
else
{
// Not a constant, generate instructions for the expression.
cctx->ctx_skip = SKIP_UNKNOWN;
if (generate_ppconst(cctx, &ppconst) == FAIL)
return NULL;
if (bool_on_stack(cctx) == FAIL)
return NULL;
}
// CMDMOD_REV must come before the jump
generate_undo_cmdmods(cctx);
scope = new_scope(cctx, IF_SCOPE);
if (scope == NULL)
return NULL;
scope->se_skip_save = skip_save;
// "is_had_return" will be reset if any block does not end in :return
scope->se_u.se_if.is_had_return = TRUE;
if (cctx->ctx_skip == SKIP_UNKNOWN)
{
// "where" is set when ":elseif", "else" or ":endif" is found
scope->se_u.se_if.is_if_label = instr->ga_len;
generate_JUMP(cctx, JUMP_IF_FALSE, 0);
}
else
scope->se_u.se_if.is_if_label = -1;
#ifdef FEAT_PROFILE
if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
&& skip_save != SKIP_YES)
{
// generated a profile start, need to generate a profile end, since it
// won't be done after returning
cctx->ctx_skip = SKIP_NOT;
generate_instr(cctx, ISN_PROF_END);
cctx->ctx_skip = SKIP_YES;
}
#endif
return p;
}
char_u *
compile_elseif(char_u *arg, cctx_T *cctx)
{
char_u *p = arg;
garray_T *instr = &cctx->ctx_instr;
int instr_count;
isn_T *isn;
scope_T *scope = cctx->ctx_scope;
ppconst_T ppconst;
skip_T save_skip = cctx->ctx_skip;
if (scope == NULL || scope->se_type != IF_SCOPE)
{
emsg(_(e_elseif_without_if));
return NULL;
}
unwind_locals(cctx, scope->se_local_count, TRUE);
if (!cctx->ctx_had_return)
scope->se_u.se_if.is_had_return = FALSE;
if (cctx->ctx_skip == SKIP_NOT)
{
// previous block was executed, this one and following will not
cctx->ctx_skip = SKIP_YES;
scope->se_u.se_if.is_seen_skip_not = TRUE;
}
if (scope->se_u.se_if.is_seen_skip_not)
{
// A previous block was executed, skip over expression and bail out.
// Do not count the "elseif" for profiling and cmdmod
instr->ga_len = current_instr_idx(cctx);
skip_expr_cctx(&p, cctx);
return p;
}
if (cctx->ctx_skip == SKIP_UNKNOWN)
{
int moved_cmdmod = FALSE;
int saved_debug = FALSE;
isn_T debug_isn;
// Move any CMDMOD instruction to after the jump
if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
{
if (GA_GROW_FAILS(instr, 1))
return NULL;
((isn_T *)instr->ga_data)[instr->ga_len] =
((isn_T *)instr->ga_data)[instr->ga_len - 1];
--instr->ga_len;
moved_cmdmod = TRUE;
}
// Remove the already generated ISN_DEBUG, it is written below the
// ISN_FOR instruction.
if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
&& ((isn_T *)instr->ga_data)[instr->ga_len - 1]
.isn_type == ISN_DEBUG)
{
--instr->ga_len;
debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
saved_debug = TRUE;
}
if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
JUMP_ALWAYS, 0, cctx) == FAIL)
return NULL;
// previous "if" or "elseif" jumps here
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
isn->isn_arg.jump.jump_where = instr->ga_len;
if (moved_cmdmod)
++instr->ga_len;
if (saved_debug)
{
// move the debug instruction here
if (GA_GROW_FAILS(instr, 1))
return NULL;
((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
++instr->ga_len;
}
}
// compile "expr"; if we know it evaluates to FALSE skip the block
CLEAR_FIELD(ppconst);
if (cctx->ctx_skip == SKIP_YES)
{
cctx->ctx_skip = SKIP_UNKNOWN;
#ifdef FEAT_PROFILE
if (cctx->ctx_compile_type == CT_PROFILE)
// the previous block was skipped, need to profile this line
generate_instr(cctx, ISN_PROF_START);
#endif
if (cctx->ctx_compile_type == CT_DEBUG)
// the previous block was skipped, may want to debug this line
generate_instr_debug(cctx);
}
instr_count = instr->ga_len;
if (compile_expr1(&p, cctx, &ppconst) == FAIL)
{
clear_ppconst(&ppconst);
return NULL;
}
cctx->ctx_skip = save_skip;
if (!ends_excmd2(arg, skipwhite(p)))
{
clear_ppconst(&ppconst);
semsg(_(e_trailing_characters_str), p);
return NULL;
}
if (scope->se_skip_save == SKIP_YES)
clear_ppconst(&ppconst);
else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
{
int error = FALSE;
int v;
// The expression result is a constant.
v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
if (error)
{
clear_ppconst(&ppconst);
return NULL;
}
cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
clear_ppconst(&ppconst);
scope->se_u.se_if.is_if_label = -1;
}
else
{
// Not a constant, generate instructions for the expression.
cctx->ctx_skip = SKIP_UNKNOWN;
if (generate_ppconst(cctx, &ppconst) == FAIL)
return NULL;
if (bool_on_stack(cctx) == FAIL)
return NULL;
// CMDMOD_REV must come before the jump
generate_undo_cmdmods(cctx);
// "where" is set when ":elseif", "else" or ":endif" is found
scope->se_u.se_if.is_if_label = instr->ga_len;
generate_JUMP(cctx, JUMP_IF_FALSE, 0);
}
return p;
}
char_u *
compile_else(char_u *arg, cctx_T *cctx)
{
char_u *p = arg;
garray_T *instr = &cctx->ctx_instr;
isn_T *isn;
scope_T *scope = cctx->ctx_scope;
if (scope == NULL || scope->se_type != IF_SCOPE)
{
emsg(_(e_else_without_if));
return NULL;
}
unwind_locals(cctx, scope->se_local_count, TRUE);
if (!cctx->ctx_had_return)
scope->se_u.se_if.is_had_return = FALSE;
scope->se_u.se_if.is_seen_else = TRUE;
#ifdef FEAT_PROFILE
if (cctx->ctx_compile_type == CT_PROFILE)
{
if (cctx->ctx_skip == SKIP_NOT
&& ((isn_T *)instr->ga_data)[instr->ga_len - 1]
.isn_type == ISN_PROF_START)
// the previous block was executed, do not count "else" for
// profiling
--instr->ga_len;
if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
{
// the previous block was not executed, this one will, do count the
// "else" for profiling
cctx->ctx_skip = SKIP_NOT;
generate_instr(cctx, ISN_PROF_END);
generate_instr(cctx, ISN_PROF_START);
cctx->ctx_skip = SKIP_YES;
}
}
#endif
if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
{
// jump from previous block to the end, unless the else block is empty
if (cctx->ctx_skip == SKIP_UNKNOWN)
{
if (!cctx->ctx_had_return
&& compile_jump_to_end(&scope->se_u.se_if.is_end_label,
JUMP_ALWAYS, 0, cctx) == FAIL)
return NULL;
}
if (cctx->ctx_skip == SKIP_UNKNOWN)
{
if (scope->se_u.se_if.is_if_label >= 0)
{
// previous "if" or "elseif" jumps here
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
isn->isn_arg.jump.jump_where = instr->ga_len;
scope->se_u.se_if.is_if_label = -1;
}
}
if (cctx->ctx_skip != SKIP_UNKNOWN)
cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
}
return p;
}
char_u *
compile_endif(char_u *arg, cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
ifscope_T *ifscope;
garray_T *instr = &cctx->ctx_instr;
isn_T *isn;
if (misplaced_cmdmod(cctx))
return NULL;
if (scope == NULL || scope->se_type != IF_SCOPE)
{
emsg(_(e_endif_without_if));
return NULL;
}
ifscope = &scope->se_u.se_if;
unwind_locals(cctx, scope->se_local_count, TRUE);
if (!cctx->ctx_had_return)
ifscope->is_had_return = FALSE;
if (scope->se_u.se_if.is_if_label >= 0)
{
// previous "if" or "elseif" jumps here
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
isn->isn_arg.jump.jump_where = instr->ga_len;
}
// Fill in the "end" label in jumps at the end of the blocks.
compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
#ifdef FEAT_PROFILE
// even when skipping we count the endif as executed, unless the block it's
// in is skipped
if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
&& scope->se_skip_save != SKIP_YES)
{
cctx->ctx_skip = SKIP_NOT;
generate_instr(cctx, ISN_PROF_START);
}
#endif
cctx->ctx_skip = scope->se_skip_save;
// If all the blocks end in :return and there is an :else then the
// had_return flag is set.
cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
drop_scope(cctx);
return arg;
}
/*
* Save the info needed for ENDLOOP. Used by :for and :while.
*/
static void
compile_fill_loop_info(loop_info_T *loop_info, int funcref_idx, cctx_T *cctx)
{
loop_info->li_funcref_idx = funcref_idx;
loop_info->li_local_count = cctx->ctx_locals.ga_len;
loop_info->li_closure_count = cctx->ctx_closure_count;
}
/*
* Compile "for var in expr":
*
* Produces instructions:
* STORE -1 in loop-idx Set index to -1
* EVAL expr Result of "expr" on top of stack
* top: FOR loop-idx, end Increment index, use list on bottom of stack
* - if beyond end, jump to "end"
* - otherwise get item from list and push it
* - store ec_funcrefs in var "loop-idx" + 1
* STORE var Store item in "var"
* ... body ...
* ENDLOOP funcref-idx off count Only if closure uses local var
* JUMP top Jump back to repeat
* end: DROP Drop the result of "expr"
*
* Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
* UNPACK 2 Split item in 2
* STORE var1 Store item in "var1"
* STORE var2 Store item in "var2"
*/
char_u *
compile_for(char_u *arg_start, cctx_T *cctx)
{
char_u *arg;
char_u *arg_end;
char_u *name = NULL;
char_u *p;
char_u *wp;
int var_count = 0;
int var_list = FALSE;
int semicolon = FALSE;
size_t varlen;
garray_T *instr = &cctx->ctx_instr;
scope_T *scope;
forscope_T *forscope;
lvar_T *loop_lvar; // loop iteration variable
int loop_lvar_idx;
lvar_T *funcref_lvar;
int funcref_lvar_idx;
lvar_T *var_lvar; // variable for "var"
type_T *vartype;
type_T *item_type = &t_any;
int idx;
int prev_lnum = cctx->ctx_prev_lnum;
p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
if (p == NULL)
return NULL;
if (var_count == 0)
var_count = 1;
else
var_list = TRUE; // can also be a list of one variable
// consume "in"
wp = p;
if (may_get_next_line_error(wp, &p, cctx) == FAIL)
return NULL;
if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
{
if (*p == ':' && wp != p)
semsg(_(e_no_white_space_allowed_before_colon_str), p);
else
emsg(_(e_missing_in_after_for));
return NULL;
}
wp = p + 2;
if (may_get_next_line_error(wp, &p, cctx) == FAIL)
return NULL;
// Find the already generated ISN_DEBUG to get the line number for the
// instruction written below the ISN_FOR instruction.
if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
&& ((isn_T *)instr->ga_data)[instr->ga_len - 1]
.isn_type == ISN_DEBUG)
{
prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len - 1]
.isn_arg.debug.dbg_break_lnum;
}
scope = new_scope(cctx, FOR_SCOPE);
if (scope == NULL)
return NULL;
if (scope->se_loop_depth == MAX_LOOP_DEPTH)
{
emsg(_(e_loop_nesting_too_deep));
return NULL;
}
++scope->se_loop_depth;
forscope = &scope->se_u.se_for;
// Reserve a variable to store the loop iteration counter and initialize it
// to -1.
loop_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number);
if (loop_lvar == NULL)
{
drop_scope(cctx);
return NULL; // out of memory
}
// get the index before a following reserve_local() makes the lval invalid
loop_lvar_idx = loop_lvar->lv_idx;
generate_STORENR(cctx, loop_lvar_idx, -1);
// Reserve a variable to store ec_funcrefs.ga_len, used in ISN_ENDLOOP.
// The variable index is always the loop var index plus one.
// It is not used when no closures are encountered, we don't know yet.
funcref_lvar = reserve_local(cctx, (char_u *)"", 0, ASSIGN_VAR, &t_number);
if (funcref_lvar == NULL)
{
drop_scope(cctx);
return NULL; // out of memory
}
// get the index before a following reserve_local() makes the lval invalid
funcref_lvar_idx = funcref_lvar->lv_idx;
// compile "expr", it remains on the stack until "endfor"
arg = p;
if (compile_expr0(&arg, cctx) == FAIL)
{
drop_scope(cctx);
return NULL;
}
arg_end = arg;
if (cctx->ctx_skip != SKIP_YES)
{
// If we know the type of "var" and it is not a supported type we can
// give an error now.
vartype = get_type_on_stack(cctx, 0);
if (vartype->tt_type != VAR_LIST
&& vartype->tt_type != VAR_STRING
&& vartype->tt_type != VAR_BLOB
&& vartype->tt_type != VAR_ANY
&& vartype->tt_type != VAR_UNKNOWN)
{
semsg(_(e_for_loop_on_str_not_supported),
vartype_name(vartype->tt_type));
drop_scope(cctx);
return NULL;
}
if (vartype->tt_type == VAR_STRING)
item_type = &t_string;
else if (vartype->tt_type == VAR_BLOB)
item_type = &t_number;
else if (vartype->tt_type == VAR_LIST
&& vartype->tt_member->tt_type != VAR_ANY)
{
if (!var_list)
item_type = vartype->tt_member;
else if (vartype->tt_member->tt_type == VAR_LIST
&& vartype->tt_member->tt_member->tt_type != VAR_ANY)
item_type = vartype->tt_member->tt_member;
}
// CMDMOD_REV must come before the FOR instruction.
generate_undo_cmdmods(cctx);
// "for_end" is set when ":endfor" is found
forscope->fs_top_label = current_instr_idx(cctx);
if (cctx->ctx_compile_type == CT_DEBUG)
{
int save_prev_lnum = cctx->ctx_prev_lnum;
isn_T *isn;
// Add ISN_DEBUG here, before deciding to end the loop. There will
// be another ISN_DEBUG before the next instruction.
// Use the prev_lnum from the ISN_DEBUG instruction removed above.
// Increment the variable count so that the loop variable can be
// inspected.
cctx->ctx_prev_lnum = prev_lnum;
isn = generate_instr_debug(cctx);
++isn->isn_arg.debug.dbg_var_names_len;
cctx->ctx_prev_lnum = save_prev_lnum;
}
generate_FOR(cctx, loop_lvar_idx);
arg = arg_start;
if (var_list)
{
generate_UNPACK(cctx, var_count, semicolon);
arg = skipwhite(arg + 1); // skip white after '['
// drop the list item
--cctx->ctx_type_stack.ga_len;
// add type of the items
for (idx = 0; idx < var_count; ++idx)
{
type_T *type = (semicolon && idx == 0) ? vartype : item_type;
if (push_type_stack(cctx, type) == FAIL)
{
drop_scope(cctx);
return NULL;
}
}
}
for (idx = 0; idx < var_count; ++idx)
{
assign_dest_T dest = dest_local;
int opt_flags = 0;
int vimvaridx = -1;
type_T *type = &t_any;
type_T *lhs_type = &t_any;
where_T where = WHERE_INIT;
p = skip_var_one(arg, FALSE);
varlen = p - arg;