blob: 34d4ae33f39781e92d02730affae9071cb94b228 [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{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000517 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100518 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000519 return FAIL;
520
521 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000522 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000523
524 return OK;
525}
526
527 int
528generate_TYPECHECK(
529 cctx_T *cctx,
530 type_T *expected,
531 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100532 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000533 int argidx)
534{
535 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000536
537 RETURN_OK_IF_SKIP(cctx);
538 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
539 return FAIL;
540 isn->isn_arg.type.ct_type = alloc_type(expected);
541 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100542 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000543 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
544
545 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000546 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000547
548 return OK;
549}
550
551 int
552generate_SETTYPE(
553 cctx_T *cctx,
554 type_T *expected)
555{
556 isn_T *isn;
557
558 RETURN_OK_IF_SKIP(cctx);
559 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
560 return FAIL;
561 isn->isn_arg.type.ct_type = alloc_type(expected);
562 return OK;
563}
564
565/*
566 * Generate a PUSH instruction for "tv".
567 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000568 */
569 int
570generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
571{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100572 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000573 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100574 case VAR_BOOL:
575 generate_PUSHBOOL(cctx, tv->vval.v_number);
576 break;
577 case VAR_SPECIAL:
578 generate_PUSHSPEC(cctx, tv->vval.v_number);
579 break;
580 case VAR_NUMBER:
581 generate_PUSHNR(cctx, tv->vval.v_number);
582 break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000583#ifdef FEAT_FLOAT
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100584 case VAR_FLOAT:
585 generate_PUSHF(cctx, tv->vval.v_float);
586 break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000587#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100588 case VAR_BLOB:
589 generate_PUSHBLOB(cctx, tv->vval.v_blob);
590 tv->vval.v_blob = NULL;
591 break;
592 case VAR_LIST:
593 if (tv->vval.v_list != NULL)
594 iemsg("non-empty list constant not supported");
595 generate_NEWLIST(cctx, 0, TRUE);
596 break;
597 case VAR_DICT:
598 if (tv->vval.v_dict != NULL)
599 iemsg("non-empty dict constant not supported");
600 generate_NEWDICT(cctx, 0, TRUE);
601 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000602#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100603 case VAR_JOB:
604 if (tv->vval.v_job != NULL)
605 iemsg("non-null job constant not supported");
606 generate_PUSHJOB(cctx);
607 break;
608 case VAR_CHANNEL:
609 if (tv->vval.v_channel != NULL)
610 iemsg("non-null channel constant not supported");
611 generate_PUSHCHANNEL(cctx);
612 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000613#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100614 case VAR_FUNC:
615 if (tv->vval.v_string != NULL)
616 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100617 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100618 break;
619 case VAR_PARTIAL:
620 if (tv->vval.v_partial != NULL)
621 iemsg("non-null partial constant not supported");
622 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
623 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000624 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100625 break;
626 case VAR_STRING:
627 generate_PUSHS(cctx, &tv->vval.v_string);
628 tv->vval.v_string = NULL;
629 break;
630 default:
631 siemsg("constant type %d not supported", tv->v_type);
632 clear_tv(tv);
633 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000634 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100635 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000636 return OK;
637}
638
639/*
640 * Generate an ISN_PUSHNR instruction.
641 */
642 int
643generate_PUSHNR(cctx_T *cctx, varnumber_T number)
644{
645 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000646
647 RETURN_OK_IF_SKIP(cctx);
648 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
649 return FAIL;
650 isn->isn_arg.number = number;
651
652 if (number == 0 || number == 1)
653 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000654 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000655 return OK;
656}
657
658/*
659 * Generate an ISN_PUSHBOOL instruction.
660 */
661 int
662generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
663{
664 isn_T *isn;
665
666 RETURN_OK_IF_SKIP(cctx);
667 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
668 return FAIL;
669 isn->isn_arg.number = number;
670
671 return OK;
672}
673
674/*
675 * Generate an ISN_PUSHSPEC instruction.
676 */
677 int
678generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
679{
680 isn_T *isn;
681
682 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000683 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
684 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000685 return FAIL;
686 isn->isn_arg.number = number;
687
688 return OK;
689}
690
691#if defined(FEAT_FLOAT) || defined(PROTO)
692/*
693 * Generate an ISN_PUSHF instruction.
694 */
695 int
696generate_PUSHF(cctx_T *cctx, float_T fnumber)
697{
698 isn_T *isn;
699
700 RETURN_OK_IF_SKIP(cctx);
701 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
702 return FAIL;
703 isn->isn_arg.fnumber = fnumber;
704
705 return OK;
706}
707#endif
708
709/*
710 * Generate an ISN_PUSHS instruction.
711 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100712 * Note that if "str" is used in the instruction OK is returned and "*str" is
713 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000714 */
715 int
716generate_PUSHS(cctx_T *cctx, char_u **str)
717{
718 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100719 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000720
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100721 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000722 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100723 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
724 ret = FAIL;
725 else
726 {
727 isn->isn_arg.string = str == NULL ? NULL : *str;
728 return OK;
729 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000730 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100731 if (str != NULL)
732 VIM_CLEAR(*str);
733 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000734}
735
736/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000737 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000738 */
739 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000740generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000741{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000742 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100743#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100744 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000745 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000746 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100747#else
748 emsg(_(e_channel_job_feature_not_available));
749 return FAIL;
750#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000751}
752
753/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000754 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000755 */
756 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000757generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000758{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000759 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100760#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100761 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000762 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000763 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100764#else
765 emsg(_(e_channel_job_feature_not_available));
766 return FAIL;
767#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000768}
769
770/*
771 * Generate an ISN_PUSHBLOB instruction.
772 * Consumes "blob".
773 */
774 int
775generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
776{
777 isn_T *isn;
778
779 RETURN_OK_IF_SKIP(cctx);
780 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
781 return FAIL;
782 isn->isn_arg.blob = blob;
783
784 return OK;
785}
786
787/*
788 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100789 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
790 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000791 */
792 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100793generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000794{
795 isn_T *isn;
796 char_u *funcname;
797
798 RETURN_OK_IF_SKIP(cctx);
799 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
800 return FAIL;
801 if (name == NULL)
802 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100803 else if (!may_prefix
804 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000805 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000806 funcname = vim_strsave(name);
807 else
808 {
809 funcname = alloc(STRLEN(name) + 3);
810 if (funcname != NULL)
811 {
812 STRCPY(funcname, "g:");
813 STRCPY(funcname + 2, name);
814 }
815 }
816
817 isn->isn_arg.string = funcname;
818 return OK;
819}
820
821/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000822 * Generate an ISN_AUTOLOAD instruction.
823 */
824 int
825generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
826{
827 isn_T *isn;
828
829 RETURN_OK_IF_SKIP(cctx);
830 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
831 return FAIL;
832 isn->isn_arg.string = vim_strsave(name);
833 if (isn->isn_arg.string == NULL)
834 return FAIL;
835 return OK;
836}
837
838/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000839 * Generate an ISN_GETITEM instruction with "index".
840 * "with_op" is TRUE for "+=" and other operators, the stack has the current
841 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100842 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000843 */
844 int
845generate_GETITEM(cctx_T *cctx, int index, int with_op)
846{
847 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000848 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000849 type_T *item_type = &t_any;
850
851 RETURN_OK_IF_SKIP(cctx);
852
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853 item_type = type->tt_member;
854 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
855 return FAIL;
856 isn->isn_arg.getitem.gi_index = index;
857 isn->isn_arg.getitem.gi_with_op = with_op;
858
859 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000860 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000861}
862
863/*
864 * Generate an ISN_SLICE instruction with "count".
865 */
866 int
867generate_SLICE(cctx_T *cctx, int count)
868{
869 isn_T *isn;
870
871 RETURN_OK_IF_SKIP(cctx);
872 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
873 return FAIL;
874 isn->isn_arg.number = count;
875 return OK;
876}
877
878/*
879 * Generate an ISN_CHECKLEN instruction with "min_len".
880 */
881 int
882generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
883{
884 isn_T *isn;
885
886 RETURN_OK_IF_SKIP(cctx);
887
888 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
889 return FAIL;
890 isn->isn_arg.checklen.cl_min_len = min_len;
891 isn->isn_arg.checklen.cl_more_OK = more_OK;
892
893 return OK;
894}
895
896/*
897 * Generate an ISN_STORE instruction.
898 */
899 int
900generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
901{
902 isn_T *isn;
903
904 RETURN_OK_IF_SKIP(cctx);
905 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
906 return FAIL;
907 if (name != NULL)
908 isn->isn_arg.string = vim_strsave(name);
909 else
910 isn->isn_arg.number = idx;
911
912 return OK;
913}
914
915/*
916 * Generate an ISN_STOREOUTER instruction.
917 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000918 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000919generate_STOREOUTER(cctx_T *cctx, int idx, int level)
920{
921 isn_T *isn;
922
923 RETURN_OK_IF_SKIP(cctx);
924 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
925 return FAIL;
926 isn->isn_arg.outer.outer_idx = idx;
927 isn->isn_arg.outer.outer_depth = level;
928
929 return OK;
930}
931
932/*
933 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
934 */
935 int
936generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
937{
938 isn_T *isn;
939
940 RETURN_OK_IF_SKIP(cctx);
941 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
942 return FAIL;
943 isn->isn_arg.storenr.stnr_idx = idx;
944 isn->isn_arg.storenr.stnr_val = value;
945
946 return OK;
947}
948
949/*
950 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
951 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000952 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000953generate_STOREOPT(
954 cctx_T *cctx,
955 isntype_T isn_type,
956 char_u *name,
957 int opt_flags)
958{
959 isn_T *isn;
960
961 RETURN_OK_IF_SKIP(cctx);
962 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
963 return FAIL;
964 isn->isn_arg.storeopt.so_name = vim_strsave(name);
965 isn->isn_arg.storeopt.so_flags = opt_flags;
966
967 return OK;
968}
969
970/*
971 * Generate an ISN_LOAD or similar instruction.
972 */
973 int
974generate_LOAD(
975 cctx_T *cctx,
976 isntype_T isn_type,
977 int idx,
978 char_u *name,
979 type_T *type)
980{
981 isn_T *isn;
982
983 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000984 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000985 return FAIL;
986 if (name != NULL)
987 isn->isn_arg.string = vim_strsave(name);
988 else
989 isn->isn_arg.number = idx;
990
991 return OK;
992}
993
994/*
995 * Generate an ISN_LOADOUTER instruction
996 */
997 int
998generate_LOADOUTER(
999 cctx_T *cctx,
1000 int idx,
1001 int nesting,
1002 type_T *type)
1003{
1004 isn_T *isn;
1005
1006 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001007 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001008 return FAIL;
1009 isn->isn_arg.outer.outer_idx = idx;
1010 isn->isn_arg.outer.outer_depth = nesting;
1011
1012 return OK;
1013}
1014
1015/*
1016 * Generate an ISN_LOADV instruction for v:var.
1017 */
1018 int
1019generate_LOADV(
1020 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001021 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001022{
1023 int di_flags;
1024 int vidx = find_vim_var(name, &di_flags);
1025 type_T *type;
1026
1027 RETURN_OK_IF_SKIP(cctx);
1028 if (vidx < 0)
1029 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001030 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001031 return FAIL;
1032 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001033 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001034 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1035}
1036
1037/*
1038 * Generate an ISN_UNLET instruction.
1039 */
1040 int
1041generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1042{
1043 isn_T *isn;
1044
1045 RETURN_OK_IF_SKIP(cctx);
1046 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1047 return FAIL;
1048 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1049 isn->isn_arg.unlet.ul_forceit = forceit;
1050
1051 return OK;
1052}
1053
1054/*
1055 * Generate an ISN_LOCKCONST instruction.
1056 */
1057 int
1058generate_LOCKCONST(cctx_T *cctx)
1059{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001060 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001061 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001062 return FAIL;
1063 return OK;
1064}
1065
1066/*
1067 * Generate an ISN_LOADS instruction.
1068 */
1069 int
1070generate_OLDSCRIPT(
1071 cctx_T *cctx,
1072 isntype_T isn_type,
1073 char_u *name,
1074 int sid,
1075 type_T *type)
1076{
1077 isn_T *isn;
1078
1079 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001080 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001081 isn = generate_instr_type(cctx, isn_type, type);
1082 else
1083 isn = generate_instr_drop(cctx, isn_type, 1);
1084 if (isn == NULL)
1085 return FAIL;
1086 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1087 isn->isn_arg.loadstore.ls_sid = sid;
1088
1089 return OK;
1090}
1091
1092/*
1093 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1094 */
1095 int
1096generate_VIM9SCRIPT(
1097 cctx_T *cctx,
1098 isntype_T isn_type,
1099 int sid,
1100 int idx,
1101 type_T *type)
1102{
1103 isn_T *isn;
1104 scriptref_T *sref;
1105 scriptitem_T *si = SCRIPT_ITEM(sid);
1106
1107 RETURN_OK_IF_SKIP(cctx);
1108 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001109 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001110 else
1111 isn = generate_instr_drop(cctx, isn_type, 1);
1112 if (isn == NULL)
1113 return FAIL;
1114
1115 // This requires three arguments, which doesn't fit in an instruction, thus
1116 // we need to allocate a struct for this.
1117 sref = ALLOC_ONE(scriptref_T);
1118 if (sref == NULL)
1119 return FAIL;
1120 isn->isn_arg.script.scriptref = sref;
1121 sref->sref_sid = sid;
1122 sref->sref_idx = idx;
1123 sref->sref_seq = si->sn_script_seq;
1124 sref->sref_type = type;
1125 return OK;
1126}
1127
1128/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001129 * Generate an ISN_NEWLIST instruction for "count" items.
1130 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001131 */
1132 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001133generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001134{
1135 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001136 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001137 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001138 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001139
1140 RETURN_OK_IF_SKIP(cctx);
1141 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1142 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001143 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001144
Bram Moolenaar078a4612022-01-04 15:17:03 +00001145 // Get the member type and the declared member type from all the items on
1146 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001147 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001148 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001149 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001150
1151 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001152 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001153
1154 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001155 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001156}
1157
1158/*
1159 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001160 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001161 */
1162 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001163generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001164{
1165 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001166 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001167 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001168 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001169
1170 RETURN_OK_IF_SKIP(cctx);
1171 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1172 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001173 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001174
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001175 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001176 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001177 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001178
1179 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001180 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001181
1182 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001183 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001184}
1185
1186/*
1187 * Generate an ISN_FUNCREF instruction.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001188 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001189 */
1190 int
Bram Moolenaara915fa02022-03-23 11:29:15 +00001191generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc, isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001192{
1193 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001194 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001195
1196 RETURN_OK_IF_SKIP(cctx);
1197 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1198 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001199 if (isnp != NULL)
1200 *isnp = isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001201 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1202 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1203 else
1204 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
1205 cctx->ctx_has_closure = 1;
1206
1207 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001208 // the nested context, including this one. But not a function defined at
1209 // the script level.
1210 if ((ufunc->uf_flags & FC_CLOSURE)
1211 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001212 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1213
Bram Moolenaar078a4612022-01-04 15:17:03 +00001214 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1215 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001216}
1217
1218/*
1219 * Generate an ISN_NEWFUNC instruction.
1220 * "lambda_name" and "func_name" must be in allocated memory and will be
1221 * consumed.
1222 */
1223 int
1224generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1225{
1226 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001227 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001228
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001229 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001230 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001231 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1232 ret = FAIL;
1233 else
1234 {
1235 isn->isn_arg.newfunc.nf_lambda = lambda_name;
1236 isn->isn_arg.newfunc.nf_global = func_name;
1237 return OK;
1238 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001239 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001240 vim_free(lambda_name);
1241 vim_free(func_name);
1242 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001243}
1244
1245/*
1246 * Generate an ISN_DEF instruction: list functions
1247 */
1248 int
1249generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1250{
1251 isn_T *isn;
1252
1253 RETURN_OK_IF_SKIP(cctx);
1254 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1255 return FAIL;
1256 if (len > 0)
1257 {
1258 isn->isn_arg.string = vim_strnsave(name, len);
1259 if (isn->isn_arg.string == NULL)
1260 return FAIL;
1261 }
1262 return OK;
1263}
1264
1265/*
1266 * Generate an ISN_JUMP instruction.
1267 */
1268 int
1269generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1270{
1271 isn_T *isn;
1272 garray_T *stack = &cctx->ctx_type_stack;
1273
1274 RETURN_OK_IF_SKIP(cctx);
1275 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1276 return FAIL;
1277 isn->isn_arg.jump.jump_when = when;
1278 isn->isn_arg.jump.jump_where = where;
1279
1280 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1281 --stack->ga_len;
1282
1283 return OK;
1284}
1285
1286/*
1287 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1288 */
1289 int
1290generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1291{
1292 isn_T *isn;
1293
1294 RETURN_OK_IF_SKIP(cctx);
1295 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1296 return FAIL;
1297 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1298 // jump_where is set later
1299 return OK;
1300}
1301
1302 int
1303generate_FOR(cctx_T *cctx, int loop_idx)
1304{
1305 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001306
1307 RETURN_OK_IF_SKIP(cctx);
1308 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1309 return FAIL;
1310 isn->isn_arg.forloop.for_idx = loop_idx;
1311
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001312 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001313 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001314}
1315/*
1316 * Generate an ISN_TRYCONT instruction.
1317 */
1318 int
1319generate_TRYCONT(cctx_T *cctx, int levels, int where)
1320{
1321 isn_T *isn;
1322
1323 RETURN_OK_IF_SKIP(cctx);
1324 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1325 return FAIL;
1326 isn->isn_arg.trycont.tct_levels = levels;
1327 isn->isn_arg.trycont.tct_where = where;
1328
1329 return OK;
1330}
1331
1332
1333/*
1334 * Generate an ISN_BCALL instruction.
1335 * "method_call" is TRUE for "value->method()"
1336 * Return FAIL if the number of arguments is wrong.
1337 */
1338 int
1339generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1340{
1341 isn_T *isn;
1342 garray_T *stack = &cctx->ctx_type_stack;
1343 int argoff;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001344 type2_T *typep;
1345 type2_T *argtypes = NULL;
1346 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1347 type2_T *maptype = NULL;
1348 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001349 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001350
1351 RETURN_OK_IF_SKIP(cctx);
1352 argoff = check_internal_func(func_idx, argcount);
1353 if (argoff < 0)
1354 return FAIL;
1355
1356 if (method_call && argoff > 1)
1357 {
1358 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1359 return FAIL;
1360 isn->isn_arg.shuffle.shfl_item = argcount;
1361 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1362 }
1363
1364 if (argcount > 0)
1365 {
1366 // Check the types of the arguments.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001367 typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001368 if (method_call && argoff > 1)
1369 {
1370 int i;
1371
1372 for (i = 0; i < argcount; ++i)
1373 shuffled_argtypes[i] = (i < argoff - 1)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001374 ? typep[i + 1]
1375 : (i == argoff - 1) ? typep[0] : typep[i];
1376 argtypes = shuffled_argtypes;
1377 }
1378 else
1379 {
1380 int i;
1381
1382 for (i = 0; i < argcount; ++i)
1383 shuffled_argtypes[i] = typep[i];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001384 argtypes = shuffled_argtypes;
1385 }
1386 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1387 cctx) == FAIL)
1388 return FAIL;
1389 if (internal_func_is_map(func_idx))
Bram Moolenaar078a4612022-01-04 15:17:03 +00001390 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001391 }
1392
1393 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1394 return FAIL;
1395 isn->isn_arg.bfunc.cbf_idx = func_idx;
1396 isn->isn_arg.bfunc.cbf_argcount = argcount;
1397
1398 // Drop the argument types and push the return type.
1399 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001400 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1401 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001402 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001403
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001404 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1405 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001406 // Check that map() didn't change the item types.
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01001407 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001408
1409 return OK;
1410}
1411
1412/*
1413 * Generate an ISN_LISTAPPEND instruction. Works like add().
1414 * Argument count is already checked.
1415 */
1416 int
1417generate_LISTAPPEND(cctx_T *cctx)
1418{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001419 type_T *list_type;
1420 type_T *item_type;
1421 type_T *expected;
1422
1423 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001424 // For checking the item type we use the declared type of the list and the
1425 // current type of the added item, adding a string to [1, 2] is OK.
1426 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001427 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001428 expected = list_type->tt_member;
1429 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1430 return FAIL;
1431
1432 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1433 return FAIL;
1434
Bram Moolenaar078a4612022-01-04 15:17:03 +00001435 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001436 return OK;
1437}
1438
1439/*
1440 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1441 * Argument count is already checked.
1442 */
1443 int
1444generate_BLOBAPPEND(cctx_T *cctx)
1445{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001446 type_T *item_type;
1447
1448 // Caller already checked that blob_type is a blob.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001449 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001450 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1451 return FAIL;
1452
1453 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1454 return FAIL;
1455
Bram Moolenaar078a4612022-01-04 15:17:03 +00001456 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001457 return OK;
1458}
1459
1460/*
1461 * Generate an ISN_DCALL or ISN_UCALL instruction.
1462 * Return FAIL if the number of arguments is wrong.
1463 */
1464 int
1465generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1466{
1467 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001468 int regular_args = ufunc->uf_args.ga_len;
1469 int argcount = pushed_argcount;
1470
1471 RETURN_OK_IF_SKIP(cctx);
1472 if (argcount > regular_args && !has_varargs(ufunc))
1473 {
1474 semsg(_(e_too_many_arguments_for_function_str),
1475 printable_func_name(ufunc));
1476 return FAIL;
1477 }
1478 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1479 {
1480 semsg(_(e_not_enough_arguments_for_function_str),
1481 printable_func_name(ufunc));
1482 return FAIL;
1483 }
1484
1485 if (ufunc->uf_def_status != UF_NOT_COMPILED
1486 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1487 {
1488 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001489 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001490
1491 for (i = 0; i < argcount; ++i)
1492 {
1493 type_T *expected;
1494 type_T *actual;
1495
Bram Moolenaar078a4612022-01-04 15:17:03 +00001496 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001497 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001498 && i >= regular_args - ufunc->uf_def_args.ga_len)
1499 {
1500 // assume v:none used for default argument value
1501 continue;
1502 }
1503 if (i < regular_args)
1504 {
1505 if (ufunc->uf_arg_types == NULL)
1506 continue;
1507 expected = ufunc->uf_arg_types[i];
1508 }
1509 else if (ufunc->uf_va_type == NULL
1510 || ufunc->uf_va_type == &t_list_any)
1511 // possibly a lambda or "...: any"
1512 expected = &t_any;
1513 else
1514 expected = ufunc->uf_va_type->tt_member;
1515 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1516 TRUE, FALSE) == FAIL)
1517 {
1518 arg_type_mismatch(expected, actual, i + 1);
1519 return FAIL;
1520 }
1521 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001522 compile_type = get_compile_type(ufunc);
1523 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001524 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001525 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001526 return FAIL;
1527 }
1528 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1529 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001530 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001531 ufunc->uf_name);
1532 return FAIL;
1533 }
1534
1535 if ((isn = generate_instr(cctx,
1536 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1537 : ISN_UCALL)) == NULL)
1538 return FAIL;
1539 if (isn->isn_type == ISN_DCALL)
1540 {
1541 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1542 isn->isn_arg.dfunc.cdf_argcount = argcount;
1543 }
1544 else
1545 {
1546 // A user function may be deleted and redefined later, can't use the
1547 // ufunc pointer, need to look it up again at runtime.
1548 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1549 isn->isn_arg.ufunc.cuf_argcount = argcount;
1550 }
1551
Bram Moolenaar078a4612022-01-04 15:17:03 +00001552 // drop the argument types
1553 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001554
Bram Moolenaar078a4612022-01-04 15:17:03 +00001555 // add return type
1556 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001557}
1558
1559/*
1560 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1561 */
1562 int
1563generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1564{
1565 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001566
1567 RETURN_OK_IF_SKIP(cctx);
1568 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1569 return FAIL;
1570 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1571 isn->isn_arg.ufunc.cuf_argcount = argcount;
1572
Bram Moolenaar078a4612022-01-04 15:17:03 +00001573 // drop the argument types
1574 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001575
Bram Moolenaar078a4612022-01-04 15:17:03 +00001576 // add return value
1577 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001578}
1579
1580/*
1581 * Generate an ISN_PCALL instruction.
1582 * "type" is the type of the FuncRef.
1583 */
1584 int
1585generate_PCALL(
1586 cctx_T *cctx,
1587 int argcount,
1588 char_u *name,
1589 type_T *type,
1590 int at_top)
1591{
1592 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001593 type_T *ret_type;
1594
1595 RETURN_OK_IF_SKIP(cctx);
1596
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001597 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001598 ret_type = &t_any;
1599 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1600 {
1601 if (type->tt_argcount != -1)
1602 {
1603 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1604
1605 if (argcount < type->tt_min_argcount - varargs)
1606 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001607 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001608 return FAIL;
1609 }
1610 if (!varargs && argcount > type->tt_argcount)
1611 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001612 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001613 return FAIL;
1614 }
1615 if (type->tt_args != NULL)
1616 {
1617 int i;
1618
1619 for (i = 0; i < argcount; ++i)
1620 {
1621 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001622 type_T *actual = get_type_on_stack(cctx, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001623 type_T *expected;
1624
1625 if (varargs && i >= type->tt_argcount - 1)
1626 expected = type->tt_args[
1627 type->tt_argcount - 1]->tt_member;
1628 else if (i >= type->tt_min_argcount
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001629 && actual->tt_type == VAR_SPECIAL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001630 expected = &t_any;
1631 else
1632 expected = type->tt_args[i];
1633 if (need_type(actual, expected, offset, i + 1,
1634 cctx, TRUE, FALSE) == FAIL)
1635 {
1636 arg_type_mismatch(expected, actual, i + 1);
1637 return FAIL;
1638 }
1639 }
1640 }
1641 }
1642 ret_type = type->tt_member;
1643 if (ret_type == &t_unknown)
1644 // return type not known yet, use a runtime check
1645 ret_type = &t_any;
1646 }
1647 else
1648 {
1649 semsg(_(e_not_callable_type_str), name);
1650 return FAIL;
1651 }
1652
1653 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1654 return FAIL;
1655 isn->isn_arg.pfunc.cpf_top = at_top;
1656 isn->isn_arg.pfunc.cpf_argcount = argcount;
1657
Bram Moolenaar078a4612022-01-04 15:17:03 +00001658 // drop the arguments and the funcref/partial
1659 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001660
Bram Moolenaar078a4612022-01-04 15:17:03 +00001661 // push the return value
1662 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001663
1664 // If partial is above the arguments it must be cleared and replaced with
1665 // the return value.
1666 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1667 return FAIL;
1668
1669 return OK;
1670}
1671
1672/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001673 * Generate an ISN_DEFER instruction.
1674 */
1675 int
1676generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
1677{
1678 isn_T *isn;
1679
1680 RETURN_OK_IF_SKIP(cctx);
1681 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
1682 return FAIL;
1683 isn->isn_arg.defer.defer_var_idx = var_idx;
1684 isn->isn_arg.defer.defer_argcount = argcount;
1685 return OK;
1686}
1687
1688/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001689 * Generate an ISN_STRINGMEMBER instruction.
1690 */
1691 int
1692generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1693{
1694 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001695 type_T *type;
1696
1697 RETURN_OK_IF_SKIP(cctx);
1698 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1699 return FAIL;
1700 isn->isn_arg.string = vim_strnsave(name, len);
1701
1702 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001703 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001704 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001705 {
1706 char *tofree;
1707
1708 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1709 name, type_name(type, &tofree));
1710 vim_free(tofree);
1711 return FAIL;
1712 }
1713 // change dict type to dict member type
1714 if (type->tt_type == VAR_DICT)
1715 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001716 type_T *ntype = type->tt_member == &t_unknown
1717 ? &t_any : type->tt_member;
1718 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001719 }
1720
1721 return OK;
1722}
1723
1724/*
1725 * Generate an ISN_ECHO instruction.
1726 */
1727 int
1728generate_ECHO(cctx_T *cctx, int with_white, int count)
1729{
1730 isn_T *isn;
1731
1732 RETURN_OK_IF_SKIP(cctx);
1733 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1734 return FAIL;
1735 isn->isn_arg.echo.echo_with_white = with_white;
1736 isn->isn_arg.echo.echo_count = count;
1737
1738 return OK;
1739}
1740
1741/*
1742 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1743 */
1744 int
1745generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1746{
1747 isn_T *isn;
1748
1749 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1750 return FAIL;
1751 isn->isn_arg.number = count;
1752
1753 return OK;
1754}
1755
1756/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001757 * Generate an ISN_SOURCE instruction.
1758 */
1759 int
1760generate_SOURCE(cctx_T *cctx, int sid)
1761{
1762 isn_T *isn;
1763
1764 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
1765 return FAIL;
1766 isn->isn_arg.number = sid;
1767
1768 return OK;
1769}
1770
1771/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001772 * Generate an ISN_PUT instruction.
1773 */
1774 int
1775generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1776{
1777 isn_T *isn;
1778
1779 RETURN_OK_IF_SKIP(cctx);
1780 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1781 return FAIL;
1782 isn->isn_arg.put.put_regname = regname;
1783 isn->isn_arg.put.put_lnum = lnum;
1784 return OK;
1785}
1786
1787/*
1788 * Generate an EXEC instruction that takes a string argument.
1789 * A copy is made of "line".
1790 */
1791 int
1792generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1793{
1794 isn_T *isn;
1795
1796 RETURN_OK_IF_SKIP(cctx);
1797 if ((isn = generate_instr(cctx, isntype)) == NULL)
1798 return FAIL;
1799 isn->isn_arg.string = vim_strsave(line);
1800 return OK;
1801}
1802
1803/*
1804 * Generate an EXEC instruction that takes a string argument.
1805 * "str" must be allocated, it is consumed.
1806 */
1807 int
1808generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1809{
1810 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001811 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001812
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001813 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001814 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001815 if ((isn = generate_instr(cctx, isntype)) == NULL)
1816 ret = FAIL;
1817 else
1818 {
1819 isn->isn_arg.string = str;
1820 return OK;
1821 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001822 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001823 vim_free(str);
1824 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001825}
1826
1827 int
1828generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1829{
1830 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001831
1832 RETURN_OK_IF_SKIP(cctx);
1833 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1834 return FAIL;
1835 isn->isn_arg.string = vim_strsave(line);
1836
Bram Moolenaar078a4612022-01-04 15:17:03 +00001837 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001838}
1839
1840 int
1841generate_EXECCONCAT(cctx_T *cctx, int count)
1842{
1843 isn_T *isn;
1844
1845 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1846 return FAIL;
1847 isn->isn_arg.number = count;
1848 return OK;
1849}
1850
1851/*
1852 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1853 */
1854 int
1855generate_RANGE(cctx_T *cctx, char_u *range)
1856{
1857 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001858
1859 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1860 return FAIL;
1861 isn->isn_arg.string = range;
1862
Bram Moolenaar078a4612022-01-04 15:17:03 +00001863 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001864}
1865
1866 int
1867generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1868{
1869 isn_T *isn;
1870
1871 RETURN_OK_IF_SKIP(cctx);
1872 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1873 return FAIL;
1874 isn->isn_arg.unpack.unp_count = var_count;
1875 isn->isn_arg.unpack.unp_semicolon = semicolon;
1876 return OK;
1877}
1878
1879/*
1880 * Generate an instruction for any command modifiers.
1881 */
1882 int
1883generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
1884{
1885 isn_T *isn;
1886
1887 if (has_cmdmod(cmod, FALSE))
1888 {
1889 cctx->ctx_has_cmdmod = TRUE;
1890
1891 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
1892 return FAIL;
1893 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1894 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1895 return FAIL;
1896 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1897 // filter program now belongs to the instruction
1898 cmod->cmod_filter_regmatch.regprog = NULL;
1899 }
1900
1901 return OK;
1902}
1903
1904 int
1905generate_undo_cmdmods(cctx_T *cctx)
1906{
1907 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1908 return FAIL;
1909 cctx->ctx_has_cmdmod = FALSE;
1910 return OK;
1911}
1912
1913/*
1914 * Generate a STORE instruction for "dest", not being "dest_local".
1915 * Return FAIL when out of memory.
1916 */
1917 int
1918generate_store_var(
1919 cctx_T *cctx,
1920 assign_dest_T dest,
1921 int opt_flags,
1922 int vimvaridx,
1923 int scriptvar_idx,
1924 int scriptvar_sid,
1925 type_T *type,
1926 char_u *name)
1927{
1928 switch (dest)
1929 {
1930 case dest_option:
1931 return generate_STOREOPT(cctx, ISN_STOREOPT,
1932 skip_option_env_lead(name), opt_flags);
1933 case dest_func_option:
1934 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
1935 skip_option_env_lead(name), opt_flags);
1936 case dest_global:
1937 // include g: with the name, easier to execute that way
1938 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
1939 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
1940 case dest_buffer:
1941 // include b: with the name, easier to execute that way
1942 return generate_STORE(cctx, ISN_STOREB, 0, name);
1943 case dest_window:
1944 // include w: with the name, easier to execute that way
1945 return generate_STORE(cctx, ISN_STOREW, 0, name);
1946 case dest_tab:
1947 // include t: with the name, easier to execute that way
1948 return generate_STORE(cctx, ISN_STORET, 0, name);
1949 case dest_env:
1950 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
1951 case dest_reg:
1952 return generate_STORE(cctx, ISN_STOREREG,
1953 name[1] == '@' ? '"' : name[1], NULL);
1954 case dest_vimvar:
1955 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
1956 case dest_script:
1957 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001958 {
1959 isntype_T isn_type = ISN_STORES;
1960
1961 if (SCRIPT_ID_VALID(scriptvar_sid)
Bram Moolenaarccbfd482022-03-31 16:18:23 +01001962 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
1963 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
1964 == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001965 {
1966 // "import autoload './dir/script.vim'" - load script first
1967 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
1968 return FAIL;
1969 isn_type = ISN_STOREEXPORT;
1970 }
1971
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001972 // "s:" may be included in the name.
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001973 return generate_OLDSCRIPT(cctx, isn_type, name,
1974 scriptvar_sid, type);
1975 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001976 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
1977 scriptvar_sid, scriptvar_idx, type);
1978 case dest_local:
1979 case dest_expr:
1980 // cannot happen
1981 break;
1982 }
1983 return FAIL;
1984}
1985
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001986/*
1987 * Return TRUE when inside a "for" or "while" loop.
1988 */
1989 int
1990inside_loop_scope(cctx_T *cctx)
1991{
1992 scope_T *scope = cctx->ctx_scope;
1993
1994 for (;;)
1995 {
1996 if (scope == NULL)
1997 break;
1998 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
1999 return TRUE;
2000 scope = scope->se_outer;
2001 }
2002 return FALSE;
2003}
2004
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002005 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002006generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002007{
2008 if (lhs->lhs_dest != dest_local)
2009 return generate_store_var(cctx, lhs->lhs_dest,
2010 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
2011 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
2012 lhs->lhs_type, lhs->lhs_name);
2013
2014 if (lhs->lhs_lvar != NULL)
2015 {
2016 garray_T *instr = &cctx->ctx_instr;
2017 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2018
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002019 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2020 // ISN_STORENR.
2021 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002022 if (lhs->lhs_lvar->lv_from_outer == 0
2023 && instr->ga_len == instr_count + 1
2024 && isn->isn_type == ISN_PUSHNR)
2025 {
2026 varnumber_T val = isn->isn_arg.number;
2027 garray_T *stack = &cctx->ctx_type_stack;
2028
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002029 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002030 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002031 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002032 --instr->ga_len;
2033 }
2034 else
2035 {
2036 isn->isn_type = ISN_STORENR;
2037 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2038 isn->isn_arg.storenr.stnr_val = val;
2039 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002040 if (stack->ga_len > 0)
2041 --stack->ga_len;
2042 }
2043 else if (lhs->lhs_lvar->lv_from_outer > 0)
2044 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2045 lhs->lhs_lvar->lv_from_outer);
2046 else
2047 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
2048 }
2049 return OK;
2050}
2051
2052#if defined(FEAT_PROFILE) || defined(PROTO)
2053 void
2054may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2055{
2056 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2057 generate_instr(cctx, ISN_PROF_END);
2058}
2059#endif
2060
2061
2062/*
2063 * Delete an instruction, free what it contains.
2064 */
2065 void
2066delete_instr(isn_T *isn)
2067{
2068 switch (isn->isn_type)
2069 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002070 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002071 case ISN_DEF:
2072 case ISN_EXEC:
2073 case ISN_EXECRANGE:
2074 case ISN_EXEC_SPLIT:
2075 case ISN_LEGACY_EVAL:
2076 case ISN_LOADAUTO:
2077 case ISN_LOADB:
2078 case ISN_LOADENV:
2079 case ISN_LOADG:
2080 case ISN_LOADOPT:
2081 case ISN_LOADT:
2082 case ISN_LOADW:
2083 case ISN_LOCKUNLOCK:
2084 case ISN_PUSHEXC:
2085 case ISN_PUSHFUNC:
2086 case ISN_PUSHS:
2087 case ISN_RANGE:
2088 case ISN_STOREAUTO:
2089 case ISN_STOREB:
2090 case ISN_STOREENV:
2091 case ISN_STOREG:
2092 case ISN_STORET:
2093 case ISN_STOREW:
2094 case ISN_STRINGMEMBER:
2095 vim_free(isn->isn_arg.string);
2096 break;
2097
2098 case ISN_SUBSTITUTE:
2099 {
2100 int idx;
2101 isn_T *list = isn->isn_arg.subs.subs_instr;
2102
2103 vim_free(isn->isn_arg.subs.subs_cmd);
2104 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2105 delete_instr(list + idx);
2106 vim_free(list);
2107 }
2108 break;
2109
2110 case ISN_INSTR:
2111 {
2112 int idx;
2113 isn_T *list = isn->isn_arg.instr;
2114
2115 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2116 delete_instr(list + idx);
2117 vim_free(list);
2118 }
2119 break;
2120
2121 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002122 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002123 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002124 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002125 vim_free(isn->isn_arg.loadstore.ls_name);
2126 break;
2127
2128 case ISN_UNLET:
2129 case ISN_UNLETENV:
2130 vim_free(isn->isn_arg.unlet.ul_name);
2131 break;
2132
2133 case ISN_STOREOPT:
2134 case ISN_STOREFUNCOPT:
2135 vim_free(isn->isn_arg.storeopt.so_name);
2136 break;
2137
2138 case ISN_PUSHBLOB: // push blob isn_arg.blob
2139 blob_unref(isn->isn_arg.blob);
2140 break;
2141
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002142 case ISN_UCALL:
2143 vim_free(isn->isn_arg.ufunc.cuf_name);
2144 break;
2145
2146 case ISN_FUNCREF:
2147 {
2148 if (isn->isn_arg.funcref.fr_func_name == NULL)
2149 {
2150 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002151 + isn->isn_arg.funcref.fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002152 ufunc_T *ufunc = dfunc->df_ufunc;
2153
2154 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2155 func_ptr_unref(ufunc);
2156 }
2157 else
2158 {
2159 char_u *name = isn->isn_arg.funcref.fr_func_name;
2160
2161 if (name != NULL)
2162 func_unref(name);
2163 vim_free(isn->isn_arg.funcref.fr_func_name);
2164 }
2165 }
2166 break;
2167
2168 case ISN_DCALL:
2169 {
2170 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002171 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002172
2173 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002174 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002175 func_ptr_unref(dfunc->df_ufunc);
2176 }
2177 break;
2178
2179 case ISN_NEWFUNC:
2180 {
2181 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002182 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002183
2184 if (ufunc != NULL)
2185 {
2186 unlink_def_function(ufunc);
2187 func_ptr_unref(ufunc);
2188 }
2189
2190 vim_free(lambda);
2191 vim_free(isn->isn_arg.newfunc.nf_global);
2192 }
2193 break;
2194
2195 case ISN_CHECKTYPE:
2196 case ISN_SETTYPE:
2197 free_type(isn->isn_arg.type.ct_type);
2198 break;
2199
2200 case ISN_CMDMOD:
2201 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002202 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002203 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2204 break;
2205
2206 case ISN_LOADSCRIPT:
2207 case ISN_STORESCRIPT:
2208 vim_free(isn->isn_arg.script.scriptref);
2209 break;
2210
2211 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002212 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002213 break;
2214
2215 case ISN_CEXPR_CORE:
2216 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2217 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2218 break;
2219
2220 case ISN_2BOOL:
2221 case ISN_2STRING:
2222 case ISN_2STRING_ANY:
2223 case ISN_ADDBLOB:
2224 case ISN_ADDLIST:
2225 case ISN_ANYINDEX:
2226 case ISN_ANYSLICE:
2227 case ISN_BCALL:
2228 case ISN_BLOBAPPEND:
2229 case ISN_BLOBINDEX:
2230 case ISN_BLOBSLICE:
2231 case ISN_CATCH:
2232 case ISN_CEXPR_AUCMD:
2233 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002234 case ISN_CLEARDICT:
2235 case ISN_CMDMOD_REV:
2236 case ISN_COMPAREANY:
2237 case ISN_COMPAREBLOB:
2238 case ISN_COMPAREBOOL:
2239 case ISN_COMPAREDICT:
2240 case ISN_COMPAREFLOAT:
2241 case ISN_COMPAREFUNC:
2242 case ISN_COMPARELIST:
2243 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002244 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002245 case ISN_COMPARESPECIAL:
2246 case ISN_COMPARESTRING:
2247 case ISN_CONCAT:
2248 case ISN_COND2BOOL:
2249 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002250 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002251 case ISN_DROP:
2252 case ISN_ECHO:
2253 case ISN_ECHOCONSOLE:
2254 case ISN_ECHOERR:
2255 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002256 case ISN_ECHOWINDOW:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002257 case ISN_ENDTRY:
2258 case ISN_EXECCONCAT:
2259 case ISN_EXECUTE:
2260 case ISN_FINALLY:
2261 case ISN_FINISH:
2262 case ISN_FOR:
2263 case ISN_GETITEM:
2264 case ISN_JUMP:
2265 case ISN_JUMP_IF_ARG_SET:
2266 case ISN_LISTAPPEND:
2267 case ISN_LISTINDEX:
2268 case ISN_LISTSLICE:
2269 case ISN_LOAD:
2270 case ISN_LOADBDICT:
2271 case ISN_LOADGDICT:
2272 case ISN_LOADOUTER:
2273 case ISN_LOADREG:
2274 case ISN_LOADTDICT:
2275 case ISN_LOADV:
2276 case ISN_LOADWDICT:
2277 case ISN_LOCKCONST:
2278 case ISN_MEMBER:
2279 case ISN_NEGATENR:
2280 case ISN_NEWDICT:
2281 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002282 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002283 case ISN_OPANY:
2284 case ISN_OPFLOAT:
2285 case ISN_OPNR:
2286 case ISN_PCALL:
2287 case ISN_PCALL_END:
2288 case ISN_PROF_END:
2289 case ISN_PROF_START:
2290 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002291 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002292 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002293 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002294 case ISN_PUSHNR:
2295 case ISN_PUSHSPEC:
2296 case ISN_PUT:
2297 case ISN_REDIREND:
2298 case ISN_REDIRSTART:
2299 case ISN_RETURN:
2300 case ISN_RETURN_VOID:
2301 case ISN_SHUFFLE:
2302 case ISN_SLICE:
2303 case ISN_STORE:
2304 case ISN_STOREINDEX:
2305 case ISN_STORENR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002306 case ISN_SOURCE:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002307 case ISN_STOREOUTER:
2308 case ISN_STORERANGE:
2309 case ISN_STOREREG:
2310 case ISN_STOREV:
2311 case ISN_STRINDEX:
2312 case ISN_STRSLICE:
2313 case ISN_THROW:
2314 case ISN_TRYCONT:
2315 case ISN_UNLETINDEX:
2316 case ISN_UNLETRANGE:
2317 case ISN_UNPACK:
2318 case ISN_USEDICT:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002319 // nothing allocated
2320 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002321 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002322}
2323
2324 void
2325clear_instr_ga(garray_T *gap)
2326{
2327 int idx;
2328
2329 for (idx = 0; idx < gap->ga_len; ++idx)
2330 delete_instr(((isn_T *)gap->ga_data) + idx);
2331 ga_clear(gap);
2332}
2333
2334
2335#endif // defined(FEAT_EVAL)