blob: ae4de33f41e37a6b10c4f8aaa41d6791c5d15e27 [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{
Bram Moolenaardc7c3662021-12-20 15:04:29 +000060 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +000061 cctx->ctx_type_stack.ga_len -= drop;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000062 return generate_instr(cctx, isn_type);
63}
64
65/*
Bram Moolenaar078a4612022-01-04 15:17:03 +000066 * Generate instruction "isn_type" and put "type" on the type stack,
67 * use "decl_type" for the declared type.
Bram Moolenaardc7c3662021-12-20 15:04:29 +000068 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +000069 static isn_T *
Bram Moolenaar078a4612022-01-04 15:17:03 +000070generate_instr_type2(
71 cctx_T *cctx,
72 isntype_T isn_type,
73 type_T *type,
74 type_T *decl_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000075{
76 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000077
78 if ((isn = generate_instr(cctx, isn_type)) == NULL)
79 return NULL;
80
Bram Moolenaar078a4612022-01-04 15:17:03 +000081 if (push_type_stack2(cctx, type == NULL ? &t_any : type,
82 decl_type == NULL ? &t_any : decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000083 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000084
85 return isn;
86}
87
88/*
Bram Moolenaar078a4612022-01-04 15:17:03 +000089 * Generate instruction "isn_type" and put "type" on the type stack.
90 * Uses "any" for the declared type, which works for constants. For declared
91 * variables use generate_instr_type2().
92 */
93 isn_T *
94generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
95{
96 return generate_instr_type2(cctx, isn_type, type, &t_any);
97}
98
99/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000100 * Generate an ISN_DEBUG instruction.
101 */
102 isn_T *
103generate_instr_debug(cctx_T *cctx)
104{
105 isn_T *isn;
106 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
107 + cctx->ctx_ufunc->uf_dfunc_idx;
108
109 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
110 return NULL;
111 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
112 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
113 return isn;
114}
115
116/*
117 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
118 * But only for simple types.
119 * When "tolerant" is TRUE convert most types to string, e.g. a List.
120 */
121 int
122may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
123{
124 isn_T *isn;
125 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000126 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000127
128 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000129 type = get_type_on_stack(cctx, -1 - offset);
130 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000131 {
132 // nothing to be done
133 case VAR_STRING: return OK;
134
135 // conversion possible
136 case VAR_SPECIAL:
137 case VAR_BOOL:
138 case VAR_NUMBER:
139 case VAR_FLOAT:
140 break;
141
142 // conversion possible (with runtime check)
143 case VAR_ANY:
144 case VAR_UNKNOWN:
145 isntype = ISN_2STRING_ANY;
146 break;
147
148 // conversion possible when tolerant
149 case VAR_LIST:
150 if (tolerant)
151 {
152 isntype = ISN_2STRING_ANY;
153 break;
154 }
155 // FALLTHROUGH
156
157 // conversion not possible
158 case VAR_VOID:
159 case VAR_BLOB:
160 case VAR_FUNC:
161 case VAR_PARTIAL:
162 case VAR_DICT:
163 case VAR_JOB:
164 case VAR_CHANNEL:
165 case VAR_INSTR:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000166 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000167 return FAIL;
168 }
169
Bram Moolenaar078a4612022-01-04 15:17:03 +0000170 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000171 if ((isn = generate_instr(cctx, isntype)) == NULL)
172 return FAIL;
173 isn->isn_arg.tostring.offset = offset;
174 isn->isn_arg.tostring.tolerant = tolerant;
175
176 return OK;
177}
178
179 static int
180check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
181{
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000182 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
183 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000184 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000185 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000186 {
187 if (*op == '+')
188 emsg(_(e_wrong_argument_type_for_plus));
189 else
190 semsg(_(e_char_requires_number_or_float_arguments), *op);
191 return FAIL;
192 }
193 return OK;
194}
195
196/*
197 * Generate instruction for "+". For a list this creates a new list.
198 */
199 int
200generate_add_instr(
201 cctx_T *cctx,
202 vartype_T vartype,
203 type_T *type1,
204 type_T *type2,
205 exprtype_T expr_type)
206{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000207 isn_T *isn = generate_instr_drop(cctx,
208 vartype == VAR_NUMBER ? ISN_OPNR
209 : vartype == VAR_LIST ? ISN_ADDLIST
210 : vartype == VAR_BLOB ? ISN_ADDBLOB
211#ifdef FEAT_FLOAT
212 : vartype == VAR_FLOAT ? ISN_OPFLOAT
213#endif
214 : ISN_OPANY, 1);
215
216 if (vartype != VAR_LIST && vartype != VAR_BLOB
217 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000218 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000219 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000220 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000221 && check_number_or_float(
222 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
223 return FAIL;
224
225 if (isn != NULL)
226 {
227 if (isn->isn_type == ISN_ADDLIST)
228 isn->isn_arg.op.op_type = expr_type;
229 else
230 isn->isn_arg.op.op_type = EXPR_ADD;
231 }
232
233 // When concatenating two lists with different member types the member type
234 // becomes "any".
235 if (vartype == VAR_LIST
236 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
237 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000238 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000239
240 return isn == NULL ? FAIL : OK;
241}
242
243/*
244 * Get the type to use for an instruction for an operation on "type1" and
245 * "type2". If they are matching use a type-specific instruction. Otherwise
246 * fall back to runtime type checking.
247 */
248 vartype_T
249operator_type(type_T *type1, type_T *type2)
250{
251 if (type1->tt_type == type2->tt_type
252 && (type1->tt_type == VAR_NUMBER
253 || type1->tt_type == VAR_LIST
254#ifdef FEAT_FLOAT
255 || type1->tt_type == VAR_FLOAT
256#endif
257 || type1->tt_type == VAR_BLOB))
258 return type1->tt_type;
259 return VAR_ANY;
260}
261
262/*
263 * Generate an instruction with two arguments. The instruction depends on the
264 * type of the arguments.
265 */
266 int
267generate_two_op(cctx_T *cctx, char_u *op)
268{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000269 type_T *type1;
270 type_T *type2;
271 vartype_T vartype;
272 isn_T *isn;
273
274 RETURN_OK_IF_SKIP(cctx);
275
276 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000277 type1 = get_type_on_stack(cctx, 1);
278 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000279 vartype = operator_type(type1, type2);
280
281 switch (*op)
282 {
283 case '+':
284 if (generate_add_instr(cctx, vartype, type1, type2,
285 EXPR_COPY) == FAIL)
286 return FAIL;
287 break;
288
289 case '-':
290 case '*':
291 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
292 op) == FAIL)
293 return FAIL;
294 if (vartype == VAR_NUMBER)
295 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
296#ifdef FEAT_FLOAT
297 else if (vartype == VAR_FLOAT)
298 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
299#endif
300 else
301 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
302 if (isn != NULL)
303 isn->isn_arg.op.op_type = *op == '*'
304 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
305 break;
306
307 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000308 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000309 && type1->tt_type != VAR_NUMBER)
310 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000311 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000312 && type2->tt_type != VAR_NUMBER))
313 {
314 emsg(_(e_percent_requires_number_arguments));
315 return FAIL;
316 }
317 isn = generate_instr_drop(cctx,
318 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
319 if (isn != NULL)
320 isn->isn_arg.op.op_type = EXPR_REM;
321 break;
322 }
323
324 // correct type of result
325 if (vartype == VAR_ANY)
326 {
327 type_T *type = &t_any;
328
329#ifdef FEAT_FLOAT
330 // float+number and number+float results in float
331 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
332 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
333 type = &t_float;
334#endif
Bram Moolenaar078a4612022-01-04 15:17:03 +0000335 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000336 }
337
338 return OK;
339}
340
341/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000342 * Get the instruction to use for comparing two values with specified types.
343 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000344 * Return ISN_DROP when failed.
345 */
346 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000347get_compare_isn(
348 exprtype_T exprtype,
349 typval_T *tv1,
350 typval_T *tv2,
351 type_T *type1,
352 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000353{
354 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000355 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
356 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000357
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000358 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000359 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000360 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000361 {
362 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
363 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
364 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
365 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
366 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
367 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
368 case VAR_LIST: isntype = ISN_COMPARELIST; break;
369 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
370 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
371 default: isntype = ISN_COMPAREANY; break;
372 }
373 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000374 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
375 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
376 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
377 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
378 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000379 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000380 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000381 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000382 if ((vartype1 == VAR_SPECIAL
383 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
384 : type1 == &t_none)
385 && vartype2 != VAR_STRING)
386 || (vartype2 == VAR_SPECIAL
387 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
388 : type2 == &t_none)
389 && vartype1 != VAR_STRING))
390 {
391 semsg(_(e_cannot_compare_str_with_str),
392 vartype_name(vartype1), vartype_name(vartype2));
393 return ISN_DROP;
394 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000395 // although comparing null with number, float or bool is not useful, we
396 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000397 isntype = ISN_COMPARENULL;
398 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000399
400 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
401 && (isntype == ISN_COMPAREBOOL
402 || isntype == ISN_COMPARESPECIAL
403 || isntype == ISN_COMPARENR
404 || isntype == ISN_COMPAREFLOAT))
405 {
406 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000407 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000408 return ISN_DROP;
409 }
410 if (isntype == ISN_DROP
411 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000412 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
413 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000414 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar7a222242022-03-01 19:23:24 +0000415 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000416 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
417 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000418 {
419 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000420 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000421 return ISN_DROP;
422 }
423 return isntype;
424}
425
426 int
427check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
428{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000429 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000430 return FAIL;
431 return OK;
432}
433
434/*
435 * Generate an ISN_COMPARE* instruction with a boolean result.
436 */
437 int
438generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
439{
440 isntype_T isntype;
441 isn_T *isn;
442 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000443
444 RETURN_OK_IF_SKIP(cctx);
445
446 // Get the known type of the two items on the stack. If they are matching
447 // use a type-specific instruction. Otherwise fall back to runtime type
448 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000449 isntype = get_compare_isn(exprtype, NULL, NULL,
450 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000451 if (isntype == ISN_DROP)
452 return FAIL;
453
454 if ((isn = generate_instr(cctx, isntype)) == NULL)
455 return FAIL;
456 isn->isn_arg.op.op_type = exprtype;
457 isn->isn_arg.op.op_ic = ic;
458
459 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100460 --stack->ga_len;
461 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000462
463 return OK;
464}
465
466/*
LemonBoy372bcce2022-04-25 12:43:20 +0100467 * Generate an ISN_CONCAT instruction.
468 * "count" is the number of stack elements to join together and it must be
469 * greater or equal to one.
470 * The caller ensures all the "count" elements on the stack have the right type.
471 */
472 int
473generate_CONCAT(cctx_T *cctx, int count)
474{
475 isn_T *isn;
476 garray_T *stack = &cctx->ctx_type_stack;
477
478 RETURN_OK_IF_SKIP(cctx);
479
LemonBoy372bcce2022-04-25 12:43:20 +0100480 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
481 return FAIL;
482 isn->isn_arg.number = count;
483
484 // drop the argument types
485 stack->ga_len -= count - 1;
486
487 return OK;
488}
489
490/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000491 * Generate an ISN_2BOOL instruction.
492 * "offset" is the offset in the type stack.
493 */
494 int
495generate_2BOOL(cctx_T *cctx, int invert, int offset)
496{
497 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000498
499 RETURN_OK_IF_SKIP(cctx);
500 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
501 return FAIL;
502 isn->isn_arg.tobool.invert = invert;
503 isn->isn_arg.tobool.offset = offset;
504
505 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000506 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000507
508 return OK;
509}
510
511/*
512 * Generate an ISN_COND2BOOL instruction.
513 */
514 int
515generate_COND2BOOL(cctx_T *cctx)
516{
517 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000518
519 RETURN_OK_IF_SKIP(cctx);
520 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
521 return FAIL;
522
523 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000524 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000525
526 return OK;
527}
528
529 int
530generate_TYPECHECK(
531 cctx_T *cctx,
532 type_T *expected,
533 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100534 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000535 int argidx)
536{
537 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000538
539 RETURN_OK_IF_SKIP(cctx);
540 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
541 return FAIL;
542 isn->isn_arg.type.ct_type = alloc_type(expected);
543 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100544 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000545 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
546
547 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000548 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000549
550 return OK;
551}
552
553 int
554generate_SETTYPE(
555 cctx_T *cctx,
556 type_T *expected)
557{
558 isn_T *isn;
559
560 RETURN_OK_IF_SKIP(cctx);
561 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
562 return FAIL;
563 isn->isn_arg.type.ct_type = alloc_type(expected);
564 return OK;
565}
566
567/*
568 * Generate a PUSH instruction for "tv".
569 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000570 */
571 int
572generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
573{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100574 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000575 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100576 case VAR_BOOL:
577 generate_PUSHBOOL(cctx, tv->vval.v_number);
578 break;
579 case VAR_SPECIAL:
580 generate_PUSHSPEC(cctx, tv->vval.v_number);
581 break;
582 case VAR_NUMBER:
583 generate_PUSHNR(cctx, tv->vval.v_number);
584 break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000585#ifdef FEAT_FLOAT
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100586 case VAR_FLOAT:
587 generate_PUSHF(cctx, tv->vval.v_float);
588 break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000589#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100590 case VAR_BLOB:
591 generate_PUSHBLOB(cctx, tv->vval.v_blob);
592 tv->vval.v_blob = NULL;
593 break;
594 case VAR_LIST:
595 if (tv->vval.v_list != NULL)
596 iemsg("non-empty list constant not supported");
597 generate_NEWLIST(cctx, 0, TRUE);
598 break;
599 case VAR_DICT:
600 if (tv->vval.v_dict != NULL)
601 iemsg("non-empty dict constant not supported");
602 generate_NEWDICT(cctx, 0, TRUE);
603 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000604#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100605 case VAR_JOB:
606 if (tv->vval.v_job != NULL)
607 iemsg("non-null job constant not supported");
608 generate_PUSHJOB(cctx);
609 break;
610 case VAR_CHANNEL:
611 if (tv->vval.v_channel != NULL)
612 iemsg("non-null channel constant not supported");
613 generate_PUSHCHANNEL(cctx);
614 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000615#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100616 case VAR_FUNC:
617 if (tv->vval.v_string != NULL)
618 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100619 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100620 break;
621 case VAR_PARTIAL:
622 if (tv->vval.v_partial != NULL)
623 iemsg("non-null partial constant not supported");
624 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
625 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000626 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100627 break;
628 case VAR_STRING:
629 generate_PUSHS(cctx, &tv->vval.v_string);
630 tv->vval.v_string = NULL;
631 break;
632 default:
633 siemsg("constant type %d not supported", tv->v_type);
634 clear_tv(tv);
635 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000636 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100637 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000638 return OK;
639}
640
641/*
642 * Generate an ISN_PUSHNR instruction.
643 */
644 int
645generate_PUSHNR(cctx_T *cctx, varnumber_T number)
646{
647 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000648
649 RETURN_OK_IF_SKIP(cctx);
650 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
651 return FAIL;
652 isn->isn_arg.number = number;
653
654 if (number == 0 || number == 1)
655 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000656 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000657 return OK;
658}
659
660/*
661 * Generate an ISN_PUSHBOOL instruction.
662 */
663 int
664generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
665{
666 isn_T *isn;
667
668 RETURN_OK_IF_SKIP(cctx);
669 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
670 return FAIL;
671 isn->isn_arg.number = number;
672
673 return OK;
674}
675
676/*
677 * Generate an ISN_PUSHSPEC instruction.
678 */
679 int
680generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
681{
682 isn_T *isn;
683
684 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000685 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
686 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000687 return FAIL;
688 isn->isn_arg.number = number;
689
690 return OK;
691}
692
693#if defined(FEAT_FLOAT) || defined(PROTO)
694/*
695 * Generate an ISN_PUSHF instruction.
696 */
697 int
698generate_PUSHF(cctx_T *cctx, float_T fnumber)
699{
700 isn_T *isn;
701
702 RETURN_OK_IF_SKIP(cctx);
703 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
704 return FAIL;
705 isn->isn_arg.fnumber = fnumber;
706
707 return OK;
708}
709#endif
710
711/*
712 * Generate an ISN_PUSHS instruction.
713 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100714 * Note that if "str" is used in the instruction OK is returned and "*str" is
715 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000716 */
717 int
718generate_PUSHS(cctx_T *cctx, char_u **str)
719{
720 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100721 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000722
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100723 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000724 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100725 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
726 ret = FAIL;
727 else
728 {
729 isn->isn_arg.string = str == NULL ? NULL : *str;
730 return OK;
731 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000732 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100733 if (str != NULL)
734 VIM_CLEAR(*str);
735 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000736}
737
738/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000739 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000740 */
741 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000742generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000743{
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100744#ifdef FEAT_JOB_CHANNEL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000745 isn_T *isn;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100746#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000747
748 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100749#ifdef FEAT_JOB_CHANNEL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000750 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
751 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000752 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100753#else
754 emsg(_(e_channel_job_feature_not_available));
755 return FAIL;
756#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000757}
758
759/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000760 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000761 */
762 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000763generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000764{
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100765#ifdef FEAT_JOB_CHANNEL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000766 isn_T *isn;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100767#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000768
769 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100770#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000771 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_job)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000772 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000773 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100774#else
775 emsg(_(e_channel_job_feature_not_available));
776 return FAIL;
777#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000778}
779
780/*
781 * Generate an ISN_PUSHBLOB instruction.
782 * Consumes "blob".
783 */
784 int
785generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
786{
787 isn_T *isn;
788
789 RETURN_OK_IF_SKIP(cctx);
790 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
791 return FAIL;
792 isn->isn_arg.blob = blob;
793
794 return OK;
795}
796
797/*
798 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100799 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
800 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000801 */
802 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100803generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000804{
805 isn_T *isn;
806 char_u *funcname;
807
808 RETURN_OK_IF_SKIP(cctx);
809 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
810 return FAIL;
811 if (name == NULL)
812 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100813 else if (!may_prefix
814 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000815 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000816 funcname = vim_strsave(name);
817 else
818 {
819 funcname = alloc(STRLEN(name) + 3);
820 if (funcname != NULL)
821 {
822 STRCPY(funcname, "g:");
823 STRCPY(funcname + 2, name);
824 }
825 }
826
827 isn->isn_arg.string = funcname;
828 return OK;
829}
830
831/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000832 * Generate an ISN_AUTOLOAD instruction.
833 */
834 int
835generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
836{
837 isn_T *isn;
838
839 RETURN_OK_IF_SKIP(cctx);
840 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
841 return FAIL;
842 isn->isn_arg.string = vim_strsave(name);
843 if (isn->isn_arg.string == NULL)
844 return FAIL;
845 return OK;
846}
847
848/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000849 * Generate an ISN_GETITEM instruction with "index".
850 * "with_op" is TRUE for "+=" and other operators, the stack has the current
851 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100852 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853 */
854 int
855generate_GETITEM(cctx_T *cctx, int index, int with_op)
856{
857 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000858 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000859 type_T *item_type = &t_any;
860
861 RETURN_OK_IF_SKIP(cctx);
862
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000863 item_type = type->tt_member;
864 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
865 return FAIL;
866 isn->isn_arg.getitem.gi_index = index;
867 isn->isn_arg.getitem.gi_with_op = with_op;
868
869 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000870 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000871}
872
873/*
874 * Generate an ISN_SLICE instruction with "count".
875 */
876 int
877generate_SLICE(cctx_T *cctx, int count)
878{
879 isn_T *isn;
880
881 RETURN_OK_IF_SKIP(cctx);
882 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
883 return FAIL;
884 isn->isn_arg.number = count;
885 return OK;
886}
887
888/*
889 * Generate an ISN_CHECKLEN instruction with "min_len".
890 */
891 int
892generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
893{
894 isn_T *isn;
895
896 RETURN_OK_IF_SKIP(cctx);
897
898 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
899 return FAIL;
900 isn->isn_arg.checklen.cl_min_len = min_len;
901 isn->isn_arg.checklen.cl_more_OK = more_OK;
902
903 return OK;
904}
905
906/*
907 * Generate an ISN_STORE instruction.
908 */
909 int
910generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
911{
912 isn_T *isn;
913
914 RETURN_OK_IF_SKIP(cctx);
915 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
916 return FAIL;
917 if (name != NULL)
918 isn->isn_arg.string = vim_strsave(name);
919 else
920 isn->isn_arg.number = idx;
921
922 return OK;
923}
924
925/*
926 * Generate an ISN_STOREOUTER instruction.
927 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000928 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000929generate_STOREOUTER(cctx_T *cctx, int idx, int level)
930{
931 isn_T *isn;
932
933 RETURN_OK_IF_SKIP(cctx);
934 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
935 return FAIL;
936 isn->isn_arg.outer.outer_idx = idx;
937 isn->isn_arg.outer.outer_depth = level;
938
939 return OK;
940}
941
942/*
943 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
944 */
945 int
946generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
947{
948 isn_T *isn;
949
950 RETURN_OK_IF_SKIP(cctx);
951 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
952 return FAIL;
953 isn->isn_arg.storenr.stnr_idx = idx;
954 isn->isn_arg.storenr.stnr_val = value;
955
956 return OK;
957}
958
959/*
960 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
961 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000962 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000963generate_STOREOPT(
964 cctx_T *cctx,
965 isntype_T isn_type,
966 char_u *name,
967 int opt_flags)
968{
969 isn_T *isn;
970
971 RETURN_OK_IF_SKIP(cctx);
972 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
973 return FAIL;
974 isn->isn_arg.storeopt.so_name = vim_strsave(name);
975 isn->isn_arg.storeopt.so_flags = opt_flags;
976
977 return OK;
978}
979
980/*
981 * Generate an ISN_LOAD or similar instruction.
982 */
983 int
984generate_LOAD(
985 cctx_T *cctx,
986 isntype_T isn_type,
987 int idx,
988 char_u *name,
989 type_T *type)
990{
991 isn_T *isn;
992
993 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000994 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000995 return FAIL;
996 if (name != NULL)
997 isn->isn_arg.string = vim_strsave(name);
998 else
999 isn->isn_arg.number = idx;
1000
1001 return OK;
1002}
1003
1004/*
1005 * Generate an ISN_LOADOUTER instruction
1006 */
1007 int
1008generate_LOADOUTER(
1009 cctx_T *cctx,
1010 int idx,
1011 int nesting,
1012 type_T *type)
1013{
1014 isn_T *isn;
1015
1016 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001017 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001018 return FAIL;
1019 isn->isn_arg.outer.outer_idx = idx;
1020 isn->isn_arg.outer.outer_depth = nesting;
1021
1022 return OK;
1023}
1024
1025/*
1026 * Generate an ISN_LOADV instruction for v:var.
1027 */
1028 int
1029generate_LOADV(
1030 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001031 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001032{
1033 int di_flags;
1034 int vidx = find_vim_var(name, &di_flags);
1035 type_T *type;
1036
1037 RETURN_OK_IF_SKIP(cctx);
1038 if (vidx < 0)
1039 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001040 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001041 return FAIL;
1042 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001043 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001044 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1045}
1046
1047/*
1048 * Generate an ISN_UNLET instruction.
1049 */
1050 int
1051generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1052{
1053 isn_T *isn;
1054
1055 RETURN_OK_IF_SKIP(cctx);
1056 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1057 return FAIL;
1058 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1059 isn->isn_arg.unlet.ul_forceit = forceit;
1060
1061 return OK;
1062}
1063
1064/*
1065 * Generate an ISN_LOCKCONST instruction.
1066 */
1067 int
1068generate_LOCKCONST(cctx_T *cctx)
1069{
1070 isn_T *isn;
1071
1072 RETURN_OK_IF_SKIP(cctx);
1073 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1074 return FAIL;
1075 return OK;
1076}
1077
1078/*
1079 * Generate an ISN_LOADS instruction.
1080 */
1081 int
1082generate_OLDSCRIPT(
1083 cctx_T *cctx,
1084 isntype_T isn_type,
1085 char_u *name,
1086 int sid,
1087 type_T *type)
1088{
1089 isn_T *isn;
1090
1091 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001092 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001093 isn = generate_instr_type(cctx, isn_type, type);
1094 else
1095 isn = generate_instr_drop(cctx, isn_type, 1);
1096 if (isn == NULL)
1097 return FAIL;
1098 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1099 isn->isn_arg.loadstore.ls_sid = sid;
1100
1101 return OK;
1102}
1103
1104/*
1105 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1106 */
1107 int
1108generate_VIM9SCRIPT(
1109 cctx_T *cctx,
1110 isntype_T isn_type,
1111 int sid,
1112 int idx,
1113 type_T *type)
1114{
1115 isn_T *isn;
1116 scriptref_T *sref;
1117 scriptitem_T *si = SCRIPT_ITEM(sid);
1118
1119 RETURN_OK_IF_SKIP(cctx);
1120 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001121 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001122 else
1123 isn = generate_instr_drop(cctx, isn_type, 1);
1124 if (isn == NULL)
1125 return FAIL;
1126
1127 // This requires three arguments, which doesn't fit in an instruction, thus
1128 // we need to allocate a struct for this.
1129 sref = ALLOC_ONE(scriptref_T);
1130 if (sref == NULL)
1131 return FAIL;
1132 isn->isn_arg.script.scriptref = sref;
1133 sref->sref_sid = sid;
1134 sref->sref_idx = idx;
1135 sref->sref_seq = si->sn_script_seq;
1136 sref->sref_type = type;
1137 return OK;
1138}
1139
1140/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001141 * Generate an ISN_NEWLIST instruction for "count" items.
1142 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001143 */
1144 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001145generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001146{
1147 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001148 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001149 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001150 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001151
1152 RETURN_OK_IF_SKIP(cctx);
1153 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1154 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001155 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001156
Bram Moolenaar078a4612022-01-04 15:17:03 +00001157 // Get the member type and the declared member type from all the items on
1158 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001159 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001160 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001161 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001162
1163 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001164 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001165
1166 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001167 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001168}
1169
1170/*
1171 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001172 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001173 */
1174 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001175generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001176{
1177 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001178 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001179 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001180 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001181
1182 RETURN_OK_IF_SKIP(cctx);
1183 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1184 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001185 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001186
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001187 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001188 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001189 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001190
1191 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001192 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001193
1194 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001195 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001196}
1197
1198/*
1199 * Generate an ISN_FUNCREF instruction.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001200 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001201 */
1202 int
Bram Moolenaara915fa02022-03-23 11:29:15 +00001203generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc, isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001204{
1205 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001206 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001207
1208 RETURN_OK_IF_SKIP(cctx);
1209 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1210 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001211 if (isnp != NULL)
1212 *isnp = isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001213 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1214 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1215 else
1216 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
1217 cctx->ctx_has_closure = 1;
1218
1219 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001220 // the nested context, including this one. But not a function defined at
1221 // the script level.
1222 if ((ufunc->uf_flags & FC_CLOSURE)
1223 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001224 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1225
Bram Moolenaar078a4612022-01-04 15:17:03 +00001226 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1227 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001228}
1229
1230/*
1231 * Generate an ISN_NEWFUNC instruction.
1232 * "lambda_name" and "func_name" must be in allocated memory and will be
1233 * consumed.
1234 */
1235 int
1236generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1237{
1238 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001239 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001240
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001241 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001242 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001243 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1244 ret = FAIL;
1245 else
1246 {
1247 isn->isn_arg.newfunc.nf_lambda = lambda_name;
1248 isn->isn_arg.newfunc.nf_global = func_name;
1249 return OK;
1250 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001251 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001252 vim_free(lambda_name);
1253 vim_free(func_name);
1254 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001255}
1256
1257/*
1258 * Generate an ISN_DEF instruction: list functions
1259 */
1260 int
1261generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1262{
1263 isn_T *isn;
1264
1265 RETURN_OK_IF_SKIP(cctx);
1266 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1267 return FAIL;
1268 if (len > 0)
1269 {
1270 isn->isn_arg.string = vim_strnsave(name, len);
1271 if (isn->isn_arg.string == NULL)
1272 return FAIL;
1273 }
1274 return OK;
1275}
1276
1277/*
1278 * Generate an ISN_JUMP instruction.
1279 */
1280 int
1281generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1282{
1283 isn_T *isn;
1284 garray_T *stack = &cctx->ctx_type_stack;
1285
1286 RETURN_OK_IF_SKIP(cctx);
1287 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1288 return FAIL;
1289 isn->isn_arg.jump.jump_when = when;
1290 isn->isn_arg.jump.jump_where = where;
1291
1292 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1293 --stack->ga_len;
1294
1295 return OK;
1296}
1297
1298/*
1299 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1300 */
1301 int
1302generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1303{
1304 isn_T *isn;
1305
1306 RETURN_OK_IF_SKIP(cctx);
1307 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1308 return FAIL;
1309 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1310 // jump_where is set later
1311 return OK;
1312}
1313
1314 int
1315generate_FOR(cctx_T *cctx, int loop_idx)
1316{
1317 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001318
1319 RETURN_OK_IF_SKIP(cctx);
1320 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1321 return FAIL;
1322 isn->isn_arg.forloop.for_idx = loop_idx;
1323
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001324 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001325 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001326}
1327/*
1328 * Generate an ISN_TRYCONT instruction.
1329 */
1330 int
1331generate_TRYCONT(cctx_T *cctx, int levels, int where)
1332{
1333 isn_T *isn;
1334
1335 RETURN_OK_IF_SKIP(cctx);
1336 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1337 return FAIL;
1338 isn->isn_arg.trycont.tct_levels = levels;
1339 isn->isn_arg.trycont.tct_where = where;
1340
1341 return OK;
1342}
1343
1344
1345/*
1346 * Generate an ISN_BCALL instruction.
1347 * "method_call" is TRUE for "value->method()"
1348 * Return FAIL if the number of arguments is wrong.
1349 */
1350 int
1351generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1352{
1353 isn_T *isn;
1354 garray_T *stack = &cctx->ctx_type_stack;
1355 int argoff;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001356 type2_T *typep;
1357 type2_T *argtypes = NULL;
1358 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1359 type2_T *maptype = NULL;
1360 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001361 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001362
1363 RETURN_OK_IF_SKIP(cctx);
1364 argoff = check_internal_func(func_idx, argcount);
1365 if (argoff < 0)
1366 return FAIL;
1367
1368 if (method_call && argoff > 1)
1369 {
1370 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1371 return FAIL;
1372 isn->isn_arg.shuffle.shfl_item = argcount;
1373 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1374 }
1375
1376 if (argcount > 0)
1377 {
1378 // Check the types of the arguments.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001379 typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001380 if (method_call && argoff > 1)
1381 {
1382 int i;
1383
1384 for (i = 0; i < argcount; ++i)
1385 shuffled_argtypes[i] = (i < argoff - 1)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001386 ? typep[i + 1]
1387 : (i == argoff - 1) ? typep[0] : typep[i];
1388 argtypes = shuffled_argtypes;
1389 }
1390 else
1391 {
1392 int i;
1393
1394 for (i = 0; i < argcount; ++i)
1395 shuffled_argtypes[i] = typep[i];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001396 argtypes = shuffled_argtypes;
1397 }
1398 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1399 cctx) == FAIL)
1400 return FAIL;
1401 if (internal_func_is_map(func_idx))
Bram Moolenaar078a4612022-01-04 15:17:03 +00001402 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001403 }
1404
1405 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1406 return FAIL;
1407 isn->isn_arg.bfunc.cbf_idx = func_idx;
1408 isn->isn_arg.bfunc.cbf_argcount = argcount;
1409
1410 // Drop the argument types and push the return type.
1411 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001412 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1413 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001414 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001415
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001416 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1417 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001418 // Check that map() didn't change the item types.
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01001419 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001420
1421 return OK;
1422}
1423
1424/*
1425 * Generate an ISN_LISTAPPEND instruction. Works like add().
1426 * Argument count is already checked.
1427 */
1428 int
1429generate_LISTAPPEND(cctx_T *cctx)
1430{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001431 type_T *list_type;
1432 type_T *item_type;
1433 type_T *expected;
1434
1435 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001436 // For checking the item type we use the declared type of the list and the
1437 // current type of the added item, adding a string to [1, 2] is OK.
1438 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001439 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001440 expected = list_type->tt_member;
1441 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1442 return FAIL;
1443
1444 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1445 return FAIL;
1446
Bram Moolenaar078a4612022-01-04 15:17:03 +00001447 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001448 return OK;
1449}
1450
1451/*
1452 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1453 * Argument count is already checked.
1454 */
1455 int
1456generate_BLOBAPPEND(cctx_T *cctx)
1457{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001458 type_T *item_type;
1459
1460 // Caller already checked that blob_type is a blob.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001461 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001462 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1463 return FAIL;
1464
1465 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1466 return FAIL;
1467
Bram Moolenaar078a4612022-01-04 15:17:03 +00001468 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001469 return OK;
1470}
1471
1472/*
1473 * Generate an ISN_DCALL or ISN_UCALL instruction.
1474 * Return FAIL if the number of arguments is wrong.
1475 */
1476 int
1477generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1478{
1479 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001480 int regular_args = ufunc->uf_args.ga_len;
1481 int argcount = pushed_argcount;
1482
1483 RETURN_OK_IF_SKIP(cctx);
1484 if (argcount > regular_args && !has_varargs(ufunc))
1485 {
1486 semsg(_(e_too_many_arguments_for_function_str),
1487 printable_func_name(ufunc));
1488 return FAIL;
1489 }
1490 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1491 {
1492 semsg(_(e_not_enough_arguments_for_function_str),
1493 printable_func_name(ufunc));
1494 return FAIL;
1495 }
1496
1497 if (ufunc->uf_def_status != UF_NOT_COMPILED
1498 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1499 {
1500 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001501 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001502
1503 for (i = 0; i < argcount; ++i)
1504 {
1505 type_T *expected;
1506 type_T *actual;
1507
Bram Moolenaar078a4612022-01-04 15:17:03 +00001508 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001509 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001510 && i >= regular_args - ufunc->uf_def_args.ga_len)
1511 {
1512 // assume v:none used for default argument value
1513 continue;
1514 }
1515 if (i < regular_args)
1516 {
1517 if (ufunc->uf_arg_types == NULL)
1518 continue;
1519 expected = ufunc->uf_arg_types[i];
1520 }
1521 else if (ufunc->uf_va_type == NULL
1522 || ufunc->uf_va_type == &t_list_any)
1523 // possibly a lambda or "...: any"
1524 expected = &t_any;
1525 else
1526 expected = ufunc->uf_va_type->tt_member;
1527 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1528 TRUE, FALSE) == FAIL)
1529 {
1530 arg_type_mismatch(expected, actual, i + 1);
1531 return FAIL;
1532 }
1533 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001534 compile_type = get_compile_type(ufunc);
1535 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001536 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001537 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001538 return FAIL;
1539 }
1540 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1541 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001542 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001543 ufunc->uf_name);
1544 return FAIL;
1545 }
1546
1547 if ((isn = generate_instr(cctx,
1548 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1549 : ISN_UCALL)) == NULL)
1550 return FAIL;
1551 if (isn->isn_type == ISN_DCALL)
1552 {
1553 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1554 isn->isn_arg.dfunc.cdf_argcount = argcount;
1555 }
1556 else
1557 {
1558 // A user function may be deleted and redefined later, can't use the
1559 // ufunc pointer, need to look it up again at runtime.
1560 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1561 isn->isn_arg.ufunc.cuf_argcount = argcount;
1562 }
1563
Bram Moolenaar078a4612022-01-04 15:17:03 +00001564 // drop the argument types
1565 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001566
Bram Moolenaar078a4612022-01-04 15:17:03 +00001567 // add return type
1568 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001569}
1570
1571/*
1572 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1573 */
1574 int
1575generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1576{
1577 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001578
1579 RETURN_OK_IF_SKIP(cctx);
1580 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1581 return FAIL;
1582 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1583 isn->isn_arg.ufunc.cuf_argcount = argcount;
1584
Bram Moolenaar078a4612022-01-04 15:17:03 +00001585 // drop the argument types
1586 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001587
Bram Moolenaar078a4612022-01-04 15:17:03 +00001588 // add return value
1589 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001590}
1591
1592/*
1593 * Generate an ISN_PCALL instruction.
1594 * "type" is the type of the FuncRef.
1595 */
1596 int
1597generate_PCALL(
1598 cctx_T *cctx,
1599 int argcount,
1600 char_u *name,
1601 type_T *type,
1602 int at_top)
1603{
1604 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001605 type_T *ret_type;
1606
1607 RETURN_OK_IF_SKIP(cctx);
1608
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001609 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001610 ret_type = &t_any;
1611 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1612 {
1613 if (type->tt_argcount != -1)
1614 {
1615 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1616
1617 if (argcount < type->tt_min_argcount - varargs)
1618 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001619 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001620 return FAIL;
1621 }
1622 if (!varargs && argcount > type->tt_argcount)
1623 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001624 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001625 return FAIL;
1626 }
1627 if (type->tt_args != NULL)
1628 {
1629 int i;
1630
1631 for (i = 0; i < argcount; ++i)
1632 {
1633 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001634 type_T *actual = get_type_on_stack(cctx, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001635 type_T *expected;
1636
1637 if (varargs && i >= type->tt_argcount - 1)
1638 expected = type->tt_args[
1639 type->tt_argcount - 1]->tt_member;
1640 else if (i >= type->tt_min_argcount
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001641 && actual->tt_type == VAR_SPECIAL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001642 expected = &t_any;
1643 else
1644 expected = type->tt_args[i];
1645 if (need_type(actual, expected, offset, i + 1,
1646 cctx, TRUE, FALSE) == FAIL)
1647 {
1648 arg_type_mismatch(expected, actual, i + 1);
1649 return FAIL;
1650 }
1651 }
1652 }
1653 }
1654 ret_type = type->tt_member;
1655 if (ret_type == &t_unknown)
1656 // return type not known yet, use a runtime check
1657 ret_type = &t_any;
1658 }
1659 else
1660 {
1661 semsg(_(e_not_callable_type_str), name);
1662 return FAIL;
1663 }
1664
1665 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1666 return FAIL;
1667 isn->isn_arg.pfunc.cpf_top = at_top;
1668 isn->isn_arg.pfunc.cpf_argcount = argcount;
1669
Bram Moolenaar078a4612022-01-04 15:17:03 +00001670 // drop the arguments and the funcref/partial
1671 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001672
Bram Moolenaar078a4612022-01-04 15:17:03 +00001673 // push the return value
1674 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001675
1676 // If partial is above the arguments it must be cleared and replaced with
1677 // the return value.
1678 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1679 return FAIL;
1680
1681 return OK;
1682}
1683
1684/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001685 * Generate an ISN_DEFER instruction.
1686 */
1687 int
1688generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
1689{
1690 isn_T *isn;
1691
1692 RETURN_OK_IF_SKIP(cctx);
1693 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
1694 return FAIL;
1695 isn->isn_arg.defer.defer_var_idx = var_idx;
1696 isn->isn_arg.defer.defer_argcount = argcount;
1697 return OK;
1698}
1699
1700/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001701 * Generate an ISN_STRINGMEMBER instruction.
1702 */
1703 int
1704generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1705{
1706 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001707 type_T *type;
1708
1709 RETURN_OK_IF_SKIP(cctx);
1710 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1711 return FAIL;
1712 isn->isn_arg.string = vim_strnsave(name, len);
1713
1714 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001715 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001716 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001717 {
1718 char *tofree;
1719
1720 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1721 name, type_name(type, &tofree));
1722 vim_free(tofree);
1723 return FAIL;
1724 }
1725 // change dict type to dict member type
1726 if (type->tt_type == VAR_DICT)
1727 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001728 type_T *ntype = type->tt_member == &t_unknown
1729 ? &t_any : type->tt_member;
1730 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001731 }
1732
1733 return OK;
1734}
1735
1736/*
1737 * Generate an ISN_ECHO instruction.
1738 */
1739 int
1740generate_ECHO(cctx_T *cctx, int with_white, int count)
1741{
1742 isn_T *isn;
1743
1744 RETURN_OK_IF_SKIP(cctx);
1745 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1746 return FAIL;
1747 isn->isn_arg.echo.echo_with_white = with_white;
1748 isn->isn_arg.echo.echo_count = count;
1749
1750 return OK;
1751}
1752
1753/*
1754 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1755 */
1756 int
1757generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1758{
1759 isn_T *isn;
1760
1761 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1762 return FAIL;
1763 isn->isn_arg.number = count;
1764
1765 return OK;
1766}
1767
1768/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001769 * Generate an ISN_SOURCE instruction.
1770 */
1771 int
1772generate_SOURCE(cctx_T *cctx, int sid)
1773{
1774 isn_T *isn;
1775
1776 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
1777 return FAIL;
1778 isn->isn_arg.number = sid;
1779
1780 return OK;
1781}
1782
1783/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001784 * Generate an ISN_PUT instruction.
1785 */
1786 int
1787generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1788{
1789 isn_T *isn;
1790
1791 RETURN_OK_IF_SKIP(cctx);
1792 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1793 return FAIL;
1794 isn->isn_arg.put.put_regname = regname;
1795 isn->isn_arg.put.put_lnum = lnum;
1796 return OK;
1797}
1798
1799/*
1800 * Generate an EXEC instruction that takes a string argument.
1801 * A copy is made of "line".
1802 */
1803 int
1804generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1805{
1806 isn_T *isn;
1807
1808 RETURN_OK_IF_SKIP(cctx);
1809 if ((isn = generate_instr(cctx, isntype)) == NULL)
1810 return FAIL;
1811 isn->isn_arg.string = vim_strsave(line);
1812 return OK;
1813}
1814
1815/*
1816 * Generate an EXEC instruction that takes a string argument.
1817 * "str" must be allocated, it is consumed.
1818 */
1819 int
1820generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1821{
1822 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001823 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001824
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001825 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001826 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001827 if ((isn = generate_instr(cctx, isntype)) == NULL)
1828 ret = FAIL;
1829 else
1830 {
1831 isn->isn_arg.string = str;
1832 return OK;
1833 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001834 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001835 vim_free(str);
1836 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001837}
1838
1839 int
1840generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1841{
1842 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001843
1844 RETURN_OK_IF_SKIP(cctx);
1845 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1846 return FAIL;
1847 isn->isn_arg.string = vim_strsave(line);
1848
Bram Moolenaar078a4612022-01-04 15:17:03 +00001849 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001850}
1851
1852 int
1853generate_EXECCONCAT(cctx_T *cctx, int count)
1854{
1855 isn_T *isn;
1856
1857 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1858 return FAIL;
1859 isn->isn_arg.number = count;
1860 return OK;
1861}
1862
1863/*
1864 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1865 */
1866 int
1867generate_RANGE(cctx_T *cctx, char_u *range)
1868{
1869 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001870
1871 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1872 return FAIL;
1873 isn->isn_arg.string = range;
1874
Bram Moolenaar078a4612022-01-04 15:17:03 +00001875 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001876}
1877
1878 int
1879generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1880{
1881 isn_T *isn;
1882
1883 RETURN_OK_IF_SKIP(cctx);
1884 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1885 return FAIL;
1886 isn->isn_arg.unpack.unp_count = var_count;
1887 isn->isn_arg.unpack.unp_semicolon = semicolon;
1888 return OK;
1889}
1890
1891/*
1892 * Generate an instruction for any command modifiers.
1893 */
1894 int
1895generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
1896{
1897 isn_T *isn;
1898
1899 if (has_cmdmod(cmod, FALSE))
1900 {
1901 cctx->ctx_has_cmdmod = TRUE;
1902
1903 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
1904 return FAIL;
1905 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1906 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1907 return FAIL;
1908 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1909 // filter program now belongs to the instruction
1910 cmod->cmod_filter_regmatch.regprog = NULL;
1911 }
1912
1913 return OK;
1914}
1915
1916 int
1917generate_undo_cmdmods(cctx_T *cctx)
1918{
1919 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1920 return FAIL;
1921 cctx->ctx_has_cmdmod = FALSE;
1922 return OK;
1923}
1924
1925/*
1926 * Generate a STORE instruction for "dest", not being "dest_local".
1927 * Return FAIL when out of memory.
1928 */
1929 int
1930generate_store_var(
1931 cctx_T *cctx,
1932 assign_dest_T dest,
1933 int opt_flags,
1934 int vimvaridx,
1935 int scriptvar_idx,
1936 int scriptvar_sid,
1937 type_T *type,
1938 char_u *name)
1939{
1940 switch (dest)
1941 {
1942 case dest_option:
1943 return generate_STOREOPT(cctx, ISN_STOREOPT,
1944 skip_option_env_lead(name), opt_flags);
1945 case dest_func_option:
1946 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
1947 skip_option_env_lead(name), opt_flags);
1948 case dest_global:
1949 // include g: with the name, easier to execute that way
1950 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
1951 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
1952 case dest_buffer:
1953 // include b: with the name, easier to execute that way
1954 return generate_STORE(cctx, ISN_STOREB, 0, name);
1955 case dest_window:
1956 // include w: with the name, easier to execute that way
1957 return generate_STORE(cctx, ISN_STOREW, 0, name);
1958 case dest_tab:
1959 // include t: with the name, easier to execute that way
1960 return generate_STORE(cctx, ISN_STORET, 0, name);
1961 case dest_env:
1962 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
1963 case dest_reg:
1964 return generate_STORE(cctx, ISN_STOREREG,
1965 name[1] == '@' ? '"' : name[1], NULL);
1966 case dest_vimvar:
1967 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
1968 case dest_script:
1969 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001970 {
1971 isntype_T isn_type = ISN_STORES;
1972
1973 if (SCRIPT_ID_VALID(scriptvar_sid)
Bram Moolenaarccbfd482022-03-31 16:18:23 +01001974 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
1975 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
1976 == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001977 {
1978 // "import autoload './dir/script.vim'" - load script first
1979 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
1980 return FAIL;
1981 isn_type = ISN_STOREEXPORT;
1982 }
1983
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001984 // "s:" may be included in the name.
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001985 return generate_OLDSCRIPT(cctx, isn_type, name,
1986 scriptvar_sid, type);
1987 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001988 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
1989 scriptvar_sid, scriptvar_idx, type);
1990 case dest_local:
1991 case dest_expr:
1992 // cannot happen
1993 break;
1994 }
1995 return FAIL;
1996}
1997
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001998/*
1999 * Return TRUE when inside a "for" or "while" loop.
2000 */
2001 int
2002inside_loop_scope(cctx_T *cctx)
2003{
2004 scope_T *scope = cctx->ctx_scope;
2005
2006 for (;;)
2007 {
2008 if (scope == NULL)
2009 break;
2010 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2011 return TRUE;
2012 scope = scope->se_outer;
2013 }
2014 return FALSE;
2015}
2016
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002017 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002018generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002019{
2020 if (lhs->lhs_dest != dest_local)
2021 return generate_store_var(cctx, lhs->lhs_dest,
2022 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
2023 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
2024 lhs->lhs_type, lhs->lhs_name);
2025
2026 if (lhs->lhs_lvar != NULL)
2027 {
2028 garray_T *instr = &cctx->ctx_instr;
2029 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2030
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002031 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2032 // ISN_STORENR.
2033 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002034 if (lhs->lhs_lvar->lv_from_outer == 0
2035 && instr->ga_len == instr_count + 1
2036 && isn->isn_type == ISN_PUSHNR)
2037 {
2038 varnumber_T val = isn->isn_arg.number;
2039 garray_T *stack = &cctx->ctx_type_stack;
2040
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002041 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002042 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002043 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002044 --instr->ga_len;
2045 }
2046 else
2047 {
2048 isn->isn_type = ISN_STORENR;
2049 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2050 isn->isn_arg.storenr.stnr_val = val;
2051 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002052 if (stack->ga_len > 0)
2053 --stack->ga_len;
2054 }
2055 else if (lhs->lhs_lvar->lv_from_outer > 0)
2056 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2057 lhs->lhs_lvar->lv_from_outer);
2058 else
2059 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
2060 }
2061 return OK;
2062}
2063
2064#if defined(FEAT_PROFILE) || defined(PROTO)
2065 void
2066may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2067{
2068 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2069 generate_instr(cctx, ISN_PROF_END);
2070}
2071#endif
2072
2073
2074/*
2075 * Delete an instruction, free what it contains.
2076 */
2077 void
2078delete_instr(isn_T *isn)
2079{
2080 switch (isn->isn_type)
2081 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002082 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002083 case ISN_DEF:
2084 case ISN_EXEC:
2085 case ISN_EXECRANGE:
2086 case ISN_EXEC_SPLIT:
2087 case ISN_LEGACY_EVAL:
2088 case ISN_LOADAUTO:
2089 case ISN_LOADB:
2090 case ISN_LOADENV:
2091 case ISN_LOADG:
2092 case ISN_LOADOPT:
2093 case ISN_LOADT:
2094 case ISN_LOADW:
2095 case ISN_LOCKUNLOCK:
2096 case ISN_PUSHEXC:
2097 case ISN_PUSHFUNC:
2098 case ISN_PUSHS:
2099 case ISN_RANGE:
2100 case ISN_STOREAUTO:
2101 case ISN_STOREB:
2102 case ISN_STOREENV:
2103 case ISN_STOREG:
2104 case ISN_STORET:
2105 case ISN_STOREW:
2106 case ISN_STRINGMEMBER:
2107 vim_free(isn->isn_arg.string);
2108 break;
2109
2110 case ISN_SUBSTITUTE:
2111 {
2112 int idx;
2113 isn_T *list = isn->isn_arg.subs.subs_instr;
2114
2115 vim_free(isn->isn_arg.subs.subs_cmd);
2116 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2117 delete_instr(list + idx);
2118 vim_free(list);
2119 }
2120 break;
2121
2122 case ISN_INSTR:
2123 {
2124 int idx;
2125 isn_T *list = isn->isn_arg.instr;
2126
2127 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2128 delete_instr(list + idx);
2129 vim_free(list);
2130 }
2131 break;
2132
2133 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002134 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002135 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002136 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002137 vim_free(isn->isn_arg.loadstore.ls_name);
2138 break;
2139
2140 case ISN_UNLET:
2141 case ISN_UNLETENV:
2142 vim_free(isn->isn_arg.unlet.ul_name);
2143 break;
2144
2145 case ISN_STOREOPT:
2146 case ISN_STOREFUNCOPT:
2147 vim_free(isn->isn_arg.storeopt.so_name);
2148 break;
2149
2150 case ISN_PUSHBLOB: // push blob isn_arg.blob
2151 blob_unref(isn->isn_arg.blob);
2152 break;
2153
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002154 case ISN_UCALL:
2155 vim_free(isn->isn_arg.ufunc.cuf_name);
2156 break;
2157
2158 case ISN_FUNCREF:
2159 {
2160 if (isn->isn_arg.funcref.fr_func_name == NULL)
2161 {
2162 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002163 + isn->isn_arg.funcref.fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002164 ufunc_T *ufunc = dfunc->df_ufunc;
2165
2166 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2167 func_ptr_unref(ufunc);
2168 }
2169 else
2170 {
2171 char_u *name = isn->isn_arg.funcref.fr_func_name;
2172
2173 if (name != NULL)
2174 func_unref(name);
2175 vim_free(isn->isn_arg.funcref.fr_func_name);
2176 }
2177 }
2178 break;
2179
2180 case ISN_DCALL:
2181 {
2182 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002183 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002184
2185 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002186 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002187 func_ptr_unref(dfunc->df_ufunc);
2188 }
2189 break;
2190
2191 case ISN_NEWFUNC:
2192 {
2193 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002194 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002195
2196 if (ufunc != NULL)
2197 {
2198 unlink_def_function(ufunc);
2199 func_ptr_unref(ufunc);
2200 }
2201
2202 vim_free(lambda);
2203 vim_free(isn->isn_arg.newfunc.nf_global);
2204 }
2205 break;
2206
2207 case ISN_CHECKTYPE:
2208 case ISN_SETTYPE:
2209 free_type(isn->isn_arg.type.ct_type);
2210 break;
2211
2212 case ISN_CMDMOD:
2213 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002214 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002215 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2216 break;
2217
2218 case ISN_LOADSCRIPT:
2219 case ISN_STORESCRIPT:
2220 vim_free(isn->isn_arg.script.scriptref);
2221 break;
2222
2223 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002224 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002225 break;
2226
2227 case ISN_CEXPR_CORE:
2228 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2229 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2230 break;
2231
2232 case ISN_2BOOL:
2233 case ISN_2STRING:
2234 case ISN_2STRING_ANY:
2235 case ISN_ADDBLOB:
2236 case ISN_ADDLIST:
2237 case ISN_ANYINDEX:
2238 case ISN_ANYSLICE:
2239 case ISN_BCALL:
2240 case ISN_BLOBAPPEND:
2241 case ISN_BLOBINDEX:
2242 case ISN_BLOBSLICE:
2243 case ISN_CATCH:
2244 case ISN_CEXPR_AUCMD:
2245 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002246 case ISN_CLEARDICT:
2247 case ISN_CMDMOD_REV:
2248 case ISN_COMPAREANY:
2249 case ISN_COMPAREBLOB:
2250 case ISN_COMPAREBOOL:
2251 case ISN_COMPAREDICT:
2252 case ISN_COMPAREFLOAT:
2253 case ISN_COMPAREFUNC:
2254 case ISN_COMPARELIST:
2255 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002256 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002257 case ISN_COMPARESPECIAL:
2258 case ISN_COMPARESTRING:
2259 case ISN_CONCAT:
2260 case ISN_COND2BOOL:
2261 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002262 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002263 case ISN_DROP:
2264 case ISN_ECHO:
2265 case ISN_ECHOCONSOLE:
2266 case ISN_ECHOERR:
2267 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002268 case ISN_ECHOWINDOW:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002269 case ISN_ENDTRY:
2270 case ISN_EXECCONCAT:
2271 case ISN_EXECUTE:
2272 case ISN_FINALLY:
2273 case ISN_FINISH:
2274 case ISN_FOR:
2275 case ISN_GETITEM:
2276 case ISN_JUMP:
2277 case ISN_JUMP_IF_ARG_SET:
2278 case ISN_LISTAPPEND:
2279 case ISN_LISTINDEX:
2280 case ISN_LISTSLICE:
2281 case ISN_LOAD:
2282 case ISN_LOADBDICT:
2283 case ISN_LOADGDICT:
2284 case ISN_LOADOUTER:
2285 case ISN_LOADREG:
2286 case ISN_LOADTDICT:
2287 case ISN_LOADV:
2288 case ISN_LOADWDICT:
2289 case ISN_LOCKCONST:
2290 case ISN_MEMBER:
2291 case ISN_NEGATENR:
2292 case ISN_NEWDICT:
2293 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002294 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002295 case ISN_OPANY:
2296 case ISN_OPFLOAT:
2297 case ISN_OPNR:
2298 case ISN_PCALL:
2299 case ISN_PCALL_END:
2300 case ISN_PROF_END:
2301 case ISN_PROF_START:
2302 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002303 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002304 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002305 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002306 case ISN_PUSHNR:
2307 case ISN_PUSHSPEC:
2308 case ISN_PUT:
2309 case ISN_REDIREND:
2310 case ISN_REDIRSTART:
2311 case ISN_RETURN:
2312 case ISN_RETURN_VOID:
2313 case ISN_SHUFFLE:
2314 case ISN_SLICE:
2315 case ISN_STORE:
2316 case ISN_STOREINDEX:
2317 case ISN_STORENR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002318 case ISN_SOURCE:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002319 case ISN_STOREOUTER:
2320 case ISN_STORERANGE:
2321 case ISN_STOREREG:
2322 case ISN_STOREV:
2323 case ISN_STRINDEX:
2324 case ISN_STRSLICE:
2325 case ISN_THROW:
2326 case ISN_TRYCONT:
2327 case ISN_UNLETINDEX:
2328 case ISN_UNLETRANGE:
2329 case ISN_UNPACK:
2330 case ISN_USEDICT:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002331 // nothing allocated
2332 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002333 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002334}
2335
2336 void
2337clear_instr_ga(garray_T *gap)
2338{
2339 int idx;
2340
2341 for (idx = 0; idx < gap->ga_len; ++idx)
2342 delete_instr(((isn_T *)gap->ga_data) + idx);
2343 ga_clear(gap);
2344}
2345
2346
2347#endif // defined(FEAT_EVAL)