blob: 12c83df9b83c3fa5b6c7caff8ad98d58ef4ccbc1 [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/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000117 * Generate an ISN_CONSTRUCT instruction.
118 * The object will have "size" members.
119 */
120 int
121generate_CONSTRUCT(cctx_T *cctx, class_T *cl)
122{
123 isn_T *isn;
124
125 RETURN_OK_IF_SKIP(cctx);
126 if ((isn = generate_instr(cctx, ISN_CONSTRUCT)) == NULL)
127 return FAIL;
128 isn->isn_arg.construct.construct_size = sizeof(object_T)
129 + cl->class_obj_member_count * sizeof(typval_T);
130 isn->isn_arg.construct.construct_class = cl;
131 return OK;
132}
133
134/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000135 * Generate ISN_GET_OBJ_MEMBER - access member of object at bottom of stack by
136 * index.
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000137 */
138 int
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +0200139generate_GET_OBJ_MEMBER(cctx_T *cctx, int idx, type_T *type)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000140{
141 RETURN_OK_IF_SKIP(cctx);
142
143 // drop the object type
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000144 isn_T *isn = generate_instr_drop(cctx, ISN_GET_OBJ_MEMBER, 1);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000145 if (isn == NULL)
146 return FAIL;
147
Ernie Rael18143d32023-09-04 22:30:41 +0200148 isn->isn_arg.classmember.cm_class = NULL;
149 isn->isn_arg.classmember.cm_idx = idx;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000150 return push_type_stack2(cctx, type, &t_any);
151}
152
153/*
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000154 * Generate ISN_GET_ITF_MEMBER - access member of interface at bottom of stack
155 * by index.
156 */
157 int
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +0200158generate_GET_ITF_MEMBER(cctx_T *cctx, class_T *itf, int idx, type_T *type)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000159{
160 RETURN_OK_IF_SKIP(cctx);
161
162 // drop the object type
163 isn_T *isn = generate_instr_drop(cctx, ISN_GET_ITF_MEMBER, 1);
164 if (isn == NULL)
165 return FAIL;
166
167 isn->isn_arg.classmember.cm_class = itf;
168 ++itf->class_refcount;
169 isn->isn_arg.classmember.cm_idx = idx;
170 return push_type_stack2(cctx, type, &t_any);
171}
172
173/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000174 * Generate ISN_STORE_THIS - store value in member of "this" object with member
175 * index "idx".
176 */
177 int
178generate_STORE_THIS(cctx_T *cctx, int idx)
179{
180 RETURN_OK_IF_SKIP(cctx);
181
182 // drop the value type
183 isn_T *isn = generate_instr_drop(cctx, ISN_STORE_THIS, 1);
184 if (isn == NULL)
185 return FAIL;
186
187 isn->isn_arg.number = idx;
188 return OK;
189}
190
191/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000192 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
193 * But only for simple types.
194 * When "tolerant" is TRUE convert most types to string, e.g. a List.
195 */
196 int
197may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
198{
199 isn_T *isn;
200 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000201 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000202
203 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000204 type = get_type_on_stack(cctx, -1 - offset);
205 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000206 {
207 // nothing to be done
208 case VAR_STRING: return OK;
209
210 // conversion possible
211 case VAR_SPECIAL:
212 case VAR_BOOL:
213 case VAR_NUMBER:
214 case VAR_FLOAT:
215 break;
216
217 // conversion possible (with runtime check)
218 case VAR_ANY:
219 case VAR_UNKNOWN:
220 isntype = ISN_2STRING_ANY;
221 break;
222
223 // conversion possible when tolerant
224 case VAR_LIST:
225 if (tolerant)
226 {
227 isntype = ISN_2STRING_ANY;
228 break;
229 }
230 // FALLTHROUGH
231
232 // conversion not possible
233 case VAR_VOID:
234 case VAR_BLOB:
235 case VAR_FUNC:
236 case VAR_PARTIAL:
237 case VAR_DICT:
238 case VAR_JOB:
239 case VAR_CHANNEL:
240 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000241 case VAR_CLASS:
242 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200243 case VAR_TYPEALIAS:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000244 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000245 return FAIL;
246 }
247
Bram Moolenaar078a4612022-01-04 15:17:03 +0000248 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000249 if ((isn = generate_instr(cctx, isntype)) == NULL)
250 return FAIL;
251 isn->isn_arg.tostring.offset = offset;
252 isn->isn_arg.tostring.tolerant = tolerant;
253
254 return OK;
255}
256
257 static int
258check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
259{
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000260 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
261 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000262 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000263 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000264 {
265 if (*op == '+')
266 emsg(_(e_wrong_argument_type_for_plus));
267 else
268 semsg(_(e_char_requires_number_or_float_arguments), *op);
269 return FAIL;
270 }
271 return OK;
272}
273
274/*
275 * Generate instruction for "+". For a list this creates a new list.
276 */
277 int
278generate_add_instr(
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000279 cctx_T *cctx,
280 vartype_T vartype,
281 type_T *type1,
282 type_T *type2,
283 exprtype_T expr_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000284{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000285 isn_T *isn = generate_instr_drop(cctx,
286 vartype == VAR_NUMBER ? ISN_OPNR
287 : vartype == VAR_LIST ? ISN_ADDLIST
288 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000289 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000290 : ISN_OPANY, 1);
291
292 if (vartype != VAR_LIST && vartype != VAR_BLOB
293 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000294 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000295 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000296 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000297 && check_number_or_float(
298 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
299 return FAIL;
300
301 if (isn != NULL)
302 {
303 if (isn->isn_type == ISN_ADDLIST)
304 isn->isn_arg.op.op_type = expr_type;
305 else
306 isn->isn_arg.op.op_type = EXPR_ADD;
307 }
308
309 // When concatenating two lists with different member types the member type
310 // becomes "any".
311 if (vartype == VAR_LIST
312 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
313 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000314 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000315
316 return isn == NULL ? FAIL : OK;
317}
318
319/*
320 * Get the type to use for an instruction for an operation on "type1" and
321 * "type2". If they are matching use a type-specific instruction. Otherwise
322 * fall back to runtime type checking.
323 */
324 vartype_T
325operator_type(type_T *type1, type_T *type2)
326{
327 if (type1->tt_type == type2->tt_type
328 && (type1->tt_type == VAR_NUMBER
329 || type1->tt_type == VAR_LIST
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000330 || type1->tt_type == VAR_FLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000331 || type1->tt_type == VAR_BLOB))
332 return type1->tt_type;
333 return VAR_ANY;
334}
335
336/*
337 * Generate an instruction with two arguments. The instruction depends on the
338 * type of the arguments.
339 */
340 int
341generate_two_op(cctx_T *cctx, char_u *op)
342{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000343 type_T *type1;
344 type_T *type2;
345 vartype_T vartype;
346 isn_T *isn;
347
348 RETURN_OK_IF_SKIP(cctx);
349
350 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000351 type1 = get_type_on_stack(cctx, 1);
352 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000353 vartype = operator_type(type1, type2);
354
355 switch (*op)
356 {
357 case '+':
358 if (generate_add_instr(cctx, vartype, type1, type2,
359 EXPR_COPY) == FAIL)
360 return FAIL;
361 break;
362
363 case '-':
364 case '*':
365 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
366 op) == FAIL)
367 return FAIL;
368 if (vartype == VAR_NUMBER)
369 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000370 else if (vartype == VAR_FLOAT)
371 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000372 else
373 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
374 if (isn != NULL)
375 isn->isn_arg.op.op_type = *op == '*'
376 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
377 break;
378
379 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000380 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000381 && type1->tt_type != VAR_NUMBER)
382 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000383 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000384 && type2->tt_type != VAR_NUMBER))
385 {
386 emsg(_(e_percent_requires_number_arguments));
387 return FAIL;
388 }
389 isn = generate_instr_drop(cctx,
390 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
391 if (isn != NULL)
392 isn->isn_arg.op.op_type = EXPR_REM;
393 break;
394 }
395
396 // correct type of result
397 if (vartype == VAR_ANY)
398 {
399 type_T *type = &t_any;
400
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000401 // float+number and number+float results in float
402 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100403 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000404 type = &t_float;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000405 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000406 }
407
408 return OK;
409}
410
411/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000412 * Get the instruction to use for comparing two values with specified types.
413 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000414 * Return ISN_DROP when failed.
415 */
416 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000417get_compare_isn(
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100418 exprtype_T exprtype,
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000419 typval_T *tv1,
420 typval_T *tv2,
421 type_T *type1,
422 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000423{
424 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000425 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
426 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000427
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000428 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000429 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000430 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000431 {
432 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
433 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
434 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
435 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
436 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
437 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
438 case VAR_LIST: isntype = ISN_COMPARELIST; break;
439 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
440 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000441 case VAR_CLASS: isntype = ISN_COMPARECLASS; break;
442 case VAR_OBJECT: isntype = ISN_COMPAREOBJECT; break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000443 default: isntype = ISN_COMPAREANY; break;
444 }
445 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000446 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
447 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
448 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
449 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
450 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000451 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000452 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000453 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000454 if ((vartype1 == VAR_SPECIAL
455 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
456 : type1 == &t_none)
457 && vartype2 != VAR_STRING)
458 || (vartype2 == VAR_SPECIAL
459 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
460 : type2 == &t_none)
461 && vartype1 != VAR_STRING))
462 {
463 semsg(_(e_cannot_compare_str_with_str),
464 vartype_name(vartype1), vartype_name(vartype2));
465 return ISN_DROP;
466 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000467 // although comparing null with number, float or bool is not useful, we
468 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000469 isntype = ISN_COMPARENULL;
470 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000471
472 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
473 && (isntype == ISN_COMPAREBOOL
474 || isntype == ISN_COMPARESPECIAL
475 || isntype == ISN_COMPARENR
476 || isntype == ISN_COMPAREFLOAT))
477 {
478 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000479 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000480 return ISN_DROP;
481 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000482 if (!(exprtype == EXPR_IS || exprtype == EXPR_ISNOT
483 || exprtype == EXPR_EQUAL || exprtype == EXPR_NEQUAL)
484 && (isntype == ISN_COMPAREOBJECT || isntype == ISN_COMPARECLASS))
485 {
486 semsg(_(e_invalid_operation_for_str), vartype_name(vartype1));
487 return ISN_DROP;
488 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000489 if (isntype == ISN_DROP
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100490 || (isntype != ISN_COMPARENULL
491 && (((exprtype != EXPR_EQUAL
492 && exprtype != EXPR_NEQUAL
493 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
494 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
495 || ((exprtype != EXPR_EQUAL
496 && exprtype != EXPR_NEQUAL
497 && exprtype != EXPR_IS
498 && exprtype != EXPR_ISNOT
499 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
500 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000501 {
502 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000503 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000504 return ISN_DROP;
505 }
506 return isntype;
507}
508
509 int
510check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
511{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000512 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000513 return FAIL;
514 return OK;
515}
516
517/*
518 * Generate an ISN_COMPARE* instruction with a boolean result.
519 */
520 int
521generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
522{
523 isntype_T isntype;
524 isn_T *isn;
525 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000526
527 RETURN_OK_IF_SKIP(cctx);
528
529 // Get the known type of the two items on the stack. If they are matching
530 // use a type-specific instruction. Otherwise fall back to runtime type
531 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000532 isntype = get_compare_isn(exprtype, NULL, NULL,
533 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000534 if (isntype == ISN_DROP)
535 return FAIL;
536
537 if ((isn = generate_instr(cctx, isntype)) == NULL)
538 return FAIL;
539 isn->isn_arg.op.op_type = exprtype;
540 isn->isn_arg.op.op_ic = ic;
541
542 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100543 --stack->ga_len;
544 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000545
546 return OK;
547}
548
549/*
LemonBoy372bcce2022-04-25 12:43:20 +0100550 * Generate an ISN_CONCAT instruction.
551 * "count" is the number of stack elements to join together and it must be
552 * greater or equal to one.
553 * The caller ensures all the "count" elements on the stack have the right type.
554 */
555 int
556generate_CONCAT(cctx_T *cctx, int count)
557{
558 isn_T *isn;
559 garray_T *stack = &cctx->ctx_type_stack;
560
561 RETURN_OK_IF_SKIP(cctx);
562
LemonBoy372bcce2022-04-25 12:43:20 +0100563 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
564 return FAIL;
565 isn->isn_arg.number = count;
566
567 // drop the argument types
568 stack->ga_len -= count - 1;
569
570 return OK;
571}
572
573/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000574 * Generate an ISN_2BOOL instruction.
575 * "offset" is the offset in the type stack.
576 */
577 int
578generate_2BOOL(cctx_T *cctx, int invert, int offset)
579{
580 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000581
582 RETURN_OK_IF_SKIP(cctx);
583 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
584 return FAIL;
585 isn->isn_arg.tobool.invert = invert;
586 isn->isn_arg.tobool.offset = offset;
587
588 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000589 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000590
591 return OK;
592}
593
594/*
595 * Generate an ISN_COND2BOOL instruction.
596 */
597 int
598generate_COND2BOOL(cctx_T *cctx)
599{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000600 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100601 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000602 return FAIL;
603
604 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000605 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000606
607 return OK;
608}
609
610 int
611generate_TYPECHECK(
612 cctx_T *cctx,
613 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000614 int number_ok, // add TTFLAG_NUMBER_OK flag
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000615 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100616 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000617 int argidx)
618{
619 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000620
621 RETURN_OK_IF_SKIP(cctx);
622 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
623 return FAIL;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000624 type_T *tt;
625 if (expected->tt_type == VAR_FLOAT && number_ok)
626 {
627 // always allocate, also for static types
628 tt = ALLOC_ONE(type_T);
629 if (tt != NULL)
630 {
631 *tt = *expected;
Bram Moolenaarc4b3f642022-12-30 10:36:34 +0000632 tt->tt_flags &= ~TTFLAG_STATIC;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000633 tt->tt_flags |= TTFLAG_NUMBER_OK;
634 }
635 }
636 else
637 tt = alloc_type(expected);
638
639 isn->isn_arg.type.ct_type = tt;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000640 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100641 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000642 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
643
644 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000645 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000646
647 return OK;
648}
649
650 int
651generate_SETTYPE(
652 cctx_T *cctx,
653 type_T *expected)
654{
655 isn_T *isn;
656
657 RETURN_OK_IF_SKIP(cctx);
658 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
659 return FAIL;
660 isn->isn_arg.type.ct_type = alloc_type(expected);
661 return OK;
662}
663
664/*
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000665 * Generate an ISN_PUSHOBJ instruction. Object is always NULL.
666 */
Ernie Rael5c018be2023-08-27 18:40:26 +0200667 int
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000668generate_PUSHOBJ(cctx_T *cctx)
669{
670 RETURN_OK_IF_SKIP(cctx);
Ernie Rael5c018be2023-08-27 18:40:26 +0200671 if (generate_instr_type(cctx, ISN_PUSHOBJ, &t_object) == NULL)
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000672 return FAIL;
673 return OK;
674}
675
676/*
677 * Generate an ISN_PUSHCLASS instruction. "class" can be NULL.
678 */
679 static int
680generate_PUSHCLASS(cctx_T *cctx, class_T *class)
681{
682 RETURN_OK_IF_SKIP(cctx);
683 isn_T *isn = generate_instr_type(cctx, ISN_PUSHCLASS,
684 class == NULL ? &t_any : &class->class_type);
685 if (isn == NULL)
686 return FAIL;
Bram Moolenaar30a84472023-02-27 08:07:14 +0000687 isn->isn_arg.classarg = class;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000688 if (class != NULL)
689 ++class->class_refcount;
690 return OK;
691}
692
693/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000694 * Generate a PUSH instruction for "tv".
695 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000696 */
697 int
698generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
699{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100700 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000701 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100702 case VAR_BOOL:
703 generate_PUSHBOOL(cctx, tv->vval.v_number);
704 break;
705 case VAR_SPECIAL:
706 generate_PUSHSPEC(cctx, tv->vval.v_number);
707 break;
708 case VAR_NUMBER:
709 generate_PUSHNR(cctx, tv->vval.v_number);
710 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100711 case VAR_FLOAT:
712 generate_PUSHF(cctx, tv->vval.v_float);
713 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100714 case VAR_BLOB:
715 generate_PUSHBLOB(cctx, tv->vval.v_blob);
716 tv->vval.v_blob = NULL;
717 break;
718 case VAR_LIST:
719 if (tv->vval.v_list != NULL)
720 iemsg("non-empty list constant not supported");
721 generate_NEWLIST(cctx, 0, TRUE);
722 break;
723 case VAR_DICT:
724 if (tv->vval.v_dict != NULL)
725 iemsg("non-empty dict constant not supported");
726 generate_NEWDICT(cctx, 0, TRUE);
727 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000728#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100729 case VAR_JOB:
730 if (tv->vval.v_job != NULL)
731 iemsg("non-null job constant not supported");
732 generate_PUSHJOB(cctx);
733 break;
734 case VAR_CHANNEL:
735 if (tv->vval.v_channel != NULL)
736 iemsg("non-null channel constant not supported");
737 generate_PUSHCHANNEL(cctx);
738 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000739#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100740 case VAR_FUNC:
741 if (tv->vval.v_string != NULL)
742 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100743 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100744 break;
745 case VAR_PARTIAL:
746 if (tv->vval.v_partial != NULL)
747 iemsg("non-null partial constant not supported");
748 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
749 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000750 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100751 break;
752 case VAR_STRING:
753 generate_PUSHS(cctx, &tv->vval.v_string);
754 tv->vval.v_string = NULL;
755 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000756 case VAR_OBJECT:
757 if (tv->vval.v_object != NULL)
758 {
759 emsg(_(e_cannot_use_non_null_object));
760 return FAIL;
761 }
762 generate_PUSHOBJ(cctx);
763 break;
764 case VAR_CLASS:
765 generate_PUSHCLASS(cctx, tv->vval.v_class);
766 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100767 default:
768 siemsg("constant type %d not supported", tv->v_type);
769 clear_tv(tv);
770 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000771 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100772 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000773 return OK;
774}
775
776/*
777 * Generate an ISN_PUSHNR instruction.
778 */
779 int
780generate_PUSHNR(cctx_T *cctx, varnumber_T number)
781{
782 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000783
784 RETURN_OK_IF_SKIP(cctx);
785 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
786 return FAIL;
787 isn->isn_arg.number = number;
788
789 if (number == 0 || number == 1)
790 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000791 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000792 return OK;
793}
794
795/*
796 * Generate an ISN_PUSHBOOL instruction.
797 */
798 int
799generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
800{
801 isn_T *isn;
802
803 RETURN_OK_IF_SKIP(cctx);
804 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
805 return FAIL;
806 isn->isn_arg.number = number;
807
808 return OK;
809}
810
811/*
812 * Generate an ISN_PUSHSPEC instruction.
813 */
814 int
815generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
816{
817 isn_T *isn;
818
819 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000820 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
821 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000822 return FAIL;
823 isn->isn_arg.number = number;
824
825 return OK;
826}
827
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000828/*
829 * Generate an ISN_PUSHF instruction.
830 */
831 int
832generate_PUSHF(cctx_T *cctx, float_T fnumber)
833{
834 isn_T *isn;
835
836 RETURN_OK_IF_SKIP(cctx);
837 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
838 return FAIL;
839 isn->isn_arg.fnumber = fnumber;
840
841 return OK;
842}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000843
844/*
845 * Generate an ISN_PUSHS instruction.
846 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100847 * Note that if "str" is used in the instruction OK is returned and "*str" is
848 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000849 */
850 int
851generate_PUSHS(cctx_T *cctx, char_u **str)
852{
853 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100854 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000855
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100856 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000857 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100858 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
859 ret = FAIL;
860 else
861 {
862 isn->isn_arg.string = str == NULL ? NULL : *str;
863 return OK;
864 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000865 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100866 if (str != NULL)
867 VIM_CLEAR(*str);
868 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000869}
870
871/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000872 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000873 */
874 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000875generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000876{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000877 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100878#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100879 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000880 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000881 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100882#else
883 emsg(_(e_channel_job_feature_not_available));
884 return FAIL;
885#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000886}
887
888/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000889 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000890 */
891 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000892generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000893{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000894 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100895#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100896 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000897 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000898 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100899#else
900 emsg(_(e_channel_job_feature_not_available));
901 return FAIL;
902#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000903}
904
905/*
906 * Generate an ISN_PUSHBLOB instruction.
907 * Consumes "blob".
908 */
909 int
910generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
911{
912 isn_T *isn;
913
914 RETURN_OK_IF_SKIP(cctx);
915 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
916 return FAIL;
917 isn->isn_arg.blob = blob;
918
919 return OK;
920}
921
922/*
923 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100924 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
925 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000926 */
927 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100928generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000929{
930 isn_T *isn;
931 char_u *funcname;
932
933 RETURN_OK_IF_SKIP(cctx);
934 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
935 return FAIL;
936 if (name == NULL)
937 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100938 else if (!may_prefix
939 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000940 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000941 funcname = vim_strsave(name);
942 else
943 {
944 funcname = alloc(STRLEN(name) + 3);
945 if (funcname != NULL)
946 {
947 STRCPY(funcname, "g:");
948 STRCPY(funcname + 2, name);
949 }
950 }
951
952 isn->isn_arg.string = funcname;
953 return OK;
954}
955
956/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000957 * Generate an ISN_AUTOLOAD instruction.
958 */
959 int
960generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
961{
962 isn_T *isn;
963
964 RETURN_OK_IF_SKIP(cctx);
965 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
966 return FAIL;
967 isn->isn_arg.string = vim_strsave(name);
968 if (isn->isn_arg.string == NULL)
969 return FAIL;
970 return OK;
971}
972
973/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000974 * Generate an ISN_GETITEM instruction with "index".
975 * "with_op" is TRUE for "+=" and other operators, the stack has the current
976 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100977 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000978 */
979 int
980generate_GETITEM(cctx_T *cctx, int index, int with_op)
981{
982 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000983 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000984 type_T *item_type = &t_any;
985
986 RETURN_OK_IF_SKIP(cctx);
987
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000988 item_type = type->tt_member;
989 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
990 return FAIL;
991 isn->isn_arg.getitem.gi_index = index;
992 isn->isn_arg.getitem.gi_with_op = with_op;
993
994 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000995 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000996}
997
998/*
999 * Generate an ISN_SLICE instruction with "count".
1000 */
1001 int
1002generate_SLICE(cctx_T *cctx, int count)
1003{
1004 isn_T *isn;
1005
1006 RETURN_OK_IF_SKIP(cctx);
1007 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1008 return FAIL;
1009 isn->isn_arg.number = count;
1010 return OK;
1011}
1012
1013/*
1014 * Generate an ISN_CHECKLEN instruction with "min_len".
1015 */
1016 int
1017generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1018{
1019 isn_T *isn;
1020
1021 RETURN_OK_IF_SKIP(cctx);
1022
1023 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1024 return FAIL;
1025 isn->isn_arg.checklen.cl_min_len = min_len;
1026 isn->isn_arg.checklen.cl_more_OK = more_OK;
1027
1028 return OK;
1029}
1030
1031/*
1032 * Generate an ISN_STORE instruction.
1033 */
1034 int
1035generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1036{
1037 isn_T *isn;
1038
1039 RETURN_OK_IF_SKIP(cctx);
1040 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1041 return FAIL;
1042 if (name != NULL)
1043 isn->isn_arg.string = vim_strsave(name);
1044 else
1045 isn->isn_arg.number = idx;
1046
1047 return OK;
1048}
1049
1050/*
Bram Moolenaard505d172022-12-18 21:42:55 +00001051 * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER
1052 * ("load" == FALSE) instruction.
1053 */
1054 int
1055generate_CLASSMEMBER(
1056 cctx_T *cctx,
1057 int load,
1058 class_T *cl,
1059 int idx)
1060{
1061 isn_T *isn;
1062
1063 RETURN_OK_IF_SKIP(cctx);
1064 if (load)
1065 {
1066 ocmember_T *m = &cl->class_class_members[idx];
1067 isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type);
1068 }
1069 else
1070 {
1071 isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1);
1072 }
1073 if (isn == NULL)
1074 return FAIL;
1075 isn->isn_arg.classmember.cm_class = cl;
1076 ++cl->class_refcount;
1077 isn->isn_arg.classmember.cm_idx = idx;
1078
1079 return OK;
1080}
1081
1082/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001083 * Generate an ISN_STOREOUTER instruction.
1084 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001085 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001086generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001087{
1088 isn_T *isn;
1089
1090 RETURN_OK_IF_SKIP(cctx);
1091 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1092 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001093 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
1094 {
1095 // Store a variable defined in a loop. A copy will be made at the end
1096 // of the loop. TODO: how about deeper nesting?
1097 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1098 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1099 }
1100 else
1101 {
1102 isn->isn_arg.outer.outer_idx = idx;
1103 isn->isn_arg.outer.outer_depth = level;
1104 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001105
1106 return OK;
1107}
1108
1109/*
1110 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1111 */
1112 int
1113generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1114{
1115 isn_T *isn;
1116
1117 RETURN_OK_IF_SKIP(cctx);
1118 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1119 return FAIL;
1120 isn->isn_arg.storenr.stnr_idx = idx;
1121 isn->isn_arg.storenr.stnr_val = value;
1122
1123 return OK;
1124}
1125
1126/*
1127 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1128 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001129 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001130generate_STOREOPT(
1131 cctx_T *cctx,
1132 isntype_T isn_type,
1133 char_u *name,
1134 int opt_flags)
1135{
1136 isn_T *isn;
1137
1138 RETURN_OK_IF_SKIP(cctx);
1139 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1140 return FAIL;
1141 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1142 isn->isn_arg.storeopt.so_flags = opt_flags;
1143
1144 return OK;
1145}
1146
1147/*
1148 * Generate an ISN_LOAD or similar instruction.
1149 */
1150 int
1151generate_LOAD(
1152 cctx_T *cctx,
1153 isntype_T isn_type,
1154 int idx,
1155 char_u *name,
1156 type_T *type)
1157{
1158 isn_T *isn;
1159
1160 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001161 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001162 return FAIL;
1163 if (name != NULL)
1164 isn->isn_arg.string = vim_strsave(name);
1165 else
1166 isn->isn_arg.number = idx;
1167
1168 return OK;
1169}
1170
1171/*
1172 * Generate an ISN_LOADOUTER instruction
1173 */
1174 int
1175generate_LOADOUTER(
1176 cctx_T *cctx,
1177 int idx,
1178 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001179 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001180 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001181 type_T *type)
1182{
1183 isn_T *isn;
1184
1185 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001186 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001187 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001188 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1189 {
1190 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001191 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001192 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001193 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001194 }
1195 else
1196 {
1197 isn->isn_arg.outer.outer_idx = idx;
1198 isn->isn_arg.outer.outer_depth = nesting;
1199 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001200
1201 return OK;
1202}
1203
1204/*
1205 * Generate an ISN_LOADV instruction for v:var.
1206 */
1207 int
1208generate_LOADV(
1209 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001210 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001211{
1212 int di_flags;
1213 int vidx = find_vim_var(name, &di_flags);
1214 type_T *type;
1215
1216 RETURN_OK_IF_SKIP(cctx);
1217 if (vidx < 0)
1218 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001219 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001220 return FAIL;
1221 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001222 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001223 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1224}
1225
1226/*
1227 * Generate an ISN_UNLET instruction.
1228 */
1229 int
1230generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1231{
1232 isn_T *isn;
1233
1234 RETURN_OK_IF_SKIP(cctx);
1235 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1236 return FAIL;
1237 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1238 isn->isn_arg.unlet.ul_forceit = forceit;
1239
1240 return OK;
1241}
1242
1243/*
1244 * Generate an ISN_LOCKCONST instruction.
1245 */
1246 int
1247generate_LOCKCONST(cctx_T *cctx)
1248{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001249 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001250 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001251 return FAIL;
1252 return OK;
1253}
1254
1255/*
1256 * Generate an ISN_LOADS instruction.
1257 */
1258 int
1259generate_OLDSCRIPT(
1260 cctx_T *cctx,
1261 isntype_T isn_type,
1262 char_u *name,
1263 int sid,
1264 type_T *type)
1265{
1266 isn_T *isn;
1267
1268 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001269 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001270 isn = generate_instr_type(cctx, isn_type, type);
1271 else
1272 isn = generate_instr_drop(cctx, isn_type, 1);
1273 if (isn == NULL)
1274 return FAIL;
1275 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1276 isn->isn_arg.loadstore.ls_sid = sid;
1277
1278 return OK;
1279}
1280
1281/*
1282 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1283 */
1284 int
1285generate_VIM9SCRIPT(
1286 cctx_T *cctx,
1287 isntype_T isn_type,
1288 int sid,
1289 int idx,
1290 type_T *type)
1291{
1292 isn_T *isn;
1293 scriptref_T *sref;
1294 scriptitem_T *si = SCRIPT_ITEM(sid);
1295
1296 RETURN_OK_IF_SKIP(cctx);
1297 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001298 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001299 else
1300 isn = generate_instr_drop(cctx, isn_type, 1);
1301 if (isn == NULL)
1302 return FAIL;
1303
1304 // This requires three arguments, which doesn't fit in an instruction, thus
1305 // we need to allocate a struct for this.
1306 sref = ALLOC_ONE(scriptref_T);
1307 if (sref == NULL)
1308 return FAIL;
1309 isn->isn_arg.script.scriptref = sref;
1310 sref->sref_sid = sid;
1311 sref->sref_idx = idx;
1312 sref->sref_seq = si->sn_script_seq;
1313 sref->sref_type = type;
1314 return OK;
1315}
1316
1317/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001318 * Generate an ISN_NEWLIST instruction for "count" items.
1319 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001320 */
1321 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001322generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001323{
1324 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001325 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001326 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001327 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001328
1329 RETURN_OK_IF_SKIP(cctx);
1330 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1331 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001332 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001333
Bram Moolenaar078a4612022-01-04 15:17:03 +00001334 // Get the member type and the declared member type from all the items on
1335 // the stack.
Ernie Raelfa831102023-12-14 20:06:39 +01001336 if ((member_type = get_member_type_from_stack(count, 1, cctx)) == NULL)
1337 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001338 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001339 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001340
1341 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001342 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001343
1344 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001345 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001346}
1347
1348/*
1349 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001350 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001351 */
1352 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001353generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001354{
1355 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001356 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001357 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001358 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001359
1360 RETURN_OK_IF_SKIP(cctx);
1361 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1362 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001363 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001364
Ernie Raelfa831102023-12-14 20:06:39 +01001365 if ((member_type = get_member_type_from_stack(count, 2, cctx)) == NULL)
1366 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001367 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001368 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001369
1370 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001371 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001372
1373 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001374 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001375}
1376
1377/*
1378 * Generate an ISN_FUNCREF instruction.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001379 * For "obj.Method" "cl" is the class of the object (can be an interface or a
1380 * base class) and "fi" the index of the method on that class.
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001381 * "isn_idx" is set to the index of the instruction, so that fr_dfunc_idx can
1382 * be set later. The index is used instead of a pointer to the instruction
1383 * because the instruction memory can be reallocated.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001384 */
1385 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001386generate_FUNCREF(
1387 cctx_T *cctx,
1388 ufunc_T *ufunc,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001389 class_T *cl,
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001390 int object_method,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001391 int fi,
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001392 int *isn_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001393{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001394 isn_T *isn;
1395 type_T *type;
1396 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001397 loopvarinfo_T loopinfo;
1398 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001399
1400 RETURN_OK_IF_SKIP(cctx);
1401 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1402 return FAIL;
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001403 if (isn_idx != NULL)
1404 // save the index of the new instruction
1405 *isn_idx = cctx->ctx_instr.ga_len - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001406
Bram Moolenaarcc341812022-09-19 15:54:34 +01001407 has_vars = get_loop_var_info(cctx, &loopinfo);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001408 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001409 {
1410 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1411 if (extra == NULL)
1412 return FAIL;
1413 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001414 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001415 if (cl != NULL)
1416 {
1417 extra->fre_class = cl;
1418 ++cl->class_refcount;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001419 extra->fre_object_method = object_method;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001420 extra->fre_method_idx = fi;
1421 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001422 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00001423 if (ufunc->uf_def_status == UF_NOT_COMPILED || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001424 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001425 if (ufunc->uf_def_status != UF_NOT_COMPILED && cl == NULL)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001426 {
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001427 if (isn_idx == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001428 // compile the function now, we need the uf_dfunc_idx value
1429 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001430 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001431 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001432
1433 // Reserve an extra variable to keep track of the number of closures
1434 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001435 cctx->ctx_has_closure = 1;
1436
1437 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001438 // the nested context, including this one. But not a function defined at
1439 // the script level.
1440 if ((ufunc->uf_flags & FC_CLOSURE)
1441 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001442 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1443
Bram Moolenaar078a4612022-01-04 15:17:03 +00001444 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1445 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001446}
1447
1448/*
1449 * Generate an ISN_NEWFUNC instruction.
1450 * "lambda_name" and "func_name" must be in allocated memory and will be
1451 * consumed.
1452 */
1453 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001454generate_NEWFUNC(
1455 cctx_T *cctx,
1456 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001457 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001458{
1459 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001460 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001461
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001462 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001463 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001464 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1465 ret = FAIL;
1466 else
1467 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001468 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1469
1470 if (arg == NULL)
1471 ret = FAIL;
1472 else
1473 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001474 // Reserve an extra variable to keep track of the number of
1475 // closures created.
1476 cctx->ctx_has_closure = 1;
1477
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001478 isn->isn_arg.newfunc.nf_arg = arg;
1479 arg->nfa_lambda = lambda_name;
1480 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001481 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001482 return OK;
1483 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001484 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001485 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001486 vim_free(lambda_name);
1487 vim_free(func_name);
1488 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001489}
1490
1491/*
1492 * Generate an ISN_DEF instruction: list functions
1493 */
1494 int
1495generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1496{
1497 isn_T *isn;
1498
1499 RETURN_OK_IF_SKIP(cctx);
1500 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1501 return FAIL;
1502 if (len > 0)
1503 {
1504 isn->isn_arg.string = vim_strnsave(name, len);
1505 if (isn->isn_arg.string == NULL)
1506 return FAIL;
1507 }
1508 return OK;
1509}
1510
1511/*
1512 * Generate an ISN_JUMP instruction.
1513 */
1514 int
1515generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1516{
1517 isn_T *isn;
1518 garray_T *stack = &cctx->ctx_type_stack;
1519
1520 RETURN_OK_IF_SKIP(cctx);
1521 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1522 return FAIL;
1523 isn->isn_arg.jump.jump_when = when;
1524 isn->isn_arg.jump.jump_where = where;
1525
1526 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1527 --stack->ga_len;
1528
1529 return OK;
1530}
1531
1532/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001533 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1534 */
1535 int
1536generate_WHILE(cctx_T *cctx, int funcref_idx)
1537{
1538 isn_T *isn;
1539 garray_T *stack = &cctx->ctx_type_stack;
1540
1541 RETURN_OK_IF_SKIP(cctx);
1542 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1543 return FAIL;
1544 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1545 isn->isn_arg.whileloop.while_end = 0; // filled in later
1546
1547 if (stack->ga_len > 0)
1548 --stack->ga_len;
1549
1550 return OK;
1551}
1552
1553/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001554 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001555 */
1556 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001557generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001558{
1559 isn_T *isn;
1560
1561 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001562 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001563 return FAIL;
1564 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1565 // jump_where is set later
1566 return OK;
1567}
1568
1569 int
1570generate_FOR(cctx_T *cctx, int loop_idx)
1571{
1572 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001573
1574 RETURN_OK_IF_SKIP(cctx);
1575 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1576 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001577 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001578
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001579 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001580 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001581}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001582
1583 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001584generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001585{
1586 isn_T *isn;
1587
1588 RETURN_OK_IF_SKIP(cctx);
1589 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1590 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001591 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1592 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1593 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001594 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001595 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001596 return OK;
1597}
1598
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001599/*
1600 * Generate an ISN_TRYCONT instruction.
1601 */
1602 int
1603generate_TRYCONT(cctx_T *cctx, int levels, int where)
1604{
1605 isn_T *isn;
1606
1607 RETURN_OK_IF_SKIP(cctx);
1608 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1609 return FAIL;
1610 isn->isn_arg.trycont.tct_levels = levels;
1611 isn->isn_arg.trycont.tct_where = where;
1612
1613 return OK;
1614}
1615
Bram Moolenaar16900322022-09-08 19:51:45 +01001616/*
1617 * Check "argount" arguments and their types on the type stack.
1618 * Give an error and return FAIL if something is wrong.
1619 * When "method_call" is NULL no code is generated.
1620 */
1621 int
1622check_internal_func_args(
1623 cctx_T *cctx,
1624 int func_idx,
1625 int argcount,
1626 int method_call,
1627 type2_T **argtypes,
1628 type2_T *shuffled_argtypes)
1629{
1630 garray_T *stack = &cctx->ctx_type_stack;
1631 int argoff = check_internal_func(func_idx, argcount);
1632
1633 if (argoff < 0)
1634 return FAIL;
1635
1636 if (method_call && argoff > 1)
1637 {
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001638 if (argcount < argoff)
1639 {
1640 semsg(_(e_not_enough_arguments_for_function_str),
1641 internal_func_name(func_idx));
1642 return FAIL;
1643 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001644
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001645 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
Bram Moolenaar16900322022-09-08 19:51:45 +01001646 if (isn == NULL)
1647 return FAIL;
1648 isn->isn_arg.shuffle.shfl_item = argcount;
1649 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1650 }
1651
1652 if (argcount > 0)
1653 {
1654 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1655
1656 // Check the types of the arguments.
1657 if (method_call && argoff > 1)
1658 {
1659 int i;
1660
1661 for (i = 0; i < argcount; ++i)
1662 shuffled_argtypes[i] = (i < argoff - 1)
1663 ? typep[i + 1]
1664 : (i == argoff - 1) ? typep[0] : typep[i];
1665 *argtypes = shuffled_argtypes;
1666 }
1667 else
1668 {
1669 int i;
1670
1671 for (i = 0; i < argcount; ++i)
1672 shuffled_argtypes[i] = typep[i];
1673 *argtypes = shuffled_argtypes;
1674 }
1675 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1676 cctx) == FAIL)
1677 return FAIL;
1678 }
1679 return OK;
1680}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001681
1682/*
1683 * Generate an ISN_BCALL instruction.
1684 * "method_call" is TRUE for "value->method()"
1685 * Return FAIL if the number of arguments is wrong.
1686 */
1687 int
1688generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1689{
1690 isn_T *isn;
1691 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001692 type2_T *argtypes = NULL;
1693 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1694 type2_T *maptype = NULL;
1695 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001696 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001697
1698 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001699
1700 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1701 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001702 return FAIL;
1703
Bram Moolenaar16900322022-09-08 19:51:45 +01001704 if (internal_func_is_map(func_idx))
1705 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001706
1707 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1708 return FAIL;
1709 isn->isn_arg.bfunc.cbf_idx = func_idx;
1710 isn->isn_arg.bfunc.cbf_argcount = argcount;
1711
1712 // Drop the argument types and push the return type.
1713 stack->ga_len -= argcount;
Bram Moolenaar32517c42023-01-15 18:17:12 +00001714 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type,
1715 cctx->ctx_type_list);
Bram Moolenaar81330182022-02-01 12:11:58 +00001716 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001717 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001718
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001719 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1720 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001721 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001722 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001723
1724 return OK;
1725}
1726
1727/*
1728 * Generate an ISN_LISTAPPEND instruction. Works like add().
1729 * Argument count is already checked.
1730 */
1731 int
1732generate_LISTAPPEND(cctx_T *cctx)
1733{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001734 type_T *list_type;
1735 type_T *item_type;
1736 type_T *expected;
1737
1738 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001739 // For checking the item type we use the declared type of the list and the
1740 // current type of the added item, adding a string to [1, 2] is OK.
1741 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001742 if (arg_type_modifiable(list_type, 1) == FAIL)
1743 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001744 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001745 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001746 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001747 return FAIL;
1748
1749 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1750 return FAIL;
1751
Bram Moolenaar078a4612022-01-04 15:17:03 +00001752 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001753 return OK;
1754}
1755
1756/*
1757 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1758 * Argument count is already checked.
1759 */
1760 int
1761generate_BLOBAPPEND(cctx_T *cctx)
1762{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001763 type_T *item_type;
1764
Bram Moolenaarfa103972022-09-29 19:14:42 +01001765 // Caller already checked that blob_type is a blob, check it is modifiable.
1766 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1767 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001768 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001769 if (need_type(item_type, &t_number, FALSE,
1770 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001771 return FAIL;
1772
1773 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1774 return FAIL;
1775
Bram Moolenaar078a4612022-01-04 15:17:03 +00001776 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001777 return OK;
1778}
1779
1780/*
Bram Moolenaard0200c82023-01-28 15:19:40 +00001781 * Generate an ISN_DCALL, ISN_UCALL or ISN_METHODCALL instruction.
1782 * When calling a method on an object, of which we know the interface only,
1783 * then "cl" is the interface and "mi" the method index on the interface.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001784 * Return FAIL if the number of arguments is wrong.
1785 */
1786 int
Bram Moolenaard0200c82023-01-28 15:19:40 +00001787generate_CALL(
1788 cctx_T *cctx,
1789 ufunc_T *ufunc,
1790 class_T *cl,
1791 int mi,
1792 int pushed_argcount)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001793{
1794 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001795 int regular_args = ufunc->uf_args.ga_len;
1796 int argcount = pushed_argcount;
1797
1798 RETURN_OK_IF_SKIP(cctx);
1799 if (argcount > regular_args && !has_varargs(ufunc))
1800 {
1801 semsg(_(e_too_many_arguments_for_function_str),
1802 printable_func_name(ufunc));
1803 return FAIL;
1804 }
1805 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1806 {
1807 semsg(_(e_not_enough_arguments_for_function_str),
1808 printable_func_name(ufunc));
1809 return FAIL;
1810 }
1811
1812 if (ufunc->uf_def_status != UF_NOT_COMPILED
1813 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1814 {
1815 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001816 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001817
1818 for (i = 0; i < argcount; ++i)
1819 {
1820 type_T *expected;
1821 type_T *actual;
1822
Bram Moolenaar078a4612022-01-04 15:17:03 +00001823 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001824 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001825 && i >= regular_args - ufunc->uf_def_args.ga_len)
1826 {
1827 // assume v:none used for default argument value
1828 continue;
1829 }
1830 if (i < regular_args)
1831 {
1832 if (ufunc->uf_arg_types == NULL)
1833 continue;
1834 expected = ufunc->uf_arg_types[i];
1835 }
1836 else if (ufunc->uf_va_type == NULL
1837 || ufunc->uf_va_type == &t_list_any)
1838 // possibly a lambda or "...: any"
1839 expected = &t_any;
1840 else
1841 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001842 if (need_type(actual, expected, FALSE,
1843 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001844 {
1845 arg_type_mismatch(expected, actual, i + 1);
1846 return FAIL;
1847 }
1848 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001849 compile_type = get_compile_type(ufunc);
1850 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001851 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001852 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001853 return FAIL;
1854 }
1855 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1856 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001857 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001858 ufunc->uf_name);
1859 return FAIL;
1860 }
1861
Bram Moolenaard0200c82023-01-28 15:19:40 +00001862 if ((isn = generate_instr(cctx, cl != NULL ? ISN_METHODCALL
1863 : ufunc->uf_def_status != UF_NOT_COMPILED
1864 ? ISN_DCALL : ISN_UCALL)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001865 return FAIL;
Bram Moolenaar836137d2023-01-29 14:11:24 +00001866 if (cl != NULL /* isn->isn_type == ISN_METHODCALL */)
Bram Moolenaard0200c82023-01-28 15:19:40 +00001867 {
1868 isn->isn_arg.mfunc = ALLOC_ONE(cmfunc_T);
1869 if (isn->isn_arg.mfunc == NULL)
1870 return FAIL;
1871 isn->isn_arg.mfunc->cmf_itf = cl;
1872 ++cl->class_refcount;
1873 isn->isn_arg.mfunc->cmf_idx = mi;
1874 isn->isn_arg.mfunc->cmf_argcount = argcount;
1875 }
1876 else if (isn->isn_type == ISN_DCALL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001877 {
1878 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1879 isn->isn_arg.dfunc.cdf_argcount = argcount;
1880 }
1881 else
1882 {
1883 // A user function may be deleted and redefined later, can't use the
1884 // ufunc pointer, need to look it up again at runtime.
1885 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1886 isn->isn_arg.ufunc.cuf_argcount = argcount;
1887 }
1888
Bram Moolenaar078a4612022-01-04 15:17:03 +00001889 // drop the argument types
1890 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001891
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001892 // For an object or class method call, drop the object/class type.
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02001893 if (ufunc->uf_class != NULL)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001894 {
1895 // When a class method is called without the class name prefix, then
1896 // the type will not be in the stack.
1897 type_T *stype = get_type_on_stack(cctx, 0);
1898 if (stype->tt_type == VAR_CLASS || stype->tt_type == VAR_OBJECT)
1899 cctx->ctx_type_stack.ga_len--;
1900 }
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02001901
Bram Moolenaar078a4612022-01-04 15:17:03 +00001902 // add return type
1903 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001904}
1905
1906/*
1907 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1908 */
1909 int
1910generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1911{
1912 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001913
1914 RETURN_OK_IF_SKIP(cctx);
1915 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1916 return FAIL;
1917 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1918 isn->isn_arg.ufunc.cuf_argcount = argcount;
1919
Bram Moolenaar078a4612022-01-04 15:17:03 +00001920 // drop the argument types
1921 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001922
Bram Moolenaar078a4612022-01-04 15:17:03 +00001923 // add return value
1924 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001925}
1926
1927/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001928 * Check the arguments of function "type" against the types on the stack.
1929 * Returns OK or FAIL;
1930 */
1931 int
1932check_func_args_from_type(
1933 cctx_T *cctx,
1934 type_T *type,
1935 int argcount,
1936 int at_top,
1937 char_u *name)
1938{
1939 if (type->tt_argcount != -1)
1940 {
1941 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1942
1943 if (argcount < type->tt_min_argcount - varargs)
1944 {
1945 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1946 return FAIL;
1947 }
1948 if (!varargs && argcount > type->tt_argcount)
1949 {
1950 emsg_funcname(e_too_many_arguments_for_function_str, name);
1951 return FAIL;
1952 }
1953 if (type->tt_args != NULL)
1954 {
1955 int i;
1956
1957 for (i = 0; i < argcount; ++i)
1958 {
1959 int offset = -argcount + i - (at_top ? 0 : 1);
1960 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1961 type_T *expected;
1962
1963 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaar36818a92023-01-03 12:33:26 +00001964 {
1965 expected = type->tt_args[type->tt_argcount - 1];
1966 if (expected != NULL && expected->tt_type == VAR_LIST)
1967 expected = expected->tt_member;
1968 if (expected == NULL)
1969 expected = &t_any;
1970 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001971 else if (i >= type->tt_min_argcount
1972 && actual->tt_type == VAR_SPECIAL)
1973 expected = &t_any;
1974 else
1975 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001976 if (need_type(actual, expected, FALSE,
1977 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01001978 {
1979 arg_type_mismatch(expected, actual, i + 1);
1980 return FAIL;
1981 }
1982 }
1983 }
1984 }
1985
1986 return OK;
1987}
1988/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001989 * Generate an ISN_PCALL instruction.
1990 * "type" is the type of the FuncRef.
1991 */
1992 int
1993generate_PCALL(
1994 cctx_T *cctx,
1995 int argcount,
1996 char_u *name,
1997 type_T *type,
1998 int at_top)
1999{
2000 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002001 type_T *ret_type;
2002
2003 RETURN_OK_IF_SKIP(cctx);
2004
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002005 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002006 ret_type = &t_any;
2007 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
2008 {
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002009 if (check_func_args_from_type(cctx, type, argcount, at_top, name)
2010 == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01002011 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002012
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002013 ret_type = type->tt_member;
2014 if (ret_type == &t_unknown)
2015 // return type not known yet, use a runtime check
2016 ret_type = &t_any;
2017 }
2018 else
2019 {
2020 semsg(_(e_not_callable_type_str), name);
2021 return FAIL;
2022 }
2023
2024 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2025 return FAIL;
2026 isn->isn_arg.pfunc.cpf_top = at_top;
2027 isn->isn_arg.pfunc.cpf_argcount = argcount;
2028
Bram Moolenaar078a4612022-01-04 15:17:03 +00002029 // drop the arguments and the funcref/partial
2030 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002031
Bram Moolenaar078a4612022-01-04 15:17:03 +00002032 // push the return value
2033 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002034
2035 // If partial is above the arguments it must be cleared and replaced with
2036 // the return value.
2037 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2038 return FAIL;
2039
2040 return OK;
2041}
2042
2043/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002044 * Generate an ISN_DEFER instruction.
2045 */
2046 int
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02002047generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002048{
2049 isn_T *isn;
2050
2051 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02002052 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002053 return FAIL;
2054 isn->isn_arg.defer.defer_var_idx = var_idx;
2055 isn->isn_arg.defer.defer_argcount = argcount;
2056 return OK;
2057}
2058
2059/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002060 * Generate an ISN_STRINGMEMBER instruction.
2061 */
2062 int
2063generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
2064{
2065 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002066 type_T *type;
2067
2068 RETURN_OK_IF_SKIP(cctx);
2069 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
2070 return FAIL;
2071 isn->isn_arg.string = vim_strnsave(name, len);
2072
2073 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00002074 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002075 if (type->tt_type != VAR_DICT
2076 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002077 {
2078 char *tofree;
2079
2080 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2081 name, type_name(type, &tofree));
2082 vim_free(tofree);
2083 return FAIL;
2084 }
2085 // change dict type to dict member type
2086 if (type->tt_type == VAR_DICT)
2087 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002088 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00002089 ? &t_any : type->tt_member;
2090 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002091 }
2092
2093 return OK;
2094}
2095
2096/*
2097 * Generate an ISN_ECHO instruction.
2098 */
2099 int
2100generate_ECHO(cctx_T *cctx, int with_white, int count)
2101{
2102 isn_T *isn;
2103
2104 RETURN_OK_IF_SKIP(cctx);
2105 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2106 return FAIL;
2107 isn->isn_arg.echo.echo_with_white = with_white;
2108 isn->isn_arg.echo.echo_count = count;
2109
2110 return OK;
2111}
2112
2113/*
2114 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
2115 */
2116 int
2117generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
2118{
2119 isn_T *isn;
2120
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01002121 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002122 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
2123 return FAIL;
2124 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002125 return OK;
2126}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002127
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002128/*
2129 * Generate an ISN_ECHOWINDOW instruction
2130 */
2131 int
2132generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
2133{
2134 isn_T *isn;
2135
2136 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2137 return FAIL;
2138 isn->isn_arg.echowin.ewin_count = count;
2139 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002140 return OK;
2141}
2142
2143/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002144 * Generate an ISN_SOURCE instruction.
2145 */
2146 int
2147generate_SOURCE(cctx_T *cctx, int sid)
2148{
2149 isn_T *isn;
2150
2151 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2152 return FAIL;
2153 isn->isn_arg.number = sid;
2154
2155 return OK;
2156}
2157
2158/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002159 * Generate an ISN_PUT instruction.
2160 */
2161 int
2162generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2163{
2164 isn_T *isn;
2165
2166 RETURN_OK_IF_SKIP(cctx);
2167 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2168 return FAIL;
2169 isn->isn_arg.put.put_regname = regname;
2170 isn->isn_arg.put.put_lnum = lnum;
2171 return OK;
2172}
2173
2174/*
Ernie Rael64885642023-10-04 20:16:22 +02002175 * Generate a LOCKUNLOCK instruction.The root item, where the indexing starts
2176 * to find the variable, is on the stack. The instr takes
2177 * - the string to parse, "root.b[idx1][idx2].d.val", to find the variable
2178 * - the class, if any, in which the string executes.
2179 * - if the root item is a function argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002180 * A copy is made of "line".
2181 */
2182 int
Ernie Raelee865f32023-09-29 19:53:55 +02002183generate_LOCKUNLOCK(cctx_T *cctx, char_u *line, int is_arg)
2184{
2185 isn_T *isn;
2186
2187 RETURN_OK_IF_SKIP(cctx);
2188 if ((isn = generate_instr(cctx, ISN_LOCKUNLOCK)) == NULL)
2189 return FAIL;
Ernie Rael64885642023-10-04 20:16:22 +02002190 class_T *cl = cctx->ctx_ufunc != NULL ? cctx->ctx_ufunc->uf_class : NULL;
2191 isn->isn_arg.lockunlock.lu_string = vim_strsave(line);
2192 isn->isn_arg.lockunlock.lu_cl_exec = cl;
2193 if (cl != NULL)
2194 ++cl->class_refcount;
2195 isn->isn_arg.lockunlock.lu_is_arg = is_arg;
Ernie Raelee865f32023-09-29 19:53:55 +02002196 return OK;
2197}
2198
2199/*
2200 * Generate an EXEC instruction that takes a string argument.
2201 * A copy is made of "line".
2202 */
2203 int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002204generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2205{
2206 isn_T *isn;
2207
2208 RETURN_OK_IF_SKIP(cctx);
2209 if ((isn = generate_instr(cctx, isntype)) == NULL)
2210 return FAIL;
2211 isn->isn_arg.string = vim_strsave(line);
2212 return OK;
2213}
2214
2215/*
2216 * Generate an EXEC instruction that takes a string argument.
2217 * "str" must be allocated, it is consumed.
2218 */
2219 int
2220generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2221{
2222 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002223 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002224
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002225 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002226 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002227 if ((isn = generate_instr(cctx, isntype)) == NULL)
2228 ret = FAIL;
2229 else
2230 {
2231 isn->isn_arg.string = str;
2232 return OK;
2233 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002234 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002235 vim_free(str);
2236 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002237}
2238
2239 int
2240generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2241{
2242 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002243
2244 RETURN_OK_IF_SKIP(cctx);
2245 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2246 return FAIL;
2247 isn->isn_arg.string = vim_strsave(line);
2248
Bram Moolenaar078a4612022-01-04 15:17:03 +00002249 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002250}
2251
2252 int
2253generate_EXECCONCAT(cctx_T *cctx, int count)
2254{
2255 isn_T *isn;
2256
2257 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2258 return FAIL;
2259 isn->isn_arg.number = count;
2260 return OK;
2261}
2262
2263/*
2264 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2265 */
2266 int
2267generate_RANGE(cctx_T *cctx, char_u *range)
2268{
2269 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002270
2271 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2272 return FAIL;
2273 isn->isn_arg.string = range;
2274
Bram Moolenaar078a4612022-01-04 15:17:03 +00002275 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002276}
2277
2278 int
2279generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2280{
2281 isn_T *isn;
2282
2283 RETURN_OK_IF_SKIP(cctx);
2284 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2285 return FAIL;
2286 isn->isn_arg.unpack.unp_count = var_count;
2287 isn->isn_arg.unpack.unp_semicolon = semicolon;
2288 return OK;
2289}
2290
2291/*
2292 * Generate an instruction for any command modifiers.
2293 */
2294 int
2295generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2296{
2297 isn_T *isn;
2298
2299 if (has_cmdmod(cmod, FALSE))
2300 {
2301 cctx->ctx_has_cmdmod = TRUE;
2302
2303 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2304 return FAIL;
2305 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2306 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2307 return FAIL;
2308 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2309 // filter program now belongs to the instruction
2310 cmod->cmod_filter_regmatch.regprog = NULL;
2311 }
2312
2313 return OK;
2314}
2315
2316 int
2317generate_undo_cmdmods(cctx_T *cctx)
2318{
2319 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2320 return FAIL;
2321 cctx->ctx_has_cmdmod = FALSE;
2322 return OK;
2323}
2324
2325/*
2326 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002327 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002328 * Return FAIL when out of memory.
2329 */
2330 int
2331generate_store_var(
2332 cctx_T *cctx,
2333 assign_dest_T dest,
2334 int opt_flags,
2335 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002336 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002337 char_u *name,
2338 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002339{
2340 switch (dest)
2341 {
2342 case dest_option:
2343 return generate_STOREOPT(cctx, ISN_STOREOPT,
2344 skip_option_env_lead(name), opt_flags);
2345 case dest_func_option:
2346 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2347 skip_option_env_lead(name), opt_flags);
2348 case dest_global:
2349 // include g: with the name, easier to execute that way
2350 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2351 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2352 case dest_buffer:
2353 // include b: with the name, easier to execute that way
2354 return generate_STORE(cctx, ISN_STOREB, 0, name);
2355 case dest_window:
2356 // include w: with the name, easier to execute that way
2357 return generate_STORE(cctx, ISN_STOREW, 0, name);
2358 case dest_tab:
2359 // include t: with the name, easier to execute that way
2360 return generate_STORE(cctx, ISN_STORET, 0, name);
2361 case dest_env:
2362 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2363 case dest_reg:
2364 return generate_STORE(cctx, ISN_STOREREG,
2365 name[1] == '@' ? '"' : name[1], NULL);
2366 case dest_vimvar:
2367 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2368 case dest_script:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002369 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002370 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2371 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2372 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002373 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002374 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002375
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002376 if (SCRIPT_ID_VALID(scriptvar_sid)
2377 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2378 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2379 == NULL)
2380 {
2381 // "import autoload './dir/script.vim'" - load script
2382 // first
2383 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2384 return FAIL;
2385 isn_type = ISN_STOREEXPORT;
2386 }
2387
2388 // "s:" may be included in the name.
2389 return generate_OLDSCRIPT(cctx, isn_type, name,
2390 scriptvar_sid, type);
2391 }
2392 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002393 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002394 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002395 case dest_class_member:
2396 return generate_CLASSMEMBER(cctx, FALSE,
2397 lhs->lhs_class, lhs->lhs_classmember_idx);
2398
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002399 case dest_local:
2400 case dest_expr:
2401 // cannot happen
2402 break;
2403 }
2404 return FAIL;
2405}
2406
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002407/*
2408 * Return TRUE when inside a "for" or "while" loop.
2409 */
2410 int
2411inside_loop_scope(cctx_T *cctx)
2412{
2413 scope_T *scope = cctx->ctx_scope;
2414
2415 for (;;)
2416 {
2417 if (scope == NULL)
2418 break;
2419 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2420 return TRUE;
2421 scope = scope->se_outer;
2422 }
2423 return FALSE;
2424}
2425
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002426 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002427generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002428{
2429 if (lhs->lhs_dest != dest_local)
2430 return generate_store_var(cctx, lhs->lhs_dest,
2431 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002432 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002433
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002434 if (lhs->lhs_lvar == NULL)
2435 return OK;
2436
2437 garray_T *instr = &cctx->ctx_instr;
2438 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2439
2440 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2441 // ISN_STORENR.
2442 // And "var = 0" does not need any instruction.
2443 if (lhs->lhs_lvar->lv_from_outer == 0
2444 && instr->ga_len == instr_count + 1
2445 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002446 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002447 varnumber_T val = isn->isn_arg.number;
2448 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002449
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002450 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002451 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002452 // zero is the default value, no need to do anything
2453 --instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002454 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002455 else
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002456 {
2457 isn->isn_type = ISN_STORENR;
2458 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2459 isn->isn_arg.storenr.stnr_val = val;
2460 }
2461 if (stack->ga_len > 0)
2462 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002463 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002464 else if (lhs->lhs_lvar->lv_from_outer > 0)
2465 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2466 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
2467 else
2468 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002469 return OK;
2470}
2471
2472#if defined(FEAT_PROFILE) || defined(PROTO)
2473 void
2474may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2475{
2476 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2477 generate_instr(cctx, ISN_PROF_END);
2478}
2479#endif
2480
2481
2482/*
2483 * Delete an instruction, free what it contains.
2484 */
2485 void
2486delete_instr(isn_T *isn)
2487{
2488 switch (isn->isn_type)
2489 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002490 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002491 case ISN_DEF:
2492 case ISN_EXEC:
2493 case ISN_EXECRANGE:
2494 case ISN_EXEC_SPLIT:
2495 case ISN_LEGACY_EVAL:
2496 case ISN_LOADAUTO:
2497 case ISN_LOADB:
2498 case ISN_LOADENV:
2499 case ISN_LOADG:
2500 case ISN_LOADOPT:
2501 case ISN_LOADT:
2502 case ISN_LOADW:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002503 case ISN_PUSHEXC:
2504 case ISN_PUSHFUNC:
2505 case ISN_PUSHS:
2506 case ISN_RANGE:
2507 case ISN_STOREAUTO:
2508 case ISN_STOREB:
2509 case ISN_STOREENV:
2510 case ISN_STOREG:
2511 case ISN_STORET:
2512 case ISN_STOREW:
2513 case ISN_STRINGMEMBER:
2514 vim_free(isn->isn_arg.string);
2515 break;
2516
Ernie Rael64885642023-10-04 20:16:22 +02002517 case ISN_LOCKUNLOCK:
2518 class_unref(isn->isn_arg.lockunlock.lu_cl_exec);
2519 vim_free(isn->isn_arg.lockunlock.lu_string);
2520 break;
2521
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002522 case ISN_SUBSTITUTE:
2523 {
2524 int idx;
2525 isn_T *list = isn->isn_arg.subs.subs_instr;
2526
2527 vim_free(isn->isn_arg.subs.subs_cmd);
2528 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2529 delete_instr(list + idx);
2530 vim_free(list);
2531 }
2532 break;
2533
2534 case ISN_INSTR:
2535 {
2536 int idx;
2537 isn_T *list = isn->isn_arg.instr;
2538
2539 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2540 delete_instr(list + idx);
2541 vim_free(list);
2542 }
2543 break;
2544
2545 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002546 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002547 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002548 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002549 vim_free(isn->isn_arg.loadstore.ls_name);
2550 break;
2551
2552 case ISN_UNLET:
2553 case ISN_UNLETENV:
2554 vim_free(isn->isn_arg.unlet.ul_name);
2555 break;
2556
2557 case ISN_STOREOPT:
2558 case ISN_STOREFUNCOPT:
2559 vim_free(isn->isn_arg.storeopt.so_name);
2560 break;
2561
2562 case ISN_PUSHBLOB: // push blob isn_arg.blob
2563 blob_unref(isn->isn_arg.blob);
2564 break;
2565
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002566 case ISN_PUSHCLASS:
Bram Moolenaar30a84472023-02-27 08:07:14 +00002567 class_unref(isn->isn_arg.classarg);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002568 break;
2569
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002570 case ISN_UCALL:
2571 vim_free(isn->isn_arg.ufunc.cuf_name);
2572 break;
2573
2574 case ISN_FUNCREF:
2575 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002576 funcref_T *funcref = &isn->isn_arg.funcref;
2577 funcref_extra_T *extra = funcref->fr_extra;
2578
2579 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002580 {
2581 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002582 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002583 ufunc_T *ufunc = dfunc->df_ufunc;
2584
2585 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2586 func_ptr_unref(ufunc);
2587 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002588 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002589 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002590 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002591
2592 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002593 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002594 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002595 vim_free(name);
2596 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00002597 if (extra->fre_class != NULL)
2598 class_unref(extra->fre_class);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002599 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002600 }
2601 }
2602 break;
2603
2604 case ISN_DCALL:
2605 {
2606 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002607 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002608
2609 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002610 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002611 func_ptr_unref(dfunc->df_ufunc);
2612 }
2613 break;
2614
Bram Moolenaard0200c82023-01-28 15:19:40 +00002615 case ISN_METHODCALL:
2616 {
2617 cmfunc_T *mfunc = isn->isn_arg.mfunc;
2618 class_unref(mfunc->cmf_itf);
2619 vim_free(mfunc);
2620 }
2621 break;
2622
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002623 case ISN_NEWFUNC:
2624 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002625 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002626
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002627 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002628 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002629 ufunc_T *ufunc = find_func_even_dead(
2630 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002631
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002632 if (ufunc != NULL)
2633 {
2634 unlink_def_function(ufunc);
2635 func_ptr_unref(ufunc);
2636 }
2637
2638 vim_free(arg->nfa_lambda);
2639 vim_free(arg->nfa_global);
2640 vim_free(arg);
2641 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002642 }
2643 break;
2644
2645 case ISN_CHECKTYPE:
2646 case ISN_SETTYPE:
2647 free_type(isn->isn_arg.type.ct_type);
2648 break;
2649
2650 case ISN_CMDMOD:
2651 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002652 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002653 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2654 break;
2655
2656 case ISN_LOADSCRIPT:
2657 case ISN_STORESCRIPT:
2658 vim_free(isn->isn_arg.script.scriptref);
2659 break;
2660
Bram Moolenaard505d172022-12-18 21:42:55 +00002661 case ISN_LOAD_CLASSMEMBER:
2662 case ISN_STORE_CLASSMEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00002663 case ISN_GET_ITF_MEMBER:
Bram Moolenaard505d172022-12-18 21:42:55 +00002664 class_unref(isn->isn_arg.classmember.cm_class);
2665 break;
2666
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002667 case ISN_STOREINDEX:
2668 class_unref(isn->isn_arg.storeindex.si_class);
2669 break;
2670
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002671 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002672 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002673 break;
2674
2675 case ISN_CEXPR_CORE:
2676 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2677 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2678 break;
2679
2680 case ISN_2BOOL:
2681 case ISN_2STRING:
2682 case ISN_2STRING_ANY:
2683 case ISN_ADDBLOB:
2684 case ISN_ADDLIST:
2685 case ISN_ANYINDEX:
2686 case ISN_ANYSLICE:
2687 case ISN_BCALL:
2688 case ISN_BLOBAPPEND:
2689 case ISN_BLOBINDEX:
2690 case ISN_BLOBSLICE:
2691 case ISN_CATCH:
2692 case ISN_CEXPR_AUCMD:
2693 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002694 case ISN_CLEARDICT:
2695 case ISN_CMDMOD_REV:
2696 case ISN_COMPAREANY:
2697 case ISN_COMPAREBLOB:
2698 case ISN_COMPAREBOOL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002699 case ISN_COMPARECLASS:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002700 case ISN_COMPAREDICT:
2701 case ISN_COMPAREFLOAT:
2702 case ISN_COMPAREFUNC:
2703 case ISN_COMPARELIST:
2704 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002705 case ISN_COMPARENULL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002706 case ISN_COMPAREOBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002707 case ISN_COMPARESPECIAL:
2708 case ISN_COMPARESTRING:
2709 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002710 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002711 case ISN_COND2BOOL:
2712 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002713 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002714 case ISN_DROP:
2715 case ISN_ECHO:
2716 case ISN_ECHOCONSOLE:
2717 case ISN_ECHOERR:
2718 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002719 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002720 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002721 case ISN_ENDTRY:
2722 case ISN_EXECCONCAT:
2723 case ISN_EXECUTE:
2724 case ISN_FINALLY:
2725 case ISN_FINISH:
2726 case ISN_FOR:
2727 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002728 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002729 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002730 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002731 case ISN_JUMP_IF_ARG_SET:
2732 case ISN_LISTAPPEND:
2733 case ISN_LISTINDEX:
2734 case ISN_LISTSLICE:
2735 case ISN_LOAD:
2736 case ISN_LOADBDICT:
2737 case ISN_LOADGDICT:
2738 case ISN_LOADOUTER:
2739 case ISN_LOADREG:
2740 case ISN_LOADTDICT:
2741 case ISN_LOADV:
2742 case ISN_LOADWDICT:
2743 case ISN_LOCKCONST:
2744 case ISN_MEMBER:
2745 case ISN_NEGATENR:
2746 case ISN_NEWDICT:
2747 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002748 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002749 case ISN_OPANY:
2750 case ISN_OPFLOAT:
2751 case ISN_OPNR:
2752 case ISN_PCALL:
2753 case ISN_PCALL_END:
2754 case ISN_PROF_END:
2755 case ISN_PROF_START:
2756 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002757 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002758 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002759 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002760 case ISN_PUSHNR:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002761 case ISN_PUSHOBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002762 case ISN_PUSHSPEC:
2763 case ISN_PUT:
2764 case ISN_REDIREND:
2765 case ISN_REDIRSTART:
2766 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002767 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002768 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002769 case ISN_SHUFFLE:
2770 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002771 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002772 case ISN_STORE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002773 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002774 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002775 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002776 case ISN_STORERANGE:
2777 case ISN_STOREREG:
2778 case ISN_STOREV:
2779 case ISN_STRINDEX:
2780 case ISN_STRSLICE:
2781 case ISN_THROW:
2782 case ISN_TRYCONT:
2783 case ISN_UNLETINDEX:
2784 case ISN_UNLETRANGE:
2785 case ISN_UNPACK:
2786 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002787 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002788 // nothing allocated
2789 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002790 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002791}
2792
2793 void
2794clear_instr_ga(garray_T *gap)
2795{
2796 int idx;
2797
2798 for (idx = 0; idx < gap->ga_len; ++idx)
2799 delete_instr(((isn_T *)gap->ga_data) + idx);
2800 ga_clear(gap);
2801}
2802
2803
2804#endif // defined(FEAT_EVAL)