/
vim9compile.c
5614 lines (4956 loc) · 123 KB
/
vim9compile.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.
*/
/*
* vim9compile.c: :def and dealing with instructions
*/
#define USING_FLOAT_STUFF
#include "vim.h"
#if defined(FEAT_EVAL) || defined(PROTO)
#ifdef VMS
# include <float.h>
#endif
#define DEFINE_VIM9_GLOBALS
#include "vim9.h"
/*
* Chain of jump instructions where the end label needs to be set.
*/
typedef struct endlabel_S endlabel_T;
struct endlabel_S {
endlabel_T *el_next; // chain end_label locations
int el_end_label; // instruction idx where to set end
};
/*
* info specific for the scope of :if / elseif / else
*/
typedef struct {
int is_if_label; // instruction idx at IF or ELSEIF
endlabel_T *is_end_label; // instructions to set end label
} ifscope_T;
/*
* info specific for the scope of :while
*/
typedef struct {
int ws_top_label; // instruction idx at WHILE
endlabel_T *ws_end_label; // instructions to set end
} whilescope_T;
/*
* info specific for the scope of :for
*/
typedef struct {
int fs_top_label; // instruction idx at FOR
endlabel_T *fs_end_label; // break instructions
} forscope_T;
/*
* info specific for the scope of :try
*/
typedef struct {
int ts_try_label; // instruction idx at TRY
endlabel_T *ts_end_label; // jump to :finally or :endtry
int ts_catch_label; // instruction idx of last CATCH
int ts_caught_all; // "catch" without argument encountered
} tryscope_T;
typedef enum {
NO_SCOPE,
IF_SCOPE,
WHILE_SCOPE,
FOR_SCOPE,
TRY_SCOPE,
BLOCK_SCOPE
} scopetype_T;
/*
* Info for one scope, pointed to by "ctx_scope".
*/
typedef struct scope_S scope_T;
struct scope_S {
scope_T *se_outer; // scope containing this one
scopetype_T se_type;
int se_local_count; // ctx_locals.ga_len before scope
union {
ifscope_T se_if;
whilescope_T se_while;
forscope_T se_for;
tryscope_T se_try;
} se_u;
};
/*
* Entry for "ctx_locals". Used for arguments and local variables.
*/
typedef struct {
char_u *lv_name;
type_T *lv_type;
int lv_const; // when TRUE cannot be assigned to
int lv_arg; // when TRUE this is an argument
} lvar_T;
/*
* Context for compiling lines of Vim script.
* Stores info about the local variables and condition stack.
*/
struct cctx_S {
ufunc_T *ctx_ufunc; // current function
int ctx_lnum; // line number in current function
garray_T ctx_instr; // generated instructions
garray_T ctx_locals; // currently visible local variables
int ctx_max_local; // maximum number of locals at one time
garray_T ctx_imports; // imported items
int ctx_skip; // when TRUE skip commands, when FALSE skip
// commands after "else"
scope_T *ctx_scope; // current scope, NULL at toplevel
garray_T ctx_type_stack; // type of each item on the stack
garray_T *ctx_type_list; // space for adding types
};
static char e_var_notfound[] = N_("E1001: variable not found: %s");
static char e_syntax_at[] = N_("E1002: Syntax error at %s");
static int compile_expr1(char_u **arg, cctx_T *cctx);
static int compile_expr2(char_u **arg, cctx_T *cctx);
static int compile_expr3(char_u **arg, cctx_T *cctx);
static void delete_def_function_contents(dfunc_T *dfunc);
/*
* Lookup variable "name" in the local scope and return the index.
*/
static int
lookup_local(char_u *name, size_t len, cctx_T *cctx)
{
int idx;
if (len == 0)
return -1;
for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
{
lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
if (STRNCMP(name, lvar->lv_name, len) == 0
&& STRLEN(lvar->lv_name) == len)
return idx;
}
return -1;
}
/*
* Lookup an argument in the current function.
* Returns the argument index or -1 if not found.
*/
static int
lookup_arg(char_u *name, size_t len, cctx_T *cctx)
{
int idx;
if (len == 0)
return -1;
for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
{
char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
if (STRNCMP(name, arg, len) == 0 && STRLEN(arg) == len)
return idx;
}
return -1;
}
/*
* Lookup a vararg argument in the current function.
* Returns TRUE if there is a match.
*/
static int
lookup_vararg(char_u *name, size_t len, cctx_T *cctx)
{
char_u *va_name = cctx->ctx_ufunc->uf_va_name;
return len > 0 && va_name != NULL
&& STRNCMP(name, va_name, len) == 0 && STRLEN(va_name) == len;
}
/*
* Lookup a variable in the current script.
* Returns OK or FAIL.
*/
static int
lookup_script(char_u *name, size_t len)
{
int cc;
hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
dictitem_T *di;
cc = name[len];
name[len] = NUL;
di = find_var_in_ht(ht, 0, name, TRUE);
name[len] = cc;
return di == NULL ? FAIL: OK;
}
/*
* Check if "p[len]" is already defined, either in script "import_sid" or in
* compilation context "cctx".
* Return FAIL and give an error if it defined.
*/
int
check_defined(char_u *p, int len, cctx_T *cctx)
{
if (lookup_script(p, len) == OK
|| (cctx != NULL
&& (lookup_local(p, len, cctx) >= 0
|| find_imported(p, len, cctx) != NULL)))
{
semsg("E1073: imported name already defined: %s", p);
return FAIL;
}
return OK;
}
static type_T *
get_list_type(type_T *member_type, garray_T *type_list)
{
type_T *type;
// recognize commonly used types
if (member_type->tt_type == VAR_UNKNOWN)
return &t_list_any;
if (member_type->tt_type == VAR_VOID)
return &t_list_empty;
if (member_type->tt_type == VAR_BOOL)
return &t_list_bool;
if (member_type->tt_type == VAR_NUMBER)
return &t_list_number;
if (member_type->tt_type == VAR_STRING)
return &t_list_string;
// Not a common type, create a new entry.
if (ga_grow(type_list, 1) == FAIL)
return &t_any;
type = ((type_T *)type_list->ga_data) + type_list->ga_len;
++type_list->ga_len;
type->tt_type = VAR_LIST;
type->tt_member = member_type;
return type;
}
static type_T *
get_dict_type(type_T *member_type, garray_T *type_list)
{
type_T *type;
// recognize commonly used types
if (member_type->tt_type == VAR_UNKNOWN)
return &t_dict_any;
if (member_type->tt_type == VAR_VOID)
return &t_dict_empty;
if (member_type->tt_type == VAR_BOOL)
return &t_dict_bool;
if (member_type->tt_type == VAR_NUMBER)
return &t_dict_number;
if (member_type->tt_type == VAR_STRING)
return &t_dict_string;
// Not a common type, create a new entry.
if (ga_grow(type_list, 1) == FAIL)
return &t_any;
type = ((type_T *)type_list->ga_data) + type_list->ga_len;
++type_list->ga_len;
type->tt_type = VAR_DICT;
type->tt_member = member_type;
return type;
}
/////////////////////////////////////////////////////////////////////
// Following generate_ functions expect the caller to call ga_grow().
#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
/*
* Generate an instruction without arguments.
* Returns a pointer to the new instruction, NULL if failed.
*/
static isn_T *
generate_instr(cctx_T *cctx, isntype_T isn_type)
{
garray_T *instr = &cctx->ctx_instr;
isn_T *isn;
RETURN_NULL_IF_SKIP(cctx);
if (ga_grow(instr, 1) == FAIL)
return NULL;
isn = ((isn_T *)instr->ga_data) + instr->ga_len;
isn->isn_type = isn_type;
isn->isn_lnum = cctx->ctx_lnum + 1;
++instr->ga_len;
return isn;
}
/*
* Generate an instruction without arguments.
* "drop" will be removed from the stack.
* Returns a pointer to the new instruction, NULL if failed.
*/
static isn_T *
generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
{
garray_T *stack = &cctx->ctx_type_stack;
RETURN_NULL_IF_SKIP(cctx);
stack->ga_len -= drop;
return generate_instr(cctx, isn_type);
}
/*
* Generate instruction "isn_type" and put "type" on the type stack.
*/
static isn_T *
generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
if ((isn = generate_instr(cctx, isn_type)) == NULL)
return NULL;
if (ga_grow(stack, 1) == FAIL)
return NULL;
((type_T **)stack->ga_data)[stack->ga_len] = type;
++stack->ga_len;
return isn;
}
/*
* If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
*/
static int
may_generate_2STRING(int offset, cctx_T *cctx)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
if ((*type)->tt_type == VAR_STRING)
return OK;
*type = &t_string;
if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
return FAIL;
isn->isn_arg.number = offset;
return OK;
}
static int
check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
{
if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_UNKNOWN)
&& (type2 == VAR_NUMBER || type2 == VAR_FLOAT
|| type2 == VAR_UNKNOWN)))
{
if (*op == '+')
emsg(_("E1035: wrong argument type for +"));
else
semsg(_("E1036: %c requires number or float arguments"), *op);
return FAIL;
}
return OK;
}
/*
* Generate an instruction with two arguments. The instruction depends on the
* type of the arguments.
*/
static int
generate_two_op(cctx_T *cctx, char_u *op)
{
garray_T *stack = &cctx->ctx_type_stack;
type_T *type1;
type_T *type2;
vartype_T vartype;
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
// Get the known type of the two items on the stack. If they are matching
// use a type-specific instruction. Otherwise fall back to runtime type
// checking.
type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
vartype = VAR_UNKNOWN;
if (type1->tt_type == type2->tt_type
&& (type1->tt_type == VAR_NUMBER
|| type1->tt_type == VAR_LIST
#ifdef FEAT_FLOAT
|| type1->tt_type == VAR_FLOAT
#endif
|| type1->tt_type == VAR_BLOB))
vartype = type1->tt_type;
switch (*op)
{
case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
&& type1->tt_type != VAR_UNKNOWN
&& type2->tt_type != VAR_UNKNOWN
&& check_number_or_float(
type1->tt_type, type2->tt_type, op) == FAIL)
return FAIL;
isn = generate_instr_drop(cctx,
vartype == VAR_NUMBER ? ISN_OPNR
: vartype == VAR_LIST ? ISN_ADDLIST
: vartype == VAR_BLOB ? ISN_ADDBLOB
#ifdef FEAT_FLOAT
: vartype == VAR_FLOAT ? ISN_OPFLOAT
#endif
: ISN_OPANY, 1);
if (isn != NULL)
isn->isn_arg.op.op_type = EXPR_ADD;
break;
case '-':
case '*':
case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
op) == FAIL)
return FAIL;
if (vartype == VAR_NUMBER)
isn = generate_instr_drop(cctx, ISN_OPNR, 1);
#ifdef FEAT_FLOAT
else if (vartype == VAR_FLOAT)
isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
#endif
else
isn = generate_instr_drop(cctx, ISN_OPANY, 1);
if (isn != NULL)
isn->isn_arg.op.op_type = *op == '*'
? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
break;
case '%': if ((type1->tt_type != VAR_UNKNOWN
&& type1->tt_type != VAR_NUMBER)
|| (type2->tt_type != VAR_UNKNOWN
&& type2->tt_type != VAR_NUMBER))
{
emsg(_("E1035: % requires number arguments"));
return FAIL;
}
isn = generate_instr_drop(cctx,
vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
if (isn != NULL)
isn->isn_arg.op.op_type = EXPR_REM;
break;
}
// correct type of result
if (vartype == VAR_UNKNOWN)
{
type_T *type = &t_any;
#ifdef FEAT_FLOAT
// float+number and number+float results in float
if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
&& (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
type = &t_float;
#endif
((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
}
return OK;
}
/*
* Generate an ISN_COMPARE* instruction with a boolean result.
*/
static int
generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
{
isntype_T isntype = ISN_DROP;
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
vartype_T type1;
vartype_T type2;
RETURN_OK_IF_SKIP(cctx);
// Get the known type of the two items on the stack. If they are matching
// use a type-specific instruction. Otherwise fall back to runtime type
// checking.
type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
if (type1 == type2)
{
switch (type1)
{
case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
case VAR_NUMBER: isntype = ISN_COMPARENR; break;
case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
case VAR_STRING: isntype = ISN_COMPARESTRING; break;
case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
case VAR_LIST: isntype = ISN_COMPARELIST; break;
case VAR_DICT: isntype = ISN_COMPAREDICT; break;
case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
case VAR_PARTIAL: isntype = ISN_COMPAREPARTIAL; break;
default: isntype = ISN_COMPAREANY; break;
}
}
else if (type1 == VAR_UNKNOWN || type2 == VAR_UNKNOWN
|| ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
&& (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
isntype = ISN_COMPAREANY;
if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
&& (isntype == ISN_COMPAREBOOL
|| isntype == ISN_COMPARESPECIAL
|| isntype == ISN_COMPARENR
|| isntype == ISN_COMPAREFLOAT))
{
semsg(_("E1037: Cannot use \"%s\" with %s"),
exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
return FAIL;
}
if (isntype == ISN_DROP
|| ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
&& (type1 == VAR_BOOL || type1 == VAR_SPECIAL
|| type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
|| ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
&& exptype != EXPR_IS && exptype != EXPR_ISNOT
&& (type1 == VAR_BLOB || type2 == VAR_BLOB
|| type1 == VAR_LIST || type2 == VAR_LIST))))
{
semsg(_("E1072: Cannot compare %s with %s"),
vartype_name(type1), vartype_name(type2));
return FAIL;
}
if ((isn = generate_instr(cctx, isntype)) == NULL)
return FAIL;
isn->isn_arg.op.op_type = exptype;
isn->isn_arg.op.op_ic = ic;
// takes two arguments, puts one bool back
if (stack->ga_len >= 2)
{
--stack->ga_len;
((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
}
return OK;
}
/*
* Generate an ISN_2BOOL instruction.
*/
static int
generate_2BOOL(cctx_T *cctx, int invert)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
return FAIL;
isn->isn_arg.number = invert;
// type becomes bool
((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
return OK;
}
static int
generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
return FAIL;
isn->isn_arg.type.ct_type = vartype->tt_type; // TODO: whole type
isn->isn_arg.type.ct_off = offset;
// type becomes vartype
((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype;
return OK;
}
/*
* Generate an ISN_PUSHNR instruction.
*/
static int
generate_PUSHNR(cctx_T *cctx, varnumber_T number)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
return FAIL;
isn->isn_arg.number = number;
return OK;
}
/*
* Generate an ISN_PUSHBOOL instruction.
*/
static int
generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
return FAIL;
isn->isn_arg.number = number;
return OK;
}
/*
* Generate an ISN_PUSHSPEC instruction.
*/
static int
generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
return FAIL;
isn->isn_arg.number = number;
return OK;
}
#ifdef FEAT_FLOAT
/*
* Generate an ISN_PUSHF instruction.
*/
static int
generate_PUSHF(cctx_T *cctx, float_T fnumber)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
return FAIL;
isn->isn_arg.fnumber = fnumber;
return OK;
}
#endif
/*
* Generate an ISN_PUSHS instruction.
* Consumes "str".
*/
static int
generate_PUSHS(cctx_T *cctx, char_u *str)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
return FAIL;
isn->isn_arg.string = str;
return OK;
}
/*
* Generate an ISN_PUSHCHANNEL instruction.
* Consumes "channel".
*/
static int
generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
return FAIL;
isn->isn_arg.channel = channel;
return OK;
}
/*
* Generate an ISN_PUSHJOB instruction.
* Consumes "job".
*/
static int
generate_PUSHJOB(cctx_T *cctx, job_T *job)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
return FAIL;
isn->isn_arg.job = job;
return OK;
}
/*
* Generate an ISN_PUSHBLOB instruction.
* Consumes "blob".
*/
static int
generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
return FAIL;
isn->isn_arg.blob = blob;
return OK;
}
/*
* Generate an ISN_PUSHFUNC instruction with name "name".
* Consumes "name".
*/
static int
generate_PUSHFUNC(cctx_T *cctx, char_u *name)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, &t_func_void)) == NULL)
return FAIL;
isn->isn_arg.string = name;
return OK;
}
/*
* Generate an ISN_PUSHPARTIAL instruction with partial "part".
* Consumes "name".
*/
static int
generate_PUSHPARTIAL(cctx_T *cctx, partial_T *part)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHPARTIAL,
&t_partial_any)) == NULL)
return FAIL;
isn->isn_arg.partial = part;
return OK;
}
/*
* Generate an ISN_STORE instruction.
*/
static int
generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
return FAIL;
if (name != NULL)
isn->isn_arg.string = vim_strsave(name);
else
isn->isn_arg.number = idx;
return OK;
}
/*
* Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
*/
static int
generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
return FAIL;
isn->isn_arg.storenr.stnr_idx = idx;
isn->isn_arg.storenr.stnr_val = value;
return OK;
}
/*
* Generate an ISN_STOREOPT instruction
*/
static int
generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
return FAIL;
isn->isn_arg.storeopt.so_name = vim_strsave(name);
isn->isn_arg.storeopt.so_flags = opt_flags;
return OK;
}
/*
* Generate an ISN_LOAD or similar instruction.
*/
static int
generate_LOAD(
cctx_T *cctx,
isntype_T isn_type,
int idx,
char_u *name,
type_T *type)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
return FAIL;
if (name != NULL)
isn->isn_arg.string = vim_strsave(name);
else
isn->isn_arg.number = idx;
return OK;
}
/*
* Generate an ISN_LOADV instruction.
*/
static int
generate_LOADV(
cctx_T *cctx,
char_u *name,
int error)
{
// load v:var
int vidx = find_vim_var(name);
RETURN_OK_IF_SKIP(cctx);
if (vidx < 0)
{
if (error)
semsg(_(e_var_notfound), name);
return FAIL;
}
// TODO: get actual type
return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, &t_any);
}
/*
* Generate an ISN_LOADS instruction.
*/
static int
generate_OLDSCRIPT(
cctx_T *cctx,
isntype_T isn_type,
char_u *name,
int sid,
type_T *type)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if (isn_type == ISN_LOADS)
isn = generate_instr_type(cctx, isn_type, type);
else
isn = generate_instr_drop(cctx, isn_type, 1);
if (isn == NULL)
return FAIL;
isn->isn_arg.loadstore.ls_name = vim_strsave(name);
isn->isn_arg.loadstore.ls_sid = sid;
return OK;
}
/*
* Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
*/
static int
generate_VIM9SCRIPT(
cctx_T *cctx,
isntype_T isn_type,
int sid,
int idx,
type_T *type)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if (isn_type == ISN_LOADSCRIPT)
isn = generate_instr_type(cctx, isn_type, type);
else
isn = generate_instr_drop(cctx, isn_type, 1);
if (isn == NULL)
return FAIL;
isn->isn_arg.script.script_sid = sid;
isn->isn_arg.script.script_idx = idx;
return OK;
}
/*
* Generate an ISN_NEWLIST instruction.
*/
static int
generate_NEWLIST(cctx_T *cctx, int count)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
garray_T *type_list = cctx->ctx_type_list;
type_T *type;
type_T *member;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
return FAIL;
isn->isn_arg.number = count;
// drop the value types
stack->ga_len -= count;
// Use the first value type for the list member type. Use "void" for an
// empty list.
if (count > 0)
member = ((type_T **)stack->ga_data)[stack->ga_len];
else
member = &t_void;
type = get_list_type(member, type_list);
// add the list type to the type stack
if (ga_grow(stack, 1) == FAIL)
return FAIL;
((type_T **)stack->ga_data)[stack->ga_len] = type;
++stack->ga_len;
return OK;
}
/*
* Generate an ISN_NEWDICT instruction.
*/
static int
generate_NEWDICT(cctx_T *cctx, int count)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
garray_T *type_list = cctx->ctx_type_list;
type_T *type;
type_T *member;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
return FAIL;
isn->isn_arg.number = count;
// drop the key and value types
stack->ga_len -= 2 * count;
// Use the first value type for the list member type. Use "void" for an
// empty dict.
if (count > 0)
member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
else
member = &t_void;
type = get_dict_type(member, type_list);
// add the dict type to the type stack
if (ga_grow(stack, 1) == FAIL)
return FAIL;
((type_T **)stack->ga_data)[stack->ga_len] = type;
++stack->ga_len;
return OK;
}
/*
* Generate an ISN_FUNCREF instruction.
*/
static int
generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;