-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvim9instr.c
2754 lines (2428 loc) · 66.7 KB
/
vim9instr.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.
*/
/*
* vim9instr.c: Dealing with instructions 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
/////////////////////////////////////////////////////////////////////
// Following generate_ functions expect the caller to call ga_grow().
#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
/*
* Generate an instruction without arguments.
* Returns a pointer to the new instruction, NULL if failed.
*/
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_FAILS(instr, 1))
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.
*/
isn_T *
generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
{
RETURN_NULL_IF_SKIP(cctx);
cctx->ctx_type_stack.ga_len -= drop;
return generate_instr(cctx, isn_type);
}
/*
* Generate instruction "isn_type" and put "type" on the type stack,
* use "decl_type" for the declared type.
*/
static isn_T *
generate_instr_type2(
cctx_T *cctx,
isntype_T isn_type,
type_T *type,
type_T *decl_type)
{
isn_T *isn;
if ((isn = generate_instr(cctx, isn_type)) == NULL)
return NULL;
if (push_type_stack2(cctx, type == NULL ? &t_any : type,
decl_type == NULL ? &t_any : decl_type) == FAIL)
return NULL;
return isn;
}
/*
* Generate instruction "isn_type" and put "type" on the type stack.
* Uses "any" for the declared type, which works for constants. For declared
* variables use generate_instr_type2().
*/
isn_T *
generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
{
return generate_instr_type2(cctx, isn_type, type, &t_any);
}
/*
* Generate an ISN_DEBUG instruction.
*/
isn_T *
generate_instr_debug(cctx_T *cctx)
{
isn_T *isn;
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ cctx->ctx_ufunc->uf_dfunc_idx;
if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
return NULL;
isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
return isn;
}
/*
* Generate an ISN_CONSTRUCT instruction.
* The object will have "size" members.
*/
int
generate_CONSTRUCT(cctx_T *cctx, class_T *cl)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_CONSTRUCT)) == NULL)
return FAIL;
isn->isn_arg.construct.construct_size = sizeof(object_T)
+ cl->class_obj_member_count * sizeof(typval_T);
isn->isn_arg.construct.construct_class = cl;
return OK;
}
/*
* Generate ISN_GET_OBJ_MEMBER - access member of object at bottom of stack by
* index.
*/
int
generate_GET_OBJ_MEMBER(cctx_T *cctx, int idx, type_T *type)
{
RETURN_OK_IF_SKIP(cctx);
// drop the object type
isn_T *isn = generate_instr_drop(cctx, ISN_GET_OBJ_MEMBER, 1);
if (isn == NULL)
return FAIL;
isn->isn_arg.number = idx;
return push_type_stack2(cctx, type, &t_any);
}
/*
* Generate ISN_GET_ITF_MEMBER - access member of interface at bottom of stack
* by index.
*/
int
generate_GET_ITF_MEMBER(cctx_T *cctx, class_T *itf, int idx, type_T *type)
{
RETURN_OK_IF_SKIP(cctx);
// drop the object type
isn_T *isn = generate_instr_drop(cctx, ISN_GET_ITF_MEMBER, 1);
if (isn == NULL)
return FAIL;
isn->isn_arg.classmember.cm_class = itf;
++itf->class_refcount;
isn->isn_arg.classmember.cm_idx = idx;
return push_type_stack2(cctx, type, &t_any);
}
/*
* Generate ISN_STORE_THIS - store value in member of "this" object with member
* index "idx".
*/
int
generate_STORE_THIS(cctx_T *cctx, int idx)
{
RETURN_OK_IF_SKIP(cctx);
// drop the value type
isn_T *isn = generate_instr_drop(cctx, ISN_STORE_THIS, 1);
if (isn == NULL)
return FAIL;
isn->isn_arg.number = idx;
return OK;
}
/*
* If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
* But only for simple types.
* When "tolerant" is TRUE convert most types to string, e.g. a List.
*/
int
may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
{
isn_T *isn;
isntype_T isntype = ISN_2STRING;
type_T *type;
RETURN_OK_IF_SKIP(cctx);
type = get_type_on_stack(cctx, -1 - offset);
switch (type->tt_type)
{
// nothing to be done
case VAR_STRING: return OK;
// conversion possible
case VAR_SPECIAL:
case VAR_BOOL:
case VAR_NUMBER:
case VAR_FLOAT:
break;
// conversion possible (with runtime check)
case VAR_ANY:
case VAR_UNKNOWN:
isntype = ISN_2STRING_ANY;
break;
// conversion possible when tolerant
case VAR_LIST:
if (tolerant)
{
isntype = ISN_2STRING_ANY;
break;
}
// FALLTHROUGH
// conversion not possible
case VAR_VOID:
case VAR_BLOB:
case VAR_FUNC:
case VAR_PARTIAL:
case VAR_DICT:
case VAR_JOB:
case VAR_CHANNEL:
case VAR_INSTR:
case VAR_CLASS:
case VAR_OBJECT:
to_string_error(type->tt_type);
return FAIL;
}
set_type_on_stack(cctx, &t_string, -1 - offset);
if ((isn = generate_instr(cctx, isntype)) == NULL)
return FAIL;
isn->isn_arg.tostring.offset = offset;
isn->isn_arg.tostring.tolerant = tolerant;
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_ANY || type1 == VAR_UNKNOWN)
&& (type2 == VAR_NUMBER || type2 == VAR_FLOAT
|| type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
{
if (*op == '+')
emsg(_(e_wrong_argument_type_for_plus));
else
semsg(_(e_char_requires_number_or_float_arguments), *op);
return FAIL;
}
return OK;
}
/*
* Generate instruction for "+". For a list this creates a new list.
*/
int
generate_add_instr(
cctx_T *cctx,
vartype_T vartype,
type_T *type1,
type_T *type2,
exprtype_T expr_type)
{
isn_T *isn = generate_instr_drop(cctx,
vartype == VAR_NUMBER ? ISN_OPNR
: vartype == VAR_LIST ? ISN_ADDLIST
: vartype == VAR_BLOB ? ISN_ADDBLOB
: vartype == VAR_FLOAT ? ISN_OPFLOAT
: ISN_OPANY, 1);
if (vartype != VAR_LIST && vartype != VAR_BLOB
&& type1->tt_type != VAR_ANY
&& type1->tt_type != VAR_UNKNOWN
&& type2->tt_type != VAR_ANY
&& type2->tt_type != VAR_UNKNOWN
&& check_number_or_float(
type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
return FAIL;
if (isn != NULL)
{
if (isn->isn_type == ISN_ADDLIST)
isn->isn_arg.op.op_type = expr_type;
else
isn->isn_arg.op.op_type = EXPR_ADD;
}
// When concatenating two lists with different member types the member type
// becomes "any".
if (vartype == VAR_LIST
&& type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
&& type1->tt_member != type2->tt_member)
set_type_on_stack(cctx, &t_list_any, 0);
return isn == NULL ? FAIL : OK;
}
/*
* Get the type to use for an instruction for an operation on "type1" and
* "type2". If they are matching use a type-specific instruction. Otherwise
* fall back to runtime type checking.
*/
vartype_T
operator_type(type_T *type1, type_T *type2)
{
if (type1->tt_type == type2->tt_type
&& (type1->tt_type == VAR_NUMBER
|| type1->tt_type == VAR_LIST
|| type1->tt_type == VAR_FLOAT
|| type1->tt_type == VAR_BLOB))
return type1->tt_type;
return VAR_ANY;
}
/*
* Generate an instruction with two arguments. The instruction depends on the
* type of the arguments.
*/
int
generate_two_op(cctx_T *cctx, char_u *op)
{
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.
type1 = get_type_on_stack(cctx, 1);
type2 = get_type_on_stack(cctx, 0);
vartype = operator_type(type1, type2);
switch (*op)
{
case '+':
if (generate_add_instr(cctx, vartype, type1, type2,
EXPR_COPY) == FAIL)
return FAIL;
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);
else if (vartype == VAR_FLOAT)
isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
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_ANY
&& type1->tt_type != VAR_UNKNOWN
&& type1->tt_type != VAR_NUMBER)
|| (type2->tt_type != VAR_ANY
&& type2->tt_type != VAR_UNKNOWN
&& type2->tt_type != VAR_NUMBER))
{
emsg(_(e_percent_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_ANY)
{
type_T *type = &t_any;
// 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;
set_type_on_stack(cctx, type, 0);
}
return OK;
}
/*
* Get the instruction to use for comparing two values with specified types.
* Either "tv1" and "tv2" are passed or "type1" and "type2".
* Return ISN_DROP when failed.
*/
static isntype_T
get_compare_isn(
exprtype_T exprtype,
typval_T *tv1,
typval_T *tv2,
type_T *type1,
type_T *type2)
{
isntype_T isntype = ISN_DROP;
vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
if (vartype1 == vartype2)
{
switch (vartype1)
{
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_CLASS: isntype = ISN_COMPARECLASS; break;
case VAR_OBJECT: isntype = ISN_COMPAREOBJECT; break;
default: isntype = ISN_COMPAREANY; break;
}
}
else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
|| ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
&& (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
|| (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
|| (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
isntype = ISN_COMPAREANY;
else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
{
if ((vartype1 == VAR_SPECIAL
&& (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
: type1 == &t_none)
&& vartype2 != VAR_STRING)
|| (vartype2 == VAR_SPECIAL
&& (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
: type2 == &t_none)
&& vartype1 != VAR_STRING))
{
semsg(_(e_cannot_compare_str_with_str),
vartype_name(vartype1), vartype_name(vartype2));
return ISN_DROP;
}
// although comparing null with number, float or bool is not useful, we
// allow it
isntype = ISN_COMPARENULL;
}
if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
&& (isntype == ISN_COMPAREBOOL
|| isntype == ISN_COMPARESPECIAL
|| isntype == ISN_COMPARENR
|| isntype == ISN_COMPAREFLOAT))
{
semsg(_(e_cannot_use_str_with_str),
exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
return ISN_DROP;
}
if (!(exprtype == EXPR_IS || exprtype == EXPR_ISNOT
|| exprtype == EXPR_EQUAL || exprtype == EXPR_NEQUAL)
&& (isntype == ISN_COMPAREOBJECT || isntype == ISN_COMPARECLASS))
{
semsg(_(e_invalid_operation_for_str), vartype_name(vartype1));
return ISN_DROP;
}
if (isntype == ISN_DROP
|| (isntype != ISN_COMPARENULL
&& (((exprtype != EXPR_EQUAL
&& exprtype != EXPR_NEQUAL
&& (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
|| vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
|| ((exprtype != EXPR_EQUAL
&& exprtype != EXPR_NEQUAL
&& exprtype != EXPR_IS
&& exprtype != EXPR_ISNOT
&& (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
|| vartype1 == VAR_LIST || vartype2 == VAR_LIST))))))
{
semsg(_(e_cannot_compare_str_with_str),
vartype_name(vartype1), vartype_name(vartype2));
return ISN_DROP;
}
return isntype;
}
int
check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
{
if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
return FAIL;
return OK;
}
/*
* Generate an ISN_COMPARE* instruction with a boolean result.
*/
int
generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
{
isntype_T isntype;
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
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.
isntype = get_compare_isn(exprtype, NULL, NULL,
get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
if (isntype == ISN_DROP)
return FAIL;
if ((isn = generate_instr(cctx, isntype)) == NULL)
return FAIL;
isn->isn_arg.op.op_type = exprtype;
isn->isn_arg.op.op_ic = ic;
// takes two arguments, puts one bool back
--stack->ga_len;
set_type_on_stack(cctx, &t_bool, 0);
return OK;
}
/*
* Generate an ISN_CONCAT instruction.
* "count" is the number of stack elements to join together and it must be
* greater or equal to one.
* The caller ensures all the "count" elements on the stack have the right type.
*/
int
generate_CONCAT(cctx_T *cctx, int count)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
return FAIL;
isn->isn_arg.number = count;
// drop the argument types
stack->ga_len -= count - 1;
return OK;
}
/*
* Generate an ISN_2BOOL instruction.
* "offset" is the offset in the type stack.
*/
int
generate_2BOOL(cctx_T *cctx, int invert, int offset)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
return FAIL;
isn->isn_arg.tobool.invert = invert;
isn->isn_arg.tobool.offset = offset;
// type becomes bool
set_type_on_stack(cctx, &t_bool, -1 - offset);
return OK;
}
/*
* Generate an ISN_COND2BOOL instruction.
*/
int
generate_COND2BOOL(cctx_T *cctx)
{
RETURN_OK_IF_SKIP(cctx);
if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
return FAIL;
// type becomes bool
set_type_on_stack(cctx, &t_bool, 0);
return OK;
}
int
generate_TYPECHECK(
cctx_T *cctx,
type_T *expected,
int number_ok, // add TTFLAG_NUMBER_OK flag
int offset,
int is_var,
int argidx)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
return FAIL;
type_T *tt;
if (expected->tt_type == VAR_FLOAT && number_ok)
{
// always allocate, also for static types
tt = ALLOC_ONE(type_T);
if (tt != NULL)
{
*tt = *expected;
tt->tt_flags &= ~TTFLAG_STATIC;
tt->tt_flags |= TTFLAG_NUMBER_OK;
}
}
else
tt = alloc_type(expected);
isn->isn_arg.type.ct_type = tt;
isn->isn_arg.type.ct_off = (int8_T)offset;
isn->isn_arg.type.ct_is_var = is_var;
isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
// type becomes expected
set_type_on_stack(cctx, expected, -1 - offset);
return OK;
}
int
generate_SETTYPE(
cctx_T *cctx,
type_T *expected)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
return FAIL;
isn->isn_arg.type.ct_type = alloc_type(expected);
return OK;
}
/*
* Generate an ISN_PUSHOBJ instruction. Object is always NULL.
*/
static int
generate_PUSHOBJ(cctx_T *cctx)
{
RETURN_OK_IF_SKIP(cctx);
if (generate_instr_type(cctx, ISN_PUSHOBJ, &t_any) == NULL)
return FAIL;
return OK;
}
/*
* Generate an ISN_PUSHCLASS instruction. "class" can be NULL.
*/
static int
generate_PUSHCLASS(cctx_T *cctx, class_T *class)
{
RETURN_OK_IF_SKIP(cctx);
isn_T *isn = generate_instr_type(cctx, ISN_PUSHCLASS,
class == NULL ? &t_any : &class->class_type);
if (isn == NULL)
return FAIL;
isn->isn_arg.classarg = class;
if (class != NULL)
++class->class_refcount;
return OK;
}
/*
* Generate a PUSH instruction for "tv".
* "tv" will be consumed or cleared.
*/
int
generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
{
switch (tv->v_type)
{
case VAR_BOOL:
generate_PUSHBOOL(cctx, tv->vval.v_number);
break;
case VAR_SPECIAL:
generate_PUSHSPEC(cctx, tv->vval.v_number);
break;
case VAR_NUMBER:
generate_PUSHNR(cctx, tv->vval.v_number);
break;
case VAR_FLOAT:
generate_PUSHF(cctx, tv->vval.v_float);
break;
case VAR_BLOB:
generate_PUSHBLOB(cctx, tv->vval.v_blob);
tv->vval.v_blob = NULL;
break;
case VAR_LIST:
if (tv->vval.v_list != NULL)
iemsg("non-empty list constant not supported");
generate_NEWLIST(cctx, 0, TRUE);
break;
case VAR_DICT:
if (tv->vval.v_dict != NULL)
iemsg("non-empty dict constant not supported");
generate_NEWDICT(cctx, 0, TRUE);
break;
#ifdef FEAT_JOB_CHANNEL
case VAR_JOB:
if (tv->vval.v_job != NULL)
iemsg("non-null job constant not supported");
generate_PUSHJOB(cctx);
break;
case VAR_CHANNEL:
if (tv->vval.v_channel != NULL)
iemsg("non-null channel constant not supported");
generate_PUSHCHANNEL(cctx);
break;
#endif
case VAR_FUNC:
if (tv->vval.v_string != NULL)
iemsg("non-null function constant not supported");
generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
break;
case VAR_PARTIAL:
if (tv->vval.v_partial != NULL)
iemsg("non-null partial constant not supported");
if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
== NULL)
return FAIL;
break;
case VAR_STRING:
generate_PUSHS(cctx, &tv->vval.v_string);
tv->vval.v_string = NULL;
break;
case VAR_OBJECT:
if (tv->vval.v_object != NULL)
{
emsg(_(e_cannot_use_non_null_object));
return FAIL;
}
generate_PUSHOBJ(cctx);
break;
case VAR_CLASS:
generate_PUSHCLASS(cctx, tv->vval.v_class);
break;
default:
siemsg("constant type %d not supported", tv->v_type);
clear_tv(tv);
return FAIL;
}
tv->v_type = VAR_UNKNOWN;
return OK;
}
/*
* Generate an ISN_PUSHNR instruction.
*/
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;
if (number == 0 || number == 1)
// A 0 or 1 number can also be used as a bool.
set_type_on_stack(cctx, &t_number_bool, 0);
return OK;
}
/*
* Generate an ISN_PUSHBOOL instruction.
*/
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.
*/
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,
number == VVAL_NULL ? &t_null : &t_none)) == NULL)
return FAIL;
isn->isn_arg.number = number;
return OK;
}
/*
* Generate an ISN_PUSHF instruction.
*/
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;
}
/*
* Generate an ISN_PUSHS instruction.
* Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
* Note that if "str" is used in the instruction OK is returned and "*str" is
* not set to NULL.
*/
int
generate_PUSHS(cctx_T *cctx, char_u **str)
{
isn_T *isn;
int ret = OK;
if (cctx->ctx_skip != SKIP_YES)
{
if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
ret = FAIL;
else
{
isn->isn_arg.string = str == NULL ? NULL : *str;
return OK;
}
}
if (str != NULL)
VIM_CLEAR(*str);
return ret;
}
/*
* Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
*/
int
generate_PUSHCHANNEL(cctx_T *cctx)
{
RETURN_OK_IF_SKIP(cctx);
#ifdef FEAT_JOB_CHANNEL
if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
return FAIL;
return OK;
#else
emsg(_(e_channel_job_feature_not_available));
return FAIL;
#endif
}
/*
* Generate an ISN_PUSHJOB instruction. Job is always NULL.
*/
int
generate_PUSHJOB(cctx_T *cctx)
{
RETURN_OK_IF_SKIP(cctx);
#ifdef FEAT_JOB_CHANNEL
if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
return FAIL;
return OK;
#else
emsg(_(e_channel_job_feature_not_available));
return FAIL;
#endif
}
/*
* Generate an ISN_PUSHBLOB instruction.
* Consumes "blob".
*/
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".
* When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
* autoload.
*/
int
generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
{
isn_T *isn;
char_u *funcname;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
return FAIL;
if (name == NULL)
funcname = NULL;
else if (!may_prefix
|| *name == K_SPECIAL // script-local
|| vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
funcname = vim_strsave(name);
else
{
funcname = alloc(STRLEN(name) + 3);
if (funcname != NULL)
{
STRCPY(funcname, "g:");
STRCPY(funcname + 2, name);
}
}
isn->isn_arg.string = funcname;
return OK;
}
/*
* Generate an ISN_AUTOLOAD instruction.
*/
int
generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
return FAIL;
isn->isn_arg.string = vim_strsave(name);
if (isn->isn_arg.string == NULL)
return FAIL;
return OK;
}
/*
* Generate an ISN_GETITEM instruction with "index".
* "with_op" is TRUE for "+=" and other operators, the stack has the current
* value below the list with values.
* Caller must check the type is a list.
*/
int
generate_GETITEM(cctx_T *cctx, int index, int with_op)
{
isn_T *isn;
type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
type_T *item_type = &t_any;
RETURN_OK_IF_SKIP(cctx);
item_type = type->tt_member;
if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
return FAIL;
isn->isn_arg.getitem.gi_index = index;
isn->isn_arg.getitem.gi_with_op = with_op;
// add the item type to the type stack
return push_type_stack(cctx, item_type);
}
/*
* Generate an ISN_SLICE instruction with "count".
*/
int
generate_SLICE(cctx_T *cctx, int count)