blob: 3547d42f4ad8f82ae52f826f5b1ea57e35a18f11 [file] [log] [blame]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9instr.c: Dealing with instructions of a compiled function
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
24
25/////////////////////////////////////////////////////////////////////
26// Following generate_ functions expect the caller to call ga_grow().
27
28#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
29#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
30
31/*
32 * Generate an instruction without arguments.
33 * Returns a pointer to the new instruction, NULL if failed.
34 */
35 isn_T *
36generate_instr(cctx_T *cctx, isntype_T isn_type)
37{
38 garray_T *instr = &cctx->ctx_instr;
39 isn_T *isn;
40
41 RETURN_NULL_IF_SKIP(cctx);
42 if (GA_GROW_FAILS(instr, 1))
43 return NULL;
44 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
45 isn->isn_type = isn_type;
46 isn->isn_lnum = cctx->ctx_lnum + 1;
47 ++instr->ga_len;
48
49 return isn;
50}
51
52/*
53 * Generate an instruction without arguments.
54 * "drop" will be removed from the stack.
55 * Returns a pointer to the new instruction, NULL if failed.
56 */
57 isn_T *
58generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
59{
Bram Moolenaardc7c3662021-12-20 15:04:29 +000060 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +000061 cctx->ctx_type_stack.ga_len -= drop;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000062 return generate_instr(cctx, isn_type);
63}
64
65/*
Bram Moolenaar078a4612022-01-04 15:17:03 +000066 * Generate instruction "isn_type" and put "type" on the type stack,
67 * use "decl_type" for the declared type.
Bram Moolenaardc7c3662021-12-20 15:04:29 +000068 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +000069 static isn_T *
Bram Moolenaar078a4612022-01-04 15:17:03 +000070generate_instr_type2(
71 cctx_T *cctx,
72 isntype_T isn_type,
73 type_T *type,
74 type_T *decl_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000075{
76 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000077
78 if ((isn = generate_instr(cctx, isn_type)) == NULL)
79 return NULL;
80
Bram Moolenaar078a4612022-01-04 15:17:03 +000081 if (push_type_stack2(cctx, type == NULL ? &t_any : type,
82 decl_type == NULL ? &t_any : decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000083 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000084
85 return isn;
86}
87
88/*
Bram Moolenaar078a4612022-01-04 15:17:03 +000089 * Generate instruction "isn_type" and put "type" on the type stack.
90 * Uses "any" for the declared type, which works for constants. For declared
91 * variables use generate_instr_type2().
92 */
93 isn_T *
94generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
95{
96 return generate_instr_type2(cctx, isn_type, type, &t_any);
97}
98
99/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000100 * Generate an ISN_DEBUG instruction.
101 */
102 isn_T *
103generate_instr_debug(cctx_T *cctx)
104{
105 isn_T *isn;
106 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
107 + cctx->ctx_ufunc->uf_dfunc_idx;
108
109 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
110 return NULL;
111 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
112 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
113 return isn;
114}
115
116/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000117 * Generate an ISN_CONSTRUCT instruction.
118 * The object will have "size" members.
119 */
120 int
121generate_CONSTRUCT(cctx_T *cctx, class_T *cl)
122{
123 isn_T *isn;
124
125 RETURN_OK_IF_SKIP(cctx);
126 if ((isn = generate_instr(cctx, ISN_CONSTRUCT)) == NULL)
127 return FAIL;
128 isn->isn_arg.construct.construct_size = sizeof(object_T)
129 + cl->class_obj_member_count * sizeof(typval_T);
130 isn->isn_arg.construct.construct_class = cl;
131 return OK;
132}
133
134/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000135 * Generate ISN_GET_OBJ_MEMBER - access member of object at bottom of stack by
136 * index.
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000137 */
138 int
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +0200139generate_GET_OBJ_MEMBER(cctx_T *cctx, int idx, type_T *type)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000140{
141 RETURN_OK_IF_SKIP(cctx);
142
143 // drop the object type
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000144 isn_T *isn = generate_instr_drop(cctx, ISN_GET_OBJ_MEMBER, 1);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000145 if (isn == NULL)
146 return FAIL;
147
Ernie Rael18143d32023-09-04 22:30:41 +0200148 isn->isn_arg.classmember.cm_class = NULL;
149 isn->isn_arg.classmember.cm_idx = idx;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000150 return push_type_stack2(cctx, type, &t_any);
151}
152
153/*
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000154 * Generate ISN_GET_ITF_MEMBER - access member of interface at bottom of stack
155 * by index.
156 */
157 int
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +0200158generate_GET_ITF_MEMBER(cctx_T *cctx, class_T *itf, int idx, type_T *type)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000159{
160 RETURN_OK_IF_SKIP(cctx);
161
162 // drop the object type
163 isn_T *isn = generate_instr_drop(cctx, ISN_GET_ITF_MEMBER, 1);
164 if (isn == NULL)
165 return FAIL;
166
167 isn->isn_arg.classmember.cm_class = itf;
168 ++itf->class_refcount;
169 isn->isn_arg.classmember.cm_idx = idx;
170 return push_type_stack2(cctx, type, &t_any);
171}
172
173/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000174 * Generate ISN_STORE_THIS - store value in member of "this" object with member
175 * index "idx".
176 */
177 int
178generate_STORE_THIS(cctx_T *cctx, int idx)
179{
180 RETURN_OK_IF_SKIP(cctx);
181
182 // drop the value type
183 isn_T *isn = generate_instr_drop(cctx, ISN_STORE_THIS, 1);
184 if (isn == NULL)
185 return FAIL;
186
187 isn->isn_arg.number = idx;
188 return OK;
189}
190
191/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000192 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
193 * But only for simple types.
194 * When "tolerant" is TRUE convert most types to string, e.g. a List.
195 */
196 int
197may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
198{
199 isn_T *isn;
200 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000201 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000202
203 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000204 type = get_type_on_stack(cctx, -1 - offset);
205 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000206 {
207 // nothing to be done
208 case VAR_STRING: return OK;
209
210 // conversion possible
211 case VAR_SPECIAL:
212 case VAR_BOOL:
213 case VAR_NUMBER:
214 case VAR_FLOAT:
215 break;
216
217 // conversion possible (with runtime check)
218 case VAR_ANY:
219 case VAR_UNKNOWN:
220 isntype = ISN_2STRING_ANY;
221 break;
222
223 // conversion possible when tolerant
224 case VAR_LIST:
225 if (tolerant)
226 {
227 isntype = ISN_2STRING_ANY;
228 break;
229 }
230 // FALLTHROUGH
231
232 // conversion not possible
233 case VAR_VOID:
234 case VAR_BLOB:
235 case VAR_FUNC:
236 case VAR_PARTIAL:
237 case VAR_DICT:
238 case VAR_JOB:
239 case VAR_CHANNEL:
240 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000241 case VAR_CLASS:
242 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200243 case VAR_TYPEALIAS:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000244 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000245 return FAIL;
246 }
247
Bram Moolenaar078a4612022-01-04 15:17:03 +0000248 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000249 if ((isn = generate_instr(cctx, isntype)) == NULL)
250 return FAIL;
251 isn->isn_arg.tostring.offset = offset;
252 isn->isn_arg.tostring.tolerant = tolerant;
253
254 return OK;
255}
256
257 static int
258check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
259{
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000260 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
261 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000262 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000263 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000264 {
265 if (*op == '+')
266 emsg(_(e_wrong_argument_type_for_plus));
267 else
268 semsg(_(e_char_requires_number_or_float_arguments), *op);
269 return FAIL;
270 }
271 return OK;
272}
273
274/*
275 * Generate instruction for "+". For a list this creates a new list.
276 */
277 int
278generate_add_instr(
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000279 cctx_T *cctx,
280 vartype_T vartype,
281 type_T *type1,
282 type_T *type2,
283 exprtype_T expr_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000284{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000285 isn_T *isn = generate_instr_drop(cctx,
286 vartype == VAR_NUMBER ? ISN_OPNR
287 : vartype == VAR_LIST ? ISN_ADDLIST
288 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000289 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000290 : ISN_OPANY, 1);
291
292 if (vartype != VAR_LIST && vartype != VAR_BLOB
293 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000294 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000295 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000296 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000297 && check_number_or_float(
298 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
299 return FAIL;
300
301 if (isn != NULL)
302 {
303 if (isn->isn_type == ISN_ADDLIST)
304 isn->isn_arg.op.op_type = expr_type;
305 else
306 isn->isn_arg.op.op_type = EXPR_ADD;
307 }
308
309 // When concatenating two lists with different member types the member type
310 // becomes "any".
311 if (vartype == VAR_LIST
312 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
313 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000314 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000315
316 return isn == NULL ? FAIL : OK;
317}
318
319/*
320 * Get the type to use for an instruction for an operation on "type1" and
321 * "type2". If they are matching use a type-specific instruction. Otherwise
322 * fall back to runtime type checking.
323 */
324 vartype_T
325operator_type(type_T *type1, type_T *type2)
326{
327 if (type1->tt_type == type2->tt_type
328 && (type1->tt_type == VAR_NUMBER
329 || type1->tt_type == VAR_LIST
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000330 || type1->tt_type == VAR_FLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000331 || type1->tt_type == VAR_BLOB))
332 return type1->tt_type;
333 return VAR_ANY;
334}
335
336/*
337 * Generate an instruction with two arguments. The instruction depends on the
338 * type of the arguments.
339 */
340 int
341generate_two_op(cctx_T *cctx, char_u *op)
342{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000343 type_T *type1;
344 type_T *type2;
345 vartype_T vartype;
346 isn_T *isn;
347
348 RETURN_OK_IF_SKIP(cctx);
349
350 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000351 type1 = get_type_on_stack(cctx, 1);
352 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000353 vartype = operator_type(type1, type2);
354
355 switch (*op)
356 {
357 case '+':
358 if (generate_add_instr(cctx, vartype, type1, type2,
359 EXPR_COPY) == FAIL)
360 return FAIL;
361 break;
362
363 case '-':
364 case '*':
365 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
366 op) == FAIL)
367 return FAIL;
368 if (vartype == VAR_NUMBER)
369 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000370 else if (vartype == VAR_FLOAT)
371 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000372 else
373 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
374 if (isn != NULL)
375 isn->isn_arg.op.op_type = *op == '*'
376 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
377 break;
378
379 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000380 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000381 && type1->tt_type != VAR_NUMBER)
382 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000383 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000384 && type2->tt_type != VAR_NUMBER))
385 {
386 emsg(_(e_percent_requires_number_arguments));
387 return FAIL;
388 }
389 isn = generate_instr_drop(cctx,
390 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
391 if (isn != NULL)
392 isn->isn_arg.op.op_type = EXPR_REM;
393 break;
394 }
395
396 // correct type of result
397 if (vartype == VAR_ANY)
398 {
399 type_T *type = &t_any;
400
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000401 // float+number and number+float results in float
402 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100403 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000404 type = &t_float;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000405 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000406 }
407
408 return OK;
409}
410
411/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000412 * Get the instruction to use for comparing two values with specified types.
413 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000414 * Return ISN_DROP when failed.
415 */
416 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000417get_compare_isn(
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100418 exprtype_T exprtype,
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000419 typval_T *tv1,
420 typval_T *tv2,
421 type_T *type1,
422 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000423{
424 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000425 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
426 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000427
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000428 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000429 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000430 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000431 {
432 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
433 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
434 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
435 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
436 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
437 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
438 case VAR_LIST: isntype = ISN_COMPARELIST; break;
439 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
440 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000441 case VAR_CLASS: isntype = ISN_COMPARECLASS; break;
442 case VAR_OBJECT: isntype = ISN_COMPAREOBJECT; break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000443 default: isntype = ISN_COMPAREANY; break;
444 }
445 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000446 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
447 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
448 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
449 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
450 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000451 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000452 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000453 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000454 if ((vartype1 == VAR_SPECIAL
455 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
456 : type1 == &t_none)
457 && vartype2 != VAR_STRING)
458 || (vartype2 == VAR_SPECIAL
459 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
460 : type2 == &t_none)
461 && vartype1 != VAR_STRING))
462 {
463 semsg(_(e_cannot_compare_str_with_str),
464 vartype_name(vartype1), vartype_name(vartype2));
465 return ISN_DROP;
466 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000467 // although comparing null with number, float or bool is not useful, we
468 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000469 isntype = ISN_COMPARENULL;
470 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000471
472 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
473 && (isntype == ISN_COMPAREBOOL
474 || isntype == ISN_COMPARESPECIAL
475 || isntype == ISN_COMPARENR
476 || isntype == ISN_COMPAREFLOAT))
477 {
478 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000479 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000480 return ISN_DROP;
481 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000482 if (!(exprtype == EXPR_IS || exprtype == EXPR_ISNOT
483 || exprtype == EXPR_EQUAL || exprtype == EXPR_NEQUAL)
484 && (isntype == ISN_COMPAREOBJECT || isntype == ISN_COMPARECLASS))
485 {
486 semsg(_(e_invalid_operation_for_str), vartype_name(vartype1));
487 return ISN_DROP;
488 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000489 if (isntype == ISN_DROP
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100490 || (isntype != ISN_COMPARENULL
491 && (((exprtype != EXPR_EQUAL
492 && exprtype != EXPR_NEQUAL
493 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
494 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
495 || ((exprtype != EXPR_EQUAL
496 && exprtype != EXPR_NEQUAL
497 && exprtype != EXPR_IS
498 && exprtype != EXPR_ISNOT
499 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
500 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000501 {
502 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000503 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000504 return ISN_DROP;
505 }
506 return isntype;
507}
508
509 int
510check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
511{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000512 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000513 return FAIL;
514 return OK;
515}
516
517/*
518 * Generate an ISN_COMPARE* instruction with a boolean result.
519 */
520 int
521generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
522{
523 isntype_T isntype;
524 isn_T *isn;
525 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000526
527 RETURN_OK_IF_SKIP(cctx);
528
529 // Get the known type of the two items on the stack. If they are matching
530 // use a type-specific instruction. Otherwise fall back to runtime type
531 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000532 isntype = get_compare_isn(exprtype, NULL, NULL,
533 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000534 if (isntype == ISN_DROP)
535 return FAIL;
536
537 if ((isn = generate_instr(cctx, isntype)) == NULL)
538 return FAIL;
539 isn->isn_arg.op.op_type = exprtype;
540 isn->isn_arg.op.op_ic = ic;
541
542 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100543 --stack->ga_len;
544 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000545
546 return OK;
547}
548
549/*
LemonBoy372bcce2022-04-25 12:43:20 +0100550 * Generate an ISN_CONCAT instruction.
551 * "count" is the number of stack elements to join together and it must be
552 * greater or equal to one.
553 * The caller ensures all the "count" elements on the stack have the right type.
554 */
555 int
556generate_CONCAT(cctx_T *cctx, int count)
557{
558 isn_T *isn;
559 garray_T *stack = &cctx->ctx_type_stack;
560
561 RETURN_OK_IF_SKIP(cctx);
562
LemonBoy372bcce2022-04-25 12:43:20 +0100563 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
564 return FAIL;
565 isn->isn_arg.number = count;
566
567 // drop the argument types
568 stack->ga_len -= count - 1;
569
570 return OK;
571}
572
573/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000574 * Generate an ISN_2BOOL instruction.
575 * "offset" is the offset in the type stack.
576 */
577 int
578generate_2BOOL(cctx_T *cctx, int invert, int offset)
579{
580 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000581
582 RETURN_OK_IF_SKIP(cctx);
583 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
584 return FAIL;
585 isn->isn_arg.tobool.invert = invert;
586 isn->isn_arg.tobool.offset = offset;
587
588 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000589 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000590
591 return OK;
592}
593
594/*
595 * Generate an ISN_COND2BOOL instruction.
596 */
597 int
598generate_COND2BOOL(cctx_T *cctx)
599{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000600 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100601 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000602 return FAIL;
603
604 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000605 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000606
607 return OK;
608}
609
610 int
611generate_TYPECHECK(
612 cctx_T *cctx,
613 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000614 int number_ok, // add TTFLAG_NUMBER_OK flag
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000615 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100616 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000617 int argidx)
618{
619 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000620
621 RETURN_OK_IF_SKIP(cctx);
622 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
623 return FAIL;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000624 type_T *tt;
625 if (expected->tt_type == VAR_FLOAT && number_ok)
626 {
627 // always allocate, also for static types
628 tt = ALLOC_ONE(type_T);
629 if (tt != NULL)
630 {
631 *tt = *expected;
Bram Moolenaarc4b3f642022-12-30 10:36:34 +0000632 tt->tt_flags &= ~TTFLAG_STATIC;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000633 tt->tt_flags |= TTFLAG_NUMBER_OK;
634 }
635 }
636 else
637 tt = alloc_type(expected);
638
639 isn->isn_arg.type.ct_type = tt;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000640 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100641 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000642 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
643
644 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000645 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000646
647 return OK;
648}
649
650 int
651generate_SETTYPE(
652 cctx_T *cctx,
653 type_T *expected)
654{
655 isn_T *isn;
656
657 RETURN_OK_IF_SKIP(cctx);
658 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
659 return FAIL;
660 isn->isn_arg.type.ct_type = alloc_type(expected);
661 return OK;
662}
663
664/*
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000665 * Generate an ISN_PUSHOBJ instruction. Object is always NULL.
666 */
Ernie Rael5c018be2023-08-27 18:40:26 +0200667 int
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000668generate_PUSHOBJ(cctx_T *cctx)
669{
670 RETURN_OK_IF_SKIP(cctx);
Ernie Rael5c018be2023-08-27 18:40:26 +0200671 if (generate_instr_type(cctx, ISN_PUSHOBJ, &t_object) == NULL)
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000672 return FAIL;
673 return OK;
674}
675
676/*
677 * Generate an ISN_PUSHCLASS instruction. "class" can be NULL.
678 */
679 static int
680generate_PUSHCLASS(cctx_T *cctx, class_T *class)
681{
682 RETURN_OK_IF_SKIP(cctx);
683 isn_T *isn = generate_instr_type(cctx, ISN_PUSHCLASS,
684 class == NULL ? &t_any : &class->class_type);
685 if (isn == NULL)
686 return FAIL;
Bram Moolenaar30a84472023-02-27 08:07:14 +0000687 isn->isn_arg.classarg = class;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000688 if (class != NULL)
689 ++class->class_refcount;
690 return OK;
691}
692
693/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000694 * Generate a PUSH instruction for "tv".
695 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000696 */
697 int
698generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
699{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100700 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000701 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100702 case VAR_BOOL:
703 generate_PUSHBOOL(cctx, tv->vval.v_number);
704 break;
705 case VAR_SPECIAL:
706 generate_PUSHSPEC(cctx, tv->vval.v_number);
707 break;
708 case VAR_NUMBER:
709 generate_PUSHNR(cctx, tv->vval.v_number);
710 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100711 case VAR_FLOAT:
712 generate_PUSHF(cctx, tv->vval.v_float);
713 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100714 case VAR_BLOB:
715 generate_PUSHBLOB(cctx, tv->vval.v_blob);
716 tv->vval.v_blob = NULL;
717 break;
718 case VAR_LIST:
719 if (tv->vval.v_list != NULL)
720 iemsg("non-empty list constant not supported");
721 generate_NEWLIST(cctx, 0, TRUE);
722 break;
723 case VAR_DICT:
724 if (tv->vval.v_dict != NULL)
725 iemsg("non-empty dict constant not supported");
726 generate_NEWDICT(cctx, 0, TRUE);
727 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000728#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100729 case VAR_JOB:
730 if (tv->vval.v_job != NULL)
731 iemsg("non-null job constant not supported");
732 generate_PUSHJOB(cctx);
733 break;
734 case VAR_CHANNEL:
735 if (tv->vval.v_channel != NULL)
736 iemsg("non-null channel constant not supported");
737 generate_PUSHCHANNEL(cctx);
738 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000739#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100740 case VAR_FUNC:
741 if (tv->vval.v_string != NULL)
742 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100743 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100744 break;
745 case VAR_PARTIAL:
746 if (tv->vval.v_partial != NULL)
747 iemsg("non-null partial constant not supported");
748 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
749 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000750 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100751 break;
752 case VAR_STRING:
753 generate_PUSHS(cctx, &tv->vval.v_string);
754 tv->vval.v_string = NULL;
755 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000756 case VAR_OBJECT:
757 if (tv->vval.v_object != NULL)
758 {
759 emsg(_(e_cannot_use_non_null_object));
760 return FAIL;
761 }
762 generate_PUSHOBJ(cctx);
763 break;
764 case VAR_CLASS:
765 generate_PUSHCLASS(cctx, tv->vval.v_class);
766 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100767 default:
768 siemsg("constant type %d not supported", tv->v_type);
769 clear_tv(tv);
770 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000771 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100772 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000773 return OK;
774}
775
776/*
777 * Generate an ISN_PUSHNR instruction.
778 */
779 int
780generate_PUSHNR(cctx_T *cctx, varnumber_T number)
781{
782 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000783
784 RETURN_OK_IF_SKIP(cctx);
785 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
786 return FAIL;
787 isn->isn_arg.number = number;
788
789 if (number == 0 || number == 1)
790 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000791 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000792 return OK;
793}
794
795/*
796 * Generate an ISN_PUSHBOOL instruction.
797 */
798 int
799generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
800{
801 isn_T *isn;
802
803 RETURN_OK_IF_SKIP(cctx);
804 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
805 return FAIL;
806 isn->isn_arg.number = number;
807
808 return OK;
809}
810
811/*
812 * Generate an ISN_PUSHSPEC instruction.
813 */
814 int
815generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
816{
817 isn_T *isn;
818
819 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000820 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
821 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000822 return FAIL;
823 isn->isn_arg.number = number;
824
825 return OK;
826}
827
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000828/*
829 * Generate an ISN_PUSHF instruction.
830 */
831 int
832generate_PUSHF(cctx_T *cctx, float_T fnumber)
833{
834 isn_T *isn;
835
836 RETURN_OK_IF_SKIP(cctx);
837 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
838 return FAIL;
839 isn->isn_arg.fnumber = fnumber;
840
841 return OK;
842}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000843
844/*
845 * Generate an ISN_PUSHS instruction.
846 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100847 * Note that if "str" is used in the instruction OK is returned and "*str" is
848 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000849 */
850 int
851generate_PUSHS(cctx_T *cctx, char_u **str)
852{
853 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100854 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000855
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100856 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000857 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100858 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
859 ret = FAIL;
860 else
861 {
862 isn->isn_arg.string = str == NULL ? NULL : *str;
863 return OK;
864 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000865 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100866 if (str != NULL)
867 VIM_CLEAR(*str);
868 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000869}
870
871/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000872 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000873 */
874 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000875generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000876{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000877 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100878#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100879 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000880 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000881 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100882#else
883 emsg(_(e_channel_job_feature_not_available));
884 return FAIL;
885#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000886}
887
888/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000889 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000890 */
891 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000892generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000893{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000894 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100895#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100896 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000897 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000898 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100899#else
900 emsg(_(e_channel_job_feature_not_available));
901 return FAIL;
902#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000903}
904
905/*
906 * Generate an ISN_PUSHBLOB instruction.
907 * Consumes "blob".
908 */
909 int
910generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
911{
912 isn_T *isn;
913
914 RETURN_OK_IF_SKIP(cctx);
915 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
916 return FAIL;
917 isn->isn_arg.blob = blob;
918
919 return OK;
920}
921
922/*
923 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100924 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
925 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000926 */
927 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100928generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000929{
930 isn_T *isn;
931 char_u *funcname;
932
933 RETURN_OK_IF_SKIP(cctx);
934 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
935 return FAIL;
936 if (name == NULL)
937 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100938 else if (!may_prefix
939 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000940 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000941 funcname = vim_strsave(name);
942 else
943 {
944 funcname = alloc(STRLEN(name) + 3);
945 if (funcname != NULL)
946 {
947 STRCPY(funcname, "g:");
948 STRCPY(funcname + 2, name);
949 }
950 }
951
952 isn->isn_arg.string = funcname;
953 return OK;
954}
955
956/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000957 * Generate an ISN_AUTOLOAD instruction.
958 */
959 int
960generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
961{
962 isn_T *isn;
963
964 RETURN_OK_IF_SKIP(cctx);
965 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
966 return FAIL;
967 isn->isn_arg.string = vim_strsave(name);
968 if (isn->isn_arg.string == NULL)
969 return FAIL;
970 return OK;
971}
972
973/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000974 * Generate an ISN_GETITEM instruction with "index".
975 * "with_op" is TRUE for "+=" and other operators, the stack has the current
976 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100977 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000978 */
979 int
980generate_GETITEM(cctx_T *cctx, int index, int with_op)
981{
982 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000983 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000984 type_T *item_type = &t_any;
985
986 RETURN_OK_IF_SKIP(cctx);
987
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000988 item_type = type->tt_member;
989 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
990 return FAIL;
991 isn->isn_arg.getitem.gi_index = index;
992 isn->isn_arg.getitem.gi_with_op = with_op;
993
994 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000995 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000996}
997
998/*
999 * Generate an ISN_SLICE instruction with "count".
1000 */
1001 int
1002generate_SLICE(cctx_T *cctx, int count)
1003{
1004 isn_T *isn;
1005
1006 RETURN_OK_IF_SKIP(cctx);
1007 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1008 return FAIL;
1009 isn->isn_arg.number = count;
1010 return OK;
1011}
1012
1013/*
1014 * Generate an ISN_CHECKLEN instruction with "min_len".
1015 */
1016 int
1017generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1018{
1019 isn_T *isn;
1020
1021 RETURN_OK_IF_SKIP(cctx);
1022
1023 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1024 return FAIL;
1025 isn->isn_arg.checklen.cl_min_len = min_len;
1026 isn->isn_arg.checklen.cl_more_OK = more_OK;
1027
1028 return OK;
1029}
1030
1031/*
1032 * Generate an ISN_STORE instruction.
1033 */
1034 int
1035generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1036{
1037 isn_T *isn;
1038
1039 RETURN_OK_IF_SKIP(cctx);
1040 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1041 return FAIL;
1042 if (name != NULL)
1043 isn->isn_arg.string = vim_strsave(name);
1044 else
1045 isn->isn_arg.number = idx;
1046
1047 return OK;
1048}
1049
1050/*
Bram Moolenaard505d172022-12-18 21:42:55 +00001051 * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER
1052 * ("load" == FALSE) instruction.
1053 */
1054 int
1055generate_CLASSMEMBER(
1056 cctx_T *cctx,
1057 int load,
1058 class_T *cl,
1059 int idx)
1060{
1061 isn_T *isn;
1062
1063 RETURN_OK_IF_SKIP(cctx);
1064 if (load)
1065 {
1066 ocmember_T *m = &cl->class_class_members[idx];
1067 isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type);
1068 }
1069 else
1070 {
1071 isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1);
1072 }
1073 if (isn == NULL)
1074 return FAIL;
1075 isn->isn_arg.classmember.cm_class = cl;
1076 ++cl->class_refcount;
1077 isn->isn_arg.classmember.cm_idx = idx;
1078
1079 return OK;
1080}
1081
1082/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001083 * Generate an ISN_STOREOUTER instruction.
1084 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001085 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001086generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001087{
1088 isn_T *isn;
1089
1090 RETURN_OK_IF_SKIP(cctx);
1091 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1092 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001093 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
1094 {
1095 // Store a variable defined in a loop. A copy will be made at the end
1096 // of the loop. TODO: how about deeper nesting?
1097 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1098 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1099 }
1100 else
1101 {
1102 isn->isn_arg.outer.outer_idx = idx;
1103 isn->isn_arg.outer.outer_depth = level;
1104 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001105
1106 return OK;
1107}
1108
1109/*
1110 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1111 */
1112 int
1113generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1114{
1115 isn_T *isn;
1116
1117 RETURN_OK_IF_SKIP(cctx);
1118 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1119 return FAIL;
1120 isn->isn_arg.storenr.stnr_idx = idx;
1121 isn->isn_arg.storenr.stnr_val = value;
1122
1123 return OK;
1124}
1125
1126/*
1127 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1128 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001129 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001130generate_STOREOPT(
1131 cctx_T *cctx,
1132 isntype_T isn_type,
1133 char_u *name,
1134 int opt_flags)
1135{
1136 isn_T *isn;
1137
1138 RETURN_OK_IF_SKIP(cctx);
1139 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1140 return FAIL;
1141 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1142 isn->isn_arg.storeopt.so_flags = opt_flags;
1143
1144 return OK;
1145}
1146
1147/*
1148 * Generate an ISN_LOAD or similar instruction.
1149 */
1150 int
1151generate_LOAD(
1152 cctx_T *cctx,
1153 isntype_T isn_type,
1154 int idx,
1155 char_u *name,
1156 type_T *type)
1157{
1158 isn_T *isn;
1159
1160 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001161 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001162 return FAIL;
1163 if (name != NULL)
1164 isn->isn_arg.string = vim_strsave(name);
1165 else
1166 isn->isn_arg.number = idx;
1167
1168 return OK;
1169}
1170
1171/*
1172 * Generate an ISN_LOADOUTER instruction
1173 */
1174 int
1175generate_LOADOUTER(
1176 cctx_T *cctx,
1177 int idx,
1178 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001179 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001180 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001181 type_T *type)
1182{
1183 isn_T *isn;
1184
1185 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001186 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001187 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001188 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1189 {
1190 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001191 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001192 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001193 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001194 }
1195 else
1196 {
1197 isn->isn_arg.outer.outer_idx = idx;
1198 isn->isn_arg.outer.outer_depth = nesting;
1199 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001200
1201 return OK;
1202}
1203
1204/*
1205 * Generate an ISN_LOADV instruction for v:var.
1206 */
1207 int
1208generate_LOADV(
1209 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001210 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001211{
1212 int di_flags;
1213 int vidx = find_vim_var(name, &di_flags);
1214 type_T *type;
1215
1216 RETURN_OK_IF_SKIP(cctx);
1217 if (vidx < 0)
1218 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001219 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001220 return FAIL;
1221 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001222 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001223 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1224}
1225
1226/*
1227 * Generate an ISN_UNLET instruction.
1228 */
1229 int
1230generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1231{
1232 isn_T *isn;
1233
1234 RETURN_OK_IF_SKIP(cctx);
1235 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1236 return FAIL;
1237 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1238 isn->isn_arg.unlet.ul_forceit = forceit;
1239
1240 return OK;
1241}
1242
1243/*
1244 * Generate an ISN_LOCKCONST instruction.
1245 */
1246 int
1247generate_LOCKCONST(cctx_T *cctx)
1248{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001249 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001250 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001251 return FAIL;
1252 return OK;
1253}
1254
1255/*
1256 * Generate an ISN_LOADS instruction.
1257 */
1258 int
1259generate_OLDSCRIPT(
1260 cctx_T *cctx,
1261 isntype_T isn_type,
1262 char_u *name,
1263 int sid,
1264 type_T *type)
1265{
1266 isn_T *isn;
1267
1268 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001269 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001270 isn = generate_instr_type(cctx, isn_type, type);
1271 else
1272 isn = generate_instr_drop(cctx, isn_type, 1);
1273 if (isn == NULL)
1274 return FAIL;
1275 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1276 isn->isn_arg.loadstore.ls_sid = sid;
1277
1278 return OK;
1279}
1280
1281/*
1282 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1283 */
1284 int
1285generate_VIM9SCRIPT(
1286 cctx_T *cctx,
1287 isntype_T isn_type,
1288 int sid,
1289 int idx,
1290 type_T *type)
1291{
1292 isn_T *isn;
1293 scriptref_T *sref;
1294 scriptitem_T *si = SCRIPT_ITEM(sid);
1295
1296 RETURN_OK_IF_SKIP(cctx);
1297 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001298 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001299 else
1300 isn = generate_instr_drop(cctx, isn_type, 1);
1301 if (isn == NULL)
1302 return FAIL;
1303
1304 // This requires three arguments, which doesn't fit in an instruction, thus
1305 // we need to allocate a struct for this.
1306 sref = ALLOC_ONE(scriptref_T);
1307 if (sref == NULL)
1308 return FAIL;
1309 isn->isn_arg.script.scriptref = sref;
1310 sref->sref_sid = sid;
1311 sref->sref_idx = idx;
1312 sref->sref_seq = si->sn_script_seq;
1313 sref->sref_type = type;
1314 return OK;
1315}
1316
1317/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001318 * Generate an ISN_NEWLIST instruction for "count" items.
1319 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001320 */
1321 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001322generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001323{
1324 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001325 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001326 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001327 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001328
1329 RETURN_OK_IF_SKIP(cctx);
1330 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1331 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001332 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001333
Bram Moolenaar078a4612022-01-04 15:17:03 +00001334 // Get the member type and the declared member type from all the items on
1335 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001336 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001337 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001338 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001339
1340 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001341 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001342
1343 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001344 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001345}
1346
1347/*
1348 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001349 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001350 */
1351 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001352generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001353{
1354 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001355 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001356 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001357 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001358
1359 RETURN_OK_IF_SKIP(cctx);
1360 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1361 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001362 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001363
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001364 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001365 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001366 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001367
1368 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001369 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001370
1371 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001372 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001373}
1374
1375/*
1376 * Generate an ISN_FUNCREF instruction.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001377 * For "obj.Method" "cl" is the class of the object (can be an interface or a
1378 * base class) and "fi" the index of the method on that class.
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001379 * "isn_idx" is set to the index of the instruction, so that fr_dfunc_idx can
1380 * be set later. The index is used instead of a pointer to the instruction
1381 * because the instruction memory can be reallocated.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001382 */
1383 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001384generate_FUNCREF(
1385 cctx_T *cctx,
1386 ufunc_T *ufunc,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001387 class_T *cl,
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001388 int object_method,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001389 int fi,
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001390 int *isn_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001391{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001392 isn_T *isn;
1393 type_T *type;
1394 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001395 loopvarinfo_T loopinfo;
1396 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001397
1398 RETURN_OK_IF_SKIP(cctx);
1399 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1400 return FAIL;
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001401 if (isn_idx != NULL)
1402 // save the index of the new instruction
1403 *isn_idx = cctx->ctx_instr.ga_len - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001404
Bram Moolenaarcc341812022-09-19 15:54:34 +01001405 has_vars = get_loop_var_info(cctx, &loopinfo);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001406 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001407 {
1408 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1409 if (extra == NULL)
1410 return FAIL;
1411 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001412 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001413 if (cl != NULL)
1414 {
1415 extra->fre_class = cl;
1416 ++cl->class_refcount;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001417 extra->fre_object_method = object_method;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001418 extra->fre_method_idx = fi;
1419 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001420 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00001421 if (ufunc->uf_def_status == UF_NOT_COMPILED || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001422 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001423 if (ufunc->uf_def_status != UF_NOT_COMPILED && cl == NULL)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001424 {
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001425 if (isn_idx == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001426 // compile the function now, we need the uf_dfunc_idx value
1427 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001428 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001429 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001430
1431 // Reserve an extra variable to keep track of the number of closures
1432 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001433 cctx->ctx_has_closure = 1;
1434
1435 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001436 // the nested context, including this one. But not a function defined at
1437 // the script level.
1438 if ((ufunc->uf_flags & FC_CLOSURE)
1439 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001440 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1441
Bram Moolenaar078a4612022-01-04 15:17:03 +00001442 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1443 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001444}
1445
1446/*
1447 * Generate an ISN_NEWFUNC instruction.
1448 * "lambda_name" and "func_name" must be in allocated memory and will be
1449 * consumed.
1450 */
1451 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001452generate_NEWFUNC(
1453 cctx_T *cctx,
1454 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001455 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001456{
1457 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001458 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001459
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001460 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001461 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001462 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1463 ret = FAIL;
1464 else
1465 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001466 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1467
1468 if (arg == NULL)
1469 ret = FAIL;
1470 else
1471 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001472 // Reserve an extra variable to keep track of the number of
1473 // closures created.
1474 cctx->ctx_has_closure = 1;
1475
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001476 isn->isn_arg.newfunc.nf_arg = arg;
1477 arg->nfa_lambda = lambda_name;
1478 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001479 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001480 return OK;
1481 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001482 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001483 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001484 vim_free(lambda_name);
1485 vim_free(func_name);
1486 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001487}
1488
1489/*
1490 * Generate an ISN_DEF instruction: list functions
1491 */
1492 int
1493generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1494{
1495 isn_T *isn;
1496
1497 RETURN_OK_IF_SKIP(cctx);
1498 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1499 return FAIL;
1500 if (len > 0)
1501 {
1502 isn->isn_arg.string = vim_strnsave(name, len);
1503 if (isn->isn_arg.string == NULL)
1504 return FAIL;
1505 }
1506 return OK;
1507}
1508
1509/*
1510 * Generate an ISN_JUMP instruction.
1511 */
1512 int
1513generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1514{
1515 isn_T *isn;
1516 garray_T *stack = &cctx->ctx_type_stack;
1517
1518 RETURN_OK_IF_SKIP(cctx);
1519 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1520 return FAIL;
1521 isn->isn_arg.jump.jump_when = when;
1522 isn->isn_arg.jump.jump_where = where;
1523
1524 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1525 --stack->ga_len;
1526
1527 return OK;
1528}
1529
1530/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001531 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1532 */
1533 int
1534generate_WHILE(cctx_T *cctx, int funcref_idx)
1535{
1536 isn_T *isn;
1537 garray_T *stack = &cctx->ctx_type_stack;
1538
1539 RETURN_OK_IF_SKIP(cctx);
1540 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1541 return FAIL;
1542 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1543 isn->isn_arg.whileloop.while_end = 0; // filled in later
1544
1545 if (stack->ga_len > 0)
1546 --stack->ga_len;
1547
1548 return OK;
1549}
1550
1551/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001552 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001553 */
1554 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001555generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001556{
1557 isn_T *isn;
1558
1559 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001560 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001561 return FAIL;
1562 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1563 // jump_where is set later
1564 return OK;
1565}
1566
1567 int
1568generate_FOR(cctx_T *cctx, int loop_idx)
1569{
1570 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001571
1572 RETURN_OK_IF_SKIP(cctx);
1573 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1574 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001575 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001576
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001577 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001578 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001579}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001580
1581 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001582generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001583{
1584 isn_T *isn;
1585
1586 RETURN_OK_IF_SKIP(cctx);
1587 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1588 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001589 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1590 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1591 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001592 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001593 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001594 return OK;
1595}
1596
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001597/*
1598 * Generate an ISN_TRYCONT instruction.
1599 */
1600 int
1601generate_TRYCONT(cctx_T *cctx, int levels, int where)
1602{
1603 isn_T *isn;
1604
1605 RETURN_OK_IF_SKIP(cctx);
1606 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1607 return FAIL;
1608 isn->isn_arg.trycont.tct_levels = levels;
1609 isn->isn_arg.trycont.tct_where = where;
1610
1611 return OK;
1612}
1613
Bram Moolenaar16900322022-09-08 19:51:45 +01001614/*
1615 * Check "argount" arguments and their types on the type stack.
1616 * Give an error and return FAIL if something is wrong.
1617 * When "method_call" is NULL no code is generated.
1618 */
1619 int
1620check_internal_func_args(
1621 cctx_T *cctx,
1622 int func_idx,
1623 int argcount,
1624 int method_call,
1625 type2_T **argtypes,
1626 type2_T *shuffled_argtypes)
1627{
1628 garray_T *stack = &cctx->ctx_type_stack;
1629 int argoff = check_internal_func(func_idx, argcount);
1630
1631 if (argoff < 0)
1632 return FAIL;
1633
1634 if (method_call && argoff > 1)
1635 {
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001636 if (argcount < argoff)
1637 {
1638 semsg(_(e_not_enough_arguments_for_function_str),
1639 internal_func_name(func_idx));
1640 return FAIL;
1641 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001642
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001643 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
Bram Moolenaar16900322022-09-08 19:51:45 +01001644 if (isn == NULL)
1645 return FAIL;
1646 isn->isn_arg.shuffle.shfl_item = argcount;
1647 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1648 }
1649
1650 if (argcount > 0)
1651 {
1652 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1653
1654 // Check the types of the arguments.
1655 if (method_call && argoff > 1)
1656 {
1657 int i;
1658
1659 for (i = 0; i < argcount; ++i)
1660 shuffled_argtypes[i] = (i < argoff - 1)
1661 ? typep[i + 1]
1662 : (i == argoff - 1) ? typep[0] : typep[i];
1663 *argtypes = shuffled_argtypes;
1664 }
1665 else
1666 {
1667 int i;
1668
1669 for (i = 0; i < argcount; ++i)
1670 shuffled_argtypes[i] = typep[i];
1671 *argtypes = shuffled_argtypes;
1672 }
1673 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1674 cctx) == FAIL)
1675 return FAIL;
1676 }
1677 return OK;
1678}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001679
1680/*
1681 * Generate an ISN_BCALL instruction.
1682 * "method_call" is TRUE for "value->method()"
1683 * Return FAIL if the number of arguments is wrong.
1684 */
1685 int
1686generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1687{
1688 isn_T *isn;
1689 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001690 type2_T *argtypes = NULL;
1691 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1692 type2_T *maptype = NULL;
1693 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001694 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001695
1696 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001697
1698 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1699 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001700 return FAIL;
1701
Bram Moolenaar16900322022-09-08 19:51:45 +01001702 if (internal_func_is_map(func_idx))
1703 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001704
1705 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1706 return FAIL;
1707 isn->isn_arg.bfunc.cbf_idx = func_idx;
1708 isn->isn_arg.bfunc.cbf_argcount = argcount;
1709
1710 // Drop the argument types and push the return type.
1711 stack->ga_len -= argcount;
Bram Moolenaar32517c42023-01-15 18:17:12 +00001712 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type,
1713 cctx->ctx_type_list);
Bram Moolenaar81330182022-02-01 12:11:58 +00001714 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001715 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001716
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001717 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1718 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001719 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001720 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001721
1722 return OK;
1723}
1724
1725/*
1726 * Generate an ISN_LISTAPPEND instruction. Works like add().
1727 * Argument count is already checked.
1728 */
1729 int
1730generate_LISTAPPEND(cctx_T *cctx)
1731{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001732 type_T *list_type;
1733 type_T *item_type;
1734 type_T *expected;
1735
1736 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001737 // For checking the item type we use the declared type of the list and the
1738 // current type of the added item, adding a string to [1, 2] is OK.
1739 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001740 if (arg_type_modifiable(list_type, 1) == FAIL)
1741 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001742 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001743 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001744 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001745 return FAIL;
1746
1747 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1748 return FAIL;
1749
Bram Moolenaar078a4612022-01-04 15:17:03 +00001750 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001751 return OK;
1752}
1753
1754/*
1755 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1756 * Argument count is already checked.
1757 */
1758 int
1759generate_BLOBAPPEND(cctx_T *cctx)
1760{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001761 type_T *item_type;
1762
Bram Moolenaarfa103972022-09-29 19:14:42 +01001763 // Caller already checked that blob_type is a blob, check it is modifiable.
1764 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1765 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001766 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001767 if (need_type(item_type, &t_number, FALSE,
1768 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001769 return FAIL;
1770
1771 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1772 return FAIL;
1773
Bram Moolenaar078a4612022-01-04 15:17:03 +00001774 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001775 return OK;
1776}
1777
1778/*
Bram Moolenaard0200c82023-01-28 15:19:40 +00001779 * Generate an ISN_DCALL, ISN_UCALL or ISN_METHODCALL instruction.
1780 * When calling a method on an object, of which we know the interface only,
1781 * then "cl" is the interface and "mi" the method index on the interface.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001782 * Return FAIL if the number of arguments is wrong.
1783 */
1784 int
Bram Moolenaard0200c82023-01-28 15:19:40 +00001785generate_CALL(
1786 cctx_T *cctx,
1787 ufunc_T *ufunc,
1788 class_T *cl,
1789 int mi,
1790 int pushed_argcount)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001791{
1792 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001793 int regular_args = ufunc->uf_args.ga_len;
1794 int argcount = pushed_argcount;
1795
1796 RETURN_OK_IF_SKIP(cctx);
1797 if (argcount > regular_args && !has_varargs(ufunc))
1798 {
1799 semsg(_(e_too_many_arguments_for_function_str),
1800 printable_func_name(ufunc));
1801 return FAIL;
1802 }
1803 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1804 {
1805 semsg(_(e_not_enough_arguments_for_function_str),
1806 printable_func_name(ufunc));
1807 return FAIL;
1808 }
1809
1810 if (ufunc->uf_def_status != UF_NOT_COMPILED
1811 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1812 {
1813 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001814 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001815
1816 for (i = 0; i < argcount; ++i)
1817 {
1818 type_T *expected;
1819 type_T *actual;
1820
Bram Moolenaar078a4612022-01-04 15:17:03 +00001821 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001822 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001823 && i >= regular_args - ufunc->uf_def_args.ga_len)
1824 {
1825 // assume v:none used for default argument value
1826 continue;
1827 }
1828 if (i < regular_args)
1829 {
1830 if (ufunc->uf_arg_types == NULL)
1831 continue;
1832 expected = ufunc->uf_arg_types[i];
1833 }
1834 else if (ufunc->uf_va_type == NULL
1835 || ufunc->uf_va_type == &t_list_any)
1836 // possibly a lambda or "...: any"
1837 expected = &t_any;
1838 else
1839 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001840 if (need_type(actual, expected, FALSE,
1841 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001842 {
1843 arg_type_mismatch(expected, actual, i + 1);
1844 return FAIL;
1845 }
1846 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001847 compile_type = get_compile_type(ufunc);
1848 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001849 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001850 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001851 return FAIL;
1852 }
1853 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1854 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001855 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001856 ufunc->uf_name);
1857 return FAIL;
1858 }
1859
Bram Moolenaard0200c82023-01-28 15:19:40 +00001860 if ((isn = generate_instr(cctx, cl != NULL ? ISN_METHODCALL
1861 : ufunc->uf_def_status != UF_NOT_COMPILED
1862 ? ISN_DCALL : ISN_UCALL)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001863 return FAIL;
Bram Moolenaar836137d2023-01-29 14:11:24 +00001864 if (cl != NULL /* isn->isn_type == ISN_METHODCALL */)
Bram Moolenaard0200c82023-01-28 15:19:40 +00001865 {
1866 isn->isn_arg.mfunc = ALLOC_ONE(cmfunc_T);
1867 if (isn->isn_arg.mfunc == NULL)
1868 return FAIL;
1869 isn->isn_arg.mfunc->cmf_itf = cl;
1870 ++cl->class_refcount;
1871 isn->isn_arg.mfunc->cmf_idx = mi;
1872 isn->isn_arg.mfunc->cmf_argcount = argcount;
1873 }
1874 else if (isn->isn_type == ISN_DCALL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001875 {
1876 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1877 isn->isn_arg.dfunc.cdf_argcount = argcount;
1878 }
1879 else
1880 {
1881 // A user function may be deleted and redefined later, can't use the
1882 // ufunc pointer, need to look it up again at runtime.
1883 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1884 isn->isn_arg.ufunc.cuf_argcount = argcount;
1885 }
1886
Bram Moolenaar078a4612022-01-04 15:17:03 +00001887 // drop the argument types
1888 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001889
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001890 // For an object or class method call, drop the object/class type.
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02001891 if (ufunc->uf_class != NULL)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001892 {
1893 // When a class method is called without the class name prefix, then
1894 // the type will not be in the stack.
1895 type_T *stype = get_type_on_stack(cctx, 0);
1896 if (stype->tt_type == VAR_CLASS || stype->tt_type == VAR_OBJECT)
1897 cctx->ctx_type_stack.ga_len--;
1898 }
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02001899
Bram Moolenaar078a4612022-01-04 15:17:03 +00001900 // add return type
1901 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001902}
1903
1904/*
1905 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1906 */
1907 int
1908generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1909{
1910 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001911
1912 RETURN_OK_IF_SKIP(cctx);
1913 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1914 return FAIL;
1915 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1916 isn->isn_arg.ufunc.cuf_argcount = argcount;
1917
Bram Moolenaar078a4612022-01-04 15:17:03 +00001918 // drop the argument types
1919 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001920
Bram Moolenaar078a4612022-01-04 15:17:03 +00001921 // add return value
1922 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001923}
1924
1925/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001926 * Check the arguments of function "type" against the types on the stack.
1927 * Returns OK or FAIL;
1928 */
1929 int
1930check_func_args_from_type(
1931 cctx_T *cctx,
1932 type_T *type,
1933 int argcount,
1934 int at_top,
1935 char_u *name)
1936{
1937 if (type->tt_argcount != -1)
1938 {
1939 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1940
1941 if (argcount < type->tt_min_argcount - varargs)
1942 {
1943 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1944 return FAIL;
1945 }
1946 if (!varargs && argcount > type->tt_argcount)
1947 {
1948 emsg_funcname(e_too_many_arguments_for_function_str, name);
1949 return FAIL;
1950 }
1951 if (type->tt_args != NULL)
1952 {
1953 int i;
1954
1955 for (i = 0; i < argcount; ++i)
1956 {
1957 int offset = -argcount + i - (at_top ? 0 : 1);
1958 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1959 type_T *expected;
1960
1961 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaar36818a92023-01-03 12:33:26 +00001962 {
1963 expected = type->tt_args[type->tt_argcount - 1];
1964 if (expected != NULL && expected->tt_type == VAR_LIST)
1965 expected = expected->tt_member;
1966 if (expected == NULL)
1967 expected = &t_any;
1968 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001969 else if (i >= type->tt_min_argcount
1970 && actual->tt_type == VAR_SPECIAL)
1971 expected = &t_any;
1972 else
1973 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001974 if (need_type(actual, expected, FALSE,
1975 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01001976 {
1977 arg_type_mismatch(expected, actual, i + 1);
1978 return FAIL;
1979 }
1980 }
1981 }
1982 }
1983
1984 return OK;
1985}
1986/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001987 * Generate an ISN_PCALL instruction.
1988 * "type" is the type of the FuncRef.
1989 */
1990 int
1991generate_PCALL(
1992 cctx_T *cctx,
1993 int argcount,
1994 char_u *name,
1995 type_T *type,
1996 int at_top)
1997{
1998 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001999 type_T *ret_type;
2000
2001 RETURN_OK_IF_SKIP(cctx);
2002
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002003 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002004 ret_type = &t_any;
2005 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
2006 {
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002007 if (check_func_args_from_type(cctx, type, argcount, at_top, name)
2008 == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01002009 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002010
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002011 ret_type = type->tt_member;
2012 if (ret_type == &t_unknown)
2013 // return type not known yet, use a runtime check
2014 ret_type = &t_any;
2015 }
2016 else
2017 {
2018 semsg(_(e_not_callable_type_str), name);
2019 return FAIL;
2020 }
2021
2022 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2023 return FAIL;
2024 isn->isn_arg.pfunc.cpf_top = at_top;
2025 isn->isn_arg.pfunc.cpf_argcount = argcount;
2026
Bram Moolenaar078a4612022-01-04 15:17:03 +00002027 // drop the arguments and the funcref/partial
2028 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002029
Bram Moolenaar078a4612022-01-04 15:17:03 +00002030 // push the return value
2031 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002032
2033 // If partial is above the arguments it must be cleared and replaced with
2034 // the return value.
2035 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2036 return FAIL;
2037
2038 return OK;
2039}
2040
2041/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002042 * Generate an ISN_DEFER instruction.
2043 */
2044 int
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02002045generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002046{
2047 isn_T *isn;
2048
2049 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02002050 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002051 return FAIL;
2052 isn->isn_arg.defer.defer_var_idx = var_idx;
2053 isn->isn_arg.defer.defer_argcount = argcount;
2054 return OK;
2055}
2056
2057/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002058 * Generate an ISN_STRINGMEMBER instruction.
2059 */
2060 int
2061generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
2062{
2063 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002064 type_T *type;
2065
2066 RETURN_OK_IF_SKIP(cctx);
2067 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
2068 return FAIL;
2069 isn->isn_arg.string = vim_strnsave(name, len);
2070
2071 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00002072 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002073 if (type->tt_type != VAR_DICT
2074 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002075 {
2076 char *tofree;
2077
2078 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2079 name, type_name(type, &tofree));
2080 vim_free(tofree);
2081 return FAIL;
2082 }
2083 // change dict type to dict member type
2084 if (type->tt_type == VAR_DICT)
2085 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002086 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00002087 ? &t_any : type->tt_member;
2088 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002089 }
2090
2091 return OK;
2092}
2093
2094/*
2095 * Generate an ISN_ECHO instruction.
2096 */
2097 int
2098generate_ECHO(cctx_T *cctx, int with_white, int count)
2099{
2100 isn_T *isn;
2101
2102 RETURN_OK_IF_SKIP(cctx);
2103 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2104 return FAIL;
2105 isn->isn_arg.echo.echo_with_white = with_white;
2106 isn->isn_arg.echo.echo_count = count;
2107
2108 return OK;
2109}
2110
2111/*
2112 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
2113 */
2114 int
2115generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
2116{
2117 isn_T *isn;
2118
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01002119 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002120 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
2121 return FAIL;
2122 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002123 return OK;
2124}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002125
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002126/*
2127 * Generate an ISN_ECHOWINDOW instruction
2128 */
2129 int
2130generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
2131{
2132 isn_T *isn;
2133
2134 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2135 return FAIL;
2136 isn->isn_arg.echowin.ewin_count = count;
2137 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002138 return OK;
2139}
2140
2141/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002142 * Generate an ISN_SOURCE instruction.
2143 */
2144 int
2145generate_SOURCE(cctx_T *cctx, int sid)
2146{
2147 isn_T *isn;
2148
2149 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2150 return FAIL;
2151 isn->isn_arg.number = sid;
2152
2153 return OK;
2154}
2155
2156/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002157 * Generate an ISN_PUT instruction.
2158 */
2159 int
2160generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2161{
2162 isn_T *isn;
2163
2164 RETURN_OK_IF_SKIP(cctx);
2165 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2166 return FAIL;
2167 isn->isn_arg.put.put_regname = regname;
2168 isn->isn_arg.put.put_lnum = lnum;
2169 return OK;
2170}
2171
2172/*
Ernie Rael64885642023-10-04 20:16:22 +02002173 * Generate a LOCKUNLOCK instruction.The root item, where the indexing starts
2174 * to find the variable, is on the stack. The instr takes
2175 * - the string to parse, "root.b[idx1][idx2].d.val", to find the variable
2176 * - the class, if any, in which the string executes.
2177 * - if the root item is a function argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002178 * A copy is made of "line".
2179 */
2180 int
Ernie Raelee865f32023-09-29 19:53:55 +02002181generate_LOCKUNLOCK(cctx_T *cctx, char_u *line, int is_arg)
2182{
2183 isn_T *isn;
2184
2185 RETURN_OK_IF_SKIP(cctx);
2186 if ((isn = generate_instr(cctx, ISN_LOCKUNLOCK)) == NULL)
2187 return FAIL;
Ernie Rael64885642023-10-04 20:16:22 +02002188 class_T *cl = cctx->ctx_ufunc != NULL ? cctx->ctx_ufunc->uf_class : NULL;
2189 isn->isn_arg.lockunlock.lu_string = vim_strsave(line);
2190 isn->isn_arg.lockunlock.lu_cl_exec = cl;
2191 if (cl != NULL)
2192 ++cl->class_refcount;
2193 isn->isn_arg.lockunlock.lu_is_arg = is_arg;
Ernie Raelee865f32023-09-29 19:53:55 +02002194 return OK;
2195}
2196
2197/*
2198 * Generate an EXEC instruction that takes a string argument.
2199 * A copy is made of "line".
2200 */
2201 int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002202generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2203{
2204 isn_T *isn;
2205
2206 RETURN_OK_IF_SKIP(cctx);
2207 if ((isn = generate_instr(cctx, isntype)) == NULL)
2208 return FAIL;
2209 isn->isn_arg.string = vim_strsave(line);
2210 return OK;
2211}
2212
2213/*
2214 * Generate an EXEC instruction that takes a string argument.
2215 * "str" must be allocated, it is consumed.
2216 */
2217 int
2218generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2219{
2220 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002221 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002222
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002223 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002224 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002225 if ((isn = generate_instr(cctx, isntype)) == NULL)
2226 ret = FAIL;
2227 else
2228 {
2229 isn->isn_arg.string = str;
2230 return OK;
2231 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002232 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002233 vim_free(str);
2234 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002235}
2236
2237 int
2238generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2239{
2240 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002241
2242 RETURN_OK_IF_SKIP(cctx);
2243 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2244 return FAIL;
2245 isn->isn_arg.string = vim_strsave(line);
2246
Bram Moolenaar078a4612022-01-04 15:17:03 +00002247 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002248}
2249
2250 int
2251generate_EXECCONCAT(cctx_T *cctx, int count)
2252{
2253 isn_T *isn;
2254
2255 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2256 return FAIL;
2257 isn->isn_arg.number = count;
2258 return OK;
2259}
2260
2261/*
2262 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2263 */
2264 int
2265generate_RANGE(cctx_T *cctx, char_u *range)
2266{
2267 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002268
2269 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2270 return FAIL;
2271 isn->isn_arg.string = range;
2272
Bram Moolenaar078a4612022-01-04 15:17:03 +00002273 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002274}
2275
2276 int
2277generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2278{
2279 isn_T *isn;
2280
2281 RETURN_OK_IF_SKIP(cctx);
2282 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2283 return FAIL;
2284 isn->isn_arg.unpack.unp_count = var_count;
2285 isn->isn_arg.unpack.unp_semicolon = semicolon;
2286 return OK;
2287}
2288
2289/*
2290 * Generate an instruction for any command modifiers.
2291 */
2292 int
2293generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2294{
2295 isn_T *isn;
2296
2297 if (has_cmdmod(cmod, FALSE))
2298 {
2299 cctx->ctx_has_cmdmod = TRUE;
2300
2301 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2302 return FAIL;
2303 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2304 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2305 return FAIL;
2306 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2307 // filter program now belongs to the instruction
2308 cmod->cmod_filter_regmatch.regprog = NULL;
2309 }
2310
2311 return OK;
2312}
2313
2314 int
2315generate_undo_cmdmods(cctx_T *cctx)
2316{
2317 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2318 return FAIL;
2319 cctx->ctx_has_cmdmod = FALSE;
2320 return OK;
2321}
2322
2323/*
2324 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002325 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002326 * Return FAIL when out of memory.
2327 */
2328 int
2329generate_store_var(
2330 cctx_T *cctx,
2331 assign_dest_T dest,
2332 int opt_flags,
2333 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002334 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002335 char_u *name,
2336 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002337{
2338 switch (dest)
2339 {
2340 case dest_option:
2341 return generate_STOREOPT(cctx, ISN_STOREOPT,
2342 skip_option_env_lead(name), opt_flags);
2343 case dest_func_option:
2344 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2345 skip_option_env_lead(name), opt_flags);
2346 case dest_global:
2347 // include g: with the name, easier to execute that way
2348 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2349 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2350 case dest_buffer:
2351 // include b: with the name, easier to execute that way
2352 return generate_STORE(cctx, ISN_STOREB, 0, name);
2353 case dest_window:
2354 // include w: with the name, easier to execute that way
2355 return generate_STORE(cctx, ISN_STOREW, 0, name);
2356 case dest_tab:
2357 // include t: with the name, easier to execute that way
2358 return generate_STORE(cctx, ISN_STORET, 0, name);
2359 case dest_env:
2360 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2361 case dest_reg:
2362 return generate_STORE(cctx, ISN_STOREREG,
2363 name[1] == '@' ? '"' : name[1], NULL);
2364 case dest_vimvar:
2365 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2366 case dest_script:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002367 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002368 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2369 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2370 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002371 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002372 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002373
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002374 if (SCRIPT_ID_VALID(scriptvar_sid)
2375 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2376 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2377 == NULL)
2378 {
2379 // "import autoload './dir/script.vim'" - load script
2380 // first
2381 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2382 return FAIL;
2383 isn_type = ISN_STOREEXPORT;
2384 }
2385
2386 // "s:" may be included in the name.
2387 return generate_OLDSCRIPT(cctx, isn_type, name,
2388 scriptvar_sid, type);
2389 }
2390 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002391 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002392 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002393 case dest_class_member:
2394 return generate_CLASSMEMBER(cctx, FALSE,
2395 lhs->lhs_class, lhs->lhs_classmember_idx);
2396
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002397 case dest_local:
2398 case dest_expr:
2399 // cannot happen
2400 break;
2401 }
2402 return FAIL;
2403}
2404
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002405/*
2406 * Return TRUE when inside a "for" or "while" loop.
2407 */
2408 int
2409inside_loop_scope(cctx_T *cctx)
2410{
2411 scope_T *scope = cctx->ctx_scope;
2412
2413 for (;;)
2414 {
2415 if (scope == NULL)
2416 break;
2417 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2418 return TRUE;
2419 scope = scope->se_outer;
2420 }
2421 return FALSE;
2422}
2423
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002424 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002425generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002426{
2427 if (lhs->lhs_dest != dest_local)
2428 return generate_store_var(cctx, lhs->lhs_dest,
2429 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002430 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002431
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002432 if (lhs->lhs_lvar == NULL)
2433 return OK;
2434
2435 garray_T *instr = &cctx->ctx_instr;
2436 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2437
2438 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2439 // ISN_STORENR.
2440 // And "var = 0" does not need any instruction.
2441 if (lhs->lhs_lvar->lv_from_outer == 0
2442 && instr->ga_len == instr_count + 1
2443 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002444 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002445 varnumber_T val = isn->isn_arg.number;
2446 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002447
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002448 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002449 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002450 // zero is the default value, no need to do anything
2451 --instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002452 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002453 else
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002454 {
2455 isn->isn_type = ISN_STORENR;
2456 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2457 isn->isn_arg.storenr.stnr_val = val;
2458 }
2459 if (stack->ga_len > 0)
2460 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002461 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002462 else if (lhs->lhs_lvar->lv_from_outer > 0)
2463 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2464 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
2465 else
2466 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002467 return OK;
2468}
2469
2470#if defined(FEAT_PROFILE) || defined(PROTO)
2471 void
2472may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2473{
2474 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2475 generate_instr(cctx, ISN_PROF_END);
2476}
2477#endif
2478
2479
2480/*
2481 * Delete an instruction, free what it contains.
2482 */
2483 void
2484delete_instr(isn_T *isn)
2485{
2486 switch (isn->isn_type)
2487 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002488 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002489 case ISN_DEF:
2490 case ISN_EXEC:
2491 case ISN_EXECRANGE:
2492 case ISN_EXEC_SPLIT:
2493 case ISN_LEGACY_EVAL:
2494 case ISN_LOADAUTO:
2495 case ISN_LOADB:
2496 case ISN_LOADENV:
2497 case ISN_LOADG:
2498 case ISN_LOADOPT:
2499 case ISN_LOADT:
2500 case ISN_LOADW:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002501 case ISN_PUSHEXC:
2502 case ISN_PUSHFUNC:
2503 case ISN_PUSHS:
2504 case ISN_RANGE:
2505 case ISN_STOREAUTO:
2506 case ISN_STOREB:
2507 case ISN_STOREENV:
2508 case ISN_STOREG:
2509 case ISN_STORET:
2510 case ISN_STOREW:
2511 case ISN_STRINGMEMBER:
2512 vim_free(isn->isn_arg.string);
2513 break;
2514
Ernie Rael64885642023-10-04 20:16:22 +02002515 case ISN_LOCKUNLOCK:
2516 class_unref(isn->isn_arg.lockunlock.lu_cl_exec);
2517 vim_free(isn->isn_arg.lockunlock.lu_string);
2518 break;
2519
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002520 case ISN_SUBSTITUTE:
2521 {
2522 int idx;
2523 isn_T *list = isn->isn_arg.subs.subs_instr;
2524
2525 vim_free(isn->isn_arg.subs.subs_cmd);
2526 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2527 delete_instr(list + idx);
2528 vim_free(list);
2529 }
2530 break;
2531
2532 case ISN_INSTR:
2533 {
2534 int idx;
2535 isn_T *list = isn->isn_arg.instr;
2536
2537 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2538 delete_instr(list + idx);
2539 vim_free(list);
2540 }
2541 break;
2542
2543 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002544 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002545 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002546 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002547 vim_free(isn->isn_arg.loadstore.ls_name);
2548 break;
2549
2550 case ISN_UNLET:
2551 case ISN_UNLETENV:
2552 vim_free(isn->isn_arg.unlet.ul_name);
2553 break;
2554
2555 case ISN_STOREOPT:
2556 case ISN_STOREFUNCOPT:
2557 vim_free(isn->isn_arg.storeopt.so_name);
2558 break;
2559
2560 case ISN_PUSHBLOB: // push blob isn_arg.blob
2561 blob_unref(isn->isn_arg.blob);
2562 break;
2563
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002564 case ISN_PUSHCLASS:
Bram Moolenaar30a84472023-02-27 08:07:14 +00002565 class_unref(isn->isn_arg.classarg);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002566 break;
2567
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002568 case ISN_UCALL:
2569 vim_free(isn->isn_arg.ufunc.cuf_name);
2570 break;
2571
2572 case ISN_FUNCREF:
2573 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002574 funcref_T *funcref = &isn->isn_arg.funcref;
2575 funcref_extra_T *extra = funcref->fr_extra;
2576
2577 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002578 {
2579 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002580 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002581 ufunc_T *ufunc = dfunc->df_ufunc;
2582
2583 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2584 func_ptr_unref(ufunc);
2585 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002586 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002587 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002588 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002589
2590 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002591 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002592 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002593 vim_free(name);
2594 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00002595 if (extra->fre_class != NULL)
2596 class_unref(extra->fre_class);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002597 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002598 }
2599 }
2600 break;
2601
2602 case ISN_DCALL:
2603 {
2604 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002605 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002606
2607 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002608 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002609 func_ptr_unref(dfunc->df_ufunc);
2610 }
2611 break;
2612
Bram Moolenaard0200c82023-01-28 15:19:40 +00002613 case ISN_METHODCALL:
2614 {
2615 cmfunc_T *mfunc = isn->isn_arg.mfunc;
2616 class_unref(mfunc->cmf_itf);
2617 vim_free(mfunc);
2618 }
2619 break;
2620
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002621 case ISN_NEWFUNC:
2622 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002623 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002624
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002625 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002626 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002627 ufunc_T *ufunc = find_func_even_dead(
2628 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002629
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002630 if (ufunc != NULL)
2631 {
2632 unlink_def_function(ufunc);
2633 func_ptr_unref(ufunc);
2634 }
2635
2636 vim_free(arg->nfa_lambda);
2637 vim_free(arg->nfa_global);
2638 vim_free(arg);
2639 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002640 }
2641 break;
2642
2643 case ISN_CHECKTYPE:
2644 case ISN_SETTYPE:
2645 free_type(isn->isn_arg.type.ct_type);
2646 break;
2647
2648 case ISN_CMDMOD:
2649 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002650 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002651 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2652 break;
2653
2654 case ISN_LOADSCRIPT:
2655 case ISN_STORESCRIPT:
2656 vim_free(isn->isn_arg.script.scriptref);
2657 break;
2658
Bram Moolenaard505d172022-12-18 21:42:55 +00002659 case ISN_LOAD_CLASSMEMBER:
2660 case ISN_STORE_CLASSMEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00002661 case ISN_GET_ITF_MEMBER:
Bram Moolenaard505d172022-12-18 21:42:55 +00002662 class_unref(isn->isn_arg.classmember.cm_class);
2663 break;
2664
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002665 case ISN_STOREINDEX:
2666 class_unref(isn->isn_arg.storeindex.si_class);
2667 break;
2668
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002669 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002670 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002671 break;
2672
2673 case ISN_CEXPR_CORE:
2674 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2675 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2676 break;
2677
2678 case ISN_2BOOL:
2679 case ISN_2STRING:
2680 case ISN_2STRING_ANY:
2681 case ISN_ADDBLOB:
2682 case ISN_ADDLIST:
2683 case ISN_ANYINDEX:
2684 case ISN_ANYSLICE:
2685 case ISN_BCALL:
2686 case ISN_BLOBAPPEND:
2687 case ISN_BLOBINDEX:
2688 case ISN_BLOBSLICE:
2689 case ISN_CATCH:
2690 case ISN_CEXPR_AUCMD:
2691 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002692 case ISN_CLEARDICT:
2693 case ISN_CMDMOD_REV:
2694 case ISN_COMPAREANY:
2695 case ISN_COMPAREBLOB:
2696 case ISN_COMPAREBOOL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002697 case ISN_COMPARECLASS:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002698 case ISN_COMPAREDICT:
2699 case ISN_COMPAREFLOAT:
2700 case ISN_COMPAREFUNC:
2701 case ISN_COMPARELIST:
2702 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002703 case ISN_COMPARENULL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002704 case ISN_COMPAREOBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002705 case ISN_COMPARESPECIAL:
2706 case ISN_COMPARESTRING:
2707 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002708 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002709 case ISN_COND2BOOL:
2710 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002711 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002712 case ISN_DROP:
2713 case ISN_ECHO:
2714 case ISN_ECHOCONSOLE:
2715 case ISN_ECHOERR:
2716 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002717 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002718 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002719 case ISN_ENDTRY:
2720 case ISN_EXECCONCAT:
2721 case ISN_EXECUTE:
2722 case ISN_FINALLY:
2723 case ISN_FINISH:
2724 case ISN_FOR:
2725 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002726 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002727 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002728 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002729 case ISN_JUMP_IF_ARG_SET:
2730 case ISN_LISTAPPEND:
2731 case ISN_LISTINDEX:
2732 case ISN_LISTSLICE:
2733 case ISN_LOAD:
2734 case ISN_LOADBDICT:
2735 case ISN_LOADGDICT:
2736 case ISN_LOADOUTER:
2737 case ISN_LOADREG:
2738 case ISN_LOADTDICT:
2739 case ISN_LOADV:
2740 case ISN_LOADWDICT:
2741 case ISN_LOCKCONST:
2742 case ISN_MEMBER:
2743 case ISN_NEGATENR:
2744 case ISN_NEWDICT:
2745 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002746 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002747 case ISN_OPANY:
2748 case ISN_OPFLOAT:
2749 case ISN_OPNR:
2750 case ISN_PCALL:
2751 case ISN_PCALL_END:
2752 case ISN_PROF_END:
2753 case ISN_PROF_START:
2754 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002755 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002756 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002757 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002758 case ISN_PUSHNR:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002759 case ISN_PUSHOBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002760 case ISN_PUSHSPEC:
2761 case ISN_PUT:
2762 case ISN_REDIREND:
2763 case ISN_REDIRSTART:
2764 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002765 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002766 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002767 case ISN_SHUFFLE:
2768 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002769 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002770 case ISN_STORE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002771 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002772 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002773 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002774 case ISN_STORERANGE:
2775 case ISN_STOREREG:
2776 case ISN_STOREV:
2777 case ISN_STRINDEX:
2778 case ISN_STRSLICE:
2779 case ISN_THROW:
2780 case ISN_TRYCONT:
2781 case ISN_UNLETINDEX:
2782 case ISN_UNLETRANGE:
2783 case ISN_UNPACK:
2784 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002785 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002786 // nothing allocated
2787 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002788 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002789}
2790
2791 void
2792clear_instr_ga(garray_T *gap)
2793{
2794 int idx;
2795
2796 for (idx = 0; idx < gap->ga_len; ++idx)
2797 delete_instr(((isn_T *)gap->ga_data) + idx);
2798 ga_clear(gap);
2799}
2800
2801
2802#endif // defined(FEAT_EVAL)