blob: 49936c37bba2e0b451757a1b1e953adaacff2d0f [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 * vim9instr.c: Dealing with instructions 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/////////////////////////////////////////////////////////////////////
26// Following generate_ functions expect the caller to call ga_grow().
27
28#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
29#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
30
31/*
32 * Generate an instruction without arguments.
33 * Returns a pointer to the new instruction, NULL if failed.
34 */
35 isn_T *
36generate_instr(cctx_T *cctx, isntype_T isn_type)
37{
38 garray_T *instr = &cctx->ctx_instr;
39 isn_T *isn;
40
41 RETURN_NULL_IF_SKIP(cctx);
42 if (GA_GROW_FAILS(instr, 1))
43 return NULL;
44 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
45 isn->isn_type = isn_type;
46 isn->isn_lnum = cctx->ctx_lnum + 1;
47 ++instr->ga_len;
48
49 return isn;
50}
51
52/*
53 * Generate an instruction without arguments.
54 * "drop" will be removed from the stack.
55 * Returns a pointer to the new instruction, NULL if failed.
56 */
57 isn_T *
58generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
59{
60 garray_T *stack = &cctx->ctx_type_stack;
61
62 RETURN_NULL_IF_SKIP(cctx);
63 stack->ga_len -= drop;
64 return generate_instr(cctx, isn_type);
65}
66
67/*
68 * Generate instruction "isn_type" and put "type" on the type stack.
69 */
70 isn_T *
71generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
72{
73 isn_T *isn;
74 garray_T *stack = &cctx->ctx_type_stack;
75
76 if ((isn = generate_instr(cctx, isn_type)) == NULL)
77 return NULL;
78
79 if (GA_GROW_FAILS(stack, 1))
80 return NULL;
81 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
82 ++stack->ga_len;
83
84 return isn;
85}
86
87/*
88 * Generate an ISN_DEBUG instruction.
89 */
90 isn_T *
91generate_instr_debug(cctx_T *cctx)
92{
93 isn_T *isn;
94 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
95 + cctx->ctx_ufunc->uf_dfunc_idx;
96
97 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
98 return NULL;
99 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
100 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
101 return isn;
102}
103
104/*
105 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
106 * But only for simple types.
107 * When "tolerant" is TRUE convert most types to string, e.g. a List.
108 */
109 int
110may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
111{
112 isn_T *isn;
113 isntype_T isntype = ISN_2STRING;
114 garray_T *stack = &cctx->ctx_type_stack;
115 type_T **type;
116
117 RETURN_OK_IF_SKIP(cctx);
118 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
119 switch ((*type)->tt_type)
120 {
121 // nothing to be done
122 case VAR_STRING: return OK;
123
124 // conversion possible
125 case VAR_SPECIAL:
126 case VAR_BOOL:
127 case VAR_NUMBER:
128 case VAR_FLOAT:
129 break;
130
131 // conversion possible (with runtime check)
132 case VAR_ANY:
133 case VAR_UNKNOWN:
134 isntype = ISN_2STRING_ANY;
135 break;
136
137 // conversion possible when tolerant
138 case VAR_LIST:
139 if (tolerant)
140 {
141 isntype = ISN_2STRING_ANY;
142 break;
143 }
144 // FALLTHROUGH
145
146 // conversion not possible
147 case VAR_VOID:
148 case VAR_BLOB:
149 case VAR_FUNC:
150 case VAR_PARTIAL:
151 case VAR_DICT:
152 case VAR_JOB:
153 case VAR_CHANNEL:
154 case VAR_INSTR:
155 to_string_error((*type)->tt_type);
156 return FAIL;
157 }
158
159 *type = &t_string;
160 if ((isn = generate_instr(cctx, isntype)) == NULL)
161 return FAIL;
162 isn->isn_arg.tostring.offset = offset;
163 isn->isn_arg.tostring.tolerant = tolerant;
164
165 return OK;
166}
167
168 static int
169check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
170{
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000171 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
172 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000173 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000174 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000175 {
176 if (*op == '+')
177 emsg(_(e_wrong_argument_type_for_plus));
178 else
179 semsg(_(e_char_requires_number_or_float_arguments), *op);
180 return FAIL;
181 }
182 return OK;
183}
184
185/*
186 * Generate instruction for "+". For a list this creates a new list.
187 */
188 int
189generate_add_instr(
190 cctx_T *cctx,
191 vartype_T vartype,
192 type_T *type1,
193 type_T *type2,
194 exprtype_T expr_type)
195{
196 garray_T *stack = &cctx->ctx_type_stack;
197 isn_T *isn = generate_instr_drop(cctx,
198 vartype == VAR_NUMBER ? ISN_OPNR
199 : vartype == VAR_LIST ? ISN_ADDLIST
200 : vartype == VAR_BLOB ? ISN_ADDBLOB
201#ifdef FEAT_FLOAT
202 : vartype == VAR_FLOAT ? ISN_OPFLOAT
203#endif
204 : ISN_OPANY, 1);
205
206 if (vartype != VAR_LIST && vartype != VAR_BLOB
207 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000208 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000209 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000210 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000211 && check_number_or_float(
212 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
213 return FAIL;
214
215 if (isn != NULL)
216 {
217 if (isn->isn_type == ISN_ADDLIST)
218 isn->isn_arg.op.op_type = expr_type;
219 else
220 isn->isn_arg.op.op_type = EXPR_ADD;
221 }
222
223 // When concatenating two lists with different member types the member type
224 // becomes "any".
225 if (vartype == VAR_LIST
226 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
227 && type1->tt_member != type2->tt_member)
228 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
229
230 return isn == NULL ? FAIL : OK;
231}
232
233/*
234 * Get the type to use for an instruction for an operation on "type1" and
235 * "type2". If they are matching use a type-specific instruction. Otherwise
236 * fall back to runtime type checking.
237 */
238 vartype_T
239operator_type(type_T *type1, type_T *type2)
240{
241 if (type1->tt_type == type2->tt_type
242 && (type1->tt_type == VAR_NUMBER
243 || type1->tt_type == VAR_LIST
244#ifdef FEAT_FLOAT
245 || type1->tt_type == VAR_FLOAT
246#endif
247 || type1->tt_type == VAR_BLOB))
248 return type1->tt_type;
249 return VAR_ANY;
250}
251
252/*
253 * Generate an instruction with two arguments. The instruction depends on the
254 * type of the arguments.
255 */
256 int
257generate_two_op(cctx_T *cctx, char_u *op)
258{
259 garray_T *stack = &cctx->ctx_type_stack;
260 type_T *type1;
261 type_T *type2;
262 vartype_T vartype;
263 isn_T *isn;
264
265 RETURN_OK_IF_SKIP(cctx);
266
267 // Get the known type of the two items on the stack.
268 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
269 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
270 vartype = operator_type(type1, type2);
271
272 switch (*op)
273 {
274 case '+':
275 if (generate_add_instr(cctx, vartype, type1, type2,
276 EXPR_COPY) == FAIL)
277 return FAIL;
278 break;
279
280 case '-':
281 case '*':
282 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
283 op) == FAIL)
284 return FAIL;
285 if (vartype == VAR_NUMBER)
286 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
287#ifdef FEAT_FLOAT
288 else if (vartype == VAR_FLOAT)
289 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
290#endif
291 else
292 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
293 if (isn != NULL)
294 isn->isn_arg.op.op_type = *op == '*'
295 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
296 break;
297
298 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000299 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000300 && type1->tt_type != VAR_NUMBER)
301 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000302 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000303 && type2->tt_type != VAR_NUMBER))
304 {
305 emsg(_(e_percent_requires_number_arguments));
306 return FAIL;
307 }
308 isn = generate_instr_drop(cctx,
309 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
310 if (isn != NULL)
311 isn->isn_arg.op.op_type = EXPR_REM;
312 break;
313 }
314
315 // correct type of result
316 if (vartype == VAR_ANY)
317 {
318 type_T *type = &t_any;
319
320#ifdef FEAT_FLOAT
321 // float+number and number+float results in float
322 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
323 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
324 type = &t_float;
325#endif
326 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
327 }
328
329 return OK;
330}
331
332/*
333 * Get the instruction to use for comparing "type1" with "type2"
334 * Return ISN_DROP when failed.
335 */
336 static isntype_T
337get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
338{
339 isntype_T isntype = ISN_DROP;
340
341 if (type1 == VAR_UNKNOWN)
342 type1 = VAR_ANY;
343 if (type2 == VAR_UNKNOWN)
344 type2 = VAR_ANY;
345
346 if (type1 == type2)
347 {
348 switch (type1)
349 {
350 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
351 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
352 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
353 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
354 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
355 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
356 case VAR_LIST: isntype = ISN_COMPARELIST; break;
357 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
358 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
359 default: isntype = ISN_COMPAREANY; break;
360 }
361 }
362 else if (type1 == VAR_ANY || type2 == VAR_ANY
363 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
364 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
365 isntype = ISN_COMPAREANY;
366
367 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
368 && (isntype == ISN_COMPAREBOOL
369 || isntype == ISN_COMPARESPECIAL
370 || isntype == ISN_COMPARENR
371 || isntype == ISN_COMPAREFLOAT))
372 {
373 semsg(_(e_cannot_use_str_with_str),
374 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
375 return ISN_DROP;
376 }
377 if (isntype == ISN_DROP
378 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
379 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
380 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
381 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
382 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
383 && (type1 == VAR_BLOB || type2 == VAR_BLOB
384 || type1 == VAR_LIST || type2 == VAR_LIST))))
385 {
386 semsg(_(e_cannot_compare_str_with_str),
387 vartype_name(type1), vartype_name(type2));
388 return ISN_DROP;
389 }
390 return isntype;
391}
392
393 int
394check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
395{
396 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
397 return FAIL;
398 return OK;
399}
400
401/*
402 * Generate an ISN_COMPARE* instruction with a boolean result.
403 */
404 int
405generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
406{
407 isntype_T isntype;
408 isn_T *isn;
409 garray_T *stack = &cctx->ctx_type_stack;
410 vartype_T type1;
411 vartype_T type2;
412
413 RETURN_OK_IF_SKIP(cctx);
414
415 // Get the known type of the two items on the stack. If they are matching
416 // use a type-specific instruction. Otherwise fall back to runtime type
417 // checking.
418 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
419 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
420 isntype = get_compare_isn(exprtype, type1, type2);
421 if (isntype == ISN_DROP)
422 return FAIL;
423
424 if ((isn = generate_instr(cctx, isntype)) == NULL)
425 return FAIL;
426 isn->isn_arg.op.op_type = exprtype;
427 isn->isn_arg.op.op_ic = ic;
428
429 // takes two arguments, puts one bool back
430 if (stack->ga_len >= 2)
431 {
432 --stack->ga_len;
433 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
434 }
435
436 return OK;
437}
438
439/*
440 * Generate an ISN_2BOOL instruction.
441 * "offset" is the offset in the type stack.
442 */
443 int
444generate_2BOOL(cctx_T *cctx, int invert, int offset)
445{
446 isn_T *isn;
447 garray_T *stack = &cctx->ctx_type_stack;
448
449 RETURN_OK_IF_SKIP(cctx);
450 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
451 return FAIL;
452 isn->isn_arg.tobool.invert = invert;
453 isn->isn_arg.tobool.offset = offset;
454
455 // type becomes bool
456 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
457
458 return OK;
459}
460
461/*
462 * Generate an ISN_COND2BOOL instruction.
463 */
464 int
465generate_COND2BOOL(cctx_T *cctx)
466{
467 isn_T *isn;
468 garray_T *stack = &cctx->ctx_type_stack;
469
470 RETURN_OK_IF_SKIP(cctx);
471 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
472 return FAIL;
473
474 // type becomes bool
475 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
476
477 return OK;
478}
479
480 int
481generate_TYPECHECK(
482 cctx_T *cctx,
483 type_T *expected,
484 int offset,
485 int argidx)
486{
487 isn_T *isn;
488 garray_T *stack = &cctx->ctx_type_stack;
489
490 RETURN_OK_IF_SKIP(cctx);
491 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
492 return FAIL;
493 isn->isn_arg.type.ct_type = alloc_type(expected);
494 isn->isn_arg.type.ct_off = (int8_T)offset;
495 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
496
497 // type becomes expected
498 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
499
500 return OK;
501}
502
503 int
504generate_SETTYPE(
505 cctx_T *cctx,
506 type_T *expected)
507{
508 isn_T *isn;
509
510 RETURN_OK_IF_SKIP(cctx);
511 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
512 return FAIL;
513 isn->isn_arg.type.ct_type = alloc_type(expected);
514 return OK;
515}
516
517/*
518 * Generate a PUSH instruction for "tv".
519 * "tv" will be consumed or cleared.
520 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
521 */
522 int
523generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
524{
525 if (tv != NULL)
526 {
527 switch (tv->v_type)
528 {
529 case VAR_UNKNOWN:
530 break;
531 case VAR_BOOL:
532 generate_PUSHBOOL(cctx, tv->vval.v_number);
533 break;
534 case VAR_SPECIAL:
535 generate_PUSHSPEC(cctx, tv->vval.v_number);
536 break;
537 case VAR_NUMBER:
538 generate_PUSHNR(cctx, tv->vval.v_number);
539 break;
540#ifdef FEAT_FLOAT
541 case VAR_FLOAT:
542 generate_PUSHF(cctx, tv->vval.v_float);
543 break;
544#endif
545 case VAR_BLOB:
546 generate_PUSHBLOB(cctx, tv->vval.v_blob);
547 tv->vval.v_blob = NULL;
548 break;
549 case VAR_STRING:
550 generate_PUSHS(cctx, &tv->vval.v_string);
551 tv->vval.v_string = NULL;
552 break;
553 default:
554 iemsg("constant type not supported");
555 clear_tv(tv);
556 return FAIL;
557 }
558 tv->v_type = VAR_UNKNOWN;
559 }
560 return OK;
561}
562
563/*
564 * Generate an ISN_PUSHNR instruction.
565 */
566 int
567generate_PUSHNR(cctx_T *cctx, varnumber_T number)
568{
569 isn_T *isn;
570 garray_T *stack = &cctx->ctx_type_stack;
571
572 RETURN_OK_IF_SKIP(cctx);
573 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
574 return FAIL;
575 isn->isn_arg.number = number;
576
577 if (number == 0 || number == 1)
578 // A 0 or 1 number can also be used as a bool.
579 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
580 return OK;
581}
582
583/*
584 * Generate an ISN_PUSHBOOL instruction.
585 */
586 int
587generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
588{
589 isn_T *isn;
590
591 RETURN_OK_IF_SKIP(cctx);
592 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
593 return FAIL;
594 isn->isn_arg.number = number;
595
596 return OK;
597}
598
599/*
600 * Generate an ISN_PUSHSPEC instruction.
601 */
602 int
603generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
604{
605 isn_T *isn;
606
607 RETURN_OK_IF_SKIP(cctx);
608 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
609 return FAIL;
610 isn->isn_arg.number = number;
611
612 return OK;
613}
614
615#if defined(FEAT_FLOAT) || defined(PROTO)
616/*
617 * Generate an ISN_PUSHF instruction.
618 */
619 int
620generate_PUSHF(cctx_T *cctx, float_T fnumber)
621{
622 isn_T *isn;
623
624 RETURN_OK_IF_SKIP(cctx);
625 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
626 return FAIL;
627 isn->isn_arg.fnumber = fnumber;
628
629 return OK;
630}
631#endif
632
633/*
634 * Generate an ISN_PUSHS instruction.
635 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
636 */
637 int
638generate_PUSHS(cctx_T *cctx, char_u **str)
639{
640 isn_T *isn;
641
642 if (cctx->ctx_skip == SKIP_YES)
643 {
644 if (str != NULL)
645 VIM_CLEAR(*str);
646 return OK;
647 }
648 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
649 {
650 if (str != NULL)
651 VIM_CLEAR(*str);
652 return FAIL;
653 }
654 isn->isn_arg.string = str == NULL ? NULL : *str;
655
656 return OK;
657}
658
659/*
660 * Generate an ISN_PUSHCHANNEL instruction.
661 * Consumes "channel".
662 */
663 int
664generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
665{
666 isn_T *isn;
667
668 RETURN_OK_IF_SKIP(cctx);
669 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
670 return FAIL;
671 isn->isn_arg.channel = channel;
672
673 return OK;
674}
675
676/*
677 * Generate an ISN_PUSHJOB instruction.
678 * Consumes "job".
679 */
680 int
681generate_PUSHJOB(cctx_T *cctx, job_T *job)
682{
683 isn_T *isn;
684
685 RETURN_OK_IF_SKIP(cctx);
686 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
687 return FAIL;
688 isn->isn_arg.job = job;
689
690 return OK;
691}
692
693/*
694 * Generate an ISN_PUSHBLOB instruction.
695 * Consumes "blob".
696 */
697 int
698generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
699{
700 isn_T *isn;
701
702 RETURN_OK_IF_SKIP(cctx);
703 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
704 return FAIL;
705 isn->isn_arg.blob = blob;
706
707 return OK;
708}
709
710/*
711 * Generate an ISN_PUSHFUNC instruction with name "name".
712 * Consumes "name".
713 */
714 int
715generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
716{
717 isn_T *isn;
718 char_u *funcname;
719
720 RETURN_OK_IF_SKIP(cctx);
721 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
722 return FAIL;
723 if (name == NULL)
724 funcname = NULL;
725 else if (*name == K_SPECIAL) // script-local
726 funcname = vim_strsave(name);
727 else
728 {
729 funcname = alloc(STRLEN(name) + 3);
730 if (funcname != NULL)
731 {
732 STRCPY(funcname, "g:");
733 STRCPY(funcname + 2, name);
734 }
735 }
736
737 isn->isn_arg.string = funcname;
738 return OK;
739}
740
741/*
742 * Generate an ISN_GETITEM instruction with "index".
743 * "with_op" is TRUE for "+=" and other operators, the stack has the current
744 * value below the list with values.
745 */
746 int
747generate_GETITEM(cctx_T *cctx, int index, int with_op)
748{
749 isn_T *isn;
750 garray_T *stack = &cctx->ctx_type_stack;
751 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
752 - (with_op ? 2 : 1)];
753 type_T *item_type = &t_any;
754
755 RETURN_OK_IF_SKIP(cctx);
756
757 if (type->tt_type != VAR_LIST)
758 {
759 // cannot happen, caller has checked the type
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000760 emsg(_(e_list_required));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000761 return FAIL;
762 }
763 item_type = type->tt_member;
764 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
765 return FAIL;
766 isn->isn_arg.getitem.gi_index = index;
767 isn->isn_arg.getitem.gi_with_op = with_op;
768
769 // add the item type to the type stack
770 if (GA_GROW_FAILS(stack, 1))
771 return FAIL;
772 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
773 ++stack->ga_len;
774 return OK;
775}
776
777/*
778 * Generate an ISN_SLICE instruction with "count".
779 */
780 int
781generate_SLICE(cctx_T *cctx, int count)
782{
783 isn_T *isn;
784
785 RETURN_OK_IF_SKIP(cctx);
786 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
787 return FAIL;
788 isn->isn_arg.number = count;
789 return OK;
790}
791
792/*
793 * Generate an ISN_CHECKLEN instruction with "min_len".
794 */
795 int
796generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
797{
798 isn_T *isn;
799
800 RETURN_OK_IF_SKIP(cctx);
801
802 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
803 return FAIL;
804 isn->isn_arg.checklen.cl_min_len = min_len;
805 isn->isn_arg.checklen.cl_more_OK = more_OK;
806
807 return OK;
808}
809
810/*
811 * Generate an ISN_STORE instruction.
812 */
813 int
814generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
815{
816 isn_T *isn;
817
818 RETURN_OK_IF_SKIP(cctx);
819 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
820 return FAIL;
821 if (name != NULL)
822 isn->isn_arg.string = vim_strsave(name);
823 else
824 isn->isn_arg.number = idx;
825
826 return OK;
827}
828
829/*
830 * Generate an ISN_STOREOUTER instruction.
831 */
832 int
833generate_STOREOUTER(cctx_T *cctx, int idx, int level)
834{
835 isn_T *isn;
836
837 RETURN_OK_IF_SKIP(cctx);
838 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
839 return FAIL;
840 isn->isn_arg.outer.outer_idx = idx;
841 isn->isn_arg.outer.outer_depth = level;
842
843 return OK;
844}
845
846/*
847 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
848 */
849 int
850generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
851{
852 isn_T *isn;
853
854 RETURN_OK_IF_SKIP(cctx);
855 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
856 return FAIL;
857 isn->isn_arg.storenr.stnr_idx = idx;
858 isn->isn_arg.storenr.stnr_val = value;
859
860 return OK;
861}
862
863/*
864 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
865 */
866 int
867generate_STOREOPT(
868 cctx_T *cctx,
869 isntype_T isn_type,
870 char_u *name,
871 int opt_flags)
872{
873 isn_T *isn;
874
875 RETURN_OK_IF_SKIP(cctx);
876 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
877 return FAIL;
878 isn->isn_arg.storeopt.so_name = vim_strsave(name);
879 isn->isn_arg.storeopt.so_flags = opt_flags;
880
881 return OK;
882}
883
884/*
885 * Generate an ISN_LOAD or similar instruction.
886 */
887 int
888generate_LOAD(
889 cctx_T *cctx,
890 isntype_T isn_type,
891 int idx,
892 char_u *name,
893 type_T *type)
894{
895 isn_T *isn;
896
897 RETURN_OK_IF_SKIP(cctx);
898 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
899 return FAIL;
900 if (name != NULL)
901 isn->isn_arg.string = vim_strsave(name);
902 else
903 isn->isn_arg.number = idx;
904
905 return OK;
906}
907
908/*
909 * Generate an ISN_LOADOUTER instruction
910 */
911 int
912generate_LOADOUTER(
913 cctx_T *cctx,
914 int idx,
915 int nesting,
916 type_T *type)
917{
918 isn_T *isn;
919
920 RETURN_OK_IF_SKIP(cctx);
921 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
922 return FAIL;
923 isn->isn_arg.outer.outer_idx = idx;
924 isn->isn_arg.outer.outer_depth = nesting;
925
926 return OK;
927}
928
929/*
930 * Generate an ISN_LOADV instruction for v:var.
931 */
932 int
933generate_LOADV(
934 cctx_T *cctx,
935 char_u *name,
936 int error)
937{
938 int di_flags;
939 int vidx = find_vim_var(name, &di_flags);
940 type_T *type;
941
942 RETURN_OK_IF_SKIP(cctx);
943 if (vidx < 0)
944 {
945 if (error)
946 semsg(_(e_variable_not_found_str), name);
947 return FAIL;
948 }
Bram Moolenaard787e402021-12-24 21:36:12 +0000949 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000950 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
951}
952
953/*
954 * Generate an ISN_UNLET instruction.
955 */
956 int
957generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
958{
959 isn_T *isn;
960
961 RETURN_OK_IF_SKIP(cctx);
962 if ((isn = generate_instr(cctx, isn_type)) == NULL)
963 return FAIL;
964 isn->isn_arg.unlet.ul_name = vim_strsave(name);
965 isn->isn_arg.unlet.ul_forceit = forceit;
966
967 return OK;
968}
969
970/*
971 * Generate an ISN_LOCKCONST instruction.
972 */
973 int
974generate_LOCKCONST(cctx_T *cctx)
975{
976 isn_T *isn;
977
978 RETURN_OK_IF_SKIP(cctx);
979 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
980 return FAIL;
981 return OK;
982}
983
984/*
985 * Generate an ISN_LOADS instruction.
986 */
987 int
988generate_OLDSCRIPT(
989 cctx_T *cctx,
990 isntype_T isn_type,
991 char_u *name,
992 int sid,
993 type_T *type)
994{
995 isn_T *isn;
996
997 RETURN_OK_IF_SKIP(cctx);
998 if (isn_type == ISN_LOADS)
999 isn = generate_instr_type(cctx, isn_type, type);
1000 else
1001 isn = generate_instr_drop(cctx, isn_type, 1);
1002 if (isn == NULL)
1003 return FAIL;
1004 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1005 isn->isn_arg.loadstore.ls_sid = sid;
1006
1007 return OK;
1008}
1009
1010/*
1011 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1012 */
1013 int
1014generate_VIM9SCRIPT(
1015 cctx_T *cctx,
1016 isntype_T isn_type,
1017 int sid,
1018 int idx,
1019 type_T *type)
1020{
1021 isn_T *isn;
1022 scriptref_T *sref;
1023 scriptitem_T *si = SCRIPT_ITEM(sid);
1024
1025 RETURN_OK_IF_SKIP(cctx);
1026 if (isn_type == ISN_LOADSCRIPT)
1027 isn = generate_instr_type(cctx, isn_type, type);
1028 else
1029 isn = generate_instr_drop(cctx, isn_type, 1);
1030 if (isn == NULL)
1031 return FAIL;
1032
1033 // This requires three arguments, which doesn't fit in an instruction, thus
1034 // we need to allocate a struct for this.
1035 sref = ALLOC_ONE(scriptref_T);
1036 if (sref == NULL)
1037 return FAIL;
1038 isn->isn_arg.script.scriptref = sref;
1039 sref->sref_sid = sid;
1040 sref->sref_idx = idx;
1041 sref->sref_seq = si->sn_script_seq;
1042 sref->sref_type = type;
1043 return OK;
1044}
1045
1046/*
1047 * Generate an ISN_NEWLIST instruction.
1048 */
1049 int
1050generate_NEWLIST(cctx_T *cctx, int count)
1051{
1052 isn_T *isn;
1053 garray_T *stack = &cctx->ctx_type_stack;
1054 type_T *type;
1055 type_T *member;
1056
1057 RETURN_OK_IF_SKIP(cctx);
1058 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1059 return FAIL;
1060 isn->isn_arg.number = count;
1061
1062 // get the member type from all the items on the stack.
1063 if (count == 0)
1064 member = &t_unknown;
1065 else
1066 member = get_member_type_from_stack(
1067 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1068 cctx->ctx_type_list);
1069 type = get_list_type(member, cctx->ctx_type_list);
1070
1071 // drop the value types
1072 stack->ga_len -= count;
1073
1074 // add the list type to the type stack
1075 if (GA_GROW_FAILS(stack, 1))
1076 return FAIL;
1077 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1078 ++stack->ga_len;
1079
1080 return OK;
1081}
1082
1083/*
1084 * Generate an ISN_NEWDICT instruction.
1085 */
1086 int
1087generate_NEWDICT(cctx_T *cctx, int count)
1088{
1089 isn_T *isn;
1090 garray_T *stack = &cctx->ctx_type_stack;
1091 type_T *type;
1092 type_T *member;
1093
1094 RETURN_OK_IF_SKIP(cctx);
1095 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1096 return FAIL;
1097 isn->isn_arg.number = count;
1098
1099 if (count == 0)
1100 member = &t_void;
1101 else
1102 member = get_member_type_from_stack(
1103 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1104 cctx->ctx_type_list);
1105 type = get_dict_type(member, cctx->ctx_type_list);
1106
1107 // drop the key and value types
1108 stack->ga_len -= 2 * count;
1109
1110 // add the dict type to the type stack
1111 if (GA_GROW_FAILS(stack, 1))
1112 return FAIL;
1113 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1114 ++stack->ga_len;
1115
1116 return OK;
1117}
1118
1119/*
1120 * Generate an ISN_FUNCREF instruction.
1121 */
1122 int
1123generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
1124{
1125 isn_T *isn;
1126 garray_T *stack = &cctx->ctx_type_stack;
1127
1128 RETURN_OK_IF_SKIP(cctx);
1129 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1130 return FAIL;
1131 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1132 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1133 else
1134 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
1135 cctx->ctx_has_closure = 1;
1136
1137 // If the referenced function is a closure, it may use items further up in
1138 // the nested context, including this one.
1139 if (ufunc->uf_flags & FC_CLOSURE)
1140 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1141
1142 if (GA_GROW_FAILS(stack, 1))
1143 return FAIL;
1144 ((type_T **)stack->ga_data)[stack->ga_len] =
1145 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1146 ++stack->ga_len;
1147
1148 return OK;
1149}
1150
1151/*
1152 * Generate an ISN_NEWFUNC instruction.
1153 * "lambda_name" and "func_name" must be in allocated memory and will be
1154 * consumed.
1155 */
1156 int
1157generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1158{
1159 isn_T *isn;
1160
1161 if (cctx->ctx_skip == SKIP_YES)
1162 {
1163 vim_free(lambda_name);
1164 vim_free(func_name);
1165 return OK;
1166 }
1167 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1168 {
1169 vim_free(lambda_name);
1170 vim_free(func_name);
1171 return FAIL;
1172 }
1173 isn->isn_arg.newfunc.nf_lambda = lambda_name;
1174 isn->isn_arg.newfunc.nf_global = func_name;
1175
1176 return OK;
1177}
1178
1179/*
1180 * Generate an ISN_DEF instruction: list functions
1181 */
1182 int
1183generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1184{
1185 isn_T *isn;
1186
1187 RETURN_OK_IF_SKIP(cctx);
1188 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1189 return FAIL;
1190 if (len > 0)
1191 {
1192 isn->isn_arg.string = vim_strnsave(name, len);
1193 if (isn->isn_arg.string == NULL)
1194 return FAIL;
1195 }
1196 return OK;
1197}
1198
1199/*
1200 * Generate an ISN_JUMP instruction.
1201 */
1202 int
1203generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1204{
1205 isn_T *isn;
1206 garray_T *stack = &cctx->ctx_type_stack;
1207
1208 RETURN_OK_IF_SKIP(cctx);
1209 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1210 return FAIL;
1211 isn->isn_arg.jump.jump_when = when;
1212 isn->isn_arg.jump.jump_where = where;
1213
1214 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1215 --stack->ga_len;
1216
1217 return OK;
1218}
1219
1220/*
1221 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1222 */
1223 int
1224generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1225{
1226 isn_T *isn;
1227
1228 RETURN_OK_IF_SKIP(cctx);
1229 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1230 return FAIL;
1231 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1232 // jump_where is set later
1233 return OK;
1234}
1235
1236 int
1237generate_FOR(cctx_T *cctx, int loop_idx)
1238{
1239 isn_T *isn;
1240 garray_T *stack = &cctx->ctx_type_stack;
1241
1242 RETURN_OK_IF_SKIP(cctx);
1243 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1244 return FAIL;
1245 isn->isn_arg.forloop.for_idx = loop_idx;
1246
1247 if (GA_GROW_FAILS(stack, 1))
1248 return FAIL;
1249 // type doesn't matter, will be stored next
1250 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1251 ++stack->ga_len;
1252
1253 return OK;
1254}
1255/*
1256 * Generate an ISN_TRYCONT instruction.
1257 */
1258 int
1259generate_TRYCONT(cctx_T *cctx, int levels, int where)
1260{
1261 isn_T *isn;
1262
1263 RETURN_OK_IF_SKIP(cctx);
1264 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1265 return FAIL;
1266 isn->isn_arg.trycont.tct_levels = levels;
1267 isn->isn_arg.trycont.tct_where = where;
1268
1269 return OK;
1270}
1271
1272
1273/*
1274 * Generate an ISN_BCALL instruction.
1275 * "method_call" is TRUE for "value->method()"
1276 * Return FAIL if the number of arguments is wrong.
1277 */
1278 int
1279generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1280{
1281 isn_T *isn;
1282 garray_T *stack = &cctx->ctx_type_stack;
1283 int argoff;
1284 type_T **argtypes = NULL;
1285 type_T *shuffled_argtypes[MAX_FUNC_ARGS];
1286 type_T *maptype = NULL;
1287
1288 RETURN_OK_IF_SKIP(cctx);
1289 argoff = check_internal_func(func_idx, argcount);
1290 if (argoff < 0)
1291 return FAIL;
1292
1293 if (method_call && argoff > 1)
1294 {
1295 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1296 return FAIL;
1297 isn->isn_arg.shuffle.shfl_item = argcount;
1298 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1299 }
1300
1301 if (argcount > 0)
1302 {
1303 // Check the types of the arguments.
1304 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1305 if (method_call && argoff > 1)
1306 {
1307 int i;
1308
1309 for (i = 0; i < argcount; ++i)
1310 shuffled_argtypes[i] = (i < argoff - 1)
1311 ? argtypes[i + 1]
1312 : (i == argoff - 1) ? argtypes[0] : argtypes[i];
1313 argtypes = shuffled_argtypes;
1314 }
1315 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1316 cctx) == FAIL)
1317 return FAIL;
1318 if (internal_func_is_map(func_idx))
1319 maptype = *argtypes;
1320 }
1321
1322 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1323 return FAIL;
1324 isn->isn_arg.bfunc.cbf_idx = func_idx;
1325 isn->isn_arg.bfunc.cbf_argcount = argcount;
1326
1327 // Drop the argument types and push the return type.
1328 stack->ga_len -= argcount;
1329 if (GA_GROW_FAILS(stack, 1))
1330 return FAIL;
1331 ((type_T **)stack->ga_data)[stack->ga_len] =
1332 internal_func_ret_type(func_idx, argcount, argtypes);
1333 ++stack->ga_len;
1334
1335 if (maptype != NULL && maptype->tt_member != NULL
1336 && maptype->tt_member != &t_any)
1337 // Check that map() didn't change the item types.
1338 generate_TYPECHECK(cctx, maptype, -1, 1);
1339
1340 return OK;
1341}
1342
1343/*
1344 * Generate an ISN_LISTAPPEND instruction. Works like add().
1345 * Argument count is already checked.
1346 */
1347 int
1348generate_LISTAPPEND(cctx_T *cctx)
1349{
1350 garray_T *stack = &cctx->ctx_type_stack;
1351 type_T *list_type;
1352 type_T *item_type;
1353 type_T *expected;
1354
1355 // Caller already checked that list_type is a list.
1356 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1357 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1358 expected = list_type->tt_member;
1359 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1360 return FAIL;
1361
1362 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1363 return FAIL;
1364
1365 --stack->ga_len; // drop the argument
1366 return OK;
1367}
1368
1369/*
1370 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1371 * Argument count is already checked.
1372 */
1373 int
1374generate_BLOBAPPEND(cctx_T *cctx)
1375{
1376 garray_T *stack = &cctx->ctx_type_stack;
1377 type_T *item_type;
1378
1379 // Caller already checked that blob_type is a blob.
1380 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1381 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1382 return FAIL;
1383
1384 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1385 return FAIL;
1386
1387 --stack->ga_len; // drop the argument
1388 return OK;
1389}
1390
1391/*
1392 * Generate an ISN_DCALL or ISN_UCALL instruction.
1393 * Return FAIL if the number of arguments is wrong.
1394 */
1395 int
1396generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1397{
1398 isn_T *isn;
1399 garray_T *stack = &cctx->ctx_type_stack;
1400 int regular_args = ufunc->uf_args.ga_len;
1401 int argcount = pushed_argcount;
1402
1403 RETURN_OK_IF_SKIP(cctx);
1404 if (argcount > regular_args && !has_varargs(ufunc))
1405 {
1406 semsg(_(e_too_many_arguments_for_function_str),
1407 printable_func_name(ufunc));
1408 return FAIL;
1409 }
1410 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1411 {
1412 semsg(_(e_not_enough_arguments_for_function_str),
1413 printable_func_name(ufunc));
1414 return FAIL;
1415 }
1416
1417 if (ufunc->uf_def_status != UF_NOT_COMPILED
1418 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1419 {
1420 int i;
1421
1422 for (i = 0; i < argcount; ++i)
1423 {
1424 type_T *expected;
1425 type_T *actual;
1426
1427 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1428 if (actual == &t_special
1429 && i >= regular_args - ufunc->uf_def_args.ga_len)
1430 {
1431 // assume v:none used for default argument value
1432 continue;
1433 }
1434 if (i < regular_args)
1435 {
1436 if (ufunc->uf_arg_types == NULL)
1437 continue;
1438 expected = ufunc->uf_arg_types[i];
1439 }
1440 else if (ufunc->uf_va_type == NULL
1441 || ufunc->uf_va_type == &t_list_any)
1442 // possibly a lambda or "...: any"
1443 expected = &t_any;
1444 else
1445 expected = ufunc->uf_va_type->tt_member;
1446 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1447 TRUE, FALSE) == FAIL)
1448 {
1449 arg_type_mismatch(expected, actual, i + 1);
1450 return FAIL;
1451 }
1452 }
1453 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
1454 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
1455 COMPILE_TYPE(ufunc), NULL) == FAIL)
1456 return FAIL;
1457 }
1458 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1459 {
1460 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1461 ufunc->uf_name);
1462 return FAIL;
1463 }
1464
1465 if ((isn = generate_instr(cctx,
1466 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1467 : ISN_UCALL)) == NULL)
1468 return FAIL;
1469 if (isn->isn_type == ISN_DCALL)
1470 {
1471 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1472 isn->isn_arg.dfunc.cdf_argcount = argcount;
1473 }
1474 else
1475 {
1476 // A user function may be deleted and redefined later, can't use the
1477 // ufunc pointer, need to look it up again at runtime.
1478 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1479 isn->isn_arg.ufunc.cuf_argcount = argcount;
1480 }
1481
1482 stack->ga_len -= argcount; // drop the arguments
1483 if (GA_GROW_FAILS(stack, 1))
1484 return FAIL;
1485 // add return value
1486 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1487 ++stack->ga_len;
1488
1489 return OK;
1490}
1491
1492/*
1493 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1494 */
1495 int
1496generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1497{
1498 isn_T *isn;
1499 garray_T *stack = &cctx->ctx_type_stack;
1500
1501 RETURN_OK_IF_SKIP(cctx);
1502 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1503 return FAIL;
1504 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1505 isn->isn_arg.ufunc.cuf_argcount = argcount;
1506
1507 stack->ga_len -= argcount; // drop the arguments
1508 if (GA_GROW_FAILS(stack, 1))
1509 return FAIL;
1510 // add return value
1511 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1512 ++stack->ga_len;
1513
1514 return OK;
1515}
1516
1517/*
1518 * Generate an ISN_PCALL instruction.
1519 * "type" is the type of the FuncRef.
1520 */
1521 int
1522generate_PCALL(
1523 cctx_T *cctx,
1524 int argcount,
1525 char_u *name,
1526 type_T *type,
1527 int at_top)
1528{
1529 isn_T *isn;
1530 garray_T *stack = &cctx->ctx_type_stack;
1531 type_T *ret_type;
1532
1533 RETURN_OK_IF_SKIP(cctx);
1534
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001535 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001536 ret_type = &t_any;
1537 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1538 {
1539 if (type->tt_argcount != -1)
1540 {
1541 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1542
1543 if (argcount < type->tt_min_argcount - varargs)
1544 {
1545 semsg(_(e_not_enough_arguments_for_function_str), name);
1546 return FAIL;
1547 }
1548 if (!varargs && argcount > type->tt_argcount)
1549 {
1550 semsg(_(e_too_many_arguments_for_function_str), name);
1551 return FAIL;
1552 }
1553 if (type->tt_args != NULL)
1554 {
1555 int i;
1556
1557 for (i = 0; i < argcount; ++i)
1558 {
1559 int offset = -argcount + i - (at_top ? 0 : 1);
1560 type_T *actual = ((type_T **)stack->ga_data)[
1561 stack->ga_len + offset];
1562 type_T *expected;
1563
1564 if (varargs && i >= type->tt_argcount - 1)
1565 expected = type->tt_args[
1566 type->tt_argcount - 1]->tt_member;
1567 else if (i >= type->tt_min_argcount
1568 && actual == &t_special)
1569 expected = &t_any;
1570 else
1571 expected = type->tt_args[i];
1572 if (need_type(actual, expected, offset, i + 1,
1573 cctx, TRUE, FALSE) == FAIL)
1574 {
1575 arg_type_mismatch(expected, actual, i + 1);
1576 return FAIL;
1577 }
1578 }
1579 }
1580 }
1581 ret_type = type->tt_member;
1582 if (ret_type == &t_unknown)
1583 // return type not known yet, use a runtime check
1584 ret_type = &t_any;
1585 }
1586 else
1587 {
1588 semsg(_(e_not_callable_type_str), name);
1589 return FAIL;
1590 }
1591
1592 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1593 return FAIL;
1594 isn->isn_arg.pfunc.cpf_top = at_top;
1595 isn->isn_arg.pfunc.cpf_argcount = argcount;
1596
1597 stack->ga_len -= argcount; // drop the arguments
1598
1599 // drop the funcref/partial, get back the return value
1600 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
1601
1602 // If partial is above the arguments it must be cleared and replaced with
1603 // the return value.
1604 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1605 return FAIL;
1606
1607 return OK;
1608}
1609
1610/*
1611 * Generate an ISN_STRINGMEMBER instruction.
1612 */
1613 int
1614generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1615{
1616 isn_T *isn;
1617 garray_T *stack = &cctx->ctx_type_stack;
1618 type_T *type;
1619
1620 RETURN_OK_IF_SKIP(cctx);
1621 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1622 return FAIL;
1623 isn->isn_arg.string = vim_strnsave(name, len);
1624
1625 // check for dict type
1626 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001627 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001628 {
1629 char *tofree;
1630
1631 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1632 name, type_name(type, &tofree));
1633 vim_free(tofree);
1634 return FAIL;
1635 }
1636 // change dict type to dict member type
1637 if (type->tt_type == VAR_DICT)
1638 {
1639 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
1640 type->tt_member == &t_unknown ? &t_any : type->tt_member;
1641 }
1642
1643 return OK;
1644}
1645
1646/*
1647 * Generate an ISN_ECHO instruction.
1648 */
1649 int
1650generate_ECHO(cctx_T *cctx, int with_white, int count)
1651{
1652 isn_T *isn;
1653
1654 RETURN_OK_IF_SKIP(cctx);
1655 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1656 return FAIL;
1657 isn->isn_arg.echo.echo_with_white = with_white;
1658 isn->isn_arg.echo.echo_count = count;
1659
1660 return OK;
1661}
1662
1663/*
1664 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1665 */
1666 int
1667generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1668{
1669 isn_T *isn;
1670
1671 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1672 return FAIL;
1673 isn->isn_arg.number = count;
1674
1675 return OK;
1676}
1677
1678/*
1679 * Generate an ISN_PUT instruction.
1680 */
1681 int
1682generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1683{
1684 isn_T *isn;
1685
1686 RETURN_OK_IF_SKIP(cctx);
1687 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1688 return FAIL;
1689 isn->isn_arg.put.put_regname = regname;
1690 isn->isn_arg.put.put_lnum = lnum;
1691 return OK;
1692}
1693
1694/*
1695 * Generate an EXEC instruction that takes a string argument.
1696 * A copy is made of "line".
1697 */
1698 int
1699generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1700{
1701 isn_T *isn;
1702
1703 RETURN_OK_IF_SKIP(cctx);
1704 if ((isn = generate_instr(cctx, isntype)) == NULL)
1705 return FAIL;
1706 isn->isn_arg.string = vim_strsave(line);
1707 return OK;
1708}
1709
1710/*
1711 * Generate an EXEC instruction that takes a string argument.
1712 * "str" must be allocated, it is consumed.
1713 */
1714 int
1715generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1716{
1717 isn_T *isn;
1718
1719 if (cctx->ctx_skip == SKIP_YES)
1720 {
1721 vim_free(str);
1722 return OK;
1723 }
1724 if ((isn = generate_instr(cctx, isntype)) == NULL)
1725 {
1726 vim_free(str);
1727 return FAIL;
1728 }
1729 isn->isn_arg.string = str;
1730 return OK;
1731}
1732
1733 int
1734generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1735{
1736 isn_T *isn;
1737 garray_T *stack = &cctx->ctx_type_stack;
1738
1739 RETURN_OK_IF_SKIP(cctx);
1740 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1741 return FAIL;
1742 isn->isn_arg.string = vim_strsave(line);
1743
1744 if (GA_GROW_FAILS(stack, 1))
1745 return FAIL;
1746 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1747 ++stack->ga_len;
1748
1749 return OK;
1750}
1751
1752 int
1753generate_EXECCONCAT(cctx_T *cctx, int count)
1754{
1755 isn_T *isn;
1756
1757 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1758 return FAIL;
1759 isn->isn_arg.number = count;
1760 return OK;
1761}
1762
1763/*
1764 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1765 */
1766 int
1767generate_RANGE(cctx_T *cctx, char_u *range)
1768{
1769 isn_T *isn;
1770 garray_T *stack = &cctx->ctx_type_stack;
1771
1772 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1773 return FAIL;
1774 isn->isn_arg.string = range;
1775
1776 if (GA_GROW_FAILS(stack, 1))
1777 return FAIL;
1778 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
1779 ++stack->ga_len;
1780 return OK;
1781}
1782
1783 int
1784generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1785{
1786 isn_T *isn;
1787
1788 RETURN_OK_IF_SKIP(cctx);
1789 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1790 return FAIL;
1791 isn->isn_arg.unpack.unp_count = var_count;
1792 isn->isn_arg.unpack.unp_semicolon = semicolon;
1793 return OK;
1794}
1795
1796/*
1797 * Generate an instruction for any command modifiers.
1798 */
1799 int
1800generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
1801{
1802 isn_T *isn;
1803
1804 if (has_cmdmod(cmod, FALSE))
1805 {
1806 cctx->ctx_has_cmdmod = TRUE;
1807
1808 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
1809 return FAIL;
1810 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1811 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1812 return FAIL;
1813 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1814 // filter program now belongs to the instruction
1815 cmod->cmod_filter_regmatch.regprog = NULL;
1816 }
1817
1818 return OK;
1819}
1820
1821 int
1822generate_undo_cmdmods(cctx_T *cctx)
1823{
1824 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1825 return FAIL;
1826 cctx->ctx_has_cmdmod = FALSE;
1827 return OK;
1828}
1829
1830/*
1831 * Generate a STORE instruction for "dest", not being "dest_local".
1832 * Return FAIL when out of memory.
1833 */
1834 int
1835generate_store_var(
1836 cctx_T *cctx,
1837 assign_dest_T dest,
1838 int opt_flags,
1839 int vimvaridx,
1840 int scriptvar_idx,
1841 int scriptvar_sid,
1842 type_T *type,
1843 char_u *name)
1844{
1845 switch (dest)
1846 {
1847 case dest_option:
1848 return generate_STOREOPT(cctx, ISN_STOREOPT,
1849 skip_option_env_lead(name), opt_flags);
1850 case dest_func_option:
1851 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
1852 skip_option_env_lead(name), opt_flags);
1853 case dest_global:
1854 // include g: with the name, easier to execute that way
1855 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
1856 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
1857 case dest_buffer:
1858 // include b: with the name, easier to execute that way
1859 return generate_STORE(cctx, ISN_STOREB, 0, name);
1860 case dest_window:
1861 // include w: with the name, easier to execute that way
1862 return generate_STORE(cctx, ISN_STOREW, 0, name);
1863 case dest_tab:
1864 // include t: with the name, easier to execute that way
1865 return generate_STORE(cctx, ISN_STORET, 0, name);
1866 case dest_env:
1867 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
1868 case dest_reg:
1869 return generate_STORE(cctx, ISN_STOREREG,
1870 name[1] == '@' ? '"' : name[1], NULL);
1871 case dest_vimvar:
1872 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
1873 case dest_script:
1874 if (scriptvar_idx < 0)
1875 // "s:" may be included in the name.
1876 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
1877 scriptvar_sid, type);
1878 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
1879 scriptvar_sid, scriptvar_idx, type);
1880 case dest_local:
1881 case dest_expr:
1882 // cannot happen
1883 break;
1884 }
1885 return FAIL;
1886}
1887
1888 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001889generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001890{
1891 if (lhs->lhs_dest != dest_local)
1892 return generate_store_var(cctx, lhs->lhs_dest,
1893 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
1894 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
1895 lhs->lhs_type, lhs->lhs_name);
1896
1897 if (lhs->lhs_lvar != NULL)
1898 {
1899 garray_T *instr = &cctx->ctx_instr;
1900 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1901
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001902 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
1903 // ISN_STORENR.
1904 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001905 if (lhs->lhs_lvar->lv_from_outer == 0
1906 && instr->ga_len == instr_count + 1
1907 && isn->isn_type == ISN_PUSHNR)
1908 {
1909 varnumber_T val = isn->isn_arg.number;
1910 garray_T *stack = &cctx->ctx_type_stack;
1911
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001912 if (val == 0 && is_decl)
1913 {
1914 --instr->ga_len;
1915 }
1916 else
1917 {
1918 isn->isn_type = ISN_STORENR;
1919 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
1920 isn->isn_arg.storenr.stnr_val = val;
1921 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001922 if (stack->ga_len > 0)
1923 --stack->ga_len;
1924 }
1925 else if (lhs->lhs_lvar->lv_from_outer > 0)
1926 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
1927 lhs->lhs_lvar->lv_from_outer);
1928 else
1929 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
1930 }
1931 return OK;
1932}
1933
1934#if defined(FEAT_PROFILE) || defined(PROTO)
1935 void
1936may_generate_prof_end(cctx_T *cctx, int prof_lnum)
1937{
1938 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
1939 generate_instr(cctx, ISN_PROF_END);
1940}
1941#endif
1942
1943
1944/*
1945 * Delete an instruction, free what it contains.
1946 */
1947 void
1948delete_instr(isn_T *isn)
1949{
1950 switch (isn->isn_type)
1951 {
1952 case ISN_DEF:
1953 case ISN_EXEC:
1954 case ISN_EXECRANGE:
1955 case ISN_EXEC_SPLIT:
1956 case ISN_LEGACY_EVAL:
1957 case ISN_LOADAUTO:
1958 case ISN_LOADB:
1959 case ISN_LOADENV:
1960 case ISN_LOADG:
1961 case ISN_LOADOPT:
1962 case ISN_LOADT:
1963 case ISN_LOADW:
1964 case ISN_LOCKUNLOCK:
1965 case ISN_PUSHEXC:
1966 case ISN_PUSHFUNC:
1967 case ISN_PUSHS:
1968 case ISN_RANGE:
1969 case ISN_STOREAUTO:
1970 case ISN_STOREB:
1971 case ISN_STOREENV:
1972 case ISN_STOREG:
1973 case ISN_STORET:
1974 case ISN_STOREW:
1975 case ISN_STRINGMEMBER:
1976 vim_free(isn->isn_arg.string);
1977 break;
1978
1979 case ISN_SUBSTITUTE:
1980 {
1981 int idx;
1982 isn_T *list = isn->isn_arg.subs.subs_instr;
1983
1984 vim_free(isn->isn_arg.subs.subs_cmd);
1985 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
1986 delete_instr(list + idx);
1987 vim_free(list);
1988 }
1989 break;
1990
1991 case ISN_INSTR:
1992 {
1993 int idx;
1994 isn_T *list = isn->isn_arg.instr;
1995
1996 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
1997 delete_instr(list + idx);
1998 vim_free(list);
1999 }
2000 break;
2001
2002 case ISN_LOADS:
2003 case ISN_STORES:
2004 vim_free(isn->isn_arg.loadstore.ls_name);
2005 break;
2006
2007 case ISN_UNLET:
2008 case ISN_UNLETENV:
2009 vim_free(isn->isn_arg.unlet.ul_name);
2010 break;
2011
2012 case ISN_STOREOPT:
2013 case ISN_STOREFUNCOPT:
2014 vim_free(isn->isn_arg.storeopt.so_name);
2015 break;
2016
2017 case ISN_PUSHBLOB: // push blob isn_arg.blob
2018 blob_unref(isn->isn_arg.blob);
2019 break;
2020
2021 case ISN_PUSHJOB:
2022#ifdef FEAT_JOB_CHANNEL
2023 job_unref(isn->isn_arg.job);
2024#endif
2025 break;
2026
2027 case ISN_PUSHCHANNEL:
2028#ifdef FEAT_JOB_CHANNEL
2029 channel_unref(isn->isn_arg.channel);
2030#endif
2031 break;
2032
2033 case ISN_UCALL:
2034 vim_free(isn->isn_arg.ufunc.cuf_name);
2035 break;
2036
2037 case ISN_FUNCREF:
2038 {
2039 if (isn->isn_arg.funcref.fr_func_name == NULL)
2040 {
2041 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2042 + isn->isn_arg.funcref.fr_dfunc_idx;
2043 ufunc_T *ufunc = dfunc->df_ufunc;
2044
2045 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2046 func_ptr_unref(ufunc);
2047 }
2048 else
2049 {
2050 char_u *name = isn->isn_arg.funcref.fr_func_name;
2051
2052 if (name != NULL)
2053 func_unref(name);
2054 vim_free(isn->isn_arg.funcref.fr_func_name);
2055 }
2056 }
2057 break;
2058
2059 case ISN_DCALL:
2060 {
2061 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2062 + isn->isn_arg.dfunc.cdf_idx;
2063
2064 if (dfunc->df_ufunc != NULL
2065 && func_name_refcount(dfunc->df_ufunc->uf_name))
2066 func_ptr_unref(dfunc->df_ufunc);
2067 }
2068 break;
2069
2070 case ISN_NEWFUNC:
2071 {
2072 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
2073 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
2074
2075 if (ufunc != NULL)
2076 {
2077 unlink_def_function(ufunc);
2078 func_ptr_unref(ufunc);
2079 }
2080
2081 vim_free(lambda);
2082 vim_free(isn->isn_arg.newfunc.nf_global);
2083 }
2084 break;
2085
2086 case ISN_CHECKTYPE:
2087 case ISN_SETTYPE:
2088 free_type(isn->isn_arg.type.ct_type);
2089 break;
2090
2091 case ISN_CMDMOD:
2092 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
2093 ->cmod_filter_regmatch.regprog);
2094 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2095 break;
2096
2097 case ISN_LOADSCRIPT:
2098 case ISN_STORESCRIPT:
2099 vim_free(isn->isn_arg.script.scriptref);
2100 break;
2101
2102 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002103 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002104 break;
2105
2106 case ISN_CEXPR_CORE:
2107 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2108 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2109 break;
2110
2111 case ISN_2BOOL:
2112 case ISN_2STRING:
2113 case ISN_2STRING_ANY:
2114 case ISN_ADDBLOB:
2115 case ISN_ADDLIST:
2116 case ISN_ANYINDEX:
2117 case ISN_ANYSLICE:
2118 case ISN_BCALL:
2119 case ISN_BLOBAPPEND:
2120 case ISN_BLOBINDEX:
2121 case ISN_BLOBSLICE:
2122 case ISN_CATCH:
2123 case ISN_CEXPR_AUCMD:
2124 case ISN_CHECKLEN:
2125 case ISN_CHECKNR:
2126 case ISN_CLEARDICT:
2127 case ISN_CMDMOD_REV:
2128 case ISN_COMPAREANY:
2129 case ISN_COMPAREBLOB:
2130 case ISN_COMPAREBOOL:
2131 case ISN_COMPAREDICT:
2132 case ISN_COMPAREFLOAT:
2133 case ISN_COMPAREFUNC:
2134 case ISN_COMPARELIST:
2135 case ISN_COMPARENR:
2136 case ISN_COMPARESPECIAL:
2137 case ISN_COMPARESTRING:
2138 case ISN_CONCAT:
2139 case ISN_COND2BOOL:
2140 case ISN_DEBUG:
2141 case ISN_DROP:
2142 case ISN_ECHO:
2143 case ISN_ECHOCONSOLE:
2144 case ISN_ECHOERR:
2145 case ISN_ECHOMSG:
2146 case ISN_ENDTRY:
2147 case ISN_EXECCONCAT:
2148 case ISN_EXECUTE:
2149 case ISN_FINALLY:
2150 case ISN_FINISH:
2151 case ISN_FOR:
2152 case ISN_GETITEM:
2153 case ISN_JUMP:
2154 case ISN_JUMP_IF_ARG_SET:
2155 case ISN_LISTAPPEND:
2156 case ISN_LISTINDEX:
2157 case ISN_LISTSLICE:
2158 case ISN_LOAD:
2159 case ISN_LOADBDICT:
2160 case ISN_LOADGDICT:
2161 case ISN_LOADOUTER:
2162 case ISN_LOADREG:
2163 case ISN_LOADTDICT:
2164 case ISN_LOADV:
2165 case ISN_LOADWDICT:
2166 case ISN_LOCKCONST:
2167 case ISN_MEMBER:
2168 case ISN_NEGATENR:
2169 case ISN_NEWDICT:
2170 case ISN_NEWLIST:
2171 case ISN_OPANY:
2172 case ISN_OPFLOAT:
2173 case ISN_OPNR:
2174 case ISN_PCALL:
2175 case ISN_PCALL_END:
2176 case ISN_PROF_END:
2177 case ISN_PROF_START:
2178 case ISN_PUSHBOOL:
2179 case ISN_PUSHF:
2180 case ISN_PUSHNR:
2181 case ISN_PUSHSPEC:
2182 case ISN_PUT:
2183 case ISN_REDIREND:
2184 case ISN_REDIRSTART:
2185 case ISN_RETURN:
2186 case ISN_RETURN_VOID:
2187 case ISN_SHUFFLE:
2188 case ISN_SLICE:
2189 case ISN_STORE:
2190 case ISN_STOREINDEX:
2191 case ISN_STORENR:
2192 case ISN_STOREOUTER:
2193 case ISN_STORERANGE:
2194 case ISN_STOREREG:
2195 case ISN_STOREV:
2196 case ISN_STRINDEX:
2197 case ISN_STRSLICE:
2198 case ISN_THROW:
2199 case ISN_TRYCONT:
2200 case ISN_UNLETINDEX:
2201 case ISN_UNLETRANGE:
2202 case ISN_UNPACK:
2203 case ISN_USEDICT:
2204 // nothing allocated
2205 break;
2206 }
2207}
2208
2209 void
2210clear_instr_ga(garray_T *gap)
2211{
2212 int idx;
2213
2214 for (idx = 0; idx < gap->ga_len; ++idx)
2215 delete_instr(((isn_T *)gap->ga_data) + idx);
2216 ga_clear(gap);
2217}
2218
2219
2220#endif // defined(FEAT_EVAL)