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