blob: d4593ea138087f6a2a3e05148eb0d5cb7d78d643 [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.
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200194 * When tostring_flags has TOSTRING_TOLERANT, convert a List to a series of
195 * strings. When tostring_flags has TOSTRING_INTERPOLATE, convert a List or a
196 * Dict to the corresponding textual representation.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000197 */
198 int
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200199may_generate_2STRING(int offset, int tostring_flags, cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000200{
201 isn_T *isn;
202 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000203 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000204
205 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000206 type = get_type_on_stack(cctx, -1 - offset);
207 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000208 {
209 // nothing to be done
210 case VAR_STRING: return OK;
211
212 // conversion possible
213 case VAR_SPECIAL:
214 case VAR_BOOL:
215 case VAR_NUMBER:
216 case VAR_FLOAT:
217 break;
218
219 // conversion possible (with runtime check)
220 case VAR_ANY:
221 case VAR_UNKNOWN:
222 isntype = ISN_2STRING_ANY;
223 break;
224
225 // conversion possible when tolerant
226 case VAR_LIST:
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100227 case VAR_TUPLE:
Yegappan Lakshmananf01493c2024-04-14 23:21:02 +0200228 case VAR_DICT:
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200229 if (tostring_flags & TOSTRING_TOLERANT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000230 {
231 isntype = ISN_2STRING_ANY;
232 break;
233 }
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200234 if (tostring_flags & TOSTRING_INTERPOLATE)
235 break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000236 // FALLTHROUGH
237
238 // conversion not possible
239 case VAR_VOID:
240 case VAR_BLOB:
241 case VAR_FUNC:
242 case VAR_PARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000243 case VAR_JOB:
244 case VAR_CHANNEL:
245 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000246 case VAR_CLASS:
247 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200248 case VAR_TYPEALIAS:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000249 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000250 return FAIL;
251 }
252
Bram Moolenaar078a4612022-01-04 15:17:03 +0000253 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000254 if ((isn = generate_instr(cctx, isntype)) == NULL)
255 return FAIL;
256 isn->isn_arg.tostring.offset = offset;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200257 isn->isn_arg.tostring.flags = tostring_flags;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000258
259 return OK;
260}
261
262 static int
Ernie Raele75fde62023-12-21 17:18:54 +0100263check_number_or_float(type_T *typ1, type_T *typ2, char_u *op)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000264{
Ernie Raele75fde62023-12-21 17:18:54 +0100265 vartype_T type1 = typ1->tt_type;
266 vartype_T type2 = typ2->tt_type;
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000267 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
268 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000269 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000270 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000271 {
Ernie Raele75fde62023-12-21 17:18:54 +0100272 if (check_type_is_value(typ1) == FAIL
273 || check_type_is_value(typ2) == FAIL)
274 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000275 if (*op == '+')
276 emsg(_(e_wrong_argument_type_for_plus));
277 else
278 semsg(_(e_char_requires_number_or_float_arguments), *op);
279 return FAIL;
280 }
281 return OK;
282}
283
284/*
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100285 * Append the tuple item types from "tuple_type" to the grow array "gap".
286 */
287 static int
288ga_append_tuple_types(type_T *tuple_type, garray_T *gap)
289{
290 for (int i = 0; i < tuple_type->tt_argcount; i++)
291 {
292 if (ga_grow(gap, 1) == FAIL)
293 return FAIL;
294
295 ((type_T **)gap->ga_data)[gap->ga_len] = tuple_type->tt_args[i];
296 gap->ga_len++;
297 }
298
299 return OK;
300}
301
302/*
303 * When concatenating two tuples, the resulting tuple gets a union of item
304 * types from both the tuples. This function sets the union tuple type in the
305 * stack.
306 *
307 * Returns OK on success and FAIL on memory allocation failure.
308 */
309 static int
310set_tuple_union_type_on_stack(type_T *type1, type_T *type2, cctx_T *cctx)
311{
312 // The concatenated tuple has the union of types from both the tuples
313 garray_T tuple_types_ga;
314
315 ga_init2(&tuple_types_ga, sizeof(type_T *), 10);
316
317 if (type1->tt_argcount > 0)
318 ga_append_tuple_types(type1, &tuple_types_ga);
319 if (!(type1->tt_flags & TTFLAG_VARARGS) && (type2->tt_argcount > 0))
320 ga_append_tuple_types(type2, &tuple_types_ga);
321
322 type_T *new_tuple_type = get_tuple_type(&tuple_types_ga,
323 cctx->ctx_type_list);
324 // result inherits the variadic flag from the operands
325 new_tuple_type->tt_flags |= (type1->tt_flags & TTFLAG_VARARGS)
326 | (type2->tt_flags & TTFLAG_VARARGS);
327
328 // set the type on the stack for the resulting tuple
329 set_type_on_stack(cctx, new_tuple_type, 0);
330
331 ga_clear(&tuple_types_ga);
332
333 return OK;
334}
335
336/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000337 * Generate instruction for "+". For a list this creates a new list.
338 */
339 int
340generate_add_instr(
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000341 cctx_T *cctx,
342 vartype_T vartype,
343 type_T *type1,
344 type_T *type2,
345 exprtype_T expr_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000346{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000347 isn_T *isn = generate_instr_drop(cctx,
348 vartype == VAR_NUMBER ? ISN_OPNR
349 : vartype == VAR_LIST ? ISN_ADDLIST
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100350 : vartype == VAR_TUPLE ? ISN_ADDTUPLE
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000351 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000352 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000353 : ISN_OPANY, 1);
354
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100355 if (vartype != VAR_LIST && vartype != VAR_BLOB && vartype != VAR_TUPLE
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000356 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000357 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000358 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000359 && type2->tt_type != VAR_UNKNOWN
Ernie Raele75fde62023-12-21 17:18:54 +0100360 && check_number_or_float(type1, type2, (char_u *)"+") == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000361 return FAIL;
362
363 if (isn != NULL)
364 {
365 if (isn->isn_type == ISN_ADDLIST)
366 isn->isn_arg.op.op_type = expr_type;
367 else
368 isn->isn_arg.op.op_type = EXPR_ADD;
369 }
370
371 // When concatenating two lists with different member types the member type
372 // becomes "any".
373 if (vartype == VAR_LIST
374 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
375 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000376 set_type_on_stack(cctx, &t_list_any, 0);
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100377 else if (vartype == VAR_TUPLE)
378 {
379 if (!check_tuples_addable(type1, type2))
380 return FAIL;
381
382 if (set_tuple_union_type_on_stack(type1, type2, cctx) == FAIL)
383 return FAIL;
384 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000385
386 return isn == NULL ? FAIL : OK;
387}
388
389/*
390 * Get the type to use for an instruction for an operation on "type1" and
391 * "type2". If they are matching use a type-specific instruction. Otherwise
392 * fall back to runtime type checking.
393 */
394 vartype_T
395operator_type(type_T *type1, type_T *type2)
396{
397 if (type1->tt_type == type2->tt_type
398 && (type1->tt_type == VAR_NUMBER
399 || type1->tt_type == VAR_LIST
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100400 || type1->tt_type == VAR_TUPLE
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000401 || type1->tt_type == VAR_FLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000402 || type1->tt_type == VAR_BLOB))
403 return type1->tt_type;
404 return VAR_ANY;
405}
406
407/*
408 * Generate an instruction with two arguments. The instruction depends on the
409 * type of the arguments.
410 */
411 int
412generate_two_op(cctx_T *cctx, char_u *op)
413{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000414 type_T *type1;
415 type_T *type2;
416 vartype_T vartype;
417 isn_T *isn;
418
419 RETURN_OK_IF_SKIP(cctx);
420
421 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000422 type1 = get_type_on_stack(cctx, 1);
423 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000424 vartype = operator_type(type1, type2);
425
426 switch (*op)
427 {
428 case '+':
429 if (generate_add_instr(cctx, vartype, type1, type2,
430 EXPR_COPY) == FAIL)
431 return FAIL;
432 break;
433
434 case '-':
435 case '*':
Ernie Raele75fde62023-12-21 17:18:54 +0100436 case '/': if (check_number_or_float(type1, type2, op) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000437 return FAIL;
438 if (vartype == VAR_NUMBER)
439 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000440 else if (vartype == VAR_FLOAT)
441 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000442 else
443 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
444 if (isn != NULL)
445 isn->isn_arg.op.op_type = *op == '*'
446 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
447 break;
448
449 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000450 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000451 && type1->tt_type != VAR_NUMBER)
452 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000453 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000454 && type2->tt_type != VAR_NUMBER))
455 {
456 emsg(_(e_percent_requires_number_arguments));
457 return FAIL;
458 }
459 isn = generate_instr_drop(cctx,
460 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
461 if (isn != NULL)
462 isn->isn_arg.op.op_type = EXPR_REM;
463 break;
464 }
465
466 // correct type of result
467 if (vartype == VAR_ANY)
468 {
469 type_T *type = &t_any;
470
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000471 // float+number and number+float results in float
472 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100473 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000474 type = &t_float;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000475 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000476 }
477
478 return OK;
479}
480
481/*
Ernie Raele75fde62023-12-21 17:18:54 +0100482 * Choose correct error message for the specified type information.
483 */
484 static isntype_T
485compare_isn_not_values(typval_T *tv, type_T *type)
486{
487 if (tv != NULL)
Christian Brabandt6b9492e2023-12-24 11:14:37 +0100488 (void)check_typval_is_value(tv);
Ernie Raele75fde62023-12-21 17:18:54 +0100489 else
Christian Brabandt6b9492e2023-12-24 11:14:37 +0100490 (void)check_type_is_value(type);
Ernie Raele75fde62023-12-21 17:18:54 +0100491 return ISN_DROP;
492}
493
494/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000495 * Get the instruction to use for comparing two values with specified types.
496 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000497 * Return ISN_DROP when failed.
498 */
499 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000500get_compare_isn(
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100501 exprtype_T exprtype,
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000502 typval_T *tv1,
503 typval_T *tv2,
504 type_T *type1,
505 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000506{
507 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000508 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
509 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000510
Ernie Raele75fde62023-12-21 17:18:54 +0100511 if (vartype1 == VAR_CLASS || vartype1 == VAR_TYPEALIAS)
512 return compare_isn_not_values(tv1, type1);
513 if (vartype2 == VAR_CLASS || vartype2 == VAR_TYPEALIAS)
514 return compare_isn_not_values(tv2, type2);
515
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000516 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000517 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000518 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000519 {
520 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
521 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
522 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
523 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
524 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
525 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
526 case VAR_LIST: isntype = ISN_COMPARELIST; break;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100527 case VAR_TUPLE: isntype = ISN_COMPARETUPLE; break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000528 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
529 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000530 case VAR_OBJECT: isntype = ISN_COMPAREOBJECT; break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000531 default: isntype = ISN_COMPAREANY; break;
532 }
533 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000534 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
535 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
536 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
537 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
538 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000539 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000540 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000541 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000542 if ((vartype1 == VAR_SPECIAL
543 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
544 : type1 == &t_none)
545 && vartype2 != VAR_STRING)
546 || (vartype2 == VAR_SPECIAL
547 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
548 : type2 == &t_none)
549 && vartype1 != VAR_STRING))
550 {
551 semsg(_(e_cannot_compare_str_with_str),
552 vartype_name(vartype1), vartype_name(vartype2));
553 return ISN_DROP;
554 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000555 // although comparing null with number, float or bool is not useful, we
556 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000557 isntype = ISN_COMPARENULL;
558 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000559
560 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
561 && (isntype == ISN_COMPAREBOOL
562 || isntype == ISN_COMPARESPECIAL
563 || isntype == ISN_COMPARENR
564 || isntype == ISN_COMPAREFLOAT))
565 {
566 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000567 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000568 return ISN_DROP;
569 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000570 if (!(exprtype == EXPR_IS || exprtype == EXPR_ISNOT
571 || exprtype == EXPR_EQUAL || exprtype == EXPR_NEQUAL)
Ernie Raele75fde62023-12-21 17:18:54 +0100572 && (isntype == ISN_COMPAREOBJECT))
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000573 {
574 semsg(_(e_invalid_operation_for_str), vartype_name(vartype1));
575 return ISN_DROP;
576 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000577 if (isntype == ISN_DROP
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100578 || (isntype != ISN_COMPARENULL
579 && (((exprtype != EXPR_EQUAL
580 && exprtype != EXPR_NEQUAL
581 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
582 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
583 || ((exprtype != EXPR_EQUAL
584 && exprtype != EXPR_NEQUAL
585 && exprtype != EXPR_IS
586 && exprtype != EXPR_ISNOT
587 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
588 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000589 {
590 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000591 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000592 return ISN_DROP;
593 }
594 return isntype;
595}
596
597 int
598check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
599{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000600 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000601 return FAIL;
602 return OK;
603}
604
605/*
606 * Generate an ISN_COMPARE* instruction with a boolean result.
607 */
608 int
609generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
610{
611 isntype_T isntype;
612 isn_T *isn;
613 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000614
615 RETURN_OK_IF_SKIP(cctx);
616
617 // Get the known type of the two items on the stack. If they are matching
618 // use a type-specific instruction. Otherwise fall back to runtime type
619 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000620 isntype = get_compare_isn(exprtype, NULL, NULL,
621 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000622 if (isntype == ISN_DROP)
623 return FAIL;
624
625 if ((isn = generate_instr(cctx, isntype)) == NULL)
626 return FAIL;
627 isn->isn_arg.op.op_type = exprtype;
628 isn->isn_arg.op.op_ic = ic;
629
630 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100631 --stack->ga_len;
632 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000633
634 return OK;
635}
636
637/*
LemonBoy372bcce2022-04-25 12:43:20 +0100638 * Generate an ISN_CONCAT instruction.
639 * "count" is the number of stack elements to join together and it must be
640 * greater or equal to one.
641 * The caller ensures all the "count" elements on the stack have the right type.
642 */
643 int
644generate_CONCAT(cctx_T *cctx, int count)
645{
646 isn_T *isn;
647 garray_T *stack = &cctx->ctx_type_stack;
648
649 RETURN_OK_IF_SKIP(cctx);
650
LemonBoy372bcce2022-04-25 12:43:20 +0100651 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
652 return FAIL;
653 isn->isn_arg.number = count;
654
655 // drop the argument types
656 stack->ga_len -= count - 1;
657
658 return OK;
659}
660
661/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000662 * Generate an ISN_2BOOL instruction.
663 * "offset" is the offset in the type stack.
664 */
665 int
666generate_2BOOL(cctx_T *cctx, int invert, int offset)
667{
668 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000669
670 RETURN_OK_IF_SKIP(cctx);
671 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
672 return FAIL;
673 isn->isn_arg.tobool.invert = invert;
674 isn->isn_arg.tobool.offset = offset;
675
676 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000677 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000678
679 return OK;
680}
681
682/*
683 * Generate an ISN_COND2BOOL instruction.
684 */
685 int
686generate_COND2BOOL(cctx_T *cctx)
687{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000688 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100689 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000690 return FAIL;
691
692 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000693 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000694
695 return OK;
696}
697
698 int
699generate_TYPECHECK(
700 cctx_T *cctx,
701 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000702 int number_ok, // add TTFLAG_NUMBER_OK flag
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000703 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100704 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000705 int argidx)
706{
707 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000708
709 RETURN_OK_IF_SKIP(cctx);
710 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
711 return FAIL;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000712 type_T *tt;
713 if (expected->tt_type == VAR_FLOAT && number_ok)
714 {
715 // always allocate, also for static types
716 tt = ALLOC_ONE(type_T);
717 if (tt != NULL)
718 {
719 *tt = *expected;
Bram Moolenaarc4b3f642022-12-30 10:36:34 +0000720 tt->tt_flags &= ~TTFLAG_STATIC;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000721 tt->tt_flags |= TTFLAG_NUMBER_OK;
722 }
723 }
724 else
725 tt = alloc_type(expected);
726
727 isn->isn_arg.type.ct_type = tt;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000728 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100729 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000730 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
731
732 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000733 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000734
735 return OK;
736}
737
738 int
739generate_SETTYPE(
740 cctx_T *cctx,
741 type_T *expected)
742{
743 isn_T *isn;
744
745 RETURN_OK_IF_SKIP(cctx);
746 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
747 return FAIL;
748 isn->isn_arg.type.ct_type = alloc_type(expected);
749 return OK;
750}
751
752/*
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000753 * Generate an ISN_PUSHOBJ instruction. Object is always NULL.
754 */
Ernie Rael5c018be2023-08-27 18:40:26 +0200755 int
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000756generate_PUSHOBJ(cctx_T *cctx)
757{
758 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanande8f8f72025-04-01 20:43:36 +0200759 if (generate_instr_type(cctx, ISN_PUSHOBJ, &t_object_any) == NULL)
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000760 return FAIL;
761 return OK;
762}
763
764/*
765 * Generate an ISN_PUSHCLASS instruction. "class" can be NULL.
766 */
767 static int
768generate_PUSHCLASS(cctx_T *cctx, class_T *class)
769{
770 RETURN_OK_IF_SKIP(cctx);
771 isn_T *isn = generate_instr_type(cctx, ISN_PUSHCLASS,
772 class == NULL ? &t_any : &class->class_type);
773 if (isn == NULL)
774 return FAIL;
Bram Moolenaar30a84472023-02-27 08:07:14 +0000775 isn->isn_arg.classarg = class;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000776 if (class != NULL)
777 ++class->class_refcount;
778 return OK;
779}
780
781/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000782 * Generate a PUSH instruction for "tv".
783 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000784 */
785 int
786generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
787{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100788 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000789 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100790 case VAR_BOOL:
791 generate_PUSHBOOL(cctx, tv->vval.v_number);
792 break;
793 case VAR_SPECIAL:
794 generate_PUSHSPEC(cctx, tv->vval.v_number);
795 break;
796 case VAR_NUMBER:
797 generate_PUSHNR(cctx, tv->vval.v_number);
798 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100799 case VAR_FLOAT:
800 generate_PUSHF(cctx, tv->vval.v_float);
801 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100802 case VAR_BLOB:
803 generate_PUSHBLOB(cctx, tv->vval.v_blob);
804 tv->vval.v_blob = NULL;
805 break;
806 case VAR_LIST:
807 if (tv->vval.v_list != NULL)
808 iemsg("non-empty list constant not supported");
809 generate_NEWLIST(cctx, 0, TRUE);
810 break;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100811 case VAR_TUPLE:
812 if (tv->vval.v_tuple != NULL)
813 iemsg("non-empty tuple constant not supported");
814 generate_NEWTUPLE(cctx, 0, TRUE);
815 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100816 case VAR_DICT:
817 if (tv->vval.v_dict != NULL)
818 iemsg("non-empty dict constant not supported");
819 generate_NEWDICT(cctx, 0, TRUE);
820 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000821#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100822 case VAR_JOB:
823 if (tv->vval.v_job != NULL)
824 iemsg("non-null job constant not supported");
825 generate_PUSHJOB(cctx);
826 break;
827 case VAR_CHANNEL:
828 if (tv->vval.v_channel != NULL)
829 iemsg("non-null channel constant not supported");
830 generate_PUSHCHANNEL(cctx);
831 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000832#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100833 case VAR_FUNC:
834 if (tv->vval.v_string != NULL)
835 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100836 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100837 break;
838 case VAR_PARTIAL:
839 if (tv->vval.v_partial != NULL)
840 iemsg("non-null partial constant not supported");
841 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
842 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000843 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100844 break;
845 case VAR_STRING:
846 generate_PUSHS(cctx, &tv->vval.v_string);
847 tv->vval.v_string = NULL;
848 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000849 case VAR_OBJECT:
850 if (tv->vval.v_object != NULL)
851 {
852 emsg(_(e_cannot_use_non_null_object));
853 return FAIL;
854 }
855 generate_PUSHOBJ(cctx);
856 break;
857 case VAR_CLASS:
858 generate_PUSHCLASS(cctx, tv->vval.v_class);
859 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100860 default:
861 siemsg("constant type %d not supported", tv->v_type);
862 clear_tv(tv);
863 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000864 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100865 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000866 return OK;
867}
868
869/*
870 * Generate an ISN_PUSHNR instruction.
871 */
872 int
873generate_PUSHNR(cctx_T *cctx, varnumber_T number)
874{
875 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000876
877 RETURN_OK_IF_SKIP(cctx);
878 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
879 return FAIL;
880 isn->isn_arg.number = number;
881
882 if (number == 0 || number == 1)
883 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000884 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000885 return OK;
886}
887
888/*
889 * Generate an ISN_PUSHBOOL instruction.
890 */
891 int
892generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
893{
894 isn_T *isn;
895
896 RETURN_OK_IF_SKIP(cctx);
897 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
898 return FAIL;
899 isn->isn_arg.number = number;
900
901 return OK;
902}
903
904/*
905 * Generate an ISN_PUSHSPEC instruction.
906 */
907 int
908generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
909{
910 isn_T *isn;
911
912 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000913 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
914 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000915 return FAIL;
916 isn->isn_arg.number = number;
917
918 return OK;
919}
920
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000921/*
922 * Generate an ISN_PUSHF instruction.
923 */
924 int
925generate_PUSHF(cctx_T *cctx, float_T fnumber)
926{
927 isn_T *isn;
928
929 RETURN_OK_IF_SKIP(cctx);
930 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
931 return FAIL;
932 isn->isn_arg.fnumber = fnumber;
933
934 return OK;
935}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000936
937/*
938 * Generate an ISN_PUSHS instruction.
939 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100940 * Note that if "str" is used in the instruction OK is returned and "*str" is
941 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000942 */
943 int
944generate_PUSHS(cctx_T *cctx, char_u **str)
945{
946 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100947 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000948
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100949 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000950 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100951 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
952 ret = FAIL;
953 else
954 {
955 isn->isn_arg.string = str == NULL ? NULL : *str;
956 return OK;
957 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000958 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100959 if (str != NULL)
960 VIM_CLEAR(*str);
961 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000962}
963
964/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000965 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000966 */
967 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000968generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000969{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000970 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100971#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100972 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000973 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000974 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100975#else
976 emsg(_(e_channel_job_feature_not_available));
977 return FAIL;
978#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000979}
980
981/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000982 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000983 */
984 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000985generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000986{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000987 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100988#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100989 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000990 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000991 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100992#else
993 emsg(_(e_channel_job_feature_not_available));
994 return FAIL;
995#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000996}
997
998/*
999 * Generate an ISN_PUSHBLOB instruction.
1000 * Consumes "blob".
1001 */
1002 int
1003generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1004{
1005 isn_T *isn;
1006
1007 RETURN_OK_IF_SKIP(cctx);
1008 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1009 return FAIL;
1010 isn->isn_arg.blob = blob;
1011
1012 return OK;
1013}
1014
1015/*
1016 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001017 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
1018 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001019 */
1020 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001021generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001022{
1023 isn_T *isn;
1024 char_u *funcname;
1025
1026 RETURN_OK_IF_SKIP(cctx);
1027 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
1028 return FAIL;
1029 if (name == NULL)
1030 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001031 else if (!may_prefix
1032 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +00001033 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001034 funcname = vim_strsave(name);
1035 else
1036 {
1037 funcname = alloc(STRLEN(name) + 3);
1038 if (funcname != NULL)
1039 {
1040 STRCPY(funcname, "g:");
1041 STRCPY(funcname + 2, name);
1042 }
1043 }
1044
1045 isn->isn_arg.string = funcname;
1046 return OK;
1047}
1048
1049/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00001050 * Generate an ISN_AUTOLOAD instruction.
1051 */
1052 int
1053generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
1054{
1055 isn_T *isn;
1056
1057 RETURN_OK_IF_SKIP(cctx);
1058 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
1059 return FAIL;
1060 isn->isn_arg.string = vim_strsave(name);
1061 if (isn->isn_arg.string == NULL)
1062 return FAIL;
1063 return OK;
1064}
1065
1066/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001067 * Generate an ISN_GETITEM instruction with "index".
1068 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1069 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001070 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001071 */
1072 int
1073generate_GETITEM(cctx_T *cctx, int index, int with_op)
1074{
1075 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001076 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001077 type_T *item_type = &t_any;
1078
1079 RETURN_OK_IF_SKIP(cctx);
1080
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001081 item_type = get_item_type(type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001082 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1083 return FAIL;
1084 isn->isn_arg.getitem.gi_index = index;
1085 isn->isn_arg.getitem.gi_with_op = with_op;
1086
1087 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001088 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001089}
1090
1091/*
1092 * Generate an ISN_SLICE instruction with "count".
1093 */
1094 int
1095generate_SLICE(cctx_T *cctx, int count)
1096{
1097 isn_T *isn;
1098
1099 RETURN_OK_IF_SKIP(cctx);
1100 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1101 return FAIL;
1102 isn->isn_arg.number = count;
1103 return OK;
1104}
1105
1106/*
1107 * Generate an ISN_CHECKLEN instruction with "min_len".
1108 */
1109 int
1110generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1111{
1112 isn_T *isn;
1113
1114 RETURN_OK_IF_SKIP(cctx);
1115
1116 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1117 return FAIL;
1118 isn->isn_arg.checklen.cl_min_len = min_len;
1119 isn->isn_arg.checklen.cl_more_OK = more_OK;
1120
1121 return OK;
1122}
1123
1124/*
1125 * Generate an ISN_STORE instruction.
1126 */
1127 int
1128generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1129{
1130 isn_T *isn;
1131
1132 RETURN_OK_IF_SKIP(cctx);
1133 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1134 return FAIL;
1135 if (name != NULL)
1136 isn->isn_arg.string = vim_strsave(name);
1137 else
1138 isn->isn_arg.number = idx;
1139
1140 return OK;
1141}
1142
1143/*
Bram Moolenaard505d172022-12-18 21:42:55 +00001144 * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER
1145 * ("load" == FALSE) instruction.
1146 */
1147 int
1148generate_CLASSMEMBER(
1149 cctx_T *cctx,
1150 int load,
1151 class_T *cl,
1152 int idx)
1153{
1154 isn_T *isn;
1155
1156 RETURN_OK_IF_SKIP(cctx);
1157 if (load)
1158 {
1159 ocmember_T *m = &cl->class_class_members[idx];
1160 isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type);
1161 }
1162 else
1163 {
1164 isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1);
1165 }
1166 if (isn == NULL)
1167 return FAIL;
1168 isn->isn_arg.classmember.cm_class = cl;
1169 ++cl->class_refcount;
1170 isn->isn_arg.classmember.cm_idx = idx;
1171
1172 return OK;
1173}
1174
1175/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001176 * Generate an ISN_STOREOUTER instruction.
1177 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001178 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001179generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001180{
1181 isn_T *isn;
1182
1183 RETURN_OK_IF_SKIP(cctx);
1184 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1185 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001186 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
1187 {
1188 // Store a variable defined in a loop. A copy will be made at the end
1189 // of the loop. TODO: how about deeper nesting?
1190 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1191 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1192 }
1193 else
1194 {
1195 isn->isn_arg.outer.outer_idx = idx;
1196 isn->isn_arg.outer.outer_depth = level;
1197 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001198
1199 return OK;
1200}
1201
1202/*
1203 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1204 */
1205 int
1206generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1207{
1208 isn_T *isn;
1209
1210 RETURN_OK_IF_SKIP(cctx);
1211 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1212 return FAIL;
1213 isn->isn_arg.storenr.stnr_idx = idx;
1214 isn->isn_arg.storenr.stnr_val = value;
1215
1216 return OK;
1217}
1218
1219/*
1220 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1221 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001222 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001223generate_STOREOPT(
1224 cctx_T *cctx,
1225 isntype_T isn_type,
1226 char_u *name,
1227 int opt_flags)
1228{
1229 isn_T *isn;
1230
1231 RETURN_OK_IF_SKIP(cctx);
1232 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1233 return FAIL;
1234 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1235 isn->isn_arg.storeopt.so_flags = opt_flags;
1236
1237 return OK;
1238}
1239
1240/*
1241 * Generate an ISN_LOAD or similar instruction.
1242 */
1243 int
1244generate_LOAD(
1245 cctx_T *cctx,
1246 isntype_T isn_type,
1247 int idx,
1248 char_u *name,
1249 type_T *type)
1250{
1251 isn_T *isn;
1252
1253 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001254 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001255 return FAIL;
1256 if (name != NULL)
1257 isn->isn_arg.string = vim_strsave(name);
1258 else
1259 isn->isn_arg.number = idx;
1260
1261 return OK;
1262}
1263
1264/*
1265 * Generate an ISN_LOADOUTER instruction
1266 */
1267 int
1268generate_LOADOUTER(
1269 cctx_T *cctx,
1270 int idx,
1271 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001272 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001273 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001274 type_T *type)
1275{
1276 isn_T *isn;
1277
1278 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001279 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001280 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001281 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1282 {
1283 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001284 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001285 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001286 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001287 }
1288 else
1289 {
1290 isn->isn_arg.outer.outer_idx = idx;
1291 isn->isn_arg.outer.outer_depth = nesting;
1292 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001293
1294 return OK;
1295}
1296
1297/*
1298 * Generate an ISN_LOADV instruction for v:var.
1299 */
1300 int
1301generate_LOADV(
1302 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001303 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001304{
1305 int di_flags;
1306 int vidx = find_vim_var(name, &di_flags);
1307 type_T *type;
1308
1309 RETURN_OK_IF_SKIP(cctx);
1310 if (vidx < 0)
1311 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001312 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001313 return FAIL;
1314 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001315 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001316 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1317}
1318
1319/*
1320 * Generate an ISN_UNLET instruction.
1321 */
1322 int
1323generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1324{
1325 isn_T *isn;
1326
1327 RETURN_OK_IF_SKIP(cctx);
1328 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1329 return FAIL;
1330 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1331 isn->isn_arg.unlet.ul_forceit = forceit;
1332
1333 return OK;
1334}
1335
1336/*
1337 * Generate an ISN_LOCKCONST instruction.
1338 */
1339 int
1340generate_LOCKCONST(cctx_T *cctx)
1341{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001342 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001343 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001344 return FAIL;
1345 return OK;
1346}
1347
1348/*
1349 * Generate an ISN_LOADS instruction.
1350 */
1351 int
1352generate_OLDSCRIPT(
1353 cctx_T *cctx,
1354 isntype_T isn_type,
1355 char_u *name,
1356 int sid,
1357 type_T *type)
1358{
1359 isn_T *isn;
1360
1361 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001362 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001363 isn = generate_instr_type(cctx, isn_type, type);
1364 else
1365 isn = generate_instr_drop(cctx, isn_type, 1);
1366 if (isn == NULL)
1367 return FAIL;
1368 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1369 isn->isn_arg.loadstore.ls_sid = sid;
1370
1371 return OK;
1372}
1373
1374/*
1375 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1376 */
1377 int
1378generate_VIM9SCRIPT(
1379 cctx_T *cctx,
1380 isntype_T isn_type,
1381 int sid,
1382 int idx,
1383 type_T *type)
1384{
1385 isn_T *isn;
1386 scriptref_T *sref;
1387 scriptitem_T *si = SCRIPT_ITEM(sid);
1388
1389 RETURN_OK_IF_SKIP(cctx);
1390 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001391 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001392 else
1393 isn = generate_instr_drop(cctx, isn_type, 1);
1394 if (isn == NULL)
1395 return FAIL;
1396
1397 // This requires three arguments, which doesn't fit in an instruction, thus
1398 // we need to allocate a struct for this.
1399 sref = ALLOC_ONE(scriptref_T);
1400 if (sref == NULL)
1401 return FAIL;
1402 isn->isn_arg.script.scriptref = sref;
1403 sref->sref_sid = sid;
1404 sref->sref_idx = idx;
1405 sref->sref_seq = si->sn_script_seq;
1406 sref->sref_type = type;
1407 return OK;
1408}
1409
1410/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001411 * Generate an ISN_NEWLIST instruction for "count" items.
1412 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001413 */
1414 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001415generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001416{
1417 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001418 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001419 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001420 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001421
1422 RETURN_OK_IF_SKIP(cctx);
1423 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1424 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001425 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001426
Bram Moolenaar078a4612022-01-04 15:17:03 +00001427 // Get the member type and the declared member type from all the items on
1428 // the stack.
Ernie Raelfa831102023-12-14 20:06:39 +01001429 if ((member_type = get_member_type_from_stack(count, 1, cctx)) == NULL)
1430 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001431 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001432 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001433
1434 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001435 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001436
1437 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001438 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001439}
1440
1441/*
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001442 * Generate an ISN_NEWTUPLE instruction for "count" items.
1443 * "use_null" is TRUE for null_tuple.
1444 */
1445 int
1446generate_NEWTUPLE(cctx_T *cctx, int count, int use_null)
1447{
1448 isn_T *isn;
1449 type_T *type;
1450 type_T *decl_type;
1451
1452 RETURN_OK_IF_SKIP(cctx);
1453 if ((isn = generate_instr(cctx, ISN_NEWTUPLE)) == NULL)
1454 return FAIL;
1455 isn->isn_arg.number = use_null ? -1 : count;
1456
1457 // Get the member type and the declared member type from all the items on
1458 // the stack.
1459 garray_T tuple_types_ga;
1460 ga_init2(&tuple_types_ga, sizeof(type_T *), 10);
1461
1462 if (get_tuple_type_from_stack(count, &tuple_types_ga, cctx) < 0)
1463 {
1464 ga_clear(&tuple_types_ga);
1465 return FAIL;
1466 }
1467
1468 type = get_tuple_type(&tuple_types_ga, cctx->ctx_type_list);
1469 decl_type = &t_tuple_any;
1470
1471 ga_clear(&tuple_types_ga);
1472
1473 // drop the value types
1474 cctx->ctx_type_stack.ga_len -= count;
1475
1476 // add the tuple type to the type stack
1477 return push_type_stack2(cctx, type, decl_type);
1478}
1479
1480/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001481 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001482 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001483 */
1484 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001485generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001486{
1487 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001488 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001489 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001490 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001491
1492 RETURN_OK_IF_SKIP(cctx);
1493 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1494 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001495 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001496
Ernie Raelfa831102023-12-14 20:06:39 +01001497 if ((member_type = get_member_type_from_stack(count, 2, cctx)) == NULL)
1498 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001499 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001500 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001501
1502 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001503 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001504
1505 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001506 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001507}
1508
1509/*
1510 * Generate an ISN_FUNCREF instruction.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001511 * For "obj.Method" "cl" is the class of the object (can be an interface or a
1512 * base class) and "fi" the index of the method on that class.
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001513 * "isn_idx" is set to the index of the instruction, so that fr_dfunc_idx can
1514 * be set later. The index is used instead of a pointer to the instruction
1515 * because the instruction memory can be reallocated.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001516 */
1517 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001518generate_FUNCREF(
1519 cctx_T *cctx,
1520 ufunc_T *ufunc,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001521 class_T *cl,
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001522 int object_method,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001523 int fi,
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001524 int *isn_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001525{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001526 isn_T *isn;
1527 type_T *type;
1528 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001529 loopvarinfo_T loopinfo;
1530 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001531
1532 RETURN_OK_IF_SKIP(cctx);
1533 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1534 return FAIL;
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001535 if (isn_idx != NULL)
1536 // save the index of the new instruction
1537 *isn_idx = cctx->ctx_instr.ga_len - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001538
Bram Moolenaarcc341812022-09-19 15:54:34 +01001539 has_vars = get_loop_var_info(cctx, &loopinfo);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001540 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001541 {
1542 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1543 if (extra == NULL)
1544 return FAIL;
1545 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001546 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001547 if (cl != NULL)
1548 {
1549 extra->fre_class = cl;
1550 ++cl->class_refcount;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001551 extra->fre_object_method = object_method;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001552 extra->fre_method_idx = fi;
1553 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001554 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00001555 if (ufunc->uf_def_status == UF_NOT_COMPILED || cl != NULL)
John Marriottb32800f2025-02-01 15:25:34 +01001556 extra->fre_func_name = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001557 if (ufunc->uf_def_status != UF_NOT_COMPILED && cl == NULL)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001558 {
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001559 if (isn_idx == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001560 // compile the function now, we need the uf_dfunc_idx value
1561 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001562 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001563 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001564
1565 // Reserve an extra variable to keep track of the number of closures
1566 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001567 cctx->ctx_has_closure = 1;
1568
1569 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001570 // the nested context, including this one. But not a function defined at
1571 // the script level.
1572 if ((ufunc->uf_flags & FC_CLOSURE)
1573 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001574 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1575
Bram Moolenaar078a4612022-01-04 15:17:03 +00001576 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1577 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001578}
1579
1580/*
1581 * Generate an ISN_NEWFUNC instruction.
1582 * "lambda_name" and "func_name" must be in allocated memory and will be
1583 * consumed.
1584 */
1585 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001586generate_NEWFUNC(
1587 cctx_T *cctx,
1588 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001589 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001590{
1591 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001592 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001593
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001594 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001595 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001596 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1597 ret = FAIL;
1598 else
1599 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001600 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1601
1602 if (arg == NULL)
1603 ret = FAIL;
1604 else
1605 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001606 // Reserve an extra variable to keep track of the number of
1607 // closures created.
1608 cctx->ctx_has_closure = 1;
1609
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001610 isn->isn_arg.newfunc.nf_arg = arg;
1611 arg->nfa_lambda = lambda_name;
1612 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001613 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001614 return OK;
1615 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001616 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001617 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001618 vim_free(lambda_name);
1619 vim_free(func_name);
1620 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001621}
1622
1623/*
1624 * Generate an ISN_DEF instruction: list functions
1625 */
1626 int
1627generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1628{
1629 isn_T *isn;
1630
1631 RETURN_OK_IF_SKIP(cctx);
1632 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1633 return FAIL;
1634 if (len > 0)
1635 {
1636 isn->isn_arg.string = vim_strnsave(name, len);
1637 if (isn->isn_arg.string == NULL)
1638 return FAIL;
1639 }
1640 return OK;
1641}
1642
1643/*
1644 * Generate an ISN_JUMP instruction.
1645 */
1646 int
1647generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1648{
1649 isn_T *isn;
1650 garray_T *stack = &cctx->ctx_type_stack;
1651
1652 RETURN_OK_IF_SKIP(cctx);
1653 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1654 return FAIL;
1655 isn->isn_arg.jump.jump_when = when;
1656 isn->isn_arg.jump.jump_where = where;
1657
1658 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1659 --stack->ga_len;
1660
1661 return OK;
1662}
1663
1664/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001665 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1666 */
1667 int
1668generate_WHILE(cctx_T *cctx, int funcref_idx)
1669{
1670 isn_T *isn;
1671 garray_T *stack = &cctx->ctx_type_stack;
1672
1673 RETURN_OK_IF_SKIP(cctx);
1674 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1675 return FAIL;
1676 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1677 isn->isn_arg.whileloop.while_end = 0; // filled in later
1678
1679 if (stack->ga_len > 0)
1680 --stack->ga_len;
1681
1682 return OK;
1683}
1684
1685/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001686 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001687 */
1688 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001689generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001690{
1691 isn_T *isn;
1692
1693 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001694 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001695 return FAIL;
1696 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1697 // jump_where is set later
1698 return OK;
1699}
1700
1701 int
1702generate_FOR(cctx_T *cctx, int loop_idx)
1703{
1704 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001705
1706 RETURN_OK_IF_SKIP(cctx);
1707 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1708 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001709 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001710
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001711 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001712 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001713}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001714
1715 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001716generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001717{
1718 isn_T *isn;
1719
1720 RETURN_OK_IF_SKIP(cctx);
1721 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1722 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001723 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1724 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1725 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001726 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001727 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001728 return OK;
1729}
1730
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001731/*
1732 * Generate an ISN_TRYCONT instruction.
1733 */
1734 int
1735generate_TRYCONT(cctx_T *cctx, int levels, int where)
1736{
1737 isn_T *isn;
1738
1739 RETURN_OK_IF_SKIP(cctx);
1740 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1741 return FAIL;
1742 isn->isn_arg.trycont.tct_levels = levels;
1743 isn->isn_arg.trycont.tct_where = where;
1744
1745 return OK;
1746}
1747
Bram Moolenaar16900322022-09-08 19:51:45 +01001748/*
1749 * Check "argount" arguments and their types on the type stack.
1750 * Give an error and return FAIL if something is wrong.
1751 * When "method_call" is NULL no code is generated.
1752 */
1753 int
1754check_internal_func_args(
1755 cctx_T *cctx,
1756 int func_idx,
1757 int argcount,
1758 int method_call,
1759 type2_T **argtypes,
1760 type2_T *shuffled_argtypes)
1761{
1762 garray_T *stack = &cctx->ctx_type_stack;
1763 int argoff = check_internal_func(func_idx, argcount);
1764
1765 if (argoff < 0)
1766 return FAIL;
1767
1768 if (method_call && argoff > 1)
1769 {
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001770 if (argcount < argoff)
1771 {
1772 semsg(_(e_not_enough_arguments_for_function_str),
1773 internal_func_name(func_idx));
1774 return FAIL;
1775 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001776
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001777 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
Bram Moolenaar16900322022-09-08 19:51:45 +01001778 if (isn == NULL)
1779 return FAIL;
1780 isn->isn_arg.shuffle.shfl_item = argcount;
1781 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1782 }
1783
1784 if (argcount > 0)
1785 {
1786 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1787
1788 // Check the types of the arguments.
1789 if (method_call && argoff > 1)
1790 {
1791 int i;
1792
1793 for (i = 0; i < argcount; ++i)
1794 shuffled_argtypes[i] = (i < argoff - 1)
1795 ? typep[i + 1]
1796 : (i == argoff - 1) ? typep[0] : typep[i];
1797 *argtypes = shuffled_argtypes;
1798 }
1799 else
1800 {
1801 int i;
1802
1803 for (i = 0; i < argcount; ++i)
1804 shuffled_argtypes[i] = typep[i];
1805 *argtypes = shuffled_argtypes;
1806 }
1807 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1808 cctx) == FAIL)
1809 return FAIL;
1810 }
1811 return OK;
1812}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001813
1814/*
1815 * Generate an ISN_BCALL instruction.
1816 * "method_call" is TRUE for "value->method()"
1817 * Return FAIL if the number of arguments is wrong.
1818 */
1819 int
1820generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1821{
1822 isn_T *isn;
1823 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001824 type2_T *argtypes = NULL;
1825 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1826 type2_T *maptype = NULL;
1827 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001828 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001829
1830 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001831
1832 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1833 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001834 return FAIL;
1835
Bram Moolenaar16900322022-09-08 19:51:45 +01001836 if (internal_func_is_map(func_idx))
1837 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001838
1839 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1840 return FAIL;
1841 isn->isn_arg.bfunc.cbf_idx = func_idx;
1842 isn->isn_arg.bfunc.cbf_argcount = argcount;
1843
1844 // Drop the argument types and push the return type.
1845 stack->ga_len -= argcount;
Bram Moolenaar32517c42023-01-15 18:17:12 +00001846 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type,
1847 cctx->ctx_type_list);
Bram Moolenaar81330182022-02-01 12:11:58 +00001848 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001849 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001850
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001851 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1852 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001853 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001854 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001855
1856 return OK;
1857}
1858
1859/*
1860 * Generate an ISN_LISTAPPEND instruction. Works like add().
1861 * Argument count is already checked.
1862 */
1863 int
1864generate_LISTAPPEND(cctx_T *cctx)
1865{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001866 type_T *list_type;
1867 type_T *item_type;
1868 type_T *expected;
1869
1870 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001871 // For checking the item type we use the declared type of the list and the
1872 // current type of the added item, adding a string to [1, 2] is OK.
1873 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001874 if (arg_type_modifiable(list_type, 1) == FAIL)
1875 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001876 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001877 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001878 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001879 return FAIL;
1880
1881 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1882 return FAIL;
1883
Bram Moolenaar078a4612022-01-04 15:17:03 +00001884 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001885 return OK;
1886}
1887
1888/*
1889 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1890 * Argument count is already checked.
1891 */
1892 int
1893generate_BLOBAPPEND(cctx_T *cctx)
1894{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001895 type_T *item_type;
1896
Bram Moolenaarfa103972022-09-29 19:14:42 +01001897 // Caller already checked that blob_type is a blob, check it is modifiable.
1898 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1899 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001900 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001901 if (need_type(item_type, &t_number, FALSE,
1902 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001903 return FAIL;
1904
1905 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1906 return FAIL;
1907
Bram Moolenaar078a4612022-01-04 15:17:03 +00001908 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001909 return OK;
1910}
1911
1912/*
Bram Moolenaard0200c82023-01-28 15:19:40 +00001913 * Generate an ISN_DCALL, ISN_UCALL or ISN_METHODCALL instruction.
1914 * When calling a method on an object, of which we know the interface only,
1915 * then "cl" is the interface and "mi" the method index on the interface.
Ernie Rael58c95792024-08-13 23:27:22 +02001916 * save is_super in the "isn->isn_arg"; it flags execution to use mfunc
1917 * directly to determine ufunc.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001918 * Return FAIL if the number of arguments is wrong.
1919 */
1920 int
Bram Moolenaard0200c82023-01-28 15:19:40 +00001921generate_CALL(
1922 cctx_T *cctx,
1923 ufunc_T *ufunc,
1924 class_T *cl,
1925 int mi,
Ernie Rael58c95792024-08-13 23:27:22 +02001926 int pushed_argcount,
1927 int is_super)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001928{
1929 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001930 int regular_args = ufunc->uf_args.ga_len;
1931 int argcount = pushed_argcount;
1932
1933 RETURN_OK_IF_SKIP(cctx);
1934 if (argcount > regular_args && !has_varargs(ufunc))
1935 {
1936 semsg(_(e_too_many_arguments_for_function_str),
1937 printable_func_name(ufunc));
1938 return FAIL;
1939 }
1940 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1941 {
1942 semsg(_(e_not_enough_arguments_for_function_str),
1943 printable_func_name(ufunc));
1944 return FAIL;
1945 }
1946
1947 if (ufunc->uf_def_status != UF_NOT_COMPILED
1948 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1949 {
1950 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001951 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001952
1953 for (i = 0; i < argcount; ++i)
1954 {
1955 type_T *expected;
1956 type_T *actual;
1957
Bram Moolenaar078a4612022-01-04 15:17:03 +00001958 actual = get_type_on_stack(cctx, argcount - i - 1);
Ernie Raelb077b582023-12-14 20:11:44 +01001959 if (check_type_is_value(actual) == FAIL)
1960 return FAIL;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001961 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001962 && i >= regular_args - ufunc->uf_def_args.ga_len)
1963 {
1964 // assume v:none used for default argument value
1965 continue;
1966 }
1967 if (i < regular_args)
1968 {
1969 if (ufunc->uf_arg_types == NULL)
1970 continue;
1971 expected = ufunc->uf_arg_types[i];
1972 }
1973 else if (ufunc->uf_va_type == NULL
1974 || ufunc->uf_va_type == &t_list_any)
1975 // possibly a lambda or "...: any"
1976 expected = &t_any;
1977 else
1978 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001979 if (need_type(actual, expected, FALSE,
1980 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001981 {
1982 arg_type_mismatch(expected, actual, i + 1);
1983 return FAIL;
1984 }
1985 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001986 compile_type = get_compile_type(ufunc);
1987 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001988 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001989 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001990 return FAIL;
1991 }
1992 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1993 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001994 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001995 ufunc->uf_name);
1996 return FAIL;
1997 }
1998
Bram Moolenaard0200c82023-01-28 15:19:40 +00001999 if ((isn = generate_instr(cctx, cl != NULL ? ISN_METHODCALL
2000 : ufunc->uf_def_status != UF_NOT_COMPILED
2001 ? ISN_DCALL : ISN_UCALL)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002002 return FAIL;
Bram Moolenaar836137d2023-01-29 14:11:24 +00002003 if (cl != NULL /* isn->isn_type == ISN_METHODCALL */)
Bram Moolenaard0200c82023-01-28 15:19:40 +00002004 {
2005 isn->isn_arg.mfunc = ALLOC_ONE(cmfunc_T);
2006 if (isn->isn_arg.mfunc == NULL)
2007 return FAIL;
2008 isn->isn_arg.mfunc->cmf_itf = cl;
2009 ++cl->class_refcount;
2010 isn->isn_arg.mfunc->cmf_idx = mi;
2011 isn->isn_arg.mfunc->cmf_argcount = argcount;
Ernie Rael58c95792024-08-13 23:27:22 +02002012 isn->isn_arg.mfunc->cmf_is_super = is_super;
Bram Moolenaard0200c82023-01-28 15:19:40 +00002013 }
2014 else if (isn->isn_type == ISN_DCALL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002015 {
2016 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2017 isn->isn_arg.dfunc.cdf_argcount = argcount;
2018 }
2019 else
2020 {
2021 // A user function may be deleted and redefined later, can't use the
2022 // ufunc pointer, need to look it up again at runtime.
John Marriottb32800f2025-02-01 15:25:34 +01002023 isn->isn_arg.ufunc.cuf_name = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002024 isn->isn_arg.ufunc.cuf_argcount = argcount;
2025 }
2026
Bram Moolenaar078a4612022-01-04 15:17:03 +00002027 // drop the argument types
2028 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002029
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002030 // For an object or class method call, drop the object/class type.
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02002031 if (ufunc->uf_class != NULL)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002032 {
2033 // When a class method is called without the class name prefix, then
2034 // the type will not be in the stack.
2035 type_T *stype = get_type_on_stack(cctx, 0);
2036 if (stype->tt_type == VAR_CLASS || stype->tt_type == VAR_OBJECT)
2037 cctx->ctx_type_stack.ga_len--;
2038 }
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02002039
Bram Moolenaar078a4612022-01-04 15:17:03 +00002040 // add return type
2041 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002042}
2043
2044/*
2045 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2046 */
2047 int
2048generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2049{
2050 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002051
2052 RETURN_OK_IF_SKIP(cctx);
2053 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2054 return FAIL;
2055 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2056 isn->isn_arg.ufunc.cuf_argcount = argcount;
2057
Bram Moolenaar078a4612022-01-04 15:17:03 +00002058 // drop the argument types
2059 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002060
Bram Moolenaar078a4612022-01-04 15:17:03 +00002061 // add return value
2062 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002063}
2064
2065/*
Bram Moolenaar16900322022-09-08 19:51:45 +01002066 * Check the arguments of function "type" against the types on the stack.
2067 * Returns OK or FAIL;
2068 */
2069 int
2070check_func_args_from_type(
2071 cctx_T *cctx,
2072 type_T *type,
2073 int argcount,
2074 int at_top,
2075 char_u *name)
2076{
2077 if (type->tt_argcount != -1)
2078 {
2079 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2080
2081 if (argcount < type->tt_min_argcount - varargs)
2082 {
2083 emsg_funcname(e_not_enough_arguments_for_function_str, name);
2084 return FAIL;
2085 }
2086 if (!varargs && argcount > type->tt_argcount)
2087 {
2088 emsg_funcname(e_too_many_arguments_for_function_str, name);
2089 return FAIL;
2090 }
2091 if (type->tt_args != NULL)
2092 {
2093 int i;
2094
2095 for (i = 0; i < argcount; ++i)
2096 {
2097 int offset = -argcount + i - (at_top ? 0 : 1);
2098 type_T *actual = get_type_on_stack(cctx, -1 - offset);
2099 type_T *expected;
2100
Ernie Raelb077b582023-12-14 20:11:44 +01002101 if (check_type_is_value(actual) == FAIL)
2102 return FAIL;
Bram Moolenaar16900322022-09-08 19:51:45 +01002103 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaar36818a92023-01-03 12:33:26 +00002104 {
2105 expected = type->tt_args[type->tt_argcount - 1];
2106 if (expected != NULL && expected->tt_type == VAR_LIST)
2107 expected = expected->tt_member;
2108 if (expected == NULL)
2109 expected = &t_any;
2110 }
Bram Moolenaar16900322022-09-08 19:51:45 +01002111 else if (i >= type->tt_min_argcount
2112 && actual->tt_type == VAR_SPECIAL)
2113 expected = &t_any;
2114 else
2115 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002116 if (need_type(actual, expected, FALSE,
2117 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01002118 {
2119 arg_type_mismatch(expected, actual, i + 1);
2120 return FAIL;
2121 }
2122 }
2123 }
2124 }
2125
2126 return OK;
2127}
2128/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002129 * Generate an ISN_PCALL instruction.
2130 * "type" is the type of the FuncRef.
2131 */
2132 int
2133generate_PCALL(
2134 cctx_T *cctx,
2135 int argcount,
2136 char_u *name,
2137 type_T *type,
2138 int at_top)
2139{
2140 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002141 type_T *ret_type;
2142
2143 RETURN_OK_IF_SKIP(cctx);
2144
Yegappan Lakshmanande8f8f72025-04-01 20:43:36 +02002145 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN
2146 || type == &t_object_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002147 ret_type = &t_any;
2148 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
2149 {
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002150 if (check_func_args_from_type(cctx, type, argcount, at_top, name)
2151 == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01002152 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002153
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002154 ret_type = type->tt_member;
2155 if (ret_type == &t_unknown)
2156 // return type not known yet, use a runtime check
2157 ret_type = &t_any;
2158 }
2159 else
2160 {
2161 semsg(_(e_not_callable_type_str), name);
2162 return FAIL;
2163 }
2164
2165 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2166 return FAIL;
2167 isn->isn_arg.pfunc.cpf_top = at_top;
2168 isn->isn_arg.pfunc.cpf_argcount = argcount;
2169
Bram Moolenaar078a4612022-01-04 15:17:03 +00002170 // drop the arguments and the funcref/partial
2171 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002172
Bram Moolenaar078a4612022-01-04 15:17:03 +00002173 // push the return value
2174 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002175
2176 // If partial is above the arguments it must be cleared and replaced with
2177 // the return value.
2178 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2179 return FAIL;
2180
2181 return OK;
2182}
2183
2184/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002185 * Generate an ISN_DEFER instruction.
2186 */
2187 int
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02002188generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002189{
2190 isn_T *isn;
2191
2192 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02002193 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002194 return FAIL;
2195 isn->isn_arg.defer.defer_var_idx = var_idx;
2196 isn->isn_arg.defer.defer_argcount = argcount;
2197 return OK;
2198}
2199
2200/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002201 * Generate an ISN_STRINGMEMBER instruction.
2202 */
2203 int
2204generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
2205{
2206 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002207 type_T *type;
2208
2209 RETURN_OK_IF_SKIP(cctx);
2210 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
2211 return FAIL;
2212 isn->isn_arg.string = vim_strnsave(name, len);
2213
2214 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00002215 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002216 if (type->tt_type != VAR_DICT
Yegappan Lakshmanande8f8f72025-04-01 20:43:36 +02002217 && type->tt_type != VAR_OBJECT
2218 && type->tt_type != VAR_ANY
2219 && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002220 {
2221 char *tofree;
2222
2223 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2224 name, type_name(type, &tofree));
2225 vim_free(tofree);
2226 return FAIL;
2227 }
2228 // change dict type to dict member type
2229 if (type->tt_type == VAR_DICT)
2230 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002231 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00002232 ? &t_any : type->tt_member;
2233 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002234 }
2235
2236 return OK;
2237}
2238
2239/*
2240 * Generate an ISN_ECHO instruction.
2241 */
2242 int
2243generate_ECHO(cctx_T *cctx, int with_white, int count)
2244{
2245 isn_T *isn;
2246
2247 RETURN_OK_IF_SKIP(cctx);
2248 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2249 return FAIL;
2250 isn->isn_arg.echo.echo_with_white = with_white;
2251 isn->isn_arg.echo.echo_count = count;
2252
2253 return OK;
2254}
2255
2256/*
2257 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
2258 */
2259 int
2260generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
2261{
2262 isn_T *isn;
2263
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01002264 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002265 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
2266 return FAIL;
2267 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002268 return OK;
2269}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002270
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002271/*
2272 * Generate an ISN_ECHOWINDOW instruction
2273 */
2274 int
2275generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
2276{
2277 isn_T *isn;
2278
2279 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2280 return FAIL;
2281 isn->isn_arg.echowin.ewin_count = count;
2282 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002283 return OK;
2284}
2285
2286/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002287 * Generate an ISN_SOURCE instruction.
2288 */
2289 int
2290generate_SOURCE(cctx_T *cctx, int sid)
2291{
2292 isn_T *isn;
2293
2294 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2295 return FAIL;
2296 isn->isn_arg.number = sid;
2297
2298 return OK;
2299}
2300
2301/*
64-bitmane08f10a2025-03-18 22:14:34 +01002302 * Generate an ISN_PUT or ISN_IPUT instruction depending on fixindent.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002303 */
2304 int
64-bitmane08f10a2025-03-18 22:14:34 +01002305generate_PUT(cctx_T *cctx, int regname, linenr_T lnum, int fixindent)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002306{
2307 isn_T *isn;
2308
2309 RETURN_OK_IF_SKIP(cctx);
64-bitmane08f10a2025-03-18 22:14:34 +01002310 isn = (fixindent) ? generate_instr(cctx, ISN_IPUT) :
2311 generate_instr(cctx, ISN_PUT);
2312 if (isn == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002313 return FAIL;
2314 isn->isn_arg.put.put_regname = regname;
2315 isn->isn_arg.put.put_lnum = lnum;
2316 return OK;
2317}
2318
2319/*
Ernie Rael64885642023-10-04 20:16:22 +02002320 * Generate a LOCKUNLOCK instruction.The root item, where the indexing starts
2321 * to find the variable, is on the stack. The instr takes
2322 * - the string to parse, "root.b[idx1][idx2].d.val", to find the variable
2323 * - the class, if any, in which the string executes.
2324 * - if the root item is a function argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002325 * A copy is made of "line".
2326 */
2327 int
Ernie Raelee865f32023-09-29 19:53:55 +02002328generate_LOCKUNLOCK(cctx_T *cctx, char_u *line, int is_arg)
2329{
2330 isn_T *isn;
2331
2332 RETURN_OK_IF_SKIP(cctx);
2333 if ((isn = generate_instr(cctx, ISN_LOCKUNLOCK)) == NULL)
2334 return FAIL;
Ernie Rael64885642023-10-04 20:16:22 +02002335 class_T *cl = cctx->ctx_ufunc != NULL ? cctx->ctx_ufunc->uf_class : NULL;
2336 isn->isn_arg.lockunlock.lu_string = vim_strsave(line);
2337 isn->isn_arg.lockunlock.lu_cl_exec = cl;
2338 if (cl != NULL)
2339 ++cl->class_refcount;
2340 isn->isn_arg.lockunlock.lu_is_arg = is_arg;
Ernie Raelee865f32023-09-29 19:53:55 +02002341 return OK;
2342}
2343
2344/*
2345 * Generate an EXEC instruction that takes a string argument.
2346 * A copy is made of "line".
2347 */
2348 int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002349generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2350{
2351 isn_T *isn;
2352
2353 RETURN_OK_IF_SKIP(cctx);
2354 if ((isn = generate_instr(cctx, isntype)) == NULL)
2355 return FAIL;
2356 isn->isn_arg.string = vim_strsave(line);
2357 return OK;
2358}
2359
2360/*
2361 * Generate an EXEC instruction that takes a string argument.
2362 * "str" must be allocated, it is consumed.
2363 */
2364 int
2365generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2366{
2367 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002368 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002369
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002370 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002371 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002372 if ((isn = generate_instr(cctx, isntype)) == NULL)
2373 ret = FAIL;
2374 else
2375 {
2376 isn->isn_arg.string = str;
2377 return OK;
2378 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002379 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002380 vim_free(str);
2381 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002382}
2383
2384 int
2385generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2386{
2387 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002388
2389 RETURN_OK_IF_SKIP(cctx);
2390 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2391 return FAIL;
2392 isn->isn_arg.string = vim_strsave(line);
2393
Bram Moolenaar078a4612022-01-04 15:17:03 +00002394 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002395}
2396
2397 int
2398generate_EXECCONCAT(cctx_T *cctx, int count)
2399{
2400 isn_T *isn;
2401
2402 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2403 return FAIL;
2404 isn->isn_arg.number = count;
2405 return OK;
2406}
2407
2408/*
2409 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2410 */
2411 int
2412generate_RANGE(cctx_T *cctx, char_u *range)
2413{
2414 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002415
2416 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2417 return FAIL;
2418 isn->isn_arg.string = range;
2419
Bram Moolenaar078a4612022-01-04 15:17:03 +00002420 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002421}
2422
2423 int
2424generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2425{
2426 isn_T *isn;
2427
2428 RETURN_OK_IF_SKIP(cctx);
2429 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2430 return FAIL;
2431 isn->isn_arg.unpack.unp_count = var_count;
2432 isn->isn_arg.unpack.unp_semicolon = semicolon;
2433 return OK;
2434}
2435
2436/*
2437 * Generate an instruction for any command modifiers.
2438 */
2439 int
2440generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2441{
2442 isn_T *isn;
2443
2444 if (has_cmdmod(cmod, FALSE))
2445 {
2446 cctx->ctx_has_cmdmod = TRUE;
2447
2448 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2449 return FAIL;
2450 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2451 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2452 return FAIL;
2453 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2454 // filter program now belongs to the instruction
2455 cmod->cmod_filter_regmatch.regprog = NULL;
2456 }
2457
2458 return OK;
2459}
2460
2461 int
2462generate_undo_cmdmods(cctx_T *cctx)
2463{
2464 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2465 return FAIL;
2466 cctx->ctx_has_cmdmod = FALSE;
2467 return OK;
2468}
2469
2470/*
2471 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002472 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002473 * Return FAIL when out of memory.
2474 */
2475 int
2476generate_store_var(
2477 cctx_T *cctx,
2478 assign_dest_T dest,
2479 int opt_flags,
2480 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002481 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002482 char_u *name,
2483 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002484{
2485 switch (dest)
2486 {
2487 case dest_option:
2488 return generate_STOREOPT(cctx, ISN_STOREOPT,
2489 skip_option_env_lead(name), opt_flags);
2490 case dest_func_option:
2491 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2492 skip_option_env_lead(name), opt_flags);
2493 case dest_global:
2494 // include g: with the name, easier to execute that way
2495 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2496 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2497 case dest_buffer:
2498 // include b: with the name, easier to execute that way
2499 return generate_STORE(cctx, ISN_STOREB, 0, name);
2500 case dest_window:
2501 // include w: with the name, easier to execute that way
2502 return generate_STORE(cctx, ISN_STOREW, 0, name);
2503 case dest_tab:
2504 // include t: with the name, easier to execute that way
2505 return generate_STORE(cctx, ISN_STORET, 0, name);
2506 case dest_env:
2507 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2508 case dest_reg:
2509 return generate_STORE(cctx, ISN_STOREREG,
2510 name[1] == '@' ? '"' : name[1], NULL);
2511 case dest_vimvar:
2512 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2513 case dest_script:
Ernie Rael3f821d62024-04-24 20:07:50 +02002514 case dest_script_v9:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002515 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002516 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2517 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2518 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002519 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002520 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002521
Ernie Rael3f821d62024-04-24 20:07:50 +02002522 // If "sn_import_autoload", generate ISN_STOREEXPORT (not
2523 // ISN_STORES) if destination is in a vim9script or if
2524 // there is no "sn_autoload_prefix".
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002525 if (SCRIPT_ID_VALID(scriptvar_sid)
2526 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
Ernie Rael3f821d62024-04-24 20:07:50 +02002527 && ((SCRIPT_ITEM(scriptvar_sid)
2528 ->sn_autoload_prefix == NULL)
2529 || lhs->lhs_dest == dest_script_v9))
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002530 {
2531 // "import autoload './dir/script.vim'" - load script
2532 // first
2533 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2534 return FAIL;
2535 isn_type = ISN_STOREEXPORT;
2536 }
2537
2538 // "s:" may be included in the name.
2539 return generate_OLDSCRIPT(cctx, isn_type, name,
2540 scriptvar_sid, type);
2541 }
2542 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002543 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002544 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002545 case dest_class_member:
2546 return generate_CLASSMEMBER(cctx, FALSE,
2547 lhs->lhs_class, lhs->lhs_classmember_idx);
2548
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002549 case dest_local:
2550 case dest_expr:
2551 // cannot happen
2552 break;
2553 }
2554 return FAIL;
2555}
2556
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002557/*
2558 * Return TRUE when inside a "for" or "while" loop.
2559 */
2560 int
2561inside_loop_scope(cctx_T *cctx)
2562{
2563 scope_T *scope = cctx->ctx_scope;
2564
2565 for (;;)
2566 {
2567 if (scope == NULL)
2568 break;
2569 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2570 return TRUE;
2571 scope = scope->se_outer;
2572 }
2573 return FALSE;
2574}
2575
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002576 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002577generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002578{
2579 if (lhs->lhs_dest != dest_local)
2580 return generate_store_var(cctx, lhs->lhs_dest,
2581 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002582 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002583
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002584 if (lhs->lhs_lvar == NULL)
2585 return OK;
2586
2587 garray_T *instr = &cctx->ctx_instr;
2588 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2589
2590 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2591 // ISN_STORENR.
2592 // And "var = 0" does not need any instruction.
2593 if (lhs->lhs_lvar->lv_from_outer == 0
2594 && instr->ga_len == instr_count + 1
2595 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002596 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002597 varnumber_T val = isn->isn_arg.number;
2598 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002599
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002600 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002601 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002602 // zero is the default value, no need to do anything
2603 --instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002604 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002605 else
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002606 {
2607 isn->isn_type = ISN_STORENR;
2608 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2609 isn->isn_arg.storenr.stnr_val = val;
2610 }
2611 if (stack->ga_len > 0)
2612 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002613 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002614 else if (lhs->lhs_lvar->lv_from_outer > 0)
2615 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2616 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
2617 else
2618 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002619 return OK;
2620}
2621
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01002622/*
2623 * Generate instruction to set the script context. Used to evaluate an
2624 * object member variable initialization expression in the context of the
2625 * script where the class is defined.
2626 */
2627 int
2628generate_SCRIPTCTX_SET(cctx_T *cctx, sctx_T new_sctx)
2629{
2630 isn_T *isn;
2631
2632 RETURN_OK_IF_SKIP(cctx);
2633 if ((isn = generate_instr(cctx, ISN_SCRIPTCTX_SET)) == NULL)
2634 return FAIL;
2635 isn->isn_arg.setsctx = new_sctx;
2636 return OK;
2637}
2638
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002639#if defined(FEAT_PROFILE) || defined(PROTO)
2640 void
2641may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2642{
2643 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2644 generate_instr(cctx, ISN_PROF_END);
2645}
2646#endif
2647
2648
2649/*
2650 * Delete an instruction, free what it contains.
2651 */
2652 void
2653delete_instr(isn_T *isn)
2654{
2655 switch (isn->isn_type)
2656 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002657 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002658 case ISN_DEF:
2659 case ISN_EXEC:
2660 case ISN_EXECRANGE:
2661 case ISN_EXEC_SPLIT:
2662 case ISN_LEGACY_EVAL:
2663 case ISN_LOADAUTO:
2664 case ISN_LOADB:
2665 case ISN_LOADENV:
2666 case ISN_LOADG:
2667 case ISN_LOADOPT:
2668 case ISN_LOADT:
2669 case ISN_LOADW:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002670 case ISN_PUSHEXC:
2671 case ISN_PUSHFUNC:
2672 case ISN_PUSHS:
2673 case ISN_RANGE:
2674 case ISN_STOREAUTO:
2675 case ISN_STOREB:
2676 case ISN_STOREENV:
2677 case ISN_STOREG:
2678 case ISN_STORET:
2679 case ISN_STOREW:
2680 case ISN_STRINGMEMBER:
2681 vim_free(isn->isn_arg.string);
2682 break;
2683
Ernie Rael64885642023-10-04 20:16:22 +02002684 case ISN_LOCKUNLOCK:
2685 class_unref(isn->isn_arg.lockunlock.lu_cl_exec);
2686 vim_free(isn->isn_arg.lockunlock.lu_string);
2687 break;
2688
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002689 case ISN_SUBSTITUTE:
2690 {
2691 int idx;
2692 isn_T *list = isn->isn_arg.subs.subs_instr;
2693
2694 vim_free(isn->isn_arg.subs.subs_cmd);
2695 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2696 delete_instr(list + idx);
2697 vim_free(list);
2698 }
2699 break;
2700
2701 case ISN_INSTR:
2702 {
2703 int idx;
2704 isn_T *list = isn->isn_arg.instr;
2705
2706 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2707 delete_instr(list + idx);
2708 vim_free(list);
2709 }
2710 break;
2711
2712 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002713 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002714 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002715 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002716 vim_free(isn->isn_arg.loadstore.ls_name);
2717 break;
2718
2719 case ISN_UNLET:
2720 case ISN_UNLETENV:
2721 vim_free(isn->isn_arg.unlet.ul_name);
2722 break;
2723
2724 case ISN_STOREOPT:
2725 case ISN_STOREFUNCOPT:
2726 vim_free(isn->isn_arg.storeopt.so_name);
2727 break;
2728
2729 case ISN_PUSHBLOB: // push blob isn_arg.blob
2730 blob_unref(isn->isn_arg.blob);
2731 break;
2732
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002733 case ISN_PUSHCLASS:
Bram Moolenaar30a84472023-02-27 08:07:14 +00002734 class_unref(isn->isn_arg.classarg);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002735 break;
2736
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002737 case ISN_UCALL:
2738 vim_free(isn->isn_arg.ufunc.cuf_name);
2739 break;
2740
2741 case ISN_FUNCREF:
2742 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002743 funcref_T *funcref = &isn->isn_arg.funcref;
2744 funcref_extra_T *extra = funcref->fr_extra;
2745
2746 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002747 {
2748 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002749 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002750 ufunc_T *ufunc = dfunc->df_ufunc;
2751
2752 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2753 func_ptr_unref(ufunc);
2754 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002755 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002756 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002757 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002758
2759 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002760 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002761 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002762 vim_free(name);
2763 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00002764 if (extra->fre_class != NULL)
2765 class_unref(extra->fre_class);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002766 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002767 }
2768 }
2769 break;
2770
2771 case ISN_DCALL:
2772 {
2773 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002774 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002775
2776 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002777 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002778 func_ptr_unref(dfunc->df_ufunc);
2779 }
2780 break;
2781
Bram Moolenaard0200c82023-01-28 15:19:40 +00002782 case ISN_METHODCALL:
2783 {
2784 cmfunc_T *mfunc = isn->isn_arg.mfunc;
2785 class_unref(mfunc->cmf_itf);
2786 vim_free(mfunc);
2787 }
2788 break;
2789
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002790 case ISN_NEWFUNC:
2791 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002792 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002793
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002794 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002795 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002796 ufunc_T *ufunc = find_func_even_dead(
2797 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002798
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002799 if (ufunc != NULL)
2800 {
2801 unlink_def_function(ufunc);
2802 func_ptr_unref(ufunc);
2803 }
2804
2805 vim_free(arg->nfa_lambda);
2806 vim_free(arg->nfa_global);
2807 vim_free(arg);
2808 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002809 }
2810 break;
2811
2812 case ISN_CHECKTYPE:
2813 case ISN_SETTYPE:
2814 free_type(isn->isn_arg.type.ct_type);
2815 break;
2816
2817 case ISN_CMDMOD:
2818 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002819 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002820 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2821 break;
2822
2823 case ISN_LOADSCRIPT:
2824 case ISN_STORESCRIPT:
2825 vim_free(isn->isn_arg.script.scriptref);
2826 break;
2827
Bram Moolenaard505d172022-12-18 21:42:55 +00002828 case ISN_LOAD_CLASSMEMBER:
2829 case ISN_STORE_CLASSMEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00002830 case ISN_GET_ITF_MEMBER:
Bram Moolenaard505d172022-12-18 21:42:55 +00002831 class_unref(isn->isn_arg.classmember.cm_class);
2832 break;
2833
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002834 case ISN_STOREINDEX:
2835 class_unref(isn->isn_arg.storeindex.si_class);
2836 break;
2837
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002838 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002839 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002840 break;
2841
2842 case ISN_CEXPR_CORE:
2843 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2844 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2845 break;
2846
2847 case ISN_2BOOL:
2848 case ISN_2STRING:
2849 case ISN_2STRING_ANY:
2850 case ISN_ADDBLOB:
2851 case ISN_ADDLIST:
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002852 case ISN_ADDTUPLE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002853 case ISN_ANYINDEX:
2854 case ISN_ANYSLICE:
2855 case ISN_BCALL:
2856 case ISN_BLOBAPPEND:
2857 case ISN_BLOBINDEX:
2858 case ISN_BLOBSLICE:
2859 case ISN_CATCH:
2860 case ISN_CEXPR_AUCMD:
2861 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002862 case ISN_CLEARDICT:
2863 case ISN_CMDMOD_REV:
2864 case ISN_COMPAREANY:
2865 case ISN_COMPAREBLOB:
2866 case ISN_COMPAREBOOL:
2867 case ISN_COMPAREDICT:
2868 case ISN_COMPAREFLOAT:
2869 case ISN_COMPAREFUNC:
2870 case ISN_COMPARELIST:
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002871 case ISN_COMPARETUPLE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002872 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002873 case ISN_COMPARENULL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002874 case ISN_COMPAREOBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002875 case ISN_COMPARESPECIAL:
2876 case ISN_COMPARESTRING:
2877 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002878 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002879 case ISN_COND2BOOL:
2880 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002881 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002882 case ISN_DROP:
2883 case ISN_ECHO:
2884 case ISN_ECHOCONSOLE:
2885 case ISN_ECHOERR:
2886 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002887 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002888 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002889 case ISN_ENDTRY:
2890 case ISN_EXECCONCAT:
2891 case ISN_EXECUTE:
2892 case ISN_FINALLY:
2893 case ISN_FINISH:
2894 case ISN_FOR:
2895 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002896 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002897 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002898 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002899 case ISN_JUMP_IF_ARG_SET:
2900 case ISN_LISTAPPEND:
2901 case ISN_LISTINDEX:
2902 case ISN_LISTSLICE:
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002903 case ISN_TUPLEINDEX:
2904 case ISN_TUPLESLICE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002905 case ISN_LOAD:
2906 case ISN_LOADBDICT:
2907 case ISN_LOADGDICT:
2908 case ISN_LOADOUTER:
2909 case ISN_LOADREG:
2910 case ISN_LOADTDICT:
2911 case ISN_LOADV:
2912 case ISN_LOADWDICT:
2913 case ISN_LOCKCONST:
2914 case ISN_MEMBER:
2915 case ISN_NEGATENR:
2916 case ISN_NEWDICT:
2917 case ISN_NEWLIST:
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002918 case ISN_NEWTUPLE:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002919 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002920 case ISN_OPANY:
2921 case ISN_OPFLOAT:
2922 case ISN_OPNR:
2923 case ISN_PCALL:
2924 case ISN_PCALL_END:
2925 case ISN_PROF_END:
2926 case ISN_PROF_START:
2927 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002928 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002929 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002930 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002931 case ISN_PUSHNR:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002932 case ISN_PUSHOBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002933 case ISN_PUSHSPEC:
2934 case ISN_PUT:
64-bitmane08f10a2025-03-18 22:14:34 +01002935 case ISN_IPUT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002936 case ISN_REDIREND:
2937 case ISN_REDIRSTART:
2938 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002939 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002940 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002941 case ISN_SHUFFLE:
2942 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002943 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002944 case ISN_STORE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002945 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002946 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002947 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002948 case ISN_STORERANGE:
2949 case ISN_STOREREG:
2950 case ISN_STOREV:
2951 case ISN_STRINDEX:
2952 case ISN_STRSLICE:
2953 case ISN_THROW:
2954 case ISN_TRYCONT:
2955 case ISN_UNLETINDEX:
2956 case ISN_UNLETRANGE:
2957 case ISN_UNPACK:
2958 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002959 case ISN_WHILE:
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01002960 case ISN_SCRIPTCTX_SET:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002961 // nothing allocated
2962 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002963 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002964}
2965
2966 void
2967clear_instr_ga(garray_T *gap)
2968{
2969 int idx;
2970
2971 for (idx = 0; idx < gap->ga_len; ++idx)
2972 delete_instr(((isn_T *)gap->ga_data) + idx);
2973 ga_clear(gap);
2974}
2975
2976
2977#endif // defined(FEAT_EVAL)